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