Mercurial > mplayer.hg
annotate libmpcodecs/ve_lavc.c @ 7097:5c87f14ad947
SetDLLAccessPath isn't used. Patch by Andres Hess <jaska@gmx.net>
author | alex |
---|---|
date | Tue, 27 Aug 2002 15:46:41 +0000 |
parents | 43d7b60bd7f4 |
children | 1e47c2e7aa8e |
rev | line source |
---|---|
5550 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 | |
5 #include "../config.h" | |
6 | |
7 #ifdef USE_LIBAVCODEC | |
8 | |
9 #include "../mp_msg.h" | |
10 #include "../help_mp.h" | |
11 | |
12 #include "codec-cfg.h" | |
13 #include "stream.h" | |
14 #include "demuxer.h" | |
15 #include "stheader.h" | |
16 | |
17 #include "aviwrite.h" | |
18 | |
5607 | 19 #include "img_format.h" |
20 #include "mp_image.h" | |
5550 | 21 #include "vf.h" |
22 | |
6673 | 23 #ifdef HAVE_DIVX4ENCORE |
5550 | 24 #include "divx4_vbr.h" |
6673 | 25 #endif |
5550 | 26 |
7090
43d7b60bd7f4
fixing compilation without divx4 patch by (Rmi Guyomarch <rguyom at pobox dot com>)
michael
parents:
7089
diff
changeset
|
27 extern char* passtmpfile; |
5550 | 28 extern int pass; |
29 | |
30 //===========================================================================// | |
31 | |
32 #ifdef USE_LIBAVCODEC_SO | |
7004 | 33 #include <ffmpeg/avcodec.h> |
5550 | 34 #else |
35 #include "libavcodec/avcodec.h" | |
36 #endif | |
37 | |
5778 | 38 #if LIBAVCODEC_BUILD < 4601 |
39 #error your version of libavcodec is too old, get a newer one, and dont send a bugreport, THIS IS NO BUG | |
40 #endif | |
41 | |
7088 | 42 #if LIBAVCODEC_BUILD < 4620 |
43 #warning your version of libavcodec is old, u might want to get a newer one | |
44 #endif | |
45 | |
5550 | 46 extern int avcodec_inited; |
47 | |
48 /* video options */ | |
49 static char *lavc_param_vcodec = NULL; | |
50 static int lavc_param_vbitrate = -1; | |
7088 | 51 static int lavc_param_vrate_tolerance = 1000*8; |
5550 | 52 static int lavc_param_vhq = 0; /* default is realtime encoding */ |
53 static int lavc_param_v4mv = 0; | |
5556 | 54 static int lavc_param_vme = 4; |
5550 | 55 static int lavc_param_vqscale = 0; |
7088 | 56 static int lavc_param_vqmin = 2; |
57 static int lavc_param_vqmax = 31; | |
5550 | 58 static int lavc_param_vqdiff = 3; |
59 static float lavc_param_vqcompress = 0.5; | |
60 static float lavc_param_vqblur = 0.5; | |
7088 | 61 static float lavc_param_vb_qfactor = 1.25; |
62 static float lavc_param_vb_qoffset = 1.25; | |
63 static float lavc_param_vi_qfactor = 0.8; | |
64 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
|
65 static int lavc_param_vmax_b_frames = 0; |
5550 | 66 static int lavc_param_keyint = -1; |
5778 | 67 static int lavc_param_vpass = 0; |
68 static int lavc_param_vrc_strategy = 2; | |
69 static int lavc_param_vb_strategy = 0; | |
6255
140e94c4c89b
single coefficient elimination disabled by default
michael
parents:
6228
diff
changeset
|
70 static int lavc_param_luma_elim_threshold = 0; |
140e94c4c89b
single coefficient elimination disabled by default
michael
parents:
6228
diff
changeset
|
71 static int lavc_param_chroma_elim_threshold = 0; |
6228
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
72 static int lavc_param_packet_size= 0; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
73 static int lavc_param_strict= 0; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
74 static int lavc_param_data_partitioning= 0; |
6461 | 75 static int lavc_param_gray=0; |
7088 | 76 static float lavc_param_rc_qsquish=1.0; |
77 static float lavc_param_rc_qmod_amp=0; | |
78 static int lavc_param_rc_qmod_freq=0; | |
79 static char *lavc_param_rc_override_string=NULL; | |
80 static char *lavc_param_rc_eq="tex^qComp"; | |
81 static int lavc_param_rc_buffer_size=0; | |
82 static float lavc_param_rc_buffer_aggressivity=1.0; | |
83 static int lavc_param_rc_max_rate=0; | |
84 static int lavc_param_rc_min_rate=0; | |
85 static float lavc_param_rc_initial_cplx=0; | |
6944 | 86 static int lavc_param_mpeg_quant=0; |
5550 | 87 |
88 #include "cfgparser.h" | |
89 | |
90 #ifdef USE_LIBAVCODEC | |
91 struct config lavcopts_conf[]={ | |
92 {"vcodec", &lavc_param_vcodec, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
93 {"vbitrate", &lavc_param_vbitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
94 {"vratetol", &lavc_param_vrate_tolerance, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
95 {"vhq", &lavc_param_vhq, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
96 {"v4mv", &lavc_param_v4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
97 {"vme", &lavc_param_vme, CONF_TYPE_INT, CONF_RANGE, 0, 5, NULL}, | |
98 {"vqscale", &lavc_param_vqscale, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
99 {"vqmin", &lavc_param_vqmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
100 {"vqmax", &lavc_param_vqmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
101 {"vqdiff", &lavc_param_vqdiff, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
102 {"vqcomp", &lavc_param_vqcompress, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL}, | |
103 {"vqblur", &lavc_param_vqblur, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL}, | |
7088 | 104 {"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
|
105 {"vmax_b_frames", &lavc_param_vmax_b_frames, CONF_TYPE_INT, CONF_RANGE, 0, FF_MAX_B_FRAMES, NULL}, |
5778 | 106 {"vpass", &lavc_param_vpass, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL}, |
7088 | 107 #if LIBAVCODEC_BUILD < 4620 |
5778 | 108 {"vrc_strategy", &lavc_param_vrc_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL}, |
7088 | 109 #endif |
5778 | 110 {"vb_strategy", &lavc_param_vb_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 1, NULL}, |
6228
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
111 #ifdef CODEC_FLAG_PART |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
112 {"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
|
113 {"vlelim", &lavc_param_luma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL}, |
7038 | 114 {"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
|
115 {"vpsize", &lavc_param_packet_size, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL}, |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
116 {"vstrict", &lavc_param_strict, CONF_TYPE_FLAG, 0, 0, 1, NULL}, |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
117 {"vdpart", &lavc_param_data_partitioning, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL}, |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
118 #endif |
5550 | 119 {"keyint", &lavc_param_keyint, CONF_TYPE_INT, 0, 0, 0, NULL}, |
6461 | 120 #if LIBAVCODEC_BUILD >= 4614 |
121 {"gray", &lavc_param_gray, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL}, | |
122 #endif | |
6944 | 123 #if LIBAVCODEC_BUILD >= 4619 |
124 {"mpeg_quant", &lavc_param_mpeg_quant, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
125 #endif | |
7088 | 126 #if LIBAVCODEC_BUILD >= 4620 |
127 {"vi_qfactor", &lavc_param_vi_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL}, | |
128 {"vi_qoffset", &lavc_param_vi_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL}, | |
129 {"vqsquish", &lavc_param_rc_qsquish, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL}, | |
130 {"vqmod_amp", &lavc_param_rc_qmod_amp, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL}, | |
131 {"vqmod_freq", &lavc_param_rc_qmod_freq, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
132 {"vrc_eq", &lavc_param_rc_eq, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
133 {"vrc_override", &lavc_param_rc_override_string, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
134 {"vrc_maxrate", &lavc_param_rc_max_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
135 {"vrc_minrate", &lavc_param_rc_min_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
136 {"vrc_buf_size", &lavc_param_rc_min_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, | |
137 {"vrc_buf_aggressivity", &lavc_param_rc_buffer_aggressivity, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL}, | |
138 {"vrc_init_cplx", &lavc_param_rc_initial_cplx, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 9999999.0, NULL}, | |
139 #endif | |
5550 | 140 {NULL, NULL, 0, 0, 0, 0, NULL} |
141 }; | |
142 #endif | |
143 | |
144 struct vf_priv_s { | |
145 aviwrite_stream_t* mux; | |
146 AVCodecContext context; | |
147 AVCodec *codec; | |
7088 | 148 FILE *stats_file; |
5550 | 149 }; |
150 | |
7088 | 151 #define stats_file (vf->priv->stats_file) |
5550 | 152 #define mux_v (vf->priv->mux) |
153 #define lavc_venc_context (vf->priv->context) | |
154 | |
155 static int config(struct vf_instance_s* vf, | |
156 int width, int height, int d_width, int d_height, | |
157 unsigned int flags, unsigned int outfmt){ | |
7088 | 158 int size, i; |
159 void *p; | |
160 | |
5550 | 161 mux_v->bih->biWidth=width; |
162 mux_v->bih->biHeight=height; | |
163 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8); | |
164 | |
165 memset(&lavc_venc_context, 0, sizeof(lavc_venc_context)); | |
166 | |
167 printf("videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n", | |
168 mux_v->bih->biWidth, mux_v->bih->biHeight, mux_v->bih->biCompression, | |
169 (char *)&mux_v->bih->biCompression); | |
170 | |
171 lavc_venc_context.width = width; | |
172 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
|
173 if (lavc_param_vbitrate > 16000) /* != -1 */ |
a21cab74cde8
bitrate>16000 means bits not kbits - noticed by George Hawkins <george_hawkins@yahoo.com>
arpi
parents:
5657
diff
changeset
|
174 lavc_venc_context.bit_rate = lavc_param_vbitrate; |
a21cab74cde8
bitrate>16000 means bits not kbits - noticed by George Hawkins <george_hawkins@yahoo.com>
arpi
parents:
5657
diff
changeset
|
175 else if (lavc_param_vbitrate >= 0) /* != -1 */ |
5550 | 176 lavc_venc_context.bit_rate = lavc_param_vbitrate*1000; |
177 else | |
178 lavc_venc_context.bit_rate = 800000; /* default */ | |
179 lavc_venc_context.bit_rate_tolerance= lavc_param_vrate_tolerance*1000; | |
180 lavc_venc_context.frame_rate = (float)mux_v->h.dwRate/mux_v->h.dwScale * FRAME_RATE_BASE; | |
181 lavc_venc_context.qmin= lavc_param_vqmin; | |
182 lavc_venc_context.qmax= lavc_param_vqmax; | |
183 lavc_venc_context.max_qdiff= lavc_param_vqdiff; | |
184 lavc_venc_context.qcompress= lavc_param_vqcompress; | |
185 lavc_venc_context.qblur= lavc_param_vqblur; | |
5657
ee2efbf3dc9d
Preliminary support for lavcs b-frame encoding, disabled by default.
atmos4
parents:
5646
diff
changeset
|
186 lavc_venc_context.max_b_frames= lavc_param_vmax_b_frames; |
5778 | 187 lavc_venc_context.b_quant_factor= lavc_param_vb_qfactor; |
188 lavc_venc_context.rc_strategy= lavc_param_vrc_strategy; | |
189 lavc_venc_context.b_frame_strategy= lavc_param_vb_strategy; | |
6228
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
190 |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
191 #ifdef CODEC_FLAG_PART |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
192 lavc_venc_context.b_quant_offset= lavc_param_vb_qoffset; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
193 lavc_venc_context.luma_elim_threshold= lavc_param_luma_elim_threshold; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
194 lavc_venc_context.chroma_elim_threshold= lavc_param_chroma_elim_threshold; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
195 lavc_venc_context.rtp_payload_size= lavc_param_packet_size; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
196 if(lavc_param_packet_size )lavc_venc_context.rtp_mode=1; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
197 lavc_venc_context.strict_std_compliance= lavc_param_strict; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
198 #endif |
7088 | 199 #if LIBAVCODEC_BUILD >= 4620 |
200 lavc_venc_context.i_quant_factor= lavc_param_vi_qfactor; | |
201 lavc_venc_context.i_quant_offset= lavc_param_vi_qoffset; | |
202 lavc_venc_context.rc_qsquish= lavc_param_rc_qsquish; | |
203 lavc_venc_context.rc_qmod_amp= lavc_param_rc_qmod_amp; | |
204 lavc_venc_context.rc_qmod_freq= lavc_param_rc_qmod_freq; | |
205 lavc_venc_context.rc_eq= lavc_param_rc_eq; | |
206 lavc_venc_context.rc_max_rate= lavc_param_rc_max_rate*1000; | |
207 lavc_venc_context.rc_min_rate= lavc_param_rc_min_rate*1000; | |
208 lavc_venc_context.rc_buffer_size= lavc_param_rc_buffer_size*1000; | |
209 lavc_venc_context.rc_buffer_aggressivity= lavc_param_rc_buffer_aggressivity; | |
210 lavc_venc_context.rc_initial_cplx= lavc_param_rc_initial_cplx; | |
211 p= lavc_param_rc_override_string; | |
212 for(i=0; p; i++){ | |
213 int start, end, q; | |
214 int e=sscanf(p, "%d,%d,%d", &start, &end, &q); | |
215 if(e!=3){ | |
216 mp_msg(MSGT_MENCODER,MSGL_ERR,"error parsing vrc_q\n"); | |
217 return 0; | |
218 } | |
219 lavc_venc_context.rc_override= | |
220 realloc(lavc_venc_context.rc_override, sizeof(RcOverride)*(i+1)); | |
221 lavc_venc_context.rc_override[i].start_frame= start; | |
222 lavc_venc_context.rc_override[i].end_frame = end; | |
223 if(q>0){ | |
224 lavc_venc_context.rc_override[i].qscale= q; | |
225 lavc_venc_context.rc_override[i].quality_factor= 1.0; | |
226 } | |
227 else{ | |
228 lavc_venc_context.rc_override[i].qscale= 0; | |
229 lavc_venc_context.rc_override[i].quality_factor= -q/100.0; | |
230 } | |
231 p= strchr(p, '/'); | |
232 if(p) p++; | |
233 } | |
234 lavc_venc_context.rc_override_count=i; | |
235 #endif | |
236 | |
6944 | 237 #if LIBAVCODEC_BUILD >= 4619 |
238 lavc_venc_context.mpeg_quant=lavc_param_mpeg_quant; | |
239 #endif | |
240 | |
5550 | 241 /* keyframe interval */ |
242 if (lavc_param_keyint >= 0) /* != -1 */ | |
243 lavc_venc_context.gop_size = lavc_param_keyint; | |
244 else | |
245 lavc_venc_context.gop_size = 250; /* default */ | |
246 | |
247 if (lavc_param_vhq) | |
248 { | |
249 printf("High quality encoding selected (non real time)!\n"); | |
250 lavc_venc_context.flags = CODEC_FLAG_HQ; | |
251 } | |
252 else | |
253 lavc_venc_context.flags = 0; | |
254 | |
5781
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
255 /* 4mv is currently buggy with B frames */ |
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
256 if (lavc_param_vmax_b_frames > 0 && lavc_param_v4mv) { |
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
257 printf("4MV with B-Frames not yet supported -> 4MV disabled\n"); |
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
258 lavc_param_v4mv = 0; |
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
259 } |
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
260 |
5550 | 261 lavc_venc_context.flags|= lavc_param_v4mv ? CODEC_FLAG_4MV : 0; |
6228
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
262 #ifdef CODEC_FLAG_PART |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
263 lavc_venc_context.flags|= lavc_param_data_partitioning; |
ea6b20e70ac5
new options to support Michael's libavcodec patch, #ifdefed
arpi
parents:
5977
diff
changeset
|
264 #endif |
6461 | 265 #if LIBAVCODEC_BUILD >= 4614 |
266 if(lavc_param_gray) lavc_venc_context.flags|= CODEC_FLAG_GRAY; | |
267 #endif | |
268 | |
5550 | 269 |
5778 | 270 /* lavc internal 2pass bitrate control */ |
6673 | 271 #ifdef HAVE_DIVX4ENCORE |
272 switch(lavc_param_vpass){ | |
273 #else | |
274 switch(lavc_param_vpass?lavc_param_vpass:pass){ | |
275 #endif | |
7088 | 276 case 1: |
277 lavc_venc_context.flags|= CODEC_FLAG_PASS1; | |
7089 | 278 #if LIBAVCODEC_BUILD >= 4620 |
7088 | 279 stats_file= fopen(passtmpfile, "w"); |
280 if(stats_file==NULL){ | |
281 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
282 return 0; | |
283 } | |
7089 | 284 #endif |
7088 | 285 break; |
286 case 2: | |
287 lavc_venc_context.flags|= CODEC_FLAG_PASS2; | |
7089 | 288 #if LIBAVCODEC_BUILD >= 4620 |
7088 | 289 stats_file= fopen(passtmpfile, "r"); |
290 if(stats_file==NULL){ | |
291 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
292 return 0; | |
293 } | |
294 fseek(stats_file, 0, SEEK_END); | |
295 size= ftell(stats_file); | |
296 fseek(stats_file, 0, SEEK_SET); | |
297 | |
298 lavc_venc_context.stats_in= malloc(size + 1); | |
299 lavc_venc_context.stats_in[size]=0; | |
300 | |
301 if(fread(lavc_venc_context.stats_in, size, 1, stats_file)<1){ | |
302 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: reading from filename=%s\n", passtmpfile); | |
303 return 0; | |
7089 | 304 } |
305 #endif | |
7088 | 306 break; |
6673 | 307 } |
5778 | 308 |
5624 | 309 #ifdef ME_ZERO |
310 // workaround Juanjo's stupid incompatible change: | |
5550 | 311 motion_estimation_method = lavc_param_vme; |
5624 | 312 #else |
313 lavc_venc_context.me_method = ME_ZERO+lavc_param_vme; | |
314 #endif | |
5550 | 315 |
316 /* fixed qscale :p */ | |
317 if (lavc_param_vqscale) | |
318 { | |
319 printf("Using constant qscale = %d (VBR)\n", lavc_param_vqscale); | |
320 lavc_venc_context.flags |= CODEC_FLAG_QSCALE; | |
321 lavc_venc_context.quality = lavc_param_vqscale; | |
322 } | |
323 | |
6673 | 324 #ifdef HAVE_DIVX4ENCORE |
5550 | 325 switch(pass){ |
326 case 1: | |
327 if (VbrControl_init_2pass_vbr_analysis(passtmpfile, 5) == -1){ | |
328 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
329 pass=0; | |
5781
62e9e0c0bd6a
hq mode in first pass should work (as bad, ehh as well as LQ)
michael
parents:
5778
diff
changeset
|
330 } |
5550 | 331 break; |
332 case 2: | |
333 if (VbrControl_init_2pass_vbr_encoding(passtmpfile, | |
334 lavc_venc_context.bit_rate, | |
335 (float)mux_v->h.dwRate/mux_v->h.dwScale, | |
336 100, /* crispness */ | |
337 5) == -1){ | |
338 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
339 pass=0; | |
5632 | 340 } else |
341 lavc_venc_context.flags|=CODEC_FLAG_QSCALE|CODEC_FLAG_TYPE; // VBR | |
5550 | 342 break; |
343 } | |
6673 | 344 #endif |
5550 | 345 |
346 if (avcodec_open(&lavc_venc_context, vf->priv->codec) != 0) { | |
347 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec); | |
348 return 0; | |
349 } | |
350 | |
351 if (lavc_venc_context.codec->encode == NULL) { | |
352 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n"); | |
353 return 0; | |
354 } | |
7088 | 355 |
356 #if LIBAVCODEC_BUILD >= 4620 | |
357 /* free second pass buffer, its not needed anymore */ | |
358 if(lavc_venc_context.stats_in) free(lavc_venc_context.stats_in); | |
359 lavc_venc_context.stats_in= NULL; | |
360 #endif | |
361 | |
5550 | 362 return 1; |
363 } | |
364 | |
365 static int control(struct vf_instance_s* vf, int request, void* data){ | |
366 | |
367 return CONTROL_UNKNOWN; | |
368 } | |
369 | |
370 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
371 switch(fmt){ | |
372 case IMGFMT_YV12: | |
373 case IMGFMT_IYUV: | |
374 case IMGFMT_I420: | |
5565
0b301fec999a
capabilities support -> automatic insertion of scale, expand, pp
arpi
parents:
5556
diff
changeset
|
375 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE; |
5550 | 376 } |
377 return 0; | |
378 } | |
379 | |
380 static void put_image(struct vf_instance_s* vf, mp_image_t *mpi){ | |
381 int out_size; | |
382 AVPicture lavc_venc_picture; | |
383 | |
384 lavc_venc_picture.data[0]=mpi->planes[0]; | |
385 lavc_venc_picture.data[1]=mpi->planes[1]; | |
386 lavc_venc_picture.data[2]=mpi->planes[2]; | |
387 lavc_venc_picture.linesize[0]=mpi->stride[0]; | |
388 lavc_venc_picture.linesize[1]=mpi->stride[1]; | |
389 lavc_venc_picture.linesize[2]=mpi->stride[2]; | |
390 | |
6673 | 391 #ifdef HAVE_DIVX4ENCORE |
5550 | 392 if(pass==2){ // handle 2-pass: |
393 lavc_venc_context.flags|=CODEC_FLAG_QSCALE; // enable VBR | |
394 lavc_venc_context.quality=VbrControl_get_quant(); | |
395 lavc_venc_context.key_frame=VbrControl_get_intra(); | |
396 lavc_venc_context.gop_size=0x3fffffff; | |
397 out_size = avcodec_encode_video(&lavc_venc_context, mux_v->buffer, mux_v->buffer_size, | |
398 &lavc_venc_picture); | |
399 VbrControl_update_2pass_vbr_encoding(lavc_venc_context.mv_bits, | |
400 lavc_venc_context.i_tex_bits+lavc_venc_context.p_tex_bits, | |
401 8*out_size); | |
6673 | 402 } else |
403 #endif | |
404 { | |
5550 | 405 out_size = avcodec_encode_video(&lavc_venc_context, mux_v->buffer, mux_v->buffer_size, |
406 &lavc_venc_picture); | |
6673 | 407 #ifdef HAVE_DIVX4ENCORE |
5550 | 408 if(pass==1){ |
409 VbrControl_update_2pass_vbr_analysis(lavc_venc_context.key_frame, | |
410 lavc_venc_context.mv_bits, | |
411 lavc_venc_context.i_tex_bits+lavc_venc_context.p_tex_bits, | |
412 8*out_size, lavc_venc_context.quality); | |
413 } | |
6673 | 414 #endif |
5550 | 415 } |
416 | |
417 mencoder_write_chunk(mux_v,out_size,lavc_venc_context.key_frame?0x10:0); | |
7088 | 418 |
419 #if LIBAVCODEC_BUILD >= 4620 | |
420 /* store stats if there are any */ | |
421 if(lavc_venc_context.stats_out && stats_file) | |
422 fprintf(stats_file, "%s", lavc_venc_context.stats_out); | |
423 #endif | |
5550 | 424 } |
425 | |
5551 | 426 static void uninit(struct vf_instance_s* vf){ |
427 avcodec_close(&lavc_venc_context); | |
7088 | 428 |
7089 | 429 #if LIBAVCODEC_BUILD >= 4620 |
7088 | 430 if(stats_file) fclose(stats_file); |
7089 | 431 |
7088 | 432 /* free rc_override */ |
433 if(lavc_venc_context.rc_override) free(lavc_venc_context.rc_override); | |
434 lavc_venc_context.rc_override= NULL; | |
435 #endif | |
5551 | 436 } |
437 | |
5550 | 438 //===========================================================================// |
439 | |
440 static int vf_open(vf_instance_t *vf, char* args){ | |
5551 | 441 vf->uninit=uninit; |
5550 | 442 vf->config=config; |
443 vf->control=control; | |
444 vf->query_format=query_format; | |
445 vf->put_image=put_image; | |
446 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
447 memset(vf->priv,0,sizeof(struct vf_priv_s)); | |
448 vf->priv->mux=args; | |
449 | |
5977
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
450 /* 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
|
451 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
|
452 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
|
453 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
|
454 { |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
455 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
|
456 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
|
457 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
|
458 } |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
459 else |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
460 { |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
461 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)); |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
462 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
|
463 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER); |
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
464 } |
5550 | 465 mux_v->bih->biWidth=0; |
466 mux_v->bih->biHeight=0; | |
467 mux_v->bih->biPlanes=1; | |
468 mux_v->bih->biBitCount=24; | |
469 if (!lavc_param_vcodec) | |
470 { | |
5977
e8f6f477aad0
a hack to make ffmjpeg created files viewable with windows dlls too
alex
parents:
5972
diff
changeset
|
471 printf("No libavcodec codec specified! It's required!\n"); |
5550 | 472 return 0; |
473 } | |
474 | |
475 if (!strcasecmp(lavc_param_vcodec, "mpeg1") || !strcasecmp(lavc_param_vcodec, "mpeg1video")) | |
476 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '1'); | |
477 else if (!strcasecmp(lavc_param_vcodec, "h263") || !strcasecmp(lavc_param_vcodec, "h263p")) | |
478 mux_v->bih->biCompression = mmioFOURCC('h', '2', '6', '3'); | |
479 else if (!strcasecmp(lavc_param_vcodec, "rv10")) | |
480 mux_v->bih->biCompression = mmioFOURCC('R', 'V', '1', '0'); | |
481 else if (!strcasecmp(lavc_param_vcodec, "mjpeg")) | |
482 mux_v->bih->biCompression = mmioFOURCC('M', 'J', 'P', 'G'); | |
483 else if (!strcasecmp(lavc_param_vcodec, "mpeg4")) | |
484 mux_v->bih->biCompression = mmioFOURCC('D', 'I', 'V', 'X'); | |
485 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4")) | |
486 mux_v->bih->biCompression = mmioFOURCC('d', 'i', 'v', '3'); | |
5972 | 487 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4v2")) |
488 mux_v->bih->biCompression = mmioFOURCC('M', 'P', '4', '2'); | |
6461 | 489 else if (!strcasecmp(lavc_param_vcodec, "wmv1")) |
490 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '1'); | |
5550 | 491 else |
492 mux_v->bih->biCompression = mmioFOURCC(lavc_param_vcodec[0], | |
493 lavc_param_vcodec[1], lavc_param_vcodec[2], lavc_param_vcodec[3]); /* FIXME!!! */ | |
494 | |
495 if (!avcodec_inited){ | |
496 avcodec_init(); | |
497 avcodec_register_all(); | |
498 avcodec_inited=1; | |
499 } | |
500 | |
501 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name(lavc_param_vcodec); | |
502 if (!vf->priv->codec) { | |
503 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, lavc_param_vcodec); | |
504 return 0; | |
505 } | |
506 | |
507 return 1; | |
508 } | |
509 | |
510 vf_info_t ve_info_lavc = { | |
511 "libavcodec encoder", | |
512 "lavc", | |
6673 | 513 "A'rpi, Alex, Michael", |
5550 | 514 "for internal use by mencoder", |
515 vf_open | |
516 }; | |
517 | |
518 //===========================================================================// | |
519 #endif |