libmpeg2.c 22.2 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
gbazin's avatar
 
gbazin committed
5
 * $Id: libmpeg2.c,v 1.26 2003/09/02 20:19:25 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
    {
gbazin's avatar
 
gbazin committed
179
        state = mpeg2_parse( p_sys->p_mpeg2dec );
gbazin's avatar
 
gbazin committed
180 181 182 183

        switch( state )
        {
        case STATE_BUFFER:
gbazin's avatar
 
gbazin committed
184
                if( !p_block->i_buffer || b_need_more_data )
gbazin's avatar
 
gbazin committed
185
                {
gbazin's avatar
 
gbazin committed
186 187
                    block_Release( p_block );
                    return VLC_SUCCESS;
gbazin's avatar
 
gbazin committed
188 189
                }

gbazin's avatar
 
gbazin committed
190 191 192
#if 0
                if( p_pes->b_discontinuity && p_sys->p_synchro 
                     && p_sys->p_info->sequence->width != (unsigned)-1 )
193
                {
gbazin's avatar
 
gbazin committed
194 195 196
                    vout_SynchroReset( p_sys->p_synchro );
                    if ( p_sys->p_info->current_fbuf != NULL
                          && p_sys->p_info->current_fbuf->id != NULL )
197
                    {
gbazin's avatar
 
gbazin committed
198 199
                        p_sys->b_garbage_pic = 1;
                        p_pic = p_sys->p_info->current_fbuf->id;
200 201 202 203 204 205 206
                    }
                    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
207
                        mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
208
                    }
gbazin's avatar
 
gbazin committed
209
                    p_sys->p_picture_to_destroy = p_pic;
210 211

                    memset( p_pic->p[0].p_pixels, 0,
gbazin's avatar
 
gbazin committed
212 213
                            p_sys->p_info->sequence->width
                             * p_sys->p_info->sequence->height );
214
                    memset( p_pic->p[1].p_pixels, 0x80,
gbazin's avatar
 
gbazin committed
215 216
                            p_sys->p_info->sequence->width
                             * p_sys->p_info->sequence->height / 4 );
217
                    memset( p_pic->p[2].p_pixels, 0x80,
gbazin's avatar
 
gbazin committed
218 219
                            p_sys->p_info->sequence->width
                             * p_sys->p_info->sequence->height / 4 );
220

gbazin's avatar
 
gbazin committed
221
                    if ( p_sys->b_slice_i )
222
                    {
gbazin's avatar
 
gbazin committed
223 224 225 226
                        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 );
227
                    }
228
                }
gbazin's avatar
 
gbazin committed
229
#endif
230

gbazin's avatar
 
gbazin committed
231 232 233 234 235
            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
236 237
            }

gbazin's avatar
 
gbazin committed
238
            p_sys->i_current_rate = DEFAULT_RATE;//p_pes->i_rate;
gbazin's avatar
 
gbazin committed
239

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

        case STATE_SEQUENCE:
gbazin's avatar
 
gbazin committed
247
        {
gbazin's avatar
 
gbazin committed
248
            /* Initialize video output */
gbazin's avatar
 
gbazin committed
249
            uint8_t *buf[3];
250
            buf[0] = buf[1] = buf[2] = NULL;
gbazin's avatar
 
gbazin committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

            /* 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
271 272
                                   p_sys->p_info->sequence->width /
                                   p_sys->p_info->sequence->height;
gbazin's avatar
 
gbazin committed
273 274 275 276 277 278
                    break;
                }
            }
            else
            {
                /* Use the value provided in the MPEG sequence header */
gbazin's avatar
 
gbazin committed
279 280 281 282
                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
283 284
            }

gbazin's avatar
 
gbazin committed
285 286 287
            if ( p_dec->p_sys->p_vout != NULL )
            {
              int i_pic;
288
                /* Temporary hack to free the pictures in use by libmpeg2 */
gbazin's avatar
 
gbazin committed
289
                for ( i_pic = 0; i_pic < p_dec->p_sys->p_vout->render.i_pictures; i_pic++ )
290
                {
gbazin's avatar
 
gbazin committed
291
                    if( p_dec->p_sys->p_vout->render.pp_picture[i_pic]->i_status ==
292
                          RESERVED_PICTURE )
gbazin's avatar
 
gbazin committed
293 294 295 296 297
                        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] );
