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