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 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 */
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 */
197 xvid_enc_create
.zones
= NULL
;
198 xvid_enc_create
.num_zones
= 0;
200 xvid_enc_create
.num_threads
= avctx
->thread_count
;
202 xvid_enc_create
.plugins
= plugins
;
203 xvid_enc_create
.num_plugins
= 0;
205 /* Initialize Buffers */
206 x
->twopassbuffer
= NULL
;
207 x
->old_twopassbuffer
= NULL
;
208 x
->twopassfile
= NULL
;
210 if( xvid_flags
& CODEC_FLAG_PASS1
) {
211 memset(&rc2pass1
, 0, sizeof(xvid_ff_pass1_t
));
212 rc2pass1
.version
= XVID_VERSION
;
213 rc2pass1
.context
= x
;
214 x
->twopassbuffer
= av_malloc(BUFFER_SIZE
);
215 x
->old_twopassbuffer
= av_malloc(BUFFER_SIZE
);
216 if( x
->twopassbuffer
== NULL
|| x
->old_twopassbuffer
== NULL
) {
217 av_log(avctx
, AV_LOG_ERROR
,
218 "Xvid: Cannot allocate 2-pass log buffers\n");
221 x
->twopassbuffer
[0] = x
->old_twopassbuffer
[0] = 0;
223 plugins
[xvid_enc_create
.num_plugins
].func
= xvid_ff_2pass
;
224 plugins
[xvid_enc_create
.num_plugins
].param
= &rc2pass1
;
225 xvid_enc_create
.num_plugins
++;
226 } else if( xvid_flags
& CODEC_FLAG_PASS2
) {
227 memset(&rc2pass2
, 0, sizeof(xvid_plugin_2pass2_t
));
228 rc2pass2
.version
= XVID_VERSION
;
229 rc2pass2
.bitrate
= avctx
->bit_rate
;
231 fd
= av_tempfile("xvidff.", &(x
->twopassfile
));
233 av_log(avctx
, AV_LOG_ERROR
,
234 "Xvid: Cannot write 2-pass pipe\n");
238 if( avctx
->stats_in
== NULL
) {
239 av_log(avctx
, AV_LOG_ERROR
,
240 "Xvid: No 2-pass information loaded for second pass\n");
244 if( strlen(avctx
->stats_in
) >
245 write(fd
, avctx
->stats_in
, strlen(avctx
->stats_in
)) ) {
247 av_log(avctx
, AV_LOG_ERROR
,
248 "Xvid: Cannot write to 2-pass pipe\n");
253 rc2pass2
.filename
= x
->twopassfile
;
254 plugins
[xvid_enc_create
.num_plugins
].func
= xvid_plugin_2pass2
;
255 plugins
[xvid_enc_create
.num_plugins
].param
= &rc2pass2
;
256 xvid_enc_create
.num_plugins
++;
257 } else if( !(xvid_flags
& CODEC_FLAG_QSCALE
) ) {
258 /* Single Pass Bitrate Control! */
259 memset(&single
, 0, sizeof(xvid_plugin_single_t
));
260 single
.version
= XVID_VERSION
;
261 single
.bitrate
= avctx
->bit_rate
;
263 plugins
[xvid_enc_create
.num_plugins
].func
= xvid_plugin_single
;
264 plugins
[xvid_enc_create
.num_plugins
].param
= &single
;
265 xvid_enc_create
.num_plugins
++;
268 /* Luminance Masking */
269 if( 0.0 != avctx
->lumi_masking
) {
270 plugins
[xvid_enc_create
.num_plugins
].func
= xvid_plugin_lumimasking
;
271 plugins
[xvid_enc_create
.num_plugins
].param
= NULL
;
272 xvid_enc_create
.num_plugins
++;
275 /* Frame Rate and Key Frames */
276 xvid_correct_framerate(avctx
);
277 xvid_enc_create
.fincr
= avctx
->time_base
.num
;
278 xvid_enc_create
.fbase
= avctx
->time_base
.den
;
279 if( avctx
->gop_size
> 0 )
280 xvid_enc_create
.max_key_interval
= avctx
->gop_size
;
282 xvid_enc_create
.max_key_interval
= 240; /* Xvid's best default */
285 if( xvid_flags
& CODEC_FLAG_QSCALE
) x
->qscale
= 1;
288 xvid_enc_create
.min_quant
[0] = avctx
->qmin
;
289 xvid_enc_create
.min_quant
[1] = avctx
->qmin
;
290 xvid_enc_create
.min_quant
[2] = avctx
->qmin
;
291 xvid_enc_create
.max_quant
[0] = avctx
->qmax
;
292 xvid_enc_create
.max_quant
[1] = avctx
->qmax
;
293 xvid_enc_create
.max_quant
[2] = avctx
->qmax
;
296 x
->intra_matrix
= x
->inter_matrix
= NULL
;
297 if( avctx
->mpeg_quant
)
298 x
->vol_flags
|= XVID_VOL_MPEGQUANT
;
299 if( (avctx
->intra_matrix
|| avctx
->inter_matrix
) ) {
300 x
->vol_flags
|= XVID_VOL_MPEGQUANT
;
302 if( avctx
->intra_matrix
) {
303 intra
= avctx
->intra_matrix
;
304 x
->intra_matrix
= av_malloc(sizeof(unsigned char) * 64);
307 if( avctx
->inter_matrix
) {
308 inter
= avctx
->inter_matrix
;
309 x
->inter_matrix
= av_malloc(sizeof(unsigned char) * 64);
313 for( i
= 0; i
< 64; i
++ ) {
315 x
->intra_matrix
[i
] = (unsigned char)intra
[i
];
317 x
->inter_matrix
[i
] = (unsigned char)inter
[i
];
322 xvid_enc_create
.frame_drop_ratio
= 0;
323 xvid_enc_create
.global
= 0;
324 if( xvid_flags
& CODEC_FLAG_CLOSED_GOP
)
325 xvid_enc_create
.global
|= XVID_GLOBAL_CLOSED_GOP
;
327 /* Determines which codec mode we are operating in */
328 avctx
->extradata
= NULL
;
329 avctx
->extradata_size
= 0;
330 if( xvid_flags
& CODEC_FLAG_GLOBAL_HEADER
) {
331 /* In this case, we are claiming to be MPEG4 */
332 x
->quicktime_format
= 1;
333 avctx
->codec_id
= CODEC_ID_MPEG4
;
335 /* We are claiming to be Xvid */
336 x
->quicktime_format
= 0;
337 if(!avctx
->codec_tag
)
338 avctx
->codec_tag
= ff_get_fourcc("xvid");
342 xvid_enc_create
.max_bframes
= avctx
->max_b_frames
;
343 xvid_enc_create
.bquant_offset
= 100 * avctx
->b_quant_offset
;
344 xvid_enc_create
.bquant_ratio
= 100 * avctx
->b_quant_factor
;
345 if( avctx
->max_b_frames
> 0 && !x
->quicktime_format
) xvid_enc_create
.global
|= XVID_GLOBAL_PACKED
;
347 /* Create encoder context */
348 xerr
= xvid_encore(NULL
, XVID_ENC_CREATE
, &xvid_enc_create
, NULL
);
350 av_log(avctx
, AV_LOG_ERROR
, "Xvid: Could not create encoder reference\n");
354 x
->encoder_handle
= xvid_enc_create
.handle
;
355 avctx
->coded_frame
= &x
->encoded_picture
;
361 * Encodes a single frame.
363 * @param avctx AVCodecContext pointer to context
364 * @param frame Pointer to encoded frame buffer
365 * @param buf_size Size of encoded frame buffer
366 * @param data Pointer to AVFrame of unencoded frame
367 * @return Returns 0 on success, -1 on failure
369 int ff_xvid_encode_frame(AVCodecContext
*avctx
,
370 unsigned char *frame
, int buf_size
, void *data
) {
373 xvid_context_t
*x
= avctx
->priv_data
;
374 AVFrame
*picture
= data
;
375 AVFrame
*p
= &(x
->encoded_picture
);
377 xvid_enc_frame_t xvid_enc_frame
;
378 xvid_enc_stats_t xvid_enc_stats
;
380 /* Start setting up the frame */
381 memset(&xvid_enc_frame
, 0, sizeof(xvid_enc_frame
));
382 xvid_enc_frame
.version
= XVID_VERSION
;
383 memset(&xvid_enc_stats
, 0, sizeof(xvid_enc_stats
));
384 xvid_enc_stats
.version
= XVID_VERSION
;
387 /* Let Xvid know where to put the frame. */
388 xvid_enc_frame
.bitstream
= frame
;
389 xvid_enc_frame
.length
= buf_size
;
391 /* Initialize input image fields */
392 if( avctx
->pix_fmt
!= PIX_FMT_YUV420P
) {
393 av_log(avctx
, AV_LOG_ERROR
, "Xvid: Color spaces other than 420p not supported\n");
397 xvid_enc_frame
.input
.csp
= XVID_CSP_PLANAR
; /* YUV420P */
399 for( i
= 0; i
< 4; i
++ ) {
400 xvid_enc_frame
.input
.plane
[i
] = picture
->data
[i
];
401 xvid_enc_frame
.input
.stride
[i
] = picture
->linesize
[i
];
405 xvid_enc_frame
.vop_flags
= x
->vop_flags
;
406 xvid_enc_frame
.vol_flags
= x
->vol_flags
;
407 xvid_enc_frame
.motion
= x
->me_flags
;
408 xvid_enc_frame
.type
= XVID_TYPE_AUTO
;
410 /* Pixel aspect ratio setting */
411 if (avctx
->sample_aspect_ratio
.num
< 1 || avctx
->sample_aspect_ratio
.num
> 255 ||
412 avctx
->sample_aspect_ratio
.den
< 1 || avctx
->sample_aspect_ratio
.den
> 255) {
413 av_log(avctx
, AV_LOG_ERROR
, "Invalid pixel aspect ratio %i/%i\n",
414 avctx
->sample_aspect_ratio
.num
, avctx
->sample_aspect_ratio
.den
);
417 xvid_enc_frame
.par
= XVID_PAR_EXT
;
418 xvid_enc_frame
.par_width
= avctx
->sample_aspect_ratio
.num
;
419 xvid_enc_frame
.par_height
= avctx
->sample_aspect_ratio
.den
;
422 if( x
->qscale
) xvid_enc_frame
.quant
= picture
->quality
/ FF_QP2LAMBDA
;
423 else xvid_enc_frame
.quant
= 0;
426 xvid_enc_frame
.quant_intra_matrix
= x
->intra_matrix
;
427 xvid_enc_frame
.quant_inter_matrix
= x
->inter_matrix
;
430 xerr
= xvid_encore(x
->encoder_handle
, XVID_ENC_ENCODE
,
431 &xvid_enc_frame
, &xvid_enc_stats
);
433 /* Two-pass log buffer swapping */
434 avctx
->stats_out
= NULL
;
435 if( x
->twopassbuffer
) {
436 tmp
= x
->old_twopassbuffer
;
437 x
->old_twopassbuffer
= x
->twopassbuffer
;
438 x
->twopassbuffer
= tmp
;
439 x
->twopassbuffer
[0] = 0;
440 if( x
->old_twopassbuffer
[0] != 0 ) {
441 avctx
->stats_out
= x
->old_twopassbuffer
;
446 p
->quality
= xvid_enc_stats
.quant
* FF_QP2LAMBDA
;
447 if( xvid_enc_stats
.type
== XVID_TYPE_PVOP
)
448 p
->pict_type
= FF_P_TYPE
;
449 else if( xvid_enc_stats
.type
== XVID_TYPE_BVOP
)
450 p
->pict_type
= FF_B_TYPE
;
451 else if( xvid_enc_stats
.type
== XVID_TYPE_SVOP
)
452 p
->pict_type
= FF_S_TYPE
;
454 p
->pict_type
= FF_I_TYPE
;
455 if( xvid_enc_frame
.out_flags
& XVID_KEYFRAME
) {
457 if( x
->quicktime_format
)
458 return xvid_strip_vol_header(avctx
, frame
,
459 xvid_enc_stats
.hlength
, xerr
);
465 av_log(avctx
, AV_LOG_ERROR
, "Xvid: Encoding Error Occurred: %i\n", xerr
);
471 * Destroys the private context for the encoder.
472 * All buffers are freed, and the Xvid encoder context is destroyed.
474 * @param avctx AVCodecContext pointer to context
475 * @return Returns 0, success guaranteed
477 av_cold
int ff_xvid_encode_close(AVCodecContext
*avctx
) {
478 xvid_context_t
*x
= avctx
->priv_data
;
480 xvid_encore(x
->encoder_handle
, XVID_ENC_DESTROY
, NULL
, NULL
);
482 if( avctx
->extradata
!= NULL
)
483 av_free(avctx
->extradata
);
484 if( x
->twopassbuffer
!= NULL
) {
485 av_free(x
->twopassbuffer
);
486 av_free(x
->old_twopassbuffer
);
488 if( x
->twopassfile
!= NULL
)
489 av_free(x
->twopassfile
);
490 if( x
->intra_matrix
!= NULL
)
491 av_free(x
->intra_matrix
);
492 if( x
->inter_matrix
!= NULL
)
493 av_free(x
->inter_matrix
);
499 * Routine to create a global VO/VOL header for MP4 container.
500 * What we do here is extract the header from the Xvid bitstream
501 * as it is encoded. We also strip the repeated headers from the
502 * bitstream when a global header is requested for MPEG-4 ISO
505 * @param avctx AVCodecContext pointer to context
506 * @param frame Pointer to encoded frame data
507 * @param header_len Length of header to search
508 * @param frame_len Length of encoded frame data
509 * @return Returns new length of frame data
511 int xvid_strip_vol_header(AVCodecContext
*avctx
,
512 unsigned char *frame
,
513 unsigned int header_len
,
514 unsigned int frame_len
) {
517 for( i
= 0; i
< header_len
- 3; i
++ ) {
518 if( frame
[i
] == 0x00 &&
519 frame
[i
+1] == 0x00 &&
520 frame
[i
+2] == 0x01 &&
521 frame
[i
+3] == 0xB6 ) {
528 /* We need to store the header, so extract it */
529 if( avctx
->extradata
== NULL
) {
530 avctx
->extradata
= av_malloc(vo_len
);
531 memcpy(avctx
->extradata
, frame
, vo_len
);
532 avctx
->extradata_size
= vo_len
;
534 /* Less dangerous now, memmove properly copies the two
535 chunks of overlapping data */
536 memmove(frame
, &(frame
[vo_len
]), frame_len
- vo_len
);
537 return frame_len
- vo_len
;
543 * Routine to correct a possibly erroneous framerate being fed to us.
544 * Xvid currently chokes on framerates where the ticks per frame is
545 * extremely large. This function works to correct problems in this area
546 * by estimating a new framerate and taking the simpler fraction of
549 * @param avctx Context that contains the framerate to correct.
551 void xvid_correct_framerate(AVCodecContext
*avctx
) {
553 int est_frate
, est_fbase
;
557 frate
= avctx
->time_base
.den
;
558 fbase
= avctx
->time_base
.num
;
560 gcd
= ff_gcd(frate
, fbase
);
566 if( frate
<= 65000 && fbase
<= 65000 ) {
567 avctx
->time_base
.den
= frate
;
568 avctx
->time_base
.num
= fbase
;
572 fps
= (float)frate
/ (float)fbase
;
573 est_fps
= roundf(fps
* 1000.0) / 1000.0;
575 est_frate
= (int)est_fps
;
576 if( est_fps
> (int)est_fps
) {
577 est_frate
= (est_frate
+ 1) * 1000;
578 est_fbase
= (int)roundf((float)est_frate
/ est_fps
);
582 gcd
= ff_gcd(est_frate
, est_fbase
);
588 if( fbase
> est_fbase
) {
589 avctx
->time_base
.den
= est_frate
;
590 avctx
->time_base
.num
= est_fbase
;
591 av_log(avctx
, AV_LOG_DEBUG
,
592 "Xvid: framerate re-estimated: %.2f, %.3f%% correction\n",
593 est_fps
, (((est_fps
- fps
)/fps
) * 100.0));
595 avctx
->time_base
.den
= frate
;
596 avctx
->time_base
.num
= fbase
;
601 * Xvid 2-Pass Kludge Section
603 * Xvid's default 2-pass doesn't allow us to create data as we need to, so
604 * this section spends time replacing the first pass plugin so we can write
605 * statistic information as libavcodec requests in. We have another kludge
606 * that allows us to pass data to the second pass in Xvid without a custom
607 * rate-control plugin.
611 * Initializes the two-pass plugin and context.
613 * @param param Input construction parameter structure
614 * @param handle Private context handle
615 * @return Returns XVID_ERR_xxxx on failure, or 0 on success.
617 static int xvid_ff_2pass_create(xvid_plg_create_t
* param
,
619 xvid_ff_pass1_t
*x
= (xvid_ff_pass1_t
*)param
->param
;
620 char *log
= x
->context
->twopassbuffer
;
622 /* Do a quick bounds check */
624 return XVID_ERR_FAIL
;
626 /* We use snprintf() */
627 /* This is because we can safely prevent a buffer overflow */
629 snprintf(log
, BUFFER_REMAINING(log
),
630 "# ffmpeg 2-pass log file, using xvid codec\n");
631 snprintf(BUFFER_CAT(log
), BUFFER_REMAINING(log
),
632 "# Do not modify. libxvidcore version: %d.%d.%d\n\n",
633 XVID_VERSION_MAJOR(XVID_VERSION
),
634 XVID_VERSION_MINOR(XVID_VERSION
),
635 XVID_VERSION_PATCH(XVID_VERSION
));
637 *handle
= x
->context
;
642 * Destroys the two-pass plugin context.
644 * @param ref Context pointer for the plugin
645 * @param param Destrooy context
646 * @return Returns 0, success guaranteed
648 static int xvid_ff_2pass_destroy(xvid_context_t
*ref
,
649 xvid_plg_destroy_t
*param
) {
650 /* Currently cannot think of anything to do on destruction */
651 /* Still, the framework should be here for reference/use */
652 if( ref
->twopassbuffer
!= NULL
)
653 ref
->twopassbuffer
[0] = 0;
658 * Enables fast encode mode during the first pass.
660 * @param ref Context pointer for the plugin
661 * @param param Frame data
662 * @return Returns 0, success guaranteed
664 static int xvid_ff_2pass_before(xvid_context_t
*ref
,
665 xvid_plg_data_t
*param
) {
667 int motion_replacements
;
670 /* Nothing to do here, result is changed too much */
671 if( param
->zone
&& param
->zone
->mode
== XVID_ZONE_QUANT
)
674 /* We can implement a 'turbo' first pass mode here */
678 motion_remove
= ~XVID_ME_CHROMA_PVOP
&
679 ~XVID_ME_CHROMA_BVOP
&
680 ~XVID_ME_EXTSEARCH16
&
681 ~XVID_ME_ADVANCEDDIAMOND16
;
682 motion_replacements
= XVID_ME_FAST_MODEINTERPOLATE
|
683 XVID_ME_SKIP_DELTASEARCH
|
684 XVID_ME_FASTREFINE16
|
685 XVID_ME_BFRAME_EARLYSTOP
;
686 vop_remove
= ~XVID_VOP_MODEDECISION_RD
&
687 ~XVID_VOP_FAST_MODEDECISION_RD
&
688 ~XVID_VOP_TRELLISQUANT
&
692 param
->vol_flags
&= ~XVID_VOL_GMC
;
693 param
->vop_flags
&= vop_remove
;
694 param
->motion_flags
&= motion_remove
;
695 param
->motion_flags
|= motion_replacements
;
701 * Captures statistic data and writes it during first pass.
703 * @param ref Context pointer for the plugin
704 * @param param Statistic data
705 * @return Returns XVID_ERR_xxxx on failure, or 0 on success
707 static int xvid_ff_2pass_after(xvid_context_t
*ref
,
708 xvid_plg_data_t
*param
) {
709 char *log
= ref
->twopassbuffer
;
710 char *frame_types
= " ipbs";
713 /* Quick bounds check */
715 return XVID_ERR_FAIL
;
717 /* Convert the type given to us into a character */
718 if( param
->type
< 5 && param
->type
> 0 ) {
719 frame_type
= frame_types
[param
->type
];
721 return XVID_ERR_FAIL
;
724 snprintf(BUFFER_CAT(log
), BUFFER_REMAINING(log
),
725 "%c %d %d %d %d %d %d\n",
726 frame_type
, param
->stats
.quant
, param
->stats
.kblks
, param
->stats
.mblks
,
727 param
->stats
.ublks
, param
->stats
.length
, param
->stats
.hlength
);
733 * Dispatch function for our custom plugin.
734 * This handles the dispatch for the Xvid plugin. It passes data
735 * on to other functions for actual processing.
737 * @param ref Context pointer for the plugin
738 * @param cmd The task given for us to complete
739 * @param p1 First parameter (varies)
740 * @param p2 Second parameter (varies)
741 * @return Returns XVID_ERR_xxxx on failure, or 0 on success
743 int xvid_ff_2pass(void *ref
, int cmd
, void *p1
, void *p2
) {
749 case XVID_PLG_BEFORE
:
750 return xvid_ff_2pass_before(ref
, p1
);
752 case XVID_PLG_CREATE
:
753 return xvid_ff_2pass_create(p1
, p2
);
756 return xvid_ff_2pass_after(ref
, p1
);
758 case XVID_PLG_DESTROY
:
759 return xvid_ff_2pass_destroy(ref
, p1
);
762 return XVID_ERR_FAIL
;
767 * Xvid codec definition for libavcodec.
769 AVCodec libxvid_encoder
= {
773 sizeof(xvid_context_t
),
775 ff_xvid_encode_frame
,
776 ff_xvid_encode_close
,
777 .pix_fmts
= (enum PixelFormat
[]){PIX_FMT_YUV420P
, PIX_FMT_NONE
},
778 .long_name
= NULL_IF_CONFIG_SMALL("libxvidcore MPEG-4 part 2"),