VirtualBox

source: vbox/trunk/src/libs/ffmpeg-20060710/libavcodec/xvidff.c@ 9441

Last change on this file since 9441 was 5776, checked in by vboxsync, 17 years ago

ffmpeg: exported to OSE

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette