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

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
    }

155 156 157 158 159
#if LIBAVCODEC_BUILD >= 4723
    p_dec->fmt_out.video.i_width >>= p_context->lowres;
    p_dec->fmt_out.video.i_height >>= p_context->lowres;
#endif

gbazin's avatar
 
gbazin committed
160
    if( !p_dec->fmt_out.i_codec )
161 162
    {
        /* we make conversion if possible*/
gbazin's avatar
 
gbazin committed
163
        p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
164
    }
165

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

492

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            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;
            }
637 638 639 640 641 642 643

            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
644 645 646 647 648
            return p_pic;
        }
        else
        {
            p_dec->pf_vout_buffer_del( p_dec, p_pic );
649
        }
gbazin's avatar
 
gbazin committed
650 651
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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