video.c 26.2 KB
Newer Older
1
/*****************************************************************************
gbazin's avatar
   
gbazin committed
2
 * video.c: video decoder using the ffmpeg library
3
4
 *****************************************************************************
 * Copyright (C) 1999-2001 VideoLAN
5
 * $Id$
6
7
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Gildas Bazin <gbazin@videolan.org>
9
10
11
12
13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
14
 *
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc/decoder.h>

gbazin's avatar
   
gbazin committed
31
32
33
34
35
36
37
/* ffmpeg header */
#ifdef HAVE_FFMPEG_AVCODEC_H
#   include <ffmpeg/avcodec.h>
#else
#   include <avcodec.h>
#endif

38
#include "ffmpeg.h"
39

gbazin's avatar
   
gbazin committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*****************************************************************************
 * decoder_sys_t : decoder descriptor
 *****************************************************************************/
struct decoder_sys_t
{
    /* Common part between video and audio decoder */
    int i_cat;
    int i_codec_id;
    char *psz_namecodec;

    AVCodecContext      *p_context;
    AVCodec             *p_codec;

    /* Video decoder specific part */
    mtime_t input_pts;
gbazin's avatar
   
gbazin committed
55
    mtime_t input_dts;
gbazin's avatar
   
gbazin committed
56
57
58
59
    mtime_t i_pts;

    AVFrame          *p_ff_pic;
    BITMAPINFOHEADER *p_format;
60

gbazin's avatar
   
gbazin committed
61
62
63
64
65
66
67
68
69
70
71
72
73
    /* for frame skipping algo */
    int b_hurry_up;
    int i_frame_skip;

    /* how many decoded frames are late */
    int     i_late_frames;
    mtime_t i_late_frames_start;

    /* for direct rendering */
    int b_direct_rendering;

    vlc_bool_t b_has_b_frames;

gbazin's avatar
   
gbazin committed
74
75
    int i_buffer_orig, i_buffer;
    char *p_buffer_orig, *p_buffer;
gbazin's avatar
   
gbazin committed
76
77
78

    /* Postprocessing handle */
    void *p_pp;
gbazin's avatar
   
gbazin committed
79
80
    vlc_bool_t b_pp;
    vlc_bool_t b_pp_async;
gbazin's avatar
   
gbazin committed
81
    vlc_bool_t b_pp_init;
gbazin's avatar
   
gbazin committed
82
};
83

gbazin's avatar
   
gbazin committed
84
85
86
/* FIXME (dummy palette for now) */
static AVPaletteControl palette_control;

gbazin's avatar
   
gbazin committed
87
/*****************************************************************************
88
 * Local prototypes
gbazin's avatar
   
gbazin committed
89
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
90
91
92
static void ffmpeg_CopyPicture    ( decoder_t *, picture_t *, AVFrame * );
static int  ffmpeg_GetFrameBuf    ( struct AVCodecContext *, AVFrame * );
static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
93

94
95
96
97
98
99
static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
{
    uint8_t *p = (uint8_t*)&fcc;
    return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}

gbazin's avatar
   
gbazin committed
100
101
102
/*****************************************************************************
 * Local Functions
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
103
static uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
104
{
105
106
    switch( i_ff_chroma )
    {
gbazin's avatar
   
gbazin committed
107
    case PIX_FMT_YUV420P:
gbazin's avatar
   
gbazin committed
108
        return VLC_FOURCC('I','4','2','0');
gbazin's avatar
   
gbazin committed
109
    case PIX_FMT_YUV422P:
gbazin's avatar
   
gbazin committed
110
        return VLC_FOURCC('I','4','2','2');
gbazin's avatar
   
gbazin committed
111
    case PIX_FMT_YUV444P:
gbazin's avatar
   
gbazin committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
        return VLC_FOURCC('I','4','4','4');

    case PIX_FMT_YUV422:
        return VLC_FOURCC('Y','U','Y','2');

    case PIX_FMT_RGB555:
        return VLC_FOURCC('R','V','1','5');
    case PIX_FMT_RGB565:
        return VLC_FOURCC('R','V','1','6');
    case PIX_FMT_RGB24:
        return VLC_FOURCC('R','V','2','4');
    case PIX_FMT_RGBA32:
        return VLC_FOURCC('R','V','3','2');
    case PIX_FMT_GRAY8:
        return VLC_FOURCC('G','R','E','Y');

gbazin's avatar
   
gbazin committed
128
129
130
131
132
    case PIX_FMT_YUV410P:
    case PIX_FMT_YUV411P:
    case PIX_FMT_BGR24:
    default:
        return 0;
133
134
135
    }
}

gbazin's avatar
   
gbazin committed
136
137
138
/* Returns a new picture buffer */
static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
                                            AVCodecContext *p_context )
