libmpeg2.c 22.3 KB
Newer Older
gbazin's avatar
 
gbazin committed
1 2 3 4
/*****************************************************************************
 * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
 *****************************************************************************
 * Copyright (C) 1999-2001 VideoLAN
5
 * $Id: libmpeg2.c,v 1.27 2003/09/03 10:23:17 gbazin Exp $
gbazin's avatar
 
gbazin committed
6 7
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
gbazin's avatar
 
gbazin committed
8
 *          Christophe Massiot <massiot@via.ecp.fr>
gbazin's avatar
 
gbazin committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * 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.
 * 
 * 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/vout.h>
#include <vlc/input.h>
#include <vlc/decoder.h>

#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>                                    /* memcpy(), memset() */

#include <mpeg2dec/mpeg2.h>

38 39
#include "vout_synchro.h"

gbazin's avatar
 
gbazin committed
40 41 42 43 44 45 46
/* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
#define AR_SQUARE_PICTURE       1                           /* square pixels */
#define AR_3_4_PICTURE          2                        /* 3:4 picture (TV) */
#define AR_16_9_PICTURE         3              /* 16:9 picture (wide screen) */
#define AR_221_1_PICTURE        4                  /* 2.21:1 picture (movie) */

/*****************************************************************************
gbazin's avatar
 
gbazin committed
47
 * decoder_sys_t : libmpeg2 decoder descriptor
gbazin's avatar
 
gbazin committed
48
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
49
struct decoder_sys_t
gbazin's avatar
 
gbazin committed
50 51 52 53 54 55
{
    /*
     * libmpeg2 properties
     */
    mpeg2dec_t          *p_mpeg2dec;
    const mpeg2_info_t  *p_info;
56
    vlc_bool_t          b_skip;
gbazin's avatar
 
gbazin committed
57 58 59 60 61 62 63 64 65

    /*
     * Input properties
     */
    pes_packet_t     *p_pes;                  /* current PES we are decoding */
    mtime_t          i_pts;
    mtime_t          i_previous_pts;
    mtime_t          i_current_pts;
    mtime_t          i_period_remainder;
66 67
    int              i_current_rate;
    picture_t *      p_picture_to_destroy;
68 69 70 71
    vlc_bool_t       b_garbage_pic;
    vlc_bool_t       b_after_sequence_header; /* is it the next frame after
                                               * the sequence header ?    */
    vlc_bool_t       b_slice_i;             /* intra-slice refresh stream */
gbazin's avatar
 
gbazin committed
72 73 74 75 76

    /*
     * Output properties
     */
    vout_thread_t *p_vout;
77
    vout_synchro_t *p_synchro;
gbazin's avatar
 
gbazin committed
78

gbazin's avatar
 
gbazin committed
79
};
gbazin's avatar
 
gbazin committed
80 81 82 83 84

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  OpenDecoder  ( vlc_object_t * );
gbazin's avatar
 
gbazin committed
85 86 87
static int  InitDecoder  ( decoder_t * );
static int  RunDecoder   ( decoder_t *, block_t * );
static int  EndDecoder   ( decoder_t * );
gbazin's avatar
 
gbazin committed
88

gbazin's avatar
 
gbazin committed
89
static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
gbazin's avatar
 
gbazin committed
90

gbazin's avatar
 
gbazin committed
91 92 93 94 95
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    set_description( _("MPEG I/II video decoder (using libmpeg2)") );
gbazin's avatar
 
gbazin committed
96
    set_capability( "decoder", 150 );
gbazin's avatar
 
gbazin committed
97 98 99 100 101 102 103 104 105
    set_callbacks( OpenDecoder, NULL );
    add_shortcut( "libmpeg2" );
vlc_module_end();

/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
gbazin's avatar
 
gbazin committed
106
    decoder_t *p_dec = (decoder_t*)p_this;
gbazin's avatar
 
gbazin committed
107

gbazin's avatar
 
gbazin committed
108 109 110
    if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('m','p','g','v') &&
        p_dec->p_fifo->i_fourcc != VLC_FOURCC('m','p','g','1') &&
        p_dec->p_fifo->i_fourcc != VLC_FOURCC('m','p','g','2') )
gbazin's avatar
 
gbazin committed
111 112 113 114
    {
        return VLC_EGENERIC;
    }

