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