139
{
gbazin's avatar
   
gbazin committed
140
141
    decoder_sys_t *p_sys = p_dec->p_sys;
    picture_t *p_pic;
142

gbazin's avatar
   
gbazin committed
143
144
145
146
147
    p_dec->fmt_out.video.i_width = p_context->width;
    p_dec->fmt_out.video.i_height = p_context->height;
    p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );

    if( !p_context->width || !p_context->height )
148
    {
gbazin's avatar
   
gbazin committed
149
        return NULL; /* invalid display size */
150
151
    }

gbazin's avatar
   
gbazin committed
152
    if( !p_dec->fmt_out.i_codec )
153
154
    {
        /* we make conversion if possible*/
gbazin's avatar
   
gbazin committed
155
        p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
156
    }
157

158
159
160
161
162
163
164
    /* If an aspect-ratio was specified in the input format then force it */
    if( p_dec->fmt_in.video.i_aspect )
    {
        p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
    }
    else
    {
gbazin's avatar
   
gbazin committed
165
#if LIBAVCODEC_BUILD >= 4687
166
167
168
        p_dec->fmt_out.video.i_aspect =
            VOUT_ASPECT_FACTOR * ( av_q2d(p_context->sample_aspect_ratio) *
                p_context->width / p_context->height );
gbazin's avatar
   
gbazin committed
169
#else
gbazin's avatar
   
gbazin committed
170
        p_dec->fmt_out.video.i_aspect =
171
172
173
174
175
176
177
            VOUT_ASPECT_FACTOR * p_context->aspect_ratio;
#endif
        if( p_dec->fmt_out.video.i_aspect == 0 )
        {
            p_dec->fmt_out.video.i_aspect =
                VOUT_ASPECT_FACTOR * p_context->width / p_context->height;
        }
178
    }
179

gbazin's avatar
   
gbazin committed
180
    p_pic = p_dec->pf_vout_buffer_new( p_dec );
181

gbazin's avatar
   
gbazin committed
182
#ifdef LIBAVCODEC_PP
gbazin's avatar
   
gbazin committed
183
    if( p_sys->p_pp && p_sys->b_pp && !p_sys->b_pp_init )
gbazin's avatar
   
gbazin committed
184
185
186
187
188
    {
        E_(InitPostproc)( p_dec, p_sys->p_pp, p_context->width,
                          p_context->height, p_context->pix_fmt );
        p_sys->b_pp_init = VLC_TRUE;
    }
189
#endif
gbazin's avatar
   
gbazin committed
190

gbazin's avatar
   
gbazin committed
191
    return p_pic;
192
193
194
}

/*****************************************************************************
gbazin's avatar
   
gbazin committed
195
 * InitVideo: initialize the video decoder
196
 *****************************************************************************
gbazin's avatar
   
gbazin committed
197
198
 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
 * opened (done after the first decoded frame).
199
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
200
201
int E_(InitVideoDec)( decoder_t *p_dec, AVCodecContext *p_context,
                      AVCodec *p_codec, int i_codec_id, char *psz_namecodec )
202
{
gbazin's avatar
   
gbazin committed
203
    decoder_sys_t *p_sys;
204
    vlc_value_t lockval;
gbazin's avatar
   
gbazin committed
205
    vlc_value_t val;
206
207
208

    var_Get( p_dec->p_libvlc, "avcodec", &lockval );

gbazin's avatar
   
gbazin committed
209
210
211
212
213
214
215
216
217
218
219
220
221
    /* Allocate the memory needed to store the decoder's structure */
    if( ( p_dec->p_sys = p_sys =
          (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
    {
        msg_Err( p_dec, "out of memory" );
        return VLC_EGENERIC;
    }

    p_dec->p_sys->p_context = p_context;
    p_dec->p_sys->p_codec = p_codec;
    p_dec->p_sys->i_codec_id = i_codec_id;
    p_dec->p_sys->psz_namecodec = psz_namecodec;
    p_sys->p_ff_pic = avcodec_alloc_frame();
gbazin's avatar
   
gbazin committed
222

gbazin's avatar
   
gbazin committed
223
    /* ***** Fill p_context with init values ***** */
224
    p_sys->p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_codec );
gbazin's avatar
   
gbazin committed
225
226
    p_sys->p_context->width  = p_dec->fmt_in.video.i_width;
    p_sys->p_context->height = p_dec->fmt_in.video.i_height;
sigmunau's avatar
sigmunau committed
227
    p_sys->p_context->bits_per_sample = p_dec->fmt_in.video.i_bits_per_pixel;
228
229

    /*  ***** Get configuration of ffmpeg plugin ***** */
gbazin's avatar
gbazin committed
230
231
232
233
    p_sys->p_context->workaround_bugs =
        config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
    p_sys->p_context->error_resilience =
        config_GetInt( p_dec, "ffmpeg-error-resilience" );
234

gbazin's avatar
   
gbazin committed
235
236
237
    var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Get( p_dec, "grayscale", &val );
    if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
238

gbazin's avatar
gbazin committed
239
240
    var_Create( p_dec, "ffmpeg-vismv", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Get( p_dec, "ffmpeg-vismv", &val );
241
#if LIBAVCODEC_BUILD >= 4698
gbazin's avatar
gbazin committed
242
    if( val.i_int ) p_sys->p_context->debug_mv = val.i_int;
243
#endif
gbazin's avatar
gbazin committed
244

gbazin's avatar
   
gbazin committed
245
246
247
248
249
250
251
252
253
254
255
    /* ***** ffmpeg frame skipping ***** */
    var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Get( p_dec, "ffmpeg-hurry-up", &val );
    p_sys->b_hurry_up = val.b_bool;

    /* ***** ffmpeg direct rendering ***** */
    p_sys->b_direct_rendering = 0;
    var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
    var_Get( p_dec, "ffmpeg-dr", &val );
    if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
        ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) &&
gbazin's avatar
   
gbazin committed
256
        /* Apparently direct rendering doesn't work with YUV422P */
gbazin's avatar
   
gbazin committed
257
        p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
gbazin's avatar
gbazin committed
258
259
        /* H264 uses too many reference frames */
        p_sys->i_codec_id != CODEC_ID_H264 &&
260
        !(p_sys->p_context->width % 16) && !(p_sys->p_context->height % 16) &&
261
#if LIBAVCODEC_BUILD >= 4698
262
        !p_sys->p_context->debug_mv )
263
264
265
#else
        1 )
