video.c 27.3 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;

74
75
76
    /* Hack to force display of still pictures */
    vlc_bool_t b_first_frame;

gbazin's avatar
   
gbazin committed
77
78
    int i_buffer_orig, i_buffer;
    char *p_buffer_orig, *p_buffer;
gbazin's avatar
   
gbazin committed
79
80
81

    /* Postprocessing handle */
    void *p_pp;
gbazin's avatar
   
gbazin committed
82
83
    vlc_bool_t b_pp;
    vlc_bool_t b_pp_async;
gbazin's avatar
   
gbazin committed
84
    vlc_bool_t b_pp_init;
gbazin's avatar
   
gbazin committed
85
};
86

gbazin's avatar
   
gbazin committed
87
88
89
/* FIXME (dummy palette for now) */
static AVPaletteControl palette_control;

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

97
98
99
100
101
102
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
103
104
105
/*****************************************************************************
 * Local Functions
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
106
static uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
107
{
108
109
    switch( i_ff_chroma )
    {
gbazin's avatar
   
gbazin committed
110
    case PIX_FMT_YUV420P:
gbazin's avatar
   
gbazin committed
111
        return VLC_FOURCC('I','4','2','0');
gbazin's avatar
   
gbazin committed
112
    case PIX_FMT_YUV422P:
gbazin's avatar
   
gbazin committed
113
        return VLC_FOURCC('I','4','2','2');
gbazin's avatar
   
gbazin committed
114
    case PIX_FMT_YUV444P:
gbazin's avatar
   
gbazin committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
        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
131
132
133
134
135
    case PIX_FMT_YUV410P:
    case PIX_FMT_YUV411P:
    case PIX_FMT_BGR24:
    default:
        return 0;
136
137
138
    }
}

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

gbazin's avatar
   
gbazin committed
146
147
148
149
150
    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 )
151
    {
gbazin's avatar
   
gbazin committed
152
        return NULL; /* invalid display size */
153
154
    }

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

161
162
163
164
165
166
167
    /* 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
168
#if LIBAVCODEC_BUILD >= 4687
169
170
171
        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
172
#else
gbazin's avatar
   
gbazin committed
173
        p_dec->fmt_out.video.i_aspect =
174
175
176
177
178
179
180
            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;
        }
181
    }
182

183
184
185
186
187
188
    if( p_context->frame_rate > 0 && p_context->frame_rate_base > 0 )
    {
        p_dec->fmt_out.video.i_frame_rate = p_context->frame_rate;
        p_dec->fmt_out.video.i_frame_rate_base = p_context->frame_rate_base;
    }

gbazin's avatar
   
gbazin committed
189
    p_pic = p_dec->pf_vout_buffer_new( p_dec );
190

gbazin's avatar
   
gbazin committed
191
#ifdef LIBAVCODEC_PP
gbazin's avatar
   
gbazin committed
192
    if( p_sys->p_pp && p_sys->b_pp && !p_sys->b_pp_init )
gbazin's avatar
   
gbazin committed
193
194
195
196
197
    {
        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;
    }
198
#endif
gbazin's avatar
   
gbazin committed
199

gbazin's avatar
   
gbazin committed
200
    return p_pic;
201
202
203
}

/*****************************************************************************
gbazin's avatar
   
gbazin committed
204
 * InitVideo: initialize the video decoder
205
 *****************************************************************************
gbazin's avatar
   
gbazin committed
206
207
 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
 * opened (done after the first decoded frame).
208
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
209
210
int E_(InitVideoDec)( decoder_t *p_dec, AVCodecContext *p_context,
                      AVCodec *p_codec, int i_codec_id, char *psz_namecodec )
211
{
gbazin's avatar
   
gbazin committed
212
    decoder_sys_t *p_sys;
213
    vlc_value_t lockval;
gbazin's avatar
   
gbazin committed
214
    vlc_value_t val;
215
216
217

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

gbazin's avatar
   
gbazin committed
218
219
220
221
222
223
224
225
226
227
228
229
230
    /* 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
231

gbazin's avatar
   
gbazin committed
232
    /* ***** Fill p_context with init values ***** */
233
    p_sys->p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_codec );
