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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * mpeg_audio.c: parse MPEG audio sync info and packetize the stream
  3.  *****************************************************************************
  4.  * Copyright (C) 2001-2003 VideoLAN
  5.  * $Id: mpeg_audio.c 8561 2004-08-29 01:28:28Z gbazin $
  6.  *
  7.  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  8.  *          Eric Petit <titer@videolan.org>
  9.  *          Christophe Massiot <massiot@via.ecp.fr>
  10.  *          Gildas Bazin <gbazin@netcourrier.com>
  11.  *
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  25.  *****************************************************************************/
  26. /*****************************************************************************
  27.  * Preamble
  28.  *****************************************************************************/
  29. #include <vlc/vlc.h>
  30. #include <vlc/decoder.h>
  31. #include <vlc/aout.h>
  32. #include "vlc_block_helper.h"
  33. /*****************************************************************************
  34.  * decoder_sys_t : decoder descriptor
  35.  *****************************************************************************/
  36. struct decoder_sys_t
  37. {
  38.     /* Module mode */
  39.     vlc_bool_t b_packetizer;
  40.     /*
  41.      * Input properties
  42.      */
  43.     int        i_state;
  44.     block_bytestream_t bytestream;
  45.     /*
  46.      * Common properties
  47.      */
  48.     audio_date_t          end_date;
  49.     unsigned int          i_current_layer;
  50.     mtime_t i_pts;
  51.     int i_frame_size, i_free_frame_size;
  52.     unsigned int i_channels_conf, i_channels;
  53.     unsigned int i_rate, i_max_frame_size, i_frame_length;
  54.     unsigned int i_layer, i_bit_rate;
  55. };
  56. enum {
  57.     STATE_NOSYNC,
  58.     STATE_SYNC,
  59.     STATE_HEADER,
  60.     STATE_NEXT_SYNC,
  61.     STATE_GET_DATA,
  62.     STATE_SEND_DATA
  63. };
  64. /* This isn't the place to put mad-specific stuff. However, it makes the
  65.  * mad plug-in's life much easier if we put 8 extra bytes at the end of the
  66.  * buffer, because that way it doesn't have to copy the aout_buffer_t to a
  67.  * bigger buffer. This has no implication on other plug-ins, and we only
  68.  * lose 8 bytes per frame. --Meuuh */
  69. #define MAD_BUFFER_GUARD 8
  70. #define MPGA_HEADER_SIZE 4
  71. /****************************************************************************
  72.  * Local prototypes
  73.  ****************************************************************************/
  74. static int  OpenDecoder   ( vlc_object_t * );
  75. static int  OpenPacketizer( vlc_object_t * );
  76. static void CloseDecoder  ( vlc_object_t * );
  77. static void *DecodeBlock  ( decoder_t *, block_t ** );
  78. static uint8_t       *GetOutBuffer ( decoder_t *, void ** );
  79. static aout_buffer_t *GetAoutBuffer( decoder_t * );
  80. static block_t       *GetSoutBuffer( decoder_t * );
  81. static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
  82.                      unsigned int * pi_channels_conf,
  83.                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
  84.                      unsigned int * pi_frame_length,
  85.                      unsigned int * pi_max_frame_size,
  86.                      unsigned int * pi_layer );
  87. /*****************************************************************************
  88.  * Module descriptor
  89.  *****************************************************************************/
  90. vlc_module_begin();
  91.     set_description( _("MPEG audio layer I/II/III parser") );
  92. #if defined(SYS_DARWIN)
  93.     set_capability( "decoder", 5 );
  94. #else
  95.     set_capability( "decoder", 100 );
  96. #endif
  97.     set_callbacks( OpenDecoder, CloseDecoder );
  98.     add_submodule();
  99.     set_description( _("MPEG audio layer I/II/III packetizer") );
  100.     set_capability( "packetizer", 10 );
  101.     set_callbacks( OpenPacketizer, CloseDecoder );
  102. vlc_module_end();
  103. /*****************************************************************************
  104.  * OpenDecoder: probe the decoder and return score
  105.  *****************************************************************************/
  106. static int OpenDecoder( vlc_object_t *p_this )
  107. {
  108.     decoder_t *p_dec = (decoder_t*)p_this;
  109.     decoder_sys_t *p_sys;
  110.     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','a') )
  111.     {
  112.         return VLC_EGENERIC;
  113.     }
  114.     /* Allocate the memory needed to store the decoder's structure */
  115.     if( ( p_dec->p_sys = p_sys =
  116.           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
  117.     {
  118.         msg_Err( p_dec, "out of memory" );
  119.         return VLC_EGENERIC;
  120.     }
  121.     /* Misc init */
  122.     p_sys->b_packetizer = VLC_FALSE;
  123.     p_sys->i_state = STATE_NOSYNC;
  124.     aout_DateSet( &p_sys->end_date, 0 );
  125.     p_sys->bytestream = block_BytestreamInit( p_dec );
  126.     /* Set output properties */
  127.     p_dec->fmt_out.i_cat = AUDIO_ES;
  128.     p_dec->fmt_out.i_codec = VLC_FOURCC('m','p','g','a');
  129.     p_dec->fmt_out.audio.i_rate = 0; /* So end_date gets initialized */
  130.     /* Set callback */
  131.     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
  132.         DecodeBlock;
  133.     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
  134.         DecodeBlock;
  135.     /* Start with the minimum size for a free bitrate frame */
  136.     p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
  137.     return VLC_SUCCESS;
  138. }
  139. static int OpenPacketizer( vlc_object_t *p_this )
  140. {
  141.     decoder_t *p_dec = (decoder_t*)p_this;
  142.     int i_ret = OpenDecoder( p_this );
  143.     if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
  144.     return i_ret;
  145. }
  146. /****************************************************************************
  147.  * DecodeBlock: the whole thing
  148.  ****************************************************************************
  149.  * This function is called just after the thread is launched.
  150.  ****************************************************************************/
  151. static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
  152. {
  153.     decoder_sys_t *p_sys = p_dec->p_sys;
  154.     uint8_t p_header[MAD_BUFFER_GUARD];
  155.     uint32_t i_header;
  156.     uint8_t *p_buf;
  157.     void *p_out_buffer;
  158.     if( !pp_block || !*pp_block ) return NULL;
  159.     if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
  160.     {
  161.         /* We've just started the stream, wait for the first PTS. */
  162.         block_Release( *pp_block );
  163.         return NULL;
  164.     }
  165.     if( (*pp_block)->i_flags&BLOCK_FLAG_DISCONTINUITY )
  166.     {
  167.         p_sys->i_state = STATE_NOSYNC;
  168.     }
  169.     block_BytestreamPush( &p_sys->bytestream, *pp_block );
  170.     while( 1 )
  171.     {
  172.         switch( p_sys->i_state )
  173.         {
  174.         case STATE_NOSYNC:
  175.             while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
  176.                    == VLC_SUCCESS )
  177.             {
  178.                 /* Look for sync word - should be 0xffe */
  179.                 if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
  180.                 {
  181.                     p_sys->i_state = STATE_SYNC;
  182.                     break;
  183.                 }
  184.                 block_SkipByte( &p_sys->bytestream );
  185.             }
  186.             if( p_sys->i_state != STATE_SYNC )
  187.             {
  188.                 block_BytestreamFlush( &p_sys->bytestream );
  189.                 /* Need more data */
  190.                 return NULL;
  191.             }
  192.         case STATE_SYNC:
  193.             /* New frame, set the Presentation Time Stamp */
  194.             p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
  195.             if( p_sys->i_pts != 0 &&
  196.                 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
  197.             {
  198.                 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
  199.             }
  200.             p_sys->i_state = STATE_HEADER;
  201.         case STATE_HEADER:
  202.             /* Get MPGA frame header (MPGA_HEADER_SIZE bytes) */
  203.             if( block_PeekBytes( &p_sys->bytestream, p_header,
  204.                                  MPGA_HEADER_SIZE ) != VLC_SUCCESS )
  205.             {
  206.                 /* Need more data */
  207.                 return NULL;
  208.             }
  209.             /* Build frame header */
  210.             i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
  211.                        |p_header[3];
  212.             /* Check if frame is valid and get frame info */
  213.             p_sys->i_frame_size = SyncInfo( i_header,
  214.                                             &p_sys->i_channels,
  215.                                             &p_sys->i_channels_conf,
  216.                                             &p_sys->i_rate,
  217.                                             &p_sys->i_bit_rate,
  218.                                             &p_sys->i_frame_length,
  219.                                             &p_sys->i_max_frame_size,
  220.                                             &p_sys->i_layer );
  221.             if( p_sys->i_frame_size == -1 )
  222.             {
  223.                 msg_Dbg( p_dec, "emulated startcode" );
  224.                 block_SkipByte( &p_sys->bytestream );
  225.                 p_sys->i_state = STATE_NOSYNC;
  226.                 break;
  227.             }
  228.             if( p_sys->i_bit_rate == 0 )
  229.             {
  230.                 /* Free birate, but 99% emulated startcode :( */
  231.                 if( p_dec->p_sys->i_free_frame_size == MPGA_HEADER_SIZE )
  232.                 {
  233.                     msg_Dbg( p_dec, "free bitrate mode");
  234.                 }
  235.                 p_sys->i_frame_size = p_sys->i_free_frame_size;
  236.             }
  237.             p_sys->i_state = STATE_NEXT_SYNC;
  238.         case STATE_NEXT_SYNC:
  239.             /* TODO: If p_block == NULL, flush the buffer without checking the
  240.              * next sync word */
  241.             /* Check if next expected frame contains the sync word */
  242.             if( block_PeekOffsetBytes( &p_sys->bytestream,
  243.                                        p_sys->i_frame_size, p_header,
  244.                                        MAD_BUFFER_GUARD ) != VLC_SUCCESS )
  245.             {
  246.                 /* Need more data */
  247.                 return NULL;
  248.             }
  249.             if( p_header[0] == 0xff && (p_header[1] & 0xe0) == 0xe0 )
  250.             {
  251.                 /* Startcode is fine, let's try the header as an extra check */
  252.                 int i_next_frame_size;
  253.                 unsigned int i_next_channels, i_next_channels_conf;
  254.                 unsigned int i_next_rate, i_next_bit_rate;
  255.                 unsigned int i_next_frame_length, i_next_max_frame_size;
  256.                 unsigned int i_next_layer;
  257.                 /* Build frame header */
  258.                 i_header = (p_header[0]<<24)|(p_header[1]<<16)|(p_header[2]<<8)
  259.                            |p_header[3];
  260.                 i_next_frame_size = SyncInfo( i_header,
  261.                                               &i_next_channels,
  262.                                               &i_next_channels_conf,
  263.                                               &i_next_rate,
  264.                                               &i_next_bit_rate,
  265.                                               &i_next_frame_length,
  266.                                               &i_next_max_frame_size,
  267.                                               &i_next_layer );
  268.                 /* Free bitrate only */
  269.                 if( p_sys->i_bit_rate == 0 && i_next_frame_size == -1 )
  270.                 {
  271.                     if( (unsigned int)p_sys->i_frame_size >
  272.                         p_sys->i_max_frame_size )
  273.                     {
  274.                         msg_Dbg( p_dec, "frame too big %d > %d "
  275.                                  "(emulated startcode ?)", p_sys->i_frame_size,
  276.                                  p_sys->i_max_frame_size );
  277.                         block_SkipByte( &p_sys->bytestream );
  278.                         p_sys->i_state = STATE_NOSYNC;
  279.                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
  280.                         break;
  281.                     }
  282.                     p_sys->i_frame_size++;
  283.                     break;
  284.                 }
  285.                 if( i_next_frame_size == -1 )
  286.                 {
  287.                     msg_Dbg( p_dec, "emulated startcode on next frame" );
  288.                     block_SkipByte( &p_sys->bytestream );
  289.                     p_sys->i_state = STATE_NOSYNC;
  290.                     break;
  291.                 }
  292.                 /* Check info is in sync with previous one */
  293.                 if( i_next_channels_conf != p_sys->i_channels_conf ||
  294.                     i_next_rate != p_sys->i_rate ||
  295.                     i_next_layer != p_sys->i_layer ||
  296.                     i_next_frame_length != p_sys->i_frame_length )
  297.                 {
  298.                     /* Free bitrate only */
  299.                     if( p_sys->i_bit_rate == 0 )
  300.                     {
  301.                         p_sys->i_frame_size++;
  302.                         break;
  303.                     }
  304.                     msg_Dbg( p_dec, "parameters changed unexpectedly "
  305.                              "(emulated startcode ?)" );
  306.                     block_SkipByte( &p_sys->bytestream );
  307.                     p_sys->i_state = STATE_NOSYNC;
  308.                     break;
  309.                 }
  310.                 /* Free bitrate only */
  311.                 if( p_sys->i_bit_rate == 0 )
  312.                 {
  313.                     if( i_next_bit_rate != 0 )
  314.                     {
  315.                         p_sys->i_frame_size++;
  316.                         break;
  317.                     }
  318.                 }
  319.             }
  320.             else
  321.             {
  322.                 /* Free bitrate only */
  323.                 if( p_sys->i_bit_rate == 0 )
  324.                 {
  325.                     if( (unsigned int)p_sys->i_frame_size >
  326.                         p_sys->i_max_frame_size )
  327.                     {
  328.                         msg_Dbg( p_dec, "frame too big %d > %d "
  329.                                  "(emulated startcode ?)", p_sys->i_frame_size,
  330.                                  p_sys->i_max_frame_size );
  331.                         block_SkipByte( &p_sys->bytestream );
  332.                         p_sys->i_state = STATE_NOSYNC;
  333.                         p_sys->i_free_frame_size = MPGA_HEADER_SIZE;
  334.                         break;
  335.                     }
  336.                     p_sys->i_frame_size++;
  337.                     break;
  338.                 }
  339.                 msg_Dbg( p_dec, "emulated startcode "
  340.                          "(no startcode on following frame)" );
  341.                 p_sys->i_state = STATE_NOSYNC;
  342.                 block_SkipByte( &p_sys->bytestream );
  343.                 break;
  344.             }
  345.             p_sys->i_state = STATE_SEND_DATA;
  346.             break;
  347.         case STATE_GET_DATA:
  348.             /* Make sure we have enough data.
  349.              * (Not useful if we went through NEXT_SYNC) */
  350.             if( block_WaitBytes( &p_sys->bytestream,
  351.                                  p_sys->i_frame_size ) != VLC_SUCCESS )
  352.             {
  353.                 /* Need more data */
  354.                 return NULL;
  355.             }
  356.             p_sys->i_state = STATE_SEND_DATA;
  357.         case STATE_SEND_DATA:
  358.             if( !(p_buf = GetOutBuffer( p_dec, &p_out_buffer )) )
  359.             {
  360.                 //p_dec->b_error = VLC_TRUE;
  361.                 return NULL;
  362.             }
  363.             /* Free bitrate only */
  364.             if( p_sys->i_bit_rate == 0 )
  365.             {
  366.                 p_sys->i_free_frame_size = p_sys->i_frame_size;
  367.             }
  368.             /* Copy the whole frame into the buffer. When we reach this point
  369.              * we already know we have enough data available. */
  370.             block_GetBytes( &p_sys->bytestream, p_buf, p_sys->i_frame_size );
  371.             /* Get beginning of next frame for libmad */
  372.             if( !p_sys->b_packetizer )
  373.             {
  374.                 memcpy( p_buf + p_sys->i_frame_size,
  375.                         p_header, MAD_BUFFER_GUARD );
  376.             }
  377.             p_sys->i_state = STATE_NOSYNC;
  378.             /* Make sure we don't reuse the same pts twice */
  379.             if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
  380.                 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
  381.             /* So p_block doesn't get re-added several times */
  382.             *pp_block = block_BytestreamPop( &p_sys->bytestream );
  383.             return p_out_buffer;
  384.         }
  385.     }
  386.     return NULL;
  387. }
  388. /*****************************************************************************
  389.  * GetOutBuffer:
  390.  *****************************************************************************/
  391. static uint8_t *GetOutBuffer( decoder_t *p_dec, void **pp_out_buffer )
  392. {
  393.     decoder_sys_t *p_sys = p_dec->p_sys;
  394.     uint8_t *p_buf;
  395.     if( p_dec->fmt_out.audio.i_rate != p_sys->i_rate )
  396.     {
  397.         msg_Info( p_dec, "MPGA channels:%d samplerate:%d bitrate:%d",
  398.                   p_sys->i_channels, p_sys->i_rate, p_sys->i_bit_rate );
  399.         aout_DateInit( &p_sys->end_date, p_sys->i_rate );
  400.         aout_DateSet( &p_sys->end_date, p_sys->i_pts );
  401.     }
  402.     p_dec->fmt_out.audio.i_rate     = p_sys->i_rate;
  403.     p_dec->fmt_out.audio.i_channels = p_sys->i_channels;
  404.     p_dec->fmt_out.audio.i_frame_length = p_sys->i_frame_length;
  405.     p_dec->fmt_out.audio.i_bytes_per_frame =
  406.         p_sys->i_max_frame_size + MAD_BUFFER_GUARD;
  407.     p_dec->fmt_out.audio.i_original_channels = p_sys->i_channels_conf;
  408.     p_dec->fmt_out.audio.i_physical_channels =
  409.         p_sys->i_channels_conf & AOUT_CHAN_PHYSMASK;
  410.     p_dec->fmt_out.i_bitrate = p_sys->i_bit_rate * 1000;
  411.     if( p_sys->b_packetizer )
  412.     {
  413.         block_t *p_sout_buffer = GetSoutBuffer( p_dec );
  414.         p_buf = p_sout_buffer ? p_sout_buffer->p_buffer : NULL;
  415.         *pp_out_buffer = p_sout_buffer;
  416.     }
  417.     else
  418.     {
  419.         aout_buffer_t *p_aout_buffer = GetAoutBuffer( p_dec );
  420.         p_buf = p_aout_buffer ? p_aout_buffer->p_buffer : NULL;
  421.         *pp_out_buffer = p_aout_buffer;
  422.     }
  423.     return p_buf;
  424. }
  425. /*****************************************************************************
  426.  * GetAoutBuffer:
  427.  *****************************************************************************/
  428. static aout_buffer_t *GetAoutBuffer( decoder_t *p_dec )
  429. {
  430.     decoder_sys_t *p_sys = p_dec->p_sys;
  431.     aout_buffer_t *p_buf;
  432.     p_buf = p_dec->pf_aout_buffer_new( p_dec, p_sys->i_frame_length );
  433.     if( p_buf == NULL ) return NULL;
  434.     p_buf->start_date = aout_DateGet( &p_sys->end_date );
  435.     p_buf->end_date =
  436.         aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
  437.     /* Hack for libmad filter */
  438.     p_buf->i_nb_bytes = p_sys->i_frame_size + MAD_BUFFER_GUARD;
  439.     return p_buf;
  440. }
  441. /*****************************************************************************
  442.  * GetSoutBuffer:
  443.  *****************************************************************************/
  444. static block_t *GetSoutBuffer( decoder_t *p_dec )
  445. {
  446.     decoder_sys_t *p_sys = p_dec->p_sys;
  447.     block_t *p_block;
  448.     p_block = block_New( p_dec, p_sys->i_frame_size );
  449.     if( p_block == NULL ) return NULL;
  450.     p_block->i_pts = p_block->i_dts = aout_DateGet( &p_sys->end_date );
  451.     p_block->i_length =
  452.         aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length ) -
  453.             p_block->i_pts;
  454.     return p_block;
  455. }
  456. /*****************************************************************************
  457.  * CloseDecoder: clean up the decoder
  458.  *****************************************************************************/
  459. static void CloseDecoder( vlc_object_t *p_this )
  460. {
  461.     decoder_t *p_dec = (decoder_t *)p_this;
  462.     decoder_sys_t *p_sys = p_dec->p_sys;
  463.     block_BytestreamRelease( &p_sys->bytestream );
  464.     free( p_sys );
  465. }
  466. /*****************************************************************************
  467.  * SyncInfo: parse MPEG audio sync info
  468.  *****************************************************************************/
  469. static int SyncInfo( uint32_t i_header, unsigned int * pi_channels,
  470.                      unsigned int * pi_channels_conf,
  471.                      unsigned int * pi_sample_rate, unsigned int * pi_bit_rate,
  472.                      unsigned int * pi_frame_length,
  473.                      unsigned int * pi_max_frame_size, unsigned int * pi_layer)
  474. {
  475.     static const int ppi_bitrate[2][3][16] =
  476.     {
  477.         {
  478.             /* v1 l1 */
  479.             { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384,
  480.               416, 448, 0},
  481.             /* v1 l2 */
  482.             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256,
  483.               320, 384, 0},
  484.             /* v1 l3 */
  485.             { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224,
  486.               256, 320, 0}
  487.         },
  488.         {
  489.             /* v2 l1 */
  490.             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192,
  491.               224, 256, 0},
  492.             /* v2 l2 */
  493.             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
  494.               144, 160, 0},
  495.             /* v2 l3 */
  496.             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128,
  497.               144, 160, 0}
  498.         }
  499.     };
  500.     static const int ppi_samplerate[2][4] = /* version 1 then 2 */
  501.     {
  502.         { 44100, 48000, 32000, 0 },
  503.         { 22050, 24000, 16000, 0 }
  504.     };
  505.     int i_version, i_mode, i_emphasis;
  506.     vlc_bool_t b_padding, b_mpeg_2_5, b_crc;
  507.     int i_frame_size = 0;
  508.     int i_bitrate_index, i_samplerate_index;
  509.     int i_max_bit_rate;
  510.     b_mpeg_2_5  = 1 - ((i_header & 0x100000) >> 20);
  511.     i_version   = 1 - ((i_header & 0x80000) >> 19);
  512.     *pi_layer   = 4 - ((i_header & 0x60000) >> 17);
  513.     b_crc = !((i_header >> 16) & 0x01);
  514.     i_bitrate_index = (i_header & 0xf000) >> 12;
  515.     i_samplerate_index = (i_header & 0xc00) >> 10;
  516.     b_padding   = (i_header & 0x200) >> 9;
  517.     /* Extension */
  518.     i_mode      = (i_header & 0xc0) >> 6;
  519.     /* Modeext, copyright & original */
  520.     i_emphasis  = i_header & 0x3;
  521.     if( *pi_layer != 4 &&
  522.         i_bitrate_index < 0x0f &&
  523.         i_samplerate_index != 0x03 &&
  524.         i_emphasis != 0x02 )
  525.     {
  526.         switch ( i_mode )
  527.         {
  528.         case 0: /* stereo */
  529.         case 1: /* joint stereo */
  530.             *pi_channels = 2;
  531.             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
  532.             break;
  533.         case 2: /* dual-mono */
  534.             *pi_channels = 2;
  535.             *pi_channels_conf = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
  536.                                 | AOUT_CHAN_DUALMONO;
  537.             break;
  538.         case 3: /* mono */
  539.             *pi_channels = 1;
  540.             *pi_channels_conf = AOUT_CHAN_CENTER;
  541.             break;
  542.         }
  543.         *pi_bit_rate = ppi_bitrate[i_version][*pi_layer-1][i_bitrate_index];
  544.         i_max_bit_rate = ppi_bitrate[i_version][*pi_layer-1][14];
  545.         *pi_sample_rate = ppi_samplerate[i_version][i_samplerate_index];
  546.         if ( b_mpeg_2_5 )
  547.         {
  548.             *pi_sample_rate >>= 1;
  549.         }
  550.         switch( *pi_layer )
  551.         {
  552.         case 1:
  553.             i_frame_size = ( 12000 * *pi_bit_rate / *pi_sample_rate +
  554.                            b_padding ) * 4;
  555.             *pi_max_frame_size = ( 12000 * i_max_bit_rate /
  556.                                  *pi_sample_rate + 1 ) * 4;
  557.             *pi_frame_length = 384;
  558.             break;
  559.         case 2:
  560.             i_frame_size = 144000 * *pi_bit_rate / *pi_sample_rate + b_padding;
  561.             *pi_max_frame_size = 144000 * i_max_bit_rate / *pi_sample_rate + 1;
  562.             *pi_frame_length = 1152;
  563.             break;
  564.         case 3:
  565.             i_frame_size = ( i_version ? 72000 : 144000 ) *
  566.                            *pi_bit_rate / *pi_sample_rate + b_padding;
  567.             *pi_max_frame_size = ( i_version ? 72000 : 144000 ) *
  568.                                  i_max_bit_rate / *pi_sample_rate + 1;
  569.             *pi_frame_length = i_version ? 576 : 1152;
  570.             break;
  571.         default:
  572.             break;
  573.         }
  574.     }
  575.     else
  576.     {
  577.         return -1;
  578.     }
  579.     return i_frame_size;
  580. }