#endif
gbazin's avatar
   
gbazin committed
266
    {
gbazin's avatar
   
gbazin committed
267
268
        /* Some codecs set pix_fmt only after the 1st frame has been decoded,
         * so we need to do another check in ffmpeg_GetFrameBuf() */
gbazin's avatar
   
gbazin committed
269
        p_sys->b_direct_rendering = 1;
gbazin's avatar
   
gbazin committed
270
271
272
    }

#ifdef LIBAVCODEC_PP
273
    p_sys->p_pp = NULL;
gbazin's avatar
   
gbazin committed
274
275
    p_sys->b_pp = p_sys->b_pp_async = p_sys->b_pp_init = VLC_FALSE;
    p_sys->p_pp = E_(OpenPostproc)( p_dec, &p_sys->b_pp_async );
gbazin's avatar
   
gbazin committed
276
277
#endif

gbazin's avatar
   
gbazin committed
278
    /* ffmpeg doesn't properly release old pictures when frames are skipped */
gbazin's avatar
   
gbazin committed
279
    //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
gbazin's avatar
   
gbazin committed
280
281
282
283
284
    if( p_sys->b_direct_rendering )
    {
        msg_Dbg( p_dec, "using direct rendering" );
        p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
    }
gbazin's avatar
   
gbazin committed
285

gbazin's avatar
   
gbazin committed
286
287
    /* Always use our get_buffer wrapper so we can calculate the
     * PTS correctly */
gbazin's avatar
   
gbazin committed
288
289
290
    p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
    p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
    p_sys->p_context->opaque = p_dec;
gbazin's avatar
   
gbazin committed
291

292
    /* ***** init this codec with special data ***** */
gbazin's avatar
   
gbazin committed
293
    if( p_dec->fmt_in.i_extra )
294
    {
gbazin's avatar
   
gbazin committed
295
        int i_size = p_dec->fmt_in.i_extra;
296

297
        if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
298
        {
Laurent Aimar's avatar
Laurent Aimar committed
299
300
            uint8_t *p;

gbazin's avatar
   
gbazin committed
301
302
303
            p_sys->p_context->extradata_size = i_size + 12;
            p = p_sys->p_context->extradata  =
                malloc( p_sys->p_context->extradata_size );
Laurent Aimar's avatar
Laurent Aimar committed
304
305
306

            memcpy( &p[0],  "SVQ3", 4 );
            memset( &p[4], 0, 8 );
gbazin's avatar
   
gbazin committed
307
            memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
308
309

            /* Now remove all atoms before the SMI one */
310
311
            if( p_sys->p_context->extradata_size > 0x5a &&
                strncmp( &p[0x56], "SMI ", 4 ) )
312
313
314
315
316
317
318
319
320
321
322
323
324
            {
                uint8_t *psz = &p[0x52];

                while( psz < &p[p_sys->p_context->extradata_size - 8] )
                {
                    int i_size = GetDWBE( psz );
                    if( i_size <= 1 )
                    {
                        /* FIXME handle 1 as long size */
                        break;
                    }
                    if( !strncmp( &psz[4], "SMI ", 4 ) )
                    {
325
326
                        memmove( &p[0x52], psz,
                                 &p[p_sys->p_context->extradata_size] - psz );
327
328
329
330
331
332
                        break;
                    }

                    psz += i_size;
                }
            }
333
        }
334
335
336
337
338
339
340
341
342
343
344
345
346
347
        else if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '0' ) ||
                 p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '3' ) ||
                 p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '2', '0' ) )
        {
            if( p_dec->fmt_in.i_extra == 8 )
            {
                p_sys->p_context->extradata_size = 8;
                p_sys->p_context->extradata = malloc( 8 );

                memcpy( p_sys->p_context->extradata,
                        p_dec->fmt_in.p_extra,
                        p_dec->fmt_in.i_extra );
                p_sys->p_context->sub_id= ((uint32_t*)p_dec->fmt_in.p_extra)[1];

348
349
                msg_Warn( p_dec, "using extra data for RV codec sub_id=%08x",
                          p_sys->p_context->sub_id );
350
351
            }
        }
352
        else if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'a', 'v', 'c', '1' ) )
353
        {
gbazin's avatar
   
gbazin committed
354
355
            p_sys->p_context->extradata_size = i_size;
            p_sys->p_context->extradata =
gbazin's avatar
   
gbazin committed
356
                malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
gbazin's avatar
   
gbazin committed
357
            memcpy( p_sys->p_context->extradata,
gbazin's avatar
   
gbazin committed
358
                    p_dec->fmt_in.p_extra, i_size );
gbazin's avatar
   
gbazin committed
359
            memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
gbazin's avatar
   
gbazin committed
360
                    0, FF_INPUT_BUFFER_PADDING_SIZE );
361
362
        }
    }
gbazin's avatar
   
gbazin committed
363

gbazin's avatar
   
gbazin committed
364
    /* ***** misc init ***** */
gbazin's avatar
   
gbazin committed
365
    p_sys->input_pts = p_sys->input_dts = 0;
gbazin's avatar
   
gbazin committed
366
367
368
    p_sys->i_pts = 0;
    p_sys->b_has_b_frames = VLC_FALSE;
    p_sys->i_late_frames = 0;
gbazin's avatar
   
gbazin committed
369
370
371
372
373
374
375
    p_sys->i_buffer = 0;
    p_sys->i_buffer_orig = 1;
    p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer );

    /* Set output properties */
    p_dec->fmt_out.i_cat = VIDEO_ES;
    p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
gbazin's avatar
   
gbazin committed
376

gbazin's avatar
   
gbazin committed
377
    /* Setup palette */
378
#if LIBAVCODEC_BUILD >= 4688
gbazin's avatar
   
gbazin committed
379
380
381
382
383
    if( p_dec->fmt_in.video.p_palette )
        p_sys->p_context->palctrl =
            (AVPaletteControl *)p_dec->fmt_in.video.p_palette;
    else
        p_sys->p_context->palctrl = &palette_control;
384
#endif
gbazin's avatar
   
gbazin committed
385

386
387
388
389
390
391
392
393
394
395
396
397
    /* ***** Open the codec ***** */
    vlc_mutex_lock( lockval.p_address );
    if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
    {
        vlc_mutex_unlock( lockval.p_address );
        msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
        return VLC_EGENERIC;
    }
    vlc_mutex_unlock( lockval.p_address );
    msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );


gbazin's avatar
   
gbazin committed
398
    return VLC_SUCCESS;
399
400
401
}

/*****************************************************************************
gbazin's avatar
   
gbazin committed
402
 * DecodeVideo: Called to decode one or more frames
403
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
404
picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
405
{
gbazin's avatar
   
gbazin committed
406
    decoder_sys_t *p_sys = p_dec->p_sys;
gbazin's avatar
   
gbazin committed
407
    int b_drawpicture;
408
    int b_null_size = VLC_FALSE;
gbazin's avatar
   
gbazin committed
409
410
411
412
413
    block_t *p_block;

    if( !pp_block || !*pp_block ) return NULL;

    p_block = *pp_block;
gbazin's avatar
   
gbazin committed
414

415
    if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
416
417
418
419
    {
        p_sys->i_buffer = 0;
        p_sys->i_pts = 0; /* To make sure we recover properly */

420
421
422
        p_sys->input_pts = p_sys->input_dts = 0;
        p_sys->i_late_frames = 0;

