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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * pva.c: PVA demuxer
  3.  *****************************************************************************
  4.  * Copyright (C) 2004 the VideoLAN team
  5.  * $Id: 68093d98df0e8ab65dd9887d1874c48d29ef2f02 $
  6.  *
  7.  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22.  *****************************************************************************/
  23. /*****************************************************************************
  24.  * Preamble
  25.  *****************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. # include "config.h"
  28. #endif
  29. #include <vlc_common.h>
  30. #include <vlc_plugin.h>
  31. #include <vlc_demux.h>
  32. /*****************************************************************************
  33.  * Module descriptor
  34.  *****************************************************************************/
  35. static int  Open    ( vlc_object_t * );
  36. static void Close  ( vlc_object_t * );
  37. vlc_module_begin ()
  38.     set_description( N_("PVA demuxer" ) )
  39.     set_capability( "demux", 10 )
  40.     set_category( CAT_INPUT )
  41.     set_subcategory( SUBCAT_INPUT_DEMUX )
  42.     set_callbacks( Open, Close )
  43.     add_shortcut( "pva" )
  44. vlc_module_end ()
  45. /*****************************************************************************
  46.  * Local prototypes
  47.  *****************************************************************************/
  48. struct demux_sys_t
  49. {
  50.     es_out_id_t *p_video;
  51.     es_out_id_t *p_audio;
  52.     /* counter */
  53.     int          i_vc;
  54.     int          i_ac;
  55.     /* audio/video block */
  56.     block_t     *p_pes; /* audio */
  57.     block_t     *p_es;  /* video */
  58.     int64_t     b_pcr_audio;
  59. };
  60. static int  Demux   ( demux_t *p_demux );
  61. static int  Control ( demux_t *p_demux, int i_query, va_list args );
  62. static int  ReSynch ( demux_t * );
  63. static void ParsePES( demux_t * );
  64. /*****************************************************************************
  65.  * Open
  66.  *****************************************************************************/
  67. static int Open( vlc_object_t *p_this )
  68. {
  69.     demux_t     *p_demux = (demux_t*)p_this;
  70.     demux_sys_t *p_sys;
  71.     es_format_t  fmt;
  72.     const uint8_t *p_peek;
  73.     if( stream_Peek( p_demux->s, &p_peek, 5 ) < 5 ) return VLC_EGENERIC;
  74.     if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
  75.     {
  76.         /* In case we had forced this demuxer we try to resynch */
  77.         if( !p_demux->b_force || ReSynch( p_demux ) )
  78.             return VLC_EGENERIC;
  79.     }
  80.     /* Fill p_demux field */
  81.     p_demux->pf_demux = Demux;
  82.     p_demux->pf_control = Control;
  83.     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
  84.     /* Register one audio and one video stream */
  85.     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
  86.     p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
  87.     es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
  88.     p_sys->p_video = es_out_Add( p_demux->out, &fmt );
  89.     p_sys->i_vc    = -1;
  90.     p_sys->i_ac    = -1;
  91.     p_sys->p_pes   = NULL;
  92.     p_sys->p_es    = NULL;
  93.     p_sys->b_pcr_audio = false;
  94.     return VLC_SUCCESS;
  95. }
  96. /*****************************************************************************
  97.  * Close
  98.  *****************************************************************************/
  99. static void Close( vlc_object_t *p_this )
  100. {
  101.     demux_t     *p_demux = (demux_t*)p_this;
  102.     demux_sys_t *p_sys = p_demux->p_sys;
  103.     if( p_sys->p_es )  block_Release( p_sys->p_es );
  104.     if( p_sys->p_pes ) block_Release( p_sys->p_pes );
  105.     free( p_sys );
  106. }
  107. /*****************************************************************************
  108.  * Demux:
  109.  *****************************************************************************/
  110. static int Demux( demux_t *p_demux )
  111. {
  112.     demux_sys_t *p_sys = p_demux->p_sys;
  113.     const uint8_t *p_peek;
  114.     int         i_size;
  115.     block_t     *p_frame;
  116.     int64_t     i_pts;
  117.     int         i_skip;
  118.     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
  119.     {
  120.         msg_Warn( p_demux, "eof ?" );
  121.         return 0;
  122.     }
  123.     if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
  124.     {
  125.         msg_Warn( p_demux, "lost synchro" );
  126.         if( ReSynch( p_demux ) )
  127.         {
  128.             return -1;
  129.         }
  130.         if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
  131.         {
  132.             msg_Warn( p_demux, "eof ?" );
  133.             return 0;
  134.         }
  135.     }
  136.     i_size = GetWBE( &p_peek[6] );
  137.     switch( p_peek[2] )
  138.     {
  139.         case 0x01:  /* VideoStream */
  140.             if( p_sys->i_vc < 0 )
  141.             {
  142.                 msg_Dbg( p_demux, "first packet for video" );
  143.             }
  144.             else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
  145.             {
  146.                 msg_Dbg( p_demux, "packet lost (video)" );
  147.                 if( p_sys->p_es )
  148.                 {
  149.                     block_ChainRelease( p_sys->p_es );
  150.                     p_sys->p_es = NULL;
  151.                 }
  152.             }
  153.             p_sys->i_vc = p_peek[3];
  154.             /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
  155.             i_pts = -1;
  156.             i_skip = 8;
  157.             if( p_peek[5]&0x10 )
  158.             {
  159.                 int i_pre = p_peek[5]&0x3;
  160.                 if( ( p_frame = stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
  161.                 {
  162.                     i_pts = GetDWBE( &p_frame->p_buffer[8] );
  163.                     if( p_frame->i_buffer > 12 )
  164.                     {
  165.                         p_frame->p_buffer += 12;
  166.                         p_frame->i_buffer -= 12;
  167.                         block_ChainAppend( &p_sys->p_es, p_frame );
  168.                     }
  169.                     else
  170.                     {
  171.                         block_Release( p_frame );
  172.                     }
  173.                 }
  174.                 i_size -= 4 + i_pre;
  175.                 i_skip  = 0;
  176.                 /* Set PCR */
  177.                 if( ( p_frame = p_sys->p_es ) )
  178.                 {
  179.                     if( p_frame->i_pts > 0 && !p_sys->b_pcr_audio )
  180.                     {
  181.                         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
  182.                     }
  183.                     es_out_Send( p_demux->out, p_sys->p_video, p_frame );
  184.                     p_sys->p_es = NULL;
  185.                 }
  186.             }
  187.             if( ( p_frame = stream_Block( p_demux->s, i_size + i_skip ) ) )
  188.             {
  189.                 p_frame->p_buffer += i_skip;
  190.                 p_frame->i_buffer -= i_skip;
  191.                 if( i_pts > 0 ) p_frame->i_pts = i_pts * 100 / 9;
  192.                 block_ChainAppend( &p_sys->p_es, p_frame );
  193.             }
  194.             break;
  195.         case 0x02:  /* MainAudioStream */
  196.             if( p_sys->i_ac < 0 )
  197.             {
  198.                 msg_Dbg( p_demux, "first packet for audio" );
  199.             }
  200.             else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
  201.             {
  202.                 msg_Dbg( p_demux, "packet lost (audio)" );
  203.                 if( p_sys->p_pes )
  204.                 {
  205.                     block_ChainRelease( p_sys->p_pes );
  206.                     p_sys->p_pes = NULL;
  207.                 }
  208.             }
  209.             p_sys->i_ac = p_peek[3];
  210.             if( p_peek[5]&0x10 && p_sys->p_pes )
  211.             {
  212.                 ParsePES( p_demux );
  213.             }
  214.             if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
  215.             {
  216.                 p_frame->p_buffer += 8;
  217.                 p_frame->i_buffer -= 8;
  218.                 /* XXX this a hack, some streams aren't compliant and
  219.                  * doesn't set pes_start flag */
  220.                 if( p_sys->p_pes && p_frame->i_buffer > 4 &&
  221.                     p_frame->p_buffer[0] == 0x00 &&
  222.                     p_frame->p_buffer[1] == 0x00 &&
  223.                     p_frame->p_buffer[2] == 0x01 )
  224.                 {
  225.                     ParsePES( p_demux );
  226.                 }
  227.                 block_ChainAppend( &p_sys->p_pes, p_frame );
  228.             }
  229.             break;
  230.         default:
  231.             msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
  232.             stream_Read( p_demux->s, NULL, i_size + 8 );
  233.             break;
  234.     }
  235.     return 1;
  236. }
  237. /*****************************************************************************
  238.  * Control:
  239.  *****************************************************************************/
  240. static int Control( demux_t *p_demux, int i_query, va_list args )
  241. {
  242.     /* demux_sys_t *p_sys = p_demux->p_sys; */
  243.     double f, *pf;
  244.     int64_t i64;
  245.     switch( i_query )
  246.     {
  247.         case DEMUX_GET_POSITION:
  248.             if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
  249.             {
  250.                 pf = (double*) va_arg( args, double* );
  251.                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
  252.                 return VLC_SUCCESS;
  253.             }
  254.             return VLC_EGENERIC;
  255.         case DEMUX_SET_POSITION:
  256.             f = (double) va_arg( args, double );
  257.             i64 = stream_Size( p_demux->s );
  258.             if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
  259.             {
  260.                 return VLC_EGENERIC;
  261.             }
  262.             return VLC_SUCCESS;
  263. #if 0
  264.         case DEMUX_GET_TIME:
  265.             pi64 = (int64_t*)va_arg( args, int64_t * );
  266.             if( p_sys->i_time < 0 )
  267.             {
  268.                 *pi64 = 0;
  269.                 return VLC_EGENERIC;
  270.             }
  271.             *pi64 = p_sys->i_time;
  272.             return VLC_SUCCESS;
  273. #if 0
  274.         case DEMUX_GET_LENGTH:
  275.             pi64 = (int64_t*)va_arg( args, int64_t * );
  276.             if( p_sys->i_mux_rate > 0 )
  277.             {
  278.                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
  279.                 return VLC_SUCCESS;
  280.             }
  281.             *pi64 = 0;
  282.             return VLC_EGENERIC;
  283. #endif
  284.         case DEMUX_GET_FPS:
  285.             pf = (double*)va_arg( args, double * );
  286.             *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
  287.             return VLC_SUCCESS;
  288. #endif
  289.         case DEMUX_SET_TIME:
  290.         default:
  291.             return VLC_EGENERIC;
  292.     }
  293. }
  294. /*****************************************************************************
  295.  * ReSynch:
  296.  *****************************************************************************/
  297. static int ReSynch( demux_t *p_demux )
  298. {
  299.     const uint8_t *p_peek;
  300.     int      i_skip;
  301.     int      i_peek;
  302.     while( vlc_object_alive (p_demux) )
  303.     {
  304.         if( ( i_peek = stream_Peek( p_demux->s, &p_peek, 1024 ) ) < 8 )
  305.         {
  306.             return VLC_EGENERIC;
  307.         }
  308.         i_skip = 0;
  309.         while( i_skip < i_peek - 5 )
  310.         {
  311.             if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
  312.             {
  313.                 if( i_skip > 0 )
  314.                 {
  315.                     stream_Read( p_demux->s, NULL, i_skip );
  316.                 }
  317.                 return VLC_SUCCESS;
  318.             }
  319.             p_peek++;
  320.             i_skip++;
  321.         }
  322.         stream_Read( p_demux->s, NULL, i_skip );
  323.     }
  324.     return VLC_EGENERIC;
  325. }
  326. static void ParsePES( demux_t *p_demux )
  327. {
  328.     demux_sys_t *p_sys = p_demux->p_sys;
  329.     block_t     *p_pes = p_sys->p_pes;
  330.     uint8_t     hdr[30];
  331.     int         i_pes_size;
  332.     int         i_skip;
  333.     mtime_t     i_dts = -1;
  334.     mtime_t     i_pts = -1;
  335.     p_sys->p_pes = NULL;
  336.     /* FIXME find real max size */
  337.     block_ChainExtract( p_pes, hdr, 30 );
  338.     if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
  339.     {
  340.         msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
  341.                   hdr[0], hdr[1],hdr[2],hdr[3] );
  342.         block_ChainRelease( p_pes );
  343.         return;
  344.     }
  345.     i_pes_size = GetWBE( &hdr[4] );
  346.     /* we assume mpeg2 PES */
  347.     i_skip = hdr[8] + 9;
  348.     if( hdr[7]&0x80 )    /* has pts */
  349.     {
  350.         i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
  351.                  (mtime_t)(hdr[10] << 22)|
  352.                 ((mtime_t)(hdr[11]&0xfe) << 14)|
  353.                  (mtime_t)(hdr[12] << 7)|
  354.                  (mtime_t)(hdr[12] >> 1);
  355.         if( hdr[7]&0x40 )    /* has dts */
  356.         {
  357.              i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
  358.                      (mtime_t)(hdr[15] << 22)|
  359.                     ((mtime_t)(hdr[16]&0xfe) << 14)|
  360.                      (mtime_t)(hdr[17] << 7)|
  361.                      (mtime_t)(hdr[18] >> 1);
  362.         }
  363.     }
  364.     p_pes = block_ChainGather( p_pes );
  365.     if( p_pes->i_buffer <= i_skip )
  366.     {
  367.         block_ChainRelease( p_pes );
  368.         return;
  369.     }
  370.     p_pes->i_buffer -= i_skip;
  371.     p_pes->p_buffer += i_skip;
  372.     if( i_dts >= 0 ) p_pes->i_dts = i_dts * 100 / 9;
  373.     if( i_pts >= 0 ) p_pes->i_pts = i_pts * 100 / 9;
  374.     /* Set PCR */
  375.     if( p_pes->i_pts > 0 )
  376.     {
  377.         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
  378.         p_sys->b_pcr_audio = true;
  379.     }
  380.     es_out_Send( p_demux->out, p_sys->p_audio, p_pes );
  381. }