~ubuntu-branches/ubuntu/maverick/vlc/maverick

« back to all changes in this revision

Viewing changes to modules/codec/avcodec/video.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2008-09-17 21:56:14 UTC
  • mfrom: (1.1.17 upstream)
  • Revision ID: james.westby@ubuntu.com-20080917215614-tj0vx8xzd57e52t8
Tags: 0.9.2-1ubuntu1
* New Upstream Release, exception granted by
    - dktrkranz, norsetto, Hobbsee (via irc). LP: #270404

Changes done in ubuntu:

* add libxul-dev to build-depends
* make sure that vlc is build against libxul in configure. This doesn't
  change anything in the package, but makes it more robust if building
  in an 'unclean' chroot or when modifying the package.
* debian/control: make Vcs-* fields point to the motumedia branch
* add libx264-dev and libass-dev to build-depends
  LP: #210354, #199870
* actually enable libass support by passing --enable-libass to configure
* enable libdca: add libdca-dev to build depends and --enable-libdca
* install the x264 plugin.

Changes already in the pkg-multimedia branch in debian:

* don't install usr/share/vlc/mozilla in debian/mozilla-plugin-vlc.install  
* new upstream .desktop file now registers flash video mimetype LP: #261567
* add Xb-Npp-Applications to mozilla-plugin-vlc
* remove duplicate entries in debian/vlc-nox.install

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 * video.c: video decoder using the ffmpeg library
 
3
 *****************************************************************************
 
4
 * Copyright (C) 1999-2001 the VideoLAN team
 
5
 * $Id: cba209c7e631161135b3054d3c353d29fedb6ac7 $
 
6
 *
 
7
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 
8
 *          Gildas Bazin <gbazin@videolan.org>
 
9
 *
 
10
 * This program is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 2 of the License, or
 
13
 * (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 
23
 *****************************************************************************/
 
24
 
 
25
/*****************************************************************************
 
26
 * Preamble
 
27
 *****************************************************************************/
 
28
#ifdef HAVE_CONFIG_H
 
29
# include "config.h"
 
30
#endif
 
31
 
 
32
#include <vlc_common.h>
 
33
#include <vlc_codec.h>
 
34
#include <vlc_vout.h>
 
35
#include <vlc_input.h>                  /* hmmm, just for INPUT_RATE_DEFAULT */
 
36
#include <vlc_codecs.h>                               /* BITMAPINFOHEADER */
 
37
 
 
38
/* ffmpeg header */
 
39
#ifdef HAVE_LIBAVCODEC_AVCODEC_H
 
40
#   include <libavcodec/avcodec.h>
 
41
#elif defined(HAVE_FFMPEG_AVCODEC_H)
 
42
#   include <ffmpeg/avcodec.h>
 
43
#else
 
44
#   include <avcodec.h>
 
45
#endif
 
46
 
 
47
#include "avcodec.h"
 
48
#include "chroma.h"
 
49
 
 
50
/*****************************************************************************
 
51
 * decoder_sys_t : decoder descriptor
 
52
 *****************************************************************************/
 
53
struct decoder_sys_t
 
54
{
 
55
    FFMPEG_COMMON_MEMBERS
 
56
 
 
57
    /* Video decoder specific part */
 
58
    mtime_t input_pts;
 
59
    mtime_t input_dts;
 
60
    mtime_t i_pts;
 
61
 
 
62
    AVFrame          *p_ff_pic;
 
63
    BITMAPINFOHEADER *p_format;
 
64
 
 
65
    /* for frame skipping algo */
 
66
    int b_hurry_up;
 
67
    enum AVDiscard i_skip_frame;
 
68
    enum AVDiscard i_skip_idct;
 
69
 
 
70
    /* how many decoded frames are late */
 
71
    int     i_late_frames;
 
72
    mtime_t i_late_frames_start;
 
73
 
 
74
    /* for direct rendering */
 
75
    int b_direct_rendering;
 
76
 
 
77
    bool b_has_b_frames;
 
78
 
 
79
    /* Hack to force display of still pictures */
 
80
    bool b_first_frame;
 
81
 
 
82
    int i_buffer_orig, i_buffer;
 
83
    char *p_buffer_orig, *p_buffer;
 
84
 
 
85
 
 
86
    /* */
 
87
    bool b_flush;
 
88
};
 
89
 
 
90
/* FIXME (dummy palette for now) */
 
91
static const AVPaletteControl palette_control;
 
92
 
 
93
/*****************************************************************************
 
94
 * Local prototypes
 
95
 *****************************************************************************/
 
96
static void ffmpeg_InitCodec      ( decoder_t * );
 
97
static void ffmpeg_CopyPicture    ( decoder_t *, picture_t *, AVFrame * );
 
98
static int  ffmpeg_GetFrameBuf    ( struct AVCodecContext *, AVFrame * );
 
99
static int  ffmpeg_ReGetFrameBuf( struct AVCodecContext *, AVFrame * );
 
100
static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
 
101
static void ffmpeg_NextPts( decoder_t *, int i_block_rate );
 
102
 
 
103
static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
 
104
{
 
105
    uint8_t *p = (uint8_t*)&fcc;
 
106
    return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
 
107
}
 
108
 
 
109
/*****************************************************************************
 
110
 * Local Functions
 
111
 *****************************************************************************/
 
112
 
 
113
/* Returns a new picture buffer */
 
114
static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
 
115
                                            AVCodecContext *p_context )
 