423
424
425
426
        block_Release( p_block );
        return NULL;
    }

427
428
429
430
431
432
433
434
435
436
437
438
439
440
    if( p_sys->i_late_frames > 0 &&
        mdate() - p_sys->i_late_frames_start > I64C(5000000) )
    {
        if( p_sys->i_pts )
        {
            msg_Err( p_dec, "more than 5 seconds of late video -> "
                     "dropping frame (computer too slow ?)" );
            p_sys->i_pts = 0; /* To make sure we recover properly */
        }
        block_Release( p_block );
        p_sys->i_late_frames--;
        return NULL;
    }

gbazin's avatar
   
gbazin committed
441
    if( p_block->i_pts > 0 || p_block->i_dts > 0 )
gbazin's avatar
   
gbazin committed
442
443
    {
        p_sys->input_pts = p_block->i_pts;
gbazin's avatar
   
gbazin committed
444
445
446
447
        p_sys->input_dts = p_block->i_dts;

        /* Make sure we don't reuse the same timestamps twice */
        p_block->i_pts = p_block->i_dts = 0;
gbazin's avatar
   
gbazin committed
448
    }
449

450
451
    /* TODO implement it in a better way */
    /* A good idea could be to decode all I pictures and see for the other */
gbazin's avatar
   
gbazin committed
452
    if( p_sys->b_hurry_up && p_sys->i_late_frames > 4 )
453
454
    {
        b_drawpicture = 0;
gbazin's avatar
   
gbazin committed
455
        if( p_sys->i_late_frames < 8 )
456
        {
gbazin's avatar
   
gbazin committed
457
            p_sys->p_context->hurry_up = 2;
458
459
460
        }
        else
        {
gbazin's avatar
   
gbazin committed
461
462
463
464
465
            /* picture too late, won't decode
             * but break picture until a new I, and for mpeg4 ...*/

            p_sys->i_late_frames--; /* needed else it will never be decrease */
            block_Release( p_block );
gbazin's avatar
   
gbazin committed
466
467
            p_sys->i_buffer = 0;
            return NULL;
468
469
470
471
472
        }
    }
    else
    {
        b_drawpicture = 1;
gbazin's avatar
   
gbazin committed
473
        p_sys->p_context->hurry_up = 0;
474
475
    }

476

477
    if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
gbazin's avatar
   
gbazin committed
478
479
    {
        p_sys->p_context->hurry_up = 5;
480
        b_null_size = VLC_TRUE;
481
482
    }

gbazin's avatar
   
gbazin committed
483
484
485
486
    /*
     * Do the actual decoding now
     */

gbazin's avatar
   
gbazin committed
487
488
489
    /* Check if post-processing was enabled */
    p_sys->b_pp = p_sys->b_pp_async;

gbazin's avatar
   
gbazin committed
490
491
    /* Don't forget that ffmpeg requires a little more bytes
     * that the real frame size */
492
    if( p_block->i_buffer > 0 )
493
    {
gbazin's avatar
   
gbazin committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
        p_sys->i_buffer = p_block->i_buffer;
        if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
            p_sys->i_buffer_orig )
        {
            free( p_sys->p_buffer_orig );
            p_sys->i_buffer_orig =
                p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
            p_sys->p_buffer_orig = malloc( p_sys->i_buffer_orig );
        }
        p_sys->p_buffer = p_sys->p_buffer_orig;
        p_sys->i_buffer = p_block->i_buffer;
        p_dec->p_vlc->pf_memcpy( p_sys->p_buffer, p_block->p_buffer,
                                 p_block->i_buffer );
        memset( p_sys->p_buffer + p_block->i_buffer, 0,
                FF_INPUT_BUFFER_PADDING_SIZE );

        p_block->i_buffer = 0;
511
512
    }

513
    while( p_sys->i_buffer > 0 )
514
    {
gbazin's avatar
   
gbazin committed
515
516
517
518
519
        int i_used, b_gotpicture;
        picture_t *p_pic;

        i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
                                       &b_gotpicture,
gbazin's avatar
   
gbazin committed
520
                                       p_sys->p_buffer, p_sys->i_buffer );
521
522
523
524
525
526
527
528
529
530
        if( b_null_size && p_sys->p_context->width > 0 && p_sys->p_context->height > 0 )
        {
            /* Reparse it to not drop the I frame */
            b_null_size = VLC_FALSE;
            p_sys->p_context->hurry_up = 0;
            i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
                                           &b_gotpicture,
                                           p_sys->p_buffer, p_sys->i_buffer );
        }

gbazin's avatar
   
gbazin committed
531
        if( i_used < 0 )