gbazin's avatar
   
gbazin committed
234
235
    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
236
    p_sys->p_context->bits_per_sample = p_dec->fmt_in.video.i_bits_per_pixel;
237
238

    /*  ***** Get configuration of ffmpeg plugin ***** */
gbazin's avatar
gbazin committed
239
240
241
242
    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" );
243

gbazin's avatar
   
gbazin committed
244
245
246
    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;
247

gbazin's avatar
gbazin committed
248
249
    var_Create( p_dec, "ffmpeg-vismv", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Get( p_dec, "ffmpeg-vismv", &val );
250
#if LIBAVCODEC_BUILD >= 4698
gbazin's avatar
gbazin committed
251
    if( val.i_int ) p_sys->p_context->debug_mv = val.i_int;
252
#endif
gbazin's avatar
gbazin committed
253

254
255
256
257
258
259
    var_Create( p_dec, "ffmpeg-lowres", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Get( p_dec, "ffmpeg-lowres", &val );
#if LIBAVCODEC_BUILD >= 4723
    if( val.i_int > 0 && val.i_int <= 2 ) p_sys->p_context->lowres = val.i_int;
#endif

gbazin's avatar
   
gbazin committed
260
261
262
263
264
265
266
267
268
269
270
    /* ***** 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
271
        /* Apparently direct rendering doesn't work with YUV422P */
gbazin's avatar
   
gbazin committed
272
        p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
gbazin's avatar
gbazin committed
273
274
        /* H264 uses too many reference frames */
        p_sys->i_codec_id != CODEC_ID_H264 &&
275
        !(p_sys->p_context->width % 16) && !(p_sys->p_context->height % 16) &&
276
#if LIBAVCODEC_BUILD >= 4698
277
        !p_sys->p_context->debug_mv )
278
279
280
#else
        1 )
#endif
gbazin's avatar
   
gbazin committed
281
    {
gbazin's avatar
   
gbazin committed
282
283
        /* 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
284
        p_sys->b_direct_rendering = 1;
gbazin's avatar
   
gbazin committed
285
286
287
    }

#ifdef LIBAVCODEC_PP
288
    p_sys->p_pp = NULL;
gbazin's avatar
   
gbazin committed
289
290
    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
291
292
#endif

gbazin's avatar
   
gbazin committed
293
    /* ffmpeg doesn't properly release old pictures when frames are skipped */
gbazin's avatar
   
gbazin committed
294
    //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
gbazin's avatar
   
gbazin committed
295
296
297
298
299
    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
300

gbazin's avatar
   
gbazin committed
301
302
    /* Always use our get_buffer wrapper so we can calculate the
     * PTS correctly */
gbazin's avatar
   
gbazin committed
303
304
305
    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
306

307
    /* ***** init this codec with special data ***** */
gbazin's avatar
   
gbazin committed
308
    if( p_dec->fmt_in.i_extra )
309
    {
gbazin's avatar
   
gbazin committed
310
        int i_size = p_dec->fmt_in.i_extra;
311

312
        if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
313
        {
Laurent Aimar's avatar
Laurent Aimar committed
314
315
            uint8_t *p;

gbazin's avatar
   
gbazin committed
316
317
318
            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
319
320
321

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

            /* Now remove all atoms before the SMI one */
325
326
            if( p_sys->p_context->extradata_size > 0x5a &&
                strncmp( &p[0x56], "SMI ", 4 ) )
327
328
329
330
331
332
333
334
335
336
337
338
339
            {
                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 ) )
                    {
340
341
                        memmove( &p[0x52], psz,
                                 &p[p_sys->p_context->extradata_size] - psz );
342
343
344
345
346
347
                        break;
                    }

                    psz += i_size;
                }
            }
348
        }
349
350
351
352
353
354
355
356
357
358
359
360
361
362
        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];

363
364
                msg_Warn( p_dec, "using extra data for RV codec sub_id=%08x",
                          p_sys->p_context->sub_id );
365
366
            }
        }
367
        else
