Mercurial > libavcodec.hg
comparison xvidff.c @ 2131:060053df9538 libavcodec
XviD Support patch by (Adam Thayer <krevnik at comcast dot net>)
author | michael |
---|---|
date | Fri, 16 Jul 2004 19:48:30 +0000 |
parents | |
children | 0fcee7879d3f |
comparison
equal
deleted
inserted
replaced
2130:50779a18844c | 2131:060053df9538 |
---|---|
1 /* | |
2 * Interface to xvidcore for mpeg4 encoding | |
3 * Copyright (c) 2004 Adam Thayer <krevnik@comcast.net> | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 | |
20 /** | |
21 * @file xvidmpeg4.c | |
22 * Interface to xvidcore for MPEG-4 compliant encoding. | |
23 * @author Adam Thayer (krevnik@comcast.net) | |
24 */ | |
25 | |
26 #include <xvid.h> | |
27 #include <unistd.h> | |
28 #include "common.h" | |
29 #include "avcodec.h" | |
30 | |
31 /** | |
32 * Buffer management macros. | |
33 */ | |
34 #define BUFFER_SIZE 1024 | |
35 #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x)) | |
36 #define BUFFER_CAT(x) (&((x)[strlen(x)])) | |
37 | |
38 /* For PPC Use */ | |
39 #if HAVE_ALTIVEC==1 | |
40 extern int has_altivec(void); | |
41 #endif | |
42 | |
43 /** | |
44 * Structure for the private XviD context. | |
45 * This stores all the private context for the codec. | |
46 */ | |
47 typedef struct xvid_context { | |
48 void *encoder_handle; /** Handle for XviD Encoder */ | |
49 int xsize, ysize; /** Frame size */ | |
50 int vop_flags; /** VOP flags for XviD Encoder */ | |
51 int vol_flags; /** VOL flags for XviD Encoder */ | |
52 int me_flags; /** Motion Estimation flags */ | |
53 int qscale; /** Do we use constant scale? */ | |
54 int quicktime_format; /** Are we in a QT-based format? */ | |
55 AVFrame encoded_picture; /** Encoded frame information */ | |
56 char *twopassbuffer; /** Character buffer for two-pass */ | |
57 char *old_twopassbuffer; /** Old character buffer (two-pass) */ | |
58 char *twopassfile; /** second pass temp file name */ | |
59 unsigned char *intra_matrix; /** P-Frame Quant Matrix */ | |
60 unsigned char *inter_matrix; /** I-Frame Quant Matrix */ | |
61 } xvid_context_t; | |
62 | |
63 /** | |
64 * Structure for the private first-pass plugin. | |
65 */ | |
66 typedef struct xvid_ff_pass1 { | |
67 int version; /** XviD version */ | |
68 xvid_context_t *context; /** Pointer to private context */ | |
69 } xvid_ff_pass1_t; | |
70 | |
71 /* Prototypes - See function implementation for details */ | |
72 int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len); | |
73 int xvid_ff_2pass(void *ref, int opt, void *p1, void *p2); | |
74 void xvid_correct_framerate(AVCodecContext *avctx); | |
75 | |
76 /** | |
77 * Creates the private context for the encoder. | |
78 * All buffers are allocated, settings are loaded from the user, | |
79 * and the encoder context created. | |
80 * | |
81 * @param avctx AVCodecContext pointer to context | |
82 * @return Returns 0 on success, -1 on failure | |
83 */ | |
84 int ff_xvid_encode_init(AVCodecContext *avctx) { | |
85 int xerr, i; | |
86 int xvid_flags = avctx->flags; | |
87 xvid_context_t *x = avctx->priv_data; | |
88 uint16_t *intra, *inter; | |
89 int fd; | |
90 | |
91 xvid_plugin_single_t single; | |
92 xvid_ff_pass1_t rc2pass1; | |
93 xvid_plugin_2pass2_t rc2pass2; | |
94 xvid_gbl_init_t xvid_gbl_init; | |
95 xvid_enc_create_t xvid_enc_create; | |
96 xvid_enc_plugin_t plugins[7]; | |
97 | |
98 /* Bring in VOP flags from ffmpeg command-line */ | |
99 x->vop_flags = XVID_VOP_HALFPEL; /* Bare minimum quality */ | |
100 if( xvid_flags & CODEC_FLAG_4MV ) | |
101 x->vop_flags |= XVID_VOP_INTER4V; /* Level 3 */ | |
102 if( xvid_flags & CODEC_FLAG_TRELLIS_QUANT) | |
103 x->vop_flags |= XVID_VOP_TRELLISQUANT; /* Level 5 */ | |
104 if( xvid_flags & CODEC_FLAG_AC_PRED ) | |
105 x->vop_flags |= XVID_VOP_HQACPRED; /* Level 6 */ | |
106 if( xvid_flags & CODEC_FLAG_GRAY ) | |
107 x->vop_flags |= XVID_VOP_GREYSCALE; | |
108 | |
109 /* Decide which ME quality setting to use */ | |
110 x->me_flags = 0; | |
111 switch( avctx->me_method ) { | |
112 case ME_FULL: /* Quality 6 */ | |
113 x->me_flags |= XVID_ME_EXTSEARCH16 | |
114 | XVID_ME_EXTSEARCH8; | |
115 | |
116 case ME_EPZS: /* Quality 4 */ | |
117 x->me_flags |= XVID_ME_ADVANCEDDIAMOND8 | |
118 | XVID_ME_HALFPELREFINE8 | |
119 | XVID_ME_CHROMA_PVOP | |
120 | XVID_ME_CHROMA_BVOP; | |
121 | |
122 case ME_LOG: /* Quality 2 */ | |
123 case ME_PHODS: | |
124 case ME_X1: | |
125 x->me_flags |= XVID_ME_ADVANCEDDIAMOND16 | |
126 | XVID_ME_HALFPELREFINE16; | |
127 | |
128 case ME_ZERO: /* Quality 0 */ | |
129 default: | |
130 break; | |
131 } | |
132 | |
133 /* Decide how we should decide blocks */ | |
134 switch( avctx->mb_decision ) { | |
135 case 2: | |
136 x->vop_flags |= XVID_VOP_MODEDECISION_RD; | |
137 x->me_flags |= XVID_ME_HALFPELREFINE8_RD | |
138 | XVID_ME_QUARTERPELREFINE8_RD | |
139 | XVID_ME_EXTSEARCH_RD | |
140 | XVID_ME_CHECKPREDICTION_RD; | |
141 case 1: | |
142 if( !(x->vop_flags & XVID_VOP_MODEDECISION_RD) ) | |
143 x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD; | |
144 x->me_flags |= XVID_ME_HALFPELREFINE16_RD | |
145 | XVID_ME_QUARTERPELREFINE16_RD; | |
146 | |
147 default: | |
148 break; | |
149 } | |
150 | |
151 /* Bring in VOL flags from ffmpeg command-line */ | |
152 x->vol_flags = 0; | |
153 if( xvid_flags & CODEC_FLAG_GMC ) { | |
154 x->vol_flags |= XVID_VOL_GMC; | |
155 x->me_flags |= XVID_ME_GME_REFINE; | |
156 } | |
157 if( xvid_flags & CODEC_FLAG_QPEL ) { | |
158 x->vol_flags |= XVID_VOL_QUARTERPEL; | |
159 x->me_flags |= XVID_ME_QUARTERPELREFINE16; | |
160 if( x->vop_flags & XVID_VOP_INTER4V ) | |
161 x->me_flags |= XVID_ME_QUARTERPELREFINE8; | |
162 } | |
163 | |
164 memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); | |
165 xvid_gbl_init.version = XVID_VERSION; | |
166 xvid_gbl_init.debug = 0; | |
167 | |
168 #ifdef ARCH_POWERPC | |
169 /* XviD's PPC support is borked, use libavcodec to detect */ | |
170 #if HAVE_ALTIVEC==1 | |
171 if( has_altivec() ) { | |
172 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC; | |
173 } else | |
174 #endif | |
175 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; | |
176 #else | |
177 /* XviD can detect on x86 */ | |
178 xvid_gbl_init.cpu_flags = 0; | |
179 #endif | |
180 | |
181 /* Initialize */ | |
182 xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); | |
183 | |
184 /* Create the encoder reference */ | |
185 memset(&xvid_enc_create, 0, sizeof(xvid_enc_create)); | |
186 xvid_enc_create.version = XVID_VERSION; | |
187 | |
188 /* Store the desired frame size */ | |
189 xvid_enc_create.width = x->xsize = avctx->width; | |
190 xvid_enc_create.height = x->ysize = avctx->height; | |
191 | |
192 /* XviD can determine the proper profile to use */ | |
193 /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */ | |
194 | |
195 /* We don't use zones or threads */ | |
196 xvid_enc_create.zones = NULL; | |
197 xvid_enc_create.num_zones = 0; | |
198 xvid_enc_create.num_threads = 0; | |
199 | |
200 xvid_enc_create.plugins = plugins; | |
201 xvid_enc_create.num_plugins = 0; | |
202 | |
203 /* Initialize Buffers */ | |
204 x->twopassbuffer = NULL; | |
205 x->old_twopassbuffer = NULL; | |
206 x->twopassfile = NULL; | |
207 | |
208 if( xvid_flags & CODEC_FLAG_PASS1 ) { | |
209 memset(&rc2pass1, 0, sizeof(xvid_ff_pass1_t)); | |
210 rc2pass1.version = XVID_VERSION; | |
211 rc2pass1.context = x; | |
212 x->twopassbuffer = av_malloc(BUFFER_SIZE); | |
213 x->old_twopassbuffer = av_malloc(BUFFER_SIZE); | |
214 if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) { | |
215 av_log(avctx, AV_LOG_ERROR, | |
216 "XviD: Cannot allocate 2-pass log buffers\n"); | |
217 return -1; | |
218 } | |
219 x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0; | |
220 | |
221 plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass; | |
222 plugins[xvid_enc_create.num_plugins].param = &rc2pass1; | |
223 xvid_enc_create.num_plugins++; | |
224 } else if( xvid_flags & CODEC_FLAG_PASS2 ) { | |
225 memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t)); | |
226 rc2pass2.version = XVID_VERSION; | |
227 rc2pass2.bitrate = avctx->bit_rate; | |
228 | |
229 x->twopassfile = av_malloc(BUFFER_SIZE); | |
230 if( x->twopassfile == NULL ) { | |
231 av_log(avctx, AV_LOG_ERROR, | |
232 "XviD: Cannot allocate 2-pass buffer\n"); | |
233 return -1; | |
234 } | |
235 strcpy(x->twopassfile, "/tmp/xvidff.XXXXXX"); | |
236 fd = mkstemp(x->twopassfile); | |
237 if( fd == -1 ) { | |
238 av_log(avctx, AV_LOG_ERROR, | |
239 "XviD: Cannot write 2-pass pipe\n"); | |
240 return -1; | |
241 } | |
242 | |
243 if( avctx->stats_in == NULL ) { | |
244 av_log(avctx, AV_LOG_ERROR, | |
245 "XviD: No 2-pass information loaded for second pass\n"); | |
246 return -1; | |
247 } | |
248 | |
249 if( strlen(avctx->stats_in) > | |
250 write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) { | |
251 close(fd); | |
252 av_log(avctx, AV_LOG_ERROR, | |
253 "XviD: Cannot write to 2-pass pipe\n"); | |
254 return -1; | |
255 } | |
256 | |
257 close(fd); | |
258 rc2pass2.filename = x->twopassfile; | |
259 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2; | |
260 plugins[xvid_enc_create.num_plugins].param = &rc2pass2; | |
261 xvid_enc_create.num_plugins++; | |
262 } else if( !(xvid_flags & CODEC_FLAG_QSCALE) ) { | |
263 /* Single Pass Bitrate Control! */ | |
264 memset(&single, 0, sizeof(xvid_plugin_single_t)); | |
265 single.version = XVID_VERSION; | |
266 single.bitrate = avctx->bit_rate; | |
267 | |
268 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single; | |
269 plugins[xvid_enc_create.num_plugins].param = &single; | |
270 xvid_enc_create.num_plugins++; | |
271 } | |
272 | |
273 /* Luminance Masking */ | |
274 if( 0.0 != avctx->lumi_masking ) { | |
275 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking; | |
276 plugins[xvid_enc_create.num_plugins].param = NULL; | |
277 xvid_enc_create.num_plugins++; | |
278 } | |
279 | |
280 /* Frame Rate and Key Frames */ | |
281 xvid_correct_framerate(avctx); | |
282 xvid_enc_create.fincr = avctx->frame_rate_base; | |
283 xvid_enc_create.fbase = avctx->frame_rate; | |
284 if( avctx->gop_size > 0 ) | |
285 xvid_enc_create.max_key_interval = avctx->gop_size; | |
286 else | |
287 xvid_enc_create.max_key_interval = 240; /* XviD's best default */ | |
288 | |
289 /* Quants */ | |
290 if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1; | |
291 else x->qscale = 0; | |
292 | |
293 xvid_enc_create.min_quant[0] = avctx->qmin; | |
294 xvid_enc_create.min_quant[1] = avctx->qmin; | |
295 xvid_enc_create.min_quant[2] = avctx->qmin; | |
296 xvid_enc_create.max_quant[0] = avctx->qmax; | |
297 xvid_enc_create.max_quant[1] = avctx->qmax; | |
298 xvid_enc_create.max_quant[2] = avctx->qmax; | |
299 | |
300 /* Quant Matrices */ | |
301 x->intra_matrix = x->inter_matrix = NULL; | |
302 if( avctx->mpeg_quant ) | |
303 x->vol_flags |= XVID_VOL_MPEGQUANT; | |
304 if( (avctx->intra_matrix || avctx->inter_matrix) ) { | |
305 x->vol_flags |= XVID_VOL_MPEGQUANT; | |
306 | |
307 if( avctx->intra_matrix ) { | |
308 intra = avctx->intra_matrix; | |
309 x->intra_matrix = av_malloc(sizeof(unsigned char) * 64); | |
310 } else | |
311 intra = NULL; | |
312 if( avctx->inter_matrix ) { | |
313 inter = avctx->inter_matrix; | |
314 x->inter_matrix = av_malloc(sizeof(unsigned char) * 64); | |
315 } else | |
316 inter = NULL; | |
317 | |
318 for( i = 0; i < 64; i++ ) { | |
319 if( intra ) | |
320 x->intra_matrix[i] = (unsigned char)intra[i]; | |
321 if( inter ) | |
322 x->inter_matrix[i] = (unsigned char)inter[i]; | |
323 } | |
324 } | |
325 | |
326 /* Misc Settings */ | |
327 xvid_enc_create.frame_drop_ratio = 0; | |
328 xvid_enc_create.global = 0; | |
329 if( xvid_flags & CODEC_FLAG_CLOSED_GOP ) | |
330 xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP; | |
331 | |
332 /* Determines which codec mode we are operating in */ | |
333 avctx->extradata = NULL; | |
334 avctx->extradata_size = 0; | |
335 if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) { | |
336 /* In this case, we are claiming to be MPEG4 */ | |
337 x->quicktime_format = 1; | |
338 avctx->codec_id = CODEC_ID_MPEG4; | |
339 } else { | |
340 /* We are claiming to be XviD */ | |
341 x->quicktime_format = 0; | |
342 avctx->codec_tag = ff_get_fourcc("xvid"); | |
343 } | |
344 | |
345 /* Bframes */ | |
346 xvid_enc_create.max_bframes = avctx->max_b_frames; | |
347 xvid_enc_create.bquant_offset = avctx->b_quant_offset; | |
348 xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor; | |
349 if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED; | |
350 | |
351 /* Create encoder context */ | |
352 xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); | |
353 if( xerr ) { | |
354 av_log(avctx, AV_LOG_ERROR, "XviD: Could not create encoder reference\n"); | |
355 return -1; | |
356 } | |
357 | |
358 x->encoder_handle = xvid_enc_create.handle; | |
359 avctx->coded_frame = &x->encoded_picture; | |
360 | |
361 return 0; | |
362 } | |
363 | |
364 /** | |
365 * Encodes a single frame. | |
366 * | |
367 * @param avctx AVCodecContext pointer to context | |
368 * @param frame Pointer to encoded frame buffer | |
369 * @param buf_size Size of encoded frame buffer | |
370 * @param data Pointer to AVFrame of unencoded frame | |
371 * @return Returns 0 on success, -1 on failure | |
372 */ | |
373 int ff_xvid_encode_frame(AVCodecContext *avctx, | |
374 unsigned char *frame, int buf_size, void *data) { | |
375 int xerr, i; | |
376 char *tmp; | |
377 xvid_context_t *x = avctx->priv_data; | |
378 AVFrame *picture = data; | |
379 AVFrame *p = &(x->encoded_picture); | |
380 | |
381 xvid_enc_frame_t xvid_enc_frame; | |
382 xvid_enc_stats_t xvid_enc_stats; | |
383 | |
384 /* Start setting up the frame */ | |
385 memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame)); | |
386 xvid_enc_frame.version = XVID_VERSION; | |
387 memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); | |
388 xvid_enc_stats.version = XVID_VERSION; | |
389 *p = *picture; | |
390 | |
391 /* Let XviD know where to put the frame. */ | |
392 xvid_enc_frame.bitstream = frame; | |
393 xvid_enc_frame.length = buf_size; | |
394 | |
395 /* Initialize input image fields */ | |
396 if( avctx->pix_fmt != PIX_FMT_YUV420P ) { | |
397 av_log(avctx, AV_LOG_ERROR, "XviD: Color spaces other than 420p not supported\n"); | |
398 return -1; | |
399 } | |
400 | |
401 xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */ | |
402 | |
403 for( i = 0; i < 4; i++ ) { | |
404 xvid_enc_frame.input.plane[i] = picture->data[i]; | |
405 xvid_enc_frame.input.stride[i] = picture->linesize[i]; | |
406 } | |
407 | |
408 /* Encoder Flags */ | |
409 xvid_enc_frame.vop_flags = x->vop_flags; | |
410 xvid_enc_frame.vol_flags = x->vol_flags; | |
411 xvid_enc_frame.motion = x->me_flags; | |
412 xvid_enc_frame.type = XVID_TYPE_AUTO; | |
413 | |
414 /* Quant Setting */ | |
415 if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA; | |
416 else xvid_enc_frame.quant = 0; | |
417 | |
418 /* Matrices */ | |
419 xvid_enc_frame.quant_intra_matrix = x->intra_matrix; | |
420 xvid_enc_frame.quant_inter_matrix = x->inter_matrix; | |
421 | |
422 /* Encode */ | |
423 xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE, | |
424 &xvid_enc_frame, &xvid_enc_stats); | |
425 | |
426 /* Two-pass log buffer swapping */ | |
427 avctx->stats_out = NULL; | |
428 if( x->twopassbuffer ) { | |
429 tmp = x->old_twopassbuffer; | |
430 x->old_twopassbuffer = x->twopassbuffer; | |
431 x->twopassbuffer = tmp; | |
432 x->twopassbuffer[0] = 0; | |
433 if( x->old_twopassbuffer[0] != 0 ) { | |
434 avctx->stats_out = x->old_twopassbuffer; | |
435 } | |
436 } | |
437 | |
438 if( 0 <= xerr ) { | |
439 p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA; | |
440 if( xvid_enc_stats.type == XVID_TYPE_PVOP ) | |
441 p->pict_type = FF_P_TYPE; | |
442 else if( xvid_enc_stats.type == XVID_TYPE_BVOP ) | |
443 p->pict_type = FF_B_TYPE; | |
444 else if( xvid_enc_stats.type == XVID_TYPE_SVOP ) | |
445 p->pict_type = FF_SP_TYPE; | |
446 else | |
447 p->pict_type = FF_I_TYPE; | |
448 if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) { | |
449 p->key_frame = 1; | |
450 if( x->quicktime_format ) | |
451 return xvid_strip_vol_header(avctx, frame, | |
452 xvid_enc_stats.hlength, xerr); | |
453 } else | |
454 p->key_frame = 0; | |
455 | |
456 return xerr; | |
457 } else { | |
458 av_log(avctx, AV_LOG_ERROR, "XviD: Encoding Error Occurred: %i\n", xerr); | |
459 return -1; | |
460 } | |
461 } | |
462 | |
463 /** | |
464 * Destroys the private context for the encoder. | |
465 * All buffers are freed, and the XviD encoder context is destroyed. | |
466 * | |
467 * @param avctx AVCodecContext pointer to context | |
468 * @return Returns 0, success guaranteed | |
469 */ | |
470 int ff_xvid_encode_close(AVCodecContext *avctx) { | |
471 xvid_context_t *x = avctx->priv_data; | |
472 | |
473 xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL); | |
474 | |
475 if( avctx->extradata != NULL ) | |
476 av_free(avctx->extradata); | |
477 if( x->twopassbuffer != NULL ) { | |
478 av_free(x->twopassbuffer); | |
479 av_free(x->old_twopassbuffer); | |
480 } | |
481 if( x->twopassfile != NULL ) | |
482 av_free(x->twopassfile); | |
483 if( x->intra_matrix != NULL ) | |
484 av_free(x->intra_matrix); | |
485 if( x->inter_matrix != NULL ) | |
486 av_free(x->inter_matrix); | |
487 | |
488 return 0; | |
489 } | |
490 | |
491 /** | |
492 * Routine to create a global VO/VOL header for MP4 container. | |
493 * What we do here is extract the header from the XviD bitstream | |
494 * as it is encoded. We also strip the repeated headers from the | |
495 * bitstream when a global header is requested for MPEG-4 ISO | |
496 * compliance. | |
497 * | |
498 * @param avctx AVCodecContext pointer to context | |
499 * @param frame Pointer to encoded frame data | |
500 * @param header_len Length of header to search | |
501 * @param frame_len Length of encoded frame data | |
502 * @return Returns new length of frame data | |
503 */ | |
504 int xvid_strip_vol_header(AVCodecContext *avctx, | |
505 unsigned char *frame, | |
506 unsigned int header_len, | |
507 unsigned int frame_len) { | |
508 int vo_len = 0, i; | |
509 | |
510 for( i = 0; i < header_len - 3; i++ ) { | |
511 if( frame[i] == 0x00 && | |
512 frame[i+1] == 0x00 && | |
513 frame[i+2] == 0x01 && | |
514 frame[i+3] == 0xB6 ) { | |
515 vo_len = i; | |
516 break; | |
517 } | |
518 } | |
519 | |
520 if( vo_len > 0 ) { | |
521 /* We need to store the header, so extract it */ | |
522 if( avctx->extradata == NULL ) { | |
523 avctx->extradata = av_malloc(vo_len); | |
524 memcpy(avctx->extradata, frame, vo_len); | |
525 avctx->extradata_size = vo_len; | |
526 } | |
527 /* Less dangerous now, memmove properly copies the two | |
528 chunks of overlapping data */ | |
529 memmove(frame, &(frame[vo_len]), frame_len - vo_len); | |
530 return frame_len - vo_len; | |
531 } else | |
532 return frame_len; | |
533 } | |
534 | |
535 /** | |
536 * Routine to correct a possibly erroneous framerate being fed to us. | |
537 * XviD currently chokes on framerates where the ticks per frame is | |
538 * extremely large. This function works to correct problems in this area | |
539 * by estimating a new framerate and taking the simpler fraction of | |
540 * the two presented. | |
541 * | |
542 * @param avctx Context that contains the framerate to correct. | |
543 */ | |
544 void xvid_correct_framerate(AVCodecContext *avctx) { | |
545 int frate, fbase; | |
546 int est_frate, est_fbase; | |
547 int gcd; | |
548 float est_fps, fps; | |
549 | |
550 frate = avctx->frame_rate; | |
551 fbase = avctx->frame_rate_base; | |
552 | |
553 gcd = ff_gcd(frate, fbase); | |
554 if( gcd > 1 ) { | |
555 frate /= gcd; | |
556 fbase /= gcd; | |
557 } | |
558 | |
559 if( frate <= 65000 && fbase <= 65000 ) { | |
560 avctx->frame_rate = frate; | |
561 avctx->frame_rate_base = fbase; | |
562 return; | |
563 } | |
564 | |
565 fps = (float)frate / (float)fbase; | |
566 est_fps = roundf(fps * 1000.0) / 1000.0; | |
567 | |
568 est_frate = (int)est_fps; | |
569 if( est_fps > (int)est_fps ) { | |
570 est_frate = (est_frate + 1) * 1000; | |
571 est_fbase = (int)roundf((float)est_frate / est_fps); | |
572 } else | |
573 est_fbase = 1; | |
574 | |
575 gcd = ff_gcd(est_frate, est_fbase); | |
576 if( gcd > 1 ) { | |
577 est_frate /= gcd; | |
578 est_fbase /= gcd; | |
579 } | |
580 | |
581 if( fbase > est_fbase ) { | |
582 avctx->frame_rate = est_frate; | |
583 avctx->frame_rate_base = est_fbase; | |
584 av_log(avctx, AV_LOG_DEBUG, | |
585 "XviD: framerate re-estimated: %.2f, %.3f%% correction\n", | |
586 est_fps, (((est_fps - fps)/fps) * 100.0)); | |
587 } else { | |
588 avctx->frame_rate = frate; | |
589 avctx->frame_rate_base = fbase; | |
590 } | |
591 } | |
592 | |
593 /* | |
594 * XviD 2-Pass Kludge Section | |
595 * | |
596 * XviD's default 2-pass doesn't allow us to create data as we need to, so | |
597 * this section spends time replacing the first pass plugin so we can write | |
598 * statistic information as libavcodec requests in. We have another kludge | |
599 * that allows us to pass data to the second pass in XviD without a custom | |
600 * rate-control plugin. | |
601 */ | |
602 | |
603 /** | |
604 * Initializes the two-pass plugin and context. | |
605 * | |
606 * @param param Input construction parameter structure | |
607 * @param handle Private context handle | |
608 * @return Returns XVID_ERR_xxxx on failure, or 0 on success. | |
609 */ | |
610 static int xvid_ff_2pass_create(xvid_plg_create_t * param, | |
611 void ** handle) { | |
612 xvid_ff_pass1_t *x = (xvid_ff_pass1_t *)param->param; | |
613 char *log = x->context->twopassbuffer; | |
614 | |
615 /* Do a quick bounds check */ | |
616 if( log == NULL ) | |
617 return XVID_ERR_FAIL; | |
618 | |
619 /* We use snprintf() */ | |
620 /* This is because we can safely prevent a buffer overflow */ | |
621 log[0] = 0; | |
622 snprintf(log, BUFFER_REMAINING(log), | |
623 "# ffmpeg 2-pass log file, using xvid codec\n"); | |
624 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log), | |
625 "# Do not modify. libxvidcore version: %d.%d.%d\n\n", | |
626 XVID_VERSION_MAJOR(XVID_VERSION), | |
627 XVID_VERSION_MINOR(XVID_VERSION), | |
628 XVID_VERSION_PATCH(XVID_VERSION)); | |
629 | |
630 *handle = x->context; | |
631 return 0; | |
632 } | |
633 | |
634 /** | |
635 * Destroys the two-pass plugin context. | |
636 * | |
637 * @param ref Context pointer for the plugin | |
638 * @param param Destrooy context | |
639 * @return Returns 0, success guaranteed | |
640 */ | |
641 static int xvid_ff_2pass_destroy(xvid_context_t *ref, | |
642 xvid_plg_destroy_t *param) { | |
643 /* Currently cannot think of anything to do on destruction */ | |
644 /* Still, the framework should be here for reference/use */ | |
645 if( ref->twopassbuffer != NULL ) | |
646 ref->twopassbuffer[0] = 0; | |
647 return 0; | |
648 } | |
649 | |
650 /** | |
651 * Enables fast encode mode during the first pass. | |
652 * | |
653 * @param ref Context pointer for the plugin | |
654 * @param param Frame data | |
655 * @return Returns 0, success guaranteed | |
656 */ | |
657 static int xvid_ff_2pass_before(xvid_context_t *ref, | |
658 xvid_plg_data_t *param) { | |
659 int motion_remove; | |
660 int motion_replacements; | |
661 int vop_remove; | |
662 | |
663 /* Nothing to do here, result is changed too much */ | |
664 if( param->zone && param->zone->mode == XVID_ZONE_QUANT ) | |
665 return 0; | |
666 | |
667 /* We can implement a 'turbo' first pass mode here */ | |
668 param->quant = 2; | |
669 | |
670 /* Init values */ | |
671 motion_remove = ~XVID_ME_CHROMA_PVOP & | |
672 ~XVID_ME_CHROMA_BVOP & | |
673 ~XVID_ME_EXTSEARCH16 & | |
674 ~XVID_ME_ADVANCEDDIAMOND16; | |
675 motion_replacements = XVID_ME_FAST_MODEINTERPOLATE | | |
676 XVID_ME_SKIP_DELTASEARCH | | |
677 XVID_ME_FASTREFINE16 | | |
678 XVID_ME_BFRAME_EARLYSTOP; | |
679 vop_remove = ~XVID_VOP_MODEDECISION_RD & | |
680 ~XVID_VOP_FAST_MODEDECISION_RD & | |
681 ~XVID_VOP_TRELLISQUANT & | |
682 ~XVID_VOP_INTER4V & | |
683 ~XVID_VOP_HQACPRED; | |
684 | |
685 param->vol_flags &= ~XVID_VOL_GMC; | |
686 param->vop_flags &= vop_remove; | |
687 param->motion_flags &= motion_remove; | |
688 param->motion_flags |= motion_replacements; | |
689 | |
690 return 0; | |
691 } | |
692 | |
693 /** | |
694 * Captures statistic data and writes it during first pass. | |
695 * | |
696 * @param ref Context pointer for the plugin | |
697 * @param param Statistic data | |
698 * @return Returns XVID_ERR_xxxx on failure, or 0 on success | |
699 */ | |
700 static int xvid_ff_2pass_after(xvid_context_t *ref, | |
701 xvid_plg_data_t *param) { | |
702 char *log = ref->twopassbuffer; | |
703 char *frame_types = " ipbs"; | |
704 char frame_type; | |
705 | |
706 /* Quick bounds check */ | |
707 if( log == NULL ) | |
708 return XVID_ERR_FAIL; | |
709 | |
710 /* Convert the type given to us into a character */ | |
711 if( param->type < 5 && param->type > 0 ) { | |
712 frame_type = frame_types[param->type]; | |
713 } else { | |
714 return XVID_ERR_FAIL; | |
715 } | |
716 | |
717 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log), | |
718 "%c %d %d %d %d %d %d\n", | |
719 frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks, | |
720 param->stats.ublks, param->stats.length, param->stats.hlength); | |
721 | |
722 return 0; | |
723 } | |
724 | |
725 /** | |
726 * Dispatch function for our custom plugin. | |
727 * This handles the dispatch for the XviD plugin. It passes data | |
728 * on to other functions for actual processing. | |
729 * | |
730 * @param ref Context pointer for the plugin | |
731 * @param cmd The task given for us to complete | |
732 * @param p1 First parameter (varies) | |
733 * @param p2 Second parameter (varies) | |
734 * @return Returns XVID_ERR_xxxx on failure, or 0 on success | |
735 */ | |
736 int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) { | |
737 switch( cmd ) { | |
738 case XVID_PLG_INFO: | |
739 case XVID_PLG_FRAME: | |
740 return 0; | |
741 | |
742 case XVID_PLG_BEFORE: | |
743 return xvid_ff_2pass_before(ref, p1); | |
744 | |
745 case XVID_PLG_CREATE: | |
746 return xvid_ff_2pass_create(p1, p2); | |
747 | |
748 case XVID_PLG_AFTER: | |
749 return xvid_ff_2pass_after(ref, p1); | |
750 | |
751 case XVID_PLG_DESTROY: | |
752 return xvid_ff_2pass_destroy(ref, p1); | |
753 | |
754 default: | |
755 return XVID_ERR_FAIL; | |
756 } | |
757 } | |
758 | |
759 /** | |
760 * XviD codec definition for libavcodec. | |
761 */ | |
762 AVCodec xvid_encoder = { | |
763 "xvid", | |
764 CODEC_TYPE_VIDEO, | |
765 CODEC_ID_XVID, | |
766 sizeof(xvid_context_t), | |
767 ff_xvid_encode_init, | |
768 ff_xvid_encode_frame, | |
769 ff_xvid_encode_close | |
770 }; |