10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
|
3 ** Copyright (C) 2003 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 ** Commercial non-GPL licensing of this software is possible.
|
|
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
24 **
|
|
25 ** $Id$
|
|
26 **/
|
|
27
|
|
28 #include "common.h"
|
|
29 #include "structs.h"
|
|
30
|
|
31 #include "output.h"
|
|
32 #include "decoder.h"
|
|
33
|
|
34 #ifndef FIXED_POINT
|
|
35
|
|
36 #include "dither.h"
|
|
37
|
|
38
|
|
39 #define ftol(A,B) {tmp = *(int32_t*) & A - 0x4B7F8000; \
|
|
40 B = (int16_t)((tmp==(int16_t)tmp) ? tmp : (tmp>>31)^0x7FFF);}
|
|
41
|
|
42 #define ROUND(x) ((x >= 0) ? (int32_t)floor((x) + 0.5) : (int32_t)ceil((x) + 0.5))
|
|
43
|
|
44 #define ROUND32(x) ROUND(x)
|
|
45
|
|
46 #define ROUND64(x) (doubletmp = (x) + Dither.Add + (int64_t)0x001FFFFD80000000L, *(int64_t*)(&doubletmp) - (int64_t)0x433FFFFD80000000L)
|
|
47
|
|
48 #define FLOAT_SCALE (1.0f/(1<<15))
|
|
49
|
|
50 dither_t Dither;
|
|
51 double doubletmp;
|
|
52
|
10989
|
53 #define DM_MUL ((real_t)1.0/((real_t)1.0+(real_t)sqrt(2.0)))
|
10725
|
54
|
|
55 static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
|
|
56 uint8_t downMatrix, uint8_t *internal_channel)
|
|
57 {
|
|
58 if (downMatrix)
|
|
59 {
|
|
60 if (channel == 0)
|
|
61 {
|
|
62 return DM_MUL * (input[internal_channel[1]][sample] +
|
10989
|
63 input[internal_channel[0]][sample]/(real_t)sqrt(2.) +
|
|
64 input[internal_channel[3]][sample]/(real_t)sqrt(2.));
|
10725
|
65 } else {
|
|
66 return DM_MUL * (input[internal_channel[2]][sample] +
|
10989
|
67 input[internal_channel[0]][sample]/(real_t)sqrt(2.) +
|
|
68 input[internal_channel[4]][sample]/(real_t)sqrt(2.));
|
10725
|
69 }
|
|
70 } else {
|
|
71 return input[internal_channel[channel]][sample];
|
|
72 }
|
|
73 }
|
|
74
|
|
75 void* output_to_PCM(faacDecHandle hDecoder,
|
|
76 real_t **input, void *sample_buffer, uint8_t channels,
|
|
77 uint16_t frame_len, uint8_t format)
|
|
78 {
|
|
79 uint8_t ch;
|
|
80 uint16_t i, j = 0;
|
|
81 uint8_t internal_channel;
|
|
82
|
|
83 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
|
|
84 int32_t *int_sample_buffer = (int32_t*)sample_buffer;
|
|
85 float32_t *float_sample_buffer = (float32_t*)sample_buffer;
|
|
86 double *double_sample_buffer = (double*)sample_buffer;
|
|
87
|
|
88 /* Copy output to a standard PCM buffer */
|
|
89 for (ch = 0; ch < channels; ch++)
|
|
90 {
|
|
91 internal_channel = hDecoder->internal_channel[ch];
|
|
92
|
|
93 switch (format)
|
|
94 {
|
|
95 case FAAD_FMT_16BIT:
|
|
96 for(i = 0; i < frame_len; i++)
|
|
97 {
|
|
98 int32_t tmp;
|
|
99 real_t ftemp;
|
|
100 //real_t inp = input[internal_channel][i];
|
|
101 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
|
102
|
|
103 ftemp = inp + 0xff8000;
|
|
104 ftol(ftemp, short_sample_buffer[(i*channels)+ch]);
|
|
105 }
|
|
106 break;
|
|
107 case FAAD_FMT_16BIT_DITHER:
|
|
108 for(i = 0; i < frame_len; i++, j++)
|
|
109 {
|
10989
|
110 //real_t inp = input[internal_channel][i];
|
|
111 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
10725
|
112 double Sum = inp * 65535.f;
|
|
113 int64_t val;
|
|
114 if(j > 31)
|
|
115 j = 0;
|
|
116 val = dither_output(1, 0, j, Sum, ch) / 65536;
|
|
117 if (val > (1<<15)-1)
|
|
118 val = (1<<15)-1;
|
|
119 else if (val < -(1<<15))
|
|
120 val = -(1<<15);
|
|
121 short_sample_buffer[(i*channels)+ch] = (int16_t)val;
|
|
122 }
|
|
123 break;
|
|
124 case FAAD_FMT_16BIT_L_SHAPE:
|
|
125 case FAAD_FMT_16BIT_M_SHAPE:
|
|
126 case FAAD_FMT_16BIT_H_SHAPE:
|
|
127 for(i = 0; i < frame_len; i++, j++)
|
|
128 {
|
10989
|
129 //real_t inp = input[internal_channel][i];
|
|
130 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
10725
|
131 double Sum = inp * 65535.f;
|
|
132 int64_t val;
|
|
133 if(j > 31)
|
|
134 j = 0;
|
|
135 val = dither_output(1, 1, j, Sum, ch) / 65536;
|
|
136 if (val > (1<<15)-1)
|
|
137 val = (1<<15)-1;
|
|
138 else if (val < -(1<<15))
|
|
139 val = -(1<<15);
|
|
140 short_sample_buffer[(i*channels)+ch] = (int16_t)val;
|
|
141 }
|
|
142 break;
|
|
143 case FAAD_FMT_24BIT:
|
|
144 for(i = 0; i < frame_len; i++)
|
|
145 {
|
10989
|
146 //real_t inp = input[internal_channel][i];
|
|
147 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
10725
|
148 if (inp > (1<<15)-1)
|
|
149 inp = (1<<15)-1;
|
|
150 else if (inp < -(1<<15))
|
|
151 inp = -(1<<15);
|
|
152 int_sample_buffer[(i*channels)+ch] = ROUND(inp*(1<<8));
|
|
153 }
|
|
154 break;
|
|
155 case FAAD_FMT_32BIT:
|
|
156 for(i = 0; i < frame_len; i++)
|
|
157 {
|
10989
|
158 //real_t inp = input[internal_channel][i];
|
|
159 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
10725
|
160 if (inp > (1<<15)-1)
|
|
161 inp = (1<<15)-1;
|
|
162 else if (inp < -(1<<15))
|
|
163 inp = -(1<<15);
|
|
164 int_sample_buffer[(i*channels)+ch] = ROUND32(inp*(1<<16));
|
|
165 }
|
|
166 break;
|
|
167 case FAAD_FMT_FLOAT:
|
|
168 for(i = 0; i < frame_len; i++)
|
|
169 {
|
10989
|
170 //real_t inp = input[internal_channel][i];
|
|
171 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
10725
|
172 float_sample_buffer[(i*channels)+ch] = inp*FLOAT_SCALE;
|
|
173 }
|
|
174 break;
|
|
175 case FAAD_FMT_DOUBLE:
|
|
176 for(i = 0; i < frame_len; i++)
|
|
177 {
|
10989
|
178 //real_t inp = input[internal_channel][i];
|
|
179 real_t inp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
|
10725
|
180 double_sample_buffer[(i*channels)+ch] = (double)inp*FLOAT_SCALE;
|
|
181 }
|
|
182 break;
|
|
183 }
|
|
184 }
|
|
185
|
|
186 return sample_buffer;
|
|
187 }
|
|
188
|
|
189
|
|
190 /* Dither output */
|
|
191 static int64_t dither_output(uint8_t dithering, uint8_t shapingtype, uint16_t i, double Sum, uint8_t k)
|
|
192 {
|
|
193 double Sum2;
|
|
194 int64_t val;
|
|
195 if(dithering)
|
|
196 {
|
|
197 if(!shapingtype)
|
|
198 {
|
|
199 double tmp = Random_Equi(Dither.Dither);
|
10989
|
200 Sum2 = tmp - (double)Dither.LastRandomNumber[k];
|
|
201 Dither.LastRandomNumber[k] = (int32_t)tmp;
|
10725
|
202 Sum2 = Sum += Sum2;
|
|
203 val = ROUND64(Sum2)&Dither.Mask;
|
|
204 } else {
|
|
205 Sum2 = Random_Triangular(Dither.Dither) - scalar16(Dither.DitherHistory[k], Dither.FilterCoeff + i);
|
10989
|
206 Sum += Dither.DitherHistory[k][(-1-i)&15] = (float32_t)Sum2;
|
10725
|
207 Sum2 = Sum + scalar16(Dither.ErrorHistory[k], Dither.FilterCoeff + i );
|
|
208 val = ROUND64(Sum2)&Dither.Mask;
|
|
209 Dither.ErrorHistory[k][(-1-i)&15] = (float)(Sum - val);
|
|
210 }
|
|
211 return val;
|
|
212 }
|
|
213 else
|
|
214 return ROUND64 (Sum);
|
|
215 }
|
|
216
|
|
217 #else
|
|
218
|
|
219 void* output_to_PCM(faacDecHandle hDecoder,
|
|
220 real_t **input, void *sample_buffer, uint8_t channels,
|
|
221 uint16_t frame_len, uint8_t format)
|
|
222 {
|
|
223 uint8_t ch;
|
|
224 uint16_t i;
|
|
225 int16_t *short_sample_buffer = (int16_t*)sample_buffer;
|
|
226
|
|
227 /* Copy output to a standard PCM buffer */
|
|
228 for (ch = 0; ch < channels; ch++)
|
|
229 {
|
|
230 for(i = 0; i < frame_len; i++)
|
|
231 {
|
|
232 int32_t tmp = input[ch][i];
|
|
233 tmp += (1 << (REAL_BITS-1));
|
|
234 tmp >>= REAL_BITS;
|
|
235 if (tmp > 0x7fff) tmp = 0x7fff;
|
|
236 else if (tmp <= -32768) tmp = -32768;
|
|
237 short_sample_buffer[(i*channels)+ch] = (int16_t)tmp;
|
|
238 }
|
|
239 }
|
|
240
|
|
241 return sample_buffer;
|
|
242 }
|
|
243
|
|
244 #endif
|