368
        {
gbazin's avatar
   
gbazin committed
369
370
            p_sys->p_context->extradata_size = i_size;
            p_sys->p_context->extradata =
gbazin's avatar
   
gbazin committed
371
                malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
gbazin's avatar
   
gbazin committed
372
            memcpy( p_sys->p_context->extradata,
gbazin's avatar
   
gbazin committed
373
                    p_dec->fmt_in.p_extra, i_size );
gbazin's avatar
   
gbazin committed
374
            memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
gbazin's avatar
   
gbazin committed
375
                    0, FF_INPUT_BUFFER_PADDING_SIZE );
376
377
        }
    }
gbazin's avatar
   
gbazin committed
378

gbazin's avatar
   
gbazin committed
379
    /* ***** misc init ***** */
gbazin's avatar
   
gbazin committed
380
    p_sys->input_pts = p_sys->input_dts = 0;
gbazin's avatar
   
gbazin committed
381
382
    p_sys->i_pts = 0;
    p_sys->b_has_b_frames = VLC_FALSE;
383
    p_sys->b_first_frame = VLC_TRUE;
gbazin's avatar
   
gbazin committed
384
    p_sys->i_late_frames = 0;
gbazin's avatar
   
gbazin committed
385
386
    p_sys->i_buffer = 0;
    p_sys->i_buffer_orig = 1;
387
    p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer_orig );
gbazin's avatar
   
gbazin committed
388
389
390
391

    /* 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
392

gbazin's avatar
   
gbazin committed
393
    /* Setup palette */
394
#if LIBAVCODEC_BUILD >= 4688
gbazin's avatar
   
gbazin committed
395
396
397
398
399
    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;
400
#endif
gbazin's avatar
   
gbazin committed
401

402
403
404
405
406
407
408
409
410
411
412
413
    /* ***** 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
414
    return VLC_SUCCESS;
415
416
417
}

/*****************************************************************************
gbazin's avatar
   
gbazin committed
418
 * DecodeVideo: Called to decode one or more frames
419
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
420
picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
421
{
gbazin's avatar
   
gbazin committed
422
    decoder_sys_t *p_sys = p_dec->p_sys;
gbazin's avatar
   
gbazin committed
423
    int b_drawpicture;
424
    int b_null_size = VLC_FALSE;
gbazin's avatar
   
gbazin committed
425
426
427
428
429
    block_t *p_block;

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

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

431
    if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
432
433
434
435
    {
        p_sys->i_buffer = 0;
        p_sys->i_pts = 0; /* To make sure we recover properly */

436
437
438
        p_sys->input_pts = p_sys->input_dts = 0;
        p_sys->i_late_frames = 0;

439
440
441
442
        block_Release( p_block );
        return NULL;
    }

