vlc_list: prevent name conflict
[vlc.git] / modules / codec / libmpeg2.c
1 /*****************************************************************************
2  * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * NOTA BENE: this module requires the linking against a library which is
27  * known to require licensing under the GNU General Public License version 2
28  * (or later). Therefore, the result of compiling this module will normally
29  * be subject to the terms of that later license.
30  *****************************************************************************/
31
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39 #include <assert.h>
40
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_codec.h>
44 #include <vlc_block_helper.h>
45 #include <vlc_cpu.h>
46 #include "cc.h"
47 #include "synchro.h"
48
49 #include <mpeg2.h>
50
51 /*****************************************************************************
52  * decoder_sys_t : libmpeg2 decoder descriptor
53  *****************************************************************************/
54 #define DPB_COUNT (3+1)
55 typedef struct
56 {
57     picture_t *p_picture;
58     bool      b_linked;
59     bool      b_displayed;
60 } picture_dpb_t;
61
62 typedef struct
63 {
64     /*
65      * libmpeg2 properties
66      */
67     mpeg2dec_t          *p_mpeg2dec;
68     const mpeg2_info_t  *p_info;
69     bool                b_skip;
70
71     /*
72      * Input properties
73      */
74     vlc_tick_t       i_previous_pts;
75     vlc_tick_t       i_current_pts;
76     vlc_tick_t       i_previous_dts;
77     vlc_tick_t       i_current_dts;
78     bool             b_garbage_pic;
79     bool             b_after_sequence_header; /* is it the next frame after
80                                                * the sequence header ?    */
81     bool             b_slice_i;             /* intra-slice refresh stream */
82     bool             b_second_field;
83
84     bool             b_preroll;
85
86     /* */
87     picture_dpb_t        p_dpb[DPB_COUNT];
88
89     /*
90      * Output properties
91      */
92     decoder_synchro_t *p_synchro;
93     int             i_sar_num;
94     int             i_sar_den;
95     vlc_tick_t      i_last_frame_pts;
96
97     /* Closed captioning support */
98     uint32_t        i_cc_flags;
99     vlc_tick_t      i_cc_pts;
100     vlc_tick_t      i_cc_dts;
101 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
102     cc_data_t       cc;
103 #endif
104     uint8_t        *p_gop_user_data;
105     uint32_t        i_gop_user_data;
106 } decoder_sys_t;
107
108 /*****************************************************************************
109  * Local prototypes
110  *****************************************************************************/
111 static int  OpenDecoder( vlc_object_t * );
112 static void CloseDecoder( vlc_object_t * );
113
114 static int DecodeVideo( decoder_t *, block_t *);
115 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
116 static void SendCc( decoder_t *p_dec );
117 #endif
118
119 static picture_t *GetNewPicture( decoder_t * );
120 static void PutPicture( decoder_t *, picture_t * );
121
122 static void GetAR( decoder_t *p_dec );
123
124 static void Reset( decoder_t *p_dec );
125
126 /* */
127 static void DpbInit( decoder_t * );
128 static void DpbClean( decoder_t * );
129 static picture_t *DpbNewPicture( decoder_t * );
130 static void DpbUnlinkPicture( decoder_t *, picture_t * );
131 static int DpbDisplayPicture( decoder_t *, picture_t * );
132
133 /*****************************************************************************
134  * Module descriptor
135  *****************************************************************************/
136 vlc_module_begin ()
137     set_description( N_("MPEG I/II video decoder (using libmpeg2)") )
138     set_capability( "video decoder", 50 )
139     set_category( CAT_INPUT )
140     set_subcategory( SUBCAT_INPUT_VCODEC )
141     set_callbacks( OpenDecoder, CloseDecoder )
142     add_shortcut( "libmpeg2" )
143 vlc_module_end ()
144
145 /*****************************************************************************
146  * OpenDecoder: probe the decoder and return score
147  *****************************************************************************/
148 static int OpenDecoder( vlc_object_t *p_this )
149 {
150     decoder_t *p_dec = (decoder_t*)p_this;
151     decoder_sys_t *p_sys;
152     uint32_t i_accel = 0;
153
154     if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGV )
155         return VLC_EGENERIC;
156
157     /* Select onl recognized original format (standard mpeg video) */
158     switch( p_dec->fmt_in.i_original_fourcc )
159     {
160     case VLC_FOURCC('m','p','g','1'):
161     case VLC_FOURCC('m','p','g','2'):
162     case VLC_FOURCC('m','p','g','v'):
163     case VLC_FOURCC('P','I','M','1'):
164     case VLC_FOURCC('h','d','v','2'):
165         break;
166     default:
167         if( p_dec->fmt_in.i_original_fourcc )
168             return VLC_EGENERIC;
169         break;
170     }
171
172     /* Allocate the memory needed to store the decoder's structure */
173     if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
174         return VLC_ENOMEM;
175
176     /* Initialize the thread properties */
177     p_sys->p_mpeg2dec = NULL;
178     p_sys->p_synchro  = NULL;
179     p_sys->p_info     = NULL;
180     p_sys->i_current_pts  = 0;
181     p_sys->i_previous_pts = 0;
182     p_sys->i_current_dts  = 0;
183     p_sys->i_previous_dts = 0;
184     p_sys->i_sar_num = 0;
185     p_sys->i_sar_den = 0;
186     p_sys->b_garbage_pic = false;
187     p_sys->b_slice_i  = false;
188     p_sys->b_second_field = false;
189     p_sys->b_skip     = false;
190     p_sys->b_preroll = false;
191     DpbInit( p_dec );
192
193     p_sys->i_cc_pts = 0;
194     p_sys->i_cc_dts = 0;
195     p_sys->i_cc_flags = 0;
196 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
197     cc_Init( &p_sys->cc );
198 #endif
199     p_sys->p_gop_user_data = NULL;
200     p_sys->i_gop_user_data = 0;
201
202 #if defined( __i386__ ) || defined( __x86_64__ )
203     if( vlc_CPU_MMX() )
204         i_accel |= MPEG2_ACCEL_X86_MMX;
205     if( vlc_CPU_3dNOW() )
206         i_accel |= MPEG2_ACCEL_X86_3DNOW;
207     if( vlc_CPU_MMXEXT() )
208         i_accel |= MPEG2_ACCEL_X86_MMXEXT;
209 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
210     if( vlc_CPU_ALTIVEC() )
211         i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
212
213 #elif defined(__arm__)
214 # ifdef MPEG2_ACCEL_ARM
215     i_accel |= MPEG2_ACCEL_ARM;
216 # endif
217 # ifdef MPEG2_ACCEL_ARM_NEON
218     if( vlc_CPU_ARM_NEON() )
219         i_accel |= MPEG2_ACCEL_ARM_NEON;
220 # endif
221
222     /* TODO: sparc */
223 #else
224     /* If we do not know this CPU, trust libmpeg2's feature detection */
225     i_accel = MPEG2_ACCEL_DETECT;
226
227 #endif
228
229     /* Set CPU acceleration features */
230     mpeg2_accel( i_accel );
231
232     /* Initialize decoder */
233     p_sys->p_mpeg2dec = mpeg2_init();
234     if( p_sys->p_mpeg2dec == NULL)
235     {
236         msg_Err( p_dec, "mpeg2_init() failed" );
237         free( p_sys );
238         return VLC_EGENERIC;
239     }
240
241     p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
242
243     p_dec->pf_decode = DecodeVideo;
244     p_dec->pf_flush  = Reset;
245     p_dec->fmt_out.i_codec = 0;
246
247     return VLC_SUCCESS;
248 }
249
250 /*****************************************************************************
251  * RunDecoder: the libmpeg2 decoder
252  *****************************************************************************/
253 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
254 {
255     decoder_sys_t   *p_sys = p_dec->p_sys;
256     mpeg2_state_t   state;
257
258     block_t *p_block;
259
260     if( !pp_block || !*pp_block )
261         return NULL;
262
263     p_block = *pp_block;
264     if( p_block->i_flags & (BLOCK_FLAG_CORRUPTED) )
265         Reset( p_dec );
266
267     while( 1 )
268     {
269         state = mpeg2_parse( p_sys->p_mpeg2dec );
270
271         switch( state )
272         {
273         case STATE_SEQUENCE:
274         {
275             /* */
276             DpbClean( p_dec );
277
278             /* */
279             mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
280
281             if( p_sys->p_synchro )
282                 decoder_SynchroRelease( p_sys->p_synchro );
283
284             if( p_sys->p_info->sequence->frame_period <= 0 )
285                 p_sys->p_synchro = NULL;
286             else
287                 p_sys->p_synchro =
288                 decoder_SynchroInit( p_dec, (uint32_t)(UINT64_C(1001000000) *
289                                 27 / p_sys->p_info->sequence->frame_period) );
290             p_sys->b_after_sequence_header = true;
291
292             /* Set the first 2 reference frames */
293             GetAR( p_dec );
294             for( int i = 0; i < 2; i++ )
295             {
296                 picture_t *p_picture = DpbNewPicture( p_dec );
297                 if( !p_picture )
298                 {
299                     Reset( p_dec );
300                     block_Release( p_block );
301                     return NULL;
302                 }
303                 PutPicture( p_dec, p_picture );
304             }
305             break;
306         }
307
308         case STATE_GOP:
309             /* There can be userdata in a GOP. It needs to be remembered for the next picture. */
310             if( p_sys->p_info->user_data_len > 2 )
311             {
312                 free( p_sys->p_gop_user_data );
313                 p_sys->p_gop_user_data = calloc( p_sys->p_info->user_data_len, sizeof(uint8_t) );
314                 if( p_sys->p_gop_user_data )
315                 {
316                     p_sys->i_gop_user_data = p_sys->p_info->user_data_len;
317                     memcpy( p_sys->p_gop_user_data, p_sys->p_info->user_data, p_sys->p_info->user_data_len );
318                 }
319             }
320             break;
321
322         case STATE_PICTURE:
323         {
324             const mpeg2_info_t *p_info = p_sys->p_info;
325             const mpeg2_picture_t *p_current = p_info->current_picture;
326
327             vlc_tick_t i_pts, i_dts;
328
329             if( p_sys->b_after_sequence_header &&
330                 (p_current->flags &
331                     PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P )
332             {
333                 /* Intra-slice refresh. Simulate a blank I picture. */
334                 msg_Dbg( p_dec, "intra-slice refresh stream" );
335                 decoder_SynchroNewPicture( p_sys->p_synchro,
336                                            I_CODING_TYPE, 2, 0, 0,
337                                            p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
338                 decoder_SynchroDecode( p_sys->p_synchro );
339                 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
340                 p_sys->b_slice_i = true;
341             }
342             p_sys->b_after_sequence_header = false;
343
344 #ifdef PIC_FLAG_PTS
345             i_pts = p_current->flags & PIC_FLAG_PTS ?
346                 ( ( p_current->pts ==
347                     (uint32_t)p_sys->i_current_pts ) ?
348                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
349             i_dts = 0;
350
351             /* Hack to handle demuxers which only have DTS timestamps */
352             if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
353             {
354                 if( p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
355                     (p_current->flags &
356                       PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
357                 {
358                     i_pts = p_block->i_dts;
359                 }
360             }
361             p_block->i_pts = p_block->i_dts = 0;
362             /* End hack */
363
364 #else /* New interface */
365
366             i_pts = p_current->flags & PIC_FLAG_TAGS ?
367                 ( ( p_current->tag == (uint32_t)p_sys->i_current_pts ) ?
368                             p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
369             i_dts = p_current->flags & PIC_FLAG_TAGS ?
370                 ( ( p_current->tag2 == (uint32_t)p_sys->i_current_dts ) ?
371                             p_sys->i_current_dts : p_sys->i_previous_dts ) : 0;
372 #endif
373
374             /* If nb_fields == 1, it is a field picture, and it will be
375              * followed by another field picture for which we won't call
376              * decoder_SynchroNewPicture() because this would have other
377              * problems, so we take it into account here.
378              * This kind of sucks, but I didn't think better. --Meuuh
379              */
380             decoder_SynchroNewPicture( p_sys->p_synchro,
381                                        p_current->flags & PIC_MASK_CODING_TYPE,
382                                        p_current->nb_fields == 1 ? 2 :
383                                        p_current->nb_fields, i_pts, i_dts,
384                                        p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
385
386
387             picture_t *p_pic;
388
389             if( p_dec->b_frame_drop_allowed && !p_sys->b_preroll &&
390                 !(p_sys->b_slice_i
391                    && ((p_current->flags
392                          & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P))
393                    && !decoder_SynchroChoose( p_sys->p_synchro,
394                               p_current->flags
395                                 & PIC_MASK_CODING_TYPE,
396                               /*p_sys->p_vout->render_time*/ 0 /*FIXME*/,
397                               p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
398                 p_pic = NULL;
399             else
400             {
401                 p_pic = DpbNewPicture( p_dec );
402                 if( !p_pic )
403                 {
404                     Reset( p_dec );
405
406                     p_pic = DpbNewPicture( p_dec );
407                     if( !p_pic )
408                     {
409                         mpeg2_reset( p_sys->p_mpeg2dec, 1 );
410                         block_Release( p_block );
411                         return NULL;
412                     }
413                 }
414             }
415
416             mpeg2_skip( p_sys->p_mpeg2dec, p_pic == NULL );
417             p_sys->b_skip = p_pic == NULL;
418             if( p_pic != NULL )
419                 decoder_SynchroDecode( p_sys->p_synchro );
420             else
421                 decoder_SynchroTrash( p_sys->p_synchro );
422
423             PutPicture( p_dec, p_pic );
424
425             if( p_info->user_data_len > 2 || p_sys->i_gop_user_data > 2 )
426             {
427                 p_sys->i_cc_pts = i_pts;
428                 p_sys->i_cc_dts = i_dts;
429                 if( (p_current->flags
430                              & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P )
431                     p_sys->i_cc_flags = BLOCK_FLAG_TYPE_P;
432                 else if( (p_current->flags
433                              & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
434                     p_sys->i_cc_flags = BLOCK_FLAG_TYPE_B;
435                 else p_sys->i_cc_flags = BLOCK_FLAG_TYPE_I;
436 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
437                 bool b_top_field_first = p_sys->p_info->current_picture->flags
438                                            & PIC_FLAG_TOP_FIELD_FIRST;
439 #endif
440                 if( p_sys->i_gop_user_data > 2 )
441                 {
442                     /* We now have picture info for any cached user_data out of the gop */
443 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
444                     cc_ProbeAndExtract( &p_sys->cc, b_top_field_first,
445                                 &p_sys->p_gop_user_data[0], p_sys->i_gop_user_data );
446 #endif
447                     p_sys->i_gop_user_data = 0;
448                 }
449
450                 /* Extract the CC from the user_data of the picture */
451 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
452                 if( p_info->user_data_len > 2 )
453                     cc_ProbeAndExtract( &p_sys->cc, b_top_field_first,
454                                 &p_info->user_data[0], p_info->user_data_len );
455
456                 if( p_sys->cc.i_data )
457                     SendCc( p_dec );
458 #endif
459             }
460         }
461         break;
462
463
464         case STATE_BUFFER:
465             if( !p_block->i_buffer )
466             {
467                 block_Release( p_block );
468                 return NULL;
469             }
470
471             if( (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY
472                                       | BLOCK_FLAG_CORRUPTED)) &&
473                 p_sys->p_synchro &&
474                 p_sys->p_info->sequence &&
475                 p_sys->p_info->sequence->width != (unsigned)-1 )
476             {
477                 decoder_SynchroReset( p_sys->p_synchro );
478                 if( p_sys->p_info->current_fbuf != NULL &&
479                     p_sys->p_info->current_fbuf->id != NULL )
480                 {
481                     p_sys->b_garbage_pic = true;
482                 }
483                 if( p_sys->b_slice_i )
484                 {
485                     decoder_SynchroNewPicture( p_sys->p_synchro,
486                                                I_CODING_TYPE, 2, 0, 0,
487                                                p_sys->p_info->sequence->flags &
488                                                             SEQ_FLAG_LOW_DELAY );
489                     decoder_SynchroDecode( p_sys->p_synchro );
490                     decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
491                 }
492             }
493
494             if( p_block->i_flags & BLOCK_FLAG_PREROLL )
495             {
496                 p_sys->b_preroll = true;
497             }
498             else if( p_sys->b_preroll )
499             {
500                 p_sys->b_preroll = false;
501                 if( p_sys->p_synchro )
502                     decoder_SynchroReset( p_sys->p_synchro );
503             }
504
505 #ifdef PIC_FLAG_PTS
506             if( p_block->i_pts )
507             {
508                 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
509
510 #else /* New interface */
511             if( p_block->i_pts || p_block->i_dts )
512             {
513                 mpeg2_tag_picture( p_sys->p_mpeg2dec,
514                                    (uint32_t)p_block->i_pts,
515                                    (uint32_t)p_block->i_dts );
516 #endif
517                 p_sys->i_previous_pts = p_sys->i_current_pts;
518                 p_sys->i_current_pts = p_block->i_pts;
519                 p_sys->i_previous_dts = p_sys->i_current_dts;
520                 p_sys->i_current_dts = p_block->i_dts;
521             }
522
523             mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
524                           p_block->p_buffer + p_block->i_buffer );
525
526             p_block->i_buffer = 0;
527             break;
528
529 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
530
531         case STATE_SEQUENCE_MODIFIED:
532             GetAR( p_dec );
533             break;
534 #endif
535         case STATE_PICTURE_2ND:
536             p_sys->b_second_field = true;
537             break;
538
539
540         case STATE_INVALID_END:
541         case STATE_END:
542         case STATE_SLICE:
543         {
544             picture_t *p_pic = NULL;
545
546             if( p_sys->p_info->display_fbuf &&
547                 p_sys->p_info->display_fbuf->id )
548             {
549                 p_pic = p_sys->p_info->display_fbuf->id;
550                 if( DpbDisplayPicture( p_dec, p_pic ) )
551                     p_pic = NULL;
552
553                 decoder_SynchroEnd( p_sys->p_synchro,
554                                     p_sys->p_info->display_picture->flags & PIC_MASK_CODING_TYPE,
555                                     p_sys->b_garbage_pic );
556
557                 if( p_pic )
558                 {
559                     p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
560                     if( p_sys->b_garbage_pic )
561                         p_pic->date = 0; /* ??? */
562                     p_sys->b_garbage_pic = false;
563                 }
564             }
565
566             if( p_sys->p_info->discard_fbuf &&
567                 p_sys->p_info->discard_fbuf->id )
568             {
569                 DpbUnlinkPicture( p_dec, p_sys->p_info->discard_fbuf->id );
570             }
571
572             if( p_pic )
573             {
574                 if( state == STATE_END )
575                     p_pic->b_force = true; /* For still frames */
576
577                 /* Avoid frames with identical timestamps.
578                  * Especially needed for still frames in DVD menus. */
579                 if( p_sys->i_last_frame_pts == p_pic->date )
580                     p_pic->date++;
581                 p_sys->i_last_frame_pts = p_pic->date;
582                 return p_pic;
583             }
584             break;
585         }
586
587         case STATE_INVALID:
588         {
589             msg_Err( p_dec, "invalid picture encountered" );
590             /* I don't think we have anything to do, but well without
591              * docs ... */
592             break;
593         }
594
595         default:
596             break;
597         }
598     }
599
600     /* Never reached */
601     return NULL;
602 }
603
604 static int DecodeVideo( decoder_t *p_dec, block_t *p_block)
605 {
606     if( p_block == NULL ) /* No Drain */
607         return VLCDEC_SUCCESS;
608
609     block_t **pp_block = &p_block;
610     picture_t *p_pic;
611     while( ( p_pic = DecodeBlock( p_dec, pp_block ) ) != NULL )
612         decoder_QueueVideo( p_dec, p_pic );
613     return VLCDEC_SUCCESS;
614 }
615
616 /*****************************************************************************
617  * CloseDecoder: libmpeg2 decoder destruction
618  *****************************************************************************/
619 static void CloseDecoder( vlc_object_t *p_this )
620 {
621     decoder_t *p_dec = (decoder_t *)p_this;
622     decoder_sys_t *p_sys = p_dec->p_sys;
623
624 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
625     cc_Flush( &p_sys->cc );
626 #endif
627
628     DpbClean( p_dec );
629
630     free( p_sys->p_gop_user_data );
631
632     if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
633
634     if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
635
636     free( p_sys );
637 }
638
639 /*****************************************************************************
640  * Reset: reset the decoder state
641  *****************************************************************************/
642 static void Reset( decoder_t *p_dec )
643 {
644     decoder_sys_t *p_sys = p_dec->p_sys;
645
646 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
647     cc_Flush( &p_sys->cc );
648 #endif
649     mpeg2_reset( p_sys->p_mpeg2dec, 0 );
650     DpbClean( p_dec );
651 }
652
653 /*****************************************************************************
654  * GetNewPicture: Get a new picture from the vout and set the buf struct
655  *****************************************************************************/
656 static picture_t *GetNewPicture( decoder_t *p_dec )
657 {
658     decoder_sys_t *p_sys = p_dec->p_sys;
659     picture_t *p_pic;
660
661     p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
662     p_dec->fmt_out.video.i_visible_width =
663         p_sys->p_info->sequence->picture_width;
664     p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
665     p_dec->fmt_out.video.i_visible_height =
666         p_sys->p_info->sequence->picture_height;
667     p_dec->fmt_out.video.i_sar_num = p_sys->i_sar_num;
668     p_dec->fmt_out.video.i_sar_den = p_sys->i_sar_den;
669
670     if( p_sys->p_info->sequence->frame_period > 0 )
671     {
672         p_dec->fmt_out.video.i_frame_rate =
673             (uint32_t)( (uint64_t)1001000000 * 27 /
674                         p_sys->p_info->sequence->frame_period );
675         p_dec->fmt_out.video.i_frame_rate_base = 1001;
676     }
677
678     p_dec->fmt_out.i_codec =
679         ( p_sys->p_info->sequence->chroma_height <
680           p_sys->p_info->sequence->height ) ?
681         VLC_CODEC_I420 : VLC_CODEC_I422;
682
683     /* Get a new picture */
684     if( decoder_UpdateVideoFormat( p_dec ) )
685         return NULL;
686     p_pic = decoder_NewPicture( p_dec );
687
688     if( p_pic == NULL )
689         return NULL;
690
691     p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
692         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
693     p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
694         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
695     p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
696         p_sys->p_info->current_picture->nb_fields : 2;
697
698     return p_pic;
699 }
700
701 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
702 /*****************************************************************************
703  * SendCc: Sends the Closed Captions for the CC decoder.
704  *****************************************************************************/
705 static void SendCc( decoder_t *p_dec )
706 {
707     decoder_sys_t   *p_sys = p_dec->p_sys;
708     block_t         *p_cc = NULL;
709
710     if( !p_sys->cc.b_reorder && p_sys->cc.i_data <= 0 )
711         return;
712
713     p_cc = block_Alloc( 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->i_cc_flags & BLOCK_FLAG_TYPE_MASK;
720         decoder_cc_desc_t desc;
721         desc.i_608_channels = p_sys->cc.i_608channels;
722         desc.i_708_channels = p_sys->cc.i_708channels;
723         desc.i_reorder_depth = p_sys->cc.b_reorder ? 0 : -1;
724         decoder_QueueCc( p_dec, p_cc, &desc );
725     }
726     cc_Flush( &p_sys->cc );
727     return;
728 }
729 #endif
730
731 /*****************************************************************************
732  * GetAR: Get aspect ratio
733  *****************************************************************************/
734 static void GetAR( decoder_t *p_dec )
735 {
736     decoder_sys_t *p_sys = p_dec->p_sys;
737     int i_old_sar_num = p_sys->i_sar_num;
738     int i_old_sar_den = p_sys->i_sar_den;
739
740     /* Check whether the input gave a particular aspect ratio */
741     if( p_dec->fmt_in.video.i_sar_num > 0 &&
742         p_dec->fmt_in.video.i_sar_den > 0 )
743     {
744         p_sys->i_sar_num = p_dec->fmt_in.video.i_sar_num;
745         p_sys->i_sar_den = p_dec->fmt_in.video.i_sar_den;
746     }
747     /* Use the value provided in the MPEG sequence header */
748     else if( p_sys->p_info->sequence->pixel_height > 0 )
749     {
750         p_sys->i_sar_num = p_sys->p_info->sequence->pixel_width;
751         p_sys->i_sar_den = p_sys->p_info->sequence->pixel_height;
752     }
753     else
754     {
755         /* Invalid aspect, assume 4:3.
756          * This shouldn't happen and if it does it is a bug
757          * in libmpeg2 (likely triggered by an invalid stream) */
758         p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 4;
759         p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 3;
760     }
761
762     if( p_sys->i_sar_num == i_old_sar_num &&
763         p_sys->i_sar_den == i_old_sar_den )
764         return;
765
766     if( p_sys->p_info->sequence->frame_period > 0 )
767         msg_Dbg( p_dec,
768                  "%dx%d (display %d,%d), sar %i:%i, %u.%03u fps",
769                  p_sys->p_info->sequence->picture_width,
770                  p_sys->p_info->sequence->picture_height,
771                  p_sys->p_info->sequence->display_width,
772                  p_sys->p_info->sequence->display_height,
773                  p_sys->i_sar_num, p_sys->i_sar_den,
774                  (uint32_t)((uint64_t)1001000000 * 27 /
775                      p_sys->p_info->sequence->frame_period / 1001),
776                  (uint32_t)((uint64_t)1001000000 * 27 /
777                      p_sys->p_info->sequence->frame_period % 1001) );
778     else
779         msg_Dbg( p_dec, "bad frame period" );
780 }
781
782 /*****************************************************************************
783  * PutPicture: Put a picture_t in mpeg2 context
784  *****************************************************************************/
785 static void PutPicture( decoder_t *p_dec, picture_t *p_picture )
786 {
787     decoder_sys_t *p_sys = p_dec->p_sys;
788
789     /* */
790     uint8_t *pp_buf[3];
791     for( int j = 0; j < 3; j++ )
792         pp_buf[j] = p_picture ? p_picture->p[j].p_pixels : NULL;
793     mpeg2_set_buf( p_sys->p_mpeg2dec, pp_buf, p_picture );
794
795     /* Completly broken API, why the hell does it suppose
796      * the stride of the chroma planes ! */
797     if( p_picture )
798         mpeg2_stride( p_sys->p_mpeg2dec, p_picture->p[Y_PLANE].i_pitch );
799 }
800
801
802 /**
803  * Initialize a virtual Decoded Picture Buffer to workaround
804  * libmpeg2 deficient API
805  */
806 static void DpbInit( decoder_t *p_dec )
807 {
808     decoder_sys_t *p_sys = p_dec->p_sys;
809
810     for( int i = 0; i < DPB_COUNT; i++ )
811         p_sys->p_dpb[i].p_picture = NULL;
812 }
813 /**
814  * Empty and reset the current DPB
815  */
816 static void DpbClean( decoder_t *p_dec )
817 {
818     decoder_sys_t *p_sys = p_dec->p_sys;
819
820     for( int i = 0; i < DPB_COUNT; i++ )
821     {
822         picture_dpb_t *p = &p_sys->p_dpb[i];
823         if( !p->p_picture )
824             continue;
825         if( p->b_linked )
826             picture_Release( p->p_picture );
827         if( !p->b_displayed )
828             picture_Release( p->p_picture );
829
830         p->p_picture = NULL;
831     }
832 }
833 /**
834  * Retreive a picture and reserve a place in the DPB
835  */
836 static picture_t *DpbNewPicture( decoder_t *p_dec )
837 {
838     decoder_sys_t *p_sys = p_dec->p_sys;
839
840     picture_dpb_t *p;
841     int i;
842
843     for( i = 0; i < DPB_COUNT; i++ )
844     {
845         p = &p_sys->p_dpb[i];
846         if( !p->p_picture )
847             break;
848     }
849     if( i >= DPB_COUNT )
850     {
851         msg_Err( p_dec, "Leaking picture" );
852         return NULL;
853     }
854
855     p->p_picture = GetNewPicture( p_dec );
856     if( p->p_picture )
857     {
858         picture_Hold( p->p_picture );
859         p->b_linked = true;
860         p->b_displayed = false;
861
862         p->p_picture->date = 0;
863     }
864     return p->p_picture;
865 }
866 static picture_dpb_t *DpbFindPicture( decoder_t *p_dec, picture_t *p_picture )
867 {
868     decoder_sys_t *p_sys = p_dec->p_sys;
869
870     for( int i = 0; i < DPB_COUNT; i++ )
871     {
872         picture_dpb_t *p = &p_sys->p_dpb[i];
873         if( p->p_picture == p_picture )
874             return p;
875     }
876     return NULL;
877 }
878 /**
879  * Unlink the provided picture and ensure that the decoder
880  * does not own it anymore.
881  */
882 static void DpbUnlinkPicture( decoder_t *p_dec, picture_t *p_picture )
883 {
884     picture_dpb_t *p = DpbFindPicture( p_dec, p_picture );
885
886     /* XXX it is needed to workaround libmpeg2 bugs */
887     if( !p || !p->b_linked )
888     {
889         msg_Err( p_dec, "DpbUnlinkPicture called on an invalid picture" );
890         return;
891     }
892
893     assert( p && p->b_linked );
894
895     picture_Release( p->p_picture );
896     p->b_linked = false;
897
898     if( !p->b_displayed )
899         picture_Release( p->p_picture );
900     p->p_picture = NULL;
901 }
902 /**
903  * Mark the provided picture as displayed.
904  */
905 static int DpbDisplayPicture( decoder_t *p_dec, picture_t *p_picture )
906 {
907     picture_dpb_t *p = DpbFindPicture( p_dec, p_picture );
908
909     /* XXX it is needed to workaround libmpeg2 bugs */
910     if( !p || p->b_displayed || !p->b_linked )
911     {
912         msg_Err( p_dec, "DpbDisplayPicture called on an invalid picture" );
913         return VLC_EGENERIC;
914     }
915
916     assert( p && !p->b_displayed && p->b_linked );
917
918     p->b_displayed = true;
919     return VLC_SUCCESS;
920 }
921
922