298 299
                }
            }
gbazin's avatar
 
gbazin committed
300

gbazin's avatar
 
gbazin committed
301 302 303 304 305 306 307
            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
308
            {
gbazin's avatar
 
gbazin committed
309 310
                msg_Err( p_dec, "cannot create vout" );
                block_Release( p_block );
zorglub's avatar
zorglub committed
311 312
                return -1;
            } 
313

gbazin's avatar
 
gbazin committed
314 315 316 317 318 319 320 321 322
            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
323 324

            /* Set the first 2 reference frames */
gbazin's avatar
 
gbazin committed
325
            mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
326 327 328

            if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
            memset( p_pic->p[0].p_pixels, 0,
gbazin's avatar
 
gbazin committed
329 330
                    p_sys->p_info->sequence->width
                     * p_sys->p_info->sequence->height );
331
            memset( p_pic->p[1].p_pixels, 0x80,
gbazin's avatar
 
gbazin committed
332 333
                    p_sys->p_info->sequence->width
                     * p_sys->p_info->sequence->height / 4 );
334
            memset( p_pic->p[2].p_pixels, 0x80,
gbazin's avatar
 
gbazin committed
335 336 337
                    p_sys->p_info->sequence->width
                     * p_sys->p_info->sequence->height / 4 );
            mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
338
            /* This picture will never go through display_picture. */
gbazin's avatar
 
gbazin committed
339 340
            vout_DatePicture( p_sys->p_vout, p_pic, 0 );
            vout_DisplayPicture( p_sys->p_vout, p_pic );
341 342
            /* 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
343
            vout_LinkPicture( p_sys->p_vout, p_pic );
344

gbazin's avatar
 
gbazin committed
345
            if ( p_sys->p_synchro )
346
            {
gbazin's avatar
 
gbazin committed
347
                vout_SynchroRelease( p_sys->p_synchro );
348
            }
gbazin's avatar
 
gbazin committed
349 350 351 352
            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
353 354
        }
        break;
gbazin's avatar
 
gbazin committed
355

356
        case STATE_PICTURE_2ND:
gbazin's avatar
 
gbazin committed
357 358 359
            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,
360
                0, 0,
gbazin's avatar
 
gbazin committed
361
                p_sys->i_current_rate );
362

gbazin's avatar
 
gbazin committed
363
            if ( p_sys->b_skip )
364
            {
gbazin's avatar
 
gbazin committed
365
                vout_SynchroTrash( p_sys->p_synchro );
366 367 368
            }
            else
            {
gbazin's avatar
 
gbazin committed
369
                vout_SynchroDecode( p_sys->p_synchro );
370 371 372
            }
            break;

gbazin's avatar
 
gbazin committed
373 374 375
        case STATE_PICTURE:
        {
            uint8_t *buf[3];
376 377
            buf[0] = buf[1] = buf[2] = NULL;

gbazin's avatar
 
gbazin committed
378 379
            if ( p_sys->b_after_sequence_header
                  && ((p_sys->p_info->current_picture->flags
380 381 382 383
                        & PIC_MASK_CODING_TYPE)
                       == PIC_FLAG_CODING_TYPE_P) )
            {
                /* Intra-slice refresh. Simulate a blank I picture. */
gbazin's avatar
 
gbazin committed
384 385 386 387 388 389
                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;
390
            }
gbazin's avatar
 
gbazin committed
391 392 393 394 395 396 397 398 399 400 401 402 403
            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
404
                         & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
gbazin's avatar
 
gbazin committed
405 406
                   && !vout_SynchroChoose( p_sys->p_synchro,
                              p_sys->p_info->current_picture->flags
407
                                & PIC_MASK_CODING_TYPE ) )
