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