output.c
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:18k
源码类别:

多媒体编程

开发平台:

Visual C++

  1. /*
  2. ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
  3. ** Copyright (C) 2003-2005 M. Bakker, Ahead Software AG, http://www.nero.com
  4. **  
  5. ** This program is free software; you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation; either version 2 of the License, or
  8. ** (at your option) any later version.
  9. ** 
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ** GNU General Public License for more details.
  14. ** 
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program; if not, write to the Free Software 
  17. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. **
  19. ** Any non-GPL usage of this software or parts of this software is strictly
  20. ** forbidden.
  21. **
  22. ** Software using this code must display the following message visibly in the
  23. ** software:
  24. ** "FAAD2 AAC/HE-AAC/HE-AACv2/DRM decoder (c) Ahead Software, www.nero.com"
  25. ** in, for example, the about-box or help/startup screen.
  26. **
  27. ** Commercial non-GPL licensing of this software is possible.
  28. ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
  29. **
  30. ** $Id: output.c,v 1.3 2005/11/01 21:41:43 gabest Exp $
  31. **/
  32. #include "common.h"
  33. #include "structs.h"
  34. #include "output.h"
  35. #include "decoder.h"
  36. #ifndef FIXED_POINT
  37. #define FLOAT_SCALE (1.0f/(1<<15))
  38. #define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
  39. #define RSQRT2 REAL_CONST(0.7071067811865475244) // 1/sqrt(2)
  40. static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
  41.                                 uint8_t down_matrix, uint8_t *internal_channel)
  42. {
  43.     if (!down_matrix)
  44.         return input[internal_channel[channel]][sample];
  45.     if (channel == 0)
  46.     {
  47.         return DM_MUL * (input[internal_channel[1]][sample] +
  48.             input[internal_channel[0]][sample] * RSQRT2 +
  49.             input[internal_channel[3]][sample] * RSQRT2);
  50.     } else {
  51.         return DM_MUL * (input[internal_channel[2]][sample] +
  52.             input[internal_channel[0]][sample] * RSQRT2 +
  53.             input[internal_channel[4]][sample] * RSQRT2);
  54.     }
  55. }
  56. #ifndef HAS_LRINTF
  57. #define CLIP(sample, max, min) 
  58. if (sample >= 0.0f)            
  59. {                              
  60.     sample += 0.5f;            
  61.     if (sample >= max)         
  62.         sample = max;          
  63. } else {                       
  64.     sample += -0.5f;           
  65.     if (sample <= min)         
  66.         sample = min;          
  67. }
  68. #else
  69. #define CLIP(sample, max, min) 
  70. if (sample >= 0.0f)            
  71. {                              
  72.     if (sample >= max)         
  73.         sample = max;          
  74. } else {                       
  75.     if (sample <= min)         
  76.         sample = min;          
  77. }
  78. #endif
  79. #define CONV(a,b) ((a<<1)|(b&0x1))
  80. static void to_PCM_16bit(NeAACDecHandle hDecoder, real_t **input,
  81.                          uint8_t channels, uint16_t frame_len,
  82.                          int16_t **sample_buffer)
  83. {
  84.     uint8_t ch, ch1;
  85.     uint16_t i;
  86.     switch (CONV(channels,hDecoder->downMatrix))
  87.     {
  88.     case CONV(1,0):
  89.     case CONV(1,1):
  90.         for(i = 0; i < frame_len; i++)
  91.         {
  92.             real_t inp = input[hDecoder->internal_channel[0]][i];
  93.             CLIP(inp, 32767.0f, -32768.0f);
  94.             (*sample_buffer)[i] = (int16_t)lrintf(inp);
  95.         }
  96.         break;
  97.     case CONV(2,0):
  98.         if (hDecoder->upMatrix)
  99.         {
  100.             ch  = hDecoder->internal_channel[0];
  101.             for(i = 0; i < frame_len; i++)
  102.             {
  103.                 real_t inp0 = input[ch][i];
  104.                 CLIP(inp0, 32767.0f, -32768.0f);
  105.                 (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
  106.                 (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
  107.             }
  108.         } else {
  109.             ch  = hDecoder->internal_channel[0];
  110.             ch1 = hDecoder->internal_channel[1];
  111.             for(i = 0; i < frame_len; i++)
  112.             {
  113.                 real_t inp0 = input[ch ][i];
  114.                 real_t inp1 = input[ch1][i];
  115.                 CLIP(inp0, 32767.0f, -32768.0f);
  116.                 CLIP(inp1, 32767.0f, -32768.0f);
  117.                 (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
  118.                 (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
  119.             }
  120.         }
  121.         break;
  122.     default:
  123.         for (ch = 0; ch < channels; ch++)
  124.         {
  125.             for(i = 0; i < frame_len; i++)
  126.             {
  127.                 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
  128.                 CLIP(inp, 32767.0f, -32768.0f);
  129.                 (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
  130.             }
  131.         }
  132.         break;
  133.     }
  134. }
  135. static void to_PCM_24bit(NeAACDecHandle hDecoder, real_t **input,
  136.                          uint8_t channels, uint16_t frame_len,
  137.                          int32_t **sample_buffer)
  138. {
  139.     uint8_t ch, ch1;
  140.     uint16_t i;
  141.     switch (CONV(channels,hDecoder->downMatrix))
  142.     {
  143.     case CONV(1,0):
  144.     case CONV(1,1):
  145.         for(i = 0; i < frame_len; i++)
  146.         {
  147.             real_t inp = input[hDecoder->internal_channel[0]][i];
  148.             inp *= 256.0f;
  149.             CLIP(inp, 8388607.0f, -8388608.0f);
  150.             (*sample_buffer)[i] = (int32_t)lrintf(inp);
  151.         }
  152.         break;
  153.     case CONV(2,0):
  154.         if (hDecoder->upMatrix)
  155.         {
  156.             ch = hDecoder->internal_channel[0];
  157.             for(i = 0; i < frame_len; i++)
  158.             {
  159.                 real_t inp0 = input[ch][i];
  160.                 inp0 *= 256.0f;
  161.                 CLIP(inp0, 8388607.0f, -8388608.0f);
  162.                 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
  163.                 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
  164.             }
  165.         } else {
  166.             ch  = hDecoder->internal_channel[0];
  167.             ch1 = hDecoder->internal_channel[1];
  168.             for(i = 0; i < frame_len; i++)
  169.             {
  170.                 real_t inp0 = input[ch ][i];
  171.                 real_t inp1 = input[ch1][i];
  172.                 inp0 *= 256.0f;
  173.                 inp1 *= 256.0f;
  174.                 CLIP(inp0, 8388607.0f, -8388608.0f);
  175.                 CLIP(inp1, 8388607.0f, -8388608.0f);
  176.                 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
  177.                 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
  178.             }
  179.         }
  180.         break;
  181.     default:
  182.         for (ch = 0; ch < channels; ch++)
  183.         {
  184.             for(i = 0; i < frame_len; i++)
  185.             {
  186.                 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
  187.                 inp *= 256.0f;
  188.                 CLIP(inp, 8388607.0f, -8388608.0f);
  189.                 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
  190.             }
  191.         }
  192.         break;
  193.     }
  194. }
  195. static void to_PCM_32bit(NeAACDecHandle hDecoder, real_t **input,
  196.                          uint8_t channels, uint16_t frame_len,
  197.                          int32_t **sample_buffer)
  198. {
  199.     uint8_t ch, ch1;
  200.     uint16_t i;
  201.     switch (CONV(channels,hDecoder->downMatrix))
  202.     {
  203.     case CONV(1,0):
  204.     case CONV(1,1):
  205.         for(i = 0; i < frame_len; i++)
  206.         {
  207.             real_t inp = input[hDecoder->internal_channel[0]][i];
  208.             inp *= 65536.0f;
  209.             CLIP(inp, 2147483647.0f, -2147483648.0f);
  210.             (*sample_buffer)[i] = (int32_t)lrintf(inp);
  211.         }
  212.         break;
  213.     case CONV(2,0):
  214.         if (hDecoder->upMatrix)
  215.         {
  216.             ch = hDecoder->internal_channel[0];
  217.             for(i = 0; i < frame_len; i++)
  218.             {
  219.                 real_t inp0 = input[ch][i];
  220.                 inp0 *= 65536.0f;
  221.                 CLIP(inp0, 2147483647.0f, -2147483648.0f);
  222.                 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
  223.                 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
  224.             }
  225.         } else {
  226.             ch  = hDecoder->internal_channel[0];
  227.             ch1 = hDecoder->internal_channel[1];
  228.             for(i = 0; i < frame_len; i++)
  229.             {
  230.                 real_t inp0 = input[ch ][i];
  231.                 real_t inp1 = input[ch1][i];
  232.                 inp0 *= 65536.0f;
  233.                 inp1 *= 65536.0f;
  234.                 CLIP(inp0, 2147483647.0f, -2147483648.0f);
  235.                 CLIP(inp1, 2147483647.0f, -2147483648.0f);
  236.                 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
  237.                 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
  238.             }
  239.         }
  240.         break;
  241.     default:
  242.         for (ch = 0; ch < channels; ch++)
  243.         {
  244.             for(i = 0; i < frame_len; i++)
  245.             {
  246.                 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
  247.                 inp *= 65536.0f;
  248.                 CLIP(inp, 2147483647.0f, -2147483648.0f);
  249.                 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
  250.             }
  251.         }
  252.         break;
  253.     }
  254. }
  255. static void to_PCM_float(NeAACDecHandle hDecoder, real_t **input,
  256.                          uint8_t channels, uint16_t frame_len,
  257.                          float32_t **sample_buffer)
  258. {
  259.     uint8_t ch, ch1;
  260.     uint16_t i;
  261.     switch (CONV(channels,hDecoder->downMatrix))
  262.     {
  263.     case CONV(1,0):
  264.     case CONV(1,1):
  265.         for(i = 0; i < frame_len; i++)
  266.         {
  267.             real_t inp = input[hDecoder->internal_channel[0]][i];
  268.             (*sample_buffer)[i] = inp*FLOAT_SCALE;
  269.         }
  270.         break;
  271.     case CONV(2,0):
  272.         if (hDecoder->upMatrix)
  273.         {
  274.             ch = hDecoder->internal_channel[0];
  275.             for(i = 0; i < frame_len; i++)
  276.             {
  277.                 real_t inp0 = input[ch][i];
  278.                 (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
  279.                 (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
  280.             }
  281.         } else {
  282.             ch  = hDecoder->internal_channel[0];
  283.             ch1 = hDecoder->internal_channel[1];
  284.             for(i = 0; i < frame_len; i++)
  285.             {
  286.                 real_t inp0 = input[ch ][i];
  287.                 real_t inp1 = input[ch1][i];
  288.                 (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
  289.                 (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
  290.             }
  291.         }
  292.         break;
  293.     default:
  294.         for (ch = 0; ch < channels; ch++)
  295.         {
  296.             for(i = 0; i < frame_len; i++)
  297.             {
  298.                 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
  299.                 (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
  300.             }
  301.         }
  302.         break;
  303.     }
  304. }
  305. static void to_PCM_double(NeAACDecHandle hDecoder, real_t **input,
  306.                           uint8_t channels, uint16_t frame_len,
  307.                           double **sample_buffer)
  308. {
  309.     uint8_t ch, ch1;
  310.     uint16_t i;
  311.     switch (CONV(channels,hDecoder->downMatrix))
  312.     {
  313.     case CONV(1,0):
  314.     case CONV(1,1):
  315.         for(i = 0; i < frame_len; i++)
  316.         {
  317.             real_t inp = input[hDecoder->internal_channel[0]][i];
  318.             (*sample_buffer)[i] = (double)inp*FLOAT_SCALE;
  319.         }
  320.         break;
  321.     case CONV(2,0):
  322.         if (hDecoder->upMatrix)
  323.         {
  324.             ch = hDecoder->internal_channel[0];
  325.             for(i = 0; i < frame_len; i++)
  326.             {
  327.                 real_t inp0 = input[ch][i];
  328.                 (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
  329.                 (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
  330.             }
  331.         } else {
  332.             ch  = hDecoder->internal_channel[0];
  333.             ch1 = hDecoder->internal_channel[1];
  334.             for(i = 0; i < frame_len; i++)
  335.             {
  336.                 real_t inp0 = input[ch ][i];
  337.                 real_t inp1 = input[ch1][i];
  338.                 (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
  339.                 (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
  340.             }
  341.         }
  342.         break;
  343.     default:
  344.         for (ch = 0; ch < channels; ch++)
  345.         {
  346.             for(i = 0; i < frame_len; i++)
  347.             {
  348.                 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
  349.                 (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
  350.             }
  351.         }
  352.         break;
  353.     }
  354. }
  355. void *output_to_PCM(NeAACDecHandle hDecoder,
  356.                     real_t **input, void *sample_buffer, uint8_t channels,
  357.                     uint16_t frame_len, uint8_t format)
  358. {
  359.     int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
  360.     int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
  361.     float32_t *float_sample_buffer = (float32_t*)sample_buffer;
  362.     double    *double_sample_buffer = (double*)sample_buffer;
  363. #ifdef PROFILE
  364.     int64_t count = faad_get_ts();
  365. #endif
  366.     /* Copy output to a standard PCM buffer */
  367.     switch (format)
  368.     {
  369.     case FAAD_FMT_16BIT:
  370.         to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
  371.         break;
  372.     case FAAD_FMT_24BIT:
  373.         to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
  374.         break;
  375.     case FAAD_FMT_32BIT:
  376.         to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
  377.         break;
  378.     case FAAD_FMT_FLOAT:
  379.         to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
  380.         break;
  381.     case FAAD_FMT_DOUBLE:
  382.         to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
  383.         break;
  384.     }
  385. #ifdef PROFILE
  386.     count = faad_get_ts() - count;
  387.     hDecoder->output_cycles += count;
  388. #endif
  389.     return sample_buffer;
  390. }
  391. #else
  392. #define DM_MUL FRAC_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
  393. #define RSQRT2 FRAC_CONST(0.7071067811865475244) // 1/sqrt(2)
  394. static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
  395.                                 uint8_t down_matrix, uint8_t up_matrix,
  396.                                 uint8_t *internal_channel)
  397. {
  398.     if (up_matrix == 1)
  399.         return input[internal_channel[0]][sample];
  400.     if (!down_matrix)
  401.         return input[internal_channel[channel]][sample];
  402.     if (channel == 0)
  403.     {
  404.         real_t C   = MUL_F(input[internal_channel[0]][sample], RSQRT2);
  405.         real_t L_S = MUL_F(input[internal_channel[3]][sample], RSQRT2);
  406.         real_t cum = input[internal_channel[1]][sample] + C + L_S;
  407.         return MUL_F(cum, DM_MUL);
  408.     } else {
  409.         real_t C   = MUL_F(input[internal_channel[0]][sample], RSQRT2);
  410.         real_t R_S = MUL_F(input[internal_channel[4]][sample], RSQRT2);
  411.         real_t cum = input[internal_channel[2]][sample] + C + R_S;
  412.         return MUL_F(cum, DM_MUL);
  413.     }
  414. }
  415. void* output_to_PCM(NeAACDecHandle hDecoder,
  416.                     real_t **input, void *sample_buffer, uint8_t channels,
  417.                     uint16_t frame_len, uint8_t format)
  418. {
  419.     uint8_t ch;
  420.     uint16_t i;
  421.     int16_t *short_sample_buffer = (int16_t*)sample_buffer;
  422.     int32_t *int_sample_buffer = (int32_t*)sample_buffer;
  423.     /* Copy output to a standard PCM buffer */
  424.     for (ch = 0; ch < channels; ch++)
  425.     {
  426.         switch (format)
  427.         {
  428.         case FAAD_FMT_16BIT:
  429.             for(i = 0; i < frame_len; i++)
  430.             {
  431.                 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
  432.                     hDecoder->internal_channel);
  433.                 if (tmp >= 0)
  434.                 {
  435.                     tmp += (1 << (REAL_BITS-1));
  436.                     if (tmp >= REAL_CONST(32767))
  437.                     {
  438.                         tmp = REAL_CONST(32767);
  439.                     }
  440.                 } else {
  441.                     tmp += -(1 << (REAL_BITS-1));
  442.                     if (tmp <= REAL_CONST(-32768))
  443.                     {
  444.                         tmp = REAL_CONST(-32768);
  445.                     }
  446.                 }
  447.                 tmp >>= REAL_BITS;
  448.                 short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
  449.             }
  450.             break;
  451.         case FAAD_FMT_24BIT:
  452.             for(i = 0; i < frame_len; i++)
  453.             {
  454.                 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
  455.                     hDecoder->internal_channel);
  456.                 if (tmp >= 0)
  457.                 {
  458.                     tmp += (1 << (REAL_BITS-9));
  459.                     tmp >>= (REAL_BITS-8);
  460.                     if (tmp >= 8388607)
  461.                     {
  462.                         tmp = 8388607;
  463.                     }
  464.                 } else {
  465.                     tmp += -(1 << (REAL_BITS-9));
  466.                     tmp >>= (REAL_BITS-8);
  467.                     if (tmp <= -8388608)
  468.                     {
  469.                         tmp = -8388608;
  470.                     }
  471.                 }
  472.                 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
  473.             }
  474.             break;
  475.         case FAAD_FMT_32BIT:
  476.             for(i = 0; i < frame_len; i++)
  477.             {
  478.                 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
  479.                     hDecoder->internal_channel);
  480.                 if (tmp >= 0)
  481.                 {
  482.                     tmp += (1 << (16-REAL_BITS-1));
  483.                     tmp <<= (16-REAL_BITS);
  484.                 } else {
  485.                     tmp += -(1 << (16-REAL_BITS-1));
  486.                     tmp <<= (16-REAL_BITS);
  487.                 }
  488.                 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
  489.             }
  490.             break;
  491.         case FAAD_FMT_FIXED:
  492.             for(i = 0; i < frame_len; i++)
  493.             {
  494.                 real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
  495.                     hDecoder->internal_channel);
  496.                 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
  497.             }
  498.             break;
  499.         }
  500.     }
  501.     return sample_buffer;
  502. }
  503. #endif