Mercurial > mplayer.hg
annotate libmpcodecs/ve_lavc.c @ 7127:1e47c2e7aa8e
mostly compiler warning fixes, some small bugfix
patch by Dominik Mierzejewski <dominik@rangers.eu.org>
author | arpi |
---|---|
date | Wed, 28 Aug 2002 22:45:48 +0000 |
parents | 43d7b60bd7f4 |
children | 5d8f64646cb5 |
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; |
5550 | 88 |
89 #include "cfgparser.h" | |
90 | |
91 #ifdef USE_LIBAVCODEC | |
92 struct config lavcopts_conf[]={ | |
93 {"vcodec", &lavc_param_vcodec, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
94 {"vbitrate", &lavc_param_vbitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
95 {"vratetol", &lavc_param_vrate_tolerance, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
96 {"vhq", &lavc_param_vhq, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
97 {"v4mv", &lavc_param_v4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
98 {"vme", &lavc_param_vme, CONF_TYPE_INT, CONF_RANGE, 0, 5, NULL}, | |
99 {"vqscale", &lavc_param_vqscale, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
100 {"vqmin", &lavc_param_vqmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
101 {"vqmax", &lavc_param_vqmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
102 {"vqdiff", &lavc_param_vqdiff, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
103 {"vqcomp", &lavc_param_vqcompress, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL}, | |
104 {"vqblur", &lavc_param_vqblur, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL}, | |
7088 | 105 {"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
|
106 {"vmax_b_frames", &lavc_param_vmax_b_frames, CONF_TYPE_INT, CONF_RANGE, 0, FF_MAX_B_FRAMES, NULL}, |
5778 | 107 {"vpass", &lavc_param_vpass, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL}, |
7088 | 108 #if LIBAVCODEC_BUILD < 4620 |
5778 | 109 {"vrc_strategy", &lavc_param_vrc_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL}, |
7088 | 110 #endif |
5778 | 111 {"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
|
112 #ifdef CODEC_FLAG_PART |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
113 {"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
|
114 {"vlelim", &lavc_param_luma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL}, |
7038 | 115 {"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
|
116 {"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
|
117 {"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
|
118 {"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
|
119 #endif |
5550 | 120 {"keyint", &lavc_param_keyint, CONF_TYPE_INT, 0, 0, 0, NULL}, |
6461 | 121 #if LIBAVCODEC_BUILD >= 4614 |
122 {"gray", &lavc_param_gray, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL}, | |
123 #endif | |
6944 | 124 #if LIBAVCODEC_BUILD >= 4619 |
125 {"mpeg_quant", &lavc_param_mpeg_quant, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
126 #endif | |
7088 | 127 #if LIBAVCODEC_BUILD >= 4620 |
128 {"vi_qfactor", &lavc_param_vi_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL}, | |
129 {"vi_qoffset", &lavc_param_vi_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL}, | |
130 {"vqsquish", &lavc_param_rc_qsquish, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL}, | |
131 {"vqmod_amp", &lavc_param_rc_qmod_amp, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL}, | |
132 {"vqmod_freq", &lavc_param_rc_qmod_freq, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
133 {"vrc_eq", &lavc_param_rc_eq, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
134 {"vrc_override", &lavc_param_rc_override_string, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
135 {"vrc_maxrate", &lavc_param_rc_max_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
136 {"vrc_minrate", &lavc_param_rc_min_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
137 {"vrc_buf_size", &lavc_param_rc_min_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
138 {"vrc_buf_aggressivity", &lavc_param_rc_buffer_aggressivity, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL}, | |
139 {"vrc_init_cplx", &lavc_param_rc_initial_cplx, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 9999999.0, NULL}, | |
140 #endif | |
5550 | 141 {NULL, NULL, 0, 0, 0, 0, NULL} |
142 }; | |
143 #endif | |
144 | |
145 struct vf_priv_s { | |
146 aviwrite_stream_t* mux; | |
147 AVCodecContext context; | |
148 AVCodec *codec; | |
7088 | 149 FILE *stats_file; |
5550 | 150 }; |
151 | |
7088 | 152 #define stats_file (vf->priv->stats_file) |
5550 | 153 #define mux_v (vf->priv->mux) |
154 #define lavc_venc_context (vf->priv->context) | |
155 | |
156 static int config(struct vf_instance_s* vf, | |
157 int width, int height, int d_width, int d_height, | |
158 unsigned int flags, unsigned int outfmt){ | |
7088 | 159 int size, i; |
160 void *p; | |
161 | |
5550 | 162 mux_v->bih->biWidth=width; |
163 mux_v->bih->biHeight=height; | |
164 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8); | |
165 | |
166 memset(&lavc_venc_context, 0, sizeof(lavc_venc_context)); | |
167 | |
168 printf("videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n", | |
169 mux_v->bih->biWidth, mux_v->bih->biHeight, mux_v->bih->biCompression, | |
170 (char *)&mux_v->bih->biCompression); | |
171 | |
172 lavc_venc_context.width = width; | |
173 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
|
174 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
|
175 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
|
176 else if (lavc_param_vbitrate >= 0) /* != -1 */ |
5550 | 177 lavc_venc_context.bit_rate = lavc_param_vbitrate*1000; |
178 else | |
179 lavc_venc_context.bit_rate = 800000; /* default */ | |
180 lavc_venc_context.bit_rate_tolerance= lavc_param_vrate_tolerance*1000; | |
181 lavc_venc_context.frame_rate = (float)mux_v->h.dwRate/mux_v->h.dwScale * FRAME_RATE_BASE; | |
182 lavc_venc_context.qmin= lavc_param_vqmin; | |
183 lavc_venc_context.qmax= lavc_param_vqmax; | |
184 lavc_venc_context.max_qdiff= lavc_param_vqdiff; | |
185 lavc_venc_context.qcompress= lavc_param_vqcompress; | |
186 lavc_venc_context.qblur= lavc_param_vqblur; | |
5657
ee2efbf3dc9d
Preliminary support for lavcs b-frame encoding, disabled by default.
atmos4
parents:
5646
diff
changeset
|
187 lavc_venc_context.max_b_frames= lavc_param_vmax_b_frames; |
5778 | 188 lavc_venc_context.b_quant_factor= lavc_param_vb_qfactor; |
189 lavc_venc_context.rc_strategy= lavc_param_vrc_strategy; | |
190 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
|
191 |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
192 #ifdef CODEC_FLAG_PART |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 lavc_venc_context.strict_std_compliance= lavc_param_strict; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
199 #endif |
7088 | 200 #if LIBAVCODEC_BUILD >= 4620 |
201 lavc_venc_context.i_quant_factor= lavc_param_vi_qfactor; | |
202 lavc_venc_context.i_quant_offset= lavc_param_vi_qoffset; | |
203 lavc_venc_context.rc_qsquish= lavc_param_rc_qsquish; | |
204 lavc_venc_context.rc_qmod_amp= lavc_param_rc_qmod_amp; | |
205 lavc_venc_context.rc_qmod_freq= lavc_param_rc_qmod_freq; | |
206 lavc_venc_context.rc_eq= lavc_param_rc_eq; | |
207 lavc_venc_context.rc_max_rate= lavc_param_rc_max_rate*1000; | |
208 lavc_venc_context.rc_min_rate= lavc_param_rc_min_rate*1000; | |
209 lavc_venc_context.rc_buffer_size= lavc_param_rc_buffer_size*1000; | |
210 lavc_venc_context.rc_buffer_aggressivity= lavc_param_rc_buffer_aggressivity; | |
211 lavc_venc_context.rc_initial_cplx= lavc_param_rc_initial_cplx; | |
212 p= lavc_param_rc_override_string; | |
213 for(i=0; p; i++){ | |
214 int start, end, q; | |
215 int e=sscanf(p, "%d,%d,%d", &start, &end, &q); | |
216 if(e!=3){ | |
217 mp_msg(MSGT_MENCODER,MSGL_ERR,"error parsing vrc_q\n"); | |
218 return 0; | |
219 } | |
220 lavc_venc_context.rc_override= | |
221 realloc(lavc_venc_context.rc_override, sizeof(RcOverride)*(i+1)); | |
222 lavc_venc_context.rc_override[i].start_frame= start; | |
223 lavc_venc_context.rc_override[i].end_frame = end; | |
224 if(q>0){ | |
225 lavc_venc_context.rc_override[i].qscale= q; | |
226 lavc_venc_context.rc_override[i].quality_factor= 1.0; | |
227 } | |
228 else{ | |
229 lavc_venc_context.rc_override[i].qscale= 0; | |
230 lavc_venc_context.rc_override[i].quality_factor= -q/100.0; | |
231 } | |
232 p= strchr(p, '/'); | |
233 if(p) p++; | |
234 } | |
235 lavc_venc_context.rc_override_count=i; | |
236 #endif | |
237 | |
6944 | 238 #if LIBAVCODEC_BUILD >= 4619 |
239 lavc_venc_context.mpeg_quant=lavc_param_mpeg_quant; | |
240 #endif | |
241 | |
5550 | 242 /* keyframe interval */ |
243 if (lavc_param_keyint >= 0) /* != -1 */ | |
244 lavc_venc_context.gop_size = lavc_param_keyint; | |
245 else | |
246 lavc_venc_context.gop_size = 250; /* default */ | |
247 | |
248 if (lavc_param_vhq) | |
249 { | |
250 printf("High quality encoding selected (non real time)!\n"); | |
251 lavc_venc_context.flags = CODEC_FLAG_HQ; | |
252 } | |
253 else | |
254 lavc_venc_context.flags = 0; | |
255 | |
5781
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
256 /* 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
|
257 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
|
258 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
|
259 lavc_param_v4mv = 0; |
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
260 } |
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
261 |
5550 | 262 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
|
263 #ifdef CODEC_FLAG_PART |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
264 lavc_venc_context.flags|= lavc_param_data_partitioning; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
265 #endif |
6461 | 266 #if LIBAVCODEC_BUILD >= 4614 |
267 if(lavc_param_gray) lavc_venc_context.flags|= CODEC_FLAG_GRAY; | |
268 #endif | |
269 | |
5550 | 270 |
5778 | 271 /* lavc internal 2pass bitrate control */ |
6673 | 272 #ifdef HAVE_DIVX4ENCORE |
273 switch(lavc_param_vpass){ | |
274 #else | |
275 switch(lavc_param_vpass?lavc_param_vpass:pass){ | |
276 #endif | |
7088 | 277 case 1: |
278 lavc_venc_context.flags|= CODEC_FLAG_PASS1; | |
7089 | 279 #if LIBAVCODEC_BUILD >= 4620 |
7088 | 280 stats_file= fopen(passtmpfile, "w"); |
281 if(stats_file==NULL){ | |
282 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
283 return 0; | |
284 } | |
7089 | 285 #endif |
7088 | 286 break; |
287 case 2: | |
288 lavc_venc_context.flags|= CODEC_FLAG_PASS2; | |
7089 | 289 #if LIBAVCODEC_BUILD >= 4620 |
7088 | 290 stats_file= fopen(passtmpfile, "r"); |
291 if(stats_file==NULL){ | |
292 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
293 return 0; | |
294 } | |
295 fseek(stats_file, 0, SEEK_END); | |
296 size= ftell(stats_file); | |
297 fseek(stats_file, 0, SEEK_SET); | |
298 | |
299 lavc_venc_context.stats_in= malloc(size + 1); | |
300 lavc_venc_context.stats_in[size]=0; | |
301 | |
302 if(fread(lavc_venc_context.stats_in, size, 1, stats_file)<1){ | |
303 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: reading from filename=%s\n", passtmpfile); | |
304 return 0; | |
7089 | 305 } |
306 #endif | |
7088 | 307 break; |
6673 | 308 } |
5778 | 309 |
5624 | 310 #ifdef ME_ZERO |
311 // workaround Juanjo's stupid incompatible change: | |
5550 | 312 motion_estimation_method = lavc_param_vme; |
5624 | 313 #else |
314 lavc_venc_context.me_method = ME_ZERO+lavc_param_vme; | |
315 #endif | |
5550 | 316 |
317 /* fixed qscale :p */ | |
318 if (lavc_param_vqscale) | |
319 { | |
320 printf("Using constant qscale = %d (VBR)\n", lavc_param_vqscale); | |
321 lavc_venc_context.flags |= CODEC_FLAG_QSCALE; | |
322 lavc_venc_context.quality = lavc_param_vqscale; | |
323 } | |
324 | |
6673 | 325 #ifdef HAVE_DIVX4ENCORE |
5550 | 326 switch(pass){ |
327 case 1: | |
328 if (VbrControl_init_2pass_vbr_analysis(passtmpfile, 5) == -1){ | |
329 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
330 pass=0; | |
5781
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
331 } |
5550 | 332 break; |
333 case 2: | |
334 if (VbrControl_init_2pass_vbr_encoding(passtmpfile, | |
335 lavc_venc_context.bit_rate, | |
336 (float)mux_v->h.dwRate/mux_v->h.dwScale, | |
337 100, /* crispness */ | |
338 5) == -1){ | |
339 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
340 pass=0; | |
5632 | 341 } else |
342 lavc_venc_context.flags|=CODEC_FLAG_QSCALE|CODEC_FLAG_TYPE; // VBR | |
5550 | 343 break; |
344 } | |
6673 | 345 #endif |
5550 | 346 |
347 if (avcodec_open(&lavc_venc_context, vf->priv->codec) != 0) { | |
348 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec); | |
349 return 0; | |
350 } | |
351 | |
352 if (lavc_venc_context.codec->encode == NULL) { | |
353 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n"); | |
354 return 0; | |
355 } | |
7088 | 356 |
357 #if LIBAVCODEC_BUILD >= 4620 | |
358 /* free second pass buffer, its not needed anymore */ | |
359 if(lavc_venc_context.stats_in) free(lavc_venc_context.stats_in); | |
360 lavc_venc_context.stats_in= NULL; | |
361 #endif | |
362 | |
5550 | 363 return 1; |
364 } | |
365 | |
366 static int control(struct vf_instance_s* vf, int request, void* data){ | |
367 | |
368 return CONTROL_UNKNOWN; | |
369 } | |
370 | |
371 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
372 switch(fmt){ | |
373 case IMGFMT_YV12: | |
374 case IMGFMT_IYUV: | |
375 case IMGFMT_I420: | |
5565
0b301fec999a
capabilities support -> automatic insertion of scale, expand, pp
arpi
parents:
5556
diff
changeset
|
376 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE; |
5550 | 377 } |
378 return 0; | |
379 } | |
380 | |
381 static void put_image(struct vf_instance_s* vf, mp_image_t *mpi){ | |
382 int out_size; | |
383 AVPicture lavc_venc_picture; | |
384 | |
385 lavc_venc_picture.data[0]=mpi->planes[0]; | |
386 lavc_venc_picture.data[1]=mpi->planes[1]; | |
387 lavc_venc_picture.data[2]=mpi->planes[2]; | |
388 lavc_venc_picture.linesize[0]=mpi->stride[0]; | |
389 lavc_venc_picture.linesize[1]=mpi->stride[1]; | |
390 lavc_venc_picture.linesize[2]=mpi->stride[2]; | |
391 | |
6673 | 392 #ifdef HAVE_DIVX4ENCORE |
5550 | 393 if(pass==2){ // handle 2-pass: |
394 lavc_venc_context.flags|=CODEC_FLAG_QSCALE; // enable VBR | |
395 lavc_venc_context.quality=VbrControl_get_quant(); | |
396 lavc_venc_context.key_frame=VbrControl_get_intra(); | |
397 lavc_venc_context.gop_size=0x3fffffff; | |
398 out_size = avcodec_encode_video(&lavc_venc_context, mux_v->buffer, mux_v->buffer_size, | |
399 &lavc_venc_picture); | |
400 VbrControl_update_2pass_vbr_encoding(lavc_venc_context.mv_bits, | |
401 lavc_venc_context.i_tex_bits+lavc_venc_context.p_tex_bits, | |
402 8*out_size); | |
6673 | 403 } else |
404 #endif | |
405 { | |
5550 | 406 out_size = avcodec_encode_video(&lavc_venc_context, mux_v->buffer, mux_v->buffer_size, |
407 &lavc_venc_picture); | |
6673 | 408 #ifdef HAVE_DIVX4ENCORE |
5550 | 409 if(pass==1){ |
410 VbrControl_update_2pass_vbr_analysis(lavc_venc_context.key_frame, | |
411 lavc_venc_context.mv_bits, | |
412 lavc_venc_context.i_tex_bits+lavc_venc_context.p_tex_bits, | |
413 8*out_size, lavc_venc_context.quality); | |
414 } | |
6673 | 415 #endif |
5550 | 416 } |
417 | |
418 mencoder_write_chunk(mux_v,out_size,lavc_venc_context.key_frame?0x10:0); | |
7088 | 419 |
420 #if LIBAVCODEC_BUILD >= 4620 | |
421 /* store stats if there are any */ | |
422 if(lavc_venc_context.stats_out && stats_file) | |
423 fprintf(stats_file, "%s", lavc_venc_context.stats_out); | |
424 #endif | |
5550 | 425 } |
426 | |
5551 | 427 static void uninit(struct vf_instance_s* vf){ |
428 avcodec_close(&lavc_venc_context); | |
7088 | 429 |
7089 | 430 #if LIBAVCODEC_BUILD >= 4620 |
7088 | 431 if(stats_file) fclose(stats_file); |
7089 | 432 |
7088 | 433 /* free rc_override */ |
434 if(lavc_venc_context.rc_override) free(lavc_venc_context.rc_override); | |
435 lavc_venc_context.rc_override= NULL; | |
436 #endif | |
5551 | 437 } |
438 | |
5550 | 439 //===========================================================================// |
440 | |
441 static int vf_open(vf_instance_t *vf, char* args){ | |
5551 | 442 vf->uninit=uninit; |
5550 | 443 vf->config=config; |
444 vf->control=control; | |
445 vf->query_format=query_format; | |
446 vf->put_image=put_image; | |
447 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
448 memset(vf->priv,0,sizeof(struct vf_priv_s)); | |
7127 | 449 vf->priv->mux=(aviwrite_stream_t*)args; |
5550 | 450 |
5977
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
451 /* 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
|
452 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
|
453 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
|
454 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
|
455 { |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
456 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
|
457 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
|
458 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
|
459 } |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
460 else |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
461 { |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
462 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)); |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
463 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
|
464 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER); |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
465 } |
5550 | 466 mux_v->bih->biWidth=0; |
467 mux_v->bih->biHeight=0; | |
468 mux_v->bih->biPlanes=1; | |
469 mux_v->bih->biBitCount=24; | |
470 if (!lavc_param_vcodec) | |
471 { | |
5977
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
472 printf("No libavcodec codec specified! It's required!\n"); |
5550 | 473 return 0; |
474 } | |
475 | |
476 if (!strcasecmp(lavc_param_vcodec, "mpeg1") || !strcasecmp(lavc_param_vcodec, "mpeg1video")) | |
477 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '1'); | |
478 else if (!strcasecmp(lavc_param_vcodec, "h263") || !strcasecmp(lavc_param_vcodec, "h263p")) | |
479 mux_v->bih->biCompression = mmioFOURCC('h', '2', '6', '3'); | |
480 else if (!strcasecmp(lavc_param_vcodec, "rv10")) | |
481 mux_v->bih->biCompression = mmioFOURCC('R', 'V', '1', '0'); | |
482 else if (!strcasecmp(lavc_param_vcodec, "mjpeg")) | |
483 mux_v->bih->biCompression = mmioFOURCC('M', 'J', 'P', 'G'); | |
484 else if (!strcasecmp(lavc_param_vcodec, "mpeg4")) | |
485 mux_v->bih->biCompression = mmioFOURCC('D', 'I', 'V', 'X'); | |
486 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4")) | |
487 mux_v->bih->biCompression = mmioFOURCC('d', 'i', 'v', '3'); | |
5972 | 488 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4v2")) |
489 mux_v->bih->biCompression = mmioFOURCC('M', 'P', '4', '2'); | |
6461 | 490 else if (!strcasecmp(lavc_param_vcodec, "wmv1")) |
491 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '1'); | |
5550 | 492 else |
493 mux_v->bih->biCompression = mmioFOURCC(lavc_param_vcodec[0], | |
494 lavc_param_vcodec[1], lavc_param_vcodec[2], lavc_param_vcodec[3]); /* FIXME!!! */ | |
495 | |
496 if (!avcodec_inited){ | |
497 avcodec_init(); | |
498 avcodec_register_all(); | |
499 avcodec_inited=1; | |
500 } | |
501 | |
502 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name(lavc_param_vcodec); | |
503 if (!vf->priv->codec) { | |
504 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, lavc_param_vcodec); | |
505 return 0; | |
506 } | |
507 | |
508 return 1; | |
509 } | |
510 | |
511 vf_info_t ve_info_lavc = { | |
512 "libavcodec encoder", | |
513 "lavc", | |
6673 | 514 "A'rpi, Alex, Michael", |
5550 | 515 "for internal use by mencoder", |
516 vf_open | |
517 }; | |
518 | |
519 //===========================================================================// | |
520 #endif |