Mercurial > mplayer.hg
annotate libmpcodecs/ve_lavc.c @ 7238:6f44ed6c4568
FreeBSD fix
author | nexus |
---|---|
date | Sun, 01 Sep 2002 23:06:15 +0000 |
parents | 5d8f64646cb5 |
children | a894e99c1e51 |
rev | line source |
---|---|
5550 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 | |
5 #include "../config.h" | |
6 | |
7 #ifdef USE_LIBAVCODEC | |
8 | |
9 #include "../mp_msg.h" | |
10 #include "../help_mp.h" | |
11 | |
12 #include "codec-cfg.h" | |
13 #include "stream.h" | |
14 #include "demuxer.h" | |
15 #include "stheader.h" | |
16 | |
17 #include "aviwrite.h" | |
18 | |
5607 | 19 #include "img_format.h" |
20 #include "mp_image.h" | |
5550 | 21 #include "vf.h" |
22 | |
6673 | 23 #ifdef HAVE_DIVX4ENCORE |
5550 | 24 #include "divx4_vbr.h" |
6673 | 25 #endif |
5550 | 26 |
7090
43d7b60bd7f4
fixing compilation without divx4 patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michael
parents:
7089
diff
changeset
|
27 extern char* passtmpfile; |
5550 | 28 extern int pass; |
7127 | 29 extern void mencoder_write_chunk(aviwrite_stream_t *s,int len,unsigned int flags); |
5550 | 30 |
31 //===========================================================================// | |
32 | |
33 #ifdef USE_LIBAVCODEC_SO | |
7004 | 34 #include <ffmpeg/avcodec.h> |
5550 | 35 #else |
36 #include "libavcodec/avcodec.h" | |
37 #endif | |
38 | |
5778 | 39 #if LIBAVCODEC_BUILD < 4601 |
40 #error your version of libavcodec is too old, get a newer one, and dont send a bugreport, THIS IS NO BUG | |
41 #endif | |
42 | |
7088 | 43 #if LIBAVCODEC_BUILD < 4620 |
44 #warning your version of libavcodec is old, u might want to get a newer one | |
45 #endif | |
46 | |
5550 | 47 extern int avcodec_inited; |
48 | |
49 /* video options */ | |
50 static char *lavc_param_vcodec = NULL; | |
51 static int lavc_param_vbitrate = -1; | |
7088 | 52 static int lavc_param_vrate_tolerance = 1000*8; |
5550 | 53 static int lavc_param_vhq = 0; /* default is realtime encoding */ |
54 static int lavc_param_v4mv = 0; | |
5556 | 55 static int lavc_param_vme = 4; |
5550 | 56 static int lavc_param_vqscale = 0; |
7088 | 57 static int lavc_param_vqmin = 2; |
58 static int lavc_param_vqmax = 31; | |
5550 | 59 static int lavc_param_vqdiff = 3; |
60 static float lavc_param_vqcompress = 0.5; | |
61 static float lavc_param_vqblur = 0.5; | |
7088 | 62 static float lavc_param_vb_qfactor = 1.25; |
63 static float lavc_param_vb_qoffset = 1.25; | |
64 static float lavc_param_vi_qfactor = 0.8; | |
65 static float lavc_param_vi_qoffset = 0.0; | |
5657
ee2efbf3dc9d
Preliminary support for lavcs b-frame encoding, disabled by default.
atmos4
parents:
5646
diff
changeset
|
66 static int lavc_param_vmax_b_frames = 0; |
5550 | 67 static int lavc_param_keyint = -1; |
5778 | 68 static int lavc_param_vpass = 0; |
69 static int lavc_param_vrc_strategy = 2; | |
70 static int lavc_param_vb_strategy = 0; | |
6255
140e94c4c89b
single coefficient elimination disabled by default
michael
parents:
6228
diff
changeset
|
71 static int lavc_param_luma_elim_threshold = 0; |
140e94c4c89b
single coefficient elimination disabled by default
michael
parents:
6228
diff
changeset
|
72 static int lavc_param_chroma_elim_threshold = 0; |
6228
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
73 static int lavc_param_packet_size= 0; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
74 static int lavc_param_strict= 0; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
75 static int lavc_param_data_partitioning= 0; |
6461 | 76 static int lavc_param_gray=0; |
7088 | 77 static float lavc_param_rc_qsquish=1.0; |
78 static float lavc_param_rc_qmod_amp=0; | |
79 static int lavc_param_rc_qmod_freq=0; | |
80 static char *lavc_param_rc_override_string=NULL; | |
81 static char *lavc_param_rc_eq="tex^qComp"; | |
82 static int lavc_param_rc_buffer_size=0; | |
83 static float lavc_param_rc_buffer_aggressivity=1.0; | |
84 static int lavc_param_rc_max_rate=0; | |
85 static int lavc_param_rc_min_rate=0; | |
86 static float lavc_param_rc_initial_cplx=0; | |
6944 | 87 static int lavc_param_mpeg_quant=0; |
7152 | 88 static int lavc_param_fdct=0; |
5550 | 89 |
90 #include "cfgparser.h" | |
91 | |
92 #ifdef USE_LIBAVCODEC | |
93 struct config lavcopts_conf[]={ | |
94 {"vcodec", &lavc_param_vcodec, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
95 {"vbitrate", &lavc_param_vbitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
96 {"vratetol", &lavc_param_vrate_tolerance, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
97 {"vhq", &lavc_param_vhq, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
98 {"v4mv", &lavc_param_v4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
99 {"vme", &lavc_param_vme, CONF_TYPE_INT, CONF_RANGE, 0, 5, NULL}, | |
100 {"vqscale", &lavc_param_vqscale, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
101 {"vqmin", &lavc_param_vqmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
102 {"vqmax", &lavc_param_vqmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
103 {"vqdiff", &lavc_param_vqdiff, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
104 {"vqcomp", &lavc_param_vqcompress, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL}, | |
105 {"vqblur", &lavc_param_vqblur, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL}, | |
7088 | 106 {"vb_qfactor", &lavc_param_vb_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL}, |
5657
ee2efbf3dc9d
Preliminary support for lavcs b-frame encoding, disabled by default.
atmos4
parents:
5646
diff
changeset
|
107 {"vmax_b_frames", &lavc_param_vmax_b_frames, CONF_TYPE_INT, CONF_RANGE, 0, FF_MAX_B_FRAMES, NULL}, |
5778 | 108 {"vpass", &lavc_param_vpass, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL}, |
7088 | 109 #if LIBAVCODEC_BUILD < 4620 |
5778 | 110 {"vrc_strategy", &lavc_param_vrc_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL}, |
7088 | 111 #endif |
5778 | 112 {"vb_strategy", &lavc_param_vb_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 1, NULL}, |
6228
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
113 #ifdef CODEC_FLAG_PART |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
114 {"vb_qoffset", &lavc_param_vb_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL}, |
7039
2eae7ac0fa8b
same behavior for luma & chroma single coeff elimination
michael
parents:
7038
diff
changeset
|
115 {"vlelim", &lavc_param_luma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL}, |
7038 | 116 {"vcelim", &lavc_param_chroma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL}, |
6228
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
117 {"vpsize", &lavc_param_packet_size, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL}, |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
118 {"vstrict", &lavc_param_strict, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
119 {"vdpart", &lavc_param_data_partitioning, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL}, |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
120 #endif |
5550 | 121 {"keyint", &lavc_param_keyint, CONF_TYPE_INT, 0, 0, 0, NULL}, |
6461 | 122 #if LIBAVCODEC_BUILD >= 4614 |
123 {"gray", &lavc_param_gray, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL}, | |
124 #endif | |
6944 | 125 #if LIBAVCODEC_BUILD >= 4619 |
126 {"mpeg_quant", &lavc_param_mpeg_quant, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
127 #endif | |
7088 | 128 #if LIBAVCODEC_BUILD >= 4620 |
129 {"vi_qfactor", &lavc_param_vi_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL}, | |
130 {"vi_qoffset", &lavc_param_vi_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL}, | |
131 {"vqsquish", &lavc_param_rc_qsquish, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL}, | |
132 {"vqmod_amp", &lavc_param_rc_qmod_amp, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL}, | |
133 {"vqmod_freq", &lavc_param_rc_qmod_freq, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
134 {"vrc_eq", &lavc_param_rc_eq, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
135 {"vrc_override", &lavc_param_rc_override_string, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
136 {"vrc_maxrate", &lavc_param_rc_max_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
137 {"vrc_minrate", &lavc_param_rc_min_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
138 {"vrc_buf_size", &lavc_param_rc_min_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
139 {"vrc_buf_aggressivity", &lavc_param_rc_buffer_aggressivity, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL}, | |
140 {"vrc_init_cplx", &lavc_param_rc_initial_cplx, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 9999999.0, NULL}, | |
141 #endif | |
7152 | 142 #if LIBAVCODEC_BUILD >= 4621 |
143 {"vfdct", &lavc_param_fdct, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL}, | |
144 #endif | |
5550 | 145 {NULL, NULL, 0, 0, 0, 0, NULL} |
146 }; | |
147 #endif | |
148 | |
149 struct vf_priv_s { | |
150 aviwrite_stream_t* mux; | |
151 AVCodecContext context; | |
152 AVCodec *codec; | |
7088 | 153 FILE *stats_file; |
5550 | 154 }; |
155 | |
7088 | 156 #define stats_file (vf->priv->stats_file) |
5550 | 157 #define mux_v (vf->priv->mux) |
158 #define lavc_venc_context (vf->priv->context) | |
159 | |
160 static int config(struct vf_instance_s* vf, | |
161 int width, int height, int d_width, int d_height, | |
162 unsigned int flags, unsigned int outfmt){ | |
7088 | 163 int size, i; |
164 void *p; | |
165 | |
5550 | 166 mux_v->bih->biWidth=width; |
167 mux_v->bih->biHeight=height; | |
168 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8); | |
169 | |
170 memset(&lavc_venc_context, 0, sizeof(lavc_venc_context)); | |
171 | |
172 printf("videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n", | |
173 mux_v->bih->biWidth, mux_v->bih->biHeight, mux_v->bih->biCompression, | |
174 (char *)&mux_v->bih->biCompression); | |
175 | |
176 lavc_venc_context.width = width; | |
177 lavc_venc_context.height = height; | |
5677
a21cab74cde8
bitrate>16000 means bits not kbits - noticed by George Hawkins <george_hawkins@yahoo.com>
arpi
parents:
5657
diff
changeset
|
178 if (lavc_param_vbitrate > 16000) /* != -1 */ |
a21cab74cde8
bitrate>16000 means bits not kbits - noticed by George Hawkins <george_hawkins@yahoo.com>
arpi
parents:
5657
diff
changeset
|
179 lavc_venc_context.bit_rate = lavc_param_vbitrate; |
a21cab74cde8
bitrate>16000 means bits not kbits - noticed by George Hawkins <george_hawkins@yahoo.com>
arpi
parents:
5657
diff
changeset
|
180 else if (lavc_param_vbitrate >= 0) /* != -1 */ |
5550 | 181 lavc_venc_context.bit_rate = lavc_param_vbitrate*1000; |
182 else | |
183 lavc_venc_context.bit_rate = 800000; /* default */ | |
184 lavc_venc_context.bit_rate_tolerance= lavc_param_vrate_tolerance*1000; | |
185 lavc_venc_context.frame_rate = (float)mux_v->h.dwRate/mux_v->h.dwScale * FRAME_RATE_BASE; | |
186 lavc_venc_context.qmin= lavc_param_vqmin; | |
187 lavc_venc_context.qmax= lavc_param_vqmax; | |
188 lavc_venc_context.max_qdiff= lavc_param_vqdiff; | |
189 lavc_venc_context.qcompress= lavc_param_vqcompress; | |
190 lavc_venc_context.qblur= lavc_param_vqblur; | |
5657
ee2efbf3dc9d
Preliminary support for lavcs b-frame encoding, disabled by default.
atmos4
parents:
5646
diff
changeset
|
191 lavc_venc_context.max_b_frames= lavc_param_vmax_b_frames; |
5778 | 192 lavc_venc_context.b_quant_factor= lavc_param_vb_qfactor; |
193 lavc_venc_context.rc_strategy= lavc_param_vrc_strategy; | |
194 lavc_venc_context.b_frame_strategy= lavc_param_vb_strategy; | |
6228
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
195 |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
196 #ifdef CODEC_FLAG_PART |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
197 lavc_venc_context.b_quant_offset= lavc_param_vb_qoffset; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
198 lavc_venc_context.luma_elim_threshold= lavc_param_luma_elim_threshold; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
199 lavc_venc_context.chroma_elim_threshold= lavc_param_chroma_elim_threshold; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
200 lavc_venc_context.rtp_payload_size= lavc_param_packet_size; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
201 if(lavc_param_packet_size )lavc_venc_context.rtp_mode=1; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
202 lavc_venc_context.strict_std_compliance= lavc_param_strict; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
203 #endif |
7088 | 204 #if LIBAVCODEC_BUILD >= 4620 |
205 lavc_venc_context.i_quant_factor= lavc_param_vi_qfactor; | |
206 lavc_venc_context.i_quant_offset= lavc_param_vi_qoffset; | |
207 lavc_venc_context.rc_qsquish= lavc_param_rc_qsquish; | |
208 lavc_venc_context.rc_qmod_amp= lavc_param_rc_qmod_amp; | |
209 lavc_venc_context.rc_qmod_freq= lavc_param_rc_qmod_freq; | |
210 lavc_venc_context.rc_eq= lavc_param_rc_eq; | |
211 lavc_venc_context.rc_max_rate= lavc_param_rc_max_rate*1000; | |
212 lavc_venc_context.rc_min_rate= lavc_param_rc_min_rate*1000; | |
213 lavc_venc_context.rc_buffer_size= lavc_param_rc_buffer_size*1000; | |
214 lavc_venc_context.rc_buffer_aggressivity= lavc_param_rc_buffer_aggressivity; | |
215 lavc_venc_context.rc_initial_cplx= lavc_param_rc_initial_cplx; | |
7152 | 216 |
7088 | 217 p= lavc_param_rc_override_string; |
218 for(i=0; p; i++){ | |
219 int start, end, q; | |
220 int e=sscanf(p, "%d,%d,%d", &start, &end, &q); | |
221 if(e!=3){ | |
222 mp_msg(MSGT_MENCODER,MSGL_ERR,"error parsing vrc_q\n"); | |
223 return 0; | |
224 } | |
225 lavc_venc_context.rc_override= | |
226 realloc(lavc_venc_context.rc_override, sizeof(RcOverride)*(i+1)); | |
227 lavc_venc_context.rc_override[i].start_frame= start; | |
228 lavc_venc_context.rc_override[i].end_frame = end; | |
229 if(q>0){ | |
230 lavc_venc_context.rc_override[i].qscale= q; | |
231 lavc_venc_context.rc_override[i].quality_factor= 1.0; | |
232 } | |
233 else{ | |
234 lavc_venc_context.rc_override[i].qscale= 0; | |
235 lavc_venc_context.rc_override[i].quality_factor= -q/100.0; | |
236 } | |
237 p= strchr(p, '/'); | |
238 if(p) p++; | |
239 } | |
240 lavc_venc_context.rc_override_count=i; | |
241 #endif | |
242 | |
6944 | 243 #if LIBAVCODEC_BUILD >= 4619 |
244 lavc_venc_context.mpeg_quant=lavc_param_mpeg_quant; | |
245 #endif | |
7152 | 246 |
247 #if LIBAVCODEC_BUILD >= 4621 | |
248 lavc_venc_context.dct_algo= lavc_param_fdct; | |
249 #endif | |
250 | |
5550 | 251 /* keyframe interval */ |
252 if (lavc_param_keyint >= 0) /* != -1 */ | |
253 lavc_venc_context.gop_size = lavc_param_keyint; | |
254 else | |
255 lavc_venc_context.gop_size = 250; /* default */ | |
256 | |
257 if (lavc_param_vhq) | |
258 { | |
259 printf("High quality encoding selected (non real time)!\n"); | |
260 lavc_venc_context.flags = CODEC_FLAG_HQ; | |
261 } | |
262 else | |
263 lavc_venc_context.flags = 0; | |
264 | |
5781
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
265 /* 4mv is currently buggy with B frames */ |
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
266 if (lavc_param_vmax_b_frames > 0 && lavc_param_v4mv) { |
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
267 printf("4MV with B-Frames not yet supported -> 4MV disabled\n"); |
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
268 lavc_param_v4mv = 0; |
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
269 } |
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
270 |
5550 | 271 lavc_venc_context.flags|= lavc_param_v4mv ? CODEC_FLAG_4MV : 0; |
6228
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
272 #ifdef CODEC_FLAG_PART |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
273 lavc_venc_context.flags|= lavc_param_data_partitioning; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
274 #endif |
6461 | 275 #if LIBAVCODEC_BUILD >= 4614 |
276 if(lavc_param_gray) lavc_venc_context.flags|= CODEC_FLAG_GRAY; | |
277 #endif | |
278 | |
5550 | 279 |
5778 | 280 /* lavc internal 2pass bitrate control */ |
6673 | 281 #ifdef HAVE_DIVX4ENCORE |
282 switch(lavc_param_vpass){ | |
283 #else | |
284 switch(lavc_param_vpass?lavc_param_vpass:pass){ | |
285 #endif | |
7088 | 286 case 1: |
287 lavc_venc_context.flags|= CODEC_FLAG_PASS1; | |
7089 | 288 #if LIBAVCODEC_BUILD >= 4620 |
7088 | 289 stats_file= fopen(passtmpfile, "w"); |
290 if(stats_file==NULL){ | |
291 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
292 return 0; | |
293 } | |
7089 | 294 #endif |
7088 | 295 break; |
296 case 2: | |
297 lavc_venc_context.flags|= CODEC_FLAG_PASS2; | |
7089 | 298 #if LIBAVCODEC_BUILD >= 4620 |
7088 | 299 stats_file= fopen(passtmpfile, "r"); |
300 if(stats_file==NULL){ | |
301 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
302 return 0; | |
303 } | |
304 fseek(stats_file, 0, SEEK_END); | |
305 size= ftell(stats_file); | |
306 fseek(stats_file, 0, SEEK_SET); | |
307 | |
308 lavc_venc_context.stats_in= malloc(size + 1); | |
309 lavc_venc_context.stats_in[size]=0; | |
310 | |
311 if(fread(lavc_venc_context.stats_in, size, 1, stats_file)<1){ | |
312 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: reading from filename=%s\n", passtmpfile); | |
313 return 0; | |
7089 | 314 } |
315 #endif | |
7088 | 316 break; |
6673 | 317 } |
5778 | 318 |
5624 | 319 #ifdef ME_ZERO |
320 // workaround Juanjo's stupid incompatible change: | |
5550 | 321 motion_estimation_method = lavc_param_vme; |
5624 | 322 #else |
323 lavc_venc_context.me_method = ME_ZERO+lavc_param_vme; | |
324 #endif | |
5550 | 325 |
326 /* fixed qscale :p */ | |
327 if (lavc_param_vqscale) | |
328 { | |
329 printf("Using constant qscale = %d (VBR)\n", lavc_param_vqscale); | |
330 lavc_venc_context.flags |= CODEC_FLAG_QSCALE; | |
331 lavc_venc_context.quality = lavc_param_vqscale; | |
332 } | |
333 | |
6673 | 334 #ifdef HAVE_DIVX4ENCORE |
5550 | 335 switch(pass){ |
336 case 1: | |
337 if (VbrControl_init_2pass_vbr_analysis(passtmpfile, 5) == -1){ | |
338 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
339 pass=0; | |
5781
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
340 } |
5550 | 341 break; |
342 case 2: | |
343 if (VbrControl_init_2pass_vbr_encoding(passtmpfile, | |
344 lavc_venc_context.bit_rate, | |
345 (float)mux_v->h.dwRate/mux_v->h.dwScale, | |
346 100, /* crispness */ | |
347 5) == -1){ | |
348 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
349 pass=0; | |
5632 | 350 } else |
351 lavc_venc_context.flags|=CODEC_FLAG_QSCALE|CODEC_FLAG_TYPE; // VBR | |
5550 | 352 break; |
353 } | |
6673 | 354 #endif |
5550 | 355 |
356 if (avcodec_open(&lavc_venc_context, vf->priv->codec) != 0) { | |
357 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec); | |
358 return 0; | |
359 } | |
360 | |
361 if (lavc_venc_context.codec->encode == NULL) { | |
362 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n"); | |
363 return 0; | |
364 } | |
7088 | 365 |
366 #if LIBAVCODEC_BUILD >= 4620 | |
367 /* free second pass buffer, its not needed anymore */ | |
368 if(lavc_venc_context.stats_in) free(lavc_venc_context.stats_in); | |
369 lavc_venc_context.stats_in= NULL; | |
370 #endif | |
371 | |
5550 | 372 return 1; |
373 } | |
374 | |
375 static int control(struct vf_instance_s* vf, int request, void* data){ | |
376 | |
377 return CONTROL_UNKNOWN; | |
378 } | |
379 | |
380 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
381 switch(fmt){ | |
382 case IMGFMT_YV12: | |
383 case IMGFMT_IYUV: | |
384 case IMGFMT_I420: | |
5565
0b301fec999a
capabilities support -> automatic insertion of scale, expand, pp
arpi
parents:
5556
diff
changeset
|
385 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE; |
5550 | 386 } |
387 return 0; | |
388 } | |
389 | |
390 static void put_image(struct vf_instance_s* vf, mp_image_t *mpi){ | |
391 int out_size; | |
392 AVPicture lavc_venc_picture; | |
393 | |
394 lavc_venc_picture.data[0]=mpi->planes[0]; | |
395 lavc_venc_picture.data[1]=mpi->planes[1]; | |
396 lavc_venc_picture.data[2]=mpi->planes[2]; | |
397 lavc_venc_picture.linesize[0]=mpi->stride[0]; | |
398 lavc_venc_picture.linesize[1]=mpi->stride[1]; | |
399 lavc_venc_picture.linesize[2]=mpi->stride[2]; | |
400 | |
6673 | 401 #ifdef HAVE_DIVX4ENCORE |
5550 | 402 if(pass==2){ // handle 2-pass: |
403 lavc_venc_context.flags|=CODEC_FLAG_QSCALE; // enable VBR | |
404 lavc_venc_context.quality=VbrControl_get_quant(); | |
405 lavc_venc_context.key_frame=VbrControl_get_intra(); | |
406 lavc_venc_context.gop_size=0x3fffffff; | |
407 out_size = avcodec_encode_video(&lavc_venc_context, mux_v->buffer, mux_v->buffer_size, | |
408 &lavc_venc_picture); | |
409 VbrControl_update_2pass_vbr_encoding(lavc_venc_context.mv_bits, | |
410 lavc_venc_context.i_tex_bits+lavc_venc_context.p_tex_bits, | |
411 8*out_size); | |
6673 | 412 } else |
413 #endif | |
414 { | |
5550 | 415 out_size = avcodec_encode_video(&lavc_venc_context, mux_v->buffer, mux_v->buffer_size, |
416 &lavc_venc_picture); | |
6673 | 417 #ifdef HAVE_DIVX4ENCORE |
5550 | 418 if(pass==1){ |
419 VbrControl_update_2pass_vbr_analysis(lavc_venc_context.key_frame, | |
420 lavc_venc_context.mv_bits, | |
421 lavc_venc_context.i_tex_bits+lavc_venc_context.p_tex_bits, | |
422 8*out_size, lavc_venc_context.quality); | |
423 } | |
6673 | 424 #endif |
5550 | 425 } |
426 | |
427 mencoder_write_chunk(mux_v,out_size,lavc_venc_context.key_frame?0x10:0); | |
7088 | 428 |
429 #if LIBAVCODEC_BUILD >= 4620 | |
430 /* store stats if there are any */ | |
431 if(lavc_venc_context.stats_out && stats_file) | |
432 fprintf(stats_file, "%s", lavc_venc_context.stats_out); | |
433 #endif | |
5550 | 434 } |
435 | |
5551 | 436 static void uninit(struct vf_instance_s* vf){ |
437 avcodec_close(&lavc_venc_context); | |
7088 | 438 |
7089 | 439 #if LIBAVCODEC_BUILD >= 4620 |
7088 | 440 if(stats_file) fclose(stats_file); |
7089 | 441 |
7088 | 442 /* free rc_override */ |
443 if(lavc_venc_context.rc_override) free(lavc_venc_context.rc_override); | |
444 lavc_venc_context.rc_override= NULL; | |
445 #endif | |
5551 | 446 } |
447 | |
5550 | 448 //===========================================================================// |
449 | |
450 static int vf_open(vf_instance_t *vf, char* args){ | |
5551 | 451 vf->uninit=uninit; |
5550 | 452 vf->config=config; |
453 vf->control=control; | |
454 vf->query_format=query_format; | |
455 vf->put_image=put_image; | |
456 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
457 memset(vf->priv,0,sizeof(struct vf_priv_s)); | |
7127 | 458 vf->priv->mux=(aviwrite_stream_t*)args; |
5550 | 459 |
5977
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
460 /* XXX: hack: some of the MJPEG decoder DLL's needs exported huffman |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
461 table, so we define a zero-table, also lavc mjpeg encoder is putting |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
462 huffman tables into the stream, so no problem */ |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
463 if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "mjpeg")) |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
464 { |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
465 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+28); |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
466 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+28); |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
467 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+28; |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
468 } |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
469 else |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
470 { |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
471 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)); |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
472 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)); |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
473 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER); |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
474 } |
5550 | 475 mux_v->bih->biWidth=0; |
476 mux_v->bih->biHeight=0; | |
477 mux_v->bih->biPlanes=1; | |
478 mux_v->bih->biBitCount=24; | |
479 if (!lavc_param_vcodec) | |
480 { | |
5977
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
481 printf("No libavcodec codec specified! It's required!\n"); |
5550 | 482 return 0; |
483 } | |
484 | |
485 if (!strcasecmp(lavc_param_vcodec, "mpeg1") || !strcasecmp(lavc_param_vcodec, "mpeg1video")) | |
486 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '1'); | |
487 else if (!strcasecmp(lavc_param_vcodec, "h263") || !strcasecmp(lavc_param_vcodec, "h263p")) | |
488 mux_v->bih->biCompression = mmioFOURCC('h', '2', '6', '3'); | |
489 else if (!strcasecmp(lavc_param_vcodec, "rv10")) | |
490 mux_v->bih->biCompression = mmioFOURCC('R', 'V', '1', '0'); | |
491 else if (!strcasecmp(lavc_param_vcodec, "mjpeg")) | |
492 mux_v->bih->biCompression = mmioFOURCC('M', 'J', 'P', 'G'); | |
493 else if (!strcasecmp(lavc_param_vcodec, "mpeg4")) | |
494 mux_v->bih->biCompression = mmioFOURCC('D', 'I', 'V', 'X'); | |
495 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4")) | |
496 mux_v->bih->biCompression = mmioFOURCC('d', 'i', 'v', '3'); | |
5972 | 497 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4v2")) |
498 mux_v->bih->biCompression = mmioFOURCC('M', 'P', '4', '2'); | |
6461 | 499 else if (!strcasecmp(lavc_param_vcodec, "wmv1")) |
500 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '1'); | |
5550 | 501 else |
502 mux_v->bih->biCompression = mmioFOURCC(lavc_param_vcodec[0], | |
503 lavc_param_vcodec[1], lavc_param_vcodec[2], lavc_param_vcodec[3]); /* FIXME!!! */ | |
504 | |
505 if (!avcodec_inited){ | |
506 avcodec_init(); | |
507 avcodec_register_all(); | |
508 avcodec_inited=1; | |
509 } | |
510 | |
511 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name(lavc_param_vcodec); | |
512 if (!vf->priv->codec) { | |
513 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, lavc_param_vcodec); | |
514 return 0; | |
515 } | |
516 | |
517 return 1; | |
518 } | |
519 | |
520 vf_info_t ve_info_lavc = { | |
521 "libavcodec encoder", | |
522 "lavc", | |
6673 | 523 "A'rpi, Alex, Michael", |
5550 | 524 "for internal use by mencoder", |
525 vf_open | |
526 }; | |
527 | |
528 //===========================================================================// | |
529 #endif |