116
{
 
117
    picture_t *p_pic;
 
118
 
 
119
    p_dec->fmt_out.video.i_width = p_context->width;
 
120
    p_dec->fmt_out.video.i_height = p_context->height;
 
121
 
 
122
    if( !p_context->width || !p_context->height )
 
123
    {
 
124
        return NULL; /* invalid display size */
 
125
    }
 
126
 
 
127
    if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS )
 
128
    {
 
129
        /* we are doomed, but not really, because most codecs set their pix_fmt much later */
 
130
        p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
 
131
    }
 
132
    p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
 
133
 
 
134
    /* If an aspect-ratio was specified in the input format then force it */
 
135
    if( p_dec->fmt_in.video.i_aspect )
 
136
    {
 
137
        p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
 
138
    }
 
139
    else
 
140
    {
 
141
        p_dec->fmt_out.video.i_aspect =
 
142
            VOUT_ASPECT_FACTOR * ( av_q2d(p_context->sample_aspect_ratio) *
 
143
                p_context->width / p_context->height );
 
144
        p_dec->fmt_out.video.i_sar_num = p_context->sample_aspect_ratio.num;
 
145
        p_dec->fmt_out.video.i_sar_den = p_context->sample_aspect_ratio.den;
 
146
 
 
147
        if( p_dec->fmt_out.video.i_aspect == 0 )
 
148
        {
 
149
            p_dec->fmt_out.video.i_aspect =
 
150
                VOUT_ASPECT_FACTOR * p_context->width / p_context->height;
 
151
        }
 
152
    }
 
153
 
 
154
    if( p_dec->fmt_out.video.i_frame_rate > 0 &&
 
155
        p_dec->fmt_out.video.i_frame_rate_base > 0 )
 
156
    {
 
157
        p_dec->fmt_out.video.i_frame_rate =
 
158
            p_dec->fmt_in.video.i_frame_rate;
 
159
        p_dec->fmt_out.video.i_frame_rate_base =
 
160
            p_dec->fmt_in.video.i_frame_rate_base;
 
161
    }
 
162
    else if( p_context->time_base.num > 0 && p_context->time_base.den > 0 )
 
163
    {
 
164
        p_dec->fmt_out.video.i_frame_rate = p_context->time_base.den;
 
165
        p_dec->fmt_out.video.i_frame_rate_base = p_context->time_base.num;
 
166
    }
 
167
 
 
168
    p_pic = p_dec->pf_vout_buffer_new( p_dec );
 
169
 
 
170
    return p_pic;
 
171
}
 
172
 
 
173
/*****************************************************************************
 
174
 * InitVideo: initialize the video decoder
 
175
 *****************************************************************************
 
176
 * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
 
177
 * opened (done after the first decoded frame).
 
178
 *****************************************************************************/
 
179
int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
 
180
                      AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
 