532
        {
gbazin's avatar
   
gbazin committed
533
534
            msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
                      p_sys->i_buffer );
gbazin's avatar
   
gbazin committed
535
            block_Release( p_block );
gbazin's avatar
   
gbazin committed
536
            return NULL;
537
        }
538
539
540
541
        else if( i_used > p_sys->i_buffer )
        {
            i_used = p_sys->i_buffer;
        }
gbazin's avatar
   
gbazin committed
542

gbazin's avatar
   
gbazin committed
543
        /* Consumed bytes */
gbazin's avatar
   
gbazin committed
544
545
        p_sys->i_buffer -= i_used;
        p_sys->p_buffer += i_used;
gbazin's avatar
   
gbazin committed
546
547

        /* Nothing to display */
548
549
        if( !b_gotpicture )
        {
gbazin's avatar
gbazin committed
550
            if( i_used == 0 ) break;
551
552
            continue;
        }
gbazin's avatar
   
gbazin committed
553
554
555
556
557
558
559
560
561

        /* Update frame late count*/
        if( p_sys->i_pts && p_sys->i_pts <= mdate() )
        {
            p_sys->i_late_frames++;
            if( p_sys->i_late_frames == 1 )
                p_sys->i_late_frames_start = mdate();
        }
        else
562
        {
gbazin's avatar
   
gbazin committed
563
            p_sys->i_late_frames = 0;
564
565
        }

gbazin's avatar
gbazin committed
566
        if( !b_drawpicture || !p_sys->p_ff_pic->linesize[0] )
gbazin's avatar
   
gbazin committed
567
568
569
570
        {
            /* Do not display the picture */
            continue;
        }
571

gbazin's avatar
   
gbazin committed
572
        if( !p_sys->p_ff_pic->opaque )
573
        {
gbazin's avatar
   
gbazin committed
574
            /* Get a new picture */
gbazin's avatar
   
gbazin committed
575
576
            p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
            if( !p_pic )
577
            {
gbazin's avatar
   
gbazin committed
578
579
                block_Release( p_block );
                return NULL;
580
            }
581

gbazin's avatar
   
gbazin committed
582
583
584
585
586
            /* Fill p_picture_t from AVVideoFrame and do chroma conversion
             * if needed */
            ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
        }
        else
587
        {
gbazin's avatar
   
gbazin committed
588
            p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
589
        }
gbazin's avatar
   
gbazin committed
590

gbazin's avatar
   
gbazin committed
591
592
593
594
        /* Set the PTS */
        if( p_sys->p_ff_pic->pts ) p_sys->i_pts = p_sys->p_ff_pic->pts;

        /* Sanity check (seems to be needed for some streams ) */
gbazin's avatar
   
gbazin committed
595
        if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
596
        {
gbazin's avatar
   
gbazin committed
597
            p_sys->b_has_b_frames = VLC_TRUE;
598
599
        }

gbazin's avatar
   
gbazin committed
600
601
        /* Send decoded frame to vout */
        if( p_sys->i_pts )
602
        {
gbazin's avatar
   
gbazin committed
603
            p_pic->date = p_sys->i_pts;
gbazin's avatar
   
gbazin committed
604
605
606

            /* interpolate the next PTS */
            if( p_sys->p_context->frame_rate > 0 )
gbazin's avatar
   
gbazin committed
607
            {
gbazin's avatar
   
gbazin committed
608
609
610
611
                p_sys->i_pts += I64C(1000000) *
                    (2 + p_sys->p_ff_pic->repeat_pict) *
                    p_sys->p_context->frame_rate_base /
                    (2 * p_sys->p_context->frame_rate);
gbazin's avatar
   
gbazin committed
612
            }
gbazin's avatar
   
gbazin committed
613
614
615
616
617
            return p_pic;
        }
        else
        {
            p_dec->pf_vout_buffer_del( p_dec, p_pic );
618
        }
gbazin's avatar
   
gbazin committed
619
620
    }

gbazin's avatar
   
gbazin committed
621
    block_Release( p_block );
gbazin's avatar
   
gbazin committed
622
    return NULL;
623
624
625
}

