libmpeg2.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:29k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2001 the VideoLAN team
  5.  * $Id: c55a243c27fb75f94936373dbf8bc773c3263cd8 $
  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.  * Preamble
  26.  *****************************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. # include "config.h"
  29. #endif
  30. #include <assert.h>
  31. #include <vlc_common.h>
  32. #include <vlc_plugin.h>
  33. #include <vlc_vout.h>
  34. #include <vlc_codec.h>
  35. #include <vlc_block_helper.h>
  36. #include "../codec/cc.h"
  37. #include <mpeg2.h>
  38. #include <vlc_codec_synchro.h>
  39. /*****************************************************************************
  40.  * decoder_sys_t : libmpeg2 decoder descriptor
  41.  *****************************************************************************/
  42. #define DPB_COUNT (3+1)
  43. typedef struct
  44. {
  45.     picture_t *p_picture;
  46.     bool      b_linked;
  47.     bool      b_displayed;
  48. } picture_dpb_t;
  49. struct decoder_sys_t
  50. {
  51.     /*
  52.      * libmpeg2 properties
  53.      */
  54.     mpeg2dec_t          *p_mpeg2dec;
  55.     const mpeg2_info_t  *p_info;
  56.     bool                b_skip;
  57.     /*
  58.      * Input properties
  59.      */
  60.     mtime_t          i_previous_pts;
  61.     mtime_t          i_current_pts;
  62.     mtime_t          i_previous_dts;
  63.     mtime_t          i_current_dts;
  64.     bool             b_garbage_pic;
  65.     bool             b_after_sequence_header; /* is it the next frame after
  66.                                                * the sequence header ?    */
  67.     bool             b_slice_i;             /* intra-slice refresh stream */
  68.     bool             b_second_field;
  69.     bool             b_preroll;
  70.     /* */
  71.     picture_dpb_t        p_dpb[DPB_COUNT];
  72.     /*
  73.      * Output properties
  74.      */
  75.     decoder_synchro_t *p_synchro;
  76.     int             i_aspect;
  77.     int             i_sar_num;
  78.     int             i_sar_den;
  79.     mtime_t         i_last_frame_pts;
  80.     /* Closed captioning support */
  81.     uint32_t        i_cc_flags;
  82.     mtime_t         i_cc_pts;
  83.     mtime_t         i_cc_dts;
  84.     cc_data_t       cc;
  85.     uint8_t        *p_gop_user_data;
  86.     uint32_t        i_gop_user_data;
  87. };
  88. /*****************************************************************************
  89.  * Local prototypes
  90.  *****************************************************************************/
  91. static int  OpenDecoder( vlc_object_t * );
  92. static void CloseDecoder( vlc_object_t * );
  93. static picture_t *DecodeBlock( decoder_t *, block_t ** );
  94. static block_t   *GetCc( decoder_t *p_dec, bool pb_present[4] );
  95. static picture_t *GetNewPicture( decoder_t * );
  96. static void PutPicture( decoder_t *, picture_t * );
  97. static void GetAR( decoder_t *p_dec );
  98. static void Reset( decoder_t *p_dec );
  99. /* */
  100. static void DpbInit( decoder_t * );
  101. static void DpbClean( decoder_t * );
  102. static picture_t *DpbNewPicture( decoder_t * );
  103. static void DpbUnlinkPicture( decoder_t *, picture_t * );
  104. static int DpbDisplayPicture( decoder_t *, picture_t * );
  105. /*****************************************************************************
  106.  * Module descriptor
  107.  *****************************************************************************/
  108. vlc_module_begin ()
  109.     set_description( N_("MPEG I/II video decoder (using libmpeg2)") )
  110.     set_capability( "decoder", 150 )
  111.     set_category( CAT_INPUT )
  112.     set_subcategory( SUBCAT_INPUT_VCODEC )
  113.     set_callbacks( OpenDecoder, CloseDecoder )
  114.     add_shortcut( "libmpeg2" )
  115. vlc_module_end ()
  116. /*****************************************************************************
  117.  * OpenDecoder: probe the decoder and return score
  118.  *****************************************************************************/
  119. static int OpenDecoder( vlc_object_t *p_this )
  120. {
  121.     decoder_t *p_dec = (decoder_t*)p_this;
  122.     decoder_sys_t *p_sys;
  123.     uint32_t i_accel = 0;
  124.     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','v') &&
  125.         p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','1') &&
  126.         /* Pinnacle hardware-mpeg1 */
  127.         p_dec->fmt_in.i_codec != VLC_FOURCC('P','I','M','1') &&
  128.         p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','2','v') &&
  129.         p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','2') &&
  130.         p_dec->fmt_in.i_codec != VLC_FOURCC('h','d','v','2') )
  131.     {
  132.         return VLC_EGENERIC;
  133.     }
  134.     /* Allocate the memory needed to store the decoder's structure */
  135.     if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
  136.         return VLC_ENOMEM;
  137.     /* Initialize the thread properties */
  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->i_aspect = 0;
  146.     p_sys->b_garbage_pic = false;
  147.     p_sys->b_slice_i  = false;
  148.     p_sys->b_second_field = false;
  149.     p_sys->b_skip     = false;
  150.     p_sys->b_preroll = false;
  151.     DpbInit( p_dec );
  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.     p_sys->p_gop_user_data = NULL;
  160.     p_sys->i_gop_user_data = 0;
  161. #if defined( __i386__ ) || defined( __x86_64__ )
  162.     if( vlc_CPU() & CPU_CAPABILITY_MMX )
  163.     {
  164.         i_accel |= MPEG2_ACCEL_X86_MMX;
  165.     }
  166.     if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
  167.     {
  168.         i_accel |= MPEG2_ACCEL_X86_3DNOW;
  169.     }
  170.     if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
  171.     {
  172.         i_accel |= MPEG2_ACCEL_X86_MMXEXT;
  173.     }
  174. #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
  175.     if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
  176.     {
  177.         i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
  178.     }
  179. #else
  180.     /* If we do not know this CPU, trust libmpeg2's feature detection */
  181.     i_accel = MPEG2_ACCEL_DETECT;
  182. #endif
  183.     /* Set CPU acceleration features */
  184.     mpeg2_accel( i_accel );
  185.     /* Initialize decoder */
  186.     p_sys->p_mpeg2dec = mpeg2_init();
  187.     if( p_sys->p_mpeg2dec == NULL)
  188.     {
  189.         msg_Err( p_dec, "mpeg2_init() failed" );
  190.         free( p_sys );
  191.         return VLC_EGENERIC;
  192.     }
  193.     p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
  194.     p_dec->pf_decode_video = DecodeBlock;
  195.     p_dec->fmt_out.i_cat = VIDEO_ES;
  196.     p_dec->fmt_out.i_codec = 0;
  197.     return VLC_SUCCESS;
  198. }
  199. /*****************************************************************************
  200.  * RunDecoder: the libmpeg2 decoder
  201.  *****************************************************************************/
  202. static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
  203. {
  204.     decoder_sys_t   *p_sys = p_dec->p_sys;
  205.     mpeg2_state_t   state;
  206.     picture_t       *p_pic;
  207.     block_t *p_block;
  208.     if( !pp_block || !*pp_block )
  209.         return NULL;
  210.     p_block = *pp_block;
  211.     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED) )
  212.         Reset( p_dec );
  213.     while( 1 )
  214.     {
  215.         state = mpeg2_parse( p_sys->p_mpeg2dec );
  216.         switch( state )
  217.         {
  218.         case STATE_SEQUENCE:
  219.         {
  220.             /* */
  221.             DpbClean( p_dec );
  222.             /* */
  223.             mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
  224.             /* Set the first 2 reference frames */
  225.             p_sys->i_aspect = 0;
  226.             GetAR( p_dec );
  227.             for( int i = 0; i < 2; i++ )
  228.             {
  229.                 picture_t *p_picture = DpbNewPicture( p_dec );
  230.                 if( !p_picture )
  231.                 {
  232.                     Reset( p_dec );
  233.                     block_Release( p_block );
  234.                     return NULL;
  235.                 }
  236.                 PutPicture( p_dec, p_picture );
  237.             }
  238.             if( p_sys->p_synchro )
  239.                 decoder_SynchroRelease( p_sys->p_synchro );
  240.             if( p_sys->p_info->sequence->frame_period <= 0 )
  241.                 p_sys->p_synchro = NULL;
  242.             else
  243.                 p_sys->p_synchro =
  244.                 decoder_SynchroInit( p_dec, (uint32_t)(UINT64_C(1001000000) *
  245.                                 27 / p_sys->p_info->sequence->frame_period) );
  246.             p_sys->b_after_sequence_header = true;
  247.             break;
  248.         }
  249.         case STATE_GOP:
  250.             /* There can be userdata in a GOP. It needs to be remembered for the next picture. */
  251.             if( p_sys->p_info->user_data_len > 2 )
  252.             {
  253.                 free( p_sys->p_gop_user_data );
  254.                 p_sys->p_gop_user_data = calloc( p_sys->p_info->user_data_len, sizeof(uint8_t) );
  255.                 if( p_sys->p_gop_user_data )
  256.                 {
  257.                     p_sys->i_gop_user_data = p_sys->p_info->user_data_len;
  258.                     memcpy( p_sys->p_gop_user_data, p_sys->p_info->user_data, p_sys->p_info->user_data_len );
  259.                 }
  260.             }
  261.             break;
  262.         case STATE_PICTURE:
  263.         {
  264.             const mpeg2_info_t *p_info = p_sys->p_info;
  265.             const mpeg2_picture_t *p_current = p_info->current_picture;
  266.             mtime_t i_pts, i_dts;
  267.             if( p_sys->b_after_sequence_header &&
  268.                 (p_current->flags &
  269.                     PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P )
  270.             {
  271.                 /* Intra-slice refresh. Simulate a blank I picture. */
  272.                 msg_Dbg( p_dec, "intra-slice refresh stream" );
  273.                 decoder_SynchroNewPicture( p_sys->p_synchro,
  274.                                            I_CODING_TYPE, 2, 0, 0,
  275.                                            p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
  276.                 decoder_SynchroDecode( p_sys->p_synchro );
  277.                 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
  278.                 p_sys->b_slice_i = true;
  279.             }
  280.             p_sys->b_after_sequence_header = false;
  281. #ifdef PIC_FLAG_PTS
  282.             i_pts = p_current->flags & PIC_FLAG_PTS ?
  283.                 ( ( p_current->pts ==
  284.                     (uint32_t)p_sys->i_current_pts ) ?
  285.                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
  286.             i_dts = 0;
  287.             /* Hack to handle demuxers which only have DTS timestamps */
  288.             if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
  289.             {
  290.                 if( p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
  291.                     (p_current->flags &
  292.                       PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
  293.                 {
  294.                     i_pts = p_block->i_dts;
  295.                 }
  296.             }
  297.             p_block->i_pts = p_block->i_dts = 0;
  298.             /* End hack */
  299. #else /* New interface */
  300.             i_pts = p_current->flags & PIC_FLAG_TAGS ?
  301.                 ( ( p_current->tag == (uint32_t)p_sys->i_current_pts ) ?
  302.                             p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
  303.             i_dts = p_current->flags & PIC_FLAG_TAGS ?
  304.                 ( ( p_current->tag2 == (uint32_t)p_sys->i_current_dts ) ?
  305.                             p_sys->i_current_dts : p_sys->i_previous_dts ) : 0;
  306. #endif
  307.             /* If nb_fields == 1, it is a field picture, and it will be
  308.              * followed by another field picture for which we won't call
  309.              * decoder_SynchroNewPicture() because this would have other
  310.              * problems, so we take it into account here.
  311.              * This kind of sucks, but I didn't think better. --Meuuh
  312.              */
  313.             decoder_SynchroNewPicture( p_sys->p_synchro,
  314.                                        p_current->flags & PIC_MASK_CODING_TYPE,
  315.                                        p_current->nb_fields == 1 ? 2 :
  316.                                        p_current->nb_fields, i_pts, i_dts,
  317.                                        p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
  318.             bool b_skip = false;
  319.             if( !p_dec->b_pace_control && !p_sys->b_preroll &&
  320.                 !(p_sys->b_slice_i
  321.                    && ((p_current->flags
  322.                          & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P))
  323.                    && !decoder_SynchroChoose( p_sys->p_synchro,
  324.                               p_current->flags
  325.                                 & PIC_MASK_CODING_TYPE,
  326.                               /*p_sys->p_vout->render_time*/ 0 /*FIXME*/,
  327.                               p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
  328.             {
  329.                 b_skip = true;
  330.             }
  331.             p_pic = NULL;
  332.             if( !b_skip )
  333.             {
  334.                 p_pic = DpbNewPicture( p_dec );
  335.                 if( !p_pic )
  336.                 {
  337.                     Reset( p_dec );
  338.                     p_pic = DpbNewPicture( p_dec );
  339.                     if( !p_pic )
  340.                     {
  341.                         mpeg2_reset( p_sys->p_mpeg2dec, 1 );
  342.                         block_Release( p_block );
  343.                         return NULL;
  344.                     }
  345.                 }
  346.             }
  347.             if( b_skip || !p_pic )
  348.             {
  349.                 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
  350.                 p_sys->b_skip = true;
  351.                 decoder_SynchroTrash( p_sys->p_synchro );
  352.                 PutPicture( p_dec, NULL );
  353.                 if( !b_skip )
  354.                 {
  355.                     block_Release( p_block );
  356.                     return NULL;
  357.                 }
  358.             }
  359.             else
  360.             {
  361.                 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
  362.                 p_sys->b_skip = false;
  363.                 decoder_SynchroDecode( p_sys->p_synchro );
  364.                 PutPicture( p_dec, p_pic );
  365.             }
  366.             if( p_info->user_data_len > 2 || p_sys->i_gop_user_data > 2 )
  367.             {
  368.                 p_sys->i_cc_pts = i_pts;
  369.                 p_sys->i_cc_dts = i_dts;
  370.                 if( (p_current->flags
  371.                              & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P )
  372.                     p_sys->i_cc_flags = BLOCK_FLAG_TYPE_P;
  373.                 else if( (p_current->flags
  374.                              & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
  375.                     p_sys->i_cc_flags = BLOCK_FLAG_TYPE_B;
  376.                 else p_sys->i_cc_flags = BLOCK_FLAG_TYPE_I;
  377.                 if( p_sys->i_gop_user_data > 2 )
  378.                 {
  379.                     /* We now have picture info for any cached user_data out of the gop */
  380.                     cc_Extract( &p_sys->cc, &p_sys->p_gop_user_data[0], p_sys->i_gop_user_data );
  381.                     p_sys->i_gop_user_data = 0;
  382.                 }
  383.                 /* Extract the CC from the user_data of the picture */
  384.                 if( p_info->user_data_len > 2 )
  385.                     cc_Extract( &p_sys->cc, &p_info->user_data[0], p_info->user_data_len );
  386.             }
  387.         }
  388.         break;
  389.         case STATE_BUFFER:
  390.             if( !p_block->i_buffer )
  391.             {
  392.                 block_Release( p_block );
  393.                 return NULL;
  394.             }
  395.             if( (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY
  396.                                       | BLOCK_FLAG_CORRUPTED)) &&
  397.                 p_sys->p_synchro &&
  398.                 p_sys->p_info->sequence &&
  399.                 p_sys->p_info->sequence->width != (unsigned)-1 )
  400.             {
  401.                 decoder_SynchroReset( p_sys->p_synchro );
  402.                 if( p_sys->p_info->current_fbuf != NULL &&
  403.                     p_sys->p_info->current_fbuf->id != NULL )
  404.                 {
  405.                     p_sys->b_garbage_pic = true;
  406.                 }
  407.                 if( p_sys->b_slice_i )
  408.                 {
  409.                     decoder_SynchroNewPicture( p_sys->p_synchro,
  410.                                                I_CODING_TYPE, 2, 0, 0,
  411.                                                p_sys->p_info->sequence->flags &
  412.                                                             SEQ_FLAG_LOW_DELAY );
  413.                     decoder_SynchroDecode( p_sys->p_synchro );
  414.                     decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
  415.                 }
  416.             }
  417.             if( p_block->i_flags & BLOCK_FLAG_PREROLL )
  418.             {
  419.                 p_sys->b_preroll = true;
  420.             }
  421.             else if( p_sys->b_preroll )
  422.             {
  423.                 p_sys->b_preroll = false;
  424.                 if( p_sys->p_synchro )
  425.                     decoder_SynchroReset( p_sys->p_synchro );
  426.             }
  427. #ifdef PIC_FLAG_PTS
  428.             if( p_block->i_pts )
  429.             {
  430.                 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
  431. #else /* New interface */
  432.             if( p_block->i_pts || p_block->i_dts )
  433.             {
  434.                 mpeg2_tag_picture( p_sys->p_mpeg2dec,
  435.                                    (uint32_t)p_block->i_pts,
  436.                                    (uint32_t)p_block->i_dts );
  437. #endif
  438.                 p_sys->i_previous_pts = p_sys->i_current_pts;
  439.                 p_sys->i_current_pts = p_block->i_pts;
  440.                 p_sys->i_previous_dts = p_sys->i_current_dts;
  441.                 p_sys->i_current_dts = p_block->i_dts;
  442.             }
  443.             mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
  444.                           p_block->p_buffer + p_block->i_buffer );
  445.             p_block->i_buffer = 0;
  446.             break;
  447. #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
  448.         case STATE_SEQUENCE_MODIFIED:
  449.             GetAR( p_dec );
  450.             break;
  451. #endif
  452.         case STATE_PICTURE_2ND:
  453.             p_sys->b_second_field = true;
  454.             break;
  455.         case STATE_INVALID_END:
  456.         case STATE_END:
  457.         case STATE_SLICE:
  458.             p_pic = NULL;
  459.             if( p_sys->p_info->display_fbuf &&
  460.                 p_sys->p_info->display_fbuf->id )
  461.             {
  462.                 p_pic = p_sys->p_info->display_fbuf->id;
  463.                 if( DpbDisplayPicture( p_dec, p_pic ) )
  464.                     p_pic = NULL;
  465.                 decoder_SynchroEnd( p_sys->p_synchro,
  466.                                     p_sys->p_info->display_picture->flags & PIC_MASK_CODING_TYPE,
  467.                                     p_sys->b_garbage_pic );
  468.                 if( p_pic )
  469.                 {
  470.                     p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
  471.                     if( p_sys->b_garbage_pic )
  472.                         p_pic->date = 0; /* ??? */
  473.                     p_sys->b_garbage_pic = false;
  474.                 }
  475.             }
  476.             if( p_sys->p_info->discard_fbuf &&
  477.                 p_sys->p_info->discard_fbuf->id )
  478.             {
  479.                 DpbUnlinkPicture( p_dec, p_sys->p_info->discard_fbuf->id );
  480.             }
  481.             /* For still frames */
  482.             if( state == STATE_END && p_pic )
  483.                 p_pic->b_force = true;
  484.             if( p_pic )
  485.             {
  486.                 /* Avoid frames with identical timestamps.
  487.                  * Especially needed for still frames in DVD menus. */
  488.                 if( p_sys->i_last_frame_pts == p_pic->date )
  489.                     p_pic->date++;
  490.                 p_sys->i_last_frame_pts = p_pic->date;
  491.                 return p_pic;
  492.             }
  493.             break;
  494.         case STATE_INVALID:
  495.         {
  496.             msg_Err( p_dec, "invalid picture encountered" );
  497.             /* I don't think we have anything to do, but well without
  498.              * docs ... */
  499.             break;
  500.         }
  501.         default:
  502.             break;
  503.         }
  504.     }
  505.     /* Never reached */
  506.     return NULL;
  507. }
  508. /*****************************************************************************
  509.  * CloseDecoder: libmpeg2 decoder destruction
  510.  *****************************************************************************/
  511. static void CloseDecoder( vlc_object_t *p_this )
  512. {
  513.     decoder_t *p_dec = (decoder_t *)p_this;
  514.     decoder_sys_t *p_sys = p_dec->p_sys;
  515.     DpbClean( p_dec );
  516.     free( p_sys->p_gop_user_data );
  517.     if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
  518.     if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
  519.     free( p_sys );
  520. }
  521. /*****************************************************************************
  522.  * Reset: reset the decoder state
  523.  *****************************************************************************/
  524. static void Reset( decoder_t *p_dec )
  525. {
  526.     decoder_sys_t *p_sys = p_dec->p_sys;
  527.     cc_Flush( &p_sys->cc );
  528.     mpeg2_reset( p_sys->p_mpeg2dec, 0 );
  529.     DpbClean( p_dec );
  530. }
  531. /*****************************************************************************
  532.  * GetNewPicture: Get a new picture from the vout and set the buf struct
  533.  *****************************************************************************/
  534. static picture_t *GetNewPicture( decoder_t *p_dec )
  535. {
  536.     decoder_sys_t *p_sys = p_dec->p_sys;
  537.     picture_t *p_pic;
  538.     p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
  539.     p_dec->fmt_out.video.i_visible_width =
  540.         p_sys->p_info->sequence->picture_width;
  541.     p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
  542.     p_dec->fmt_out.video.i_visible_height =
  543.         p_sys->p_info->sequence->picture_height;
  544.     p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
  545.     p_dec->fmt_out.video.i_sar_num = p_sys->i_sar_num;
  546.     p_dec->fmt_out.video.i_sar_den = p_sys->i_sar_den;
  547.     if( p_sys->p_info->sequence->frame_period > 0 )
  548.     {
  549.         p_dec->fmt_out.video.i_frame_rate =
  550.             (uint32_t)( (uint64_t)1001000000 * 27 /
  551.                         p_sys->p_info->sequence->frame_period );
  552.         p_dec->fmt_out.video.i_frame_rate_base = 1001;
  553.     }
  554.     p_dec->fmt_out.i_codec =
  555.         ( p_sys->p_info->sequence->chroma_height <
  556.           p_sys->p_info->sequence->height ) ?
  557.         VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
  558.     /* Get a new picture */
  559.     p_pic = decoder_NewPicture( p_dec );
  560.     if( p_pic == NULL )
  561.         return NULL;
  562.     p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
  563.         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
  564.     p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
  565.         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
  566.     p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
  567.         p_sys->p_info->current_picture->nb_fields : 2;
  568.     return p_pic;
  569. }
  570. /*****************************************************************************
  571.  * GetCc: Retrieves the Closed Captions for the CC decoder.
  572.  *****************************************************************************/
  573. static block_t *GetCc( decoder_t *p_dec, bool pb_present[4] )
  574. {
  575.     decoder_sys_t   *p_sys = p_dec->p_sys;
  576.     block_t         *p_cc = NULL;
  577.     int i;
  578.     for( i = 0; i < 4; i++ )
  579.         pb_present[i] = p_sys->cc.pb_present[i];
  580.     if( p_sys->cc.i_data <= 0 )
  581.         return NULL;
  582.     p_cc = block_New( p_dec, p_sys->cc.i_data);
  583.     if( p_cc )
  584.     {
  585.         memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
  586.         p_cc->i_dts =
  587.         p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
  588.         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);
  589.     }
  590.     cc_Flush( &p_sys->cc );
  591.     return p_cc;
  592. }
  593. /*****************************************************************************
  594.  * GetAR: Get aspect ratio
  595.  *****************************************************************************/
  596. static void GetAR( decoder_t *p_dec )
  597. {
  598.     decoder_sys_t *p_sys = p_dec->p_sys;
  599.     int i_old_aspect = p_sys->i_aspect;
  600.     /* Check whether the input gave a particular aspect ratio */
  601.     if( p_dec->fmt_in.video.i_aspect )
  602.     {
  603.         p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
  604.     }
  605.     else
  606.     {
  607.         /* Use the value provided in the MPEG sequence header */
  608.         if( p_sys->p_info->sequence->pixel_height > 0 )
  609.         {
  610.             p_sys->i_aspect =
  611.                 ((uint64_t)p_sys->p_info->sequence->picture_width) *
  612.                 p_sys->p_info->sequence->pixel_width *
  613.                 VOUT_ASPECT_FACTOR /
  614.                 p_sys->p_info->sequence->picture_height /
  615.                 p_sys->p_info->sequence->pixel_height;
  616.             p_sys->i_sar_num = p_sys->p_info->sequence->pixel_width;
  617.             p_sys->i_sar_den = p_sys->p_info->sequence->pixel_height;
  618.         }
  619.         else
  620.         {
  621.             /* Invalid aspect, assume 4:3.
  622.              * This shouldn't happen and if it does it is a bug
  623.              * in libmpeg2 (likely triggered by an invalid stream) */
  624.             p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
  625.             p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 4;
  626.             p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 3;
  627.         }
  628.     }
  629.     if( p_sys->i_aspect == i_old_aspect )
  630.         return;
  631.     if( p_sys->p_info->sequence->frame_period > 0 )
  632.         msg_Dbg( p_dec,
  633.                  "%dx%d (display %d,%d), aspect %d, sar %i:%i, %u.%03u fps",
  634.                  p_sys->p_info->sequence->picture_width,
  635.                  p_sys->p_info->sequence->picture_height,
  636.                  p_sys->p_info->sequence->display_width,
  637.                  p_sys->p_info->sequence->display_height,
  638.                  p_sys->i_aspect, p_sys->i_sar_num, p_sys->i_sar_den,
  639.                  (uint32_t)((uint64_t)1001000000 * 27 /
  640.                      p_sys->p_info->sequence->frame_period / 1001),
  641.                  (uint32_t)((uint64_t)1001000000 * 27 /
  642.                      p_sys->p_info->sequence->frame_period % 1001) );
  643.     else
  644.         msg_Dbg( p_dec, "bad frame period" );
  645. }
  646. /*****************************************************************************
  647.  * PutPicture: Put a picture_t in mpeg2 context
  648.  *****************************************************************************/
  649. static void PutPicture( decoder_t *p_dec, picture_t *p_picture )
  650. {
  651.     decoder_sys_t *p_sys = p_dec->p_sys;
  652.     /* */
  653.     uint8_t *pp_buf[3];
  654.     for( int j = 0; j < 3; j++ )
  655.         pp_buf[j] = p_picture ? p_picture->p[j].p_pixels : NULL;
  656.     mpeg2_set_buf( p_sys->p_mpeg2dec, pp_buf, p_picture );
  657.     /* Completly broken API, why the hell does it suppose
  658.      * the stride of the chroma planes ! */
  659.     if( p_picture )
  660.         mpeg2_stride( p_sys->p_mpeg2dec, p_picture->p[Y_PLANE].i_pitch );
  661. }
  662. /**
  663.  * Initialize a virtual Decoded Picture Buffer to workaround
  664.  * libmpeg2 deficient API
  665.  */
  666. static void DpbInit( decoder_t *p_dec )
  667. {
  668.     decoder_sys_t *p_sys = p_dec->p_sys;
  669.     for( int i = 0; i < DPB_COUNT; i++ )
  670.         p_sys->p_dpb[i].p_picture = NULL;
  671. }
  672. /**
  673.  * Empty and reset the current DPB
  674.  */
  675. static void DpbClean( decoder_t *p_dec )
  676. {
  677.     decoder_sys_t *p_sys = p_dec->p_sys;
  678.     for( int i = 0; i < DPB_COUNT; i++ )
  679.     {
  680.         picture_dpb_t *p = &p_sys->p_dpb[i];
  681.         if( !p->p_picture )
  682.             continue;
  683.         if( p->b_linked )
  684.             decoder_UnlinkPicture( p_dec, p->p_picture );
  685.         if( !p->b_displayed )
  686.             decoder_DeletePicture( p_dec, p->p_picture );
  687.         p->p_picture = NULL;
  688.     }
  689. }
  690. /**
  691.  * Retreive a picture and reserve a place in the DPB
  692.  */
  693. static picture_t *DpbNewPicture( decoder_t *p_dec )
  694. {
  695.     decoder_sys_t *p_sys = p_dec->p_sys;
  696.     picture_dpb_t *p;
  697.     int i;
  698.     for( i = 0; i < DPB_COUNT; i++ )
  699.     {
  700.         p = &p_sys->p_dpb[i];
  701.         if( !p->p_picture )
  702.             break;
  703.     }
  704.     if( i >= DPB_COUNT )
  705.     {
  706.         msg_Err( p_dec, "Leaking picture" );
  707.         return NULL;
  708.     }
  709.     p->p_picture = GetNewPicture( p_dec );
  710.     if( p->p_picture )
  711.     {
  712.         decoder_LinkPicture( p_dec, p->p_picture );
  713.         p->b_linked = true;
  714.         p->b_displayed = false;
  715.         p->p_picture->date = 0;
  716.     }
  717.     return p->p_picture;
  718. }
  719. static picture_dpb_t *DpbFindPicture( decoder_t *p_dec, picture_t *p_picture )
  720. {
  721.     decoder_sys_t *p_sys = p_dec->p_sys;
  722.     for( int i = 0; i < DPB_COUNT; i++ )
  723.     {
  724.         picture_dpb_t *p = &p_sys->p_dpb[i];
  725.         if( p->p_picture == p_picture )
  726.             return p;
  727.     }
  728.     return NULL;
  729. }
  730. /**
  731.  * Unlink the provided picture and ensure that the decoder
  732.  * does not own it anymore.
  733.  */
  734. static void DpbUnlinkPicture( decoder_t *p_dec, picture_t *p_picture )
  735. {
  736.     picture_dpb_t *p = DpbFindPicture( p_dec, p_picture );
  737.     /* XXX it is needed to workaround libmpeg2 bugs */
  738.     if( !p || !p->b_linked )
  739.     {
  740.         msg_Err( p_dec, "DpbUnlinkPicture called on an invalid picture" );
  741.         return;
  742.     }
  743.     assert( p && p->b_linked );
  744.     decoder_UnlinkPicture( p_dec, p->p_picture );
  745.     p->b_linked = false;
  746.     if( !p->b_displayed )
  747.         decoder_DeletePicture( p_dec, p->p_picture );
  748.     p->p_picture = NULL;
  749. }
  750. /**
  751.  * Mark the provided picture as displayed.
  752.  */
  753. static int DpbDisplayPicture( decoder_t *p_dec, picture_t *p_picture )
  754. {
  755.     picture_dpb_t *p = DpbFindPicture( p_dec, p_picture );
  756.     /* XXX it is needed to workaround libmpeg2 bugs */
  757.     if( !p || p->b_displayed || !p->b_linked )
  758.     {
  759.         msg_Err( p_dec, "DpbDisplayPicture called on an invalid picture" );
  760.         return VLC_EGENERIC;
  761.     }
  762.     assert( p && !p->b_displayed && p->b_linked );
  763.     p->b_displayed = true;
  764.     return VLC_SUCCESS;
  765. }