181
{
 
182
    decoder_sys_t *p_sys;
 
183
    vlc_value_t val;
 
184
 
 
185
    /* Allocate the memory needed to store the decoder's structure */
 
186
    if( ( p_dec->p_sys = p_sys =
 
187
          (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
 
188
    {
 
189
        return VLC_ENOMEM;
 
190
    }
 
191
    memset( p_sys, 0, sizeof(decoder_sys_t) );
 
192
 
 
193
    p_dec->p_sys->p_context = p_context;
 
194
    p_dec->p_sys->p_codec = p_codec;
 
195
    p_dec->p_sys->i_codec_id = i_codec_id;
 
196
    p_dec->p_sys->psz_namecodec = psz_namecodec;
 
197
    p_sys->p_ff_pic = avcodec_alloc_frame();
 
198
 
 
199
    /* ***** Fill p_context with init values ***** */
 
200
    p_sys->p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_codec );
 
201
    p_sys->p_context->width  = p_dec->fmt_in.video.i_width;
 
202
    p_sys->p_context->height = p_dec->fmt_in.video.i_height;
 
203
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
 
204
    p_sys->p_context->bits_per_sample = p_dec->fmt_in.video.i_bits_per_pixel;
 
205
#else
 
206
    p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
 
207
#endif
 
208
 
 
209
    /*  ***** Get configuration of ffmpeg plugin ***** */
 
210
    p_sys->p_context->workaround_bugs =
 
211
        config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
 
212
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
 
213
    p_sys->p_context->error_resilience =
 
214
        config_GetInt( p_dec, "ffmpeg-error-resilience" );
 
215
#else
 
216
    p_sys->p_context->error_recognition =
 
217
        config_GetInt( p_dec, "ffmpeg-error-resilience" );
 
218
#endif
 
219
 
 
220
    var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
 
221
    var_Get( p_dec, "grayscale", &val );
 
222
    if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
 
223
 
 
224
    var_Create( p_dec, "ffmpeg-vismv", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
 
225
    var_Get( p_dec, "ffmpeg-vismv", &val );
 
226
    if( val.i_int ) p_sys->p_context->debug_mv = val.i_int;
 
227
 
 
228
    var_Create( p_dec, "ffmpeg-lowres", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
 
229
    var_Get( p_dec, "ffmpeg-lowres", &val );
 
230
    if( val.i_int > 0 && val.i_int <= 2 ) p_sys->p_context->lowres = val.i_int;
 
231
 
 
232
    var_Create( p_dec, "ffmpeg-skiploopfilter",
 
233
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
 
234
    var_Get( p_dec, "ffmpeg-skiploopfilter", &val );
 
235
    if( val.i_int > 0 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONREF;
 
236
    if( val.i_int > 1 ) p_sys->p_context->skip_loop_filter = AVDISCARD_BIDIR;
 
237
    if( val.i_int > 2 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONKEY;
 
238
    if( val.i_int > 3 ) p_sys->p_context->skip_loop_filter = AVDISCARD_ALL;
 
239
 
 
240
    /* ***** ffmpeg frame skipping ***** */
 
241
    var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
 
242
    var_Get( p_dec, "ffmpeg-hurry-up", &val );
 
243
    p_sys->b_hurry_up = val.b_bool;
 
244
 
 
245
    var_Create( p_dec, "ffmpeg-skip-frame", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
 
246
    var_Get( p_dec, "ffmpeg-skip-frame", &val );
 
247
    switch( val.i_int )
 
248
    {
 
249
        case -1:
 
250
            p_sys->p_context->skip_frame = AVDISCARD_NONE;
 
251
            break;
 
252
        case 0:
 
253
            p_sys->p_context->skip_frame = AVDISCARD_DEFAULT;
 
254
            break;
 
255
        case 1:
 
256
            p_sys->p_context->skip_frame = AVDISCARD_BIDIR;
 
257
            break;
 
258
        case 2:
 
259
            p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
 
260
            break;
 
261
        case 3:
 
262
            p_sys->p_context->skip_frame = AVDISCARD_ALL;
 
263
            break;
 
264
        default:
 
265
            p_sys->p_context->skip_frame = AVDISCARD_NONE;
 
266
            break;
 
267
    }
 
268
    p_sys->i_skip_frame = p_sys->p_context->skip_frame;
 
269
 
 
270
    var_Create( p_dec, "ffmpeg-skip-idct",  VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
 
271
    var_Get( p_dec, "ffmpeg-skip-idct", &val );
 
272
    switch( val.i_int )
 
273
    {
 
274
        case -1:
 
275
            p_sys->p_context->skip_idct = AVDISCARD_NONE;
 
276
            break;
 
277
        case 0:
 
278
            p_sys->p_context->skip_idct = AVDISCARD_DEFAULT;
 
279
            break;
 
280
        case 1:
 
281
            p_sys->p_context->skip_idct = AVDISCARD_BIDIR;
 
282
            break;
 
283
        case 2:
 
284
            p_sys->p_context->skip_idct = AVDISCARD_NONKEY;
 
285
            break;
 
286
        case 3:
 
287
            p_sys->p_context->skip_idct = AVDISCARD_ALL;
 
288
            break;
 
289
        default:
 
290
            p_sys->p_context->skip_idct = AVDISCARD_NONE;
 
291
            break;
 
292
    }
 
293
    p_sys->i_skip_idct = p_sys->p_context->skip_idct;
 
294
 
 
295
    /* ***** ffmpeg direct rendering ***** */
 
296
    p_sys->b_direct_rendering = 0;
 
297
    var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
 
298
    var_Get( p_dec, "ffmpeg-dr", &val );
 
299
    if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
 
300
        /* Apparently direct rendering doesn't work with YUV422P */
 
301
        p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
 
302
        /* H264 uses too many reference frames */
 
303
        p_sys->i_codec_id != CODEC_ID_H264 &&
 
304
        !p_sys->p_context->debug_mv )
 
305
    {
 
306
        /* Some codecs set pix_fmt only after the 1st frame has been decoded,
 
307
         * so we need to do another check in ffmpeg_GetFrameBuf() */
 
308
        p_sys->b_direct_rendering = 1;
 
309
    }
 
310
 
 
311
    /* ffmpeg doesn't properly release old pictures when frames are skipped */
 
312
    //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
 
313
    if( p_sys->b_direct_rendering )
 
314
    {
 
315
        msg_Dbg( p_dec, "using direct rendering" );
 
316
        p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
 
317
    }
 
318
 
 
319
    /* Always use our get_buffer wrapper so we can calculate the
 
320
     * PTS correctly */
 
321
    p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
 
322
    p_sys->p_context->reget_buffer = ffmpeg_ReGetFrameBuf;
 
323
    p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
 
324
    p_sys->p_context->opaque = p_dec;
 
325
 
 
326
    /* ***** init this codec with special data ***** */
 
327
    ffmpeg_InitCodec( p_dec );
 
328
 
 
329
    /* ***** misc init ***** */
 
330
    p_sys->input_pts = p_sys->input_dts = 0;
 
331
    p_sys->i_pts = 0;
 
332
    p_sys->b_has_b_frames = false;
 
333
    p_sys->b_first_frame = true;
 
334
    p_sys->b_flush = false;
 
335
    p_sys->i_late_frames = 0;
 
336
    p_sys->i_buffer = 0;
 
337
    p_sys->i_buffer_orig = 1;
 
338
    p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer_orig );
 
339
    if( !p_sys->p_buffer_orig )
 
340
    {
 
341
        free( p_sys );
 
342
        return VLC_ENOMEM;
 
343
    }
 
344
 
 
345
    /* Set output properties */
 
346
    p_dec->fmt_out.i_cat = VIDEO_ES;
 
347
    if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS )
 
348
    {
 
349
        /* we are doomed. but not really, because most codecs set their pix_fmt later on */
 
350
        p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
 
351
    }
 
352
    p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
 
353
 
 
354
    /* Setup palette */
 
355
    if( p_dec->fmt_in.video.p_palette )
 
356
        p_sys->p_context->palctrl =
 
357
            (AVPaletteControl *)p_dec->fmt_in.video.p_palette;
 
358
    else if( p_sys->i_codec_id != CODEC_ID_MSVIDEO1 && p_sys->i_codec_id != CODEC_ID_CINEPAK )
 
359
        p_sys->p_context->palctrl = (AVPaletteControl *) &palette_control;
 
360
 
 
361
    /* ***** Open the codec ***** */
 
362
    vlc_mutex_t *lock = var_AcquireMutex( "avcodec" );
 
363
    if( lock == NULL )
 
364
    {
 
365
        free( p_sys->p_buffer_orig );
 
366
        free( p_sys );
 
367
        return VLC_ENOMEM;
 
368
    }
 
369
 
 
370
    if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
 
371
    {
 
372
        vlc_mutex_unlock( lock );
 
373
        msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
 
374
        free( p_sys->p_buffer_orig );
 
375
        free( p_sys );
 
376
        return VLC_EGENERIC;
 
377
    }
 
378
    vlc_mutex_unlock( lock );
 
379
    msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
 
380
 
 
381
 
 
382
    return VLC_SUCCESS;
 
383
}
 
384
 
 
385
/*****************************************************************************
 
386
 * DecodeVideo: Called to decode one or more frames
 
387
 *****************************************************************************/
 
388
picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
 
389
{
 
390
    decoder_sys_t *p_sys = p_dec->p_sys;
 
391
    int b_drawpicture;
 
392
    int b_null_size = false;
 
393
    block_t *p_block;
 
394
 
 
395
    if( !pp_block || !*pp_block ) return NULL;
 
396
 
 
397
    if( !p_sys->p_context->extradata_size && p_dec->fmt_in.i_extra )
 
398
        ffmpeg_InitCodec( p_dec );
 
399
 
 
400
    p_block = *pp_block;
 
401
 
 
402
    if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
 
403
    {
 
404
        p_sys->i_buffer = 0;
 
405
        p_sys->i_pts = 0; /* To make sure we recover properly */
 
406
 
 
407
        p_sys->input_pts = p_sys->input_dts = 0;
 
408
        p_sys->i_late_frames = 0;
 
409
 
 
410
        block_Release( p_block );
 
411
 
 
412
        //if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
 
413
            //avcodec_flush_buffers( p_sys->p_context );
 
414
        return NULL;
 
415
    }
 
416
 
 
417
    if( p_block->i_flags & BLOCK_FLAG_PREROLL )
 
418
    {
 
419
        /* Do not care about late frames when prerolling
 
420
         * TODO avoid decoding of non reference frame
 
421
         * (ie all B except for H264 where it depends only on nal_ref_idc) */
 
422
        p_sys->i_late_frames = 0;
 
423
    }
 
424
 
 
425
    if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
 
426
        (mdate() - p_sys->i_late_frames_start > INT64_C(5000000)) )
 
427
    {
 
428
        if( p_sys->i_pts )
 
429
        {
 
430
            msg_Err( p_dec, "more than 5 seconds of late video -> "
 
431
                     "dropping frame (computer too slow ?)" );
 
432
            p_sys->i_pts = 0; /* To make sure we recover properly */
 
433
        }
 
434
        block_Release( p_block );
 
435
        p_sys->i_late_frames--;
 
436
        return NULL;
 
437
    }
 
438
 
 
439
    if( p_block->i_pts > 0 || p_block->i_dts > 0 )
 
440
    {
 
441
        p_sys->input_pts = p_block->i_pts;
 
442
        p_sys->input_dts = p_block->i_dts;
 
443
 
 
444
        /* Make sure we don't reuse the same timestamps twice */
 
445
        p_block->i_pts = p_block->i_dts = 0;
 
446
    }
 
447
 
 
448
    /* A good idea could be to decode all I pictures and see for the other */
 
449
    if( !p_dec->b_pace_control &&
 
450
        p_sys->b_hurry_up &&
 
451
        (p_sys->i_late_frames > 4) )
 
452
    {
 
453
        b_drawpicture = 0;
 
454
        if( p_sys->i_late_frames < 12 )
 
455
        {
 
456
            p_sys->p_context->skip_frame =
 
457
                    (p_sys->i_skip_frame <= AVDISCARD_BIDIR) ?
 
458
                    AVDISCARD_BIDIR : p_sys->i_skip_frame;
 
459
        }
 
460
        else
 
461
        {
 
462
            /* picture too late, won't decode
 
463
             * but break picture until a new I, and for mpeg4 ...*/
 
464
            p_sys->i_late_frames--; /* needed else it will never be decrease */
 
465
            block_Release( p_block );
 
466
            p_sys->i_buffer = 0;
 
467
            return NULL;
 
468
        }
 
469
    }
 
470
    else
 
471
    {
 
472
        if( p_sys->b_hurry_up )
 
473
            p_sys->p_context->skip_frame = p_sys->i_skip_frame;
 
474
        if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
 
475
            b_drawpicture = 1;
 
476
        else
 
477
            b_drawpicture = 0;
 
478
    }
 
479
 
 
480
    if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
 
481
    {
 
482
        if( p_sys->b_hurry_up )
 
483
            p_sys->p_context->skip_frame = p_sys->i_skip_frame;
 
484
        b_null_size = true;
 
485
    }
 
486
 
 
487
    /*
 
488
     * Do the actual decoding now
 
489
     */
 
490
 
 
491
    /* Don't forget that ffmpeg requires a little more bytes
 
492
     * that the real frame size */
 
493
    if( p_block->i_buffer > 0 )
 
494
    {
 
495
        p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;
 
496
 
 
497
        p_sys->i_buffer = p_block->i_buffer;
 
498
        if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
 
499
            p_sys->i_buffer_orig )
 
500
        {
 
501
            free( p_sys->p_buffer_orig );
 
502
            p_sys->i_buffer_orig =
 
503
                p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
 
504
            p_sys->p_buffer_orig = malloc( p_sys->i_buffer_orig );
 
505
        }
 
506
        p_sys->p_buffer = p_sys->p_buffer_orig;
 
507
        p_sys->i_buffer = p_block->i_buffer;
 
508
        if( !p_sys->p_buffer )
 
509
        {
 
510
            block_Release( p_block );
 
511
            return NULL;
 
512
        }
 
513
        vlc_memcpy( p_sys->p_buffer, p_block->p_buffer, p_block->i_buffer );
 
514
        memset( p_sys->p_buffer + p_block->i_buffer, 0,
 
515
                FF_INPUT_BUFFER_PADDING_SIZE );
 
516
 
 
517
        p_block->i_buffer = 0;
 
518
    }
 
519
 
 
520
    while( p_sys->i_buffer > 0 || p_sys->b_flush )
 
521
    {
 
522
        int i_used, b_gotpicture;
 
523
        picture_t *p_pic;
 
524
 
 
525
        i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
 
526
                                       &b_gotpicture,
 
527
                                       p_sys->i_buffer <= 0 && p_sys->b_flush ? NULL : (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
 
528
 
 
529
        if( b_null_size && p_sys->p_context->width > 0 &&
 
530
            p_sys->p_context->height > 0 &&
 
531
            !p_sys->b_flush )
 
532
        {
 
533
            /* Reparse it to not drop the I frame */
 
534
            b_null_size = false;
 
535
            if( p_sys->b_hurry_up )
 
536
                p_sys->p_context->skip_frame = p_sys->i_skip_frame;
 
537
            i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
 
538
                                           &b_gotpicture,
 
539
                                           (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
 
540
        }
 
541
 
 
542
        if( p_sys->b_flush )
 
543
            p_sys->b_first_frame = true;
 
544
 
 
545
        if( p_sys->i_buffer <= 0 )
 
546
            p_sys->b_flush = false;
 
547
 
 
548
        if( i_used < 0 )
 
549
        {
 
550
            msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
 
551
                      p_sys->i_buffer );
 
552
            block_Release( p_block );
 
553
            return NULL;
 
554
        }
 
555
        else if( i_used > p_sys->i_buffer )
 
556
        {
 
557
            i_used = p_sys->i_buffer;
 
558
        }
 
559
 
 
560
        /* Consumed bytes */
 
561
        p_sys->i_buffer -= i_used;
 
562
        p_sys->p_buffer += i_used;
 
563
 
 
564
        /* Nothing to display */
 
565
        if( !b_gotpicture )
 
566
        {
 
567
            if( i_used == 0 ) break;
 
568
            continue;
 
569
        }
 
570
 
 
571
        /* Set the PTS */
 
572
        if( p_sys->p_ff_pic->pts )
 
573
            p_sys->i_pts = p_sys->p_ff_pic->pts;
 
574
 
 
575
        /* Update frame late count (except when doing preroll) */
 
576
        if( p_sys->i_pts && decoder_GetDisplayDate(p_dec, p_sys->i_pts) <= mdate() &&
 
577
            !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
 
578
        {
 
579
            p_sys->i_late_frames++;
 
580
            if( p_sys->i_late_frames == 1 )
 
581
                p_sys->i_late_frames_start = mdate();
 
582
        }
 
583
        else
 
584
        {
 
585
            p_sys->i_late_frames = 0;
 
586
        }
 
587
 
 
588
        if( !b_drawpicture || !p_sys->p_ff_pic->linesize[0] )
 
589
        {
 
590
            /* Do not display the picture */
 
591
            p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
 
592
            if( !b_drawpicture && p_pic )
 
593
                p_dec->pf_vout_buffer_del( p_dec, p_pic );
 
594
 
 
595
            ffmpeg_NextPts( p_dec, p_block->i_rate );
 
596
            continue;
 
597
        }
 
598
 
 
599
        if( !p_sys->p_ff_pic->opaque )
 
600
        {
 
601
            /* Get a new picture */
 
602
            p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
 
603
            if( !p_pic )
 
604
            {
 
605
                block_Release( p_block );
 
606
                return NULL;
 
607
            }
 
608
 
 
609
            /* Fill p_picture_t from AVVideoFrame and do chroma conversion
 
610
             * if needed */
 
611
            ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
 
612
        }
 
613
        else
 
614
        {
 
615
            p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
 
616
        }
 
617
 
 
618
        /* Sanity check (seems to be needed for some streams) */
 
619
        if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
 
620
        {
 
621
            p_sys->b_has_b_frames = true;
 
622
        }
 
623
 
 
624
        if( !p_dec->fmt_in.video.i_aspect )
 
625
        {
 
626
            /* Fetch again the aspect ratio in case it changed */
 
627
            p_dec->fmt_out.video.i_aspect =
 
628
                VOUT_ASPECT_FACTOR
 
629
                    * ( av_q2d(p_sys->p_context->sample_aspect_ratio)
 
630
                    * p_sys->p_context->width / p_sys->p_context->height );
 
631
            p_dec->fmt_out.video.i_sar_num
 
632
                = p_sys->p_context->sample_aspect_ratio.num;
 
633
            p_dec->fmt_out.video.i_sar_den
 
634
                = p_sys->p_context->sample_aspect_ratio.den;
 
635
 
 
636
            if( p_dec->fmt_out.video.i_aspect == 0 )
 
637
            {
 
638
                p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR
 
639
                    * p_sys->p_context->width / p_sys->p_context->height;
 
640
            }
 
641
        }
 
642
 
 
643
        /* Send decoded frame to vout */
 
644
        if( p_sys->i_pts )
 
645
        {
 
646
            p_pic->date = p_sys->i_pts;
 
647
 
 
648
            ffmpeg_NextPts( p_dec, p_block->i_rate );
 
649
 
 
650
            if( p_sys->b_first_frame )
 
651
            {
 
652
                /* Hack to force display of still pictures */
 
653
                p_sys->b_first_frame = false;
 
654
                p_pic->b_force = true;
 
655
            }
 
656
 
 
657
            p_pic->i_nb_fields = 2 + p_sys->p_ff_pic->repeat_pict;
 
658
            p_pic->b_progressive = !p_sys->p_ff_pic->interlaced_frame;
 
659
            p_pic->b_top_field_first = p_sys->p_ff_pic->top_field_first;
 
660
 
 
661
            return p_pic;
 
662
        }
 
663
        else
 
664
        {
 
665
            p_dec->pf_vout_buffer_del( p_dec, p_pic );
 
666
        }
 
667
    }
 
668
 
 
669
    block_Release( p_block );
 
670
    return NULL;
 
671
}
 
672
 
 
673
/*****************************************************************************
 
674
 * EndVideo: decoder destruction
 
675
 *****************************************************************************
 
676
 * This function is called when the thread ends after a successful
 
677
 * initialization.
 
678
 *****************************************************************************/
 
679
void EndVideoDec( decoder_t *p_dec )
 
680
{
 
681
    decoder_sys_t *p_sys = p_dec->p_sys;
 
682
 
 
683
    if( p_sys->p_ff_pic ) av_free( p_sys->p_ff_pic );
 
684
    free( p_sys->p_buffer_orig );
 
685
}
 
686
 
 
687
/*****************************************************************************
 
688
 * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
 
689
 *****************************************************************************/
 
690
static void ffmpeg_InitCodec( decoder_t *p_dec )
 
691
{
 
692
    decoder_sys_t *p_sys = p_dec->p_sys;
 
693
    int i_size = p_dec->fmt_in.i_extra;
 
694
 
 
695
    if( !i_size ) return;
 
696
 
 
697
    if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
 
698
    {
 
699
        uint8_t *p;
 
700
 
 
701
        p_sys->p_context->extradata_size = i_size + 12;
 
702
        p = p_sys->p_context->extradata  =
 
703
            malloc( p_sys->p_context->extradata_size );
 
704
        if( !p )
 
705
            return;
 
706
 
 
707
        memcpy( &p[0],  "SVQ3", 4 );
 
708
        memset( &p[4], 0, 8 );
 
709
        memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
 
710
 
 
711
        /* Now remove all atoms before the SMI one */
 
712
        if( p_sys->p_context->extradata_size > 0x5a &&
 
713
            strncmp( (char*)&p[0x56], "SMI ", 4 ) )
 
714
        {
 
715
            uint8_t *psz = &p[0x52];
 
716
 
 
717
            while( psz < &p[p_sys->p_context->extradata_size - 8] )
 
718
            {
 
719
                int i_size = GetDWBE( psz );
 
720
                if( i_size <= 1 )
 
721
                {
 
722
                    /* FIXME handle 1 as long size */
 
723
                    break;
 
724
                }
 
725
                if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
 
726
                {
 
727
                    memmove( &p[0x52], psz,
 
728
                             &p[p_sys->p_context->extradata_size] - psz );
 
729
                    break;
 
730
                }
 
731
 
 
732
                psz += i_size;
 
733
            }
 
734
        }
 
735
    }
 
736
    else if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '0' ) ||
 
737
             p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '3' ) ||
 
738
             p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '2', '0' ) )
 
739
    {
 
740
        if( p_dec->fmt_in.i_extra == 8 )
 
741
        {
 
742
            p_sys->p_context->extradata_size = 8;
 
743
            p_sys->p_context->extradata = malloc( 8 );
 
744
            if( p_sys->p_context->extradata )
 
745
            {
 
746
                memcpy( p_sys->p_context->extradata,
 
747
                        p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
 
748
                p_sys->p_context->sub_id = ((uint32_t*)p_dec->fmt_in.p_extra)[1];
 
749
 
 
750
                msg_Warn( p_dec, "using extra data for RV codec sub_id=%08x",
 
751
                          p_sys->p_context->sub_id );
 
752
            }
 
753
        }
 
754
    }
 
755
    else
 
756
    {
 
757
        p_sys->p_context->extradata_size = i_size;
 
758
        p_sys->p_context->extradata =
 
759
            malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
 
760
        if( p_sys->p_context->extradata )
 
761
        {
 
762
            memcpy( p_sys->p_context->extradata,
 
763
                    p_dec->fmt_in.p_extra, i_size );
 
764
            memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
 
765
                    0, FF_INPUT_BUFFER_PADDING_SIZE );
 
766
        }
 
767
    }
 
768
}
 
769
 
 
770
/*****************************************************************************
 
771
 * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
 
772
 *                     picture_t structure (when not in direct rendering mode).
 
773
 *****************************************************************************/
 
774
static void ffmpeg_CopyPicture( decoder_t *p_dec,
 
775
                                picture_t *p_pic, AVFrame *p_ff_pic )
 
776
{
 
777
    decoder_sys_t *p_sys = p_dec->p_sys;
 
778
 
 
779
    if( TestFfmpegChroma( p_sys->p_context->pix_fmt, -1 ) == VLC_SUCCESS )
 
780
    {
 
781
        int i_plane, i_size, i_line;
 
782
        uint8_t *p_dst, *p_src;
 
783
        int i_src_stride, i_dst_stride;
 
784
 
 
785
        for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
 
786
        {
 
787
            p_src  = p_ff_pic->data[i_plane];
 
788
            p_dst = p_pic->p[i_plane].p_pixels;
 
789
            i_src_stride = p_ff_pic->linesize[i_plane];
 
790
            i_dst_stride = p_pic->p[i_plane].i_pitch;
 
791
 
 
792
            i_size = __MIN( i_src_stride, i_dst_stride );
 
793
            for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
 
794
                 i_line++ )
 
795
            {
 
796
                vlc_memcpy( p_dst, p_src, i_size );
 
797
                p_src += i_src_stride;
 
798
                p_dst += i_dst_stride;
 
799
            }
 
800
        }
 
801
    }
 
802
    else
 
803
    {
 
804
        msg_Err( p_dec, "don't know how to convert chroma %i",
 
805
                 p_sys->p_context->pix_fmt );
 
806
        p_dec->b_error = 1;
 
807
    }
 
808
}
 
809
 
 
810
/*****************************************************************************
 
811
 * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
 
812
 *****************************************************************************
 
813
 * It is used for direct rendering as well as to get the right PTS for each
 
814
 * decoded picture (even in indirect rendering mode).
 
815
 *****************************************************************************/
 
816
static void ffmpeg_SetFrameBufferPts( decoder_t *p_dec, AVFrame *p_ff_pic );
 
817
 
 
818
static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
 
819
                               AVFrame *p_ff_pic )
 