/*****************************************************************************
gbazin's avatar
   
gbazin committed
626
 * EndVideo: decoder destruction
627
628
629
630
 *****************************************************************************
 * This function is called when the thread ends after a sucessful
 * initialization.
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
631
void E_(EndVideoDec)( decoder_t *p_dec )
632
{
gbazin's avatar
   
gbazin committed
633
634
635
    decoder_sys_t *p_sys = p_dec->p_sys;

    if( p_sys->p_ff_pic ) free( p_sys->p_ff_pic );
gbazin's avatar
   
gbazin committed
636

637
#ifdef LIBAVCODEC_PP
gbazin's avatar
   
gbazin committed
638
    E_(ClosePostproc)( p_dec, p_sys->p_pp );
639
#endif
640

gbazin's avatar
   
gbazin committed
641
    free( p_sys->p_buffer_orig );
gbazin's avatar
   
gbazin committed
642
643
644
645
646
647
}

/*****************************************************************************
 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
 *                     picture_t structure (when not in direct rendering mode).
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
648
649
static void ffmpeg_CopyPicture( decoder_t *p_dec,
                                picture_t *p_pic, AVFrame *p_ff_pic )
gbazin's avatar
   
gbazin committed
650
{
gbazin's avatar
   
gbazin committed
651
    decoder_sys_t *p_sys = p_dec->p_sys;
gbazin's avatar
   
gbazin committed
652

gbazin's avatar
   
gbazin committed
653
    if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
gbazin's avatar
   
gbazin committed
654
    {
gbazin's avatar
   
gbazin committed
655
656
657
        int i_plane, i_size, i_line;
        uint8_t *p_dst, *p_src;
        int i_src_stride, i_dst_stride;
658

659
#ifdef LIBAVCODEC_PP
gbazin's avatar
   
gbazin committed
660
        if( p_sys->p_pp && p_sys->b_pp )
661
662
663
            E_(PostprocPict)( p_dec, p_sys->p_pp, p_pic, p_ff_pic );
        else
#endif
gbazin's avatar
   
gbazin committed
664
        for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
665
        {
gbazin's avatar
   
gbazin committed
666
667
668
669
            p_src  = p_ff_pic->data[i_plane];
            p_dst = p_pic->p[i_plane].p_pixels;
            i_src_stride = p_ff_pic->linesize[i_plane];
            i_dst_stride = p_pic->p[i_plane].i_pitch;
670

gbazin's avatar
   
gbazin committed
671
672
673
674
675
676
            i_size = __MIN( i_src_stride, i_dst_stride );
            for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
            {
                p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_size );
                p_src += i_src_stride;
                p_dst += i_dst_stride;
677
678
            }
        }
gbazin's avatar
   
gbazin committed
679
680
681
    }
    else
    {
gbazin's avatar
   
gbazin committed
682
683
684
        AVPicture dest_pic;
        int i;

gbazin's avatar
   
gbazin committed
685
        /* we need to convert to I420 */
gbazin's avatar
   
gbazin committed
686
        switch( p_sys->p_context->pix_fmt )
gbazin's avatar
   
gbazin committed
687
        {
gbazin's avatar
   
gbazin committed
688
689
690
        case PIX_FMT_YUV410P:
        case PIX_FMT_YUV411P:
        case PIX_FMT_PAL8:
gbazin's avatar
   
gbazin committed
691
692
693
694
695
696
697
698
699
700
701
702
703
704
            for( i = 0; i < p_pic->i_planes; i++ )
            {
                dest_pic.data[i] = p_pic->p[i].p_pixels;
                dest_pic.linesize[i] = p_pic->p[i].i_pitch;
            }
            img_convert( &dest_pic, PIX_FMT_YUV420P,
                         (AVPicture *)p_ff_pic,
                         p_sys->p_context->pix_fmt,
                         p_sys->p_context->width,
                         p_sys->p_context->height );
            break;
        default:
            msg_Err( p_dec, "don't know how to convert chroma %i",
                     p_sys->p_context->pix_fmt );
Laurent Aimar's avatar
Laurent Aimar committed
705
            p_dec->b_error = 1;
gbazin's avatar
   
gbazin committed
706
            break;
gbazin's avatar
   
gbazin committed
707
708
709
        }
    }
}
710

gbazin's avatar
   
gbazin committed
711
712
/*****************************************************************************
 * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
gbazin's avatar
   
gbazin committed
713
714
715
 *****************************************************************************
 * It is used for direct rendering as well as to get the right PTS for each
 * decoded picture (even in indirect rendering mode).
gbazin's avatar
   
gbazin committed
716
 *****************************************************************************/
717
static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
718
                               AVFrame *p_ff_pic )
gbazin's avatar
   
