Mercurial > mplayer.hg
annotate libmpcodecs/ve_lavc.c @ 11442:8fae2ef16894
seek in realmedia files
author | luran |
---|---|
date | Wed, 12 Nov 2003 10:33:27 +0000 |
parents | 3544f1823f6b |
children | 36bbf719cfa6 |
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; |
5550 | 30 |
31 //===========================================================================// | |
32 | |
33 #ifdef USE_LIBAVCODEC_SO | |
7004 | 34 #include <ffmpeg/avcodec.h> |
5550 | 35 #else |
36 #include "libavcodec/avcodec.h" | |
37 #endif | |
38 | |
8339 | 39 #if LIBAVCODEC_BUILD < 4641 |
11000 | 40 #error we do not support libavcodec prior to build 4641, get the latest libavcodec CVS |
5778 | 41 #endif |
42 | |
9508 | 43 #if LIBAVCODEC_BUILD < 4659 |
7088 | 44 #warning your version of libavcodec is old, u might want to get a newer one |
45 #endif | |
46 | |
8413 | 47 #if LIBAVCODEC_BUILD < 4645 |
48 #define AVFrame AVVideoFrame | |
49 #define coded_frame coded_picture | |
50 #endif | |
51 | |
5550 | 52 extern int avcodec_inited; |
53 | |
54 /* video options */ | |
7555 | 55 static char *lavc_param_vcodec = "mpeg4"; |
5550 | 56 static int lavc_param_vbitrate = -1; |
7088 | 57 static int lavc_param_vrate_tolerance = 1000*8; |
10498 | 58 static int lavc_param_mb_decision = 0; /* default is realtime encoding */ |
5550 | 59 static int lavc_param_v4mv = 0; |
5556 | 60 static int lavc_param_vme = 4; |
11032 | 61 static float lavc_param_vqscale = 0.0; |
7088 | 62 static int lavc_param_vqmin = 2; |
63 static int lavc_param_vqmax = 31; | |
8492 | 64 static int lavc_param_mb_qmin = 2; |
65 static int lavc_param_mb_qmax = 31; | |
11032 | 66 static float lavc_param_lmin = 2; |
67 static float lavc_param_lmax = 31; | |
5550 | 68 static int lavc_param_vqdiff = 3; |
69 static float lavc_param_vqcompress = 0.5; | |
70 static float lavc_param_vqblur = 0.5; | |
7088 | 71 static float lavc_param_vb_qfactor = 1.25; |
72 static float lavc_param_vb_qoffset = 1.25; | |
73 static float lavc_param_vi_qfactor = 0.8; | |
74 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
|
75 static int lavc_param_vmax_b_frames = 0; |
5550 | 76 static int lavc_param_keyint = -1; |
5778 | 77 static int lavc_param_vpass = 0; |
78 static int lavc_param_vrc_strategy = 2; | |
79 static int lavc_param_vb_strategy = 0; | |
6255
140e94c4c89b
single coefficient elimination disabled by default
michael
parents:
6228
diff
changeset
|
80 static int lavc_param_luma_elim_threshold = 0; |
140e94c4c89b
single coefficient elimination disabled by default
michael
parents:
6228
diff
changeset
|
81 static int lavc_param_chroma_elim_threshold = 0; |
6228
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
82 static int lavc_param_packet_size= 0; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
83 static int lavc_param_strict= 0; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
84 static int lavc_param_data_partitioning= 0; |
6461 | 85 static int lavc_param_gray=0; |
7088 | 86 static float lavc_param_rc_qsquish=1.0; |
87 static float lavc_param_rc_qmod_amp=0; | |
88 static int lavc_param_rc_qmod_freq=0; | |
89 static char *lavc_param_rc_override_string=NULL; | |
90 static char *lavc_param_rc_eq="tex^qComp"; | |
91 static int lavc_param_rc_buffer_size=0; | |
92 static float lavc_param_rc_buffer_aggressivity=1.0; | |
93 static int lavc_param_rc_max_rate=0; | |
94 static int lavc_param_rc_min_rate=0; | |
95 static float lavc_param_rc_initial_cplx=0; | |
6944 | 96 static int lavc_param_mpeg_quant=0; |
7152 | 97 static int lavc_param_fdct=0; |
7564 | 98 static int lavc_param_idct=0; |
8237 | 99 static char* lavc_param_aspect = NULL; |
9993
6e7f3643bee6
optional automatic aspect encoding based on d_width and d_height
rfelker
parents:
9868
diff
changeset
|
100 static int lavc_param_autoaspect = 0; |
7490 | 101 static float lavc_param_lumi_masking= 0.0; |
7496 | 102 static float lavc_param_dark_masking= 0.0; |
7490 | 103 static float lavc_param_temporal_cplx_masking= 0.0; |
104 static float lavc_param_spatial_cplx_masking= 0.0; | |
105 static float lavc_param_p_masking= 0.0; | |
106 static int lavc_param_normalize_aqp= 0; | |
7504 | 107 static int lavc_param_interlaced_dct= 0; |
8190 | 108 static int lavc_param_prediction_method= FF_PRED_LEFT; |
10778 | 109 static int lavc_param_format= IMGFMT_YV12; |
8341 | 110 static int lavc_param_debug= 0; |
8347 | 111 static int lavc_param_psnr= 0; |
8803 | 112 static int lavc_param_me_pre_cmp= 0; |
8587 | 113 static int lavc_param_me_cmp= 0; |
114 static int lavc_param_me_sub_cmp= 0; | |
115 static int lavc_param_mb_cmp= 0; | |
8803 | 116 static int lavc_param_pre_dia_size= 0; |
8587 | 117 static int lavc_param_dia_size= 0; |
118 static int lavc_param_qpel= 0; | |
8684 | 119 static int lavc_param_trell= 0; |
9536 | 120 static int lavc_param_aic= 0; |
121 static int lavc_param_umv= 0; | |
8695 | 122 static int lavc_param_last_pred= 0; |
8709 | 123 static int lavc_param_pre_me= 1; |
8803 | 124 static int lavc_param_me_subpel_quality= 8; |
9508 | 125 static int lavc_param_me_range= 0; |
9645 | 126 #if LIBAVCODEC_BUILD >= 4663 |
127 static int lavc_param_ibias= FF_DEFAULT_QUANT_BIAS; | |
128 static int lavc_param_pbias= FF_DEFAULT_QUANT_BIAS; | |
129 #endif | |
10291 | 130 static int lavc_param_coder= 0; |
131 static int lavc_param_context= 0; | |
10779 | 132 static char *lavc_param_intra_matrix = NULL; |
133 static char *lavc_param_inter_matrix = NULL; | |
10963 | 134 static int lavc_param_cbp= 0; |
10974 | 135 static int lavc_param_mv0= 0; |
11363 | 136 static int lavc_param_noise_reduction= 0; |
5550 | 137 |
11375 | 138 char *lavc_param_acodec = "mp2"; |
139 int lavc_param_atag = 0; | |
140 int lavc_param_abitrate = 224; | |
141 | |
10594
57bdcdb061d7
Removed the historic cfgparser and switched full to the new config parser (altought some macros still remain for compatibility). As a side effect 90% of the warning messages are gone from the core. Things should be cleaner now and less confusing for newbies.
alex
parents:
10498
diff
changeset
|
142 #include "m_option.h" |
5550 | 143 |
144 #ifdef USE_LIBAVCODEC | |
10594
57bdcdb061d7
Removed the historic cfgparser and switched full to the new config parser (altought some macros still remain for compatibility). As a side effect 90% of the warning messages are gone from the core. Things should be cleaner now and less confusing for newbies.
alex
parents:
10498
diff
changeset
|
145 m_option_t lavcopts_conf[]={ |
11375 | 146 {"acodec", &lavc_param_acodec, CONF_TYPE_STRING, 0, 0, 0, NULL}, |
147 {"abitrate", &lavc_param_abitrate, CONF_TYPE_INT, CONF_RANGE, 1, 1000, NULL}, | |
148 {"atag", &lavc_param_atag, CONF_TYPE_INT, CONF_RANGE, 0, 0xffff, NULL}, | |
5550 | 149 {"vcodec", &lavc_param_vcodec, CONF_TYPE_STRING, 0, 0, 0, NULL}, |
150 {"vbitrate", &lavc_param_vbitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
151 {"vratetol", &lavc_param_vrate_tolerance, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
10498 | 152 {"vhq", &lavc_param_mb_decision, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
153 {"mbd", &lavc_param_mb_decision, CONF_TYPE_INT, CONF_RANGE, 0, 9, NULL}, | |
5550 | 154 {"v4mv", &lavc_param_v4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
155 {"vme", &lavc_param_vme, CONF_TYPE_INT, CONF_RANGE, 0, 5, NULL}, | |
11032 | 156 {"vqscale", &lavc_param_vqscale, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL}, |
5550 | 157 {"vqmin", &lavc_param_vqmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, |
158 {"vqmax", &lavc_param_vqmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
8492 | 159 {"mbqmin", &lavc_param_mb_qmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, |
160 {"mbqmax", &lavc_param_mb_qmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
11032 | 161 {"lmin", &lavc_param_lmin, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL}, |
162 {"lmax", &lavc_param_lmax, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL}, | |
5550 | 163 {"vqdiff", &lavc_param_vqdiff, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, |
164 {"vqcomp", &lavc_param_vqcompress, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL}, | |
165 {"vqblur", &lavc_param_vqblur, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL}, | |
7088 | 166 {"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
|
167 {"vmax_b_frames", &lavc_param_vmax_b_frames, CONF_TYPE_INT, CONF_RANGE, 0, FF_MAX_B_FRAMES, NULL}, |
5778 | 168 {"vpass", &lavc_param_vpass, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL}, |
169 {"vrc_strategy", &lavc_param_vrc_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL}, | |
8413 | 170 {"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
|
171 {"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
|
172 {"vlelim", &lavc_param_luma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL}, |
7038 | 173 {"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
|
174 {"vpsize", &lavc_param_packet_size, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL}, |
8195 | 175 {"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
|
176 {"vdpart", &lavc_param_data_partitioning, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL}, |
5550 | 177 {"keyint", &lavc_param_keyint, CONF_TYPE_INT, 0, 0, 0, NULL}, |
6461 | 178 {"gray", &lavc_param_gray, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL}, |
6944 | 179 {"mpeg_quant", &lavc_param_mpeg_quant, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
7088 | 180 {"vi_qfactor", &lavc_param_vi_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL}, |
181 {"vi_qoffset", &lavc_param_vi_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL}, | |
182 {"vqsquish", &lavc_param_rc_qsquish, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL}, | |
183 {"vqmod_amp", &lavc_param_rc_qmod_amp, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL}, | |
184 {"vqmod_freq", &lavc_param_rc_qmod_freq, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
185 {"vrc_eq", &lavc_param_rc_eq, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
186 {"vrc_override", &lavc_param_rc_override_string, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
187 {"vrc_maxrate", &lavc_param_rc_max_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
188 {"vrc_minrate", &lavc_param_rc_min_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
11245
9a3230a5ef38
100l patch by (Tilmann Bitterberg <transcode at tibit dot org>)
michael
parents:
11195
diff
changeset
|
189 {"vrc_buf_size", &lavc_param_rc_buffer_size, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, |
7088 | 190 {"vrc_buf_aggressivity", &lavc_param_rc_buffer_aggressivity, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL}, |
191 {"vrc_init_cplx", &lavc_param_rc_initial_cplx, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 9999999.0, NULL}, | |
7152 | 192 {"vfdct", &lavc_param_fdct, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL}, |
8237 | 193 {"aspect", &lavc_param_aspect, CONF_TYPE_STRING, 0, 0, 0, NULL}, |
9993
6e7f3643bee6
optional automatic aspect encoding based on d_width and d_height
rfelker
parents:
9868
diff
changeset
|
194 {"autoaspect", &lavc_param_autoaspect, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
7490 | 195 {"lumi_mask", &lavc_param_lumi_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL}, |
196 {"tcplx_mask", &lavc_param_temporal_cplx_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL}, | |
197 {"scplx_mask", &lavc_param_spatial_cplx_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL}, | |
198 {"p_mask", &lavc_param_p_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL}, | |
199 {"naq", &lavc_param_normalize_aqp, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
7496 | 200 {"dark_mask", &lavc_param_dark_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL}, |
7504 | 201 {"ildct", &lavc_param_interlaced_dct, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
7564 | 202 {"idct", &lavc_param_idct, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL}, |
8190 | 203 {"pred", &lavc_param_prediction_method, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL}, |
10778 | 204 {"format", &lavc_param_format, CONF_TYPE_IMGFMT, 0, 0, 0, NULL}, |
8341 | 205 #if LIBAVCODEC_BUILD >= 4642 |
206 {"debug", &lavc_param_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL}, | |
207 #endif | |
8347 | 208 #if LIBAVCODEC_BUILD >= 4643 |
209 {"psnr", &lavc_param_psnr, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PSNR, NULL}, | |
210 #endif | |
8803 | 211 {"precmp", &lavc_param_me_pre_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL}, |
8587 | 212 {"cmp", &lavc_param_me_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL}, |
213 {"subcmp", &lavc_param_me_sub_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL}, | |
214 {"mbcmp", &lavc_param_mb_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL}, | |
8803 | 215 {"predia", &lavc_param_pre_dia_size, CONF_TYPE_INT, CONF_RANGE, -2000, 2000, NULL}, |
8684 | 216 {"dia", &lavc_param_dia_size, CONF_TYPE_INT, CONF_RANGE, -2000, 2000, NULL}, |
8587 | 217 {"qpel", &lavc_param_qpel, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_QPEL, NULL}, |
8684 | 218 #if LIBAVCODEC_BUILD >= 4648 |
219 {"trell", &lavc_param_trell, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_TRELLIS_QUANT, NULL}, | |
220 #endif | |
8695 | 221 {"last_pred", &lavc_param_last_pred, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL}, |
8709 | 222 {"preme", &lavc_param_pre_me, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL}, |
8803 | 223 {"subq", &lavc_param_me_subpel_quality, CONF_TYPE_INT, CONF_RANGE, 0, 8, NULL}, |
9508 | 224 {"me_range", &lavc_param_me_range, CONF_TYPE_INT, CONF_RANGE, 0, 16000, NULL}, |
9536 | 225 #ifdef CODEC_FLAG_H263P_AIC |
226 {"aic", &lavc_param_aic, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_AIC, NULL}, | |
227 {"umv", &lavc_param_umv, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_UMV, NULL}, | |
228 #endif | |
9645 | 229 #if LIBAVCODEC_BUILD >= 4663 |
230 {"ibias", &lavc_param_ibias, CONF_TYPE_INT, CONF_RANGE, -512, 512, NULL}, | |
231 {"pbias", &lavc_param_pbias, CONF_TYPE_INT, CONF_RANGE, -512, 512, NULL}, | |
232 #endif | |
10291 | 233 #if LIBAVCODEC_BUILD >= 4669 |
234 {"coder", &lavc_param_coder, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL}, | |
235 {"context", &lavc_param_context, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL}, | |
236 #endif | |
10779 | 237 #if LIBAVCODEC_BUILD >= 4675 |
238 {"intra_matrix", &lavc_param_intra_matrix, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
239 {"inter_matrix", &lavc_param_inter_matrix, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
240 #endif | |
10963 | 241 #if LIBAVCODEC_BUILD >= 4681 |
242 {"cbp", &lavc_param_cbp, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_CBP_RD, NULL}, | |
243 #endif | |
10974 | 244 #if LIBAVCODEC_BUILD >= 4683 |
245 {"mv0", &lavc_param_mv0, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_MV0, NULL}, | |
246 #endif | |
11363 | 247 {"nr", &lavc_param_noise_reduction, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL}, |
5550 | 248 {NULL, NULL, 0, 0, 0, 0, NULL} |
249 }; | |
250 #endif | |
251 | |
252 struct vf_priv_s { | |
8585 | 253 muxer_stream_t* mux; |
7444 | 254 AVCodecContext *context; |
8413 | 255 AVFrame *pic; |
5550 | 256 AVCodec *codec; |
7088 | 257 FILE *stats_file; |
5550 | 258 }; |
259 | |
7088 | 260 #define stats_file (vf->priv->stats_file) |
5550 | 261 #define mux_v (vf->priv->mux) |
262 #define lavc_venc_context (vf->priv->context) | |
263 | |
11032 | 264 #if LIBAVCODEC_BUILD < 4684 |
265 #define FF_QP2LAMBDA 1 | |
266 #endif | |
267 | |
5550 | 268 static int config(struct vf_instance_s* vf, |
269 int width, int height, int d_width, int d_height, | |
270 unsigned int flags, unsigned int outfmt){ | |
7088 | 271 int size, i; |
272 void *p; | |
273 | |
5550 | 274 mux_v->bih->biWidth=width; |
275 mux_v->bih->biHeight=height; | |
276 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8); | |
277 | |
278 printf("videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n", | |
279 mux_v->bih->biWidth, mux_v->bih->biHeight, mux_v->bih->biCompression, | |
280 (char *)&mux_v->bih->biCompression); | |
281 | |
7444 | 282 lavc_venc_context->width = width; |
283 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
|
284 if (lavc_param_vbitrate > 16000) /* != -1 */ |
7444 | 285 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
|
286 else if (lavc_param_vbitrate >= 0) /* != -1 */ |
7444 | 287 lavc_venc_context->bit_rate = lavc_param_vbitrate*1000; |
5550 | 288 else |
7444 | 289 lavc_venc_context->bit_rate = 800000; /* default */ |
290 lavc_venc_context->bit_rate_tolerance= lavc_param_vrate_tolerance*1000; | |
9571 | 291 #if LIBAVCODEC_BUILD >= 4662 |
292 lavc_venc_context->frame_rate = mux_v->h.dwRate; | |
293 lavc_venc_context->frame_rate_base = mux_v->h.dwScale; | |
294 #else | |
295 lavc_venc_context->frame_rate = mux_v->h.dwRate*FRAME_RATE_BASE/mux_v->h.dwScale; | |
296 #endif | |
7444 | 297 lavc_venc_context->qmin= lavc_param_vqmin; |
298 lavc_venc_context->qmax= lavc_param_vqmax; | |
8492 | 299 #if LIBAVCODEC_BUILD >= 4646 |
300 lavc_venc_context->mb_qmin= lavc_param_mb_qmin; | |
301 lavc_venc_context->mb_qmax= lavc_param_mb_qmax; | |
302 #endif | |
11032 | 303 #if LIBAVCODEC_BUILD >= 4684 |
304 lavc_venc_context->lmin= (int)(FF_QP2LAMBDA * lavc_param_lmin + 0.5); | |
305 lavc_venc_context->lmax= (int)(FF_QP2LAMBDA * lavc_param_lmax + 0.5); | |
306 #endif | |
7444 | 307 lavc_venc_context->max_qdiff= lavc_param_vqdiff; |
308 lavc_venc_context->qcompress= lavc_param_vqcompress; | |
309 lavc_venc_context->qblur= lavc_param_vqblur; | |
310 lavc_venc_context->max_b_frames= lavc_param_vmax_b_frames; | |
311 lavc_venc_context->b_quant_factor= lavc_param_vb_qfactor; | |
312 lavc_venc_context->rc_strategy= lavc_param_vrc_strategy; | |
313 lavc_venc_context->b_frame_strategy= lavc_param_vb_strategy; | |
314 lavc_venc_context->b_quant_offset= lavc_param_vb_qoffset; | |
315 lavc_venc_context->luma_elim_threshold= lavc_param_luma_elim_threshold; | |
316 lavc_venc_context->chroma_elim_threshold= lavc_param_chroma_elim_threshold; | |
317 lavc_venc_context->rtp_payload_size= lavc_param_packet_size; | |
318 if(lavc_param_packet_size )lavc_venc_context->rtp_mode=1; | |
319 lavc_venc_context->strict_std_compliance= lavc_param_strict; | |
320 lavc_venc_context->i_quant_factor= lavc_param_vi_qfactor; | |
321 lavc_venc_context->i_quant_offset= lavc_param_vi_qoffset; | |
322 lavc_venc_context->rc_qsquish= lavc_param_rc_qsquish; | |
323 lavc_venc_context->rc_qmod_amp= lavc_param_rc_qmod_amp; | |
324 lavc_venc_context->rc_qmod_freq= lavc_param_rc_qmod_freq; | |
325 lavc_venc_context->rc_eq= lavc_param_rc_eq; | |
326 lavc_venc_context->rc_max_rate= lavc_param_rc_max_rate*1000; | |
327 lavc_venc_context->rc_min_rate= lavc_param_rc_min_rate*1000; | |
328 lavc_venc_context->rc_buffer_size= lavc_param_rc_buffer_size*1000; | |
329 lavc_venc_context->rc_buffer_aggressivity= lavc_param_rc_buffer_aggressivity; | |
330 lavc_venc_context->rc_initial_cplx= lavc_param_rc_initial_cplx; | |
8341 | 331 #if LIBAVCODEC_BUILD >= 4642 |
332 lavc_venc_context->debug= lavc_param_debug; | |
333 #endif | |
8695 | 334 #if LIBAVCODEC_BUILD >= 4649 |
335 lavc_venc_context->last_predictor_count= lavc_param_last_pred; | |
336 #endif | |
8709 | 337 #if LIBAVCODEC_BUILD >= 4650 |
338 lavc_venc_context->pre_me= lavc_param_pre_me; | |
339 #endif | |
8803 | 340 #if LIBAVCODEC_BUILD >= 4651 |
341 lavc_venc_context->me_pre_cmp= lavc_param_me_pre_cmp; | |
342 lavc_venc_context->pre_dia_size= lavc_param_pre_dia_size; | |
343 #endif | |
344 #if LIBAVCODEC_BUILD >= 4652 | |
345 lavc_venc_context->me_subpel_quality= lavc_param_me_subpel_quality; | |
346 #endif | |
9508 | 347 #if LIBAVCODEC_BUILD >= 4659 |
348 lavc_venc_context->me_range= lavc_param_me_range; | |
349 #endif | |
9645 | 350 #if LIBAVCODEC_BUILD >= 4663 |
351 lavc_venc_context->intra_quant_bias= lavc_param_ibias; | |
352 lavc_venc_context->inter_quant_bias= lavc_param_pbias; | |
353 #endif | |
10291 | 354 #if LIBAVCODEC_BUILD >= 4669 |
355 lavc_venc_context->coder_type= lavc_param_coder; | |
356 lavc_venc_context->context_model= lavc_param_context; | |
357 #endif | |
11363 | 358 #if LIBAVCODEC_BUILD >= 4690 |
359 lavc_venc_context->noise_reduction= lavc_param_noise_reduction; | |
360 #endif | |
10779 | 361 #if LIBAVCODEC_BUILD >= 4675 |
362 if (lavc_param_intra_matrix) | |
363 { | |
364 char *tmp; | |
365 | |
366 lavc_venc_context->intra_matrix = | |
367 malloc(sizeof(*lavc_venc_context->intra_matrix)*64); | |
368 | |
369 i = 0; | |
370 while ((tmp = strsep(&lavc_param_intra_matrix, ",")) && (i < 64)) | |
371 { | |
372 if (!tmp || (tmp && !strlen(tmp))) | |
373 break; | |
374 lavc_venc_context->intra_matrix[i++] = atoi(tmp); | |
375 } | |
376 | |
377 if (i != 64) | |
378 { | |
379 free(lavc_venc_context->intra_matrix); | |
380 lavc_venc_context->intra_matrix = NULL; | |
381 } | |
382 else | |
383 mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified intra matrix\n"); | |
384 } | |
385 if (lavc_param_inter_matrix) | |
386 { | |
387 char *tmp; | |
388 | |
389 lavc_venc_context->inter_matrix = | |
390 malloc(sizeof(*lavc_venc_context->inter_matrix)*64); | |
391 | |
392 i = 0; | |
393 while ((tmp = strsep(&lavc_param_inter_matrix, ",")) && (i < 64)) | |
394 { | |
395 if (!tmp || (tmp && !strlen(tmp))) | |
396 break; | |
397 lavc_venc_context->inter_matrix[i++] = atoi(tmp); | |
398 } | |
399 | |
400 if (i != 64) | |
401 { | |
402 free(lavc_venc_context->inter_matrix); | |
403 lavc_venc_context->inter_matrix = NULL; | |
404 } | |
405 else | |
406 mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified inter matrix\n"); | |
407 } | |
408 #endif | |
9508 | 409 |
7088 | 410 p= lavc_param_rc_override_string; |
411 for(i=0; p; i++){ | |
412 int start, end, q; | |
413 int e=sscanf(p, "%d,%d,%d", &start, &end, &q); | |
414 if(e!=3){ | |
415 mp_msg(MSGT_MENCODER,MSGL_ERR,"error parsing vrc_q\n"); | |
416 return 0; | |
417 } | |
7444 | 418 lavc_venc_context->rc_override= |
419 realloc(lavc_venc_context->rc_override, sizeof(RcOverride)*(i+1)); | |
420 lavc_venc_context->rc_override[i].start_frame= start; | |
421 lavc_venc_context->rc_override[i].end_frame = end; | |
7088 | 422 if(q>0){ |
7444 | 423 lavc_venc_context->rc_override[i].qscale= q; |
424 lavc_venc_context->rc_override[i].quality_factor= 1.0; | |
7088 | 425 } |
426 else{ | |
7444 | 427 lavc_venc_context->rc_override[i].qscale= 0; |
428 lavc_venc_context->rc_override[i].quality_factor= -q/100.0; | |
7088 | 429 } |
430 p= strchr(p, '/'); | |
431 if(p) p++; | |
432 } | |
7444 | 433 lavc_venc_context->rc_override_count=i; |
7088 | 434 |
7444 | 435 lavc_venc_context->mpeg_quant=lavc_param_mpeg_quant; |
7152 | 436 |
7444 | 437 lavc_venc_context->dct_algo= lavc_param_fdct; |
7564 | 438 lavc_venc_context->idct_algo= lavc_param_idct; |
7152 | 439 |
7490 | 440 lavc_venc_context->lumi_masking= lavc_param_lumi_masking; |
441 lavc_venc_context->temporal_cplx_masking= lavc_param_temporal_cplx_masking; | |
442 lavc_venc_context->spatial_cplx_masking= lavc_param_spatial_cplx_masking; | |
443 lavc_venc_context->p_masking= lavc_param_p_masking; | |
7496 | 444 lavc_venc_context->dark_masking= lavc_param_dark_masking; |
7490 | 445 |
8237 | 446 if (lavc_param_aspect != NULL) |
7389
6f3dd9df2cc2
aspect ratio support by encoding (currently only with mpeg4 and h263p)
alex
parents:
7368
diff
changeset
|
447 { |
8237 | 448 int par_width, par_height, e; |
449 float ratio=0; | |
450 | |
451 e= sscanf (lavc_param_aspect, "%d/%d", &par_width, &par_height); | |
452 if(e==2){ | |
453 if(par_height) | |
454 ratio= (float)par_width / (float)par_height; | |
455 }else{ | |
456 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
|
457 } |
8237 | 458 |
459 if (e && ratio > 0.1 && ratio < 10.0) { | |
11194 | 460 #if LIBAVCODEC_BUILD >= 4687 |
11195
9bfd1b5d38cd
mpeg4 has only 8bit per numerator and denominator ...
michael
parents:
11194
diff
changeset
|
461 lavc_venc_context->sample_aspect_ratio= av_d2q(ratio * height / width, 255); |
9bfd1b5d38cd
mpeg4 has only 8bit per numerator and denominator ...
michael
parents:
11194
diff
changeset
|
462 mp_dbg(MSGT_MENCODER, MSGL_DBG2, "sample_aspect_ratio: %d/%d\n", |
9bfd1b5d38cd
mpeg4 has only 8bit per numerator and denominator ...
michael
parents:
11194
diff
changeset
|
463 lavc_venc_context->sample_aspect_ratio.num, |
9bfd1b5d38cd
mpeg4 has only 8bit per numerator and denominator ...
michael
parents:
11194
diff
changeset
|
464 lavc_venc_context->sample_aspect_ratio.den); |
11194 | 465 #else |
8237 | 466 lavc_venc_context->aspect_ratio= ratio; |
11194 | 467 #endif |
8237 | 468 mp_dbg(MSGT_MENCODER, MSGL_DBG2, "aspect_ratio: %f\n", ratio); |
469 } else { | |
470 mp_dbg(MSGT_MENCODER, MSGL_ERR, "aspect ratio: cannot parse \"%s\"\n", lavc_param_aspect); | |
471 return 0; | |
7389
6f3dd9df2cc2
aspect ratio support by encoding (currently only with mpeg4 and h263p)
alex
parents:
7368
diff
changeset
|
472 } |
6f3dd9df2cc2
aspect ratio support by encoding (currently only with mpeg4 and h263p)
alex
parents:
7368
diff
changeset
|
473 } |
9993
6e7f3643bee6
optional automatic aspect encoding based on d_width and d_height
rfelker
parents:
9868
diff
changeset
|
474 else if (lavc_param_autoaspect) |
11194 | 475 #if LIBAVCODEC_BUILD >= 4687 |
11369 | 476 lavc_venc_context->sample_aspect_ratio = av_d2q((float)d_width/d_height*height / width, 255); |
11194 | 477 #else |
9993
6e7f3643bee6
optional automatic aspect encoding based on d_width and d_height
rfelker
parents:
9868
diff
changeset
|
478 lavc_venc_context->aspect_ratio = (float)d_width/d_height; |
11194 | 479 #endif |
7389
6f3dd9df2cc2
aspect ratio support by encoding (currently only with mpeg4 and h263p)
alex
parents:
7368
diff
changeset
|
480 |
5550 | 481 /* keyframe interval */ |
482 if (lavc_param_keyint >= 0) /* != -1 */ | |
7444 | 483 lavc_venc_context->gop_size = lavc_param_keyint; |
5550 | 484 else |
7444 | 485 lavc_venc_context->gop_size = 250; /* default */ |
5550 | 486 |
10498 | 487 lavc_venc_context->flags = 0; |
488 if (lavc_param_mb_decision) | |
5550 | 489 { |
490 printf("High quality encoding selected (non real time)!\n"); | |
10498 | 491 #if LIBAVCODEC_BUILD < 4673 |
492 lavc_venc_context->flags = CODEC_FLAG_HQ; | |
493 #else | |
494 lavc_venc_context->mb_decision= lavc_param_mb_decision; | |
495 #endif | |
5550 | 496 } |
497 | |
8587 | 498 #if LIBAVCODEC_BUILD >= 4647 |
499 lavc_venc_context->me_cmp= lavc_param_me_cmp; | |
500 lavc_venc_context->me_sub_cmp= lavc_param_me_sub_cmp; | |
501 lavc_venc_context->mb_cmp= lavc_param_mb_cmp; | |
502 lavc_venc_context->dia_size= lavc_param_dia_size; | |
503 lavc_venc_context->flags|= lavc_param_qpel; | |
504 #endif | |
8684 | 505 #if LIBAVCODEC_BUILD >= 4648 |
506 lavc_venc_context->flags|= lavc_param_trell; | |
507 #endif | |
9536 | 508 lavc_venc_context->flags|= lavc_param_aic; |
509 lavc_venc_context->flags|= lavc_param_umv; | |
7444 | 510 lavc_venc_context->flags|= lavc_param_v4mv ? CODEC_FLAG_4MV : 0; |
511 lavc_venc_context->flags|= lavc_param_data_partitioning; | |
10963 | 512 lavc_venc_context->flags|= lavc_param_cbp; |
10974 | 513 lavc_venc_context->flags|= lavc_param_mv0; |
7444 | 514 if(lavc_param_gray) lavc_venc_context->flags|= CODEC_FLAG_GRAY; |
6461 | 515 |
7490 | 516 if(lavc_param_normalize_aqp) lavc_venc_context->flags|= CODEC_FLAG_NORMALIZE_AQP; |
7504 | 517 if(lavc_param_interlaced_dct) lavc_venc_context->flags|= CODEC_FLAG_INTERLACED_DCT; |
8347 | 518 #if LIBAVCODEC_BUILD >= 4643 |
519 lavc_venc_context->flags|= lavc_param_psnr; | |
520 #endif | |
8190 | 521 lavc_venc_context->prediction_method= lavc_param_prediction_method; |
10778 | 522 switch(lavc_param_format) |
523 { | |
524 case IMGFMT_YV12: | |
525 lavc_venc_context->pix_fmt = PIX_FMT_YUV420P; | |
526 break; | |
527 case IMGFMT_422P: | |
528 lavc_venc_context->pix_fmt = PIX_FMT_YUV422P; | |
529 break; | |
530 case IMGFMT_444P: | |
531 lavc_venc_context->pix_fmt = PIX_FMT_YUV444P; | |
532 break; | |
533 case IMGFMT_411P: | |
534 lavc_venc_context->pix_fmt = PIX_FMT_YUV411P; | |
535 break; | |
536 case IMGFMT_YVU9: | |
537 lavc_venc_context->pix_fmt = PIX_FMT_YUV410P; | |
538 break; | |
539 case IMGFMT_BGR32: | |
540 lavc_venc_context->pix_fmt = PIX_FMT_RGBA32; | |
541 break; | |
542 default: | |
543 mp_msg(MSGT_MENCODER,MSGL_ERR,"%s is not a supported format\n", vo_format_name(lavc_param_format)); | |
544 return 0; | |
8190 | 545 } |
10302 | 546 |
10848
5844bf004dec
2pass stats curruption fix by ("Johannes E. Schindelin" <Johannes dot Schindelin at gmx dot de>)
michael
parents:
10779
diff
changeset
|
547 if(!stats_file) { |
5778 | 548 /* lavc internal 2pass bitrate control */ |
6673 | 549 switch(lavc_param_vpass){ |
7088 | 550 case 1: |
7444 | 551 lavc_venc_context->flags|= CODEC_FLAG_PASS1; |
10881
6c3555f4c5c3
fix 2-pass encoding with libavcodec onwin32, patch by Tristan Seligmann <mithrandi-mplayer-dev-eng at mithrandi.za.net>
faust3
parents:
10848
diff
changeset
|
552 stats_file= fopen(passtmpfile, "wb"); |
7088 | 553 if(stats_file==NULL){ |
554 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
555 return 0; | |
556 } | |
557 break; | |
558 case 2: | |
7444 | 559 lavc_venc_context->flags|= CODEC_FLAG_PASS2; |
10881
6c3555f4c5c3
fix 2-pass encoding with libavcodec onwin32, patch by Tristan Seligmann <mithrandi-mplayer-dev-eng at mithrandi.za.net>
faust3
parents:
10848
diff
changeset
|
560 stats_file= fopen(passtmpfile, "rb"); |
7088 | 561 if(stats_file==NULL){ |
562 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
563 return 0; | |
564 } | |
565 fseek(stats_file, 0, SEEK_END); | |
566 size= ftell(stats_file); | |
567 fseek(stats_file, 0, SEEK_SET); | |
568 | |
7444 | 569 lavc_venc_context->stats_in= malloc(size + 1); |
570 lavc_venc_context->stats_in[size]=0; | |
7088 | 571 |
7444 | 572 if(fread(lavc_venc_context->stats_in, size, 1, stats_file)<1){ |
7088 | 573 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: reading from filename=%s\n", passtmpfile); |
574 return 0; | |
7089 | 575 } |
7088 | 576 break; |
6673 | 577 } |
10848
5844bf004dec
2pass stats curruption fix by ("Johannes E. Schindelin" <Johannes dot Schindelin at gmx dot de>)
michael
parents:
10779
diff
changeset
|
578 } |
5778 | 579 |
7444 | 580 lavc_venc_context->me_method = ME_ZERO+lavc_param_vme; |
5550 | 581 |
582 /* fixed qscale :p */ | |
583 if (lavc_param_vqscale) | |
584 { | |
11032 | 585 printf("Using constant qscale = %f (VBR)\n", lavc_param_vqscale); |
7444 | 586 lavc_venc_context->flags |= CODEC_FLAG_QSCALE; |
10131 | 587 #if LIBAVCODEC_BUILD >= 4668 |
11032 | 588 lavc_venc_context->global_quality= |
10131 | 589 #endif |
11032 | 590 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * lavc_param_vqscale + 0.5); |
5550 | 591 } |
592 | |
7444 | 593 if (avcodec_open(lavc_venc_context, vf->priv->codec) != 0) { |
5550 | 594 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec); |
595 return 0; | |
596 } | |
597 | |
7444 | 598 if (lavc_venc_context->codec->encode == NULL) { |
5550 | 599 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n"); |
600 return 0; | |
601 } | |
7088 | 602 |
603 /* free second pass buffer, its not needed anymore */ | |
7444 | 604 if(lavc_venc_context->stats_in) free(lavc_venc_context->stats_in); |
605 lavc_venc_context->stats_in= NULL; | |
8190 | 606 if(lavc_venc_context->bits_per_sample) |
607 mux_v->bih->biBitCount= lavc_venc_context->bits_per_sample; | |
608 if(lavc_venc_context->extradata_size){ | |
609 memcpy(mux_v->bih + 1, lavc_venc_context->extradata, lavc_venc_context->extradata_size); | |
610 mux_v->bih->biSize= sizeof(BITMAPINFOHEADER) + lavc_venc_context->extradata_size; | |
611 } | |
7088 | 612 |
5550 | 613 return 1; |
614 } | |
615 | |
616 static int control(struct vf_instance_s* vf, int request, void* data){ | |
617 | |
618 return CONTROL_UNKNOWN; | |
619 } | |
620 | |
621 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
622 switch(fmt){ | |
623 case IMGFMT_YV12: | |
624 case IMGFMT_IYUV: | |
625 case IMGFMT_I420: | |
10778 | 626 if(lavc_param_format == IMGFMT_YV12) |
8190 | 627 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE; |
628 break; | |
10293 | 629 case IMGFMT_411P: |
10778 | 630 if(lavc_param_format == IMGFMT_411P) |
10293 | 631 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE; |
632 break; | |
8190 | 633 case IMGFMT_422P: |
10778 | 634 if(lavc_param_format == IMGFMT_422P) |
8190 | 635 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE; |
636 break; | |
10293 | 637 case IMGFMT_444P: |
10778 | 638 if(lavc_param_format == IMGFMT_444P) |
10293 | 639 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE; |
640 break; | |
641 case IMGFMT_YVU9: | |
10778 | 642 if(lavc_param_format == IMGFMT_YVU9) |
10293 | 643 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE; |
644 break; | |
10302 | 645 case IMGFMT_BGR32: |
10778 | 646 if(lavc_param_format == IMGFMT_BGR32) |
10302 | 647 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE; |
648 break; | |
5550 | 649 } |
650 return 0; | |
651 } | |
652 | |
8347 | 653 static double psnr(double d){ |
654 if(d==0) return INFINITY; | |
655 return -10.0*log(d)/log(10); | |
656 } | |
657 | |
7368 | 658 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){ |
8347 | 659 const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'}; |
5550 | 660 int out_size; |
8413 | 661 AVFrame *pic= vf->priv->pic; |
5550 | 662 |
8339 | 663 pic->data[0]=mpi->planes[0]; |
664 pic->data[1]=mpi->planes[1]; | |
665 pic->data[2]=mpi->planes[2]; | |
666 pic->linesize[0]=mpi->stride[0]; | |
667 pic->linesize[1]=mpi->stride[1]; | |
668 pic->linesize[2]=mpi->stride[2]; | |
5550 | 669 |
7444 | 670 out_size = avcodec_encode_video(lavc_venc_context, mux_v->buffer, mux_v->buffer_size, |
8339 | 671 pic); |
5550 | 672 |
9536 | 673 |
9014
c671e9adbe22
Cleanup of the muxer API, func parameters muxer & muxer_f eliminated.
arpi
parents:
8803
diff
changeset
|
674 muxer_write_chunk(mux_v,out_size,lavc_venc_context->coded_frame->key_frame?0x10:0); |
8413 | 675 |
8347 | 676 #if LIBAVCODEC_BUILD >= 4643 |
677 /* store psnr / pict size / type / qscale */ | |
678 if(lavc_param_psnr){ | |
679 static FILE *fvstats=NULL; | |
680 char filename[20]; | |
681 static long long int all_len=0; | |
682 static int frame_number=0; | |
683 static double all_frametime=0.0; | |
8413 | 684 AVFrame *pic= lavc_venc_context->coded_frame; |
8347 | 685 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0; |
9865
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
686 double quality=0.0; |
9868 | 687 int8_t *q; |
8347 | 688 |
689 if(!fvstats) { | |
690 time_t today2; | |
691 struct tm *today; | |
692 today2 = time(NULL); | |
693 today = localtime(&today2); | |
694 sprintf(filename, "psnr_%02d%02d%02d.log", today->tm_hour, | |
695 today->tm_min, today->tm_sec); | |
696 fvstats = fopen(filename,"w"); | |
697 if(!fvstats) { | |
698 perror("fopen"); | |
699 lavc_param_psnr=0; // disable block | |
700 /*exit(1);*/ | |
701 } | |
702 } | |
9865
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
703 |
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
704 // average MB quantizer |
9868 | 705 q = lavc_venc_context->coded_frame->qscale_table; |
706 if(q) { | |
9865
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
707 int x, y; |
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
708 int w = (lavc_venc_context->width+15) >> 4; |
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
709 int h = (lavc_venc_context->height+15) >> 4; |
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
710 for( y = 0; y < h; y++ ) { |
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
711 for( x = 0; x < w; x++ ) |
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
712 quality += (double)*(q+x); |
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
713 q += lavc_venc_context->coded_frame->qstride; |
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
714 } |
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
715 quality /= w * h; |
9868 | 716 } else |
11032 | 717 quality = lavc_venc_context->coded_frame->quality / (float)FF_QP2LAMBDA; |
8347 | 718 |
719 fprintf(fvstats, "%6d, %2.2f, %6d, %2.2f, %2.2f, %2.2f, %2.2f %c\n", | |
8413 | 720 lavc_venc_context->coded_frame->coded_picture_number, |
9865
30893b593947
Adaptive quantization support for "-lavcopts psnr" and "-lavdopts vstats".
rguyom
parents:
9645
diff
changeset
|
721 quality, |
8347 | 722 out_size, |
8413 | 723 psnr(lavc_venc_context->coded_frame->error[0]/f), |
724 psnr(lavc_venc_context->coded_frame->error[1]*4/f), | |
725 psnr(lavc_venc_context->coded_frame->error[2]*4/f), | |
726 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)), | |
727 pict_type_char[lavc_venc_context->coded_frame->pict_type] | |
8347 | 728 ); |
729 } | |
730 #endif | |
7088 | 731 /* store stats if there are any */ |
7444 | 732 if(lavc_venc_context->stats_out && stats_file) |
733 fprintf(stats_file, "%s", lavc_venc_context->stats_out); | |
7368 | 734 return 1; |
5550 | 735 } |
736 | |
5551 | 737 static void uninit(struct vf_instance_s* vf){ |
8347 | 738 const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'}; |
739 | |
740 #if LIBAVCODEC_BUILD >= 4643 | |
741 if(lavc_param_psnr){ | |
742 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0; | |
8413 | 743 f*= lavc_venc_context->coded_frame->coded_picture_number; |
8347 | 744 |
745 printf("PSNR: Y:%2.2f, Cb:%2.2f, Cr:%2.2f, All:%2.2f\n", | |
746 psnr(lavc_venc_context->error[0]/f), | |
747 psnr(lavc_venc_context->error[1]*4/f), | |
748 psnr(lavc_venc_context->error[2]*4/f), | |
749 psnr((lavc_venc_context->error[0]+lavc_venc_context->error[1]+lavc_venc_context->error[2])/(f*1.5)) | |
750 ); | |
751 } | |
752 #endif | |
753 | |
10779 | 754 #if LIBAVCODEC_BUILD >= 4675 |
755 if (lavc_venc_context->intra_matrix) | |
756 free(lavc_venc_context->intra_matrix); | |
757 lavc_venc_context->intra_matrix = NULL; | |
758 if (lavc_venc_context->inter_matrix) | |
759 free(lavc_venc_context->inter_matrix); | |
760 lavc_venc_context->inter_matrix = NULL; | |
761 #endif | |
762 | |
7444 | 763 avcodec_close(lavc_venc_context); |
7088 | 764 |
765 if(stats_file) fclose(stats_file); | |
7089 | 766 |
7088 | 767 /* free rc_override */ |
7444 | 768 if(lavc_venc_context->rc_override) free(lavc_venc_context->rc_override); |
769 lavc_venc_context->rc_override= NULL; | |
770 | |
771 if(vf->priv->context) free(vf->priv->context); | |
772 vf->priv->context= NULL; | |
5551 | 773 } |
774 | |
5550 | 775 //===========================================================================// |
776 | |
777 static int vf_open(vf_instance_t *vf, char* args){ | |
5551 | 778 vf->uninit=uninit; |
5550 | 779 vf->config=config; |
780 vf->control=control; | |
781 vf->query_format=query_format; | |
782 vf->put_image=put_image; | |
783 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
784 memset(vf->priv,0,sizeof(struct vf_priv_s)); | |
8585 | 785 vf->priv->mux=(muxer_stream_t*)args; |
5550 | 786 |
5977
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
787 /* 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
|
788 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
|
789 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
|
790 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
|
791 { |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
792 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
|
793 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
|
794 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
|
795 } |
8190 | 796 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "huffyuv")) |
797 { | |
798 /* XXX: hack: huffyuv needs to store huffman tables (allthough we dunno the size yet ...) */ | |
799 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+1000); | |
800 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+1000); | |
801 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+1000; | |
802 } | |
10131 | 803 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "asv1")) |
804 { | |
805 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+8); | |
806 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+8); | |
807 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+8; | |
808 } | |
10777 | 809 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "asv2")) |
810 { | |
811 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+8); | |
812 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+8); | |
813 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+8; | |
814 } | |
8587 | 815 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "wmv2")) |
816 { | |
817 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+4); | |
818 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+4); | |
819 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+4; | |
820 } | |
5977
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
821 else |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
822 { |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
823 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)); |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
824 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
|
825 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER); |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
826 } |
5550 | 827 mux_v->bih->biWidth=0; |
828 mux_v->bih->biHeight=0; | |
829 mux_v->bih->biPlanes=1; | |
830 mux_v->bih->biBitCount=24; | |
831 if (!lavc_param_vcodec) | |
832 { | |
5977
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
833 printf("No libavcodec codec specified! It's required!\n"); |
5550 | 834 return 0; |
835 } | |
836 | |
837 if (!strcasecmp(lavc_param_vcodec, "mpeg1") || !strcasecmp(lavc_param_vcodec, "mpeg1video")) | |
838 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '1'); | |
10699 | 839 else if (!strcasecmp(lavc_param_vcodec, "mpeg2") || !strcasecmp(lavc_param_vcodec, "mpeg2video")) |
840 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '2'); | |
5550 | 841 else if (!strcasecmp(lavc_param_vcodec, "h263") || !strcasecmp(lavc_param_vcodec, "h263p")) |
842 mux_v->bih->biCompression = mmioFOURCC('h', '2', '6', '3'); | |
843 else if (!strcasecmp(lavc_param_vcodec, "rv10")) | |
844 mux_v->bih->biCompression = mmioFOURCC('R', 'V', '1', '0'); | |
845 else if (!strcasecmp(lavc_param_vcodec, "mjpeg")) | |
846 mux_v->bih->biCompression = mmioFOURCC('M', 'J', 'P', 'G'); | |
10302 | 847 else if (!strcasecmp(lavc_param_vcodec, "ljpeg")) |
848 mux_v->bih->biCompression = mmioFOURCC('L', 'J', 'P', 'G'); | |
5550 | 849 else if (!strcasecmp(lavc_param_vcodec, "mpeg4")) |
850 mux_v->bih->biCompression = mmioFOURCC('D', 'I', 'V', 'X'); | |
851 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4")) | |
852 mux_v->bih->biCompression = mmioFOURCC('d', 'i', 'v', '3'); | |
5972 | 853 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4v2")) |
854 mux_v->bih->biCompression = mmioFOURCC('M', 'P', '4', '2'); | |
6461 | 855 else if (!strcasecmp(lavc_param_vcodec, "wmv1")) |
856 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '1'); | |
8587 | 857 else if (!strcasecmp(lavc_param_vcodec, "wmv2")) |
858 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '2'); | |
8190 | 859 else if (!strcasecmp(lavc_param_vcodec, "huffyuv")) |
860 mux_v->bih->biCompression = mmioFOURCC('H', 'F', 'Y', 'U'); | |
10131 | 861 else if (!strcasecmp(lavc_param_vcodec, "asv1")) |
862 mux_v->bih->biCompression = mmioFOURCC('A', 'S', 'V', '1'); | |
10777 | 863 else if (!strcasecmp(lavc_param_vcodec, "asv2")) |
864 mux_v->bih->biCompression = mmioFOURCC('A', 'S', 'V', '2'); | |
10264 | 865 else if (!strcasecmp(lavc_param_vcodec, "ffv1")) |
866 mux_v->bih->biCompression = mmioFOURCC('F', 'F', 'V', '1'); | |
5550 | 867 else |
868 mux_v->bih->biCompression = mmioFOURCC(lavc_param_vcodec[0], | |
869 lavc_param_vcodec[1], lavc_param_vcodec[2], lavc_param_vcodec[3]); /* FIXME!!! */ | |
870 | |
871 if (!avcodec_inited){ | |
872 avcodec_init(); | |
873 avcodec_register_all(); | |
874 avcodec_inited=1; | |
875 } | |
876 | |
877 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name(lavc_param_vcodec); | |
878 if (!vf->priv->codec) { | |
879 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, lavc_param_vcodec); | |
880 return 0; | |
881 } | |
882 | |
8413 | 883 #if LIBAVCODEC_BUILD >= 4645 |
884 vf->priv->pic = avcodec_alloc_frame(); | |
885 #else | |
8339 | 886 vf->priv->pic = avcodec_alloc_picture(); |
8413 | 887 #endif |
7444 | 888 vf->priv->context = avcodec_alloc_context(); |
889 | |
5550 | 890 return 1; |
891 } | |
892 | |
893 vf_info_t ve_info_lavc = { | |
894 "libavcodec encoder", | |
895 "lavc", | |
6673 | 896 "A'rpi, Alex, Michael", |
5550 | 897 "for internal use by mencoder", |
898 vf_open | |
899 }; | |
900 | |
901 //===========================================================================// | |
902 #endif |