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