2 * Interface to xvidcore for mpeg4 encoding
3 * Copyright (c) 2004 Adam Thayer <krevnik@comcast.net>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Interface to xvidcore for MPEG-4 compliant encoding.
25 * @author Adam Thayer (krevnik@comcast.net)
31 #include "libxvid_internal.h"
34 * Buffer management macros.
36 #define BUFFER_SIZE 1024
37 #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x))
38 #define BUFFER_CAT(x) (&((x)[strlen(x)]))
41 extern int has_altivec(void);
44 * Structure for the private XviD context.
45 * This stores all the private context for the codec.
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 */
64 * Structure for the private first-pass plugin.
66 typedef struct xvid_ff_pass1 {
67 int version; /** XviD version */
68 xvid_context_t *context; /** Pointer to private context */
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);
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.
81 * @param avctx AVCodecContext pointer to context
82 * @return Returns 0 on success, -1 on failure
84 av_cold int ff_xvid_encode_init(AVCodecContext *avctx) {
86 int xvid_flags = avctx->flags;
87 xvid_context_t *x = avctx->priv_data;
88 uint16_t *intra, *inter;
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];
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;
109 /* Decide which ME quality setting to use */
111 switch( avctx->me_method ) {
112 case ME_FULL: /* Quality 6 */
113 x->me_flags |= XVID_ME_EXTSEARCH16
114 | XVID_ME_EXTSEARCH8;
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;
122 case ME_LOG: /* Quality 2 */
125 x->me_flags |= XVID_ME_ADVANCEDDIAMOND16
126 | XVID_ME_HALFPELREFINE16;
128 case ME_ZERO: /* Quality 0 */
133 /* Decide how we should decide blocks */
134 switch( avctx->mb_decision ) {
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;
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;
151 /* Bring in VOL flags from ffmpeg command-line */
153 if( xvid_flags & CODEC_FLAG_GMC ) {
154 x->vol_flags |= XVID_VOL_GMC;
155 x->me_flags |= XVID_ME_GME_REFINE;
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;
164 memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
165 xvid_gbl_init.version = XVID_VERSION;
166 xvid_gbl_init.debug = 0;
169 /* XviD's PPC support is borked, use libavcodec to detect */
171 if( has_altivec() ) {
172 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC;
175 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
177 /* XviD can detect on x86 */
178 xvid_gbl_init.cpu_flags = 0;
182 xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
184 /* Create the encoder reference */
185 memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
186 xvid_enc_create.version = XVID_VERSION;
188 /* Store the desired frame size */
189 xvid_enc_create.width = x->xsize = avctx->width;
190 xvid_enc_create.height = x->ysize = avctx->height;
192 /* XviD can determine the proper profile to use */
193 /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */
195 /* We don't use zones */
196 xvid_enc_create.zones = NULL;
197 xvid_enc_create.num_zones = 0;
199 xvid_enc_create.num_threads = avctx->thread_count;
201 xvid_enc_create.plugins = plugins;
202 xvid_enc_create.num_plugins = 0;
204 /* Initialize Buffers */
205 x->twopassbuffer = NULL;
206 x->old_twopassbuffer = NULL;
207 x->twopassfile = NULL;
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");
220 x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0;
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;
230 fd = av_tempfile("xvidff.", &(x->twopassfile));
232 av_log(avctx, AV_LOG_ERROR,
233 "XviD: Cannot write 2-pass pipe\n");
237 if( avctx->stats_in == NULL ) {
238 av_log(avctx, AV_LOG_ERROR,
239 "XviD: No 2-pass information loaded for second pass\n");
243 if( strlen(avctx->stats_in) >
244 write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) {
246 av_log(avctx, AV_LOG_ERROR,
247 "XviD: Cannot write to 2-pass pipe\n");
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;
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++;
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++;
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;
281 xvid_enc_create.max_key_interval = 240; /* XviD's best default */
284 if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1;
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;
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;
301 if( avctx->intra_matrix ) {
302 intra = avctx->intra_matrix;
303 x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
306 if( avctx->inter_matrix ) {
307 inter = avctx->inter_matrix;
308 x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
312 for( i = 0; i < 64; i++ ) {
314 x->intra_matrix[i] = (unsigned char)intra[i];
316 x->inter_matrix[i] = (unsigned char)inter[i];
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;
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;
334 /* We are claiming to be XviD */
335 x->quicktime_format = 0;
336 if(!avctx->codec_tag)
337 avctx->codec_tag = ff_get_fourcc("xvid");
341 xvid_enc_create.max_bframes = avctx->max_b_frames;
342 xvid_enc_create.bquant_offset = 100 * avctx->b_quant_offset;
343 xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor;
344 if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED;
346 /* Create encoder context */
347 xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
349 av_log(avctx, AV_LOG_ERROR, "XviD: Could not create encoder reference\n");
353 x->encoder_handle = xvid_enc_create.handle;
354 avctx->coded_frame = &x->encoded_picture;
360 * Encodes a single frame.
362 * @param avctx AVCodecContext pointer to context
363 * @param frame Pointer to encoded frame buffer
364 * @param buf_size Size of encoded frame buffer
365 * @param data Pointer to AVFrame of unencoded frame
366 * @return Returns 0 on success, -1 on failure
368 int ff_xvid_encode_frame(AVCodecContext *avctx,
369 unsigned char *frame, int buf_size, void *data) {
372 xvid_context_t *x = avctx->priv_data;
373 AVFrame *picture = data;
374 AVFrame *p = &(x->encoded_picture);
376 xvid_enc_frame_t xvid_enc_frame;
377 xvid_enc_stats_t xvid_enc_stats;
379 /* Start setting up the frame */
380 memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
381 xvid_enc_frame.version = XVID_VERSION;
382 memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
383 xvid_enc_stats.version = XVID_VERSION;
386 /* Let XviD know where to put the frame. */
387 xvid_enc_frame.bitstream = frame;
388 xvid_enc_frame.length = buf_size;
390 /* Initialize input image fields */
391 if( avctx->pix_fmt != PIX_FMT_YUV420P ) {
392 av_log(avctx, AV_LOG_ERROR, "XviD: Color spaces other than 420p not supported\n");
396 xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */
398 for( i = 0; i < 4; i++ ) {
399 xvid_enc_frame.input.plane[i] = picture->data[i];
400 xvid_enc_frame.input.stride[i] = picture->linesize[i];
404 xvid_enc_frame.vop_flags = x->vop_flags;
405 xvid_enc_frame.vol_flags = x->vol_flags;
406 xvid_enc_frame.motion = x->me_flags;
407 xvid_enc_frame.type = XVID_TYPE_AUTO;
410 if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;
411 else xvid_enc_frame.quant = 0;
414 xvid_enc_frame.quant_intra_matrix = x->intra_matrix;
415 xvid_enc_frame.quant_inter_matrix = x->inter_matrix;
418 xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,
419 &xvid_enc_frame, &xvid_enc_stats);
421 /* Two-pass log buffer swapping */
422 avctx->stats_out = NULL;
423 if( x->twopassbuffer ) {
424 tmp = x->old_twopassbuffer;
425 x->old_twopassbuffer = x->twopassbuffer;
426 x->twopassbuffer = tmp;
427 x->twopassbuffer[0] = 0;
428 if( x->old_twopassbuffer[0] != 0 ) {
429 avctx->stats_out = x->old_twopassbuffer;
434 p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;
435 if( xvid_enc_stats.type == XVID_TYPE_PVOP )
436 p->pict_type = FF_P_TYPE;
437 else if( xvid_enc_stats.type == XVID_TYPE_BVOP )
438 p->pict_type = FF_B_TYPE;
439 else if( xvid_enc_stats.type == XVID_TYPE_SVOP )
440 p->pict_type = FF_S_TYPE;
442 p->pict_type = FF_I_TYPE;
443 if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) {
445 if( x->quicktime_format )
446 return xvid_strip_vol_header(avctx, frame,
447 xvid_enc_stats.hlength, xerr);
453 av_log(avctx, AV_LOG_ERROR, "XviD: Encoding Error Occurred: %i\n", xerr);
459 * Destroys the private context for the encoder.
460 * All buffers are freed, and the XviD encoder context is destroyed.
462 * @param avctx AVCodecContext pointer to context
463 * @return Returns 0, success guaranteed
465 av_cold int ff_xvid_encode_close(AVCodecContext *avctx) {
466 xvid_context_t *x = avctx->priv_data;
468 xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
470 if( avctx->extradata != NULL )
471 av_free(avctx->extradata);
472 if( x->twopassbuffer != NULL ) {
473 av_free(x->twopassbuffer);
474 av_free(x->old_twopassbuffer);
476 if( x->twopassfile != NULL )
477 av_free(x->twopassfile);
478 if( x->intra_matrix != NULL )
479 av_free(x->intra_matrix);
480 if( x->inter_matrix != NULL )
481 av_free(x->inter_matrix);
487 * Routine to create a global VO/VOL header for MP4 container.
488 * What we do here is extract the header from the XviD bitstream
489 * as it is encoded. We also strip the repeated headers from the
490 * bitstream when a global header is requested for MPEG-4 ISO
493 * @param avctx AVCodecContext pointer to context
494 * @param frame Pointer to encoded frame data
495 * @param header_len Length of header to search
496 * @param frame_len Length of encoded frame data
497 * @return Returns new length of frame data
499 int xvid_strip_vol_header(AVCodecContext *avctx,
500 unsigned char *frame,
501 unsigned int header_len,
502 unsigned int frame_len) {
505 for( i = 0; i < header_len - 3; i++ ) {
506 if( frame[i] == 0x00 &&
507 frame[i+1] == 0x00 &&
508 frame[i+2] == 0x01 &&
509 frame[i+3] == 0xB6 ) {
516 /* We need to store the header, so extract it */
517 if( avctx->extradata == NULL ) {
518 avctx->extradata = av_malloc(vo_len);
519 memcpy(avctx->extradata, frame, vo_len);
520 avctx->extradata_size = vo_len;
522 /* Less dangerous now, memmove properly copies the two
523 chunks of overlapping data */
524 memmove(frame, &(frame[vo_len]), frame_len - vo_len);
525 return frame_len - vo_len;
531 * Routine to correct a possibly erroneous framerate being fed to us.
532 * XviD currently chokes on framerates where the ticks per frame is
533 * extremely large. This function works to correct problems in this area
534 * by estimating a new framerate and taking the simpler fraction of
537 * @param avctx Context that contains the framerate to correct.
539 void xvid_correct_framerate(AVCodecContext *avctx) {
541 int est_frate, est_fbase;
545 frate = avctx->time_base.den;
546 fbase = avctx->time_base.num;
548 gcd = ff_gcd(frate, fbase);
554 if( frate <= 65000 && fbase <= 65000 ) {
555 avctx->time_base.den = frate;
556 avctx->time_base.num = fbase;
560 fps = (float)frate / (float)fbase;
561 est_fps = roundf(fps * 1000.0) / 1000.0;
563 est_frate = (int)est_fps;
564 if( est_fps > (int)est_fps ) {
565 est_frate = (est_frate + 1) * 1000;
566 est_fbase = (int)roundf((float)est_frate / est_fps);
570 gcd = ff_gcd(est_frate, est_fbase);
576 if( fbase > est_fbase ) {
577 avctx->time_base.den = est_frate;
578 avctx->time_base.num = est_fbase;
579 av_log(avctx, AV_LOG_DEBUG,
580 "XviD: framerate re-estimated: %.2f, %.3f%% correction\n",
581 est_fps, (((est_fps - fps)/fps) * 100.0));
583 avctx->time_base.den = frate;
584 avctx->time_base.num = fbase;
589 * XviD 2-Pass Kludge Section
591 * XviD's default 2-pass doesn't allow us to create data as we need to, so
592 * this section spends time replacing the first pass plugin so we can write
593 * statistic information as libavcodec requests in. We have another kludge
594 * that allows us to pass data to the second pass in XviD without a custom
595 * rate-control plugin.
599 * Initializes the two-pass plugin and context.
601 * @param param Input construction parameter structure
602 * @param handle Private context handle
603 * @return Returns XVID_ERR_xxxx on failure, or 0 on success.
605 static int xvid_ff_2pass_create(xvid_plg_create_t * param,
607 xvid_ff_pass1_t *x = (xvid_ff_pass1_t *)param->param;
608 char *log = x->context->twopassbuffer;
610 /* Do a quick bounds check */
612 return XVID_ERR_FAIL;
614 /* We use snprintf() */
615 /* This is because we can safely prevent a buffer overflow */
617 snprintf(log, BUFFER_REMAINING(log),
618 "# ffmpeg 2-pass log file, using xvid codec\n");
619 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
620 "# Do not modify. libxvidcore version: %d.%d.%d\n\n",
621 XVID_VERSION_MAJOR(XVID_VERSION),
622 XVID_VERSION_MINOR(XVID_VERSION),
623 XVID_VERSION_PATCH(XVID_VERSION));
625 *handle = x->context;
630 * Destroys the two-pass plugin context.
632 * @param ref Context pointer for the plugin
633 * @param param Destrooy context
634 * @return Returns 0, success guaranteed
636 static int xvid_ff_2pass_destroy(xvid_context_t *ref,
637 xvid_plg_destroy_t *param) {
638 /* Currently cannot think of anything to do on destruction */
639 /* Still, the framework should be here for reference/use */
640 if( ref->twopassbuffer != NULL )
641 ref->twopassbuffer[0] = 0;
646 * Enables fast encode mode during the first pass.
648 * @param ref Context pointer for the plugin
649 * @param param Frame data
650 * @return Returns 0, success guaranteed
652 static int xvid_ff_2pass_before(xvid_context_t *ref,
653 xvid_plg_data_t *param) {
655 int motion_replacements;
658 /* Nothing to do here, result is changed too much */
659 if( param->zone && param->zone->mode == XVID_ZONE_QUANT )
662 /* We can implement a 'turbo' first pass mode here */
666 motion_remove = ~XVID_ME_CHROMA_PVOP &
667 ~XVID_ME_CHROMA_BVOP &
668 ~XVID_ME_EXTSEARCH16 &
669 ~XVID_ME_ADVANCEDDIAMOND16;
670 motion_replacements = XVID_ME_FAST_MODEINTERPOLATE |
671 XVID_ME_SKIP_DELTASEARCH |
672 XVID_ME_FASTREFINE16 |
673 XVID_ME_BFRAME_EARLYSTOP;
674 vop_remove = ~XVID_VOP_MODEDECISION_RD &
675 ~XVID_VOP_FAST_MODEDECISION_RD &
676 ~XVID_VOP_TRELLISQUANT &
680 param->vol_flags &= ~XVID_VOL_GMC;
681 param->vop_flags &= vop_remove;
682 param->motion_flags &= motion_remove;
683 param->motion_flags |= motion_replacements;
689 * Captures statistic data and writes it during first pass.
691 * @param ref Context pointer for the plugin
692 * @param param Statistic data
693 * @return Returns XVID_ERR_xxxx on failure, or 0 on success
695 static int xvid_ff_2pass_after(xvid_context_t *ref,
696 xvid_plg_data_t *param) {
697 char *log = ref->twopassbuffer;
698 char *frame_types = " ipbs";
701 /* Quick bounds check */
703 return XVID_ERR_FAIL;
705 /* Convert the type given to us into a character */
706 if( param->type < 5 && param->type > 0 ) {
707 frame_type = frame_types[param->type];
709 return XVID_ERR_FAIL;
712 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
713 "%c %d %d %d %d %d %d\n",
714 frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks,
715 param->stats.ublks, param->stats.length, param->stats.hlength);
721 * Dispatch function for our custom plugin.
722 * This handles the dispatch for the XviD plugin. It passes data
723 * on to other functions for actual processing.
725 * @param ref Context pointer for the plugin
726 * @param cmd The task given for us to complete
727 * @param p1 First parameter (varies)
728 * @param p2 Second parameter (varies)
729 * @return Returns XVID_ERR_xxxx on failure, or 0 on success
731 int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) {
737 case XVID_PLG_BEFORE:
738 return xvid_ff_2pass_before(ref, p1);
740 case XVID_PLG_CREATE:
741 return xvid_ff_2pass_create(p1, p2);
744 return xvid_ff_2pass_after(ref, p1);
746 case XVID_PLG_DESTROY:
747 return xvid_ff_2pass_destroy(ref, p1);
750 return XVID_ERR_FAIL;
755 * XviD codec definition for libavcodec.
757 AVCodec libxvid_encoder = {
761 sizeof(xvid_context_t),
763 ff_xvid_encode_frame,
764 ff_xvid_encode_close,
765 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},