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