libmpeg2.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:22k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2001 VideoLAN
  5.  * $Id: libmpeg2.c 8813 2004-09-26 20:17:50Z gbazin $
  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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #include <vlc/vlc.h>
  28. #include <vlc/vout.h>
  29. #include <vlc/decoder.h>
  30. #include <mpeg2dec/mpeg2.h>
  31. #include "vout_synchro.h"
  32. /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
  33. #define AR_SQUARE_PICTURE       1                           /* square pixels */
  34. #define AR_3_4_PICTURE          2                        /* 3:4 picture (TV) */
  35. #define AR_16_9_PICTURE         3              /* 16:9 picture (wide screen) */
  36. #define AR_221_1_PICTURE        4                  /* 2.21:1 picture (movie) */
  37. /*****************************************************************************
  38.  * decoder_sys_t : libmpeg2 decoder descriptor
  39.  *****************************************************************************/
  40. struct decoder_sys_t
  41. {
  42.     /*
  43.      * libmpeg2 properties
  44.      */
  45.     mpeg2dec_t          *p_mpeg2dec;
  46.     const mpeg2_info_t  *p_info;
  47.     vlc_bool_t          b_skip;
  48.     /*
  49.      * Input properties
  50.      */
  51.     mtime_t          i_previous_pts;
  52.     mtime_t          i_current_pts;
  53.     mtime_t          i_previous_dts;
  54.     mtime_t          i_current_dts;
  55.     int              i_current_rate;
  56.     picture_t *      p_picture_to_destroy;
  57.     vlc_bool_t       b_garbage_pic;
  58.     vlc_bool_t       b_after_sequence_header; /* is it the next frame after
  59.                                                * the sequence header ?    */
  60.     vlc_bool_t       b_slice_i;             /* intra-slice refresh stream */
  61.     /*
  62.      * Output properties
  63.      */
  64.     vout_synchro_t *p_synchro;
  65.     int            i_aspect;
  66.     mtime_t        i_last_frame_pts;
  67. };
  68. /*****************************************************************************
  69.  * Local prototypes
  70.  *****************************************************************************/
  71. static int  OpenDecoder( vlc_object_t * );
  72. static void CloseDecoder( vlc_object_t * );
  73. static picture_t *DecodeBlock( decoder_t *, block_t ** );
  74. static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
  75. /*****************************************************************************
  76.  * Module descriptor
  77.  *****************************************************************************/
  78. vlc_module_begin();
  79.     set_description( _("MPEG I/II video decoder (using libmpeg2)") );
  80.     set_capability( "decoder", 150 );
  81.     set_callbacks( OpenDecoder, CloseDecoder );
  82.     add_shortcut( "libmpeg2" );
  83. vlc_module_end();
  84. /*****************************************************************************
  85.  * OpenDecoder: probe the decoder and return score
  86.  *****************************************************************************/
  87. static int OpenDecoder( vlc_object_t *p_this )
  88. {
  89.     decoder_t *p_dec = (decoder_t*)p_this;
  90.     decoder_sys_t *p_sys;
  91.     uint32_t i_accel = 0;
  92.     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','v') &&
  93.         p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','1') &&
  94.         /* Pinnacle hardware-mpeg1 */
  95.         p_dec->fmt_in.i_codec != VLC_FOURCC('P','I','M','1') &&
  96.         /* ATI Video */
  97.         p_dec->fmt_in.i_codec != VLC_FOURCC('V','C','R','2') &&
  98.         p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','2') )
  99.     {
  100.         return VLC_EGENERIC;
  101.     }
  102.     /* Allocate the memory needed to store the decoder's structure */
  103.     if( ( p_dec->p_sys = p_sys =
  104.           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
  105.     {
  106.         msg_Err( p_dec, "out of memory" );
  107.         return VLC_EGENERIC;
  108.     }
  109.     /* Initialize the thread properties */
  110.     memset( p_sys, 0, sizeof(decoder_sys_t) );
  111.     p_sys->p_mpeg2dec = NULL;
  112.     p_sys->p_synchro  = NULL;
  113.     p_sys->p_info     = NULL;
  114.     p_sys->i_current_pts  = 0;
  115.     p_sys->i_previous_pts = 0;
  116.     p_sys->i_current_dts  = 0;
  117.     p_sys->i_previous_dts = 0;
  118.     p_sys->p_picture_to_destroy = NULL;
  119.     p_sys->b_garbage_pic = 0;
  120.     p_sys->b_slice_i  = 0;
  121.     p_sys->b_skip     = 0;
  122. #if defined( __i386__ )
  123.     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
  124.     {
  125.         i_accel |= MPEG2_ACCEL_X86_MMX;
  126.     }
  127.     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW )
  128.     {
  129.         i_accel |= MPEG2_ACCEL_X86_3DNOW;
  130.     }
  131.     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
  132.     {
  133.         i_accel |= MPEG2_ACCEL_X86_MMXEXT;
  134.     }
  135. #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
  136.     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
  137.     {
  138.         i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
  139.     }
  140. #else
  141.     /* If we do not know this CPU, trust libmpeg2's feature detection */
  142.     i_accel = MPEG2_ACCEL_DETECT;
  143. #endif
  144.     /* Set CPU acceleration features */
  145.     mpeg2_accel( i_accel );
  146.     /* Initialize decoder */
  147.     p_sys->p_mpeg2dec = mpeg2_init();
  148.     if( p_sys->p_mpeg2dec == NULL)
  149.     {
  150.         msg_Err( p_dec, "mpeg2_init() failed" );
  151.         free( p_sys );
  152.         return VLC_EGENERIC;
  153.     }
  154.     p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
  155.     p_dec->pf_decode_video = DecodeBlock;
  156.     return VLC_SUCCESS;
  157. }
  158. /*****************************************************************************
  159.  * RunDecoder: the libmpeg2 decoder
  160.  *****************************************************************************/
  161. static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
  162. {
  163.     decoder_sys_t   *p_sys = p_dec->p_sys;
  164.     mpeg2_state_t   state;
  165.     picture_t       *p_pic;
  166.     block_t *p_block;
  167.     if( !pp_block || !*pp_block ) return NULL;
  168.     p_block = *pp_block;
  169.     while( 1 )
  170.     {
  171.         state = mpeg2_parse( p_sys->p_mpeg2dec );
  172.         switch( state )
  173.         {
  174.         case STATE_BUFFER:
  175.             if( !p_block->i_buffer )
  176.             {
  177.                 block_Release( p_block );
  178.                 return NULL;
  179.             }
  180.             if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
  181.                 p_sys->p_synchro &&
  182.                 p_sys->p_info->sequence &&
  183.                 p_sys->p_info->sequence->width != (unsigned)-1 )
  184.             {
  185.                 vout_SynchroReset( p_sys->p_synchro );
  186.                 if( p_sys->p_info->current_fbuf != NULL
  187.                     && p_sys->p_info->current_fbuf->id != NULL )
  188.                 {
  189.                     p_sys->b_garbage_pic = 1;
  190.                     p_pic = p_sys->p_info->current_fbuf->id;
  191.                 }
  192.                 else
  193.                 {
  194.                     uint8_t *buf[3];
  195.                     buf[0] = buf[1] = buf[2] = NULL;
  196.                     if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
  197.                         break;
  198.                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
  199.                 }
  200.                 p_sys->p_picture_to_destroy = p_pic;
  201.                 if ( p_sys->b_slice_i )
  202.                 {
  203.                     vout_SynchroNewPicture( p_sys->p_synchro,
  204.                         I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
  205.                     vout_SynchroDecode( p_sys->p_synchro );
  206.                     vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
  207.                 }
  208.             }
  209. #ifdef PIC_FLAG_PTS
  210.             if( p_block->i_pts )
  211.             {
  212.                 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
  213. #else /* New interface */
  214.             if( p_block->i_pts || p_block->i_dts )
  215.             {
  216.                 mpeg2_tag_picture( p_sys->p_mpeg2dec,
  217.                                    (uint32_t)p_block->i_pts,
  218.                                    (uint32_t)p_block->i_dts );
  219. #endif
  220.                 p_sys->i_previous_pts = p_sys->i_current_pts;
  221.                 p_sys->i_current_pts = p_block->i_pts;
  222.                 p_sys->i_previous_dts = p_sys->i_current_dts;
  223.                 p_sys->i_current_dts = p_block->i_dts;
  224.             }
  225.             p_sys->i_current_rate = p_block->i_rate;
  226.             mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
  227.                           p_block->p_buffer + p_block->i_buffer );
  228.             p_block->i_buffer = 0;
  229.             break;
  230.         case STATE_SEQUENCE:
  231.         {
  232.             /* Initialize video output */
  233.             uint8_t *buf[3];
  234.             buf[0] = buf[1] = buf[2] = NULL;
  235.             /* Check whether the input gave a particular aspect ratio */
  236.             if( p_dec->fmt_in.video.i_aspect )
  237.             {
  238.                 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
  239.                 if( p_sys->i_aspect <= AR_221_1_PICTURE )
  240.                 switch( p_sys->i_aspect )
  241.                 {
  242.                 case AR_3_4_PICTURE:
  243.                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
  244.                     break;
  245.                 case AR_16_9_PICTURE:
  246.                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
  247.                     break;
  248.                 case AR_221_1_PICTURE:
  249.                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
  250.                     break;
  251.                 case AR_SQUARE_PICTURE:
  252.                     p_sys->i_aspect = VOUT_ASPECT_FACTOR *
  253.                                    p_sys->p_info->sequence->width /
  254.                                    p_sys->p_info->sequence->height;
  255.                     break;
  256.                 }
  257.             }
  258.             else
  259.             {
  260.                 /* Use the value provided in the MPEG sequence header */
  261.                 if( p_sys->p_info->sequence->pixel_height > 0 )
  262.                 {
  263.                     p_sys->i_aspect =
  264.                         ((uint64_t)p_sys->p_info->sequence->display_width) *
  265.                         p_sys->p_info->sequence->pixel_width *
  266.                         VOUT_ASPECT_FACTOR /
  267.                         p_sys->p_info->sequence->display_height /
  268.                         p_sys->p_info->sequence->pixel_height;
  269.                 }
  270.                 else
  271.                 {
  272.                     /* Invalid aspect, assume 4:3.
  273.                      * This shouldn't happen and if it does it is a bug
  274.                      * in libmpeg2 (likely triggered by an invalid stream) */
  275.                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
  276.                 }
  277.             }
  278.             msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
  279.                      p_sys->p_info->sequence->width,
  280.                      p_sys->p_info->sequence->height, p_sys->i_aspect,
  281.                      (uint32_t)((uint64_t)1001000000 * 27 /
  282.                          p_sys->p_info->sequence->frame_period / 1001),
  283.                      (uint32_t)((uint64_t)1001000000 * 27 /
  284.                          p_sys->p_info->sequence->frame_period % 1001) );
  285.             mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
  286.             /* Set the first 2 reference frames */
  287.             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
  288.             if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
  289.             {
  290.                 block_Release( p_block );
  291.                 return NULL;
  292.             }
  293.             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
  294.             /* This picture will never go through display_picture. */
  295.             p_pic->date = 0;
  296.             /* For some reason, libmpeg2 will put this pic twice in
  297.              * discard_picture. This can be considered a bug in libmpeg2. */
  298.             p_dec->pf_picture_link( p_dec, p_pic );
  299.             if( p_sys->p_synchro )
  300.             {
  301.                 vout_SynchroRelease( p_sys->p_synchro );
  302.             }
  303.             p_sys->p_synchro = vout_SynchroInit( p_dec,
  304.                 (uint32_t)((uint64_t)1001000000 * 27 /
  305.                 p_sys->p_info->sequence->frame_period) );
  306.             p_sys->b_after_sequence_header = 1;
  307.         }
  308.         break;
  309.         case STATE_PICTURE_2ND:
  310.             vout_SynchroNewPicture( p_sys->p_synchro,
  311.                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
  312.                 p_sys->p_info->current_picture->nb_fields,
  313.                 0, 0, p_sys->i_current_rate );
  314.             if( p_sys->b_skip )
  315.             {
  316.                 vout_SynchroTrash( p_sys->p_synchro );
  317.             }
  318.             else
  319.             {
  320.                 vout_SynchroDecode( p_sys->p_synchro );
  321.             }
  322.             break;
  323.         case STATE_PICTURE:
  324.         {
  325.             uint8_t *buf[3];
  326.             mtime_t i_pts, i_dts;
  327.             buf[0] = buf[1] = buf[2] = NULL;
  328.             if ( p_sys->b_after_sequence_header &&
  329.                  ((p_sys->p_info->current_picture->flags &
  330.                        PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
  331.             {
  332.                 /* Intra-slice refresh. Simulate a blank I picture. */
  333.                 msg_Dbg( p_dec, "intra-slice refresh stream" );
  334.                 vout_SynchroNewPicture( p_sys->p_synchro,
  335.                     I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
  336.                 vout_SynchroDecode( p_sys->p_synchro );
  337.                 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
  338.                 p_sys->b_slice_i = 1;
  339.             }
  340.             p_sys->b_after_sequence_header = 0;
  341. #ifdef PIC_FLAG_PTS
  342.             i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
  343.                 ( ( p_sys->p_info->current_picture->pts ==
  344.                     (uint32_t)p_sys->i_current_pts ) ?
  345.                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
  346.             i_dts = 0;
  347.             /* Hack to handle demuxers which only have DTS timestamps */
  348.             if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
  349.             {
  350.                 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
  351.                     (p_sys->p_info->current_picture->flags &
  352.                       PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
  353.                 {
  354.                     i_pts = p_block->i_dts;
  355.                 }
  356.             }
  357.             p_block->i_pts = p_block->i_dts = 0;
  358.             /* End hack */
  359. #else /* New interface */
  360.             i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
  361.                 ( ( p_sys->p_info->current_picture->tag ==
  362.                     (uint32_t)p_sys->i_current_pts ) ?
  363.                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
  364.             i_dts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
  365.                 ( ( p_sys->p_info->current_picture->tag2 ==
  366.                     (uint32_t)p_sys->i_current_dts ) ?
  367.                   p_sys->i_current_dts : p_sys->i_previous_dts ) : 0;
  368. #endif
  369.             vout_SynchroNewPicture( p_sys->p_synchro,
  370.                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
  371.                 p_sys->p_info->current_picture->nb_fields, i_pts, i_dts,
  372.                 p_sys->i_current_rate );
  373.             if( !p_dec->b_pace_control &&
  374.                 !(p_sys->b_slice_i
  375.                    && ((p_sys->p_info->current_picture->flags
  376.                          & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
  377.                    && !vout_SynchroChoose( p_sys->p_synchro,
  378.                               p_sys->p_info->current_picture->flags
  379.                                 & PIC_MASK_CODING_TYPE,
  380.                               /*p_sys->p_vout->render_time*/ 0 /*FIXME*/ ) )
  381.             {
  382.                 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
  383.                 p_sys->b_skip = 1;
  384.                 vout_SynchroTrash( p_sys->p_synchro );
  385.                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
  386.             }
  387.             else
  388.             {
  389.                 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
  390.                 p_sys->b_skip = 0;
  391.                 vout_SynchroDecode( p_sys->p_synchro );
  392.                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
  393.                 {
  394.                     block_Release( p_block );
  395.                     return NULL;
  396.                 }
  397.                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
  398.             }
  399.         }
  400.         break;
  401.         case STATE_END:
  402.         case STATE_SLICE:
  403.             p_pic = NULL;
  404.             if( p_sys->p_info->display_fbuf
  405.                 && p_sys->p_info->display_fbuf->id )
  406.             {
  407.                 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
  408.                 vout_SynchroEnd( p_sys->p_synchro,
  409.                             p_sys->p_info->display_picture->flags
  410.                              & PIC_MASK_CODING_TYPE,
  411.                             p_sys->b_garbage_pic );
  412.                 p_sys->b_garbage_pic = 0;
  413.                 if ( p_sys->p_picture_to_destroy != p_pic )
  414.                 {
  415.                     p_pic->date = vout_SynchroDate( p_sys->p_synchro );
  416.                 }
  417.                 else
  418.                 {
  419.                     p_sys->p_picture_to_destroy = NULL;
  420.                     p_pic->date = 0;
  421.                 }
  422.             }
  423.             if( p_sys->p_info->discard_fbuf &&
  424.                 p_sys->p_info->discard_fbuf->id )
  425.             {
  426.                 p_dec->pf_picture_unlink( p_dec,
  427.                                           p_sys->p_info->discard_fbuf->id );
  428.             }
  429.             /* For still frames */
  430.             if( state == STATE_END && p_pic ) p_pic->b_force = VLC_TRUE;
  431.             if( p_pic )
  432.             {
  433.                 /* Avoid frames with identical timestamps.
  434.                  * Especially needed for still frames in DVD menus. */
  435.                 if( p_sys->i_last_frame_pts == p_pic->date ) p_pic->date++;
  436.                 p_sys->i_last_frame_pts = p_pic->date;
  437.                 return p_pic;
  438.             }
  439.             break;
  440.         case STATE_INVALID:
  441.         {
  442.             uint8_t *buf[3];
  443.             buf[0] = buf[1] = buf[2] = NULL;
  444.             msg_Warn( p_dec, "invalid picture encountered" );
  445.             if ( ( p_sys->p_info->current_picture == NULL ) ||
  446.                ( ( p_sys->p_info->current_picture->flags &
  447.                    PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
  448.             {
  449.                 if( p_sys->p_synchro ) vout_SynchroReset( p_sys->p_synchro );
  450.             }
  451.             mpeg2_skip( p_sys->p_mpeg2dec, 1 );
  452.             p_sys->b_skip = 1;
  453.             if( p_sys->p_info->current_fbuf &&
  454.                 p_sys->p_info->current_fbuf->id )
  455.             {
  456.                 p_sys->b_garbage_pic = 1;
  457.                 p_pic = p_sys->p_info->current_fbuf->id;
  458.             }
  459.             else if( !p_sys->p_info->sequence )
  460.             {
  461.                 break;
  462.             }
  463.             else
  464.             {
  465.                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
  466.                     break;
  467.                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
  468.             }
  469.             p_sys->p_picture_to_destroy = p_pic;
  470.             memset( p_pic->p[0].p_pixels, 0,
  471.                     p_sys->p_info->sequence->width
  472.                      * p_sys->p_info->sequence->height );
  473.             memset( p_pic->p[1].p_pixels, 0x80,
  474.                     p_sys->p_info->sequence->width
  475.                      * p_sys->p_info->sequence->height / 4 );
  476.             memset( p_pic->p[2].p_pixels, 0x80,
  477.                     p_sys->p_info->sequence->width
  478.                      * p_sys->p_info->sequence->height / 4 );
  479.             if( p_sys->b_slice_i )
  480.             {
  481.                 vout_SynchroNewPicture( p_sys->p_synchro,
  482.                             I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
  483.                 vout_SynchroDecode( p_sys->p_synchro );
  484.                 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
  485.             }
  486.             break;
  487.         }
  488.         default:
  489.             break;
  490.         }
  491.     }
  492.     /* Never reached */
  493.     return NULL;
  494. }
  495. /*****************************************************************************
  496.  * CloseDecoder: libmpeg2 decoder destruction
  497.  *****************************************************************************/
  498. static void CloseDecoder( vlc_object_t *p_this )
  499. {
  500.     decoder_t *p_dec = (decoder_t *)p_this;
  501.     decoder_sys_t *p_sys = p_dec->p_sys;
  502.     if( p_sys->p_synchro ) vout_SynchroRelease( p_sys->p_synchro );
  503.     if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
  504.     free( p_sys );
  505. }
  506. /*****************************************************************************
  507.  * GetNewPicture: Get a new picture from the vout and set the buf struct
  508.  *****************************************************************************/
  509. static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
  510. {
  511.     decoder_sys_t *p_sys = p_dec->p_sys;
  512.     picture_t *p_pic;
  513.     p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
  514.     p_dec->fmt_out.video.i_visible_width =
  515.         p_sys->p_info->sequence->picture_width;
  516.     p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
  517.     p_dec->fmt_out.video.i_visible_height =
  518.         p_sys->p_info->sequence->picture_height;
  519.     p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
  520.     if( p_sys->p_info->sequence->frame_period > 0 )
  521.     {
  522.         p_dec->fmt_out.video.i_frame_rate =
  523.             (uint32_t)( (uint64_t)1001000000 * 27 /
  524.                         p_sys->p_info->sequence->frame_period );
  525.         p_dec->fmt_out.video.i_frame_rate_base = 1001;
  526.     }
  527.     p_dec->fmt_out.i_codec =
  528.         ( p_sys->p_info->sequence->chroma_height <
  529.           p_sys->p_info->sequence->height ) ?
  530.         VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
  531.     /* Get a new picture */
  532.     p_pic = p_dec->pf_vout_buffer_new( p_dec );
  533.     if( p_pic == NULL ) return NULL;
  534.     p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
  535.         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
  536.     p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
  537.         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
  538.     p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
  539.         p_sys->p_info->current_picture->nb_fields : 2;
  540.     p_dec->pf_picture_link( p_dec, p_pic );
  541.     pp_buf[0] = p_pic->p[0].p_pixels;
  542.     pp_buf[1] = p_pic->p[1].p_pixels;
  543.     pp_buf[2] = p_pic->p[2].p_pixels;
  544.     return p_pic;
  545. }