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