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