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