443
    if( !p_dec->b_pace_control && p_sys->i_late_frames > 0 &&
444
445
446
447
448
449
450
451
452
453
454
455
456
        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
457
    if( p_block->i_pts > 0 || p_block->i_dts > 0 )
gbazin's avatar
   
gbazin committed
458
459
    {
        p_sys->input_pts = p_block->i_pts;
gbazin's avatar
   
gbazin committed
460
461
462
463
        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
464
    }
465

466
467
    /* TODO implement it in a better way */
    /* A good idea could be to decode all I pictures and see for the other */
468
469
    if( !p_dec->b_pace_control &&
        p_sys->b_hurry_up && p_sys->i_late_frames > 4 )
470
471
    {
        b_drawpicture = 0;
gbazin's avatar
   
gbazin committed
472
        if( p_sys->i_late_frames < 8 )
473
        {
gbazin's avatar
   
gbazin committed
474
            p_sys->p_context->hurry_up = 2;
475
476
477
        }
        else
        {
gbazin's avatar
   
gbazin committed
478
479
480
481
482
            /* 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
483
484
            p_sys->i_buffer = 0;
            return NULL;
485
486
487
488
489
        }
    }
    else
    {
        b_drawpicture = 1;
gbazin's avatar
   
gbazin committed
490
        p_sys->p_context->hurry_up = 0;
491
492
    }

493

494
    if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
gbazin's avatar
   
gbazin committed
495
496
    {
        p_sys->p_context->hurry_up = 5;
497
        b_null_size = VLC_TRUE;
498
499
    }

gbazin's avatar
   
gbazin committed
500
501
502
503
    /*
     * Do the actual decoding now
     */

gbazin's avatar
   
gbazin committed
504
505
506
    /* Check if post-processing was enabled */
    p_sys->b_pp = p_sys->b_pp_async;

gbazin's avatar
   
gbazin committed
507
508
    /* Don't forget that ffmpeg requires a little more bytes
     * that the real frame size */
509
    if( p_block->i_buffer > 0 )
510
    {
gbazin's avatar
   
gbazin committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
        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;
528
529
    }

530
    while( p_sys->i_buffer > 0 )
531
    {
gbazin's avatar
   
gbazin committed
532
533
534
535
536
        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
537
                                       p_sys->p_buffer, p_sys->i_buffer );
538
539
        if( b_null_size && p_sys->p_context->width > 0 &&
            p_sys->p_context->height > 0 )
540
541
542
543
544
545
546
547
548
        {
            /* 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
549
        if( i_used < 0 )
550
        {
gbazin's avatar
   
gbazin committed
551
552
            msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
                      p_sys->i_buffer );
gbazin's avatar
   
gbazin committed
553
            block_Release( p_block );
gbazin's avatar
   
gbazin committed
554
            return NULL;
555
        }
556
557
558
559
        else if( i_used > p_sys->i_buffer )
        {
            i_used = p_sys->i_buffer;
        }
gbazin's avatar
   
gbazin committed
560

gbazin's avatar
   
gbazin committed
561
        /* Consumed bytes */
gbazin's avatar
   
gbazin committed
562
563
        p_sys->i_buffer -= i_used;
        p_sys->p_buffer += i_used;
gbazin's avatar
   
gbazin committed
564
565

        /* Nothing to display */
566
567
        if( !b_gotpicture )
        {
gbazin's avatar
gbazin committed
568
            if( i_used == 0 ) break;
569
570
            continue;
        }
gbazin's avatar
   
gbazin committed
571
572
573
574
575
576
577
578
579

        /* 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
580
        {
gbazin's avatar
   
gbazin committed
581
            p_sys->i_late_frames = 0;
582
583
        }

gbazin's avatar
gbazin committed
584
        if( !b_drawpicture || !p_sys->p_ff_pic->linesize[0] )
gbazin's avatar
   
gbazin committed
585
586
587
588
        {
            /* Do not display the picture */
            continue;
        }
589

gbazin's avatar
   
gbazin committed
590
        if( !p_sys->p_ff_pic->opaque )
591
        {
gbazin's avatar
   
gbazin committed
592
            /* Get a new picture */
gbazin's avatar
   
gbazin committed
593
594
            p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
            if( !p_pic )
595
            {
gbazin's avatar
   
gbazin committed
596
597
                block_Release( p_block );
                return NULL;
598
            }
599

gbazin's avatar
   
gbazin committed
600
601
602
603
604
            /* Fill p_picture_t from AVVideoFrame and do chroma conversion
             * if needed */
            ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
        }
        else
605
        {
gbazin's avatar
   
gbazin committed
606
            p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
607
        }
gbazin's avatar
   
gbazin committed
608

gbazin's avatar
   
gbazin committed
609
610
611
612
        /* 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
613
        if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
614
        {
gbazin's avatar
   
gbazin committed
615
            p_sys->b_has_b_frames = VLC_TRUE;
616
617
        }

gbazin's avatar
   
gbazin committed
618
619
        /* Send decoded frame to vout */
        if( p_sys->i_pts )
620
        {
gbazin's avatar
   
gbazin committed
621
            p_pic->date = p_sys->i_pts;
gbazin's avatar
   
gbazin committed
622
623
624

            /* interpolate the next PTS */
            if( p_sys->p_context->frame_rate > 0 )
gbazin's avatar
   
gbazin committed
625
            {
gbazin's avatar
   
gbazin committed
626
627
628
629
                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
630
            }
631
632
633
634
635
636
637

            if( p_sys->b_first_frame )
            {
                /* Hack to force display of still pictures */
                p_sys->b_first_frame = VLC_FALSE;
                p_pic->b_force = VLC_TRUE;
            }
638
639
640
641
642
643
644

            p_pic->i_nb_fields = p_sys->p_ff_pic->repeat_pict;
#if LIBAVCODEC_BUILD >= 4685
            p_pic->b_progressive = !p_sys->p_ff_pic->interlaced_frame;
            p_pic->b_top_field_first = p_sys->p_ff_pic->top_field_first;
#endif

gbazin's avatar
   
gbazin committed
645
646
647
648
649
            return p_pic;
        }
        else
        {
            p_dec->pf_vout_buffer_del( p_dec, p_pic );
650
        }
gbazin's avatar
   
gbazin committed
651
652
    }

gbazin's avatar
   
gbazin committed
653
    block_Release( p_block );
gbazin's avatar
   
gbazin committed
654
    return NULL;
655
656
657
}

/*****************************************************************************
gbazin's avatar
   
gbazin committed
658
 * EndVideo: decoder destruction
659
660
661
662
 *****************************************************************************
 * This function is called when the thread ends after a sucessful
 * initialization.
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
663
void E_(EndVideoDec)( decoder_t *p_dec )
664
{
gbazin's avatar
   
gbazin committed
665
666
    decoder_sys_t *p_sys = p_dec->p_sys;

667
    if( p_sys->p_ff_pic ) av_free( p_sys->p_ff_pic );
gbazin's avatar
   
gbazin committed
668

669
#ifdef LIBAVCODEC_PP
gbazin's avatar
   
gbazin committed
670
    E_(ClosePostproc)( p_dec, p_sys->p_pp );
671
#endif
672

gbazin's avatar
   
gbazin committed
673
    free( p_sys->p_buffer_orig );
gbazin's avatar
   
gbazin committed
674
675
676
677
678
679
}

/*****************************************************************************
 * 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
680
681
static void ffmpeg_CopyPicture( decoder_t *p_dec,
                                picture_t *p_pic, AVFrame *p_ff_pic )
gbazin's avatar
   
gbazin committed
682
{
gbazin's avatar
   
gbazin committed
683
    decoder_sys_t *p_sys = p_dec->p_sys;
gbazin's avatar
   
gbazin committed
684

gbazin's avatar
   
gbazin committed
685
    if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
gbazin's avatar
   
gbazin committed
686
    {
gbazin's avatar
   
gbazin committed
687
688
689
        int i_plane, i_size, i_line;
        uint8_t *p_dst, *p_src;
        int i_src_stride, i_dst_stride;
690

691
#ifdef LIBAVCODEC_PP
gbazin's avatar
   
gbazin committed
692
        if( p_sys->p_pp && p_sys->b_pp )
693
694
695
            E_(PostprocPict)( p_dec, p_sys->p_pp, p_pic, p_ff_pic );
        else
#endif
gbazin's avatar
   
gbazin committed
696
        for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
697
        {
gbazin's avatar
   
gbazin committed
698
699
700
701
            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;
702

gbazin's avatar
   
gbazin committed
703
            i_size = __MIN( i_src_stride, i_dst_stride );
704
705
            for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
                 i_line++ )
gbazin's avatar
   
gbazin committed
706
707
708
709
            {
                p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_size );
                p_src += i_src_stride;
                p_dst += i_dst_stride;
710
711
            }
        }
gbazin's avatar
   
gbazin committed
712
713
714
    }
    else
    {
gbazin's avatar
   
gbazin committed
715
716
717
        AVPicture dest_pic;
        int i;

gbazin's avatar
   
gbazin committed
718
        /* we need to convert to I420 */
gbazin's avatar
   
gbazin committed
719
        switch( p_sys->p_context->pix_fmt )
gbazin's avatar
   
gbazin committed
720
        {
gbazin's avatar
   
gbazin committed
721
722
723
        case PIX_FMT_YUV410P:
        case PIX_FMT_YUV411P:
        case PIX_FMT_PAL8:
gbazin's avatar
   
gbazin committed
724
725
726
727
728
729
730
731
732
733
734
735
736
737
            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
738
            p_dec->b_error = 1;
gbazin's avatar
   
gbazin committed
739
            break;
gbazin's avatar
   
gbazin committed
740
741
742
        }
    }
}
743

