- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# output.c

    00001 /*
    00002 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
    00003 ** Copyright (C) 2003-2005 M. Bakker, Ahead Software AG, http://www.nero.com
    00004 **
    00005 ** This program is free software; you can redistribute it and/or modify
    00006 ** it under the terms of the GNU General Public License as published by
    00007 ** the Free Software Foundation; either version 2 of the License, or
    00008 ** (at your option) any later version.
    00009 **
    00010 ** This program is distributed in the hope that it will be useful,
    00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00013 ** GNU General Public License for more details.
    00014 **
    00015 ** You should have received a copy of the GNU General Public License
    00016 ** along with this program; if not, write to the Free Software
    00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00018 **
    00019 ** Any non-GPL usage of this software or parts of this software is strictly
    00020 ** forbidden.
    00021 **
    00022 ** Software using this code must display the following message visibly in the
    00023 ** software:
    00024 ** "FAAD2 AAC/HE-AAC/HE-AACv2/DRM decoder (c) Ahead Software, www.nero.com"
    00025 ** in, for example, the about-box or help/startup screen.
    00026 **
    00027 ** Commercial non-GPL licensing of this software is possible.
    00028 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
    00029 **
    00030 ** $Id: output.c,v 1.3 2005/11/01 21:41:43 gabest Exp $
    00031 **/
    00032
    00033 #include "common.h"
    00034 #include "structs.h"
    00035
    00036 #include "output.h"
    00037 #include "decoder.h"
    00038
    00039 #ifndef FIXED_POINT
    00040
    00041
    00042 #define FLOAT_SCALE (1.0f/(1<<15))
    00043
    00044 #define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
    00045 #define RSQRT2 REAL_CONST(0.7071067811865475244) // 1/sqrt(2)
    00046
    00047
    00048 static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
    00049                                 uint8_t down_matrix, uint8_t *internal_channel)
    00050 {
    00051     if (!down_matrix)
    00052         return input[internal_channel[channel]][sample];
    00053
    00054     if (channel == 0)
    00055     {
    00056         return DM_MUL * (input[internal_channel[1]][sample] +
    00057             input[internal_channel[0]][sample] * RSQRT2 +
    00058             input[internal_channel[3]][sample] * RSQRT2);
    00059     } else {
    00060         return DM_MUL * (input[internal_channel[2]][sample] +
    00061             input[internal_channel[0]][sample] * RSQRT2 +
    00062             input[internal_channel[4]][sample] * RSQRT2);
    00063     }
    00064 }
    00065
    00066 #ifndef HAS_LRINTF
    00067 #define CLIP(sample, max, min) \
    00068 if (sample >= 0.0f)            \
    00069 {                              \
    00070     sample += 0.5f;            \
    00071     if (sample >= max)         \
    00072         sample = max;          \
    00073 } else {                       \
    00074     sample += -0.5f;           \
    00075     if (sample <= min)         \
    00076         sample = min;          \
    00077 }
    00078 #else
    00079 #define CLIP(sample, max, min) \
    00080 if (sample >= 0.0f)            \
    00081 {                              \
    00082     if (sample >= max)         \
    00083         sample = max;          \
    00084 } else {                       \
    00085     if (sample <= min)         \
    00086         sample = min;          \
    00087 }
    00088 #endif
    00089
    00090 #define CONV(a,b) ((a<<1)|(b&0x1))
    00091
    00092 static void to_PCM_16bit(NeAACDecHandle hDecoder, real_t **input,
    00093                          uint8_t channels, uint16_t frame_len,
    00094                          int16_t **sample_buffer)
    00095 {
    00096     uint8_t ch, ch1;
    00097     uint16_t i;
    00098
    00099     switch (CONV(channels,hDecoder->downMatrix))
    00100     {
    00101     case CONV(1,0):
    00102     case CONV(1,1):
    00103         for(i = 0; i < frame_len; i++)
    00104         {
    00105             real_t inp = input[hDecoder->internal_channel[0]][i];
    00106
    00107             CLIP(inp, 32767.0f, -32768.0f);
    00108
    00109             (*sample_buffer)[i] = (int16_t)lrintf(inp);
    00110         }
    00111         break;
    00112     case CONV(2,0):
    00113         if (hDecoder->upMatrix)
    00114         {
    00115             ch  = hDecoder->internal_channel[0];
    00116             for(i = 0; i < frame_len; i++)
    00117             {
    00118                 real_t inp0 = input[ch][i];
    00119
    00120                 CLIP(inp0, 32767.0f, -32768.0f);
    00121
    00122                 (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
    00123                 (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp0);
    00124             }
    00125         } else {
    00126             ch  = hDecoder->internal_channel[0];
    00127             ch1 = hDecoder->internal_channel[1];
    00128             for(i = 0; i < frame_len; i++)
    00129             {
    00130                 real_t inp0 = input[ch ][i];
    00131                 real_t inp1 = input[ch1][i];
    00132
    00133                 CLIP(inp0, 32767.0f, -32768.0f);
    00134                 CLIP(inp1, 32767.0f, -32768.0f);
    00135
    00136                 (*sample_buffer)[(i*2)+0] = (int16_t)lrintf(inp0);
    00137                 (*sample_buffer)[(i*2)+1] = (int16_t)lrintf(inp1);
    00138             }
    00139         }
    00140         break;
    00141     default:
    00142         for (ch = 0; ch < channels; ch++)
    00143         {
    00144             for(i = 0; i < frame_len; i++)
    00145             {
    00146                 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
    00147
    00148                 CLIP(inp, 32767.0f, -32768.0f);
    00149
    00150                 (*sample_buffer)[(i*channels)+ch] = (int16_t)lrintf(inp);
    00151             }
    00152         }
    00153         break;
    00154     }
    00155 }
    00156
    00157 static void to_PCM_24bit(NeAACDecHandle hDecoder, real_t **input,
    00158                          uint8_t channels, uint16_t frame_len,
    00159                          int32_t **sample_buffer)
    00160 {
    00161     uint8_t ch, ch1;
    00162     uint16_t i;
    00163
    00164     switch (CONV(channels,hDecoder->downMatrix))
    00165     {
    00166     case CONV(1,0):
    00167     case CONV(1,1):
    00168         for(i = 0; i < frame_len; i++)
    00169         {
    00170             real_t inp = input[hDecoder->internal_channel[0]][i];
    00171
    00172             inp *= 256.0f;
    00173             CLIP(inp, 8388607.0f, -8388608.0f);
    00174
    00175             (*sample_buffer)[i] = (int32_t)lrintf(inp);
    00176         }
    00177         break;
    00178     case CONV(2,0):
    00179         if (hDecoder->upMatrix)
    00180         {
    00181             ch = hDecoder->internal_channel[0];
    00182             for(i = 0; i < frame_len; i++)
    00183             {
    00184                 real_t inp0 = input[ch][i];
    00185
    00186                 inp0 *= 256.0f;
    00187                 CLIP(inp0, 8388607.0f, -8388608.0f);
    00188
    00189                 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
    00190                 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
    00191             }
    00192         } else {
    00193             ch  = hDecoder->internal_channel[0];
    00194             ch1 = hDecoder->internal_channel[1];
    00195             for(i = 0; i < frame_len; i++)
    00196             {
    00197                 real_t inp0 = input[ch ][i];
    00198                 real_t inp1 = input[ch1][i];
    00199
    00200                 inp0 *= 256.0f;
    00201                 inp1 *= 256.0f;
    00202                 CLIP(inp0, 8388607.0f, -8388608.0f);
    00203                 CLIP(inp1, 8388607.0f, -8388608.0f);
    00204
    00205                 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
    00206                 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
    00207             }
    00208         }
    00209         break;
    00210     default:
    00211         for (ch = 0; ch < channels; ch++)
    00212         {
    00213             for(i = 0; i < frame_len; i++)
    00214             {
    00215                 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
    00216
    00217                 inp *= 256.0f;
    00218                 CLIP(inp, 8388607.0f, -8388608.0f);
    00219
    00220                 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
    00221             }
    00222         }
    00223         break;
    00224     }
    00225 }
    00226
    00227 static void to_PCM_32bit(NeAACDecHandle hDecoder, real_t **input,
    00228                          uint8_t channels, uint16_t frame_len,
    00229                          int32_t **sample_buffer)
    00230 {
    00231     uint8_t ch, ch1;
    00232     uint16_t i;
    00233
    00234     switch (CONV(channels,hDecoder->downMatrix))
    00235     {
    00236     case CONV(1,0):
    00237     case CONV(1,1):
    00238         for(i = 0; i < frame_len; i++)
    00239         {
    00240             real_t inp = input[hDecoder->internal_channel[0]][i];
    00241
    00242             inp *= 65536.0f;
    00243             CLIP(inp, 2147483647.0f, -2147483648.0f);
    00244
    00245             (*sample_buffer)[i] = (int32_t)lrintf(inp);
    00246         }
    00247         break;
    00248     case CONV(2,0):
    00249         if (hDecoder->upMatrix)
    00250         {
    00251             ch = hDecoder->internal_channel[0];
    00252             for(i = 0; i < frame_len; i++)
    00253             {
    00254                 real_t inp0 = input[ch][i];
    00255
    00256                 inp0 *= 65536.0f;
    00257                 CLIP(inp0, 2147483647.0f, -2147483648.0f);
    00258
    00259                 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
    00260                 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp0);
    00261             }
    00262         } else {
    00263             ch  = hDecoder->internal_channel[0];
    00264             ch1 = hDecoder->internal_channel[1];
    00265             for(i = 0; i < frame_len; i++)
    00266             {
    00267                 real_t inp0 = input[ch ][i];
    00268                 real_t inp1 = input[ch1][i];
    00269
    00270                 inp0 *= 65536.0f;
    00271                 inp1 *= 65536.0f;
    00272                 CLIP(inp0, 2147483647.0f, -2147483648.0f);
    00273                 CLIP(inp1, 2147483647.0f, -2147483648.0f);
    00274
    00275                 (*sample_buffer)[(i*2)+0] = (int32_t)lrintf(inp0);
    00276                 (*sample_buffer)[(i*2)+1] = (int32_t)lrintf(inp1);
    00277             }
    00278         }
    00279         break;
    00280     default:
    00281         for (ch = 0; ch < channels; ch++)
    00282         {
    00283             for(i = 0; i < frame_len; i++)
    00284             {
    00285                 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
    00286
    00287                 inp *= 65536.0f;
    00288                 CLIP(inp, 2147483647.0f, -2147483648.0f);
    00289
    00290                 (*sample_buffer)[(i*channels)+ch] = (int32_t)lrintf(inp);
    00291             }
    00292         }
    00293         break;
    00294     }
    00295 }
    00296
    00297 static void to_PCM_float(NeAACDecHandle hDecoder, real_t **input,
    00298                          uint8_t channels, uint16_t frame_len,
    00299                          float32_t **sample_buffer)
    00300 {
    00301     uint8_t ch, ch1;
    00302     uint16_t i;
    00303
    00304     switch (CONV(channels,hDecoder->downMatrix))
    00305     {
    00306     case CONV(1,0):
    00307     case CONV(1,1):
    00308         for(i = 0; i < frame_len; i++)
    00309         {
    00310             real_t inp = input[hDecoder->internal_channel[0]][i];
    00311             (*sample_buffer)[i] = inp*FLOAT_SCALE;
    00312         }
    00313         break;
    00314     case CONV(2,0):
    00315         if (hDecoder->upMatrix)
    00316         {
    00317             ch = hDecoder->internal_channel[0];
    00318             for(i = 0; i < frame_len; i++)
    00319             {
    00320                 real_t inp0 = input[ch][i];
    00321                 (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
    00322                 (*sample_buffer)[(i*2)+1] = inp0*FLOAT_SCALE;
    00323             }
    00324         } else {
    00325             ch  = hDecoder->internal_channel[0];
    00326             ch1 = hDecoder->internal_channel[1];
    00327             for(i = 0; i < frame_len; i++)
    00328             {
    00329                 real_t inp0 = input[ch ][i];
    00330                 real_t inp1 = input[ch1][i];
    00331                 (*sample_buffer)[(i*2)+0] = inp0*FLOAT_SCALE;
    00332                 (*sample_buffer)[(i*2)+1] = inp1*FLOAT_SCALE;
    00333             }
    00334         }
    00335         break;
    00336     default:
    00337         for (ch = 0; ch < channels; ch++)
    00338         {
    00339             for(i = 0; i < frame_len; i++)
    00340             {
    00341                 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
    00342                 (*sample_buffer)[(i*channels)+ch] = inp*FLOAT_SCALE;
    00343             }
    00344         }
    00345         break;
    00346     }
    00347 }
    00348
    00349 static void to_PCM_double(NeAACDecHandle hDecoder, real_t **input,
    00350                           uint8_t channels, uint16_t frame_len,
    00351                           double **sample_buffer)
    00352 {
    00353     uint8_t ch, ch1;
    00354     uint16_t i;
    00355
    00356     switch (CONV(channels,hDecoder->downMatrix))
    00357     {
    00358     case CONV(1,0):
    00359     case CONV(1,1):
    00360         for(i = 0; i < frame_len; i++)
    00361         {
    00362             real_t inp = input[hDecoder->internal_channel[0]][i];
    00363             (*sample_buffer)[i] = (double)inp*FLOAT_SCALE;
    00364         }
    00365         break;
    00366     case CONV(2,0):
    00367         if (hDecoder->upMatrix)
    00368         {
    00369             ch = hDecoder->internal_channel[0];
    00370             for(i = 0; i < frame_len; i++)
    00371             {
    00372                 real_t inp0 = input[ch][i];
    00373                 (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
    00374                 (*sample_buffer)[(i*2)+1] = (double)inp0*FLOAT_SCALE;
    00375             }
    00376         } else {
    00377             ch  = hDecoder->internal_channel[0];
    00378             ch1 = hDecoder->internal_channel[1];
    00379             for(i = 0; i < frame_len; i++)
    00380             {
    00381                 real_t inp0 = input[ch ][i];
    00382                 real_t inp1 = input[ch1][i];
    00383                 (*sample_buffer)[(i*2)+0] = (double)inp0*FLOAT_SCALE;
    00384                 (*sample_buffer)[(i*2)+1] = (double)inp1*FLOAT_SCALE;
    00385             }
    00386         }
    00387         break;
    00388     default:
    00389         for (ch = 0; ch < channels; ch++)
    00390         {
    00391             for(i = 0; i < frame_len; i++)
    00392             {
    00393                 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
    00394                 (*sample_buffer)[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
    00395             }
    00396         }
    00397         break;
    00398     }
    00399 }
    00400
    00401 void *output_to_PCM(NeAACDecHandle hDecoder,
    00402                     real_t **input, void *sample_buffer, uint8_t channels,
    00403                     uint16_t frame_len, uint8_t format)
    00404 {
    00405     int16_t   *short_sample_buffer = (int16_t*)sample_buffer;
    00406     int32_t   *int_sample_buffer = (int32_t*)sample_buffer;
    00407     float32_t *float_sample_buffer = (float32_t*)sample_buffer;
    00408     double    *double_sample_buffer = (double*)sample_buffer;
    00409
    00410 #ifdef PROFILE
    00411     int64_t count = faad_get_ts();
    00412 #endif
    00413
    00414     /* Copy output to a standard PCM buffer */
    00415     switch (format)
    00416     {
    00417     case FAAD_FMT_16BIT:
    00418         to_PCM_16bit(hDecoder, input, channels, frame_len, &short_sample_buffer);
    00419         break;
    00420     case FAAD_FMT_24BIT:
    00421         to_PCM_24bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
    00422         break;
    00423     case FAAD_FMT_32BIT:
    00424         to_PCM_32bit(hDecoder, input, channels, frame_len, &int_sample_buffer);
    00425         break;
    00426     case FAAD_FMT_FLOAT:
    00427         to_PCM_float(hDecoder, input, channels, frame_len, &float_sample_buffer);
    00428         break;
    00429     case FAAD_FMT_DOUBLE:
    00430         to_PCM_double(hDecoder, input, channels, frame_len, &double_sample_buffer);
    00431         break;
    00432     }
    00433
    00434 #ifdef PROFILE
    00435     count = faad_get_ts() - count;
    00436     hDecoder->output_cycles += count;
    00437 #endif
    00438
    00439     return sample_buffer;
    00440 }
    00441
    00442 #else
    00443
    00444 #define DM_MUL FRAC_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
    00445 #define RSQRT2 FRAC_CONST(0.7071067811865475244) // 1/sqrt(2)
    00446
    00447 static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
    00448                                 uint8_t down_matrix, uint8_t up_matrix,
    00449                                 uint8_t *internal_channel)
    00450 {
    00451     if (up_matrix == 1)
    00452         return input[internal_channel[0]][sample];
    00453
    00454     if (!down_matrix)
    00455         return input[internal_channel[channel]][sample];
    00456
    00457     if (channel == 0)
    00458     {
    00459         real_t C   = MUL_F(input[internal_channel[0]][sample], RSQRT2);
    00460         real_t L_S = MUL_F(input[internal_channel[3]][sample], RSQRT2);
    00461         real_t cum = input[internal_channel[1]][sample] + C + L_S;
    00462         return MUL_F(cum, DM_MUL);
    00463     } else {
    00464         real_t C   = MUL_F(input[internal_channel[0]][sample], RSQRT2);
    00465         real_t R_S = MUL_F(input[internal_channel[4]][sample], RSQRT2);
    00466         real_t cum = input[internal_channel[2]][sample] + C + R_S;
    00467         return MUL_F(cum, DM_MUL);
    00468     }
    00469 }
    00470
    00471 void* output_to_PCM(NeAACDecHandle hDecoder,
    00472                     real_t **input, void *sample_buffer, uint8_t channels,
    00473                     uint16_t frame_len, uint8_t format)
    00474 {
    00475     uint8_t ch;
    00476     uint16_t i;
    00477     int16_t *short_sample_buffer = (int16_t*)sample_buffer;
    00478     int32_t *int_sample_buffer = (int32_t*)sample_buffer;
    00479
    00480     /* Copy output to a standard PCM buffer */
    00481     for (ch = 0; ch < channels; ch++)
    00482     {
    00483         switch (format)
    00484         {
    00485         case FAAD_FMT_16BIT:
    00486             for(i = 0; i < frame_len; i++)
    00487             {
    00488                 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
    00489                     hDecoder->internal_channel);
    00490                 if (tmp >= 0)
    00491                 {
    00492                     tmp += (1 << (REAL_BITS-1));
    00493                     if (tmp >= REAL_CONST(32767))
    00494                     {
    00495                         tmp = REAL_CONST(32767);
    00496                     }
    00497                 } else {
    00498                     tmp += -(1 << (REAL_BITS-1));
    00499                     if (tmp <= REAL_CONST(-32768))
    00500                     {
    00501                         tmp = REAL_CONST(-32768);
    00502                     }
    00503                 }
    00504                 tmp >>= REAL_BITS;
    00505                 short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
    00506             }
    00507             break;
    00508         case FAAD_FMT_24BIT:
    00509             for(i = 0; i < frame_len; i++)
    00510             {
    00511                 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
    00512                     hDecoder->internal_channel);
    00513                 if (tmp >= 0)
    00514                 {
    00515                     tmp += (1 << (REAL_BITS-9));
    00516                     tmp >>= (REAL_BITS-8);
    00517                     if (tmp >= 8388607)
    00518                     {
    00519                         tmp = 8388607;
    00520                     }
    00521                 } else {
    00522                     tmp += -(1 << (REAL_BITS-9));
    00523                     tmp >>= (REAL_BITS-8);
    00524                     if (tmp <= -8388608)
    00525                     {
    00526                         tmp = -8388608;
    00527                     }
    00528                 }
    00529                 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
    00530             }
    00531             break;
    00532         case FAAD_FMT_32BIT:
    00533             for(i = 0; i < frame_len; i++)
    00534             {
    00535                 int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
    00536                     hDecoder->internal_channel);
    00537                 if (tmp >= 0)
    00538                 {
    00539                     tmp += (1 << (16-REAL_BITS-1));
    00540                     tmp <<= (16-REAL_BITS);
    00541                 } else {
    00542                     tmp += -(1 << (16-REAL_BITS-1));
    00543                     tmp <<= (16-REAL_BITS);
    00544                 }
    00545                 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
    00546             }
    00547             break;
    00548         case FAAD_FMT_FIXED:
    00549             for(i = 0; i < frame_len; i++)
    00550             {
    00551                 real_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->upMatrix,
    00552                     hDecoder->internal_channel);
    00553                 int_sample_buffer[(i*channels)+ch] = (int32_t)tmp;
    00554             }
    00555             break;
    00556         }
    00557     }
    00558
    00559     return sample_buffer;
    00560 }
    00561
    00562 #endif