408
            {
gbazin's avatar
 
gbazin committed
409 410 411 412
                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 );
413 414
            }
            else
gbazin's avatar
 
gbazin committed
415
            {
gbazin's avatar
 
gbazin committed
416 417 418
                mpeg2_skip( p_sys->p_mpeg2dec, 0 );
                p_sys->b_skip = 0;
                vout_SynchroDecode( p_sys->p_synchro );
419
                if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
gbazin's avatar
 
gbazin committed
420
                mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
gbazin's avatar
 
gbazin committed
421 422
            }
        }
423
        break;
gbazin's avatar
 
gbazin committed
424 425

        case STATE_END:
426
        case STATE_SLICE:
gbazin's avatar
 
gbazin committed
427 428
            if( p_sys->p_info->display_fbuf
                && p_sys->p_info->display_fbuf->id )
gbazin's avatar
 
gbazin committed
429
            {
gbazin's avatar
 
gbazin committed
430
                p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
gbazin's avatar
 
gbazin committed
431

gbazin's avatar
 
gbazin committed
432 433
                vout_SynchroEnd( p_sys->p_synchro,
                            p_sys->p_info->display_picture->flags
434
                             & PIC_MASK_CODING_TYPE,
gbazin's avatar
 
gbazin committed
435 436 437
                            p_sys->b_garbage_pic );
                p_sys->b_garbage_pic = 0;
                vout_DisplayPicture( p_sys->p_vout, p_pic );
438

gbazin's avatar
 
gbazin committed
439
                if ( p_sys->p_picture_to_destroy != p_pic )
440
                {
gbazin's avatar
 
gbazin committed
441 442
                    vout_DatePicture( p_sys->p_vout, p_pic,
                        vout_SynchroDate( p_sys->p_synchro ) );
443 444 445
                }
                else
                {
gbazin's avatar
 
gbazin committed
446 447
                    p_sys->p_picture_to_destroy = NULL;
                    vout_DatePicture( p_sys->p_vout, p_pic, 0 );
gbazin's avatar
 
gbazin committed
448
                }
449 450
            }

gbazin's avatar
 
gbazin committed
451 452
            if( p_sys->p_info->discard_fbuf &&
                p_sys->p_info->discard_fbuf->id )
453
            {
gbazin's avatar
 
gbazin committed
454 455
                p_pic = (picture_t *)p_sys->p_info->discard_fbuf->id;
                vout_UnlinkPicture( p_sys->p_vout, p_pic );
gbazin's avatar
 
gbazin committed
456 457 458
            }
            break;

459
        case STATE_INVALID:
460 461 462 463
        {
            uint8_t *buf[3];
            buf[0] = buf[1] = buf[2] = NULL;

gbazin's avatar
 
gbazin committed
464 465 466
            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)
467
                  != B_CODING_TYPE ) )
468
            {
gbazin's avatar
 
gbazin committed
469
                vout_SynchroReset( p_sys->p_synchro );
470
            }
gbazin's avatar
 
gbazin committed
471 472
            mpeg2_skip( p_sys->p_mpeg2dec, 1 );
            p_sys->b_skip = 1;
473

gbazin's avatar
 
gbazin committed
474 475
            if( p_sys->p_info->current_fbuf &&
                p_sys->p_info->current_fbuf->id )
476
            {
gbazin's avatar
 
gbazin committed
477 478
                p_sys->b_garbage_pic = 1;
                p_pic = p_sys->p_info->current_fbuf->id;
479 480 481 482 483
            }
            else
            {
                if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
                    break;
gbazin's avatar
 
gbazin committed
484
                mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
485
            }
gbazin's avatar
 
gbazin committed
486
            p_sys->p_picture_to_destroy = p_pic;
487 488

            memset( p_pic->p[0].p_pixels, 0,
gbazin's avatar
 
gbazin committed
489 490
                    p_sys->p_info->sequence->width
                     * p_sys->p_info->sequence->height );
491
            memset( p_pic->p[1].p_pixels, 0x80,
gbazin's avatar
 
gbazin committed
492 493
                    p_sys->p_info->sequence->width
                     * p_sys->p_info->sequence->height / 4 );
