]> git.sesse.net Git - vlc/blob - modules/codec/libmpeg2.c
Merge branch 1.0-bugfix
[vlc] / modules / codec / libmpeg2.c
1 /*****************************************************************************
2  * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
3  *****************************************************************************
4  * Copyright (C) 1999-2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Christophe Massiot <massiot@via.ecp.fr>
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_plugin.h>
34 #include <vlc_vout.h>
35 #include <vlc_codec.h>
36 #include <vlc_block_helper.h>
37 #include "../codec/cc.h"
38
39 #include <mpeg2.h>
40
41 #include <vlc_codec_synchro.h>
42
43 /*****************************************************************************
44  * decoder_sys_t : libmpeg2 decoder descriptor
45  *****************************************************************************/
46 struct decoder_sys_t
47 {
48     /*
49      * libmpeg2 properties
50      */
51     mpeg2dec_t          *p_mpeg2dec;
52     const mpeg2_info_t  *p_info;
53     bool                b_skip;
54
55     /*
56      * Input properties
57      */
58     mtime_t          i_previous_pts;
59     mtime_t          i_current_pts;
60     mtime_t          i_previous_dts;
61     mtime_t          i_current_dts;
62     picture_t *      p_picture_to_destroy;
63     bool             b_garbage_pic;
64     bool             b_after_sequence_header; /* is it the next frame after
65                                                * the sequence header ?    */
66     bool             b_slice_i;             /* intra-slice refresh stream */
67     bool             b_second_field;
68
69     bool             b_preroll;
70
71     /*
72      * Output properties
73      */
74     decoder_synchro_t *p_synchro;
75     int             i_aspect;
76     int             i_sar_num;
77     int             i_sar_den;
78     mtime_t         i_last_frame_pts;
79
80     /* Closed captioning support */
81     uint32_t        i_cc_flags;
82     mtime_t         i_cc_pts;
83     mtime_t         i_cc_dts;
84     cc_data_t       cc;
85     uint8_t        *p_gop_user_data;
86     uint32_t        i_gop_user_data;
87 };
88
89 /*****************************************************************************
90  * Local prototypes
91  *****************************************************************************/
92 static int  OpenDecoder( vlc_object_t * );
93 static void CloseDecoder( vlc_object_t * );
94
95 static picture_t *DecodeBlock( decoder_t *, block_t ** );
96 static block_t   *GetCc( decoder_t *p_dec, bool pb_present[4] );
97
98 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
99 static void GetAR( decoder_t *p_dec );
100
101 /*****************************************************************************
102  * Module descriptor
103  *****************************************************************************/
104 vlc_module_begin ()
105     set_description( N_("MPEG I/II video decoder (using libmpeg2)") )
106     set_capability( "decoder", 150 )
107     set_category( CAT_INPUT )
108     set_subcategory( SUBCAT_INPUT_VCODEC )
109     set_callbacks( OpenDecoder, CloseDecoder )
110     add_shortcut( "libmpeg2" )
111 vlc_module_end ()
112
113 /*****************************************************************************
114  * OpenDecoder: probe the decoder and return score
115  *****************************************************************************/
116 static int OpenDecoder( vlc_object_t *p_this )
117 {
118     decoder_t *p_dec = (decoder_t*)p_this;
119     decoder_sys_t *p_sys;
120     uint32_t i_accel = 0;
121
122     if( p_dec->fmt_in.i_codec != VLC_CODEC_MP1V &&
123         p_dec->fmt_in.i_codec != VLC_CODEC_MP2V &&
124         p_dec->fmt_in.i_codec != VLC_CODEC_MPGV )
125         return VLC_EGENERIC;
126
127     /* Select onl recognized original format (standard mpeg video) */
128     switch( p_dec->fmt_in.i_original_fourcc )
129     {
130     case VLC_FOURCC('m','p','g','1'):
131     case VLC_FOURCC('m','p','g','2'):
132     case VLC_FOURCC('m','p','g','v'):
133     case VLC_FOURCC('P','I','M','1'):
134     case VLC_FOURCC('h','d','v','2'):
135         break;
136     default:
137         if( p_dec->fmt_in.i_original_fourcc )
138             return VLC_EGENERIC;
139         break;
140     }
141
142     /* Allocate the memory needed to store the decoder's structure */
143     if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
144         return VLC_ENOMEM;
145
146     /* Initialize the thread properties */
147     p_sys->p_mpeg2dec = NULL;
148     p_sys->p_synchro  = NULL;
149     p_sys->p_info     = NULL;
150     p_sys->i_current_pts  = 0;
151     p_sys->i_previous_pts = 0;
152     p_sys->i_current_dts  = 0;
153     p_sys->i_previous_dts = 0;
154     p_sys->p_picture_to_destroy = NULL;
155     p_sys->b_garbage_pic = 0;
156     p_sys->b_slice_i  = 0;
157     p_sys->b_second_field = 0;
158     p_sys->b_skip     = 0;
159     p_sys->b_preroll = false;
160
161     p_sys->i_cc_pts = 0;
162     p_sys->i_cc_dts = 0;
163     p_sys->i_cc_flags = 0;
164 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
165     p_dec->pf_get_cc = GetCc;
166     cc_Init( &p_sys->cc );
167 #endif
168     p_sys->p_gop_user_data = NULL;
169     p_sys->i_gop_user_data = 0;
170
171 #if defined( __i386__ ) || defined( __x86_64__ )
172     if( vlc_CPU() & CPU_CAPABILITY_MMX )
173     {
174         i_accel |= MPEG2_ACCEL_X86_MMX;
175     }
176
177     if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
178     {
179         i_accel |= MPEG2_ACCEL_X86_3DNOW;
180     }
181
182     if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
183     {
184         i_accel |= MPEG2_ACCEL_X86_MMXEXT;
185     }
186
187 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
188     if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
189     {
190         i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
191     }
192
193 #else
194     /* If we do not know this CPU, trust libmpeg2's feature detection */
195     i_accel = MPEG2_ACCEL_DETECT;
196
197 #endif
198
199     /* Set CPU acceleration features */
200     mpeg2_accel( i_accel );
201
202     /* Initialize decoder */
203     p_sys->p_mpeg2dec = mpeg2_init();
204     if( p_sys->p_mpeg2dec == NULL)
205     {
206         msg_Err( p_dec, "mpeg2_init() failed" );
207         free( p_sys );
208         return VLC_EGENERIC;
209     }
210
211     p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
212
213     p_dec->pf_decode_video = DecodeBlock;
214     p_dec->fmt_out.i_cat = VIDEO_ES;
215     p_dec->fmt_out.i_codec = 0;
216
217     return VLC_SUCCESS;
218 }
219
220 /*****************************************************************************
221  * RunDecoder: the libmpeg2 decoder
222  *****************************************************************************/
223 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
224 {
225     decoder_sys_t   *p_sys = p_dec->p_sys;
226     mpeg2_state_t   state;
227     picture_t       *p_pic;
228
229     block_t *p_block;
230
231     if( !pp_block || !*pp_block ) return NULL;
232
233     p_block = *pp_block;
234
235     while( 1 )
236     {
237         state = mpeg2_parse( p_sys->p_mpeg2dec );
238
239         switch( state )
240         {
241         case STATE_BUFFER:
242             if( !p_block->i_buffer )
243             {
244                 block_Release( p_block );
245                 return NULL;
246             }
247             if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY
248                                       | BLOCK_FLAG_CORRUPTED))
249                 cc_Flush( &p_sys->cc );
250
251             if( (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY
252                                       | BLOCK_FLAG_CORRUPTED)) &&
253                 p_sys->p_synchro &&
254                 p_sys->p_info->sequence &&
255                 p_sys->p_info->sequence->width != (unsigned)-1 )
256             {
257                 decoder_SynchroReset( p_sys->p_synchro );
258                 if( p_sys->p_info->current_fbuf != NULL
259                     && p_sys->p_info->current_fbuf->id != NULL )
260                 {
261                     p_sys->b_garbage_pic = 1;
262                     p_pic = p_sys->p_info->current_fbuf->id;
263                 }
264                 else
265                 {
266                     uint8_t *buf[3];
267                     buf[0] = buf[1] = buf[2] = NULL;
268                     if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
269                     {
270                         p_block->i_buffer = 0;
271                         break;
272                     }
273                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
274                     mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
275                 }
276                 p_sys->p_picture_to_destroy = p_pic;
277
278                 if ( p_sys->b_slice_i )
279                 {
280                     decoder_SynchroNewPicture( p_sys->p_synchro,
281                         I_CODING_TYPE, 2, 0, 0,
282                         p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
283                     decoder_SynchroDecode( p_sys->p_synchro );
284                     decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
285                 }
286             }
287
288             if( p_block->i_flags & BLOCK_FLAG_PREROLL )
289             {
290                 p_sys->b_preroll = true;
291             }
292             else if( p_sys->b_preroll )
293             {
294                 p_sys->b_preroll = false;
295                 /* Reset synchro */
296                 decoder_SynchroReset( p_sys->p_synchro );
297             }
298
299 #ifdef PIC_FLAG_PTS
300             if( p_block->i_pts )
301             {
302                 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
303
304 #else /* New interface */
305             if( p_block->i_pts || p_block->i_dts )
306             {
307                 mpeg2_tag_picture( p_sys->p_mpeg2dec,
308                                    (uint32_t)p_block->i_pts,
309                                    (uint32_t)p_block->i_dts );
310 #endif
311                 p_sys->i_previous_pts = p_sys->i_current_pts;
312                 p_sys->i_current_pts = p_block->i_pts;
313                 p_sys->i_previous_dts = p_sys->i_current_dts;
314                 p_sys->i_current_dts = p_block->i_dts;
315             }
316
317             mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
318                           p_block->p_buffer + p_block->i_buffer );
319
320             p_block->i_buffer = 0;
321             break;
322
323 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
324
325         case STATE_SEQUENCE_MODIFIED:
326             GetAR( p_dec );
327             break;
328 #endif
329
330         case STATE_SEQUENCE:
331         {
332             /* Initialize video output */
333             uint8_t *buf[3];
334             buf[0] = buf[1] = buf[2] = NULL;
335
336             GetAR( p_dec );
337
338             mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
339
340             /* Set the first 2 reference frames */
341             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
342
343             if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
344             {
345                 block_Release( p_block );
346                 return NULL;
347             }
348
349             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
350             mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
351
352             /* This picture will never go through display_picture. */
353             p_pic->date = 0;
354
355             /* For some reason, libmpeg2 will put this pic twice in
356              * discard_picture. This can be considered a bug in libmpeg2. */
357             decoder_LinkPicture( p_dec, p_pic );
358
359             if( p_sys->p_synchro )
360             {
361                 decoder_SynchroRelease( p_sys->p_synchro );
362             }
363             p_sys->p_synchro = decoder_SynchroInit( p_dec,
364                 (uint32_t)((uint64_t)1001000000 * 27 /
365                 p_sys->p_info->sequence->frame_period) );
366             p_sys->b_after_sequence_header = 1;
367         }
368         break;
369
370         case STATE_PICTURE_2ND:
371             p_sys->b_second_field = 1;
372             break;
373
374         case STATE_PICTURE:
375         {
376             uint8_t *buf[3];
377             mtime_t i_pts, i_dts;
378             buf[0] = buf[1] = buf[2] = NULL;
379
380             if ( p_sys->b_after_sequence_header &&
381                  ((p_sys->p_info->current_picture->flags &
382                        PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
383             {
384                 /* Intra-slice refresh. Simulate a blank I picture. */
385                 msg_Dbg( p_dec, "intra-slice refresh stream" );
386                 decoder_SynchroNewPicture( p_sys->p_synchro,
387                     I_CODING_TYPE, 2, 0, 0,
388                     p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
389                 decoder_SynchroDecode( p_sys->p_synchro );
390                 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
391                 p_sys->b_slice_i = 1;
392             }
393             p_sys->b_after_sequence_header = 0;
394
395 #ifdef PIC_FLAG_PTS
396             i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
397                 ( ( p_sys->p_info->current_picture->pts ==
398                     (uint32_t)p_sys->i_current_pts ) ?
399                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
400             i_dts = 0;
401
402             /* Hack to handle demuxers which only have DTS timestamps */
403             if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
404             {
405                 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
406                     (p_sys->p_info->current_picture->flags &
407                       PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
408                 {
409                     i_pts = p_block->i_dts;
410                 }
411             }
412             p_block->i_pts = p_block->i_dts = 0;
413             /* End hack */
414
415 #else /* New interface */
416
417             i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
418                 ( ( p_sys->p_info->current_picture->tag ==
419                     (uint32_t)p_sys->i_current_pts ) ?
420                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
421             i_dts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
422                 ( ( p_sys->p_info->current_picture->tag2 ==
423                     (uint32_t)p_sys->i_current_dts ) ?
424                   p_sys->i_current_dts : p_sys->i_previous_dts ) : 0;
425 #endif
426
427             /* If nb_fields == 1, it is a field picture, and it will be
428              * followed by another field picture for which we won't call
429              * decoder_SynchroNewPicture() because this would have other
430              * problems, so we take it into account here.
431              * This kind of sucks, but I didn't think better. --Meuuh
432              */
433             decoder_SynchroNewPicture( p_sys->p_synchro,
434                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
435                 p_sys->p_info->current_picture->nb_fields == 1 ? 2 :
436                 p_sys->p_info->current_picture->nb_fields, i_pts, i_dts,
437                 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
438
439
440             bool b_skip = false;
441             if( !p_dec->b_pace_control && !p_sys->b_preroll &&
442                 !(p_sys->b_slice_i
443                    && ((p_sys->p_info->current_picture->flags
444                          & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P))
445                    && !decoder_SynchroChoose( p_sys->p_synchro,
446                               p_sys->p_info->current_picture->flags
447                                 & PIC_MASK_CODING_TYPE,
448                               /*p_sys->p_vout->render_time*/ 0 /*FIXME*/,
449                               p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
450             {
451                 b_skip = true;
452             }
453
454             p_pic = NULL;
455             if( !b_skip )
456                 p_pic = GetNewPicture( p_dec, buf );
457
458             if( b_skip || !p_pic )
459             {
460                 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
461                 p_sys->b_skip = 1;
462                 decoder_SynchroTrash( p_sys->p_synchro );
463                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
464
465                 if( !b_skip )
466                 {
467                     block_Release( p_block );
468                     return NULL;
469                 }
470             }
471             else
472             {
473                 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
474                 p_sys->b_skip = 0;
475                 decoder_SynchroDecode( p_sys->p_synchro );
476
477                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
478                 mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
479             }
480             if( p_sys->p_info->user_data_len > 2 || p_sys->i_gop_user_data > 2 )
481             {
482                 p_sys->i_cc_pts = i_pts;
483                 p_sys->i_cc_dts = i_dts;
484                 if( (p_sys->p_info->current_picture->flags
485                              & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P )
486                     p_sys->i_cc_flags = BLOCK_FLAG_TYPE_P;
487                 else if( (p_sys->p_info->current_picture->flags
488                              & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
489                     p_sys->i_cc_flags = BLOCK_FLAG_TYPE_B;
490                 else p_sys->i_cc_flags = BLOCK_FLAG_TYPE_I;
491
492                 if( p_sys->i_gop_user_data > 2 )
493                 {
494                     /* We now have picture info for any cached user_data out of the gop */
495                     cc_Extract( &p_sys->cc, &p_sys->p_gop_user_data[0], p_sys->i_gop_user_data );
496                     p_sys->i_gop_user_data = 0;
497                 }
498
499                 /* Extract the CC from the user_data of the picture */
500                 if( p_sys->p_info->user_data_len > 2 )
501                     cc_Extract( &p_sys->cc, &p_sys->p_info->user_data[0], p_sys->p_info->user_data_len );
502             }
503         }
504         break;
505
506         case STATE_GOP:
507             /* There can be userdata in a GOP. It needs to be remembered for the next picture. */
508             if( p_sys->p_info->user_data_len > 2 )
509             {
510                 free( p_sys->p_gop_user_data );
511                 p_sys->p_gop_user_data = calloc( p_sys->p_info->user_data_len, sizeof(uint8_t) );
512                 if( p_sys->p_gop_user_data )
513                 {
514                     p_sys->i_gop_user_data = p_sys->p_info->user_data_len;
515                     memcpy( p_sys->p_gop_user_data, p_sys->p_info->user_data, p_sys->p_info->user_data_len );
516                 }
517             }
518             break;
519
520         case STATE_END:
521         case STATE_SLICE:
522             p_pic = NULL;
523             if( p_sys->p_info->display_fbuf
524                 && p_sys->p_info->display_fbuf->id )
525             {
526                 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
527
528                 decoder_SynchroEnd( p_sys->p_synchro,
529                             p_sys->p_info->display_picture->flags
530                              & PIC_MASK_CODING_TYPE,
531                             p_sys->b_garbage_pic );
532                 p_sys->b_garbage_pic = 0;
533
534                 if( p_sys->p_picture_to_destroy != p_pic )
535                 {
536                     p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
537                 }
538                 else
539                 {
540                     p_sys->p_picture_to_destroy = NULL;
541                     p_pic->date = 0;
542                 }
543             }
544
545             if( p_sys->p_info->discard_fbuf &&
546                 p_sys->p_info->discard_fbuf->id )
547             {
548                 decoder_UnlinkPicture( p_dec,
549                                        p_sys->p_info->discard_fbuf->id );
550             }
551
552             /* For still frames */
553             if( state == STATE_END && p_pic ) p_pic->b_force = true;
554
555             if( p_pic )
556             {
557                 /* Avoid frames with identical timestamps.
558                  * Especially needed for still frames in DVD menus. */
559                 if( p_sys->i_last_frame_pts == p_pic->date ) p_pic->date++;
560                 p_sys->i_last_frame_pts = p_pic->date;
561
562                 return p_pic;
563             }
564             break;
565
566         case STATE_INVALID:
567         {
568             uint8_t *buf[3];
569             buf[0] = buf[1] = buf[2] = NULL;
570
571             msg_Warn( p_dec, "invalid picture encountered" );
572             if ( ( p_sys->p_info->current_picture == NULL ) ||
573                ( ( p_sys->p_info->current_picture->flags &
574                    PIC_MASK_CODING_TYPE) != PIC_FLAG_CODING_TYPE_B ) )
575             {
576                 if( p_sys->p_synchro ) decoder_SynchroReset( p_sys->p_synchro );
577             }
578             mpeg2_skip( p_sys->p_mpeg2dec, 1 );
579             p_sys->b_skip = 1;
580             cc_Flush( &p_sys->cc );
581
582             if( p_sys->p_info->current_fbuf &&
583                 p_sys->p_info->current_fbuf->id )
584             {
585                 p_sys->b_garbage_pic = 1;
586                 p_pic = p_sys->p_info->current_fbuf->id;
587             }
588             else if( !p_sys->p_info->sequence )
589             {
590                 break;
591             }
592             else
593             {
594                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
595                     break;
596                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
597                 mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
598             }
599             p_sys->p_picture_to_destroy = p_pic;
600
601             memset( p_pic->p[0].p_pixels, 0,
602                     p_sys->p_info->sequence->width
603                      * p_sys->p_info->sequence->height );
604             memset( p_pic->p[1].p_pixels, 0x80,
605                     p_sys->p_info->sequence->width
606                      * p_sys->p_info->sequence->height / 4 );
607             memset( p_pic->p[2].p_pixels, 0x80,
608                     p_sys->p_info->sequence->width
609                      * p_sys->p_info->sequence->height / 4 );
610
611             if( p_sys->b_slice_i )
612             {
613                 decoder_SynchroNewPicture( p_sys->p_synchro,
614                         I_CODING_TYPE, 2, 0, 0,
615                         p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
616                 decoder_SynchroDecode( p_sys->p_synchro );
617                 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
618             }
619             break;
620         }
621
622         default:
623             break;
624         }
625     }
626
627     /* Never reached */
628     return NULL;
629 }
630
631 /*****************************************************************************
632  * CloseDecoder: libmpeg2 decoder destruction
633  *****************************************************************************/
634 static void CloseDecoder( vlc_object_t *p_this )
635 {
636     decoder_t *p_dec = (decoder_t *)p_this;
637     decoder_sys_t *p_sys = p_dec->p_sys;
638
639     free( p_sys->p_gop_user_data );
640
641     if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
642
643     if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
644
645     free( p_sys );
646 }
647
648 /*****************************************************************************
649  * GetNewPicture: Get a new picture from the vout and set the buf struct
650  *****************************************************************************/
651 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
652 {
653     decoder_sys_t *p_sys = p_dec->p_sys;
654     picture_t *p_pic;
655
656     p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
657     p_dec->fmt_out.video.i_visible_width =
658         p_sys->p_info->sequence->picture_width;
659     p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
660     p_dec->fmt_out.video.i_visible_height =
661         p_sys->p_info->sequence->picture_height;
662     p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
663     p_dec->fmt_out.video.i_sar_num = p_sys->i_sar_num;
664     p_dec->fmt_out.video.i_sar_den = p_sys->i_sar_den;
665
666     if( p_sys->p_info->sequence->frame_period > 0 )
667     {
668         p_dec->fmt_out.video.i_frame_rate =
669             (uint32_t)( (uint64_t)1001000000 * 27 /
670                         p_sys->p_info->sequence->frame_period );
671         p_dec->fmt_out.video.i_frame_rate_base = 1001;
672     }
673
674     p_dec->fmt_out.i_codec =
675         ( p_sys->p_info->sequence->chroma_height <
676           p_sys->p_info->sequence->height ) ?
677         VLC_CODEC_I420 : VLC_CODEC_I422;
678
679     /* Get a new picture */
680     p_pic = decoder_NewPicture( p_dec );
681
682     if( p_pic == NULL ) return NULL;
683
684     p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
685         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
686     p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
687         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
688     p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
689         p_sys->p_info->current_picture->nb_fields : 2;
690
691     decoder_LinkPicture( p_dec, p_pic );
692
693     pp_buf[0] = p_pic->p[0].p_pixels;
694     pp_buf[1] = p_pic->p[1].p_pixels;
695     pp_buf[2] = p_pic->p[2].p_pixels;
696
697     return p_pic;
698 }
699
700 /*****************************************************************************
701  * GetCc: Retrieves the Closed Captions for the CC decoder.
702  *****************************************************************************/
703 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
704 {
705     decoder_sys_t   *p_sys = p_dec->p_sys;
706     block_t         *p_cc = NULL;
707     int i;
708
709     for( i = 0; i < 4; i++ )
710         pb_present[i] = p_sys->cc.pb_present[i];
711
712     if( p_sys->cc.i_data <= 0 )
713         return NULL;
714
715     p_cc = block_New( p_dec, p_sys->cc.i_data);
716     if( p_cc )
717     {
718         memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
719         p_cc->i_dts =
720         p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
721         p_cc->i_flags = ( p_sys->cc.b_reorder  ? p_sys->i_cc_flags : BLOCK_FLAG_TYPE_P ) & ( BLOCK_FLAG_TYPE_I|BLOCK_FLAG_TYPE_P|BLOCK_FLAG_TYPE_B);
722     }
723     cc_Flush( &p_sys->cc );
724     return p_cc;
725 }
726
727 /*****************************************************************************
728  * GetAR: Get aspect ratio
729  *****************************************************************************/
730 static void GetAR( decoder_t *p_dec )
731 {
732     decoder_sys_t *p_sys = p_dec->p_sys;
733
734     /* Check whether the input gave a particular aspect ratio */
735     if( p_dec->fmt_in.video.i_aspect )
736     {
737         p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
738     }
739     else
740     {
741         /* Use the value provided in the MPEG sequence header */
742         if( p_sys->p_info->sequence->pixel_height > 0 )
743         {
744             p_sys->i_aspect =
745                 ((uint64_t)p_sys->p_info->sequence->picture_width) *
746                 p_sys->p_info->sequence->pixel_width *
747                 VOUT_ASPECT_FACTOR /
748                 p_sys->p_info->sequence->picture_height /
749                 p_sys->p_info->sequence->pixel_height;
750             p_sys->i_sar_num = p_sys->p_info->sequence->pixel_width;
751             p_sys->i_sar_den = p_sys->p_info->sequence->pixel_height;
752         }
753         else
754         {
755             /* Invalid aspect, assume 4:3.
756              * This shouldn't happen and if it does it is a bug
757              * in libmpeg2 (likely triggered by an invalid stream) */
758             p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
759             p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 4;
760             p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 3;
761         }
762     }
763
764     msg_Dbg( p_dec, "%dx%d (display %d,%d), aspect %d, sar %i:%i, %u.%03u fps",
765              p_sys->p_info->sequence->picture_width,
766              p_sys->p_info->sequence->picture_height,
767              p_sys->p_info->sequence->display_width,
768              p_sys->p_info->sequence->display_height,
769              p_sys->i_aspect, p_sys->i_sar_num, p_sys->i_sar_den,
770              (uint32_t)((uint64_t)1001000000 * 27 /
771                  p_sys->p_info->sequence->frame_period / 1001),
772              (uint32_t)((uint64_t)1001000000 * 27 /
773                  p_sys->p_info->sequence->frame_period % 1001) );
774 }