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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * voc.c : Creative Voice File (.VOC) demux module for vlc
  3.  *****************************************************************************
  4.  * Copyright (C) 2005 Rémi Denis-Courmont
  5.  * $Id: 13d86d1b28feb78de8bae83fa220bb5d273703fe $
  6.  *
  7.  * Authors: Rémi Denis-Courmont <rem # videolan.org>
  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. #include <vlc_aout.h>
  33. #include <vlc_codecs.h>
  34. /*****************************************************************************
  35.  * Module descriptor
  36.  *****************************************************************************/
  37. static int  Open ( vlc_object_t * );
  38. static void Close( vlc_object_t * );
  39. vlc_module_begin ()
  40.     set_description( N_("VOC demuxer") )
  41.     set_category( CAT_INPUT )
  42.     set_subcategory( SUBCAT_INPUT_DEMUX )
  43.     set_capability( "demux", 10 )
  44.     set_callbacks( Open, Close )
  45. vlc_module_end ()
  46. /*****************************************************************************
  47.  * Local prototypes
  48.  *****************************************************************************/
  49. static int Demux  ( demux_t * );
  50. static int Control( demux_t *, int i_query, va_list args );
  51. struct demux_sys_t
  52. {
  53.     es_format_t     fmt;
  54.     es_out_id_t     *p_es;
  55.     int64_t         i_block_start;
  56.     int64_t         i_block_end;
  57.     int64_t         i_loop_offset;
  58.     unsigned        i_loop_count;
  59.     unsigned        i_silence_countdown;
  60.     date_t          pts;
  61. };
  62. static const char ct_header[] = "Creative Voice Filex1a";
  63. /*****************************************************************************
  64.  * Open: check file and initializes structures
  65.  *****************************************************************************/
  66. static int Open( vlc_object_t * p_this )
  67. {
  68.     demux_t     *p_demux = (demux_t*)p_this;
  69.     demux_sys_t *p_sys;
  70.     const uint8_t *p_buf;
  71.     uint16_t    i_data_offset, i_version;
  72.     if( stream_Peek( p_demux->s, &p_buf, 26 ) < 26 )
  73.         return VLC_EGENERIC;
  74.     if( memcmp( p_buf, ct_header, 20 ) )
  75.         return VLC_EGENERIC;
  76.     p_buf += 20;
  77.     i_data_offset = GetWLE( p_buf );
  78.     if ( i_data_offset < 26 /* not enough room for full VOC header */ )
  79.         return VLC_EGENERIC;
  80.     p_buf += 2;
  81.     i_version = GetWLE( p_buf );
  82.     if( ( i_version != 0x10A ) && ( i_version != 0x114 ) )
  83.         return VLC_EGENERIC; /* unknown VOC version */
  84.     p_buf += 2;
  85.     if( GetWLE( p_buf ) != (uint16_t)(0x1234 + ~i_version) )
  86.         return VLC_EGENERIC;
  87.     /* We have a valid VOC header */
  88.     msg_Dbg( p_demux, "CT Voice file v%d.%d", i_version >> 8,
  89.              i_version & 0xff );
  90.     /* skip VOC header */
  91.     if( stream_Read( p_demux->s, NULL, i_data_offset ) < i_data_offset )
  92.         return VLC_EGENERIC;
  93.     p_demux->pf_demux   = Demux;
  94.     p_demux->pf_control = Control;
  95.     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
  96.     if( p_sys == NULL )
  97.         return VLC_ENOMEM;
  98.     p_sys->i_silence_countdown = p_sys->i_block_start = p_sys->i_block_end =
  99.     p_sys->i_loop_count = 0;
  100.     p_sys->p_es = NULL;
  101.     date_Init( &p_sys->pts, 1, 1 );
  102.     date_Set( &p_sys->pts, 1 );
  103.     es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
  104.     return VLC_SUCCESS;
  105. }
  106. static int fmtcmp( es_format_t *ofmt, es_format_t *nfmt )
  107. {
  108.     return (ofmt->audio.i_bitspersample != nfmt->audio.i_bitspersample)
  109.         || (ofmt->audio.i_rate != nfmt->audio.i_rate)
  110.         || (ofmt->audio.i_channels != nfmt->audio.i_channels);
  111. }
  112. /*
  113.  * Converts old-style VOC sample rates to commonly used ones
  114.  * so as not to confuse sound card drivers.
  115.  * (I assume 16k, 24k and 32k are never found in .VOC files)
  116.  */
  117. static unsigned int fix_voc_sr( unsigned int sr )
  118. {
  119.     switch( sr )
  120.     {
  121.         /*case 8000:
  122.             return 8000;*/
  123.         case 11111:
  124.             return 11025;
  125.         case 22222:
  126.             return 22050;
  127.         case 44444:
  128.             return 44100;
  129.     }
  130.     return sr;
  131. }
  132. static int ReadBlockHeader( demux_t *p_demux )
  133. {
  134.     es_format_t     new_fmt;
  135.     uint8_t buf[8];
  136.     int32_t i_block_size;
  137.     demux_sys_t *p_sys = p_demux->p_sys;
  138.     if( stream_Read( p_demux->s, buf, 4 ) < 4 )
  139.         return VLC_EGENERIC; /* EOF */
  140.     i_block_size = GetDWLE( buf ) >> 8;
  141.     msg_Dbg( p_demux, "new block: type: %u, size: %u",
  142.              (unsigned)*buf, i_block_size );
  143.     es_format_Init( &new_fmt, AUDIO_ES, 0 );
  144.     switch( *buf )
  145.     {
  146.         case 0: /* not possible : caught with earlier stream_Read */
  147.             goto corrupt;
  148.         case 1:
  149.             if( i_block_size < 2 )
  150.                 goto corrupt;
  151.             i_block_size -= 2;
  152.             if( stream_Read( p_demux->s, buf, 2 ) < 2 )
  153.                 goto corrupt;
  154.             if( buf[1] )
  155.             {
  156.                 msg_Err( p_demux, "unsupported compression" );
  157.                 return VLC_EGENERIC;
  158.             }
  159.             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
  160.             new_fmt.audio.i_rate = fix_voc_sr( 1000000L / (256L - buf[0]) );
  161.             new_fmt.audio.i_bytes_per_frame = 1;
  162.             new_fmt.audio.i_frame_length = 1;
  163.             new_fmt.audio.i_channels = 1;
  164.             new_fmt.audio.i_blockalign = 1;
  165.             new_fmt.audio.i_bitspersample = 8;
  166.             new_fmt.i_bitrate = new_fmt.audio.i_rate * 8;
  167.             break;
  168.         case 2: /* data block with same format as the previous one */
  169.             if( p_sys->p_es == NULL )
  170.                 goto corrupt; /* no previous block! */
  171.             memcpy( &new_fmt, &p_sys->fmt, sizeof( new_fmt ) );
  172.             break;
  173.         case 3: /* silence block */
  174.             if( ( i_block_size != 3 )
  175.              || ( stream_Read( p_demux->s, buf, 3 ) < 3 ) )
  176.                 goto corrupt;
  177.             i_block_size = 0;
  178.             p_sys->i_silence_countdown = GetWLE( buf );
  179.             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
  180.             new_fmt.audio.i_rate = fix_voc_sr( 1000000L / (256L - buf[0]) );
  181.             new_fmt.audio.i_bytes_per_frame = 1;
  182.             new_fmt.audio.i_frame_length = 1;
  183.             new_fmt.audio.i_channels = 1;
  184.             new_fmt.audio.i_blockalign = 1;
  185.             new_fmt.audio.i_bitspersample = 8;
  186.             new_fmt.i_bitrate = new_fmt.audio.i_rate * 8;
  187.             break;
  188.         case 6: /* repeat block */
  189.             if( ( i_block_size != 2 )
  190.              || ( stream_Read( p_demux->s, buf, 2 ) < 2 ) )
  191.                 goto corrupt;
  192.             i_block_size = 0;
  193.             p_sys->i_loop_count = GetWLE( buf );
  194.             p_sys->i_loop_offset = stream_Tell( p_demux->s );
  195.             break;
  196.         case 7: /* repeat end block */
  197.             if( i_block_size != 0 )
  198.                 goto corrupt;
  199.             if( p_sys->i_loop_count > 0 )
  200.             {
  201.                 if( stream_Seek( p_demux->s, p_sys->i_loop_offset ) )
  202.                     msg_Warn( p_demux, "cannot loop: seek failed" );
  203.                 else
  204.                     p_sys->i_loop_count--;
  205.             }
  206.             break;
  207.         case 8:
  208.             /*
  209.              * Block 8 is a big kludge to add stereo support to block 1 :
  210.              * A block of type 8 is always followed by a block of type 1
  211.              * and specifies the number of channels in that 1-block
  212.              * (normally block 1 are always mono). In practice, block type 9
  213.              * is used for stereo rather than 8
  214.              */
  215.             if( ( i_block_size != 4 )
  216.              || ( stream_Read( p_demux->s, buf, 4 ) < 4 ) )
  217.                 goto corrupt;
  218.             if( buf[2] )
  219.             {
  220.                 msg_Err( p_demux, "unsupported compression" );
  221.                 return VLC_EGENERIC;
  222.             }
  223.             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
  224.             new_fmt.audio.i_channels = buf[3] + 1; /* can't be nul */
  225.             new_fmt.audio.i_rate = 256000000L /
  226.                           ((65536L - GetWLE(buf)) * new_fmt.audio.i_channels);
  227.             new_fmt.audio.i_bytes_per_frame = new_fmt.audio.i_channels;
  228.             new_fmt.audio.i_frame_length = 1;
  229.             new_fmt.audio.i_blockalign = new_fmt.audio.i_bytes_per_frame;
  230.             new_fmt.audio.i_bitspersample = 8 * new_fmt.audio.i_bytes_per_frame;
  231.             new_fmt.i_bitrate = new_fmt.audio.i_rate * 8;
  232.             /* read subsequent block 1 */
  233.             if( stream_Read( p_demux->s, buf, 4 ) < 4 )
  234.                 return VLC_EGENERIC; /* EOF */
  235.  
  236.             i_block_size = GetDWLE( buf ) >> 8;
  237.             msg_Dbg( p_demux, "new block: type: %u, size: %u",
  238.                     (unsigned)*buf, i_block_size );
  239.             if( i_block_size < 2 )
  240.                 goto corrupt;
  241.             i_block_size -= 2;
  242.             if( stream_Read( p_demux->s, buf, 2 ) < 2 )
  243.                 goto corrupt;
  244.             if( buf[1] )
  245.             {
  246.                 msg_Err( p_demux, "unsupported compression" );
  247.                 return VLC_EGENERIC;
  248.             }
  249.             break;
  250.         case 9: /* newer data block with channel number and bits resolution */
  251.             if( i_block_size < 12 )
  252.                 goto corrupt;
  253.             i_block_size -= 12;
  254.             if( ( stream_Read( p_demux->s, buf, 8 ) < 8 )
  255.              || ( stream_Read( p_demux->s, NULL, 4 ) < 4 ) )
  256.                 goto corrupt;
  257.             new_fmt.audio.i_rate = GetDWLE( buf );
  258.             new_fmt.audio.i_bitspersample = buf[4];
  259.             new_fmt.audio.i_channels = buf[5];
  260.             switch( GetWLE( &buf[6] ) ) /* format */
  261.             {
  262.                 case 0x0000: /* PCM */
  263.                     switch( new_fmt.audio.i_bitspersample )
  264.                     {
  265.                         case 8:
  266.                             new_fmt.i_codec = VLC_FOURCC('u','8',' ',' ');
  267.                             break;
  268.                         case 16:
  269.                             new_fmt.i_codec = VLC_FOURCC('u','1','6','l');
  270.                             break;
  271.                         default:
  272.                             msg_Err( p_demux, "unsupported bit res.: %u bits",
  273.                                      new_fmt.audio.i_bitspersample );
  274.                             return VLC_EGENERIC;
  275.                     }
  276.                     break;
  277.                 case 0x0004: /* signed */
  278.                     switch( new_fmt.audio.i_bitspersample )
  279.                     {
  280.                         case 8:
  281.                             new_fmt.i_codec = VLC_FOURCC('s','8',' ',' ');
  282.                             break;
  283.                         case 16:
  284.                             new_fmt.i_codec = VLC_FOURCC('s','1','6','l');
  285.                             break;
  286.                         default:
  287.                             msg_Err( p_demux, "unsupported bit res.: %u bits",
  288.                                      new_fmt.audio.i_bitspersample );
  289.                             return VLC_EGENERIC;
  290.                     }
  291.                     break;
  292.                 default:
  293.                     msg_Err( p_demux, "unsupported compression" );
  294.                     return VLC_EGENERIC;
  295.             }
  296.             new_fmt.audio.i_bytes_per_frame = new_fmt.audio.i_channels
  297.                 * (new_fmt.audio.i_bitspersample / 8);
  298.             new_fmt.audio.i_frame_length = 1;
  299.             new_fmt.audio.i_blockalign = new_fmt.audio.i_bytes_per_frame;
  300.             new_fmt.i_bitrate = 8 * new_fmt.audio.i_rate
  301.                                      * new_fmt.audio.i_bytes_per_frame;
  302.             break;
  303.         default:
  304.             msg_Dbg( p_demux, "unknown block type %u - skipping block",
  305.                      (unsigned)*buf);
  306.         case 4: /* blocks of non-audio types can be skipped */
  307.         case 5:
  308.             if( stream_Read( p_demux->s, NULL, i_block_size ) < i_block_size )
  309.                 goto corrupt;
  310.             i_block_size = 0;
  311.             break;
  312.     }
  313.     p_sys->i_block_start = stream_Tell( p_demux->s );
  314.     p_sys->i_block_end = p_sys->i_block_start + i_block_size;
  315.     if( i_block_size || p_sys->i_silence_countdown )
  316.     {
  317.         /* we've read a block with data in it - update decoder */
  318.         msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
  319.                  "freq: %d Hz, bitrate: %dKo/s, blockalign: %d, "
  320.                  "bits/samples: %d", (char *)&new_fmt.i_codec,
  321.                  new_fmt.audio.i_channels, new_fmt.audio.i_rate,
  322.                  new_fmt.i_bitrate / 8192, new_fmt.audio.i_blockalign,
  323.                  new_fmt.audio.i_bitspersample );
  324.         if( ( p_sys->p_es != NULL ) && fmtcmp( &p_sys->fmt, &new_fmt ) )
  325.         {
  326.             msg_Dbg( p_demux, "codec change needed" );
  327.             es_out_Del( p_demux->out, p_sys->p_es );
  328.             p_sys->p_es = NULL;
  329.         }
  330.         if( p_sys->p_es == NULL )
  331.         {
  332.             memcpy( &p_sys->fmt, &new_fmt, sizeof( p_sys->fmt ) );
  333.             date_Change( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
  334.             p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
  335.         }
  336.     }
  337.     return VLC_SUCCESS;
  338. corrupt:
  339.     msg_Err( p_demux, "corrupted file - halting demux" );
  340.     return VLC_EGENERIC;
  341. }
  342. /*****************************************************************************
  343.  * Demux: read packet and send them to decoders
  344.  *****************************************************************************
  345.  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
  346.  *****************************************************************************/
  347. #define SAMPLES_BUFFER 1000
  348. static int Demux( demux_t *p_demux )
  349. {
  350.     demux_sys_t *p_sys = p_demux->p_sys;
  351.     block_t     *p_block;
  352.     int64_t     i_offset, i;
  353.     i_offset = stream_Tell( p_demux->s );
  354.     while( ( i_offset >= p_sys->i_block_end )
  355.          && ( p_sys->i_silence_countdown == 0 ) )
  356.         if( ReadBlockHeader( p_demux ) != VLC_SUCCESS )
  357.             return 0;
  358.     if( p_sys->i_silence_countdown == 0 )
  359.     {
  360.         i = ( p_sys->i_block_end - i_offset )
  361.             / p_sys->fmt.audio.i_bytes_per_frame;
  362.         if( i > SAMPLES_BUFFER )
  363.             i = SAMPLES_BUFFER;
  364.         p_block = stream_Block( p_demux->s,
  365.                                 p_sys->fmt.audio.i_bytes_per_frame * i );
  366.         if( p_block == NULL )
  367.         {
  368.             msg_Warn( p_demux, "cannot read data" );
  369.             return 0;
  370.         }
  371.     }
  372.     else
  373.     {   /* emulates silence from the stream */
  374.         i = p_sys->i_silence_countdown;
  375.         if( i > SAMPLES_BUFFER )
  376.             i = SAMPLES_BUFFER;
  377.         p_block = block_New( p_demux, i );
  378.         if( p_block == NULL )
  379.             return VLC_ENOMEM;
  380.         memset( p_block->p_buffer, 0, i );
  381.         p_sys->i_silence_countdown -= i;
  382.     }
  383.     p_block->i_dts = p_block->i_pts =
  384.         date_Increment( &p_sys->pts, p_sys->fmt.audio.i_frame_length * i );
  385.     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
  386.     es_out_Send( p_demux->out, p_sys->p_es, p_block );
  387.     return 1;
  388. }
  389. /*****************************************************************************
  390.  * Close: frees unused data
  391.  *****************************************************************************/
  392. static void Close ( vlc_object_t * p_this )
  393. {
  394.     demux_sys_t *p_sys  = ((demux_t *)p_this)->p_sys;
  395.     free( p_sys );
  396. }
  397. /*****************************************************************************
  398.  * Control:
  399.  *****************************************************************************/
  400. static int Control( demux_t *p_demux, int i_query, va_list args )
  401. {
  402.     demux_sys_t *p_sys  = p_demux->p_sys;
  403.     return demux_vaControlHelper( p_demux->s, p_sys->i_block_start,
  404.                                    p_sys->i_block_end,
  405.                                    p_sys->fmt.i_bitrate,
  406.                                    p_sys->fmt.audio.i_blockalign,
  407.                                    i_query, args );
  408. }