gbazin's avatar
   
gbazin committed
744
745
/*****************************************************************************
 * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
gbazin's avatar
   
gbazin committed
746
747
748
 *****************************************************************************
 * 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
749
 *****************************************************************************/
750
static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
751
                               AVFrame *p_ff_pic )
gbazin's avatar
   
gbazin committed
752
{
gbazin's avatar
   
gbazin committed
753
754
    decoder_t *p_dec = (decoder_t *)p_context->opaque;
    decoder_sys_t *p_sys = p_dec->p_sys;
gbazin's avatar
   
gbazin committed
755
756
    picture_t *p_pic;

gbazin's avatar
   
gbazin committed
757
    /* Set picture PTS */
gbazin's avatar
   
gbazin committed
758
759
760
761
762
763
764
765
766
    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 ||
767
            !p_ff_pic->reference || !p_sys->i_pts )
gbazin's avatar
   
gbazin committed
768
769
770
771
772
773
774
        {
            p_ff_pic->pts = p_sys->input_dts;
        }
        else p_ff_pic->pts = 0;
    }
    else p_ff_pic->pts = 0;

775
776
777
778
779
    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
780
    p_ff_pic->opaque = 0;
gbazin's avatar
   
gbazin committed
781
782

    /* Not much to do in indirect rendering mode */
