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

midi

开发平台:

Unix_Linux

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