820
{
 
821
    decoder_t *p_dec = (decoder_t *)p_context->opaque;
 
822
    decoder_sys_t *p_sys = p_dec->p_sys;
 
823
    picture_t *p_pic;
 
824
 
 
825
    /* Set picture PTS */
 
826
    ffmpeg_SetFrameBufferPts( p_dec, p_ff_pic );
 
827
 
 
828
    /* */
 
829
    p_ff_pic->opaque = 0;
 
830
 
 
831
    /* Not much to do in indirect rendering mode */
 
832
    if( !p_sys->b_direct_rendering )
 
833
    {
 
834
        return avcodec_default_get_buffer( p_context, p_ff_pic );
 
835
    }
 
836
 
 
837
    /* Some codecs set pix_fmt only after the 1st frame has been decoded,
 
838
     * so this check is necessary. */
 
839
    if( GetVlcChroma( &p_dec->fmt_out.video, p_context->pix_fmt ) != VLC_SUCCESS ||
 
840
        p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
 
841
    {
 
842
        msg_Dbg( p_dec, "disabling direct rendering" );
 
843
        p_sys->b_direct_rendering = 0;
 
844
        return avcodec_default_get_buffer( p_context, p_ff_pic );
 
845
    }
 
846
    p_dec->fmt_out.i_codec = p_dec->fmt_out.video.i_chroma;
 
847
 
 
848
    /* Get a new picture */
 
849
    //p_sys->p_vout->render.b_allow_modify_pics = 0;
 
850
    p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
 
851
    if( !p_pic )
 
852
    {
 
853
        p_sys->b_direct_rendering = 0;
 
854
        return avcodec_default_get_buffer( p_context, p_ff_pic );
 
855
    }
 
856
    p_sys->p_context->draw_horiz_band = NULL;
 
857
 
 
858
    p_ff_pic->opaque = (void*)p_pic;
 
859
    p_ff_pic->type = FF_BUFFER_TYPE_USER;
 
860
    p_ff_pic->data[0] = p_pic->p[0].p_pixels;
 
861
    p_ff_pic->data[1] = p_pic->p[1].p_pixels;
 
862
    p_ff_pic->data[2] = p_pic->p[2].p_pixels;
 
863
    p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
 
864
 
 
865
    p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
 
866
    p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
 
867
    p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
 
868
    p_ff_pic->linesize[3] = 0;
 
869
 
 
870
    if( p_ff_pic->reference != 0 )
 
871
    {
 
872
        p_dec->pf_picture_link( p_dec, p_pic );
 
873
    }
 
874
 
 
875
    /* FIXME what is that, should give good value */
 
876
    p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
 
877
 
 
878
    return 0;
 
879
}
 
880
static int  ffmpeg_ReGetFrameBuf( struct AVCodecContext *p_context, AVFrame *p_ff_pic )
 
881
{
 
882
    decoder_t *p_dec = (decoder_t *)p_context->opaque;
 
883
    int i_ret;
 
884
 
 
885
    /* */
 
886
    p_ff_pic->pts = AV_NOPTS_VALUE;
 
887
 
 
888
    /* We always use default reget function, it works perfectly fine */
 
889
    i_ret = avcodec_default_reget_buffer( p_context, p_ff_pic );
 
890
 
 
891
    /* Set picture PTS if avcodec_default_reget_buffer didn't set it (through a
 
892
     * ffmpeg_GetFrameBuf call) */
 
893
    if( !i_ret && p_ff_pic->pts == AV_NOPTS_VALUE )
 
894
        ffmpeg_SetFrameBufferPts( p_dec, p_ff_pic );
 
895
 
 
896
    return i_ret;
 
897
}
 
898
 
 
899
static void ffmpeg_SetFrameBufferPts( decoder_t *p_dec, AVFrame *p_ff_pic )
 
900
{
 
901
    decoder_sys_t *p_sys = p_dec->p_sys;
 
902
 
 
903
    /* Set picture PTS */
 
904
    if( p_sys->input_pts )
 
905
    {
 
906
        p_ff_pic->pts = p_sys->input_pts;
 
907
    }
 
908
    else if( p_sys->input_dts )
 
909
    {
 
910
        /* Some demuxers only set the dts so let's try to find a useful
 
911
         * timestamp from this */
 
912
        if( !p_sys->p_context->has_b_frames || !p_sys->b_has_b_frames ||
 
913
            !p_ff_pic->reference || !p_sys->i_pts )
 
914
        {
 
915
            p_ff_pic->pts = p_sys->input_dts;
 
916
        }
 
917
        else
 
918
        {
 
919
            p_ff_pic->pts = 0;
 
920
        }
 
921
    }
 
922
    else
 
923
    {
 
924
        p_ff_pic->pts = 0;
 
925
    }
 
926
 
 
927
    if( p_sys->i_pts ) /* make sure 1st frame has a pts > 0 */
 
928
    {
 
929
        p_sys->input_pts = p_sys->input_dts = 0;
 
930
    }
 
931
}
 
932
 
 
933
static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
 
934
                                    AVFrame *p_ff_pic )
 
