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

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * decoder.c: AAC decoder using libfaad2
  3.  *****************************************************************************
  4.  * Copyright (C) 2001, 2003 the VideoLAN team
  5.  * $Id: ae446753da0db1a4346fa5aca8af277aff98edde $
  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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23.  *****************************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. # include "config.h"
  26. #endif
  27. #include <vlc_common.h>
  28. #include <vlc_plugin.h>
  29. #include <vlc_input.h>
  30. #include <vlc_aout.h>
  31. #include <vlc_codec.h>
  32. #include <faad.h>
  33. /*****************************************************************************
  34.  * Module descriptor
  35.  *****************************************************************************/
  36. static int  Open( vlc_object_t * );
  37. static void Close( vlc_object_t * );
  38. vlc_module_begin ()
  39.     set_description( N_("AAC audio decoder (using libfaad2)") )
  40.     set_capability( "decoder", 100 )
  41.     set_category( CAT_INPUT )
  42.     set_subcategory( SUBCAT_INPUT_ACODEC )
  43.     set_callbacks( Open, Close )
  44. vlc_module_end ()
  45. /****************************************************************************
  46.  * Local prototypes
  47.  ****************************************************************************/
  48. static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
  49. static void DoReordering( uint32_t *, uint32_t *, int, int, uint32_t * );
  50. #define MAX_CHANNEL_POSITIONS 9
  51. struct decoder_sys_t
  52. {
  53.     /* faad handler */
  54.     faacDecHandle *hfaad;
  55.     /* samples */
  56.     audio_date_t date;
  57.     /* temporary buffer */
  58.     uint8_t *p_buffer;
  59.     int     i_buffer;
  60.     size_t  i_buffer_size;
  61.     /* Channel positions of the current stream (for re-ordering) */
  62.     uint32_t pi_channel_positions[MAX_CHANNEL_POSITIONS];
  63.     bool b_sbr, b_ps;
  64. };
  65. static const uint32_t pi_channels_in[MAX_CHANNEL_POSITIONS] =
  66.     { FRONT_CHANNEL_CENTER, FRONT_CHANNEL_LEFT, FRONT_CHANNEL_RIGHT,
  67.       SIDE_CHANNEL_LEFT, SIDE_CHANNEL_RIGHT,
  68.       BACK_CHANNEL_LEFT, BACK_CHANNEL_RIGHT,
  69.       BACK_CHANNEL_CENTER, LFE_CHANNEL };
  70. static const uint32_t pi_channels_out[MAX_CHANNEL_POSITIONS] =
  71.     { AOUT_CHAN_CENTER, AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
  72.       AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
  73.       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
  74.       AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE };
  75. static const uint32_t pi_channels_ordered[MAX_CHANNEL_POSITIONS] =
  76.     { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
  77.       AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
  78.       AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
  79.       AOUT_CHAN_CENTER, AOUT_CHAN_REARCENTER, AOUT_CHAN_LFE
  80.     };
  81. static const uint32_t pi_channels_guessed[MAX_CHANNEL_POSITIONS] =
  82.     { 0, AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
  83.       AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_LFE,
  84.       AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
  85.           | AOUT_CHAN_REARRIGHT,
  86.       AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
  87.           | AOUT_CHAN_REARRIGHT | AOUT_CHAN_CENTER,
  88.       AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
  89.           | AOUT_CHAN_REARRIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_LFE,
  90.       AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_MIDDLELEFT
  91.           | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
  92.           | AOUT_CHAN_CENTER,
  93.       AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_MIDDLELEFT
  94.           | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
  95.           | AOUT_CHAN_CENTER | AOUT_CHAN_LFE
  96.     };
  97. /*****************************************************************************
  98.  * OpenDecoder: probe the decoder and return score
  99.  *****************************************************************************/
  100. static int Open( vlc_object_t *p_this )
  101. {
  102.     decoder_t *p_dec = (decoder_t*)p_this;
  103.     decoder_sys_t *p_sys = p_dec->p_sys;
  104.     faacDecConfiguration *cfg;
  105.     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','4','a') )
  106.     {
  107.         return VLC_EGENERIC;
  108.     }
  109.     /* Allocate the memory needed to store the decoder's structure */
  110.     if( ( p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) ) ) == NULL )
  111.         return VLC_ENOMEM;
  112.     /* Open a faad context */
  113.     if( ( p_sys->hfaad = faacDecOpen() ) == NULL )
  114.     {
  115.         msg_Err( p_dec, "cannot initialize faad" );
  116.         return VLC_EGENERIC;
  117.     }
  118.     /* Misc init */
  119.     aout_DateSet( &p_sys->date, 0 );
  120.     p_dec->fmt_out.i_cat = AUDIO_ES;
  121.     if (vlc_CPU() & CPU_CAPABILITY_FPU)
  122.         p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
  123.     else
  124.         p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
  125.     p_dec->pf_decode_audio = DecodeBlock;
  126.     p_dec->fmt_out.audio.i_physical_channels =
  127.         p_dec->fmt_out.audio.i_original_channels = 0;
  128.     if( p_dec->fmt_in.i_extra > 0 )
  129.     {
  130.         /* We have a decoder config so init the handle */
  131.         unsigned long i_rate;
  132.         unsigned char i_channels;
  133.         if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
  134.                           p_dec->fmt_in.i_extra,
  135.                           &i_rate, &i_channels ) < 0 )
  136.         {
  137.             msg_Err( p_dec, "Failed to initialize faad using extra data" );
  138.             return VLC_EGENERIC;
  139.         }
  140.         p_dec->fmt_out.audio.i_rate = i_rate;
  141.         p_dec->fmt_out.audio.i_channels = i_channels;
  142.         p_dec->fmt_out.audio.i_physical_channels
  143.             = p_dec->fmt_out.audio.i_original_channels
  144.             = pi_channels_guessed[i_channels];
  145.         aout_DateInit( &p_sys->date, i_rate );
  146.     }
  147.     else
  148.     {
  149.         /* Will be initalised from first frame */
  150.         p_dec->fmt_out.audio.i_rate = 0;
  151.         p_dec->fmt_out.audio.i_channels = 0;
  152.     }
  153.     /* Set the faad config */
  154.     cfg = faacDecGetCurrentConfiguration( p_sys->hfaad );
  155.     if (vlc_CPU() & CPU_CAPABILITY_FPU)
  156.         cfg->outputFormat = FAAD_FMT_FLOAT;
  157.     else
  158.         cfg->outputFormat = FAAD_FMT_16BIT;
  159.     faacDecSetConfiguration( p_sys->hfaad, cfg );
  160.     /* buffer */
  161.     p_sys->i_buffer = p_sys->i_buffer_size = 0;
  162.     p_sys->p_buffer = NULL;
  163.     /* Faad2 can't deal with truncated data (eg. from MPEG TS) */
  164.     p_dec->b_need_packetized = true;
  165.     p_sys->b_sbr = p_sys->b_ps = false;
  166.     return VLC_SUCCESS;
  167. }
  168. /*****************************************************************************
  169.  * DecodeBlock:
  170.  *****************************************************************************/
  171. static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
  172. {
  173.     decoder_sys_t *p_sys = p_dec->p_sys;
  174.     block_t *p_block;
  175.     if( !pp_block || !*pp_block ) return NULL;
  176.     p_block = *pp_block;
  177.     if( p_block->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
  178.     {
  179.         block_Release( p_block );
  180.         return NULL;
  181.     }
  182.     /* Remove ADTS header if we have decoder specific config */
  183.     if( p_dec->fmt_in.i_extra && p_block->i_buffer > 7 )
  184.     {
  185.         if( p_block->p_buffer[0] == 0xff &&
  186.             ( p_block->p_buffer[1] & 0xf0 ) == 0xf0 ) /* syncword */
  187.         {   /* ADTS header present */
  188.             size_t i_header_size; /* 7 bytes (+ 2 bytes for crc) */
  189.             i_header_size = 7 + ( ( p_block->p_buffer[1] & 0x01 ) ? 0 : 2 );
  190.             /* FIXME: multiple blocks per frame */
  191.             if( p_block->i_buffer > i_header_size )
  192.             {
  193.                 vlc_memcpy( p_block->p_buffer,
  194.                             p_block->p_buffer + i_header_size,
  195.                             p_block->i_buffer - i_header_size );
  196.                 p_block->i_buffer -= i_header_size;
  197.             }
  198.         }
  199.     }
  200.     /* Append the block to the temporary buffer */
  201.     if( p_sys->i_buffer_size < p_sys->i_buffer + p_block->i_buffer )
  202.     {
  203.         size_t  i_buffer_size = p_sys->i_buffer + p_block->i_buffer;
  204.         uint8_t *p_buffer     = realloc( p_sys->p_buffer, i_buffer_size );
  205.         if( p_buffer )
  206.         {
  207.             p_sys->i_buffer_size = i_buffer_size;
  208.             p_sys->p_buffer      = p_buffer;
  209.         }
  210.         else
  211.         {
  212.             p_block->i_buffer = 0;
  213.         }
  214.     }
  215.     if( p_block->i_buffer > 0 )
  216.     {
  217.         vlc_memcpy( &p_sys->p_buffer[p_sys->i_buffer],
  218.                      p_block->p_buffer, p_block->i_buffer );
  219.         p_sys->i_buffer += p_block->i_buffer;
  220.         p_block->i_buffer = 0;
  221.     }
  222.     if( p_dec->fmt_out.audio.i_rate == 0 && p_dec->fmt_in.i_extra > 0 )
  223.     {
  224.         /* We have a decoder config so init the handle */
  225.         unsigned long i_rate;
  226.         unsigned char i_channels;
  227.         if( faacDecInit2( p_sys->hfaad, p_dec->fmt_in.p_extra,
  228.                           p_dec->fmt_in.i_extra,
  229.                           &i_rate, &i_channels ) >= 0 )
  230.         {
  231.             p_dec->fmt_out.audio.i_rate = i_rate;
  232.             p_dec->fmt_out.audio.i_channels = i_channels;
  233.             p_dec->fmt_out.audio.i_physical_channels
  234.                 = p_dec->fmt_out.audio.i_original_channels
  235.                 = pi_channels_guessed[i_channels];
  236.             aout_DateInit( &p_sys->date, i_rate );
  237.         }
  238.     }
  239.     if( p_dec->fmt_out.audio.i_rate == 0 && p_sys->i_buffer )
  240.     {
  241.         unsigned long i_rate;
  242.         unsigned char i_channels;
  243.         /* Init faad with the first frame */
  244.         if( faacDecInit( p_sys->hfaad,
  245.                          p_sys->p_buffer, p_sys->i_buffer,
  246.                          &i_rate, &i_channels ) < 0 )
  247.         {
  248.             block_Release( p_block );
  249.             return NULL;
  250.         }
  251.         p_dec->fmt_out.audio.i_rate = i_rate;
  252.         p_dec->fmt_out.audio.i_channels = i_channels;
  253.         p_dec->fmt_out.audio.i_physical_channels
  254.             = p_dec->fmt_out.audio.i_original_channels
  255.             = pi_channels_guessed[i_channels];
  256.         aout_DateInit( &p_sys->date, i_rate );
  257.     }
  258.     if( p_block->i_pts != 0 && p_block->i_pts != aout_DateGet( &p_sys->date ) )
  259.     {
  260.         aout_DateSet( &p_sys->date, p_block->i_pts );
  261.     }
  262.     else if( !aout_DateGet( &p_sys->date ) )
  263.     {
  264.         /* We've just started the stream, wait for the first PTS. */
  265.         block_Release( p_block );
  266.         p_sys->i_buffer = 0;
  267.         return NULL;
  268.     }
  269.     /* Decode all data */
  270.     if( p_sys->i_buffer )
  271.     {
  272.         void *samples;
  273.         faacDecFrameInfo frame;
  274.         aout_buffer_t *p_out;
  275.         int i, j;
  276.         samples = faacDecDecode( p_sys->hfaad, &frame,
  277.                                  p_sys->p_buffer, p_sys->i_buffer );
  278.         if( frame.error > 0 )
  279.         {
  280.             msg_Warn( p_dec, "%s", faacDecGetErrorMessage( frame.error ) );
  281.             /* Flush the buffer */
  282.             p_sys->i_buffer = 0;
  283.             block_Release( p_block );
  284.             return NULL;
  285.         }
  286.         if( frame.channels <= 0 || frame.channels > 8 || frame.channels == 7 )
  287.         {
  288.             msg_Warn( p_dec, "invalid channels count: %i", frame.channels );
  289.             /* Flush the buffer */
  290.             p_sys->i_buffer -= frame.bytesconsumed;
  291.             if( p_sys->i_buffer > 0 )
  292.             {
  293.                 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
  294.                          p_sys->i_buffer );
  295.             }
  296.             block_Release( p_block );
  297.             return NULL;
  298.         }
  299.         if( frame.samples <= 0 )
  300.         {
  301.             msg_Warn( p_dec, "decoded zero sample" );
  302.             /* Flush the buffer */
  303.             p_sys->i_buffer -= frame.bytesconsumed;
  304.             if( p_sys->i_buffer > 0 )
  305.             {
  306.                 memmove( p_sys->p_buffer,&p_sys->p_buffer[frame.bytesconsumed],
  307.                          p_sys->i_buffer );
  308.             }
  309.             block_Release( p_block );
  310.             return NULL;
  311.         }
  312.         /* We decoded a valid frame */
  313.         if( p_dec->fmt_out.audio.i_rate != frame.samplerate )
  314.         {
  315.             aout_DateInit( &p_sys->date, frame.samplerate );
  316.             aout_DateSet( &p_sys->date, p_block->i_pts );
  317.         }
  318.         p_block->i_pts = 0;  /* PTS is valid only once */
  319.         p_dec->fmt_out.audio.i_rate = frame.samplerate;
  320.         p_dec->fmt_out.audio.i_channels = frame.channels;
  321.         p_dec->fmt_out.audio.i_physical_channels
  322.             = p_dec->fmt_out.audio.i_original_channels
  323.             = pi_channels_guessed[frame.channels];
  324.         /* Adjust stream info when dealing with SBR/PS */
  325.         if( p_sys->b_sbr != frame.sbr || p_sys->b_ps != frame.ps )
  326.         {
  327.             const char *psz_ext = (frame.sbr && frame.ps) ? "SBR+PS" :
  328.                                     frame.sbr ? "SBR" : "PS";
  329.             msg_Dbg( p_dec, "AAC %s (channels: %u, samplerate: %lu)",
  330.                     psz_ext, frame.channels, frame.samplerate );
  331.             if( !p_dec->p_description )
  332.                 p_dec->p_description = vlc_meta_New();
  333.             if( p_dec->p_description )
  334.                 vlc_meta_AddExtra( p_dec->p_description, _("AAC extension"), psz_ext );
  335.             p_sys->b_sbr = frame.sbr; p_sys->b_ps = frame.ps;
  336.         }
  337.         /* Convert frame.channel_position to our own channel values */
  338.         p_dec->fmt_out.audio.i_physical_channels = 0;
  339.         for( i = 0; i < frame.channels; i++ )
  340.         {
  341.             /* Find the channel code */
  342.             for( j = 0; j < MAX_CHANNEL_POSITIONS; j++ )
  343.             {
  344.                 if( frame.channel_position[i] == pi_channels_in[j] )
  345.                     break;
  346.             }
  347.             if( j >= MAX_CHANNEL_POSITIONS )
  348.             {
  349.                 msg_Warn( p_dec, "unknown channel ordering" );
  350.                 /* Invent something */
  351.                 j = i;
  352.             }
  353.             /* */
  354.             p_sys->pi_channel_positions[i] = pi_channels_out[j];
  355.             if( p_dec->fmt_out.audio.i_physical_channels & pi_channels_out[j] )
  356.                 frame.channels--; /* We loose a duplicated channel */
  357.             else
  358.                 p_dec->fmt_out.audio.i_physical_channels |= pi_channels_out[j];
  359.         }
  360.         p_dec->fmt_out.audio.i_original_channels =
  361.             p_dec->fmt_out.audio.i_physical_channels;
  362.         p_out = decoder_NewAudioBuffer(p_dec, frame.samples/frame.channels);
  363.         if( p_out == NULL )
  364.         {
  365.             p_sys->i_buffer = 0;
  366.             block_Release( p_block );
  367.             return NULL;
  368.         }
  369.         p_out->start_date = aout_DateGet( &p_sys->date );
  370.         p_out->end_date = aout_DateIncrement( &p_sys->date, frame.samples / frame.channels );
  371.         DoReordering( (uint32_t *)p_out->p_buffer, samples,
  372.                       frame.samples / frame.channels, frame.channels,
  373.                       p_sys->pi_channel_positions );
  374.         p_sys->i_buffer -= frame.bytesconsumed;
  375.         if( p_sys->i_buffer > 0 )
  376.         {
  377.             memmove( p_sys->p_buffer, &p_sys->p_buffer[frame.bytesconsumed],
  378.                      p_sys->i_buffer );
  379.         }
  380.         return p_out;
  381.     }
  382.     block_Release( p_block );
  383.     return NULL;
  384. }
  385. /*****************************************************************************
  386.  * Close:
  387.  *****************************************************************************/
  388. static void Close( vlc_object_t *p_this )
  389. {
  390.     decoder_t *p_dec = (decoder_t *)p_this;
  391.     decoder_sys_t *p_sys = p_dec->p_sys;
  392.     faacDecClose( p_sys->hfaad );
  393.     free( p_sys->p_buffer );
  394.     free( p_sys );
  395. }
  396. /*****************************************************************************
  397.  * DoReordering: do some channel re-ordering (the ac3 channel order is
  398.  *   different from the aac one).
  399.  *****************************************************************************/
  400. static void DoReordering( uint32_t *p_out, uint32_t *p_in, int i_samples,
  401.                           int i_nb_channels, uint32_t *pi_chan_positions )
  402. {
  403.     int pi_chan_table[MAX_CHANNEL_POSITIONS];
  404.     int i, j, k;
  405.     /* Find the channels mapping */
  406.     for( i = 0, j = 0; i < MAX_CHANNEL_POSITIONS; i++ )
  407.     {
  408.         for( k = 0; k < i_nb_channels; k++ )
  409.         {
  410.             if( pi_channels_ordered[i] == pi_chan_positions[k] )
  411.             {
  412.                 pi_chan_table[k] = j++;
  413.                 break;
  414.             }
  415.         }
  416.     }
  417.     /* Do the actual reordering */
  418.     if( vlc_CPU() & CPU_CAPABILITY_FPU )
  419.         for( i = 0; i < i_samples; i++ )
  420.             for( j = 0; j < i_nb_channels; j++ )
  421.                 p_out[i * i_nb_channels + pi_chan_table[j]] =
  422.                     p_in[i * i_nb_channels + j];
  423.     else
  424.         for( i = 0; i < i_samples; i++ )
  425.             for( j = 0; j < i_nb_channels; j++ )
  426.                 ((uint16_t *)p_out)[i * i_nb_channels + pi_chan_table[j]] =
  427.                     ((uint16_t *)p_in)[i * i_nb_channels + j];
  428. }