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