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