935
{
 
936
    decoder_t *p_dec = (decoder_t *)p_context->opaque;
 
937
    picture_t *p_pic;
 
938
 
 
939
    if( !p_ff_pic->opaque )
 
940
    {
 
941
        avcodec_default_release_buffer( p_context, p_ff_pic );
 
942
        return;
 
943
    }
 
944
 
 
945
    p_pic = (picture_t*)p_ff_pic->opaque;
 
946
 
 
947
    p_ff_pic->data[0] = NULL;
 
948
    p_ff_pic->data[1] = NULL;
 
949
    p_ff_pic->data[2] = NULL;
 
950
    p_ff_pic->data[3] = NULL;
 
951
 
 
952
    if( p_ff_pic->reference != 0 )
 
953
    {
 
954
        p_dec->pf_picture_unlink( p_dec, p_pic );
 
955
    }
 
956
}
 
957
 
 
958
static void ffmpeg_NextPts( decoder_t *p_dec, int i_block_rate )
 
959
{
 
960
    decoder_sys_t *p_sys = p_dec->p_sys;
 
961
 
 
962
    if( p_sys->i_pts <= 0 )
 
963
        return;
 
964
 
 
965
    /* interpolate the next PTS */
 
966
    if( p_dec->fmt_in.video.i_frame_rate > 0 &&
 
967
        p_dec->fmt_in.video.i_frame_rate_base > 0 )
 
968
    {
 
969
        p_sys->i_pts += INT64_C(1000000) *
 
970
            (2 + p_sys->p_ff_pic->repeat_pict) *
 
971
            p_dec->fmt_in.video.i_frame_rate_base *
 
972
            i_block_rate / INPUT_RATE_DEFAULT /
 
973
            (2 * p_dec->fmt_in.video.i_frame_rate);
 
974
    }
 
975
    else if( p_sys->p_context->time_base.den > 0 )
 
976
    {
 
977
        p_sys->i_pts += INT64_C(1000000) *
 
978
            (2 + p_sys->p_ff_pic->repeat_pict) *
 
979
            p_sys->p_context->time_base.num *
 
980
            i_block_rate / INPUT_RATE_DEFAULT /
 
981
            (2 * p_sys->p_context->time_base.den);
 
982
    }
 
983
}