494
            memset( p_pic->p[2].p_pixels, 0x80,
gbazin's avatar
 
gbazin committed
495 496
                    p_sys->p_info->sequence->width
                     * p_sys->p_info->sequence->height / 4 );
497

gbazin's avatar
 
gbazin committed
498
            if ( p_sys->b_slice_i )
499
            {
gbazin's avatar
 
gbazin committed
500 501 502 503
                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 );
504
            }
505
            break;
506
        }
507

gbazin's avatar
 
gbazin committed
508 509 510 511 512
        default:
            break;
        }
    }

gbazin's avatar
 
gbazin committed
513 514
    block_Release( p_block );
    return VLC_EGENERIC;
gbazin's avatar
 
gbazin committed
515 516 517
}

/*****************************************************************************
gbazin's avatar
 
gbazin committed
518
 * EndDecoder: libmpeg2 decoder destruction
gbazin's avatar
 
gbazin committed
519
 *****************************************************************************/
gbazin's avatar
 
gbazin committed
520
static int EndDecoder( decoder_t * p_dec )
gbazin's avatar
 
gbazin committed
521
{
gbazin's avatar
 
gbazin committed
522 523 524
    decoder_sys_t *p_sys = p_dec->p_sys;

    if( p_sys )
gbazin's avatar
 
gbazin committed
525
    {
gbazin's avatar
 
gbazin committed
526 527
        int i_pic;

gbazin's avatar
 
gbazin committed
528 529
        if( p_sys->p_synchro )
            vout_SynchroRelease( p_sys->p_synchro );
530

gbazin's avatar
 
gbazin committed
531
        if( p_sys->p_vout )
gbazin's avatar
 
gbazin committed
532 533
        {
            /* Temporary hack to free the pictures in use by libmpeg2 */
gbazin's avatar
 
gbazin committed
534
            for( i_pic = 0; i_pic < p_sys->p_vout->render.i_pictures; i_pic++ )
gbazin's avatar
 
gbazin committed
535
            {
gbazin's avatar
 
gbazin committed
536
                if( p_sys->p_vout->render.pp_picture[i_pic]->i_status ==
gbazin's avatar
 
gbazin committed
537
                      RESERVED_PICTURE )
gbazin's avatar
 
gbazin committed
538 539 540 541 542
                    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
543 544
            }

gbazin's avatar
 
gbazin committed
545
            vout_Request( p_dec, p_sys->p_vout, 0, 0, 0, 0 );
gbazin's avatar
 
gbazin committed
546
        }
gbazin's avatar
 
gbazin committed
547

gbazin's avatar
 
gbazin committed
548
        if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
gbazin's avatar
 
gbazin committed
549

gbazin's avatar
 
gbazin committed
550
        free( p_sys );
gbazin's avatar
 
gbazin committed
551
    }
gbazin's avatar
 
gbazin committed
552 553

    return VLC_SUCCESS;
gbazin's avatar
 
gbazin committed
554
}
gbazin's avatar
 
gbazin committed
555 556 557 558

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

gbazin's avatar
 
gbazin committed
563
    picture_t *p_pic;
gbazin's avatar
 
gbazin committed
564 565
    vlc_bool_t b_progressive = p_sys->p_info->current_picture != NULL ?
        p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME :
566
        1;
gbazin's avatar
 
gbazin committed
567 568
    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 :
569
        1;
gbazin's avatar
 
gbazin committed
570 571
    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
572 573

    /* Get a new picture */
gbazin's avatar
 
gbazin committed
574
    while( !(p_pic = vout_CreatePicture( p_sys->p_vout,
575
        b_progressive, b_top_field_first, i_nb_fields )) )
gbazin's avatar
 
gbazin committed
576 577 578 579 580 581 582 583
    {
        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
584
    vout_LinkPicture( p_sys->p_vout, p_pic );
gbazin's avatar
 
gbazin committed
585 586 587 588 589 590 591

    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;
}