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