2 * Interface to xvidcore for mpeg4 encoding
3 * Copyright (c) 2004 Adam Thayer <krevnik@comcast.net>
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.
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.
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
22 * Interface to xvidcore for MPEG-4 compliant encoding.
23 * @author Adam Thayer (krevnik@comcast.net)
35 * Buffer management macros.
37 #define BUFFER_SIZE 1024
38 #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x))
39 #define BUFFER_CAT(x) (&((x)[strlen(x)]))
43 extern int has_altivec(void);
47 * Structure for the private XviD context.
48 * This stores all the private context for the codec.
50 typedef struct xvid_context {
51 void *encoder_handle; /** Handle for XviD Encoder */
52 int xsize, ysize; /** Frame size */
53 int vop_flags; /** VOP flags for XviD Encoder */
54 int vol_flags; /** VOL flags for XviD Encoder */
55 int me_flags; /** Motion Estimation flags */
56 int qscale; /** Do we use constant scale? */
57 int quicktime_format; /** Are we in a QT-based format? */
58 AVFrame encoded_picture; /** Encoded frame information */
59 char *twopassbuffer; /** Character buffer for two-pass */
60 char *old_twopassbuffer; /** Old character buffer (two-pass) */
61 char *twopassfile; /** second pass temp file name */
62 unsigned char *intra_matrix; /** P-Frame Quant Matrix */
63 unsigned char *inter_matrix; /** I-Frame Quant Matrix */
67 * Structure for the private first-pass plugin.
69 typedef struct xvid_ff_pass1 {
70 int version; /** XviD version */
71 xvid_context_t *context; /** Pointer to private context */
74 /* Prototypes - See function implementation for details */
75 int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len);
76 int xvid_ff_2pass(void *ref, int opt, void *p1, void *p2);
77 void xvid_correct_framerate(AVCodecContext *avctx);
80 * Creates the private context for the encoder.
81 * All buffers are allocated, settings are loaded from the user,
82 * and the encoder context created.
84 * @param avctx AVCodecContext pointer to context
85 * @return Returns 0 on success, -1 on failure
87 int ff_xvid_encode_init(AVCodecContext *avctx) {
89 int xvid_flags = avctx->flags;
90 xvid_context_t *x = avctx->priv_data;
91 uint16_t *intra, *inter;
94 xvid_plugin_single_t single;
95 xvid_ff_pass1_t rc2pass1;
96 xvid_plugin_2pass2_t rc2pass2;
97 xvid_gbl_init_t xvid_gbl_init;
98 xvid_enc_create_t xvid_enc_create;
99 xvid_enc_plugin_t plugins[7];
101 /* Bring in VOP flags from ffmpeg command-line */
102 x->vop_flags = XVID_VOP_HALFPEL; /* Bare minimum quality */
103 if( xvid_flags & CODEC_FLAG_4MV )
104 x->vop_flags |= XVID_VOP_INTER4V; /* Level 3 */
105 if( xvid_flags & CODEC_FLAG_TRELLIS_QUANT)
106 x->vop_flags |= XVID_VOP_TRELLISQUANT; /* Level 5 */
107 if( xvid_flags & CODEC_FLAG_AC_PRED )
108 x->vop_flags |= XVID_VOP_HQACPRED; /* Level 6 */
109 if( xvid_flags & CODEC_FLAG_GRAY )
110 x->vop_flags |= XVID_VOP_GREYSCALE;
112 /* Decide which ME quality setting to use */
114 switch( avctx->me_method ) {
115 case ME_FULL: /* Quality 6 */
116 x->me_flags |= XVID_ME_EXTSEARCH16
117 | XVID_ME_EXTSEARCH8;
119 case ME_EPZS: /* Quality 4 */
120 x->me_flags |= XVID_ME_ADVANCEDDIAMOND8
121 | XVID_ME_HALFPELREFINE8
122 | XVID_ME_CHROMA_PVOP
123 | XVID_ME_CHROMA_BVOP;
125 case ME_LOG: /* Quality 2 */
128 x->me_flags |= XVID_ME_ADVANCEDDIAMOND16
129 | XVID_ME_HALFPELREFINE16;
131 case ME_ZERO: /* Quality 0 */
136 /* Decide how we should decide blocks */
137 switch( avctx->mb_decision ) {
139 x->vop_flags |= XVID_VOP_MODEDECISION_RD;
140 x->me_flags |= XVID_ME_HALFPELREFINE8_RD
141 | XVID_ME_QUARTERPELREFINE8_RD
142 | XVID_ME_EXTSEARCH_RD
143 | XVID_ME_CHECKPREDICTION_RD;
145 if( !(x->vop_flags & XVID_VOP_MODEDECISION_RD) )
146 x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD;
147 x->me_flags |= XVID_ME_HALFPELREFINE16_RD
148 | XVID_ME_QUARTERPELREFINE16_RD;
154 /* Bring in VOL flags from ffmpeg command-line */
156 if( xvid_flags & CODEC_FLAG_GMC ) {
157 x->vol_flags |= XVID_VOL_GMC;
158 x->me_flags |= XVID_ME_GME_REFINE;
160 if( xvid_flags & CODEC_FLAG_QPEL ) {
161 x->vol_flags |= XVID_VOL_QUARTERPEL;
162 x->me_flags |= XVID_ME_QUARTERPELREFINE16;
163 if( x->vop_flags & XVID_VOP_INTER4V )
164 x->me_flags |= XVID_ME_QUARTERPELREFINE8;
167 memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
168 xvid_gbl_init.version = XVID_VERSION;
169 xvid_gbl_init.debug = 0;
172 /* XviD's PPC support is borked, use libavcodec to detect */
174 if( has_altivec() ) {
175 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC;
178 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
180 /* XviD can detect on x86 */
181 xvid_gbl_init.cpu_flags = 0;
185 xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
187 /* Create the encoder reference */
188 memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
189 xvid_enc_create.version = XVID_VERSION;
191 /* Store the desired frame size */
192 xvid_enc_create.width = x->xsize = avctx->width;
193 xvid_enc_create.height = x->ysize = avctx->height;
195 /* XviD can determine the proper profile to use */
196 /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */
198 /* We don't use zones or threads */
199 xvid_enc_create.zones = NULL;
200 xvid_enc_create.num_zones = 0;
201 xvid_enc_create.num_threads = 0;
203 xvid_enc_create.plugins = plugins;
204 xvid_enc_create.num_plugins = 0;
206 /* Initialize Buffers */
207 x->twopassbuffer = NULL;
208 x->old_twopassbuffer = NULL;
209 x->twopassfile = NULL;
211 if( xvid_flags & CODEC_FLAG_PASS1 ) {
212 memset(&rc2pass1, 0, sizeof(xvid_ff_pass1_t));
213 rc2pass1.version = XVID_VERSION;
214 rc2pass1.context = x;
215 x->twopassbuffer = av_malloc(BUFFER_SIZE);
216 x->old_twopassbuffer = av_malloc(BUFFER_SIZE);
217 if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) {
218 av_log(avctx, AV_LOG_ERROR,
219 "XviD: Cannot allocate 2-pass log buffers\n");
222 x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0;
224 plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass;
225 plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
226 xvid_enc_create.num_plugins++;
227 } else if( xvid_flags & CODEC_FLAG_PASS2 ) {
228 memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t));
229 rc2pass2.version = XVID_VERSION;
230 rc2pass2.bitrate = avctx->bit_rate;
232 #ifdef CONFIG_WIN32 /* Ugly work around */
236 tempname = tempnam(".", "xvidff");
239 (fd = open(tempname, _O_RDWR | _O_BINARY)) != -1 ) {
240 x->twopassfile = av_strdup(tempname);
243 #define free please_use_av_free
244 if( x->twopassfile == NULL ) {
245 av_log(avctx, AV_LOG_ERROR,
246 "XviD: Cannot allocate 2-pass buffer\n");
252 x->twopassfile = av_malloc(BUFFER_SIZE);
253 if( x->twopassfile == NULL ) {
254 av_log(avctx, AV_LOG_ERROR,
255 "XviD: Cannot allocate 2-pass buffer\n");
258 strcpy(x->twopassfile, "/tmp/xvidff.XXXXXX");
259 fd = mkstemp(x->twopassfile);
261 strcpy(x->twopassfile, "./xvidff.XXXXXX");
262 fd = mkstemp(x->twopassfile);
266 av_log(avctx, AV_LOG_ERROR,
267 "XviD: Cannot write 2-pass pipe\n");
271 if( avctx->stats_in == NULL ) {
272 av_log(avctx, AV_LOG_ERROR,
273 "XviD: No 2-pass information loaded for second pass\n");
277 if( strlen(avctx->stats_in) >
278 write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) {
280 av_log(avctx, AV_LOG_ERROR,
281 "XviD: Cannot write to 2-pass pipe\n");
286 rc2pass2.filename = x->twopassfile;
287 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
288 plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
289 xvid_enc_create.num_plugins++;
290 } else if( !(xvid_flags & CODEC_FLAG_QSCALE) ) {
291 /* Single Pass Bitrate Control! */
292 memset(&single, 0, sizeof(xvid_plugin_single_t));
293 single.version = XVID_VERSION;
294 single.bitrate = avctx->bit_rate;
296 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
297 plugins[xvid_enc_create.num_plugins].param = &single;
298 xvid_enc_create.num_plugins++;
301 /* Luminance Masking */
302 if( 0.0 != avctx->lumi_masking ) {
303 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
304 plugins[xvid_enc_create.num_plugins].param = NULL;
305 xvid_enc_create.num_plugins++;
308 /* Frame Rate and Key Frames */
309 xvid_correct_framerate(avctx);
310 xvid_enc_create.fincr = avctx->time_base.num;
311 xvid_enc_create.fbase = avctx->time_base.den;
312 if( avctx->gop_size > 0 )
313 xvid_enc_create.max_key_interval = avctx->gop_size;
315 xvid_enc_create.max_key_interval = 240; /* XviD's best default */
318 if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1;
321 xvid_enc_create.min_quant[0] = avctx->qmin;
322 xvid_enc_create.min_quant[1] = avctx->qmin;
323 xvid_enc_create.min_quant[2] = avctx->qmin;
324 xvid_enc_create.max_quant[0] = avctx->qmax;
325 xvid_enc_create.max_quant[1] = avctx->qmax;
326 xvid_enc_create.max_quant[2] = avctx->qmax;
329 x->intra_matrix = x->inter_matrix = NULL;
330 if( avctx->mpeg_quant )
331 x->vol_flags |= XVID_VOL_MPEGQUANT;
332 if( (avctx->intra_matrix || avctx->inter_matrix) ) {
333 x->vol_flags |= XVID_VOL_MPEGQUANT;
335 if( avctx->intra_matrix ) {
336 intra = avctx->intra_matrix;
337 x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
340 if( avctx->inter_matrix ) {
341 inter = avctx->inter_matrix;
342 x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
346 for( i = 0; i < 64; i++ ) {
348 x->intra_matrix[i] = (unsigned char)intra[i];
350 x->inter_matrix[i] = (unsigned char)inter[i];
355 xvid_enc_create.frame_drop_ratio = 0;
356 xvid_enc_create.global = 0;
357 if( xvid_flags & CODEC_FLAG_CLOSED_GOP )
358 xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
360 /* Determines which codec mode we are operating in */
361 avctx->extradata = NULL;
362 avctx->extradata_size = 0;
363 if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) {
364 /* In this case, we are claiming to be MPEG4 */
365 x->quicktime_format = 1;
366 avctx->codec_id = CODEC_ID_MPEG4;
368 /* We are claiming to be XviD */
369 x->quicktime_format = 0;
370 avctx->codec_tag = ff_get_fourcc("xvid");
374 xvid_enc_create.max_bframes = avctx->max_b_frames;
375 xvid_enc_create.bquant_offset = avctx->b_quant_offset;
376 xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor;
377 if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED;
379 /* Create encoder context */
380 xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
382 av_log(avctx, AV_LOG_ERROR, "XviD: Could not create encoder reference\n");
386 x->encoder_handle = xvid_enc_create.handle;
387 avctx->coded_frame = &x->encoded_picture;
393 * Encodes a single frame.
395 * @param avctx AVCodecContext pointer to context
396 * @param frame Pointer to encoded frame buffer
397 * @param buf_size Size of encoded frame buffer
398 * @param data Pointer to AVFrame of unencoded frame
399 * @return Returns 0 on success, -1 on failure
401 int ff_xvid_encode_frame(AVCodecContext *avctx,
402 unsigned char *frame, int buf_size, void *data) {
405 xvid_context_t *x = avctx->priv_data;
406 AVFrame *picture = data;
407 AVFrame *p = &(x->encoded_picture);
409 xvid_enc_frame_t xvid_enc_frame;
410 xvid_enc_stats_t xvid_enc_stats;
412 /* Start setting up the frame */
413 memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
414 xvid_enc_frame.version = XVID_VERSION;
415 memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
416 xvid_enc_stats.version = XVID_VERSION;
419 /* Let XviD know where to put the frame. */
420 xvid_enc_frame.bitstream = frame;
421 xvid_enc_frame.length = buf_size;
423 /* Initialize input image fields */
424 if( avctx->pix_fmt != PIX_FMT_YUV420P ) {
425 av_log(avctx, AV_LOG_ERROR, "XviD: Color spaces other than 420p not supported\n");
429 xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */
431 for( i = 0; i < 4; i++ ) {
432 xvid_enc_frame.input.plane[i] = picture->data[i];
433 xvid_enc_frame.input.stride[i] = picture->linesize[i];
437 xvid_enc_frame.vop_flags = x->vop_flags;
438 xvid_enc_frame.vol_flags = x->vol_flags;
439 xvid_enc_frame.motion = x->me_flags;
440 xvid_enc_frame.type = XVID_TYPE_AUTO;
443 if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;
444 else xvid_enc_frame.quant = 0;
447 xvid_enc_frame.quant_intra_matrix = x->intra_matrix;
448 xvid_enc_frame.quant_inter_matrix = x->inter_matrix;
451 xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,
452 &xvid_enc_frame, &xvid_enc_stats);
454 /* Two-pass log buffer swapping */
455 avctx->stats_out = NULL;
456 if( x->twopassbuffer ) {
457 tmp = x->old_twopassbuffer;
458 x->old_twopassbuffer = x->twopassbuffer;
459 x->twopassbuffer = tmp;
460 x->twopassbuffer[0] = 0;
461 if( x->old_twopassbuffer[0] != 0 ) {
462 avctx->stats_out = x->old_twopassbuffer;
467 p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;
468 if( xvid_enc_stats.type == XVID_TYPE_PVOP )
469 p->pict_type = FF_P_TYPE;
470 else if( xvid_enc_stats.type == XVID_TYPE_BVOP )
471 p->pict_type = FF_B_TYPE;
472 else if( xvid_enc_stats.type == XVID_TYPE_SVOP )
473 p->pict_type = FF_S_TYPE;
475 p->pict_type = FF_I_TYPE;
476 if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) {
478 if( x->quicktime_format )
479 return xvid_strip_vol_header(avctx, frame,
480 xvid_enc_stats.hlength, xerr);
486 av_log(avctx, AV_LOG_ERROR, "XviD: Encoding Error Occurred: %i\n", xerr);
492 * Destroys the private context for the encoder.
493 * All buffers are freed, and the XviD encoder context is destroyed.
495 * @param avctx AVCodecContext pointer to context
496 * @return Returns 0, success guaranteed
498 int ff_xvid_encode_close(AVCodecContext *avctx) {
499 xvid_context_t *x = avctx->priv_data;
501 xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
503 if( avctx->extradata != NULL )
504 av_free(avctx->extradata);
505 if( x->twopassbuffer != NULL ) {
506 av_free(x->twopassbuffer);
507 av_free(x->old_twopassbuffer);
509 if( x->twopassfile != NULL )
510 av_free(x->twopassfile);
511 if( x->intra_matrix != NULL )
512 av_free(x->intra_matrix);
513 if( x->inter_matrix != NULL )
514 av_free(x->inter_matrix);
520 * Routine to create a global VO/VOL header for MP4 container.
521 * What we do here is extract the header from the XviD bitstream
522 * as it is encoded. We also strip the repeated headers from the
523 * bitstream when a global header is requested for MPEG-4 ISO
526 * @param avctx AVCodecContext pointer to context
527 * @param frame Pointer to encoded frame data
528 * @param header_len Length of header to search
529 * @param frame_len Length of encoded frame data
530 * @return Returns new length of frame data
532 int xvid_strip_vol_header(AVCodecContext *avctx,
533 unsigned char *frame,
534 unsigned int header_len,
535 unsigned int frame_len) {
538 for( i = 0; i < header_len - 3; i++ ) {
539 if( frame[i] == 0x00 &&
540 frame[i+1] == 0x00 &&
541 frame[i+2] == 0x01 &&
542 frame[i+3] == 0xB6 ) {
549 /* We need to store the header, so extract it */
550 if( avctx->extradata == NULL ) {
551 avctx->extradata = av_malloc(vo_len);
552 memcpy(avctx->extradata, frame, vo_len);
553 avctx->extradata_size = vo_len;
555 /* Less dangerous now, memmove properly copies the two
556 chunks of overlapping data */
557 memmove(frame, &(frame[vo_len]), frame_len - vo_len);
558 return frame_len - vo_len;
564 * Routine to correct a possibly erroneous framerate being fed to us.
565 * XviD currently chokes on framerates where the ticks per frame is
566 * extremely large. This function works to correct problems in this area
567 * by estimating a new framerate and taking the simpler fraction of
570 * @param avctx Context that contains the framerate to correct.
572 void xvid_correct_framerate(AVCodecContext *avctx) {
574 int est_frate, est_fbase;
578 frate = avctx->time_base.den;
579 fbase = avctx->time_base.num;
581 gcd = ff_gcd(frate, fbase);
587 if( frate <= 65000 && fbase <= 65000 ) {
588 avctx->time_base.den = frate;
589 avctx->time_base.num = fbase;
593 fps = (float)frate / (float)fbase;
594 est_fps = roundf(fps * 1000.0) / 1000.0;
596 est_frate = (int)est_fps;
597 if( est_fps > (int)est_fps ) {
598 est_frate = (est_frate + 1) * 1000;
599 est_fbase = (int)roundf((float)est_frate / est_fps);
603 gcd = ff_gcd(est_frate, est_fbase);
609 if( fbase > est_fbase ) {
610 avctx->time_base.den = est_frate;
611 avctx->time_base.num = est_fbase;
612 av_log(avctx, AV_LOG_DEBUG,
613 "XviD: framerate re-estimated: %.2f, %.3f%% correction\n",
614 est_fps, (((est_fps - fps)/fps) * 100.0));
616 avctx->time_base.den = frate;
617 avctx->time_base.num = fbase;
622 * XviD 2-Pass Kludge Section
624 * XviD's default 2-pass doesn't allow us to create data as we need to, so
625 * this section spends time replacing the first pass plugin so we can write
626 * statistic information as libavcodec requests in. We have another kludge
627 * that allows us to pass data to the second pass in XviD without a custom
628 * rate-control plugin.
632 * Initializes the two-pass plugin and context.
634 * @param param Input construction parameter structure
635 * @param handle Private context handle
636 * @return Returns XVID_ERR_xxxx on failure, or 0 on success.
638 static int xvid_ff_2pass_create(xvid_plg_create_t * param,
640 xvid_ff_pass1_t *x = (xvid_ff_pass1_t *)param->param;
641 char *log = x->context->twopassbuffer;
643 /* Do a quick bounds check */
645 return XVID_ERR_FAIL;
647 /* We use snprintf() */
648 /* This is because we can safely prevent a buffer overflow */
650 snprintf(log, BUFFER_REMAINING(log),
651 "# ffmpeg 2-pass log file, using xvid codec\n");
652 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
653 "# Do not modify. libxvidcore version: %d.%d.%d\n\n",
654 XVID_VERSION_MAJOR(XVID_VERSION),
655 XVID_VERSION_MINOR(XVID_VERSION),
656 XVID_VERSION_PATCH(XVID_VERSION));
658 *handle = x->context;
663 * Destroys the two-pass plugin context.
665 * @param ref Context pointer for the plugin
666 * @param param Destrooy context
667 * @return Returns 0, success guaranteed
669 static int xvid_ff_2pass_destroy(xvid_context_t *ref,
670 xvid_plg_destroy_t *param) {
671 /* Currently cannot think of anything to do on destruction */
672 /* Still, the framework should be here for reference/use */
673 if( ref->twopassbuffer != NULL )
674 ref->twopassbuffer[0] = 0;
679 * Enables fast encode mode during the first pass.
681 * @param ref Context pointer for the plugin
682 * @param param Frame data
683 * @return Returns 0, success guaranteed
685 static int xvid_ff_2pass_before(xvid_context_t *ref,
686 xvid_plg_data_t *param) {
688 int motion_replacements;
691 /* Nothing to do here, result is changed too much */
692 if( param->zone && param->zone->mode == XVID_ZONE_QUANT )
695 /* We can implement a 'turbo' first pass mode here */
699 motion_remove = ~XVID_ME_CHROMA_PVOP &
700 ~XVID_ME_CHROMA_BVOP &
701 ~XVID_ME_EXTSEARCH16 &
702 ~XVID_ME_ADVANCEDDIAMOND16;
703 motion_replacements = XVID_ME_FAST_MODEINTERPOLATE |
704 XVID_ME_SKIP_DELTASEARCH |
705 XVID_ME_FASTREFINE16 |
706 XVID_ME_BFRAME_EARLYSTOP;
707 vop_remove = ~XVID_VOP_MODEDECISION_RD &
708 ~XVID_VOP_FAST_MODEDECISION_RD &
709 ~XVID_VOP_TRELLISQUANT &
713 param->vol_flags &= ~XVID_VOL_GMC;
714 param->vop_flags &= vop_remove;
715 param->motion_flags &= motion_remove;
716 param->motion_flags |= motion_replacements;
722 * Captures statistic data and writes it during first pass.
724 * @param ref Context pointer for the plugin
725 * @param param Statistic data
726 * @return Returns XVID_ERR_xxxx on failure, or 0 on success
728 static int xvid_ff_2pass_after(xvid_context_t *ref,
729 xvid_plg_data_t *param) {
730 char *log = ref->twopassbuffer;
731 char *frame_types = " ipbs";
734 /* Quick bounds check */
736 return XVID_ERR_FAIL;
738 /* Convert the type given to us into a character */
739 if( param->type < 5 && param->type > 0 ) {
740 frame_type = frame_types[param->type];
742 return XVID_ERR_FAIL;
745 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
746 "%c %d %d %d %d %d %d\n",
747 frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks,
748 param->stats.ublks, param->stats.length, param->stats.hlength);
754 * Dispatch function for our custom plugin.
755 * This handles the dispatch for the XviD plugin. It passes data
756 * on to other functions for actual processing.
758 * @param ref Context pointer for the plugin
759 * @param cmd The task given for us to complete
760 * @param p1 First parameter (varies)
761 * @param p2 Second parameter (varies)
762 * @return Returns XVID_ERR_xxxx on failure, or 0 on success
764 int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) {
770 case XVID_PLG_BEFORE:
771 return xvid_ff_2pass_before(ref, p1);
773 case XVID_PLG_CREATE:
774 return xvid_ff_2pass_create(p1, p2);
777 return xvid_ff_2pass_after(ref, p1);
779 case XVID_PLG_DESTROY:
780 return xvid_ff_2pass_destroy(ref, p1);
783 return XVID_ERR_FAIL;
788 * XviD codec definition for libavcodec.
790 AVCodec xvid_encoder = {
794 sizeof(xvid_context_t),
796 ff_xvid_encode_frame,