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

多媒体

开发平台:

MultiPlatform

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