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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * xxmc.c : HW MPEG decoder thread
  3.  *****************************************************************************
  4.  * Copyright (C) 2000-2001 VideoLAN
  5.  * $Id: edfe1cc27b2cbadc6219884dc98aed416fb33bb9 $
  6.  *
  7.  * Authors: Samuel Hocevar <sam@zoy.org>
  8.  *          Laurent Aimar <fenrir@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. #ifdef HAVE_CONFIG_H
  28. # include "config.h"
  29. #endif
  30. #include <vlc_common.h>
  31. #include <vlc_plugin.h>
  32. #include <vlc_vout.h>
  33. #include <vlc_codec.h>
  34. #include <vlc_codec_synchro.h>
  35. #include <unistd.h>
  36. #ifdef __GLIBC__
  37.     #include <mcheck.h>
  38. #endif
  39. #include "mpeg2.h"
  40. //#include "attributes.h"
  41. #include "mpeg2_internal.h"
  42. //#include "xvmc_vld.h"
  43. /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
  44. #define AR_SQUARE_PICTURE       1                           /* square pixels */
  45. #define AR_3_4_PICTURE          2                        /* 3:4 picture (TV) */
  46. #define AR_16_9_PICTURE         3              /* 16:9 picture (wide screen) */
  47. #define AR_221_1_PICTURE        4                  /* 2.21:1 picture (movie) */
  48. /*****************************************************************************
  49.  * decoder_sys_t : libmpeg2 decoder descriptor
  50.  *****************************************************************************/
  51. struct decoder_sys_t
  52. {
  53.     /*
  54.      * libmpeg2 properties
  55.      */
  56.     mpeg2dec_t          *p_mpeg2dec;
  57.     const mpeg2_info_t  *p_info;
  58.     bool                b_skip;
  59.     /*
  60.      * Input properties
  61.      */
  62.     mtime_t             i_pts;
  63.     mtime_t             i_previous_pts;
  64.     mtime_t             i_current_pts;
  65.     mtime_t             i_previous_dts;
  66.     mtime_t             i_current_dts;
  67.     int                 i_current_rate;
  68.     picture_t *         p_picture_to_destroy;
  69.     bool                b_garbage_pic;
  70.     bool                b_after_sequence_header; /* is it the next frame after
  71.                                                   * the sequence header ?    */
  72.     bool                b_slice_i;             /* intra-slice refresh stream */
  73.     /*
  74.      * Output properties
  75.      */
  76.     decoder_synchro_t   *p_synchro;
  77.     int                 i_aspect;
  78.     mtime_t             i_last_frame_pts;
  79. };
  80. /*****************************************************************************
  81.  * Local prototypes
  82.  *****************************************************************************/
  83. static int  OpenDecoder( vlc_object_t * );
  84. static void CloseDecoder( vlc_object_t * );
  85. static picture_t *DecodeBlock( decoder_t *, block_t ** );
  86. static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
  87. /*****************************************************************************
  88.  * Module descriptor
  89.  *****************************************************************************/
  90. vlc_module_begin ()
  91.     set_description( N_("MPEG I/II hw video decoder (using libmpeg2)") )
  92.     set_capability( "decoder", 140 )
  93.     set_callbacks( OpenDecoder, CloseDecoder )
  94.     add_shortcut( "xxmc" )
  95. vlc_module_end ()
  96. /*****************************************************************************
  97.  * OpenDecoder: probe the decoder and return score
  98.  *****************************************************************************/
  99. static int OpenDecoder( vlc_object_t *p_this )
  100. {
  101.     decoder_t *p_dec = (decoder_t*)p_this;
  102.     decoder_sys_t *p_sys = NULL;
  103.     uint32_t i_accel = 0;
  104.     FILE *f_wd_dec; 
  105. #ifdef __GLIBC__
  106.     mtrace();
  107. #endif
  108.     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','v') &&
  109.         p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','1') &&
  110.         /* Pinnacle hardware-mpeg1 */
  111.         p_dec->fmt_in.i_codec != VLC_FOURCC('P','I','M','1') &&
  112.         /* VIA hardware-mpeg2 */
  113.         p_dec->fmt_in.i_codec != VLC_FOURCC('X','x','M','C') &&
  114.         /* ATI Video */
  115.         p_dec->fmt_in.i_codec != VLC_FOURCC('V','C','R','2') &&
  116.         p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','2') )
  117.     {
  118.         return VLC_EGENERIC;
  119.     }
  120.     msg_Dbg(p_dec, "OpenDecoder Entering");
  121.     /* Allocate the memory needed to store the decoder's structure */
  122.     p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys) );
  123.     if( !p_sys )
  124.         return VLC_ENOMEM;
  125.     /* Initialize the thread properties */
  126.     p_sys->p_mpeg2dec = NULL;
  127.     p_sys->p_synchro  = NULL;
  128.     p_sys->p_info     = NULL;
  129.     p_sys->i_pts      = mdate() + DEFAULT_PTS_DELAY;
  130.     p_sys->i_current_pts  = 0;
  131.     p_sys->i_previous_pts = 0;
  132.     p_sys->i_current_dts  = 0;
  133.     p_sys->i_previous_dts = 0;
  134.     p_sys->p_picture_to_destroy = NULL;
  135.     p_sys->b_garbage_pic = 0;
  136.     p_sys->b_slice_i  = 0;
  137.     p_sys->b_skip     = 0;
  138. #if defined( __i386__ )
  139.     if( vlc_CPU() & CPU_CAPABILITY_MMX )
  140.     {
  141.         i_accel |= MPEG2_ACCEL_X86_MMX;
  142.     }
  143.     if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
  144.     {
  145.         i_accel |= MPEG2_ACCEL_X86_3DNOW;
  146.     }
  147.     if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
  148.     {
  149.         i_accel |= MPEG2_ACCEL_X86_MMXEXT;
  150.     }
  151. #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
  152.     if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
  153.     {
  154.         i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
  155.     }
  156. #else
  157.     /* If we do not know this CPU, trust libmpeg2's feature detection */
  158.     i_accel = MPEG2_ACCEL_DETECT;
  159. #endif
  160.     /* Set CPU acceleration features */
  161.     mpeg2_accel( i_accel );
  162.     /* Initialize decoder */
  163.     p_sys->p_mpeg2dec = mpeg2_init();
  164.     if( p_sys->p_mpeg2dec == NULL)
  165.     {
  166.         msg_Err( p_dec, "mpeg2_init() failed" );
  167.         free( p_sys );
  168.         return VLC_EGENERIC;
  169.     }
  170.     p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
  171.     p_dec->pf_decode_video = DecodeBlock;
  172.     p_dec->fmt_out.i_cat = VIDEO_ES;
  173.     p_dec->fmt_out.i_codec = 0;
  174.     f_wd_dec = fopen("/vlc/dec_pid", "w");
  175.     if (f_wd_dec != NULL)
  176.     {
  177.         fprintf(f_wd_dec, "%dn", getpid());
  178.         fflush(f_wd_dec);
  179.         fclose(f_wd_dec);
  180.     }
  181.     msg_Dbg(p_dec, "OpenDecoder Leaving");
  182.     return VLC_SUCCESS;
  183. }
  184. static void WriteDecodeFile(int value)
  185. {
  186.     FILE *f_wd_ok;
  187.     f_wd_ok = fopen("/vlc/dec_ok", "w");
  188.     if (f_wd_ok != NULL)
  189.     {
  190.         fprintf(f_wd_ok, "%d", value);
  191.         fflush(f_wd_ok);
  192.         fclose(f_wd_ok);
  193.     }
  194. }
  195. /*****************************************************************************
  196.  * RunDecoder: the libmpeg2 decoder
  197.  *****************************************************************************/
  198. static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
  199. {
  200.     decoder_sys_t   *p_sys = p_dec->p_sys;
  201.     mpeg2_state_t   state;
  202.     picture_t       *p_pic;
  203.     block_t *p_block;
  204.     if( !pp_block || !*pp_block )
  205.         return NULL;
  206.     p_block = *pp_block;
  207.     while( 1 )
  208.     {
  209.         state = mpeg2_parse( p_sys->p_mpeg2dec );
  210.         switch( state )
  211.         {
  212.             case STATE_BUFFER:
  213.                 if( !p_block->i_buffer )
  214.                 {
  215.                     block_Release( p_block );
  216.                     return NULL;
  217.                 }
  218.                 if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
  219.                     p_sys->p_synchro &&
  220.                     p_sys->p_info->sequence &&
  221.                     p_sys->p_info->sequence->width != (unsigned int)-1 )
  222.                 {
  223.                     decoder_SynchroReset( p_sys->p_synchro );
  224.                     if( p_sys->p_info->current_fbuf != NULL
  225.                         && p_sys->p_info->current_fbuf->id != NULL )
  226.                     {
  227.                         p_sys->b_garbage_pic = 1;
  228.                         p_pic = p_sys->p_info->current_fbuf->id;
  229.                     }
  230.                     else
  231.                     {
  232.                         uint8_t *buf[3];
  233.                         buf[0] = buf[1] = buf[2] = NULL;
  234.                         if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
  235.                             break;
  236.                         mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
  237.                     }
  238.                     p_sys->p_picture_to_destroy = p_pic;
  239.                     if ( p_sys->b_slice_i )
  240.                     {
  241.                         decoder_SynchroNewPicture( p_sys->p_synchro,
  242.                             I_CODING_TYPE, 2, 0, 0,
  243.                             p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
  244.                         decoder_SynchroDecode( p_sys->p_synchro );
  245.                         decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
  246.                     }
  247.                 }
  248. #ifdef PIC_FLAG_PTS
  249.                 if( p_block->i_pts )
  250.                 {
  251.                     mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
  252. #else /* New interface */
  253.                 if( p_block->i_pts || p_block->i_dts )
  254.                 {
  255.                     mpeg2_tag_picture( p_sys->p_mpeg2dec,
  256.                                         (uint32_t)p_block->i_pts,
  257.                                         (uint32_t)p_block->i_dts );
  258. #endif
  259.                     p_sys->i_previous_pts = p_sys->i_current_pts;
  260.                     p_sys->i_current_pts = p_block->i_pts;
  261.                     p_sys->i_previous_dts = p_sys->i_current_dts;
  262.                     p_sys->i_current_dts = p_block->i_dts;
  263.                 }
  264.                 p_sys->i_current_rate = p_block->i_rate;
  265.                 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
  266.                                 p_block->p_buffer + p_block->i_buffer );
  267.                 p_block->i_buffer = 0;
  268.                 break;
  269.             case STATE_SEQUENCE:
  270.             {
  271.                 /* Initialize video output */
  272.                 uint8_t *buf[3];
  273.                 buf[0] = buf[1] = buf[2] = NULL;
  274.                 /* Check whether the input gave a particular aspect ratio */
  275.                 if( p_dec->fmt_in.video.i_aspect )
  276.                 {
  277.                     p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
  278.                     if( p_sys->i_aspect <= AR_221_1_PICTURE )
  279.                     switch( p_sys->i_aspect )
  280.                     {
  281.                     case AR_3_4_PICTURE:
  282.                         p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
  283.                         break;
  284.                     case AR_16_9_PICTURE:
  285.                         p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
  286.                         break;
  287.                     case AR_221_1_PICTURE:
  288.                         p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
  289.                         break;
  290.                     case AR_SQUARE_PICTURE:
  291.                         p_sys->i_aspect = VOUT_ASPECT_FACTOR *
  292.                                     p_sys->p_info->sequence->width /
  293.                                     p_sys->p_info->sequence->height;
  294.                         break;
  295.                     }
  296.                 }
  297.                 else
  298.                 {
  299.                     /* Use the value provided in the MPEG sequence header */
  300.                     if( p_sys->p_info->sequence->pixel_height > 0 )
  301.                     {
  302.                         p_sys->i_aspect =
  303.                             ((uint64_t)p_sys->p_info->sequence->display_width) *
  304.                             p_sys->p_info->sequence->pixel_width *
  305.                             VOUT_ASPECT_FACTOR /
  306.                             p_sys->p_info->sequence->display_height /
  307.                             p_sys->p_info->sequence->pixel_height;
  308.                     }
  309.                     else
  310.                     {
  311.                         /* Invalid aspect, assume 4:3.
  312.                         * This shouldn't happen and if it does it is a bug
  313.                         * in libmpeg2 (likely triggered by an invalid stream) */
  314.                         p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
  315.                     }
  316.                 }
  317.                 msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
  318.                         p_sys->p_info->sequence->width,
  319.                         p_sys->p_info->sequence->height, p_sys->i_aspect,
  320.                         (uint32_t)((uint64_t)1001000000 * 27 /
  321.                             p_sys->p_info->sequence->frame_period / 1001),
  322.                         (uint32_t)((uint64_t)1001000000 * 27 /
  323.                             p_sys->p_info->sequence->frame_period % 1001) );
  324.                 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
  325.                 /* Set the first 2 reference frames */
  326.                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
  327.                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
  328.                 {
  329.                     block_Release( p_block );
  330.                     return NULL;
  331.                 }
  332.                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
  333.                 p_pic->date = 0;
  334.                 decoder_LinkPicture( p_dec, p_pic );
  335.                 if( p_sys->p_synchro )
  336.                 {
  337.                     decoder_SynchroRelease( p_sys->p_synchro );
  338.                 }
  339.                 p_sys->p_synchro = decoder_SynchroInit( p_dec,
  340.                     (uint32_t)((uint64_t)1001000000 * 27 /
  341.                     p_sys->p_info->sequence->frame_period) );
  342.                 p_sys->b_after_sequence_header = 1;
  343.             }
  344.             break;
  345.             case STATE_PICTURE_2ND:
  346.                 decoder_SynchroNewPicture( p_sys->p_synchro,
  347.                         p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
  348.                         p_sys->p_info->current_picture->nb_fields,
  349.                         0, 0,
  350.                         p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
  351.                 if( p_sys->b_skip )
  352.                 {
  353.                     decoder_SynchroTrash( p_sys->p_synchro );
  354.                 }
  355.                 else
  356.                 {
  357.                     decoder_SynchroDecode( p_sys->p_synchro );
  358.                 }
  359.                 break;
  360.             case STATE_PICTURE:
  361.             {
  362.                 uint8_t *buf[3];
  363.                 mtime_t i_pts;
  364.                 buf[0] = buf[1] = buf[2] = NULL;
  365.                 if ( p_sys->b_after_sequence_header &&
  366.                     ((p_sys->p_info->current_picture->flags &
  367.                         PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
  368.                 {
  369.                     /* Intra-slice refresh. Simulate a blank I picture. */
  370.                     msg_Dbg( p_dec, "intra-slice refresh stream" );
  371.                     decoder_SynchroNewPicture( p_sys->p_synchro,
  372.                         I_CODING_TYPE, 2, 0, 0,
  373.                         p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
  374.                     decoder_SynchroDecode( p_sys->p_synchro );
  375.                     decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
  376.                     p_sys->b_slice_i = 1;
  377.                 }
  378.                 p_sys->b_after_sequence_header = 0;
  379. #ifdef PIC_FLAG_PTS
  380.                 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
  381.                     ( ( p_sys->p_info->current_picture->pts ==
  382.                         (uint32_t)p_sys->i_current_pts ) ?
  383.                     p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
  384. #else /* New interface */
  385.                 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
  386.                     ( ( p_sys->p_info->current_picture->tag ==
  387.                         (uint32_t)p_sys->i_current_pts ) ?
  388.                     p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
  389. #endif
  390.                 /* Hack to handle demuxers which only have DTS timestamps */
  391.                 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
  392.                 {
  393.                     if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
  394.                         (p_sys->p_info->current_picture->flags &
  395.                         PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
  396.                     {
  397.                         i_pts = p_block->i_dts;
  398.                     }
  399.                 }
  400.                 p_block->i_pts = p_block->i_dts = 0;
  401.                 /* End hack */
  402.                 decoder_SynchroNewPicture( p_sys->p_synchro,
  403.                     p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
  404.                     p_sys->p_info->current_picture->nb_fields, i_pts,
  405.                     0,
  406.                     p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
  407.                 if ( !(p_sys->b_slice_i
  408.                     && ((p_sys->p_info->current_picture->flags
  409.                             & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
  410.                     && !decoder_SynchroChoose( p_sys->p_synchro,
  411.                                 p_sys->p_info->current_picture->flags
  412.                                     & PIC_MASK_CODING_TYPE,
  413.                                 /*FindVout(p_dec)->render_time*/ 0 /*FIXME*/,
  414.                                 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
  415.                 {
  416.                     mpeg2_skip( p_sys->p_mpeg2dec, 1 );
  417.                     p_sys->b_skip = 1;
  418.                     decoder_SynchroTrash( p_sys->p_synchro );
  419.                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
  420.                 }
  421.                 else
  422.                 {
  423.                     mpeg2_skip( p_sys->p_mpeg2dec, 0 );
  424.                     p_sys->b_skip = 0;
  425.                     decoder_SynchroDecode( p_sys->p_synchro );
  426.                     if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
  427.                     {
  428.                         block_Release( p_block );
  429.                         return NULL;
  430.                     }
  431.                     //p_sys->p_mpeg2dec->ptr_forward_ref_picture = p_sys->p_mpeg2dec->fbuf[2]->id;
  432.                     //p_sys->p_mpeg2dec->ptr_backward_ref_picture = p_sys->p_mpeg2dec->fbuf[1]->id;
  433.                     if ((p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE) != B_CODING_TYPE)
  434.                     {
  435.                         //if (p_sys->p_mpeg2dec->ptr_forward_ref_picture &&
  436.                         //    p_sys->p_mpeg2dec->ptr_forward_ref_picture != picture->backward_reference_frame)
  437.                         //    p_pic->forward_reference_frame->free (p_pic->forward_reference_frame);
  438.                         //p_sys->p_mpeg2dec->ptr_forward_ref_picture =
  439.                         //            p_sys->p_mpeg2dec->ptr_backward_ref_picture;
  440.                         //p_sys->p_mpeg2dec->ptr_backward_ref_picture = (void *)p_pic;
  441.                     }
  442.                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
  443.                 }
  444.             }
  445.             break;
  446.             case STATE_END:
  447.             case STATE_SLICE:
  448.                 p_pic = NULL;
  449.                 if( p_sys->p_info->display_fbuf
  450.                     && p_sys->p_info->display_fbuf->id )
  451.                 {
  452.                     p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
  453.                     decoder_SynchroEnd( p_sys->p_synchro,
  454.                                 p_sys->p_info->display_picture->flags
  455.                                 & PIC_MASK_CODING_TYPE,
  456.                                 p_sys->b_garbage_pic );
  457.                     p_sys->b_garbage_pic = 0;
  458.                     if ( p_sys->p_picture_to_destroy != p_pic )
  459.                     {
  460.                         p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
  461.                     }
  462.                     else
  463.                     {
  464.                         p_sys->p_picture_to_destroy = NULL;
  465.                         p_pic->date = 0;
  466.                     }
  467.                 }
  468.                 if( p_sys->p_info->discard_fbuf &&
  469.                     p_sys->p_info->discard_fbuf->id )
  470.                 {
  471.                     decoder_UnlinkPicture( p_dec, p_sys->p_info->discard_fbuf->id );
  472.                 }
  473.                 /* For still frames */
  474.                 //if( state == STATE_END && p_pic ) p_pic->b_force = true;
  475.                 if( p_pic )
  476.                 {
  477. #if 0
  478.                     /* Avoid frames with identical timestamps.
  479.                      * Especially needed for still frames in DVD menus. */
  480.                      if( p_sys->i_last_frame_pts == p_pic->date ) p_pic->date++;
  481.                         p_sys->i_last_frame_pts = p_pic->date;
  482. #endif
  483.                     return p_pic;
  484.                 }
  485.                 break;
  486.             case STATE_INVALID:
  487.             {
  488.                 uint8_t *buf[3];
  489.                 buf[0] = buf[1] = buf[2] = NULL;
  490.                 msg_Warn( p_dec, "invalid picture encountered" );
  491.                 if ( ( p_sys->p_info->current_picture == NULL ) ||
  492.                 ( ( p_sys->p_info->current_picture->flags &
  493.                     PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
  494.                 {
  495.                     if( p_sys->p_synchro ) decoder_SynchroReset( p_sys->p_synchro );
  496.                 }
  497.                 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
  498.                 p_sys->b_skip = 1;
  499.                 if( p_sys->p_info->current_fbuf &&
  500.                     p_sys->p_info->current_fbuf->id )
  501.                 {
  502.                     p_sys->b_garbage_pic = 1;
  503.                     p_pic = p_sys->p_info->current_fbuf->id;
  504.                 }
  505.                 else if( !p_sys->p_info->sequence )
  506.                 {
  507.                     break;
  508.                 }
  509.                 else
  510.                 {
  511.                     if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
  512.                         break;
  513.                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
  514.                 }
  515.                 p_sys->p_picture_to_destroy = p_pic;
  516.                 memset( p_pic->p[0].p_pixels, 0,
  517.                         p_sys->p_info->sequence->width
  518.                         * p_sys->p_info->sequence->height );
  519.                 memset( p_pic->p[1].p_pixels, 0x80,
  520.                         p_sys->p_info->sequence->width
  521.                         * p_sys->p_info->sequence->height / 4 );
  522.                 memset( p_pic->p[2].p_pixels, 0x80,
  523.                         p_sys->p_info->sequence->width
  524.                         * p_sys->p_info->sequence->height / 4 );
  525.                 if( p_sys->b_slice_i )
  526.                 {
  527.                     decoder_SynchroNewPicture( p_sys->p_synchro,
  528.                         I_CODING_TYPE, 2, 0, 0,
  529.                         p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
  530.                     decoder_SynchroDecode( p_sys->p_synchro );
  531.                     decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
  532.                 }
  533.                 break;
  534.             }
  535.             default:
  536.                 break;
  537.         }
  538.     }
  539.     return NULL;
  540. }
  541. /*****************************************************************************
  542.  * CloseDecoder: libmpeg2 decoder destruction
  543.  *****************************************************************************/
  544. static void CloseDecoder( vlc_object_t *p_this )
  545. {
  546.     decoder_t *p_dec = (decoder_t *)p_this;
  547.     decoder_sys_t *p_sys = p_dec->p_sys;
  548.     FILE *f_wd_dec;
  549.     if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
  550.     if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
  551.     f_wd_dec = fopen("/vlc/dec_pid", "w");
  552.     if (f_wd_dec != NULL)
  553.     {
  554.         fprintf(f_wd_dec, "0n");
  555.         fflush(f_wd_dec);
  556.         fclose(f_wd_dec);
  557.     }
  558.     free( p_sys );
  559. }
  560. static double get_aspect_ratio( decoder_t *p_dec )
  561. {
  562.     decoder_sys_t *p_sys = p_dec->p_sys;
  563.     double ratio;
  564.     double mpeg1_pel_ratio[16] = {1.0 /* forbidden */,
  565.         1.0, 0.6735, 0.7031, 0.7615, 0.8055, 0.8437, 0.8935, 0.9157,
  566.         0.9815, 1.0255, 1.0695, 1.0950, 1.1575, 1.2015, 1.0 /*reserved*/ };
  567.     if( !p_sys->p_mpeg2dec->decoder.mpeg1 )
  568.     {
  569.         /* these hardcoded values are defined on mpeg2 standard for
  570.         * aspect ratio. other values are reserved or forbidden.  */
  571.         /*switch( p_sys->p_mpeg2dec->decoder.aspect_ratio_information )
  572.         {
  573.             case 2:
  574.                 ratio = 4.0/3.0;
  575.                 break;
  576.             case 3:
  577.                 ratio = 16.0/9.0;
  578.                 break;
  579.             case 4:
  580.                 ratio = 2.11/1.0;
  581.                 break;
  582.             case 1:
  583.                 default:*/
  584.                 ratio = (double)p_sys->p_mpeg2dec->decoder.width/(double)p_sys->p_mpeg2dec->decoder.height;
  585.         /*    break;
  586.         }*/
  587.     }
  588.     else
  589.     {
  590.         /* mpeg1 constants refer to pixel aspect ratio */
  591.         ratio = (double)p_sys->p_mpeg2dec->decoder.width/(double)p_sys->p_mpeg2dec->decoder.height;
  592.         /* ratio /= mpeg1_pel_ratio[p_sys->p_mpeg2dec->decoder.aspect_ratio_information]; */
  593.     }
  594.     return ratio;
  595. }
  596. /*****************************************************************************
  597.  * GetNewPicture: Get a new picture from the vout and set the buf struct
  598.  *****************************************************************************/
  599. static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
  600. {
  601.     //msg_Dbg(p_dec, "GetNewPicture Entering");
  602.     decoder_sys_t *p_sys = p_dec->p_sys;
  603.     picture_t *p_pic;
  604.     p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
  605.     p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
  606.     p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
  607.     if( p_sys->p_info->sequence->frame_period > 0 )
  608.     {
  609.         p_dec->fmt_out.video.i_frame_rate =
  610.             (uint32_t)( (uint64_t)1001000000 * 27 /
  611.                         p_sys->p_info->sequence->frame_period );
  612.         p_dec->fmt_out.video.i_frame_rate_base = 1001;
  613.     }
  614.     p_dec->fmt_out.i_codec =
  615.         ( p_sys->p_info->sequence->chroma_height <
  616.           p_sys->p_info->sequence->height ) ?
  617.         VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
  618. #if 0
  619.     p_sys->f_wd_nb = fopen("/vlc/dec_nb", "w");
  620.     if (p_sys->f_wd_nb != NULL)
  621.     {
  622. //      fprintf(p_sys->f_wd_nb, "%dn", mdate());
  623.         fprintf(p_sys->f_wd_nb, "%sn", mdate());
  624.         fflush(p_sys->f_wd_nb);
  625.     }
  626. #endif
  627.     p_pic = decoder_NewPicture( p_dec );
  628.     if( p_pic == NULL ) return NULL;
  629.     p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
  630.         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
  631.     p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
  632.         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
  633.     p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
  634.         p_sys->p_info->current_picture->nb_fields : 2;
  635.     p_pic->format.i_frame_rate = p_dec->fmt_out.video.i_frame_rate;
  636.     p_pic->format.i_frame_rate_base = p_dec->fmt_out.video.i_frame_rate_base;
  637.     decoder_LinkPicture( p_dec, p_pic );
  638.     pp_buf[0] = p_pic->p[0].p_pixels;
  639.     pp_buf[1] = p_pic->p[1].p_pixels;
  640.     pp_buf[2] = p_pic->p[2].p_pixels;
  641.     /* let driver ensure this image has the right format */
  642. #if 0
  643.     this->driver->update_frame_format( p_pic->p_sys->p_vout, p_pic,
  644.                                        p_dec->fmt_out.video.i_width,
  645.                                        p_dec->fmt_out.video.i_height,
  646.                                        p_dec->fmt_out.video.i_aspect,
  647.                                        format, flags);
  648.     mpeg2_xxmc_choose_coding( p_dec, &p_sys->p_mpeg2dec->decoder, p_pic,
  649.                               get_aspect_ratio(p_dec), 0 );
  650. #endif
  651.     return p_pic;
  652. }