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