gbazin's avatar
 
gbazin committed
115 116 117 118
    p_dec->pf_init = InitDecoder;
    p_dec->pf_decode = RunDecoder;
    p_dec->pf_end = EndDecoder;

gbazin's avatar
 
gbazin committed
119 120
    return VLC_SUCCESS;
}
121

gbazin's avatar
 
gbazin committed
122
/*****************************************************************************
gbazin's avatar
 
gbazin committed
123
 * InitDecoder: Initalize the decoder
gbazin's avatar
 
gbazin committed
124
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
125
static int InitDecoder( decoder_t *p_dec )
gbazin's avatar
 
gbazin committed
126
{
gbazin's avatar
 
gbazin committed
127 128 129
    /* Allocate the memory needed to store the decoder's structure */
    if( ( p_dec->p_sys =
          (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
gbazin's avatar
 
gbazin committed
130
    {
gbazin's avatar
 
gbazin committed
131 132
        msg_Err( p_dec, "out of memory" );
        return VLC_EGENERIC;
gbazin's avatar
 
gbazin committed
133 134 135
    }

    /* Initialize the thread properties */
gbazin's avatar
 
gbazin committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149
    memset( p_dec->p_sys, 0, sizeof(decoder_sys_t) );
    p_dec->p_sys->p_pes      = NULL;
    p_dec->p_sys->p_vout     = NULL;
    p_dec->p_sys->p_mpeg2dec = NULL;
    p_dec->p_sys->p_synchro  = NULL;
    p_dec->p_sys->p_info     = NULL;
    p_dec->p_sys->i_pts      = mdate() + DEFAULT_PTS_DELAY;
    p_dec->p_sys->i_current_pts  = 0;
    p_dec->p_sys->i_previous_pts = 0;
    p_dec->p_sys->i_period_remainder = 0;
    p_dec->p_sys->p_picture_to_destroy = NULL;
    p_dec->p_sys->b_garbage_pic = 0;
    p_dec->p_sys->b_slice_i  = 0;
    p_dec->p_sys->b_skip     = 0;
gbazin's avatar
 
gbazin committed
150 151

    /* Initialize decoder */
gbazin's avatar
 
gbazin committed
152 153 154 155 156 157 158
    p_dec->p_sys->p_mpeg2dec = mpeg2_init();
    if( p_dec->p_sys->p_mpeg2dec == NULL)
    {
        msg_Err( p_dec, "mpeg2_init() failed" );
        free( p_dec->p_sys );
        return VLC_EGENERIC;
    }
gbazin's avatar
 
gbazin committed
159

gbazin's avatar
 
gbazin committed
160 161 162 163
    p_dec->p_sys->p_info = mpeg2_info( p_dec->p_sys->p_mpeg2dec );

    return VLC_SUCCESS;
}
gbazin's avatar
 
gbazin committed
164

gbazin's avatar
 
gbazin committed
165 166 167 168 169 170 171 172 173 174 175 176 177
/*****************************************************************************
 * RunDecoder: the libmpeg2 decoder
 *****************************************************************************/
static int RunDecoder( decoder_t *p_dec, block_t *p_block )
{
    decoder_sys_t   *p_sys = p_dec->p_sys;
    mpeg2_state_t   state;
    picture_t       *p_pic;
    int             i_aspect;

    vlc_bool_t      b_need_more_data = VLC_FALSE;

    while( 1 )
gbazin's avatar
 
gbazin committed
178
    {
179 180 181 182 183 184
        if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
        {
            block_Release( p_block );
            return VLC_EGENERIC;
        }

gbazin's avatar
 
gbazin committed
185
        state = mpeg2_parse( p_sys->p_mpeg2dec );
gbazin's avatar
 
gbazin committed
186 187 188 189

        switch( state )
        {
        case STATE_BUFFER:
gbazin's avatar
 
gbazin committed
190
                if( !p_block->i_buffer || b_need_more_data )
gbazin's avatar
 
gbazin committed
191
                {
gbazin's avatar
 
gbazin committed
192 193
                    block_Release( p_block );
                    return VLC_SUCCESS;
gbazin's avatar
 
gbazin committed
194 195
                }

gbazin's avatar
 
gbazin committed
196 197 198
#if 0
                if( p_pes->b_discontinuity && p_sys->p_synchro 
                     && p_sys->p_info->sequence->width != (unsigned)-1 )
199
                {
gbazin's avatar
 
gbazin committed
200 201 202
                    vout_SynchroReset( p_sys->p_synchro );
                    if ( p_sys->p_info->current_fbuf != NULL
                          && p_sys->p_info->current_fbuf->id != NULL )
203
                    {
gbazin's avatar
 
gbazin committed
204 205
                        p_sys->b_garbage_pic = 1;
                        p_pic = p_sys->p_info->current_fbuf->id;
206 207 208 209 210 211 212
                    }
                    else
                    {
                        uint8_t *buf[3];
                        buf[0] = buf[1] = buf[2] = NULL;
                        if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
                            break;
gbazin's avatar
 
gbazin committed
213
                        mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
214
                    }
gbazin's avatar
 
gbazin committed
215
                    p_sys->p_picture_to_destroy = p_pic;
216 217

                    memset( p_pic->p[0].p_pixels, 0,
gbazin's avatar
 
gbazin committed
218 219
                            p_sys->p_info->sequence->width
                             * p_sys->p_info->sequence->height );
220
                    memset( p_pic->p[1].p_pixels, 0x80,
gbazin's avatar
 
gbazin committed
221 222
                            p_sys->p_info->sequence->width
                             * p_sys->p_info->sequence->height / 4 );
223
                    memset( p_pic->p[2].p_pixels, 0x80,
gbazin's avatar
 
gbazin committed
224 225
                            p_sys->p_info->sequence->width
                             * p_sys->p_info->sequence->height / 4 );
226

gbazin's avatar
 
gbazin committed
227
                    if ( p_sys->b_slice_i )
228
                    {
gbazin's avatar
 
gbazin committed
229 230 231 232
                        vout_SynchroNewPicture( p_sys->p_synchro,
                            I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
                        vout_SynchroDecode( p_sys->p_synchro );
                        vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
233
                    }
234
                }
gbazin's avatar
 
gbazin committed
235
#endif
236

gbazin's avatar
 
gbazin committed
237 238 239 240 241
            if( p_block->i_pts )
            {
                mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
                p_sys->i_previous_pts = p_sys->i_current_pts;
                p_sys->i_current_pts = p_block->i_pts;
gbazin's avatar
 
gbazin committed
242 243
            }

gbazin's avatar
 
gbazin committed
244
            p_sys->i_current_rate = DEFAULT_RATE;//p_pes->i_rate;
gbazin's avatar
 
gbazin committed
245

gbazin's avatar
 
gbazin committed
246 247 248 249
            mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
                          p_block->p_buffer + p_block->i_buffer );

            b_need_more_data = VLC_TRUE;
gbazin's avatar
 
gbazin committed
250 251 252
            break;

        case STATE_SEQUENCE:
gbazin's avatar
 
gbazin committed
253
        {
gbazin's avatar
 
gbazin committed
254
            /* Initialize video output */
gbazin's avatar
 
gbazin committed
255
            uint8_t *buf[3];
256
            buf[0] = buf[1] = buf[2] = NULL;
gbazin's avatar
 
gbazin committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

            /* Check whether the input gives a particular aspect ratio */
            if( p_dec->p_fifo->p_demux_data
                && ( *(int*)(p_dec->p_fifo->p_demux_data) & 0x7 ) )
            {
                i_aspect = *(int*)(p_dec->p_fifo->p_demux_data);
                switch( i_aspect )
                {
                case AR_3_4_PICTURE:
                    i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
                    break;
                case AR_16_9_PICTURE:
                    i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
                    break;
                case AR_221_1_PICTURE:
                    i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
                    break;
                case AR_SQUARE_PICTURE:
                default:
                    i_aspect = VOUT_ASPECT_FACTOR *
gbazin's avatar
 
gbazin committed
277 278
                                   p_sys->p_info->sequence->width /
                                   p_sys->p_info->sequence->height;
gbazin's avatar
 
gbazin committed
279 280 281 282 283 284
                    break;
                }
            }
            else
            {
                /* Use the value provided in the MPEG sequence header */
gbazin's avatar
 
gbazin committed
285 286 287 288
                i_aspect = ((uint64_t)p_sys->p_info->sequence->display_width) *
                    p_sys->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
                    p_sys->p_info->sequence->display_height /
                    p_sys->p_info->sequence->pixel_height;
gbazin's avatar
 
gbazin committed
289 290
            }

gbazin's avatar
 
gbazin committed
291 292 293
            if ( p_dec->p_sys->p_vout != NULL )
            {
              int i_pic;
294
                /* Temporary hack to free the pictures in use by libmpeg2 */
gbazin's avatar
 
gbazin committed
295
                for ( i_pic = 0; i_pic < p_dec->p_sys->p_vout->render.i_pictures; i_pic++ )
296
                {
gbazin's avatar
 
gbazin committed
297
                    if( p_dec->p_sys->p_vout->render.pp_picture[i_pic]->i_status ==
298
                          RESERVED_PICTURE )
gbazin's avatar
 
gbazin committed
299 300 301 302 303
                        vout_DestroyPicture( p_dec->p_sys->p_vout,
                                         p_dec->p_sys->p_vout->render.pp_picture[i_pic] );
                    if( p_dec->p_sys->p_vout->render.pp_picture[i_pic]->i_refcount > 0 )
                        vout_UnlinkPicture( p_dec->p_sys->p_vout,
                                         p_dec->p_sys->p_vout->render.pp_picture[i_pic] );
304 305
                }
            }
gbazin's avatar
 
gbazin committed
306

gbazin's avatar
 
gbazin committed
307 308 309 310 311 312 313
            p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
                                          p_sys->p_info->sequence->width,
                                          p_sys->p_info->sequence->height,
                                          VLC_FOURCC('Y','V','1','2'),
                                          i_aspect );

            if(p_sys->p_vout == NULL )
zorglub's avatar
zorglub committed
314
            {
gbazin's avatar
 
gbazin committed
315 316
                msg_Err( p_dec, "cannot create vout" );
                block_Release( p_block );
zorglub's avatar
zorglub committed
317 318
                return -1;
            } 
319

gbazin's avatar
 
gbazin committed
320 321 322 323 324 325 326 327 328
            msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
                     p_sys->p_info->sequence->width,
                     p_sys->p_info->sequence->height, i_aspect,
                     (uint32_t)((u64)1001000000 * 27 /
                     p_sys->p_info->sequence->frame_period / 1001),
                     (uint32_t)((u64)1001000000 * 27 /
                     p_sys->p_info->sequence->frame_period % 1001) );

            mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
gbazin's avatar
 
gbazin committed
329 330

            /* Set the first 2 reference frames */
gbazin's avatar
 
gbazin committed
331
            mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
332 333 334

            if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
            memset( p_pic->p[0].p_pixels, 0,
gbazin's avatar
 
gbazin committed
335 336
                    p_sys->p_info->sequence->width
                     * p_sys->p_info->sequence->height );
337
            memset( p_pic->p[1].p_pixels, 0x80,
gbazin's avatar
 
gbazin committed
338 339
                    p_sys->p_info->sequence->width
                     * p_sys->p_info->sequence->height / 4 );
340
            memset( p_pic->p[2].p_pixels, 0x80,
gbazin's avatar
 
gbazin committed
341 342 343
                    p_sys->p_info->sequence->width
                     * p_sys->p_info->sequence->height / 4 );
            mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
344
            /* This picture will never go through display_picture. */
gbazin's avatar
 
gbazin committed
345 346
            vout_DatePicture( p_sys->p_vout, p_pic, 0 );
            vout_DisplayPicture( p_sys->p_vout, p_pic );
347 348
            /* For some reason, libmpeg2 will put this pic twice in
             * discard_picture. This can be considered a bug in libmpeg2. */
gbazin's avatar
 
gbazin committed
349
            vout_LinkPicture( p_sys->p_vout, p_pic );
350

gbazin's avatar
 
gbazin committed
351
            if ( p_sys->p_synchro )
352
            {
gbazin's avatar
 
gbazin committed
353
                vout_SynchroRelease( p_sys->p_synchro );
354
            }
gbazin's avatar
 
gbazin committed
355 356 357 358
            p_sys->p_synchro = vout_SynchroInit( p_dec, p_sys->p_vout,
                (uint32_t)((uint64_t)1001000000 * 27 /
                p_sys->p_info->sequence->frame_period) );
            p_sys->b_after_sequence_header = 1;
gbazin's avatar
 
gbazin committed
359 360
        }
        break;
gbazin's avatar
 
gbazin committed
361

362
        case STATE_PICTURE_2ND:
gbazin's avatar
 
gbazin committed
363 364 365
            vout_SynchroNewPicture( p_sys->p_synchro,
                p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
                p_sys->p_info->current_picture->nb_fields,
366
                0, 0,
gbazin's avatar
 
gbazin committed
367
                p_sys->i_current_rate );
368

gbazin's avatar
 
gbazin committed
369
            if ( p_sys->b_skip )
370
            {
gbazin's avatar
 
gbazin committed
371
                vout_SynchroTrash( p_sys->p_synchro );
372 373 374
            }
            else
            {
gbazin's avatar
 
gbazin committed
375
                vout_SynchroDecode( p_sys->p_synchro );
376 377 378
            }
            break;

gbazin's avatar
 
gbazin committed
379 380 381
        case STATE_PICTURE:
        {
            uint8_t *buf[3];
382 383
            buf[0] = buf[1] = buf[2] = NULL;

gbazin's avatar
 
gbazin committed
384 385
            if ( p_sys->b_after_sequence_header
                  && ((p_sys->p_info->current_picture->flags
386 387 388 389
                        & PIC_MASK_CODING_TYPE)
                       == PIC_FLAG_CODING_TYPE_P) )
            {
                /* Intra-slice refresh. Simulate a blank I picture. */
gbazin's avatar
 
gbazin committed
390 391 392 393 394 395
                msg_Dbg( p_dec, "intra-slice refresh stream" );
                vout_SynchroNewPicture( p_sys->p_synchro,
                    I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
                vout_SynchroDecode( p_sys->p_synchro );
                vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
                p_sys->b_slice_i = 1;
396
            }
gbazin's avatar
 
gbazin committed
397 398 399 400 401 402 403 404 405 406 407 408 409
            p_sys->b_after_sequence_header = 0;

            vout_SynchroNewPicture( p_sys->p_synchro,
                p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
                p_sys->p_info->current_picture->nb_fields,
                (p_sys->p_info->current_picture->flags & PIC_FLAG_PTS) ?
                    ( ( p_sys->p_info->current_picture->pts ==
                        (uint32_t)p_sys->i_current_pts ) ?
                      p_sys->i_current_pts : p_sys->i_previous_pts ) : 0,
                0, p_sys->i_current_rate );

            if ( !(p_sys->b_slice_i
                   && ((p_sys->p_info->current_picture->flags
410
                         & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
gbazin's avatar
 
gbazin committed
411 412
                   && !vout_SynchroChoose( p_sys->p_synchro,
                              p_sys->p_info->current_picture->flags
413
                                & PIC_MASK_CODING_TYPE ) )
414
            {
gbazin's avatar
 
gbazin committed
415 416 417 418
                mpeg2_skip( p_sys->p_mpeg2dec, 1 );
                p_sys->b_skip = 1;
                vout_SynchroTrash( p_sys->p_synchro );
                mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
419 420
            }
            else
gbazin's avatar
 
gbazin committed
421
            {
gbazin's avatar
 
gbazin committed
422 423 424
                mpeg2_skip( p_sys->p_mpeg2dec, 0 );
                p_sys->b_skip = 0;
                vout_SynchroDecode( p_sys->p_synchro );
425
                if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
gbazin's avatar
 
gbazin committed
426
                mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
gbazin's avatar
 
gbazin committed
427 428
            }
        }
429
        break;
gbazin's avatar
 
gbazin committed
430 431

        case STATE_END:
432
        case STATE_SLICE:
gbazin's avatar
 
gbazin committed
433 434
            if( p_sys->p_info->display_fbuf
                && p_sys->p_info->display_fbuf->id )
gbazin's avatar
 
gbazin committed
435
            {
gbazin's avatar
 
gbazin committed
436
                p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
gbazin's avatar
 
gbazin committed
437

gbazin's avatar
 
gbazin committed
438 439
                vout_SynchroEnd( p_sys->p_synchro,
                            p_sys->p_info->display_picture->flags
440
                             & PIC_MASK_CODING_TYPE,
gbazin's avatar
 
gbazin committed
441 442 443
                            p_sys->b_garbage_pic );
                p_sys->b_garbage_pic = 0;
                vout_DisplayPicture( p_sys->p_vout, p_pic );
444

gbazin's avatar
 
gbazin committed
445
                if ( p_sys->p_picture_to_destroy != p_pic )
446
                {
gbazin's avatar
 
gbazin committed
447 448
                    vout_DatePicture( p_sys->p_vout, p_pic,
                        vout_SynchroDate( p_sys->p_synchro ) );
449 450 451
                }
                else
                {
gbazin's avatar
 
gbazin committed
452 453
                    p_sys->p_picture_to_destroy = NULL;
                    vout_DatePicture( p_sys->p_vout, p_pic, 0 );
gbazin's avatar
 
gbazin committed
454
                }
455 456
            }

gbazin's avatar
 
gbazin committed
457 458
            if( p_sys->p_info->discard_fbuf &&
                p_sys->p_info->discard_fbuf->id )
459
            {
gbazin's avatar
 
gbazin committed
460 461
                p_pic = (picture_t *)p_sys->p_info->discard_fbuf->id;
                vout_UnlinkPicture( p_sys->p_vout, p_pic );
gbazin's avatar
 
gbazin committed
462 463 464
            }
            break;

465
        case STATE_INVALID:
466 467 468 469
        {
            uint8_t *buf[3];
            buf[0] = buf[1] = buf[2] = NULL;

gbazin's avatar
 
gbazin committed
470 471 472
            msg_Warn( p_dec, "invalid picture encountered" );
            if ( ( p_sys->p_info->current_picture == NULL ) || 
               ( ( p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE)
473
                  != B_CODING_TYPE ) )
474
            {
gbazin's avatar
 
gbazin committed
475
                vout_SynchroReset( p_sys->p_synchro );
476
            }
gbazin's avatar
 
gbazin committed
477 478
            mpeg2_skip( p_sys->p_mpeg2dec, 1 );
            p_sys->b_skip = 1;
479

gbazin's avatar
 
gbazin committed
480 481
            if( p_sys->p_info->current_fbuf &&
                p_sys->p_info->current_fbuf->id )
482
            {
gbazin's avatar
 
gbazin committed
483 484
                p_sys->b_garbage_pic = 1;
                p_pic = p_sys->p_info->current_fbuf->id;
485 486 487 488 489
            }
            else
            {
                if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
                    break;
gbazin's avatar
 
gbazin committed
490
                mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
491
            }
gbazin's avatar
 
gbazin committed
492
            p_sys->p_picture_to_destroy = p_pic;
493 494

            memset( p_pic->p[0].p_pixels, 0,
gbazin's avatar
 
gbazin committed
495 496
                    p_sys->p_info->sequence->width
                     * p_sys->p_info->sequence->height );
497
            memset( p_pic->p[1].p_pixels, 0x80,
gbazin's avatar
 
gbazin committed
498 499
                    p_sys->p_info->sequence->width
                     * p_sys->p_info->sequence->height / 4 );
500
            memset( p_pic->p[2].p_pixels, 0x80,
gbazin's avatar
 
gbazin committed
501 502
                    p_sys->p_info->sequence->width
                     * p_sys->p_info->sequence->height / 4 );
503

gbazin's avatar
 
gbazin committed
504
            if ( p_sys->b_slice_i )
505
            {
gbazin's avatar
 
gbazin committed
506 507 508 509
                vout_SynchroNewPicture( p_sys->p_synchro,
                            I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
                vout_SynchroDecode( p_sys->p_synchro );
                vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
510
            }
511
            break;
512
        }
513

gbazin's avatar
 
gbazin committed
514 515 516 517 518
        default:
            break;
        }
    }

gbazin's avatar
 
gbazin committed
519 520
    block_Release( p_block );
    return VLC_EGENERIC;
gbazin's avatar
 
gbazin committed
521 522 523
}

/*****************************************************************************
gbazin's avatar
 
gbazin committed
524
 * EndDecoder: libmpeg2 decoder destruction
gbazin's avatar
 
gbazin committed
525
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
526
static int EndDecoder( decoder_t * p_dec )
gbazin's avatar
 
gbazin committed
527
{
gbazin's avatar
 
gbazin committed
528 529 530
    decoder_sys_t *p_sys = p_dec->p_sys;

    if( p_sys )
gbazin's avatar
 
gbazin committed
531
    {
gbazin's avatar
 
gbazin committed
532 533
        int i_pic;

gbazin's avatar
 
gbazin committed
534 535
        if( p_sys->p_synchro )
            vout_SynchroRelease( p_sys->p_synchro );
536

gbazin's avatar
 
gbazin committed
537
        if( p_sys->p_vout )
gbazin's avatar
 
gbazin committed
538 539
        {
            /* Temporary hack to free the pictures in use by libmpeg2 */
gbazin's avatar
 
gbazin committed
540
            for( i_pic = 0; i_pic < p_sys->p_vout->render.i_pictures; i_pic++ )
gbazin's avatar
 
gbazin committed
541
            {
gbazin's avatar
 
gbazin committed
542
                if( p_sys->p_vout->render.pp_picture[i_pic]->i_status ==
gbazin's avatar
 
gbazin committed
543
                      RESERVED_PICTURE )
gbazin's avatar
 
gbazin committed
544 545 546 547 548
                    vout_DestroyPicture( p_sys->p_vout,
                                     p_sys->p_vout->render.pp_picture[i_pic] );
                if( p_sys->p_vout->render.pp_picture[i_pic]->i_refcount > 0 )
                    vout_UnlinkPicture( p_sys->p_vout,
                                     p_sys->p_vout->render.pp_picture[i_pic] );
gbazin's avatar
 
gbazin committed
549 550
            }

gbazin's avatar
 
gbazin committed
551
            vout_Request( p_dec, p_sys->p_vout, 0, 0, 0, 0 );
gbazin's avatar
 
gbazin committed
552
        }
gbazin's avatar
 
gbazin committed
553

gbazin's avatar
 
gbazin committed
554
        if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
gbazin's avatar
 
gbazin committed
555

gbazin's avatar
 
gbazin committed
556
        free( p_sys );
gbazin's avatar
 
gbazin committed
557
    }
gbazin's avatar
 
gbazin committed
558 559

    return VLC_SUCCESS;
gbazin's avatar
 
gbazin committed
560
}
gbazin's avatar
 
gbazin committed
561 562 563 564

/*****************************************************************************
 * GetNewPicture: Get a new picture from the vout and set the buf struct
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
565
static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
gbazin's avatar
 
gbazin committed
566
{
gbazin's avatar
 
gbazin committed
567 568
    decoder_sys_t *p_sys = p_dec->p_sys;

gbazin's avatar
 
gbazin committed
569
    picture_t *p_pic;
gbazin's avatar
 
gbazin committed
570 571
    vlc_bool_t b_progressive = p_sys->p_info->current_picture != NULL ?
        p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME :
572
        1;
gbazin's avatar
 
gbazin committed
573 574
    vlc_bool_t b_top_field_first = p_sys->p_info->current_picture != NULL ?
        p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST :
575
        1;
gbazin's avatar
 
gbazin committed
576 577
    unsigned int i_nb_fields = p_sys->p_info->current_picture != NULL ?
        p_sys->p_info->current_picture->nb_fields : 2;
gbazin's avatar
 
gbazin committed
578 579

    /* Get a new picture */
gbazin's avatar
 
gbazin committed
580
    while( !(p_pic = vout_CreatePicture( p_sys->p_vout,
581
        b_progressive, b_top_field_first, i_nb_fields )) )
gbazin's avatar
 
gbazin committed
582 583 584 585 586 587 588 589
    {
        if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
            break;

        msleep( VOUT_OUTMEM_SLEEP );
    }
    if( p_pic == NULL )
        return NULL;
gbazin's avatar
 
gbazin committed
590
    vout_LinkPicture( p_sys->p_vout, p_pic );
gbazin's avatar
 
gbazin committed
591 592 593 594 595 596 597

    pp_buf[0] = p_pic->p[0].p_pixels;
    pp_buf[1] = p_pic->p[1].p_pixels;
    pp_buf[2] = p_pic->p[2].p_pixels;

    return p_pic;
}