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

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * decoder.c: AAC decoder using libfaad2
  3.  *****************************************************************************
  4.  * Copyright (C) 2001, 2003 VideoLAN
  5.  * $Id: faad.c 8777 2004-09-23 14:08:11Z hartman $
  6.  *
  7.  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  8.  *          Gildas Bazin <gbazin@videolan.org>
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. #include <vlc/vlc.h>
  25. #include <vlc/aout.h>
  26. #include <vlc/decoder.h>
  27. #include <faad.h>
  28. /*****************************************************************************
  29.  * Module descriptor
  30.  *****************************************************************************/
  31. static int  Open( vlc_object_t * );
  32. static void Close( vlc_object_t * );
  33. vlc_module_begin();
  34.     set_description( _("AAC audio decoder (using libfaad2)") );
  35.     set_capability( "decoder", 100 );
  36.     set_callbacks( Open, Close );
  37. vlc_module_end();
  38. /****************************************************************************
  39.  * Local prototypes
  40.  ****************************************************************************/
  41. static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
  42. static void DoReordering( decoder_t *, uint32_t *, uint32_t *, int, int,
  43.                           uint32_t * );
  44. #define MAX_CHANNEL_POSITIONS 9
  45. struct decoder_sys_t
  46. {
  47.     /* faad handler */
  48.     faacDecHandle *hfaad;
  49.     /* samples */
  50.     audio_date_t date;
  51.     /* temporary buffer */
  52.     uint8_t *p_buffer;
  53.     int     i_buffer;
  54.     int     i_buffer_size;
  55.     /* Channel positions of the current stream (for re-ordering) */
  56.     uint32_t pi_channel_positions[MAX_CHANNEL_POSITIONS];
  57. };
  58. static const uint32_t pi_channels_in[MAX_CHANNEL_POSITIONS] =
  59.     { FRONT_CHANNEL_CENTER, FRONT_CHANNEL_LEFT, FRONT_CHANNEL_RIGHT,
  60.       SIDE_CHANNEL_LEFT, SIDE_CHANNEL_RIGHT,
  61.       BACK_CHANNEL_LEFT, BACK_CHANNEL_RIGHT,
  62.       BACK_CHANNEL_CENTER, LFE_CHANNEL };
  63. static const uint32_t pi_channels_out[MAX_CHANNEL_POSITIONS] =
  64.     { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
  65.       AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
  66.       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
  67.       AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE };
  68. static const uint32_t pi_channels_ordered[MAX_CHANNEL_POSITIONS] =
  69.     { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
  70.       AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
  71.       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
  72.       AOUT_CHAN_CENTER, AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE
  73.     };
  74. /*****************************************************************************
  75.  * OpenDecoder: probe the decoder and return score
  76.  *****************************************************************************/
  77. static int Open( vlc_object_t *p_this )
  78. {
  79.     decoder_t *p_dec = (decoder_t*)p_this;
  80.     decoder_sys_t *p_sys = p_dec->p_sys;
  81.     faacDecConfiguration *cfg;
  82.     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','4','a') )
  83.     {
  84.         return VLC_EGENERIC;
  85.     }
  86.     /* Allocate the memory needed to store the decoder's structure */
  87.     if( ( p_dec->p_sys = p_sys =
  88.           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
  89.     {
  90.         msg_Err( p_dec, "out of memory" );
  91.         return VLC_EGENERIC;
  92.     }
  93.     /* Open a faad context */
  94.     if( ( p_sys->hfaad = faacDecOpen() ) == NULL )
  95.     {
  96.         msg_Err( p_dec, "cannot initialize faad" );
  97.         return VLC_EGENERIC;
  98.     }
  99.     /* Misc init */
  100.     aout_DateSet( &p_sys->date, 0 );
  101.     p_dec->fmt_out.i_cat = AUDIO_ES;
  102.     if (p_this->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
  103.         p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
  104.     else
  105.         p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
  106.     p_dec->pf_decode_audio = DecodeBlock;
  107.     p_dec->fmt_out.audio.i_physical_channels =
  108.         p_dec->fmt_out.audio.i_original_channels = 0;
  109.     if( p_dec->fmt_in.i_extra > 0 )
  110.     {
  111.         /* We have a decoder config so init the handle */
  112.         unsigned long i_rate;
  113.         unsigned char i_channels;
  114.         if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
  115.                           p_dec->fmt_in.i_extra,
  116.                           &i_rate, &i_channels ) < 0 )
  117.         {
  118.             return VLC_EGENERIC;
  119.         }
  120.         p_dec->fmt_out.audio.i_rate = i_rate;
  121.         p_dec->fmt_out.audio.i_channels = i_channels;
  122.         aout_DateInit( &p_sys->date, i_rate );
  123.     }
  124.     else
  125.     {
  126.         /* Will be initalised from first frame */
  127.         p_dec->fmt_out.audio.i_rate = 0;
  128.         p_dec->fmt_out.audio.i_channels = 0;
  129.     }
  130.     /* Set the faad config */
  131.     cfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
  132.     if (p_this->p_libvlc->i_cpu & CPU_CAPABILITY_FPU)
  133.         cfg->outputFormat = FAAD_FMT_FLOAT;
  134.     else
  135.         cfg->outputFormat = FAAD_FMT_16BIT;
  136.     faacDecSetConfiguration( p_sys->hfaad, cfg );
  137.     /* buffer */
  138.     p_sys->i_buffer = p_sys->i_buffer_size = 0;
  139.     p_sys->p_buffer = 0;
  140.     /* Faad2 can't deal with truncated data (eg. from MPEG TS) */
  141.     p_dec->b_need_packetized = VLC_TRUE;
  142.     return VLC_SUCCESS;
  143. }
  144. /*****************************************************************************
  145.  * DecodeBlock:
  146.  *****************************************************************************/
  147. static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
  148. {
  149.     decoder_sys_t *p_sys = p_dec->p_sys;
  150.     block_t *p_block;
  151.     if( !pp_block || !*pp_block ) return NULL;
  152.     p_block = *pp_block;
  153.     if( p_block->i_flags&BLOCK_FLAG_DISCONTINUITY )
  154.     {
  155.         block_Release( p_block );
  156.         return NULL;
  157.     }
  158.     /* Append the block to the temporary buffer */
  159.     if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
  160.     {
  161.         p_sys->i_buffer_size = p_sys->i_buffer + p_block->i_buffer;
  162.         p_sys->p_buffer = realloc( p_sys->p_buffer, p_sys->i_buffer_size );
  163.     }
  164.     if( p_block->i_buffer )
  165.     {
  166.         memcpy( &p_sys->p_buffer[p_sys->i_buffer],
  167.                 p_block->p_buffer, p_block->i_buffer );
  168.         p_sys->i_buffer += p_block->i_buffer;
  169.         p_block->i_buffer = 0;
  170.     }
  171.     if( p_dec->fmt_out.audio.i_rate == 0 && p_dec->fmt_in.i_extra > 0 )
  172.     {
  173.         /* We have a decoder config so init the handle */
  174.         unsigned long i_rate;
  175.         unsigned char i_channels;
  176.         if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
  177.                           p_dec->fmt_in.i_extra,
  178.                           &i_rate, &i_channels ) >= 0 )
  179.         {
  180.             p_dec->fmt_out.audio.i_rate = i_rate;
  181.             p_dec->fmt_out.audio.i_channels = i_channels;
  182.             aout_DateInit( &p_sys->date, i_rate );
  183.         }
  184.     }
  185.     if( p_dec->fmt_out.audio.i_rate == 0 && p_sys->i_buffer )
  186.     {
  187.         unsigned long i_rate;
  188.         unsigned char i_channels;
  189.         /* Init faad with the first frame */
  190.         if( faacDecInit( p_sys->hfaad,
  191.                          p_sys->p_buffer, p_sys->i_buffer,
  192.                          &i_rate, &i_channels ) < 0 )
  193.         {
  194.             block_Release( p_block );
  195.             return NULL;
  196.         }
  197.         p_dec->fmt_out.audio.i_rate = i_rate;
  198.         p_dec->fmt_out.audio.i_channels = i_channels;
  199.         aout_DateInit( &p_sys->date, i_rate );
  200.     }
  201.     if( p_block->i_pts != 0 && p_block->i_pts != aout_DateGet( &p_sys->date ) )
  202.     {
  203.         aout_DateSet( &p_sys->date, p_block->i_pts );
  204.     }
  205.     else if( !aout_DateGet( &p_sys->date ) )
  206.     {
  207.         /* We've just started the stream, wait for the first PTS. */
  208.         block_Release( p_block );
  209.         p_sys->i_buffer = 0;
  210.         return NULL;
  211.     }
  212.     /* Decode all data */
  213.     if( p_sys->i_buffer )
  214.     {
  215.         void *samples;
  216.         faacDecFrameInfo frame;
  217.         aout_buffer_t *p_out;
  218.         int i, j;
  219.         samples = faacDecDecode( p_sys->hfaad, &frame,
  220.                                  p_sys->p_buffer, p_sys->i_buffer );
  221.         if( frame.error > 0 )
  222.         {
  223.             msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
  224.             /* Flush the buffer */
  225.             p_sys->i_buffer = 0;
  226.             block_Release( p_block );
  227.             return NULL;
  228.         }
  229.         if( frame.channels <= 0 || frame.channels > 8 || frame.channels == 7 )
  230.         {
  231.             msg_Warn( p_dec, "invalid channels count: %i", frame.channels );
  232.             /* Flush the buffer */
  233.             p_sys->i_buffer -= frame.bytesconsumed;
  234.             if( p_sys->i_buffer > 0 )
  235.             {
  236.                 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
  237.                          p_sys->i_buffer );
  238.             }
  239.             block_Release( p_block );
  240.             return NULL;
  241.         }
  242.         if( frame.samples <= 0 )
  243.         {
  244.             msg_Warn( p_dec, "decoded zero sample" );
  245.             /* Flush the buffer */
  246.             p_sys->i_buffer -= frame.bytesconsumed;
  247.             if( p_sys->i_buffer > 0 )
  248.             {
  249.                 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
  250.                          p_sys->i_buffer );
  251.             }
  252.             block_Release( p_block );
  253.             return NULL;
  254.         }
  255.         /* We decoded a valid frame */
  256.         if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
  257.         {
  258.             aout_DateInit( &p_sys->date, frame.samplerate );
  259.             aout_DateSet( &p_sys->date, p_block->i_pts );
  260.         }
  261.         p_block->i_pts = 0;  /* PTS is valid only once */
  262.         p_dec->fmt_out.audio.i_rate = frame.samplerate;
  263.         p_dec->fmt_out.audio.i_channels = frame.channels;
  264.         /* Convert frame.channel_position to our own channel values */
  265.         for( i = 0; i < frame.channels; i++ )
  266.         {
  267.             /* Find the channel code */
  268.             for( j = 0; j < MAX_CHANNEL_POSITIONS; j++ )
  269.             {
  270.                 if( frame.channel_position[i] == pi_channels_in[j] )
  271.                 {
  272.                     p_sys->pi_channel_positions[i] = pi_channels_out[j];
  273.                     p_dec->fmt_out.audio.i_physical_channels |=
  274.                         pi_channels_out[j];
  275.                     break;
  276.                 }
  277.             }
  278.             if( j == MAX_CHANNEL_POSITIONS )
  279.             {
  280.                 msg_Warn( p_dec, "unknown channel ordering" );
  281.                 block_Release( p_block );
  282.                 return NULL;
  283.             }
  284.         }
  285.         p_dec->fmt_out.audio.i_original_channels =
  286.             p_dec->fmt_out.audio.i_physical_channels;
  287.         p_out = p_dec->pf_aout_buffer_new(p_dec, frame.samples/frame.channels);
  288.         if( p_out == NULL )
  289.         {
  290.             p_sys->i_buffer = 0;
  291.             block_Release( p_block );
  292.             return NULL;
  293.         }
  294.         p_out->start_date = aout_DateGet( &p_sys->date );
  295.         p_out->end_date = aout_DateIncrement( &p_sys->date,
  296.                                               frame.samples / frame.channels );
  297.         DoReordering( p_dec, (uint32_t *)p_out->p_buffer, samples,
  298.                       frame.samples / frame.channels, frame.channels,
  299.                       p_sys->pi_channel_positions );
  300.         p_sys->i_buffer -= frame.bytesconsumed;
  301.         if( p_sys->i_buffer > 0 )
  302.         {
  303.             memmove( p_sys->p_buffer, &p_sys->p_buffer[frame.bytesconsumed],
  304.                      p_sys->i_buffer );
  305.         }
  306.         return p_out;
  307.     }
  308.     block_Release( p_block );
  309.     return NULL;
  310. }
  311. /*****************************************************************************
  312.  * Close:
  313.  *****************************************************************************/
  314. static void Close( vlc_object_t *p_this )
  315. {
  316.     decoder_t *p_dec = (decoder_t *)p_this;
  317.     decoder_sys_t *p_sys = p_dec->p_sys;
  318.     faacDecClose( p_sys->hfaad );
  319.     free( p_sys );
  320. }
  321. /*****************************************************************************
  322.  * DoReordering: do some channel re-ordering (the ac3 channel order is
  323.  *   different from the aac one).
  324.  *****************************************************************************/
  325. static void DoReordering( decoder_t *p_dec,
  326.                           uint32_t *p_out, uint32_t *p_in, int i_samples,
  327.                           int i_nb_channels, uint32_t *pi_chan_positions )
  328. {
  329.     int pi_chan_table[MAX_CHANNEL_POSITIONS];
  330.     int i, j, k;
  331.     /* Find the channels mapping */
  332.     for( i = 0, j = 0; i < MAX_CHANNEL_POSITIONS; i++ )
  333.     {
  334.         for( k = 0; k < i_nb_channels; k++ )
  335.         {
  336.             if( pi_channels_ordered[i] == pi_chan_positions[k] )
  337.             {
  338.                 pi_chan_table[k] = j++;
  339.                 break;
  340.             }
  341.         }
  342.     }
  343.     /* Do the actual reordering */
  344.     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_FPU )
  345.         for( i = 0; i < i_samples; i++ )
  346.             for( j = 0; j < i_nb_channels; j++ )
  347.                 p_out[i * i_nb_channels + pi_chan_table[j]] =
  348.                     p_in[i * i_nb_channels + j];
  349.     else
  350.         for( i = 0; i < i_samples; i++ )
  351.             for( j = 0; j < i_nb_channels; j++ )
  352.                 ((uint16_t *)p_out)[i * i_nb_channels + pi_chan_table[j]] =
  353.                     ((uint16_t *)p_in)[i * i_nb_channels + j];
  354. }