/proc/self/cwd/libfaad/shim.c
Line | Count | Source |
1 | | /* |
2 | | ** FAAD2 compatibility layer over libfaad3. |
3 | | ** |
4 | | ** Exports the NeAACDec* API of FAAD2 on top of the faad3 decoder API. |
5 | | ** Everything legacy (frame counter, output format conversion, FAAD2 channel |
6 | | ** order and downmix, error numbers) lives here so libfaad3 stays clean. |
7 | | ** |
8 | | ** Copyright (C) 2026 Nils Schimmelmann |
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-1307, USA. |
23 | | ** |
24 | | ** The error message table (libfaad/error.c) and the downmix coefficients |
25 | | ** (libfaad/output.c) come from FAAD2: |
26 | | ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com |
27 | | ** The "appropriate copyright message" mentioned in section 2c of the GPLv2 |
28 | | ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com" |
29 | | */ |
30 | | |
31 | | #include <math.h> |
32 | | #include <stdint.h> |
33 | | #include <stdlib.h> |
34 | | #include <string.h> |
35 | | |
36 | | #include <faad3.h> |
37 | | #include <faar.h> |
38 | | |
39 | | #include "neaacdec.h" |
40 | | |
41 | 0 | #define SHIM_MAX_CHANNELS 64 |
42 | 0 | #define SYNC_SEARCH 768 |
43 | 1.14k | #define DRC_TARGET_DB (-20.0f) |
44 | | |
45 | | /* FAAD2 error numbers; the strings are indexed by number. */ |
46 | | enum { |
47 | | ERR_NONE = 0, |
48 | | ERR_BITSTREAM = 10, |
49 | | ERR_NO_SYNC = 5, |
50 | | ERR_BAD_CHANNELS = 12, |
51 | | ERR_INPUT_SMALL = 14, |
52 | | ERR_OUTPUT_SMALL = 27, |
53 | | ERR_NOT_ALLOWED = 32 |
54 | | }; |
55 | | |
56 | | static char *const err_msg[] = { |
57 | | "No error", |
58 | | "Gain control not yet implemented", |
59 | | "Pulse coding not allowed in short blocks", |
60 | | "Invalid huffman codebook", |
61 | | "Scalefactor out of range", |
62 | | "Unable to find ADTS syncword", |
63 | | "Channel coupling not yet implemented", |
64 | | "Channel configuration not allowed in error resilient frame", |
65 | | "Bit error in error resilient scalefactor decoding", |
66 | | "Error decoding huffman scalefactor (bitstream error)", |
67 | | "Error decoding huffman codeword (bitstream error)", |
68 | | "Non existent huffman codebook number found", |
69 | | "Invalid number of channels", |
70 | | "Maximum number of bitstream elements exceeded", |
71 | | "Input data buffer too small", |
72 | | "Array index out of range", |
73 | | "Maximum number of scalefactor bands exceeded", |
74 | | "Quantised value out of range", |
75 | | "LTP lag out of range", |
76 | | "Invalid SBR parameter decoded", |
77 | | "SBR called without being initialised", |
78 | | "Unexpected channel configuration change", |
79 | | "Error in program_config_element", |
80 | | "First SBR frame is not the same as first AAC frame", |
81 | | "Unexpected fill element with SBR data", |
82 | | "Not all elements were provided with SBR data", |
83 | | "LTP decoding not available", |
84 | | "Output data buffer too small", |
85 | | "CRC error in DRM data", |
86 | | "PNS not allowed in DRM data stream", |
87 | | "No standard extension payload allowed in DRM", |
88 | | "PCE shall be the first element in a frame", |
89 | | "Bitstream value not allowed by specification", |
90 | | "MAIN prediction not initialised", |
91 | | "Long term prediction not initialised" |
92 | | }; |
93 | | |
94 | | /* Sample type delivered by faad3 into the scratch buffer. */ |
95 | | typedef enum { T_I16, T_I32, T_F32 } pcm_type; |
96 | | |
97 | | typedef struct { |
98 | | NeAACDecConfiguration cfg; |
99 | | faad_decoder *dec; |
100 | | enum faad_stream_format stream; |
101 | | uint8_t *asc; |
102 | | uint32_t asc_len; |
103 | | pcm_type type; |
104 | | unsigned fmt; /* output format and downmix in force for the open decoder */ |
105 | | int downmix_on; |
106 | | int dirty; /* configuration changed since the decoder was opened */ |
107 | | int decoded; /* a frame has been decoded since the decoder was opened */ |
108 | | long frame; /* FAAD2 frame counter; drives the silent first frame */ |
109 | | uint8_t *pcm; /* scratch for faad3 output, max_output_bytes */ |
110 | | uint32_t pcm_cap; |
111 | | uint8_t *out; /* converted output returned by NeAACDecDecode */ |
112 | | size_t out_cap; |
113 | | int adts_sfi; /* sampling frequency index of the ADTS stream, -1 until known */ |
114 | | int upsample; /* FAAD2 doubled the rate of this stream's frames without SBR data */ |
115 | | int downsample; /* implicit SBR above 24 kHz: FAAD2 returned the core rate */ |
116 | | faar_resampler *rs; |
117 | | uint32_t rs_ch, rs_rate, rs_out; |
118 | | float *fifo; /* resampled frames not yet returned, interleaved */ |
119 | | size_t fifo_n, fifo_cap; |
120 | | float *up; /* frames returned by the last call */ |
121 | | float *cv; /* frames converted to float for decimating */ |
122 | | size_t cv_cap; |
123 | | size_t up_cap; |
124 | | } shim_t; |
125 | | |
126 | | static const uint32_t sr_table[12] = { |
127 | | 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000 |
128 | | }; |
129 | | |
130 | | char *NeAACDecGetErrorMessage(unsigned char errcode) |
131 | 1.16k | { |
132 | 1.16k | if (errcode >= sizeof(err_msg) / sizeof(err_msg[0])) |
133 | 808 | return NULL; |
134 | 352 | return err_msg[errcode]; |
135 | 1.16k | } |
136 | | |
137 | | unsigned long NeAACDecGetCapabilities(void) |
138 | 1.16k | { |
139 | 1.16k | return LC_DEC_CAP; |
140 | 1.16k | } |
141 | | |
142 | | int NeAACDecGetVersion(char **faad_id_string, char **faad_copyright_string) |
143 | 1.16k | { |
144 | 1.16k | static char id[] = FAAD2_VERSION; |
145 | 1.16k | static char copy[] = "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"; |
146 | 1.16k | if (faad_id_string) |
147 | 1.16k | *faad_id_string = id; |
148 | 1.16k | if (faad_copyright_string) |
149 | 1.16k | *faad_copyright_string = copy; |
150 | 1.16k | return 0; |
151 | 1.16k | } |
152 | | |
153 | | NeAACDecHandle NeAACDecOpen(void) |
154 | 0 | { |
155 | 0 | shim_t *s = calloc(1, sizeof(*s)); |
156 | 0 | if (!s) |
157 | 0 | return NULL; |
158 | 0 | s->adts_sfi = -1; |
159 | 0 | s->cfg.outputFormat = FAAD_FMT_16BIT; |
160 | 0 | s->cfg.defObjectType = MAIN; |
161 | 0 | s->cfg.defSampleRate = 44100; |
162 | 0 | return s; |
163 | 0 | } |
164 | | |
165 | | NeAACDecConfigurationPtr NeAACDecGetCurrentConfiguration(NeAACDecHandle h) |
166 | 0 | { |
167 | 0 | shim_t *s = h; |
168 | 0 | return s ? &s->cfg : NULL; |
169 | 0 | } |
170 | | |
171 | | unsigned char NeAACDecSetConfiguration(NeAACDecHandle h, NeAACDecConfigurationPtr c) |
172 | 0 | { |
173 | 0 | shim_t *s = h; |
174 | 0 | if (!s || !c) |
175 | 0 | return 0; |
176 | | /* defObjectType is not validated: callers read the defaults (MAIN), change |
177 | | * one field and write the structure back. */ |
178 | 0 | if (c->defSampleRate == 0) |
179 | 0 | return 0; |
180 | 0 | if (c->outputFormat < FAAD_FMT_16BIT || c->outputFormat > FAAD_FMT_DOUBLE) |
181 | 0 | return 0; |
182 | 0 | if (c->downMatrix > 1) |
183 | 0 | return 0; |
184 | | /* Callers edit the structure returned by GetCurrentConfiguration in place, |
185 | | * so compare with the settings the open decoder uses, not with s->cfg. */ |
186 | 0 | s->dirty = c->outputFormat != s->fmt || c->downMatrix != s->downmix_on; |
187 | 0 | s->cfg = *c; |
188 | 0 | return 1; |
189 | 0 | } |
190 | | |
191 | | /* FAAD2 doubled the rate of streams without SBR at or below 24 kHz, assuming |
192 | | * implicit SBR, and returned a fixed 2 x frame_length samples per frame. |
193 | | * libfaar produces its output in blocks, so frames are queued and returned |
194 | | * from a FIFO that starts with UP_LEAD frames of silence, enough that a block |
195 | | * boundary never leaves a call short. The lead is the delay of the output, and |
196 | | * 962 samples is the delay of FAAD2's QMF upsampling, which callers align |
197 | | * against. With a two-stage cascade libfaar falls at most 126 frames behind; |
198 | | * the single stage it plans on its own falls behind by up to 1748. */ |
199 | 0 | #define UP_LEAD 962 |
200 | | |
201 | | /* FAAD2 returned implicit SBR above 24 kHz at the core rate, and its 32-band |
202 | | * synthesis delayed that output by the same amount as libfaad3's full-rate SBR |
203 | | * output, so the decimated stream needs no lead to line up with FAAD2's. libfaar |
204 | | * cannot do that: a zero-phase 2:1 filter needs 147 input frames of lookahead, |
205 | | * so it always has 73 output frames less than the input timeline. The FIFO must |
206 | | * start with at least that much silence or it underruns; the output is then 73 |
207 | | * samples late against FAAD2. This is the smallest lag libfaar offers (FAST |
208 | | * quality with an explicit single stage); automatic planning and other stages |
209 | | * fall further behind. */ |
210 | 0 | #define DOWN_LEAD 73 |
211 | | |
212 | | static void upsample_reset(shim_t *s) |
213 | 0 | { |
214 | 0 | if (s->rs) |
215 | 0 | faar_resampler_close(&s->rs); |
216 | 0 | s->fifo_n = 0; |
217 | 0 | } |
218 | | |
219 | | static void upsample_free(shim_t *s) |
220 | 0 | { |
221 | 0 | upsample_reset(s); |
222 | 0 | free(s->fifo); |
223 | 0 | free(s->up); |
224 | 0 | free(s->cv); |
225 | 0 | s->fifo = s->up = s->cv = NULL; |
226 | 0 | s->fifo_cap = s->up_cap = s->cv_cap = 0; |
227 | 0 | } |
228 | | |
229 | | static int grow(float **buf, size_t *cap, size_t need) |
230 | 0 | { |
231 | 0 | float *p; |
232 | |
|
233 | 0 | if (*cap >= need) |
234 | 0 | return 0; |
235 | 0 | p = realloc(*buf, need * sizeof(float)); |
236 | 0 | if (!p) |
237 | 0 | return -1; |
238 | 0 | *buf = p; |
239 | 0 | *cap = need; |
240 | 0 | return 0; |
241 | 0 | } |
242 | | |
243 | | /* Resample interleaved frames by a factor of two, priming the FIFO with lead |
244 | | * frames of silence. Returns the frames * out_rate / in_rate result, valid until |
245 | | * the next call, or NULL on failure. */ |
246 | | static const float *resample_frames(shim_t *s, const float *in, uint32_t frames, uint32_t n, |
247 | | uint32_t in_rate, uint32_t out_rate, size_t lead) |
248 | 0 | { |
249 | 0 | const size_t out_frames = (size_t)frames * out_rate / in_rate; |
250 | 0 | size_t got; |
251 | |
|
252 | 0 | if (s->rs && (s->rs_ch != n || s->rs_rate != in_rate || s->rs_out != out_rate)) |
253 | 0 | upsample_reset(s); |
254 | 0 | if (!s->rs) { |
255 | 0 | faar_config cfg; |
256 | 0 | if (faar_config_init(&cfg, sizeof(cfg)) != FAAR_OK) |
257 | 0 | return NULL; |
258 | 0 | cfg.in_rate = in_rate; |
259 | 0 | cfg.out_rate = out_rate; |
260 | 0 | cfg.channels = n; |
261 | 0 | cfg.quality = FAAR_QUALITY_FAST; |
262 | 0 | cfg.decimate_factor = 2; |
263 | 0 | if (faar_resampler_open(&cfg, &s->rs) != FAAR_OK) |
264 | 0 | return NULL; |
265 | 0 | s->rs_ch = n; |
266 | 0 | s->rs_rate = in_rate; |
267 | 0 | s->rs_out = out_rate; |
268 | 0 | if (grow(&s->fifo, &s->fifo_cap, (size_t)(lead + out_frames) * n)) |
269 | 0 | return NULL; |
270 | 0 | memset(s->fifo, 0, lead * n * sizeof(float)); |
271 | 0 | s->fifo_n = lead; |
272 | 0 | } |
273 | | /* A block holds up to a few thousand frames beyond the input's share. */ |
274 | 0 | if (grow(&s->fifo, &s->fifo_cap, (s->fifo_n + out_frames + 8192) * n) || |
275 | 0 | grow(&s->up, &s->up_cap, out_frames * n)) |
276 | 0 | return NULL; |
277 | 0 | for (;;) { |
278 | 0 | faar_status st = faar_resampler_process(s->rs, in, frames, s->fifo + s->fifo_n * n, |
279 | 0 | s->fifo_cap / n - s->fifo_n, &got); |
280 | 0 | if (st != FAAR_OK) |
281 | 0 | return NULL; |
282 | 0 | s->fifo_n += got; |
283 | 0 | if (!got) |
284 | 0 | break; |
285 | 0 | in = NULL; |
286 | 0 | frames = 0; |
287 | 0 | if (grow(&s->fifo, &s->fifo_cap, (s->fifo_n + out_frames + 8192) * n)) |
288 | 0 | return NULL; |
289 | 0 | } |
290 | 0 | if (s->fifo_n < out_frames) { |
291 | | /* Not expected with the lead; keep the frame size at the cost of a gap. */ |
292 | 0 | size_t gap = out_frames - s->fifo_n; |
293 | 0 | memmove(s->fifo + gap * n, s->fifo, s->fifo_n * n * sizeof(float)); |
294 | 0 | memset(s->fifo, 0, gap * n * sizeof(float)); |
295 | 0 | s->fifo_n = out_frames; |
296 | 0 | } |
297 | 0 | memcpy(s->up, s->fifo, out_frames * n * sizeof(float)); |
298 | 0 | s->fifo_n -= out_frames; |
299 | 0 | memmove(s->fifo, s->fifo + out_frames * n, s->fifo_n * n * sizeof(float)); |
300 | 0 | return s->up; |
301 | 0 | } |
302 | | |
303 | | /* The stream is assumed to carry SBR implicitly. */ |
304 | | static int implicit_sbr(const faad_stream_info *info) |
305 | 96 | { |
306 | 96 | return info->object_type == FAAD_OBJ_LC && info->sample_rate > 0 && info->sample_rate <= 24000; |
307 | 96 | } |
308 | | |
309 | | static int wants_upsample(const shim_t *s, const faad_stream_info *info) |
310 | 0 | { |
311 | 0 | return !s->cfg.dontUpSampleImplicitSBR && implicit_sbr(info); |
312 | 0 | } |
313 | | |
314 | | static void shim_release(shim_t *s) |
315 | 0 | { |
316 | 0 | upsample_free(s); |
317 | 0 | if (s->dec) |
318 | 0 | faad_decoder_close(&s->dec); |
319 | 0 | free(s->pcm); |
320 | 0 | s->pcm = NULL; |
321 | 0 | s->pcm_cap = 0; |
322 | 0 | } |
323 | | |
324 | | /* Output format requested from faad3 for the configured FAAD2 format. The |
325 | | * 32-bit and double formats and the FAAD2 downmix go through float. */ |
326 | | static pcm_type wanted_type(const shim_t *s) |
327 | 0 | { |
328 | 0 | const NeAACDecConfiguration *c = &s->cfg; |
329 | |
|
330 | 0 | if (c->downMatrix || s->upsample) |
331 | 0 | return T_F32; |
332 | 0 | switch (c->outputFormat) { |
333 | 0 | case FAAD_FMT_16BIT: return T_I16; |
334 | 0 | case FAAD_FMT_24BIT: return T_I32; |
335 | 0 | default: return T_F32; |
336 | 0 | } |
337 | 0 | } |
338 | | |
339 | | static enum faad_output_format faad3_format(pcm_type t) |
340 | 1.14k | { |
341 | 1.14k | return t == T_I16 ? FAAD_OUTPUT_16BIT : t == T_I32 ? FAAD_OUTPUT_24IN32 : FAAD_OUTPUT_FLOAT; |
342 | 1.14k | } |
343 | | |
344 | | static faad_status shim_open_dec(const shim_t *s, enum faad_stream_format stream, pcm_type type, |
345 | | const uint8_t *asc, uint32_t asc_len, faad_decoder **dec) |
346 | 1.14k | { |
347 | 1.14k | faad_config cfg; |
348 | 1.14k | faad_status st = faad_config_init(&cfg, sizeof(cfg)); |
349 | 1.14k | if (st != FAAD_OK) |
350 | 0 | return st; |
351 | 1.14k | (void)s; |
352 | 1.14k | cfg.stream_format = stream; |
353 | 1.14k | cfg.output_format = faad3_format(type); |
354 | | /* FAAD2 applies the full compression and normalises to -20 dBFS. */ |
355 | 1.14k | cfg.drc_cut = 127; |
356 | 1.14k | cfg.drc_boost = 127; |
357 | 1.14k | cfg.drc_target_level = DRC_TARGET_DB; |
358 | 1.14k | return faad_decoder_open(&cfg, asc, asc_len, dec); |
359 | 1.14k | } |
360 | | |
361 | | /* (Re)open the faad3 decoder for the stored stream parameters. */ |
362 | | static int shim_reopen(shim_t *s) |
363 | 0 | { |
364 | 0 | faad_stream_info info = { .struct_size = sizeof(info) }; |
365 | 0 | if (s->dec) |
366 | 0 | faad_decoder_close(&s->dec); |
367 | 0 | free(s->pcm); |
368 | 0 | s->pcm = NULL; |
369 | 0 | s->type = wanted_type(s); |
370 | 0 | upsample_reset(s); |
371 | 0 | s->fmt = s->cfg.outputFormat; |
372 | 0 | s->downmix_on = s->cfg.downMatrix; |
373 | 0 | if (shim_open_dec(s, s->stream, s->type, s->asc, s->asc_len, &s->dec) != FAAD_OK) |
374 | 0 | return -1; |
375 | 0 | if (faad_decoder_get_info(s->dec, &info) != FAAD_OK) |
376 | 0 | return -1; |
377 | 0 | s->pcm = malloc(info.max_output_bytes); |
378 | 0 | if (!s->pcm) |
379 | 0 | return -1; |
380 | 0 | s->pcm_cap = info.max_output_bytes; |
381 | 0 | s->dirty = 0; |
382 | 0 | s->decoded = 0; |
383 | 0 | return 0; |
384 | 0 | } |
385 | | |
386 | | static int adts_sniff(const unsigned char *b, unsigned long n) |
387 | 0 | { |
388 | 0 | return n >= 7 && b[0] == 0xFF && (b[1] & 0xF6) == 0xF0; |
389 | 0 | } |
390 | | |
391 | | long NeAACDecInit(NeAACDecHandle h, unsigned char *buffer, unsigned long buffer_size, |
392 | | unsigned long *samplerate, unsigned char *channels) |
393 | 0 | { |
394 | 0 | shim_t *s = h; |
395 | 0 | faad_decoder *probe = NULL; |
396 | 0 | faad_stream_info info = { .struct_size = sizeof(info) }; |
397 | 0 | uint32_t used = 0, wr = 0, fl = 0; |
398 | 0 | uint8_t *pcm; |
399 | 0 | unsigned cc; |
400 | 0 | enum faad_object_type probed = FAAD_OBJ_LC; |
401 | |
|
402 | 0 | if (!s || !samplerate || !channels || buffer_size == 0 || !buffer) |
403 | 0 | return -1; |
404 | 0 | { |
405 | 0 | unsigned long k; |
406 | 0 | for (k = 0; k < buffer_size && !adts_sniff(buffer + k, buffer_size - k); k++) |
407 | 0 | ; |
408 | 0 | if (k >= buffer_size) |
409 | 0 | return -1; |
410 | 0 | buffer += k; |
411 | 0 | buffer_size -= k; |
412 | | /* Only AAC-LC decodes. Say so here, where players can still fall back |
413 | | * to another decoder, rather than failing on the first frame. */ |
414 | 0 | if ((buffer[2] >> 6) != 1) |
415 | 0 | return -1; |
416 | 0 | } |
417 | | |
418 | 0 | free(s->asc); |
419 | 0 | s->asc = NULL; |
420 | 0 | s->asc_len = 0; |
421 | 0 | s->stream = FAAD_STREAM_ADTS; |
422 | 0 | s->frame = 0; |
423 | 0 | s->upsample = 0; |
424 | 0 | s->downsample = 0; |
425 | 0 | s->adts_sfi = (buffer[2] >> 2) & 15; |
426 | | |
427 | | /* Report the format the decoder will really emit: decode the first frame |
428 | | * on a scratch decoder. A buffer too short for a frame falls back to the |
429 | | * ADTS header. */ |
430 | 0 | { |
431 | 0 | unsigned sfi = (buffer[2] >> 2) & 15; |
432 | 0 | cc = ((buffer[2] & 1) << 2) | (buffer[3] >> 6); |
433 | 0 | *samplerate = sr_table[sfi < 12 ? sfi : 0]; |
434 | 0 | *channels = (unsigned char)(cc == 7 ? 8 : cc); |
435 | 0 | } |
436 | 0 | if (shim_open_dec(s, FAAD_STREAM_ADTS, T_I16, NULL, 0, &probe) == FAAD_OK) { |
437 | 0 | if (faad_decoder_get_info(probe, &info) == FAAD_OK && |
438 | 0 | (pcm = malloc(info.max_output_bytes)) != NULL) { |
439 | 0 | if (faad_decode_frame(probe, buffer, (uint32_t)buffer_size, &used, pcm, |
440 | 0 | info.max_output_bytes, &wr, &fl) == FAAD_OK && |
441 | 0 | faad_decoder_get_info(probe, &info) == FAAD_OK && (info.flags & FAAD_STREAM_FORMAT_KNOWN)) { |
442 | 0 | *samplerate = info.sample_rate; |
443 | 0 | *channels = (unsigned char)info.channels; |
444 | 0 | probed = info.object_type; |
445 | 0 | } |
446 | 0 | free(pcm); |
447 | 0 | } |
448 | 0 | faad_decoder_close(&probe); |
449 | 0 | } |
450 | | /* FAAD2 2.11.3 upmixed every mono stream to stereo (implicit PS signalling): |
451 | | * the Init functions reported two channels and every frame that decoded to |
452 | | * one channel came back as dual-mono, so PS showing up mid-stream (ADTS |
453 | | * cannot signal it up front) never changes the channel count. The frames |
454 | | * are upmixed per frame in shim_decode, as FAAD2 did. */ |
455 | 0 | if (*channels == 1) |
456 | 0 | *channels = 2; |
457 | | /* A stream without SBR data at or below 24 kHz is assumed to carry SBR |
458 | | * implicitly, and FAAD2 reported and returned its doubled rate. */ |
459 | 0 | info.object_type = probed; |
460 | 0 | info.sample_rate = (uint32_t)*samplerate; |
461 | 0 | if (wants_upsample(s, &info)) { |
462 | 0 | s->upsample = 1; |
463 | 0 | *samplerate *= 2; |
464 | 0 | } else if (sr_table[((buffer[2] >> 2) & 15) < 12 ? (buffer[2] >> 2) & 15 : 0] > 24000) { |
465 | | /* Above 24 kHz FAAD2 configured the decoder from the header, assumed |
466 | | * implicit SBR and reported the core rate. Frames that carry SBR then |
467 | | * came back downsampled to it; dontUpSampleImplicitSBR kept them at the |
468 | | * SBR rate. */ |
469 | 0 | *samplerate = sr_table[((buffer[2] >> 2) & 15) < 12 ? (buffer[2] >> 2) & 15 : 0]; |
470 | 0 | if (!s->cfg.dontUpSampleImplicitSBR) |
471 | 0 | s->downsample = 1; |
472 | 0 | } else if (s->cfg.dontUpSampleImplicitSBR) { |
473 | | /* FAAD2 then reported the rate in the ADTS header, SBR or not. */ |
474 | 0 | *samplerate = sr_table[((buffer[2] >> 2) & 15) < 12 ? (buffer[2] >> 2) & 15 : 0]; |
475 | 0 | } |
476 | 0 | if (shim_reopen(s) != 0) |
477 | 0 | return -1; |
478 | | /* FAAD2 reported two channels for any ADTS channel configuration above 6, |
479 | | * whatever the stream carried; the frames report the real count. */ |
480 | 0 | if (cc == 7) |
481 | 0 | *channels = 2; |
482 | 0 | return 0; |
483 | 0 | } |
484 | | |
485 | | char NeAACDecInit2(NeAACDecHandle h, unsigned char *pBuffer, unsigned long size, |
486 | | unsigned long *samplerate, unsigned char *channels) |
487 | 0 | { |
488 | 0 | shim_t *s = h; |
489 | 0 | faad_stream_info info = { .struct_size = sizeof(info) }; |
490 | |
|
491 | 0 | if (!s || !pBuffer || size < 2 || !samplerate || !channels) |
492 | 0 | return -1; |
493 | | |
494 | 0 | free(s->asc); |
495 | 0 | s->asc = malloc(size); |
496 | 0 | if (!s->asc) |
497 | 0 | return -1; |
498 | 0 | memcpy(s->asc, pBuffer, size); |
499 | 0 | s->asc_len = (uint32_t)size; |
500 | 0 | s->stream = FAAD_STREAM_RAW; |
501 | 0 | s->frame = 0; |
502 | 0 | s->upsample = 0; |
503 | 0 | s->downsample = 0; |
504 | 0 | if (shim_reopen(s) != 0) |
505 | 0 | return -1; |
506 | 0 | if (faad_decoder_get_info(s->dec, &info) != FAAD_OK || !(info.flags & FAAD_STREAM_FORMAT_KNOWN)) |
507 | 0 | return -1; |
508 | 0 | *samplerate = info.sample_rate; |
509 | | /* An ASC without SBR signalling at or below 24 kHz is treated as implicit |
510 | | * SBR: FAAD2 reported and returned the doubled rate. */ |
511 | 0 | s->upsample = wants_upsample(s, &info); |
512 | | /* Without SBR signalling above 24 kHz, FAAD2 parsed downSampledSBR from the |
513 | | * ASC and returned the core rate whether or not dontUpSampleImplicitSBR was |
514 | | * set. Frames that turn out to carry SBR data are converted to float for the |
515 | | * decimation, so that streams without it keep their native output. */ |
516 | 0 | s->downsample = info.object_type == FAAD_OBJ_LC && info.sample_rate > 24000; |
517 | 0 | if (s->upsample && shim_reopen(s) != 0) |
518 | 0 | return -1; |
519 | | /* FAAD2 doubled the rate it reported even when dontUpSampleImplicitSBR kept |
520 | | * the frames at the core rate. */ |
521 | 0 | if (implicit_sbr(&info)) |
522 | 0 | *samplerate *= 2; |
523 | | /* FAAD2 reported the channels of the core, which is mono for PS streams; |
524 | | * the frames then carry two. */ |
525 | 0 | *channels = (unsigned char)(info.object_type == FAAD_OBJ_HE_AAC_V2 ? 1 : info.channels); |
526 | | /* FAAD2 2.11.3 upmixed every mono stream to stereo (implicit PS signalling). */ |
527 | 0 | if (*channels == 1) |
528 | 0 | *channels = 2; |
529 | | /* FAAD2 returned the channel configuration index, which is 7 for 7.1. */ |
530 | 0 | if (*channels == 8) |
531 | 0 | *channels = 7; |
532 | 0 | return 0; |
533 | 0 | } |
534 | | |
535 | | char NeAACDecInitDRM(NeAACDecHandle *h, unsigned long samplerate, unsigned char channels) |
536 | 0 | { |
537 | 0 | (void)h; |
538 | 0 | (void)samplerate; |
539 | 0 | (void)channels; |
540 | 0 | return 1; |
541 | 0 | } |
542 | | |
543 | | void NeAACDecPostSeekReset(NeAACDecHandle h, long frame) |
544 | 0 | { |
545 | 0 | shim_t *s = h; |
546 | 0 | if (!s) |
547 | 0 | return; |
548 | | /* A seek discards decoder history anyway, so this is where a changed |
549 | | * output format or downmix can take effect mid-stream. */ |
550 | 0 | if (s->dec) { |
551 | 0 | if (s->dirty && (s->cfg.outputFormat != s->fmt || s->cfg.downMatrix != s->downmix_on)) |
552 | 0 | shim_reopen(s); |
553 | 0 | else { |
554 | 0 | faad_decoder_flush(s->dec); |
555 | 0 | upsample_reset(s); |
556 | 0 | } |
557 | 0 | } |
558 | 0 | if (frame != -1) |
559 | 0 | s->frame = frame; |
560 | 0 | } |
561 | | |
562 | | void NeAACDecClose(NeAACDecHandle h) |
563 | 0 | { |
564 | 0 | shim_t *s = h; |
565 | 0 | if (!s) |
566 | 0 | return; |
567 | 0 | shim_release(s); |
568 | 0 | free(s->asc); |
569 | 0 | free(s->out); |
570 | 0 | free(s); |
571 | 0 | } |
572 | | |
573 | | char NeAACDecAudioSpecificConfig(unsigned char *pBuffer, unsigned long size, |
574 | | mp4AudioSpecificConfig *asc) |
575 | 1.16k | { |
576 | 1.16k | faad_decoder *d = NULL; |
577 | 1.16k | faad_stream_info info = { .struct_size = sizeof(info) }; |
578 | 1.16k | unsigned i; |
579 | | |
580 | 1.16k | if (!pBuffer || size < 2 || !asc) |
581 | 13 | return -1; |
582 | 1.14k | memset(asc, 0, sizeof(*asc)); |
583 | 1.14k | if (shim_open_dec(NULL, FAAD_STREAM_RAW, T_I16, pBuffer, (uint32_t)size, &d) != FAAD_OK) |
584 | 906 | return -1; |
585 | 241 | if (faad_decoder_get_info(d, &info) != FAAD_OK || !(info.flags & FAAD_STREAM_FORMAT_KNOWN)) { |
586 | 0 | faad_decoder_close(&d); |
587 | 0 | return -1; |
588 | 0 | } |
589 | 241 | faad_decoder_close(&d); |
590 | | |
591 | | /* FAAD2 reported the object type the ASC starts with when SBR is signalled |
592 | | * explicitly (AOT 5 or 29 is wrapped around an AAC-LC core, so 2), and 5 |
593 | | * when the SBR or PS extension follows an AOT 2 core. */ |
594 | 241 | { |
595 | 241 | unsigned first = pBuffer[0] >> 3; |
596 | 241 | int hev2 = info.object_type == FAAD_OBJ_HE_AAC_V2; |
597 | 241 | asc->objectTypeIndex = (unsigned char)((first == 5 || first == 29) ? LC : |
598 | 241 | hev2 ? HE_AAC : info.object_type); |
599 | 241 | asc->sbr_present_flag = info.object_type != FAAD_OBJ_LC ? 1 : -1; |
600 | 241 | asc->ps_present_flag = hev2 ? 1 : -1; |
601 | 241 | } |
602 | 241 | asc->samplingFrequency = info.sample_rate; |
603 | | /* The channel configuration index, which is 7 for 7.1. */ |
604 | 241 | asc->channelsConfiguration = (unsigned char)(info.object_type == FAAD_OBJ_HE_AAC_V2 ? 1 : |
605 | 241 | info.channels == 8 ? 7 : info.channels); |
606 | | /* FAAD2 2.11.3 upmixed mono to stereo in the ASC too (implicit PS). */ |
607 | 241 | if (asc->channelsConfiguration == 1) |
608 | 58 | asc->channelsConfiguration = 2; |
609 | 241 | asc->frameLengthFlag = (info.frame_samples % 960) == 0 && (info.frame_samples % 1024) != 0; |
610 | 241 | { |
611 | 241 | unsigned long core = asc->sbr_present_flag > 0 ? info.sample_rate / 2 : info.sample_rate; |
612 | 1.72k | for (i = 0; i < 12; i++) |
613 | 1.72k | if (sr_table[i] == core) |
614 | 241 | break; |
615 | 241 | asc->samplingFrequencyIndex = (unsigned char)(i < 12 ? i : 15); |
616 | 241 | } |
617 | | /* Without SBR signalling FAAD2 assumed implicit SBR: upsampled output at or |
618 | | * below 24 kHz, and SBR at the core rate above it. */ |
619 | 241 | if (asc->sbr_present_flag < 0) { |
620 | 96 | if (implicit_sbr(&info)) { |
621 | 51 | asc->samplingFrequency *= 2; |
622 | 51 | asc->forceUpSampling = 1; |
623 | 51 | } else if (info.sample_rate > 24000) { |
624 | 45 | asc->downSampledSBR = 1; |
625 | 45 | } |
626 | 96 | } |
627 | 241 | return 0; |
628 | 241 | } |
629 | | |
630 | | /* FAAD2 position code for each bit of a WAVE channel mask. */ |
631 | | /* wide: the mask has an inner front pair (FLC FRC), which is how libfaad3 reads |
632 | | * channelConfiguration 7. FAAD2 labels that stream's three pairs front, side and back |
633 | | * in stream order, so the inner pair is the front one, FL/FR the side one, and BL/BR the |
634 | | * back one. */ |
635 | | static unsigned mask_bit_code(unsigned bit, int wide) |
636 | 0 | { |
637 | 0 | switch (bit) { |
638 | 0 | case 0: return wide ? SIDE_CHANNEL_LEFT : FRONT_CHANNEL_LEFT; |
639 | 0 | case 1: return wide ? SIDE_CHANNEL_RIGHT : FRONT_CHANNEL_RIGHT; |
640 | 0 | case 6: return wide ? FRONT_CHANNEL_LEFT : UNKNOWN_CHANNEL; |
641 | 0 | case 7: return wide ? FRONT_CHANNEL_RIGHT : UNKNOWN_CHANNEL; |
642 | 0 | case 2: return FRONT_CHANNEL_CENTER; |
643 | 0 | case 3: return LFE_CHANNEL; |
644 | 0 | case 4: return BACK_CHANNEL_LEFT; |
645 | 0 | case 5: return BACK_CHANNEL_RIGHT; |
646 | 0 | case 8: return BACK_CHANNEL_CENTER; |
647 | 0 | case 9: return SIDE_CHANNEL_LEFT; |
648 | 0 | case 10: return SIDE_CHANNEL_RIGHT; |
649 | 0 | default: return UNKNOWN_CHANNEL; |
650 | 0 | } |
651 | 0 | } |
652 | | |
653 | | /* FAAD2 emits channels in bitstream order, which is ascending position code |
654 | | * (C L R, side, back, back centre, LFE). faad3 emits ascending WAVE mask bit. |
655 | | * perm[j] is the faad3 channel that becomes output channel j. Returns 1 when |
656 | | * the order is not the identity. */ |
657 | | static int build_perm(uint32_t mask, uint32_t n, uint8_t *perm, uint8_t *pos) |
658 | 0 | { |
659 | 0 | uint8_t code[SHIM_MAX_CHANNELS]; |
660 | 0 | uint32_t k = 0, i, j; |
661 | 0 | int moved = 0, wide = (mask & 0xC0u) == 0xC0u; |
662 | |
|
663 | 0 | for (i = 0; i < n; i++) |
664 | 0 | perm[i] = (uint8_t)i, pos[i] = UNKNOWN_CHANNEL; |
665 | 0 | if (!mask) |
666 | 0 | return 0; |
667 | 0 | for (i = 0; i < 32 && k < n; i++) |
668 | 0 | if (mask & (1u << i)) |
669 | 0 | code[k++] = (uint8_t)mask_bit_code(i, wide); |
670 | 0 | if (k != n) |
671 | 0 | return 0; |
672 | 0 | for (i = 0; i < n; i++) |
673 | 0 | if (code[i] == UNKNOWN_CHANNEL) |
674 | 0 | return 0; |
675 | 0 | for (i = 1; i < n; i++) { /* stable insertion sort by code */ |
676 | 0 | uint8_t c = code[i], p = perm[i]; |
677 | 0 | for (j = i; j > 0 && code[j - 1] > c; j--) { |
678 | 0 | code[j] = code[j - 1]; |
679 | 0 | perm[j] = perm[j - 1]; |
680 | 0 | } |
681 | 0 | code[j] = c; |
682 | 0 | perm[j] = p; |
683 | 0 | } |
684 | 0 | for (i = 0; i < n; i++) { |
685 | 0 | pos[i] = code[i]; |
686 | 0 | moved |= perm[i] != i; |
687 | 0 | } |
688 | 0 | return moved; |
689 | 0 | } |
690 | | |
691 | | static size_t out_sample_bytes(unsigned fmt) |
692 | 0 | { |
693 | 0 | switch (fmt) { |
694 | 0 | case FAAD_FMT_16BIT: return 2; |
695 | 0 | case FAAD_FMT_DOUBLE: return 8; |
696 | 0 | default: return 4; |
697 | 0 | } |
698 | 0 | } |
699 | | |
700 | | static void put_sample(unsigned fmt, uint8_t *dst, size_t idx, double v) |
701 | 0 | { |
702 | 0 | switch (fmt) { |
703 | 0 | case FAAD_FMT_16BIT: { |
704 | 0 | double x = v * 32768.0; |
705 | 0 | x = x >= 32767.0 ? 32767.0 : x <= -32768.0 ? -32768.0 : x; |
706 | 0 | ((int16_t *)dst)[idx] = (int16_t)lrint(x); |
707 | 0 | break; |
708 | 0 | } |
709 | 0 | case FAAD_FMT_24BIT: { |
710 | 0 | double x = v * 8388608.0; |
711 | 0 | x = x >= 8388607.0 ? 8388607.0 : x <= -8388608.0 ? -8388608.0 : x; |
712 | 0 | ((int32_t *)dst)[idx] = (int32_t)lrint(x); |
713 | 0 | break; |
714 | 0 | } |
715 | 0 | case FAAD_FMT_32BIT: { |
716 | 0 | double x = v * 2147483648.0; |
717 | 0 | x = x >= 2147483647.0 ? 2147483647.0 : x <= -2147483648.0 ? -2147483648.0 : x; |
718 | 0 | ((int32_t *)dst)[idx] = (int32_t)lrint(x); |
719 | 0 | break; |
720 | 0 | } |
721 | 0 | case FAAD_FMT_FLOAT: |
722 | 0 | ((float *)dst)[idx] = (float)v; |
723 | 0 | break; |
724 | 0 | default: |
725 | 0 | ((double *)dst)[idx] = v; |
726 | 0 | break; |
727 | 0 | } |
728 | 0 | } |
729 | | |
730 | | static int ensure_out(shim_t *s, size_t need) |
731 | 0 | { |
732 | 0 | if (s->out_cap < need) { |
733 | 0 | uint8_t *p = realloc(s->out, need); |
734 | 0 | if (!p) |
735 | 0 | return -1; |
736 | 0 | s->out = p; |
737 | 0 | s->out_cap = need; |
738 | 0 | } |
739 | 0 | return 0; |
740 | 0 | } |
741 | | |
742 | 0 | #define DM_MUL 0.3203772410170407 |
743 | 0 | #define RSQRT2 0.7071067811865475244 |
744 | | |
745 | | /* Map a faad3 error to the nearest FAAD2 error number. */ |
746 | | static unsigned char map_status(faad_status st) |
747 | 0 | { |
748 | 0 | switch (st) { |
749 | 0 | case FAAD_OK: return ERR_NONE; |
750 | 0 | case FAAD_ERR_SYNC_LOST: return ERR_NO_SYNC; |
751 | 0 | case FAAD_ERR_NEED_MORE_DATA: return ERR_INPUT_SMALL; |
752 | 0 | case FAAD_ERR_OUTPUT_TOO_SMALL: return ERR_OUTPUT_SMALL; |
753 | 0 | case FAAD_ERR_UNSUPPORTED: return ERR_NOT_ALLOWED; |
754 | 0 | case FAAD_ERR_DECODE_FAILED: return ERR_BITSTREAM; |
755 | 0 | default: return ERR_NOT_ALLOWED; |
756 | 0 | } |
757 | 0 | } |
758 | | |
759 | | /* FAAD2 normalised a stream that carries DRC data but no programme reference |
760 | | * level as if the reference were 0 dBFS, so those streams came out 20 dB quieter. |
761 | | * Callers may have worked around that, so it is reproduced. */ |
762 | | static void attenuate_missing_reference(void *pcm, pcm_type t, size_t count) |
763 | 0 | { |
764 | 0 | const float g = exp2f(DRC_TARGET_DB / 6.0f); |
765 | 0 | size_t i; |
766 | |
|
767 | 0 | for (i = 0; i < count; i++) { |
768 | 0 | if (t == T_I16) ((int16_t *)pcm)[i] = (int16_t)lrintf(((int16_t *)pcm)[i] * g); |
769 | 0 | else if (t == T_I32) ((int32_t *)pcm)[i] = (int32_t)lrintf(((int32_t *)pcm)[i] * g); |
770 | 0 | else ((float *)pcm)[i] *= g; |
771 | 0 | } |
772 | 0 | } |
773 | | |
774 | | static void *shim_decode(shim_t *s, NeAACDecFrameInfo *fi, const uint8_t *buf, unsigned long size, |
775 | | void *dest, unsigned long dest_size) |
776 | 0 | { |
777 | 0 | faad_stream_info info = { .struct_size = sizeof(info) }; |
778 | 0 | uint32_t used = 0, wr = 0, flags = 0, n, frames, i, c, out_ch, skipped = 0, rate; |
779 | 0 | int upsampled = 0, downsampled = 0; |
780 | 0 | pcm_type st_type; |
781 | 0 | uint8_t perm[SHIM_MAX_CHANNELS], pos[SHIM_MAX_CHANNELS]; |
782 | 0 | unsigned fmt = s->fmt; |
783 | 0 | size_t esz, osz, need; |
784 | 0 | int moved, downmix; |
785 | 0 | faad_status st; |
786 | 0 | uint8_t *src, *out, *src_pcm = NULL; |
787 | |
|
788 | 0 | if (!fi) |
789 | 0 | return NULL; |
790 | 0 | memset(fi, 0, sizeof(*fi)); |
791 | 0 | if (!s->dec || !buf || size == 0) { |
792 | 0 | fi->error = ERR_INPUT_SMALL; |
793 | 0 | return NULL; |
794 | 0 | } |
795 | 0 | if (s->dirty && !s->decoded && shim_reopen(s) != 0) { |
796 | 0 | fi->error = ERR_NOT_ALLOWED; |
797 | 0 | return NULL; |
798 | 0 | } |
799 | | |
800 | | /* FAAD2 searches up to 768 bytes for an ADTS syncword inside one call and |
801 | | * then decodes the frame behind the junk; callers rely on that. */ |
802 | 0 | for (;;) { |
803 | 0 | st = faad_decode_frame(s->dec, buf + skipped, (uint32_t)(size - skipped), &used, |
804 | 0 | s->pcm, s->pcm_cap, &wr, &flags); |
805 | 0 | if (st != FAAD_ERR_SYNC_LOST || used == 0 || skipped + used >= size || |
806 | 0 | skipped + used >= SYNC_SEARCH) |
807 | 0 | break; |
808 | 0 | skipped += used; |
809 | 0 | } |
810 | 0 | used += skipped; |
811 | 0 | fi->bytesconsumed = used; |
812 | 0 | if (st != FAAD_OK) { |
813 | 0 | fi->error = map_status(st); |
814 | | /* FAAD2 consumed nothing on an error and callers step through the |
815 | | * stream themselves; claiming a frame length could jump over real |
816 | | * frames when the header was a false sync. */ |
817 | 0 | fi->bytesconsumed = 0; |
818 | 0 | return NULL; |
819 | 0 | } |
820 | 0 | if (flags & FAAD_FRAME_CONCEALED) { |
821 | | /* FAAD2 reported corrupt core data as an error and returned no samples; |
822 | | * its callers and their resync logic expect that. The claimed length |
823 | | * of a corrupt ADTS frame cannot be trusted either. */ |
824 | 0 | fi->error = ERR_BITSTREAM; |
825 | 0 | fi->bytesconsumed = 0; |
826 | 0 | return NULL; |
827 | 0 | } |
828 | | /* FAAD2 configured the decoder once, from the first header. Frames written for |
829 | | * another sampling rate then failed to decode, with errors that depend on the |
830 | | * bits; 32 was the usual one. */ |
831 | 0 | if (s->stream == FAAD_STREAM_ADTS && s->adts_sfi >= 0 && ((buf[skipped + 2] >> 2) & 15) != s->adts_sfi) { |
832 | 0 | fi->error = ERR_NOT_ALLOWED; |
833 | 0 | fi->bytesconsumed = 0; |
834 | 0 | return NULL; |
835 | 0 | } |
836 | 0 | s->decoded = 1; |
837 | 0 | s->frame++; |
838 | 0 | fi->header_type = s->stream == FAAD_STREAM_ADTS ? ADTS : RAW; |
839 | 0 | if (wr == 0) |
840 | 0 | return NULL; |
841 | 0 | if (faad_decoder_get_info(s->dec, &info) != FAAD_OK || info.channels == 0 || |
842 | 0 | info.channels > SHIM_MAX_CHANNELS) { |
843 | 0 | fi->error = ERR_BAD_CHANNELS; |
844 | 0 | return NULL; |
845 | 0 | } |
846 | | |
847 | 0 | n = info.channels; |
848 | 0 | st_type = s->type; |
849 | 0 | esz = s->type == T_I16 ? 2 : 4; |
850 | 0 | frames = wr / (uint32_t)(esz * n); |
851 | 0 | if ((flags & FAAD_FRAME_DRC_INFO) && !(info.flags & FAAD_STREAM_DRC_REF_LEVEL)) |
852 | 0 | attenuate_missing_reference(s->pcm, s->type, (size_t)frames * n); |
853 | 0 | rate = info.sample_rate; |
854 | 0 | if (s->upsample && !(flags & FAAD_FRAME_SBR) && s->type == T_F32 && rate <= 24000) { |
855 | 0 | const float *up = resample_frames(s, (const float *)s->pcm, frames, n, rate, rate * 2, |
856 | 0 | UP_LEAD); |
857 | 0 | if (!up) { |
858 | 0 | fi->error = ERR_NOT_ALLOWED; |
859 | 0 | return NULL; |
860 | 0 | } |
861 | 0 | src_pcm = (uint8_t *)up; |
862 | 0 | frames *= 2; |
863 | 0 | rate *= 2; |
864 | 0 | upsampled = 1; |
865 | 0 | } else if (s->downsample && (flags & FAAD_FRAME_SBR) && rate > 24000) { |
866 | 0 | const float *in_f = (const float *)s->pcm, *down; |
867 | 0 | size_t total = (size_t)frames * n, k_; |
868 | 0 | if (s->type != T_F32) { |
869 | 0 | const float scale = s->type == T_I16 ? 1.0f / 32768.0f : 1.0f / 8388608.0f; |
870 | 0 | if (grow(&s->cv, &s->cv_cap, total)) { |
871 | 0 | fi->error = ERR_NOT_ALLOWED; |
872 | 0 | return NULL; |
873 | 0 | } |
874 | 0 | for (k_ = 0; k_ < total; k_++) |
875 | 0 | s->cv[k_] = s->type == T_I16 ? (float)((int16_t *)s->pcm)[k_] * scale |
876 | 0 | : (float)((int32_t *)s->pcm)[k_] * scale; |
877 | 0 | in_f = s->cv; |
878 | 0 | } |
879 | 0 | down = resample_frames(s, in_f, frames, n, rate, rate / 2, DOWN_LEAD); |
880 | 0 | if (!down) { |
881 | 0 | fi->error = ERR_NOT_ALLOWED; |
882 | 0 | return NULL; |
883 | 0 | } |
884 | 0 | src_pcm = (uint8_t *)down; |
885 | 0 | frames /= 2; |
886 | 0 | rate /= 2; |
887 | 0 | downsampled = 1; |
888 | 0 | st_type = T_F32; |
889 | 0 | } |
890 | 0 | downmix = s->downmix_on && (n == 5 || n == 6); |
891 | 0 | out_ch = n == 1 ? 2 : (downmix ? 2 : n); |
892 | 0 | moved = build_perm(info.channel_mask, n, perm, pos); |
893 | 0 | osz = out_sample_bytes(fmt); |
894 | 0 | need = (size_t)frames * out_ch * osz; |
895 | |
|
896 | 0 | src = (upsampled || downsampled) ? src_pcm : s->pcm; |
897 | 0 | if (!downmix && !moved && n != 1 && |
898 | 0 | ((st_type == T_I16 && fmt == FAAD_FMT_16BIT) || |
899 | 0 | (st_type == T_I32 && fmt == FAAD_FMT_24BIT) || |
900 | 0 | (st_type == T_F32 && fmt == FAAD_FMT_FLOAT))) { |
901 | 0 | out = src; |
902 | 0 | } else { |
903 | 0 | if (dest) { |
904 | 0 | if (dest_size < need) { |
905 | 0 | fi->error = ERR_OUTPUT_SMALL; |
906 | 0 | return NULL; |
907 | 0 | } |
908 | 0 | out = dest; |
909 | 0 | } else { |
910 | 0 | if (ensure_out(s, need) != 0) { |
911 | 0 | fi->error = ERR_NOT_ALLOWED; |
912 | 0 | return NULL; |
913 | 0 | } |
914 | 0 | out = s->out; |
915 | 0 | } |
916 | 0 | if (downmix) { |
917 | 0 | const float *f = (const float *)src; |
918 | 0 | for (i = 0; i < frames; i++) { |
919 | 0 | const float *x = f + (size_t)i * n; |
920 | 0 | double cen = x[perm[0]], l = x[perm[1]], r = x[perm[2]]; |
921 | 0 | double bl = x[perm[3]], br = x[perm[4]]; |
922 | 0 | put_sample(fmt, out, (size_t)i * 2, DM_MUL * (l + cen * RSQRT2 + bl * RSQRT2)); |
923 | 0 | put_sample(fmt, out, (size_t)i * 2 + 1, DM_MUL * (r + cen * RSQRT2 + br * RSQRT2)); |
924 | 0 | } |
925 | 0 | } else if (n == 1) { |
926 | 0 | if (st_type == T_F32) { |
927 | 0 | const float *f = (const float *)src; |
928 | 0 | for (i = 0; i < frames; i++) { |
929 | 0 | put_sample(fmt, out, (size_t)i * 2, f[i]); |
930 | 0 | put_sample(fmt, out, (size_t)i * 2 + 1, f[i]); |
931 | 0 | } |
932 | 0 | } else if (st_type == T_I16) { |
933 | 0 | const int16_t *p = (const int16_t *)src; |
934 | 0 | for (i = 0; i < frames; i++) { |
935 | 0 | ((int16_t *)out)[(size_t)i * 2] = p[i]; |
936 | 0 | ((int16_t *)out)[(size_t)i * 2 + 1] = p[i]; |
937 | 0 | } |
938 | 0 | } else { |
939 | 0 | const int32_t *p = (const int32_t *)src; |
940 | 0 | for (i = 0; i < frames; i++) { |
941 | 0 | ((int32_t *)out)[(size_t)i * 2] = p[i]; |
942 | 0 | ((int32_t *)out)[(size_t)i * 2 + 1] = p[i]; |
943 | 0 | } |
944 | 0 | } |
945 | 0 | } else if (st_type == T_F32) { |
946 | 0 | const float *f = (const float *)src; |
947 | 0 | for (i = 0; i < frames; i++) |
948 | 0 | for (c = 0; c < n; c++) |
949 | 0 | put_sample(fmt, out, (size_t)i * n + c, f[(size_t)i * n + perm[c]]); |
950 | 0 | } else if (st_type == T_I16) { |
951 | 0 | const int16_t *p = (const int16_t *)src; |
952 | 0 | for (i = 0; i < frames; i++) |
953 | 0 | for (c = 0; c < n; c++) |
954 | 0 | ((int16_t *)out)[(size_t)i * n + c] = p[(size_t)i * n + perm[c]]; |
955 | 0 | } else { |
956 | 0 | const int32_t *p = (const int32_t *)src; |
957 | 0 | for (i = 0; i < frames; i++) |
958 | 0 | for (c = 0; c < n; c++) |
959 | 0 | ((int32_t *)out)[(size_t)i * n + c] = p[(size_t)i * n + perm[c]]; |
960 | 0 | } |
961 | 0 | } |
962 | 0 | if (out == src && dest) { |
963 | 0 | if (dest_size < need) { |
964 | 0 | fi->error = ERR_OUTPUT_SMALL; |
965 | 0 | return NULL; |
966 | 0 | } |
967 | 0 | memcpy(dest, src, need); |
968 | 0 | out = dest; |
969 | 0 | } |
970 | | |
971 | 0 | fi->channels = (unsigned char)out_ch; |
972 | 0 | fi->samples = (unsigned long)frames * out_ch; |
973 | 0 | fi->samplerate = rate; |
974 | 0 | fi->object_type = info.object_type == FAAD_OBJ_LC ? LC : HE_AAC; |
975 | 0 | fi->sbr = downsampled ? SBR_DOWNSAMPLED : |
976 | 0 | upsampled ? NO_SBR_UPSAMPLED : (flags & FAAD_FRAME_SBR) ? SBR_UPSAMPLED : NO_SBR; |
977 | 0 | fi->ps = (flags & FAAD_FRAME_PS) ? 1 : 0; |
978 | 0 | if (downmix || n == 1) { |
979 | 0 | fi->num_front_channels = 2; |
980 | 0 | fi->channel_position[0] = FRONT_CHANNEL_LEFT; |
981 | 0 | fi->channel_position[1] = FRONT_CHANNEL_RIGHT; |
982 | 0 | } else { |
983 | 0 | for (c = 0; c < n; c++) { |
984 | 0 | fi->channel_position[c] = pos[c]; |
985 | 0 | switch (pos[c]) { |
986 | 0 | case FRONT_CHANNEL_CENTER: |
987 | 0 | case FRONT_CHANNEL_LEFT: |
988 | 0 | case FRONT_CHANNEL_RIGHT: fi->num_front_channels++; break; |
989 | 0 | case SIDE_CHANNEL_LEFT: |
990 | 0 | case SIDE_CHANNEL_RIGHT: fi->num_side_channels++; break; |
991 | 0 | case BACK_CHANNEL_LEFT: |
992 | 0 | case BACK_CHANNEL_RIGHT: |
993 | 0 | case BACK_CHANNEL_CENTER: fi->num_back_channels++; break; |
994 | 0 | case LFE_CHANNEL: fi->num_lfe_channels++; break; |
995 | 0 | default: break; |
996 | 0 | } |
997 | 0 | } |
998 | 0 | } |
999 | | /* FAAD2 decodes the first frame but hands back no samples for it. */ |
1000 | 0 | if (s->frame <= 1) |
1001 | 0 | fi->samples = 0; |
1002 | 0 | return out; |
1003 | 0 | } |
1004 | | |
1005 | | void *NeAACDecDecode(NeAACDecHandle h, NeAACDecFrameInfo *hInfo, unsigned char *buffer, |
1006 | | unsigned long buffer_size) |
1007 | 0 | { |
1008 | 0 | return shim_decode(h, hInfo, buffer, buffer_size, NULL, 0); |
1009 | 0 | } |
1010 | | |
1011 | | void *NeAACDecDecode2(NeAACDecHandle h, NeAACDecFrameInfo *hInfo, unsigned char *buffer, |
1012 | | unsigned long buffer_size, void **sample_buffer, |
1013 | | unsigned long sample_buffer_size) |
1014 | 0 | { |
1015 | 0 | if (!sample_buffer || !*sample_buffer || sample_buffer_size == 0) { |
1016 | 0 | if (hInfo) { |
1017 | 0 | memset(hInfo, 0, sizeof(*hInfo)); |
1018 | 0 | hInfo->error = ERR_OUTPUT_SMALL; |
1019 | 0 | } |
1020 | 0 | return NULL; |
1021 | 0 | } |
1022 | 0 | return shim_decode(h, hInfo, buffer, buffer_size, *sample_buffer, sample_buffer_size); |
1023 | 0 | } |