gbazin committed
719
{
gbazin's avatar
   
gbazin committed
720
721
    decoder_t *p_dec = (decoder_t *)p_context->opaque;
    decoder_sys_t *p_sys = p_dec->p_sys;
gbazin's avatar
   
gbazin committed
722
723
    picture_t *p_pic;

gbazin's avatar
   
gbazin committed
724
    /* Set picture PTS */
gbazin's avatar
   
gbazin committed
725
726
727
728
729
730
731
732
733
    if( p_sys->input_pts )
    {
        p_ff_pic->pts = p_sys->input_pts;
    }
    else if( p_sys->input_dts )
    {
        /* Some demuxers only set the dts so let's try to find a useful
         * timestamp from this */
        if( !p_context->has_b_frames || !p_sys->b_has_b_frames ||
734
            !p_ff_pic->reference || !p_sys->i_pts )
gbazin's avatar
   
gbazin committed
735
736
737
738
739
740
741
        {
            p_ff_pic->pts = p_sys->input_dts;
        }
        else p_ff_pic->pts = 0;
    }
    else p_ff_pic->pts = 0;

742
743
744
745
746
    if( p_sys->i_pts ) /* make sure 1st frame has a pts > 0 */
    {
        p_sys->input_pts = p_sys->input_dts = 0;
    }

gbazin's avatar
   
gbazin committed
747
    p_ff_pic->opaque = 0;
gbazin's avatar
   
gbazin committed
748
749

    /* Not much to do in indirect rendering mode */
gbazin's avatar
   
gbazin committed
750
    if( !p_sys->b_direct_rendering || p_sys->b_pp )
gbazin's avatar
   
gbazin committed
751
752
753
754
    {
        return avcodec_default_get_buffer( p_context, p_ff_pic );
    }

gbazin's avatar
   
gbazin committed
755
756
    /* Some codecs set pix_fmt only after the 1st frame has been decoded,
     * so this check is necessary. */
757
    if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
gbazin's avatar
   
gbazin committed
758
        p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
gbazin's avatar
   
gbazin committed
759
    {
gbazin's avatar
   
gbazin committed
760
761
        msg_Dbg( p_dec, "disabling direct rendering" );
        p_sys->b_direct_rendering = 0;
gbazin's avatar
   
gbazin committed
762
        return avcodec_default_get_buffer( p_context, p_ff_pic );
gbazin's avatar
   
gbazin committed
763
764
    }

gbazin's avatar
   
gbazin committed
765
766
767
768
    /* Get a new picture */
    //p_sys->p_vout->render.b_allow_modify_pics = 0;
    p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
    if( !p_pic )
gbazin's avatar
   
gbazin committed
769
    {
gbazin's avatar
   
gbazin committed
770
        p_sys->b_direct_rendering = 0;
gbazin's avatar
   
gbazin committed
771
        return avcodec_default_get_buffer( p_context, p_ff_pic );
gbazin's avatar
   
gbazin committed
772
    }
gbazin's avatar
   
gbazin committed
773
    p_sys->p_context->draw_horiz_band = NULL;
774
775

    p_ff_pic->opaque = (void*)p_pic;
776
    p_ff_pic->type = FF_BUFFER_TYPE_USER;
777
778
779
780
    p_ff_pic->data[0] = p_pic->p[0].p_pixels;
    p_ff_pic->data[1] = p_pic->p[1].p_pixels;
    p_ff_pic->data[2] = p_pic->p[2].p_pixels;
    p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
781

782
783
784
785
786
787
788
    p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
    p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
    p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
    p_ff_pic->linesize[3] = 0;

    if( p_ff_pic->reference != 0 )
    {
gbazin's avatar
   
gbazin committed
789
        p_dec->pf_picture_link( p_dec, p_pic );
790
    }
gbazin's avatar
   
gbazin committed
791

792
793
    /* FIXME what is that, should give good value */
    p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
gbazin's avatar
   
gbazin committed
794

gbazin's avatar
   
gbazin committed
795
    return 0;
796
797
}

gbazin's avatar
   
gbazin committed
798
799
static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
                                    AVFrame *p_ff_pic )
800
{
gbazin's avatar
   
gbazin committed
801
    decoder_t *p_dec = (decoder_t *)p_context->opaque;
802
    picture_t *p_pic;
gbazin's avatar
   
gbazin committed
803

gbazin's avatar
   
gbazin committed
804
    if( !p_ff_pic->opaque )
gbazin's avatar
   
gbazin committed
805
806
807
808
809
    {
        avcodec_default_release_buffer( p_context, p_ff_pic );
        return;
    }

810
    p_pic = (picture_t*)p_ff_pic->opaque;
gbazin's avatar
   
gbazin committed
811

812
813
814
815
    p_ff_pic->data[0] = NULL;
    p_ff_pic->data[1] = NULL;
    p_ff_pic->data[2] = NULL;
    p_ff_pic->data[3] = NULL;
gbazin's avatar
   
gbazin committed
816

gbazin's avatar
   
gbazin committed
817
818
    if( p_ff_pic->reference != 0 )
    {
gbazin's avatar
   
gbazin committed
819
        p_dec->pf_picture_unlink( p_dec, p_pic );
gbazin's avatar
   
gbazin committed
820
    }
821
}