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)
33 * Buffer management macros.
35 #define BUFFER_SIZE 1024
36 #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x))
37 #define BUFFER_CAT(x) (&((x)[strlen(x)]))
41 extern int has_altivec(void);
45 * Structure for the private XviD context.
46 * This stores all the private context for the codec.
48 typedef 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 */
65 * Structure for the private first-pass plugin.
67 typedef struct xvid_ff_pass1
{
68 int version
; /** XviD version */
69 xvid_context_t
*context
; /** Pointer to private context */
72 /* Prototypes - See function implementation for details */
73 int xvid_strip_vol_header(AVCodecContext
*avctx
, unsigned char *frame
, unsigned int header_len
, unsigned int frame_len
);
74 int xvid_ff_2pass(void *ref
, int opt
, void *p1
, void *p2
);
75 void xvid_correct_framerate(AVCodecContext
*avctx
);
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.
82 * @param avctx AVCodecContext pointer to context
83 * @return Returns 0 on success, -1 on failure
85 int ff_xvid_encode_init(AVCodecContext
*avctx
) {
87 int xvid_flags
= avctx
->flags
;
88 xvid_context_t
*x
= avctx
->priv_data
;
89 uint16_t *intra
, *inter
;
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];
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
;
110 /* Decide which ME quality setting to use */
112 switch( avctx
->me_method
) {
113 case ME_FULL
: /* Quality 6 */
114 x
->me_flags
|= XVID_ME_EXTSEARCH16
115 | XVID_ME_EXTSEARCH8
;
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
;
123 case ME_LOG
: /* Quality 2 */
126 x
->me_flags
|= XVID_ME_ADVANCEDDIAMOND16
127 | XVID_ME_HALFPELREFINE16
;
129 case ME_ZERO
: /* Quality 0 */
134 /* Decide how we should decide blocks */
135 switch( avctx
->mb_decision
) {
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
;
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
;
152 /* Bring in VOL flags from ffmpeg command-line */
154 if( xvid_flags
& CODEC_FLAG_GMC
) {
155 x
->vol_flags
|= XVID_VOL_GMC
;
156 x
->me_flags
|= XVID_ME_GME_REFINE
;
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
;
165 memset(&xvid_gbl_init
, 0, sizeof(xvid_gbl_init
));
166 xvid_gbl_init
.version
= XVID_VERSION
;
167 xvid_gbl_init
.debug
= 0;
170 /* XviD's PPC support is borked, use libavcodec to detect */
172 if( has_altivec() ) {
173 xvid_gbl_init
.cpu_flags
= XVID_CPU_FORCE
| XVID_CPU_ALTIVEC
;
176 xvid_gbl_init
.cpu_flags
= XVID_CPU_FORCE
;
178 /* XviD can detect on x86 */
179 xvid_gbl_init
.cpu_flags
= 0;
183 xvid_global(NULL
, XVID_GBL_INIT
, &xvid_gbl_init
, NULL
);
185 /* Create the encoder reference */
186 memset(&xvid_enc_create
, 0, sizeof(xvid_enc_create
));
187 xvid_enc_create
.version
= XVID_VERSION
;
189 /* Store the desired frame size */
190 xvid_enc_create
.width
= x
->xsize
= avctx
->width
;
191 xvid_enc_create
.height
= x
->ysize
= avctx
->height
;
193 /* XviD can determine the proper profile to use */
194 /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */
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;
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 avctx
->codec_tag
= ff_get_fourcc("xvid");
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
;
345 /* Create encoder context */
346 xerr
= xvid_encore(NULL
, XVID_ENC_CREATE
, &xvid_enc_create
, NULL
);
348 av_log(avctx
, AV_LOG_ERROR
, "XviD: Could not create encoder reference\n");
352 x
->encoder_handle
= xvid_enc_create
.handle
;
353 avctx
->coded_frame
= &x
->encoded_picture
;
359 * Encodes a single frame.
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
367 int ff_xvid_encode_frame(AVCodecContext
*avctx
,
368 unsigned char *frame
, int buf_size
, void *data
) {
371 xvid_context_t
*x
= avctx
->priv_data
;
372 AVFrame
*picture
= data
;
373 AVFrame
*p
= &(x
->encoded_picture
);
375 xvid_enc_frame_t xvid_enc_frame
;
376 xvid_enc_stats_t xvid_enc_stats
;
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
;
385 /* Let XviD know where to put the frame. */
386 xvid_enc_frame
.bitstream
= frame
;
387 xvid_enc_frame
.length
= buf_size
;
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");
395 xvid_enc_frame
.input
.csp
= XVID_CSP_PLANAR
; /* YUV420P */
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
];
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
;
409 if( x
->qscale
) xvid_enc_frame
.quant
= picture
->quality
/ FF_QP2LAMBDA
;
410 else xvid_enc_frame
.quant
= 0;
413 xvid_enc_frame
.quant_intra_matrix
= x
->intra_matrix
;
414 xvid_enc_frame
.quant_inter_matrix
= x
->inter_matrix
;
417 xerr
= xvid_encore(x
->encoder_handle
, XVID_ENC_ENCODE
,
418 &xvid_enc_frame
, &xvid_enc_stats
);
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
;
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
;
441 p
->pict_type
= FF_I_TYPE
;
442 if( xvid_enc_frame
.out_flags
& XVID_KEYFRAME
) {
444 if( x
->quicktime_format
)
445 return xvid_strip_vol_header(avctx
, frame
,
446 xvid_enc_stats
.hlength
, xerr
);
452 av_log(avctx
, AV_LOG_ERROR
, "XviD: Encoding Error Occurred: %i\n", xerr
);
458 * Destroys the private context for the encoder.
459 * All buffers are freed, and the XviD encoder context is destroyed.
461 * @param avctx AVCodecContext pointer to context
462 * @return Returns 0, success guaranteed
464 int ff_xvid_encode_close(AVCodecContext
*avctx
) {
465 xvid_context_t
*x
= avctx
->priv_data
;
467 xvid_encore(x
->encoder_handle
, XVID_ENC_DESTROY
, NULL
, NULL
);
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
);
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
);
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
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
498 int xvid_strip_vol_header(AVCodecContext
*avctx
,
499 unsigned char *frame
,
500 unsigned int header_len
,
501 unsigned int frame_len
) {
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 ) {
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
;
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
;
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
536 * @param avctx Context that contains the framerate to correct.
538 void xvid_correct_framerate(AVCodecContext
*avctx
) {
540 int est_frate
, est_fbase
;
544 frate
= avctx
->time_base
.den
;
545 fbase
= avctx
->time_base
.num
;
547 gcd
= ff_gcd(frate
, fbase
);
553 if( frate
<= 65000 && fbase
<= 65000 ) {
554 avctx
->time_base
.den
= frate
;
555 avctx
->time_base
.num
= fbase
;
559 fps
= (float)frate
/ (float)fbase
;
560 est_fps
= roundf(fps
* 1000.0) / 1000.0;
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
);
569 gcd
= ff_gcd(est_frate
, est_fbase
);
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));
582 avctx
->time_base
.den
= frate
;
583 avctx
->time_base
.num
= fbase
;
588 * XviD 2-Pass Kludge Section
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.
598 * Initializes the two-pass plugin and context.
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.
604 static int xvid_ff_2pass_create(xvid_plg_create_t
* param
,
606 xvid_ff_pass1_t
*x
= (xvid_ff_pass1_t
*)param
->param
;
607 char *log
= x
->context
->twopassbuffer
;
609 /* Do a quick bounds check */
611 return XVID_ERR_FAIL
;
613 /* We use snprintf() */
614 /* This is because we can safely prevent a buffer overflow */
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
));
624 *handle
= x
->context
;
629 * Destroys the two-pass plugin context.
631 * @param ref Context pointer for the plugin
632 * @param param Destrooy context
633 * @return Returns 0, success guaranteed
635 static 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;
645 * Enables fast encode mode during the first pass.
647 * @param ref Context pointer for the plugin
648 * @param param Frame data
649 * @return Returns 0, success guaranteed
651 static int xvid_ff_2pass_before(xvid_context_t
*ref
,
652 xvid_plg_data_t
*param
) {
654 int motion_replacements
;
657 /* Nothing to do here, result is changed too much */
658 if( param
->zone
&& param
->zone
->mode
== XVID_ZONE_QUANT
)
661 /* We can implement a 'turbo' first pass mode here */
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
&
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
;
688 * Captures statistic data and writes it during first pass.
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
694 static 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";
700 /* Quick bounds check */
702 return XVID_ERR_FAIL
;
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
];
708 return XVID_ERR_FAIL
;
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
);
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.
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
730 int xvid_ff_2pass(void *ref
, int cmd
, void *p1
, void *p2
) {
736 case XVID_PLG_BEFORE
:
737 return xvid_ff_2pass_before(ref
, p1
);
739 case XVID_PLG_CREATE
:
740 return xvid_ff_2pass_create(p1
, p2
);
743 return xvid_ff_2pass_after(ref
, p1
);
745 case XVID_PLG_DESTROY
:
746 return xvid_ff_2pass_destroy(ref
, p1
);
749 return XVID_ERR_FAIL
;
754 * XviD codec definition for libavcodec.
756 AVCodec xvid_encoder
= {
760 sizeof(xvid_context_t
),
762 ff_xvid_encode_frame
,