]> git.sesse.net Git - vlc/blob - modules/codec/libmpeg2.c
429f10f27393270ba3a043eda10c07f1296b565e
[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
427             bool b_skip = false;
428             if( !p_dec->b_pace_control && !p_sys->b_preroll &&
429                 !(p_sys->b_slice_i
430                    && ((p_sys->p_info->current_picture->flags
431                          & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P))
432                    && !decoder_SynchroChoose( p_sys->p_synchro,
433                               p_sys->p_info->current_picture->flags
434                                 & PIC_MASK_CODING_TYPE,
435                               /*p_sys->p_vout->render_time*/ 0 /*FIXME*/,
436                               p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
437             {
438                 b_skip = true;
439             }
440
441             p_pic = NULL;
442             if( !b_skip )
443                 p_pic = GetNewPicture( p_dec, buf );
444
445             if( b_skip || !p_pic )
446             {
447                 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
448                 p_sys->b_skip = 1;
449                 decoder_SynchroTrash( p_sys->p_synchro );
450                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
451
452                 if( !b_skip )
453                 {
454                     block_Release( p_block );
455                     return NULL;
456                 }
457             }
458             else
459             {
460                 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
461                 p_sys->b_skip = 0;
462                 decoder_SynchroDecode( p_sys->p_synchro );
463
464                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
465                 mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
466             }
467             if( p_sys->p_info->user_data_len > 2 )
468             {
469                 p_sys->i_cc_pts = i_pts;
470                 p_sys->i_cc_dts = i_dts;
471                 if( (p_sys->p_info->current_picture->flags
472                              & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P )
473                     p_sys->i_cc_flags = BLOCK_FLAG_TYPE_P;
474                 else if( (p_sys->p_info->current_picture->flags
475                              & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
476                     p_sys->i_cc_flags = BLOCK_FLAG_TYPE_B;
477                 else p_sys->i_cc_flags = BLOCK_FLAG_TYPE_I;
478
479                 cc_Extract( &p_sys->cc, &p_sys->p_info->user_data[0], p_sys->p_info->user_data_len );
480             }
481         }
482         break;
483
484         case STATE_END:
485         case STATE_SLICE:
486             p_pic = NULL;
487             if( p_sys->p_info->display_fbuf
488                 && p_sys->p_info->display_fbuf->id )
489             {
490                 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
491
492                 decoder_SynchroEnd( p_sys->p_synchro,
493                             p_sys->p_info->display_picture->flags
494                              & PIC_MASK_CODING_TYPE,
495                             p_sys->b_garbage_pic );
496                 p_sys->b_garbage_pic = 0;
497
498                 if( p_sys->p_picture_to_destroy != p_pic )
499                 {
500                     p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
501                 }
502                 else
503                 {
504                     p_sys->p_picture_to_destroy = NULL;
505                     p_pic->date = 0;
506                 }
507             }
508
509             if( p_sys->p_info->discard_fbuf &&
510                 p_sys->p_info->discard_fbuf->id )
511             {
512                 p_dec->pf_picture_unlink( p_dec,
513                                           p_sys->p_info->discard_fbuf->id );
514             }
515
516             /* For still frames */
517             if( state == STATE_END && p_pic ) p_pic->b_force = true;
518
519             if( p_pic )
520             {
521                 /* Avoid frames with identical timestamps.
522                  * Especially needed for still frames in DVD menus. */
523                 if( p_sys->i_last_frame_pts == p_pic->date ) p_pic->date++;
524                 p_sys->i_last_frame_pts = p_pic->date;
525
526                 return p_pic;
527             }
528             break;
529
530         case STATE_INVALID:
531         {
532             uint8_t *buf[3];
533             buf[0] = buf[1] = buf[2] = NULL;
534
535             msg_Warn( p_dec, "invalid picture encountered" );
536             if ( ( p_sys->p_info->current_picture == NULL ) ||
537                ( ( p_sys->p_info->current_picture->flags &
538                    PIC_MASK_CODING_TYPE) != PIC_FLAG_CODING_TYPE_B ) )
539             {
540                 if( p_sys->p_synchro ) decoder_SynchroReset( p_sys->p_synchro );
541             }
542             mpeg2_skip( p_sys->p_mpeg2dec, 1 );
543             p_sys->b_skip = 1;
544             cc_Flush( &p_sys->cc );
545
546             if( p_sys->p_info->current_fbuf &&
547                 p_sys->p_info->current_fbuf->id )
548             {
549                 p_sys->b_garbage_pic = 1;
550                 p_pic = p_sys->p_info->current_fbuf->id;
551             }
552             else if( !p_sys->p_info->sequence )
553             {
554                 break;
555             }
556             else
557             {
558                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
559                     break;
560                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
561                 mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
562             }
563             p_sys->p_picture_to_destroy = p_pic;
564
565             memset( p_pic->p[0].p_pixels, 0,
566                     p_sys->p_info->sequence->width
567                      * p_sys->p_info->sequence->height );
568             memset( p_pic->p[1].p_pixels, 0x80,
569                     p_sys->p_info->sequence->width
570                      * p_sys->p_info->sequence->height / 4 );
571             memset( p_pic->p[2].p_pixels, 0x80,
572                     p_sys->p_info->sequence->width
573                      * p_sys->p_info->sequence->height / 4 );
574
575             if( p_sys->b_slice_i )
576             {
577                 decoder_SynchroNewPicture( p_sys->p_synchro,
578                         I_CODING_TYPE, 2, 0, 0,
579                         p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
580                 decoder_SynchroDecode( p_sys->p_synchro );
581                 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
582             }
583             break;
584         }
585
586         default:
587             break;
588         }
589     }
590
591     /* Never reached */
592     return NULL;
593 }
594
595 /*****************************************************************************
596  * CloseDecoder: libmpeg2 decoder destruction
597  *****************************************************************************/
598 static void CloseDecoder( vlc_object_t *p_this )
599 {
600     decoder_t *p_dec = (decoder_t *)p_this;
601     decoder_sys_t *p_sys = p_dec->p_sys;
602
603     if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
604
605     if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
606
607     free( p_sys );
608 }
609
610 /*****************************************************************************
611  * GetNewPicture: Get a new picture from the vout and set the buf struct
612  *****************************************************************************/
613 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
614 {
615     decoder_sys_t *p_sys = p_dec->p_sys;
616     picture_t *p_pic;
617
618     p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
619     p_dec->fmt_out.video.i_visible_width =
620         p_sys->p_info->sequence->picture_width;
621     p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
622     p_dec->fmt_out.video.i_visible_height =
623         p_sys->p_info->sequence->picture_height;
624     p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
625     p_dec->fmt_out.video.i_sar_num = p_sys->i_sar_num;
626     p_dec->fmt_out.video.i_sar_den = p_sys->i_sar_den;
627
628     if( p_sys->p_info->sequence->frame_period > 0 )
629     {
630         p_dec->fmt_out.video.i_frame_rate =
631             (uint32_t)( (uint64_t)1001000000 * 27 /
632                         p_sys->p_info->sequence->frame_period );
633         p_dec->fmt_out.video.i_frame_rate_base = 1001;
634     }
635
636     p_dec->fmt_out.i_codec =
637         ( p_sys->p_info->sequence->chroma_height <
638           p_sys->p_info->sequence->height ) ?
639         VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
640
641     /* Get a new picture */
642     p_pic = p_dec->pf_vout_buffer_new( p_dec );
643
644     if( p_pic == NULL ) return NULL;
645
646     p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
647         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
648     p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
649         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
650     p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
651         p_sys->p_info->current_picture->nb_fields : 2;
652
653     p_dec->pf_picture_link( p_dec, p_pic );
654
655     pp_buf[0] = p_pic->p[0].p_pixels;
656     pp_buf[1] = p_pic->p[1].p_pixels;
657     pp_buf[2] = p_pic->p[2].p_pixels;
658
659     return p_pic;
660 }
661
662 /*****************************************************************************
663  * GetCc: Retrieves the Closed Captions for the CC decoder.
664  *****************************************************************************/
665 static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
666 {
667     decoder_sys_t   *p_sys = p_dec->p_sys;
668     block_t         *p_cc = NULL;
669     int i;
670
671     for( i = 0; i < 4; i++ )
672         pb_present[i] = p_sys->cc.pb_present[i];
673
674     if( p_sys->cc.i_data <= 0 )
675         return NULL;
676
677     p_cc = block_New( p_dec, p_sys->cc.i_data);
678     if( p_cc )
679     {
680         memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
681         p_cc->i_dts =
682         p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
683         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);
684     }
685     cc_Flush( &p_sys->cc );
686     return p_cc;
687 }
688
689 /*****************************************************************************
690  * GetAR: Get aspect ratio
691  *****************************************************************************/
692 static void GetAR( decoder_t *p_dec )
693 {
694     decoder_sys_t *p_sys = p_dec->p_sys;
695
696     /* Check whether the input gave a particular aspect ratio */
697     if( p_dec->fmt_in.video.i_aspect )
698     {
699         p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
700     }
701     else
702     {
703         /* Use the value provided in the MPEG sequence header */
704         if( p_sys->p_info->sequence->pixel_height > 0 )
705         {
706             p_sys->i_aspect =
707                 ((uint64_t)p_sys->p_info->sequence->picture_width) *
708                 p_sys->p_info->sequence->pixel_width *
709                 VOUT_ASPECT_FACTOR /
710                 p_sys->p_info->sequence->picture_height /
711                 p_sys->p_info->sequence->pixel_height;
712             p_sys->i_sar_num = p_sys->p_info->sequence->pixel_width;
713             p_sys->i_sar_den = p_sys->p_info->sequence->pixel_height;
714         }
715         else
716         {
717             /* Invalid aspect, assume 4:3.
718              * This shouldn't happen and if it does it is a bug
719              * in libmpeg2 (likely triggered by an invalid stream) */
720             p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
721             p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 4;
722             p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 3;
723         }
724     }
725
726     msg_Dbg( p_dec, "%dx%d (display %d,%d), aspect %d, sar %i:%i, %u.%03u fps",
727              p_sys->p_info->sequence->picture_width,
728              p_sys->p_info->sequence->picture_height,
729              p_sys->p_info->sequence->display_width,
730              p_sys->p_info->sequence->display_height,
731              p_sys->i_aspect, p_sys->i_sar_num, p_sys->i_sar_den,
732              (uint32_t)((uint64_t)1001000000 * 27 /
733                  p_sys->p_info->sequence->frame_period / 1001),
734              (uint32_t)((uint64_t)1001000000 * 27 /
735                  p_sys->p_info->sequence->frame_period % 1001) );
736 }