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