Statistics
| Branch: | Revision:

ffmpeg / libavcodec / xvidff.c @ bb54f6ab

History | View | Annotate | Download (24.5 KB)

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