gbazin's avatar
   
gbazin committed
783
    if( !p_sys->b_direct_rendering || p_sys->b_pp )
gbazin's avatar
   
gbazin committed
784
785
786
787
    {
        return avcodec_default_get_buffer( p_context, p_ff_pic );
    }

gbazin's avatar
   
gbazin committed
788
789
    /* Some codecs set pix_fmt only after the 1st frame has been decoded,
     * so this check is necessary. */
790
    if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
gbazin's avatar
   
gbazin committed
791
        p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
gbazin's avatar
   
gbazin committed
792
    {
gbazin's avatar
   
gbazin committed
793
794
        msg_Dbg( p_dec, "disabling direct rendering" );
        p_sys->b_direct_rendering = 0;
gbazin's avatar
   
gbazin committed
795
        return avcodec_default_get_buffer( p_context, p_ff_pic );
gbazin's avatar
   
gbazin committed
796
797
    }

gbazin's avatar
   
gbazin committed
798
799
800
801
    /* 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
802
    {
gbazin's avatar
   
gbazin committed
803
        p_sys->b_direct_rendering = 0;
gbazin's avatar
   
gbazin committed
804
        return avcodec_default_get_buffer( p_context, p_ff_pic );
gbazin's avatar
   
gbazin committed
805
    }
gbazin's avatar
   
gbazin committed
806
    p_sys->p_context->draw_horiz_band = NULL;
807
808

    p_ff_pic->opaque = (void*)p_pic;
809
    p_ff_pic->type = FF_BUFFER_TYPE_USER;
810
811
812
813
    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 */
814

815
816
817
818
819
820
821
    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
822
        p_dec->pf_picture_link( p_dec, p_pic );
823
    }
gbazin's avatar
   
gbazin committed
824

825
826
    /* 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
827

gbazin's avatar
   
gbazin committed
828
    return 0;
829
830
}

gbazin's avatar
   
gbazin committed
831
832
static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
                                    AVFrame *p_ff_pic )
833
{
gbazin's avatar
   
gbazin committed
834
    decoder_t *p_dec = (decoder_t *)p_context->opaque;
835
    picture_t *p_pic;
gbazin's avatar
   
gbazin committed
836

gbazin's avatar
   
gbazin committed
837
    if( !p_ff_pic->opaque )
gbazin's avatar
   
gbazin committed
838
839
840
841
842
    {
        avcodec_default_release_buffer( p_context, p_ff_pic );
        return;
    }

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

845
846
847
848
    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
849

gbazin's avatar
   
gbazin committed
850
851
    if( p_ff_pic->reference != 0 )
    {
gbazin's avatar
   
gbazin committed
852
        p_dec->pf_picture_unlink( p_dec, p_pic );
gbazin's avatar
   
gbazin committed
853
    }
854
}