blob: 6bd6279ecdff59b5fa5a8ca98c4fe6a2350b76d3 [file] [log] [blame]
xingri.gaoc18d4472023-02-28 02:51:02 +00001/*
2** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3** Copyright (C) 2003-2005 M. Bakker, Nero 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** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24**
25** Commercial non-GPL licensing of this software is possible.
26** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27**
28** $Id: decoder.c,v 1.117 2009/02/05 00:51:03 menno Exp $
29**/
30
31#include "common.h"
32#include "structs.h"
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <fcntl.h>
38#include <android/log.h>
39
40#include "mp4.h"
41#include "syntax.h"
42#include "error.h"
43#include "output.h"
44#include "filtbank.h"
45#include "drc.h"
46#ifdef SBR_DEC
47#include "sbr_dec.h"
48#include "sbr_syntax.h"
49#endif
50#ifdef SSR_DEC
51#include "ssr.h"
52#include "ssr_fb.h"
53#endif
54
55#ifdef USE_HELIX_AAC_DECODER
56#include "aaccommon.h"
57#include "aacdec.h"
58int AACDataSource = 1;
59static HAACDecoder hAACDecoder;
60static int adts_sample_rates[] = {96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, 0, 0, 0};
61
62static int FindAdtsSRIndex(int sr)
63{
64 int i;
65
66 for (i = 0; i < 16; i++) {
67 if (sr == adts_sample_rates[i]) {
68 return i;
69 }
70 }
71 return 16 - 1;
72}
73static void MakeAdtsHeader(NeAACDecFrameInfo *hInfo, unsigned char *adts_header, int channel)
74{
75 unsigned char *data;
76 int old_format = 0;
77 int profile = hInfo->object_type;
78 if (profile == 5) { //sbr
79 profile = 2;
80 }
81 profile = (profile - 1) & 0x3;
82 int sr_index = ((hInfo->sbr == SBR_UPSAMPLED) || (hInfo->sbr == NO_SBR_UPSAMPLED)) ?
83 FindAdtsSRIndex(hInfo->samplerate / 2) : FindAdtsSRIndex(hInfo->samplerate);
84 int skip = (old_format) ? 8 : 7;
85 int framesize = skip + hInfo->bytesconsumed;
86
87 if (hInfo->header_type == ADTS) {
88 framesize -= skip;
89 }
90
91 // audio_codec_print("MakeAdtsHeader profile %d ,ch %d,sr_index %d\n",profile,channel,sr_index);
92
93 data = adts_header;
94 memset(data, 0, 7 * sizeof(unsigned char));
95
96 data[0] += 0xFF; /* 8b: syncword */
97
98 data[1] += 0xF0; /* 4b: syncword */
99 /* 1b: mpeg id = 0 */
100 /* 2b: layer = 0 */
101 data[1] += 1; /* 1b: protection absent */
102
103 data[2] += ((profile << 6) & 0xC0); /* 2b: profile */
104 data[2] += ((sr_index << 2) & 0x3C); /* 4b: sampling_frequency_index */
105 /* 1b: private = 0 */
106 data[2] += ((channel >> 2) & 0x1); /* 1b: channel_configuration */
107
108 data[3] += ((channel << 6) & 0xC0); /* 2b: channel_configuration */
109 /* 1b: original */
110 /* 1b: home */
111 /* 1b: copyright_id */
112 /* 1b: copyright_id_start */
113 data[3] += ((framesize >> 11) & 0x3); /* 2b: aac_frame_length */
114
115 data[4] += ((framesize >> 3) & 0xFF); /* 8b: aac_frame_length */
116
117 data[5] += ((framesize << 5) & 0xE0); /* 3b: aac_frame_length */
118 data[5] += ((0x7FF >> 6) & 0x1F); /* 5b: adts_buffer_fullness */
119
120 data[6] += ((0x7FF << 2) & 0x3F); /* 6b: adts_buffer_fullness */
121 /* 2b: num_raw_data_blocks */
122
123}
124#endif
125
126#ifdef ANALYSIS
127uint16_t dbg_count;
128#endif
129
130#define faad_log_info audio_codec_print
131
132
133#ifdef NEW_CODE_CHECK_LATM
134#define LATM_LOG audio_codec_print
135static const int pi_sample_rates[16] = {
136 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
137 16000, 12000, 11025, 8000, 7350, 0, 0, 0
138};
139#if 0
140static int LOASSyncInfo(uint8_t p_header[LOAS_HEADER_SIZE], unsigned int *pi_header_size)
141{
142 *pi_header_size = 3;
143 return ((p_header[1] & 0x1f) << 8) + p_header[2];
144}
145#endif
146static int Mpeg4GAProgramConfigElement(bitfile *ld,mpeg4_cfg_t *p_cfg)
147{
148 /* TODO compute channels count ? */
149 //int i_tag = faad_getbits(ld, 4);
150 //if (i_tag != 0x05) {
151 // return -1;
152 //}
153 faad_getbits(ld, 2 + 4); // object type + sampling index
154 int i_num_front = faad_getbits(ld, 4);
155 int i_num_side = faad_getbits(ld, 4);
156 int i_num_back = faad_getbits(ld, 4);
157 int i_num_lfe = faad_getbits(ld, 2);
158 int i_num_assoc_data = faad_getbits(ld, 3);
159 int i_num_valid_cc = faad_getbits(ld, 4);
160 int i,tmp;
161 if (faad_getbits(ld, 1)) {
162 faad_getbits(ld, 4); // mono downmix
163 }
164 if (faad_getbits(ld, 1)) {
165 faad_getbits(ld, 4); // stereo downmix
166 }
167 if (faad_getbits(ld, 1)) {
168 faad_getbits(ld, 2 + 1); // matrix downmix + pseudo_surround
169 }
170
171 for (i = 0; i < i_num_front; i++) {
172 tmp = faad_get1bit(ld DEBUGVAR(1, 26, "program_config_element(): front_element_is_cpe"));
173 faad_getbits(ld, 4 DEBUGVAR(1, 27, "program_config_element(): front_element_tag_select"));
174
175 if (tmp & 1) {
176 p_cfg->i_channel += 2;
177 } else {
178 p_cfg->i_channel++;
179 }
180 }
181
182 for (i = 0; i < i_num_side; i++) {
183 tmp = faad_get1bit(ld DEBUGVAR(1, 28, "program_config_element(): side_element_is_cpe"));
184 faad_getbits(ld, 4 DEBUGVAR(1, 29, "program_config_element(): side_element_tag_select"));
185
186 if (tmp & 1) {
187 p_cfg->i_channel += 2;
188 } else {
189 p_cfg->i_channel++;
190 }
191 }
192
193 for (i = 0; i < i_num_back; i++) {
194 tmp = faad_get1bit(ld DEBUGVAR(1, 30, "program_config_element(): back_element_is_cpe"));
195 faad_getbits(ld, 4 DEBUGVAR(1, 31, "program_config_element(): back_element_tag_select"));
196
197 if (tmp & 1) {
198 p_cfg->i_channel += 2;
199 } else {
200 p_cfg->i_channel++;
201 }
202 }
203
204 for (i = 0; i < i_num_lfe; i++) {
205 tmp = (uint8_t)faad_getbits(ld, 4 DEBUGVAR(1, 32, "program_config_element(): lfe_element_tag_select"));
206 p_cfg->i_channel++;
207 }
208
209 for (i = 0; i < i_num_assoc_data; i++)
210 faad_getbits(ld, 4 DEBUGVAR(1, 33, "program_config_element(): assoc_data_element_tag_select"));
211
212 for (i = 0; i < i_num_valid_cc; i++) {
213 faad_get1bit(ld DEBUGVAR(1, 34, "program_config_element(): cc_element_is_ind_sw"));
214 faad_getbits(ld, 4 DEBUGVAR(1, 35, "program_config_element(): valid_cc_element_tag_select"));
215 }
216 faad_byte_align(ld);
217 int i_comment = faad_getbits(ld, 8);
218 faad_getbits(ld, i_comment * 8);
219 return 0;
220}
221
222static int Mpeg4GASpecificConfig(mpeg4_cfg_t *p_cfg, bitfile *ld)
223{
224 p_cfg->i_frame_length = faad_getbits(ld, 1) ? 960 : 1024;
225
226 if (faad_getbits(ld, 1)) { // depend on core coder
227 faad_getbits(ld, 14); // core coder delay
228 }
229
230 int i_extension_flag = faad_getbits(ld, 1);
231 if (p_cfg->i_channel == 0) {
232 Mpeg4GAProgramConfigElement(ld,p_cfg);
233 }
234 if (p_cfg->i_object_type == 6 || p_cfg->i_object_type == 20) {
235 faad_getbits(ld, 3); // layer
236 }
237
238 if (i_extension_flag) {
239 if (p_cfg->i_object_type == 22) {
240 faad_getbits(ld, 5 + 11); // numOfSubFrame + layer length
241 }
242 if (p_cfg->i_object_type == 17 || p_cfg->i_object_type == 19 ||
243 p_cfg->i_object_type == 20 || p_cfg->i_object_type == 23) {
244 faad_getbits(ld, 1 + 1 + 1); // ER data : section scale spectral */
245 }
246 if (faad_getbits(ld, 1)) { // extension 3
247 LATM_LOG("Mpeg4GASpecificConfig: error 1\n");
248 }
249 }
250 return 0;
251}
252
253static int Mpeg4ReadAudioObjectType(bitfile *ld)
254{
255 int i_type = faad_getbits(ld, 5);
256 if (i_type == 31) {
257 i_type = 32 + faad_getbits(ld, 6);
258 }
259 return i_type;
260}
261
262static int Mpeg4ReadAudioSamplerate(bitfile *ld)
263{
264 int i_index = faad_getbits(ld, 4);
265 if (i_index != 0x0f) {
266 return pi_sample_rates[i_index];
267 }
268 return faad_getbits(ld, 24);
269}
270
271static int Mpeg4ReadAudioSpecificInfo(mpeg4_cfg_t *p_cfg, int *pi_extra, uint8_t *p_extra, bitfile *ld, int i_max_size)
272{
273#if 0
274 static const char *ppsz_otype[] = {
275 "NULL",
276 "AAC Main", "AAC LC", "AAC SSR", "AAC LTP", "SBR", "AAC Scalable",
277 "TwinVQ",
278 "CELP", "HVXC",
279 "Reserved", "Reserved",
280 "TTSI",
281 "Main Synthetic", "Wavetables Synthesis", "General MIDI",
282 "Algorithmic Synthesis and Audio FX",
283 "ER AAC LC",
284 "Reserved",
285 "ER AAC LTP", "ER AAC Scalable", "ER TwinVQ", "ER BSAC", "ER AAC LD",
286 "ER CELP", "ER HVXC", "ER HILN", "ER Parametric",
287 "SSC",
288 "PS", "Reserved", "Escape",
289 "Layer 1", "Layer 2", "Layer 3",
290 "DST",
291 };
292#endif
293 const int i_pos_start = faad_get_processed_bits(ld);
294 bitfile s_sav = *ld;
295 int i_bits;
296 int i;
297
298 memset(p_cfg, 0, sizeof(*p_cfg));
299 *pi_extra = 0;
300
301 p_cfg->i_object_type = Mpeg4ReadAudioObjectType(ld);
302 p_cfg->i_samplerate = Mpeg4ReadAudioSamplerate(ld);
303
304 p_cfg->i_channel = faad_getbits(ld, 4);
305 if (p_cfg->i_channel == 7) {
306 p_cfg->i_channel = 8; // 7.1
307 } else if (p_cfg->i_channel >= 8) {
308 p_cfg->i_channel = -1;
309 }
310
311 p_cfg->i_sbr = -1;
312 p_cfg->i_ps = -1;
313 p_cfg->extension.i_object_type = 0;
314 p_cfg->extension.i_samplerate = 0;
315 if (p_cfg->i_object_type == 5 || (p_cfg->i_object_type == 29/* && (faad_showbits(ld, 3) & 0x03 && !(faad_showbits(ld, 9) & 0x3F))*/)) {
316 p_cfg->i_sbr = 1;
317 if (p_cfg->i_object_type == 29) {
318 p_cfg->i_ps = 1;
319 }
320 p_cfg->extension.i_object_type = 5;
321 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate(ld);
322
323 p_cfg->i_object_type = Mpeg4ReadAudioObjectType(ld);
324 }
325
326 switch (p_cfg->i_object_type) {
327 case 1:
328 case 2:
329 case 3:
330 case 4:
331 case 6:
332 case 7:
333 case 17:
334 case 19:
335 case 20:
336 case 21:
337 case 22:
338 case 23:
339 Mpeg4GASpecificConfig(p_cfg, ld);
340 break;
341 case 8:
342 // CelpSpecificConfig();
343 break;
344 case 9:
345 // HvxcSpecificConfig();
346 break;
347 case 12:
348 // TTSSSpecificConfig();
349 break;
350 case 13:
351 case 14:
352 case 15:
353 case 16:
354 // StructuredAudioSpecificConfig();
355 break;
356 case 24:
357 // ERCelpSpecificConfig();
358 break;
359 case 25:
360 // ERHvxcSpecificConfig();
361 break;
362 case 26:
363 case 27:
364 // ParametricSpecificConfig();
365 break;
366 case 28:
367 // SSCSpecificConfig();
368 break;
369 case 32:
370 case 33:
371 case 34:
372 // MPEG_1_2_SpecificConfig();
373 break;
374 case 35:
375 // DSTSpecificConfig();
376 break;
377 case 36:
378 // ALSSpecificConfig();
379 break;
380 default:
381 // error
382 break;
383 }
384 switch (p_cfg->i_object_type) {
385 case 17:
386 case 19:
387 case 20:
388 case 21:
389 case 22:
390 case 23:
391 case 24:
392 case 25:
393 case 26:
394 case 27: {
395 int epConfig = faad_getbits(ld, 2);
396 if (epConfig == 2 || epConfig == 3)
397 //ErrorProtectionSpecificConfig();
398 if (epConfig == 3)
399 if (faad_getbits(ld, 1)) {
400 // TODO : directMapping
401 }
402 break;
403 }
404 default:
405 break;
406 }
407
408 if (p_cfg->extension.i_object_type != 5 && i_max_size > 0 && i_max_size - (faad_get_processed_bits(ld) - i_pos_start) >= 16 &&
409 faad_getbits(ld, 11) == 0x2b7) {
410 p_cfg->extension.i_object_type = Mpeg4ReadAudioObjectType(ld);
411 if (p_cfg->extension.i_object_type == 5) {
412 p_cfg->i_sbr = faad_getbits(ld, 1);
413 if (p_cfg->i_sbr == 1) {
414 p_cfg->extension.i_samplerate = Mpeg4ReadAudioSamplerate(ld);
415 if (i_max_size > 0 && i_max_size - (faad_get_processed_bits(ld) - i_pos_start) >= 12 && faad_getbits(ld, 11) == 0x548) {
416 p_cfg->i_ps = faad_getbits(ld, 1);
417 }
418 }
419 }
420 }
421
422 //fprintf(stderr, "Mpeg4ReadAudioSpecificInfo: t=%s(%d)f=%d c=%d sbr=%d\n",
423 // ppsz_otype[p_cfg->i_object_type], p_cfg->i_object_type, p_cfg->i_samplerate, p_cfg->i_channel, p_cfg->i_sbr);
424
425 i_bits = faad_get_processed_bits(ld) - i_pos_start;
426
427 *pi_extra = min((i_bits + 7) / 8, LATM_MAX_EXTRA_SIZE);
428 for (i = 0; i < *pi_extra; i++) {
429 const int i_read = min(8, i_bits - 8 * i);
430 p_extra[i] = faad_getbits(&s_sav, i_read) << (8 - i_read);
431 }
432 return i_bits;
433}
434
435static int LatmGetValue(bitfile *ld)
436{
437 int i_bytes = faad_getbits(ld, 2);
438 int v = 0;
439 int i;
440 for (i = 0; i < i_bytes; i++) {
441 v = (v << 8) + faad_getbits(ld, 8);
442 }
443
444 return v;
445}
446
447static int LatmReadStreamMuxConfiguration(latm_mux_t *m, bitfile *ld)
448{
449 int i_mux_version;
450 int i_mux_versionA;
451
452 i_mux_version = faad_getbits(ld, 1);
453 i_mux_versionA = 0;
454 if (i_mux_version) {
455 i_mux_versionA = faad_getbits(ld, 1);
456 }
457
458 if (i_mux_versionA != 0) { /* support only A=0 */
459 return -1;
460 }
461
462 memset(m, 0, sizeof(*m));
463
464 if (i_mux_versionA == 0)
465 if (i_mux_version == 1) {
466 LatmGetValue(ld); /* taraBufferFullness */
467 }
468
469 m->b_same_time_framing = faad_getbits(ld, 1);
470 m->i_sub_frames = 1 + faad_getbits(ld, 6);
471 m->i_programs = 1 + faad_getbits(ld, 4);
472 if (m->i_programs > 1) {
473 return -1;
474 }
475 int i_program;
476 for (i_program = 0; i_program < m->i_programs; i_program++) {
477 m->pi_layers[i_program] = 1 + faad_getbits(ld, 3);
478 if (m->pi_layers[0] > 1) {
479 return -1;
480 }
481 int i_layer;
482 for (i_layer = 0; i_layer < m->pi_layers[i_program]; i_layer++) {
483 latm_stream_t *st = &m->stream[m->i_streams];
484 unsigned char b_previous_cfg;
485
486 m->pi_stream[i_program][i_layer] = m->i_streams;
487 st->i_program = i_program;
488 st->i_layer = i_layer;
489
490 b_previous_cfg = 0;
491 if (i_program != 0 || i_layer != 0) {
492 b_previous_cfg = faad_getbits(ld, 1);
493 }
494
495 if (b_previous_cfg) {
496 if (m->i_streams <= 0) {
497 LATM_LOG("assert failed \n");
498 while (1) {
499 ;
500 }
501 }
502 st->cfg = m->stream[m->i_streams - 1].cfg;
503 } else {
504 int i_cfg_size = 0;
505 if (i_mux_version == 1) {
506 i_cfg_size = LatmGetValue(ld);
507 }
508 i_cfg_size -= Mpeg4ReadAudioSpecificInfo(&st->cfg, &st->i_extra, st->extra, ld, i_cfg_size);
509 if (i_cfg_size > 0) {
510 faad_flushbits(ld, i_cfg_size);
511 }
512 }
513
514 st->i_frame_length_type = faad_getbits(ld, 3);
515 switch (st->i_frame_length_type) {
516 case 0: {
517 faad_flushbits(ld, 8); /* latmBufferFullnes */
518 if (!m->b_same_time_framing)
519 if (st->cfg.i_object_type == 6 || st->cfg.i_object_type == 20 ||
520 st->cfg.i_object_type == 8 || st->cfg.i_object_type == 24) {
521 faad_flushbits(ld, 6); /* eFrameOffset */
522 }
523 break;
524 }
525 case 1:
526 st->i_frame_length = faad_getbits(ld, 9);
527 break;
528 case 3:
529 case 4:
530 case 5:
531 st->i_frame_length_index = faad_getbits(ld, 6); // celp
532 break;
533 case 6:
534 case 7:
535 st->i_frame_length_index = faad_getbits(ld, 1); // hvxc
536 default:
537 break;
538 }
539 /* Next stream */
540 m->i_streams++;
541 }
542 }
543
544 /* other data */
545 if (faad_getbits(ld, 1)) {
546 if (i_mux_version == 1) {
547 m->i_other_data = LatmGetValue(ld);
548 } else {
549 int b_continue;
550 do {
551 b_continue = faad_getbits(ld, 1);
552 m->i_other_data = (m->i_other_data << 8) + faad_getbits(ld, 8);
553 } while (b_continue);
554 }
555 }
556
557 /* crc */
558 m->i_crc = -1;
559 if (faad_getbits(ld, 1)) {
560 m->i_crc = faad_getbits(ld, 8);
561 }
562
563 return 0;
564}
565static int LOASParse(uint8_t *p_buffer, int i_buffer, decoder_sys_t *p_sys)
566{
567 bitfile ld = {0};
568 int i_accumulated = 0;
569 const latm_stream_t *st;
570 faad_initbits(&ld, p_buffer, i_buffer);
571 int ret = 0;
572 //bs_init(&s, p_buffer, i_buffer);
573
574 /* Read the stream mux configuration if present */
575 if (!faad_getbits(&ld, 1) && !(ret = LatmReadStreamMuxConfiguration(&p_sys->latm, &ld)) &&
576 p_sys->latm.i_streams > 0) {
577 st = &p_sys->latm.stream[0];
578
579 p_sys->i_channels = st->cfg.i_channel;
580 p_sys->i_rate = st->cfg.i_samplerate;
581 p_sys->i_frame_length = st->cfg.i_frame_length;
582 // LATM_LOG("ch %d, rate %d,frame len %d \n",p_sys->i_channels,p_sys->i_rate,p_sys->i_frame_length);
583 /* FIXME And if it changes ? */
584 if (p_sys->i_channels && p_sys->i_rate && p_sys->i_frame_length > 0) {
585#if 0
586 if (!p_dec->fmt_out.i_extra && st->i_extra > 0) {
587 p_dec->fmt_out.i_extra = st->i_extra;
588 p_dec->fmt_out.p_extra = malloc(st->i_extra);
589 if (!p_dec->fmt_out.p_extra) {
590 p_dec->fmt_out.i_extra = 0;
591 return 0;
592 }
593 memcpy(p_dec->fmt_out.p_extra, st->extra, st->i_extra);
594 }
595#endif
596 p_sys->b_latm_cfg = 1;
597 }
598 }
599
600 /* Wait for the configuration */
601 if (!p_sys->b_latm_cfg || ret < 0) {
602 return 0;
603 }
604
605 /* FIXME do we need to split the subframe into independent packet ? */
606 if (p_sys->latm.i_sub_frames > 1) {
607 printf("latm sub frames not yet supported, please send a sample");
608 }
609 int i_sub;
610 for (i_sub = 0; i_sub < p_sys->latm.i_sub_frames; i_sub++) {
611 int pi_payload[LATM_MAX_PROGRAM][LATM_MAX_LAYER];
612 if (p_sys->latm.b_same_time_framing) {
613 /* Payload length */
614 int i_program, i_layer;
615 for (i_program = 0; i_program < p_sys->latm.i_programs; i_program++) {
616 for (i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++) {
617 latm_stream_t *st = &p_sys->latm.stream[p_sys->latm.pi_stream[i_program][i_layer]];
618 if (st->i_frame_length_type == 0) {
619 int i_payload = 0;
620 for (;;) {
621 int i_tmp = faad_getbits(&ld, 8);
622 i_payload += i_tmp;
623 if (i_tmp != 255) {
624 break;
625 }
626 }
627 pi_payload[i_program][i_layer] = i_payload;
628 } else if (st->i_frame_length_type == 1) {
629 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
630 } else if ((st->i_frame_length_type == 3) ||
631 (st->i_frame_length_type == 5) ||
632 (st->i_frame_length_type == 7)) {
633 faad_getbits(&ld, 2); // muxSlotLengthCoded
634 pi_payload[i_program][i_layer] = 0; /* TODO */
635 } else {
636 pi_payload[i_program][i_layer] = 0; /* TODO */
637 }
638 }
639 }
640
641 /* Payload Data */
642 // int i_program,i_layer;
643 for (i_program = 0; i_program < p_sys->latm.i_programs; i_program++) {
644 for (i_layer = 0; i_layer < p_sys->latm.pi_layers[i_program]; i_layer++) {
645 /* XXX we only extract 1 stream */
646 if (i_program != 0 || i_layer != 0) {
647 break;
648 }
649
650 if (pi_payload[i_program][i_layer] <= 0) {
651 continue;
652 }
653
654 /* FIXME that's slow (and a bit ugly to write in place) */
655 int i;
656 for (i = 0; i < pi_payload[i_program][i_layer]; i++) {
657 if (i_accumulated >= i_buffer) {
658 return 0;
659 }
660 p_buffer[i_accumulated++] = faad_getbits(&ld, 8);
661 }
662 }
663 }
664 } else {
665 const int i_chunks = faad_getbits(&ld, 4);
666 int pi_program[16];
667 int pi_layer[16];
668
669 // printf( "latm without same time frameing not yet supported, please send a sample");
670 int i_chunk;
671 for (i_chunk = 0; i_chunk < i_chunks; i_chunk++) {
672 const int streamIndex = faad_getbits(&ld, 4);
673 latm_stream_t *st = &p_sys->latm.stream[streamIndex];
674 const int i_program = st->i_program;
675 const int i_layer = st->i_layer;
676
677 pi_program[i_chunk] = i_program;
678 pi_layer[i_chunk] = i_layer;
679
680 if (st->i_frame_length_type == 0) {
681 int i_payload = 0;
682 for (;;) {
683 int i_tmp = faad_getbits(&ld, 8);
684 i_payload += i_tmp;
685 if (i_tmp != 255) {
686 break;
687 }
688 }
689 pi_payload[i_program][i_layer] = i_payload;
690 faad_getbits(&ld, 1); // auEndFlag
691 } else if (st->i_frame_length_type == 1) {
692 pi_payload[i_program][i_layer] = st->i_frame_length / 8; /* XXX not correct */
693 } else if ((st->i_frame_length_type == 3) ||
694 (st->i_frame_length_type == 5) ||
695 (st->i_frame_length_type == 7)) {
696 faad_getbits(&ld, 2); // muxSlotLengthCoded
697 }
698 }
699 // int i_chunk;
700 for (i_chunk = 0; i_chunk < i_chunks; i_chunk++) {
701 //const int i_program = pi_program[i_chunk];
702 //const int i_layer = pi_layer[i_chunk];
703
704 /* TODO ? Payload */
705 }
706 }
707 }
708
709#if 0
710 if (p_sys->latm.i_other_data > 0) {
711 ; // TODO
712 }
713#endif
714 faad_byte_align(&ld);
715
716 return i_accumulated;
717}
718
719
720#endif
721
722/* static function declarations */
723static void* aac_frame_decode(NeAACDecStruct *hDecoder,
724 NeAACDecFrameInfo *hInfo,
725 unsigned char *buffer,
726 unsigned long buffer_size,
727 void **sample_buffer2,
728 unsigned long sample_buffer_size);
729static void create_channel_config(NeAACDecStruct *hDecoder,
730 NeAACDecFrameInfo *hInfo);
731
732
733char* NEAACDECAPI NeAACDecGetErrorMessage(unsigned char errcode)
734{
735 if (errcode >= NUM_ERROR_MESSAGES) {
736 return NULL;
737 }
738 return err_msg[errcode];
739}
740
741unsigned long NEAACDECAPI NeAACDecGetCapabilities(void)
742{
743 uint32_t cap = 0;
744
745 /* can't do without it */
746 cap += LC_DEC_CAP;
747
748#ifdef MAIN_DEC
749 cap += MAIN_DEC_CAP;
750#endif
751#ifdef LTP_DEC
752 cap += LTP_DEC_CAP;
753#endif
754#ifdef LD_DEC
755 cap += LD_DEC_CAP;
756#endif
757#ifdef ERROR_RESILIENCE
758 cap += ERROR_RESILIENCE_CAP;
759#endif
760#ifdef FIXED_POINT
761 cap += FIXED_POINT_CAP;
762#endif
763
764 return cap;
765}
766
767const unsigned char mes[] = { 0x67, 0x20, 0x61, 0x20, 0x20, 0x20, 0x6f, 0x20, 0x72, 0x20, 0x65, 0x20, 0x6e, 0x20, 0x20, 0x20, 0x74, 0x20, 0x68, 0x20, 0x67, 0x20, 0x69, 0x20, 0x72, 0x20, 0x79, 0x20, 0x70, 0x20, 0x6f, 0x20, 0x63 };
768NeAACDecHandle NEAACDECAPI NeAACDecOpen(void)
769{
770 uint8_t i;
771 NeAACDecStruct *hDecoder = NULL;
772
773 if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL) {
774 return NULL;
775 }
776
777 memset(hDecoder, 0, sizeof(NeAACDecStruct));
778
779 hDecoder->cmes = mes;
780 hDecoder->config.outputFormat = FAAD_FMT_16BIT;
781 hDecoder->config.defObjectType = MAIN;
782 hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
783 hDecoder->config.downMatrix = 0x01;
784 hDecoder->adts_header_present = 0;
785 hDecoder->adif_header_present = 0;
786 hDecoder->latm_header_present = 0;
787#ifdef ERROR_RESILIENCE
788 hDecoder->aacSectionDataResilienceFlag = 0;
789 hDecoder->aacScalefactorDataResilienceFlag = 0;
790 hDecoder->aacSpectralDataResilienceFlag = 0;
791#endif
792 hDecoder->frameLength = 1024;
793
794 hDecoder->frame = 0;
795 hDecoder->sample_buffer = NULL;
796
797 hDecoder->__r1 = 1;
798 hDecoder->__r2 = 1;
799
800 for (i = 0; i < MAX_CHANNELS; i++) {
801 hDecoder->window_shape_prev[i] = 0;
802 hDecoder->time_out[i] = NULL;
803 hDecoder->fb_intermed[i] = NULL;
804#ifdef SSR_DEC
805 hDecoder->ssr_overlap[i] = NULL;
806 hDecoder->prev_fmd[i] = NULL;
807#endif
808#ifdef MAIN_DEC
809 hDecoder->pred_stat[i] = NULL;
810#endif
811#ifdef LTP_DEC
812 hDecoder->ltp_lag[i] = 0;
813 hDecoder->lt_pred_stat[i] = NULL;
814#endif
815 }
816
817#ifdef SBR_DEC
818 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) {
819 hDecoder->sbr[i] = NULL;
820 }
821#endif
822
823 hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
824 hDecoder->last_ch_configure = -1;
825 hDecoder->last_sf_index = -1;
826 return hDecoder;
827}
828
829NeAACDecConfigurationPtr NEAACDECAPI NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
830{
831 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
832 if (hDecoder) {
833 NeAACDecConfigurationPtr config = &(hDecoder->config);
834
835 return config;
836 }
837
838 return NULL;
839}
840
841unsigned char NEAACDECAPI NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
842 NeAACDecConfigurationPtr config)
843{
844 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
845 if (hDecoder && config) {
846 /* check if we can decode this object type */
847 if (can_decode_ot(config->defObjectType) < 0) {
848 return 0;
849 }
850 hDecoder->config.defObjectType = config->defObjectType;
851
852 /* samplerate: anything but 0 should be possible */
853 if (config->defSampleRate == 0) {
854 return 0;
855 }
856 hDecoder->config.defSampleRate = config->defSampleRate;
857
858 /* check output format */
859#ifdef FIXED_POINT
860 if ((config->outputFormat < 1) || (config->outputFormat > 4)) {
861 return 0;
862 }
863#else
864 if ((config->outputFormat < 1) || (config->outputFormat > 5)) {
865 return 0;
866 }
867#endif
868 hDecoder->config.outputFormat = config->outputFormat;
869
870 if (config->downMatrix > 1) {
871 return 0;
872 }
873 hDecoder->config.downMatrix = config->downMatrix;
874
875 /* OK */
876 return 1;
877 }
878
879 return 0;
880}
881
882#if 0
883static int latmCheck(latm_header *latm, bitfile *ld)
884{
885 uint32_t good = 0, bad = 0, bits, m;
886
887 while (ld->bytes_left) {
888 bits = faad_latm_frame(latm, ld);
889 if (bits == -1U) {
890 bad++;
891 } else {
892 good++;
893 while (bits > 0) {
894 m = min(bits, 8);
895 faad_getbits(ld, m DEBUGVAR(print, var, dbg));
896 bits -= m;
897 }
898 }
899 }
900
901 return (good > 0);
902}
903#endif
904#define SKIP_LATM_BYTE 16*4*2
905#if 0
906static int latm_check_internal(unsigned char *buffer, unsigned buffer_size, unsigned *byte_cost)
907{
908 latm_header l = {0};
909 int is_latm = 0;
910 bitfile ld;
911 int byte_consumed = 0;
912 int byte_left = buffer_size;
913retry:
914 memset(&l, 0, sizeof(latm_header));
915 faad_initbits(&ld, buffer + byte_consumed, buffer_size - byte_consumed);
916 is_latm = latmCheck(&l, &ld);
917 if (is_latm && l.ASCbits > 0) {
918 is_latm = 1;
919 } else {
920 is_latm = 0;
921 byte_consumed += SKIP_LATM_BYTE;
922 byte_left -= SKIP_LATM_BYTE;
923
924 }
925 if (is_latm == 0 && byte_left > 400) {
926 goto retry;
927 }
928//exit:
929 *byte_cost = byte_consumed;
930 return is_latm;
931}
932#endif
933long NEAACDECAPI NeAACDecInit(NeAACDecHandle hpDecoder,
934 unsigned char *buffer,
935 unsigned long buffer_size,
936 unsigned long *samplerate,
937 unsigned char *channels,
938 int is_latm_external,
939 int *skipbytes)
940{
941 uint32_t bits = 0;
942 bitfile ld;
943 adif_header adif;
944 adts_header adts;
945 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
946 unsigned char* temp_bufer = hDecoder->temp_bufer;
947 int temp_size = 0;
948 faad_log_info("enter NeAACDecInit \r\n");
949#ifdef NEW_CODE_CHECK_LATM
950 int i_frame_size;
951 if (buffer_size > TMP_BUF_SIZE) {
952 LATM_LOG("init input buffer size too big %lu, buffer size %d \n", buffer_size,TMP_BUF_SIZE);
953 //buffer_size = sizeof(temp_bufer);
954 buffer_size = TMP_BUF_SIZE ;
955 }
956 if (buffer_size > 0) {
957 memcpy(temp_bufer, buffer, buffer_size);
958 temp_size = buffer_size;
959 buffer = temp_bufer;
960 }
961 unsigned char *pbuffer = buffer;
962 int pbuffer_size = buffer_size;
963 decoder_sys_t *p_sys = &hDecoder->dec_sys;
964 latm_mux_t *m = &p_sys->latm;
965 latm_stream_t *st = NULL;
966#endif
967
968 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL)) {
969 return -1;
970 }
971 //memset(latm_payload,0,sizeof(latm_payload));
972 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
973 hDecoder->object_type = hDecoder->config.defObjectType;
974 *samplerate = get_sample_rate(hDecoder->sf_index);
975 *channels = 1;
976 *samplerate = 0;
977 *channels = 0;
978 //int latm_audio = 0;
979 //unsigned byte_cost = 0;
980 if (buffer != NULL) {
981 latm_header *l = &hDecoder->latm_config;
982 faad_initbits(&ld, buffer, buffer_size);
983#ifdef NEW_CODE_CHECK_LATM
984 memset(&hDecoder->dec_sys, 0, sizeof(decoder_sys_t));
985NEXT_CHECK:
986 while (pbuffer_size >= 2) {
987 if (pbuffer[0] == 0x56 && (pbuffer[1] & 0xe0) == 0xe0) { //LOAS sync word detected
988 // LATM_LOG("find LOAS sync word pos %d\n",buffer_size-pbuffer_size);
989 break;
990 }
991 pbuffer++;
992 pbuffer_size--;
993 }
994 if (pbuffer_size < LOAS_HEADER_SIZE) {
995 LATM_LOG("check the loas frame failed\n");
996 *skipbytes = buffer_size-pbuffer_size;
997 goto exit_check;
998 }
999 /* Check if frame is valid and get frame info */
1000 i_frame_size = ((pbuffer[1] & 0x1f) << 8) + pbuffer[2];
1001 if (i_frame_size <= 0 || i_frame_size > 6 * 768) {
1002 LATM_LOG("i_frame_size/%d error\n",i_frame_size);
1003 pbuffer++;
1004 pbuffer_size--;
1005 goto NEXT_CHECK;
1006 }
1007 if (pbuffer_size < (LOAS_HEADER_SIZE + i_frame_size)) {
xingri.gao619aa1a2023-12-01 03:25:38 +00001008 if (0 == is_latm_external)
1009 {
1010 goto adts_check;
1011 }
xingri.gaoc18d4472023-02-28 02:51:02 +00001012 LATM_LOG("[%s %d]buffer size %d small then frame size %d,\n", __FUNCTION__,__LINE__,pbuffer_size, i_frame_size+LOAS_HEADER_SIZE);
1013 *skipbytes = buffer_size-pbuffer_size;
1014 return -1;
1015 }
xingri.gao619aa1a2023-12-01 03:25:38 +00001016#if 0
xingri.gaoc18d4472023-02-28 02:51:02 +00001017 if (pbuffer[LOAS_HEADER_SIZE + i_frame_size] != 0x56 || (pbuffer[LOAS_HEADER_SIZE + i_frame_size + 1] & 0xe0) != 0xe0) { // next frame LOAS sync header detected
1018 LATM_LOG("emulated sync word no (sync on following frame) \n");
1019 pbuffer++;
1020 pbuffer_size--;
1021 goto NEXT_CHECK;
1022 }
1023#endif
1024 pbuffer += LOAS_HEADER_SIZE; //skip header
1025 pbuffer_size = pbuffer_size - LOAS_HEADER_SIZE;
1026 //parse the playload of one real LOAS aac frame
1027 i_frame_size = LOASParse(pbuffer, i_frame_size, p_sys);
1028 if (i_frame_size <= 0) {
1029 LATM_LOG("[%s %d]invalid i_frame_size/%d ,go on next check!...\n", __FUNCTION__, __LINE__, i_frame_size);
1030 goto NEXT_CHECK;
1031 } else {
1032 LATM_LOG("latm detected\n");
1033 hDecoder->latm_header_present = 1;
1034 }
1035 //assue latm detected. start init code
1036exit_check:
1037 if (m->i_streams > 0) {
1038 st = &m->stream[m->i_streams - 1];
1039 }
1040 memset(l, 0, sizeof(latm_header));
1041 if ((st && st->i_extra) || is_latm_external) {
1042 int32_t x;
1043
1044 hDecoder->latm_header_present = 1;
1045 if (st && st->i_extra) {
1046 x = NeAACDecInit2(hDecoder, st->extra, st->i_extra, samplerate, channels);
1047 } else {
1048 x = -1;
1049 }
1050 if (x != 0) {
1051 hDecoder->latm_header_present = 0;
1052 }
1053#ifdef USE_HELIX_AAC_DECODER
1054 else {
1055 hAACDecoder = AACInitDecoder();
1056 if (!hAACDecoder) {
1057 faad_log_info("fatal error,helix aac decoder init failed\n");
1058 return -1;
1059 } else {
1060 AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
1061 if (aacDecInfo) {
1062 aacDecInfo->format = AAC_FF_ADTS;
1063 aacDecInfo->nChans = *channels;
1064 } else {
1065 LATM_LOG("aacDecInfo NULL\n");
1066 return NULL;
1067 }
1068 }
1069 }
1070#endif
1071 LATM_LOG("latm init ret %d \n", x);
1072 return x;
1073 } else
1074#else
1075 int is_latm;
1076 memset(l, 0, sizeof(latm_header));
1077 is_latm = latmCheck(l, &ld);
1078 l->inited = 0;
1079 l->frameLength = 0;
1080 faad_rewindbits(&ld);
1081 if (is_latm && l->ASCbits > 0) {
1082 int32_t x;
1083 hDecoder->latm_header_present = 1;
1084 x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits + 7) / 8, samplerate, channels);
1085 if (x != 0) {
1086 hDecoder->latm_header_present = 0;
1087 }
1088 return x;
1089 } else
1090#endif
xingri.gao619aa1a2023-12-01 03:25:38 +00001091
1092adts_check:
xingri.gaoc18d4472023-02-28 02:51:02 +00001093 /* Check if an ADIF header is present */
1094 if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
1095 (buffer[2] == 'I') && (buffer[3] == 'F')) {
1096 hDecoder->adif_header_present = 1;
1097 faad_log_info("[%s %d]ADIF aac file detected\n", __FUNCTION__, __LINE__);
1098 get_adif_header(&adif, &ld);
1099 faad_byte_align(&ld);
1100
1101 hDecoder->sf_index = adif.pce[0].sf_index;
1102 hDecoder->object_type = adif.pce[0].object_type + 1;
1103
1104 *samplerate = get_sample_rate(hDecoder->sf_index);
1105 *channels = adif.pce[0].channels;
1106
1107 memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
1108 hDecoder->pce_set = 1;
1109
1110 bits = bit2byte(faad_get_processed_bits(&ld));
1111
1112 /* Check if an ADTS header is present */
1113 } else if (faad_showbits(&ld, 12) == 0xfff) {
1114 hDecoder->adts_header_present = 1;
1115
1116 adts.old_format = hDecoder->config.useOldADTSFormat;
1117 adts_frame(&adts, &ld);
1118
1119 hDecoder->sf_index = adts.sf_index;
1120 hDecoder->object_type = adts.profile + 1;
1121 if (adts.sf_index >= 0 && adts.sf_index < 12 && adts.channel_configuration > 0 && adts.channel_configuration <= 8) {
1122 hDecoder->last_sf_index = hDecoder->sf_index;
1123 hDecoder->last_ch_configure = adts.channel_configuration;
1124 }
1125 *samplerate = get_sample_rate(hDecoder->sf_index);
1126 *channels = (adts.channel_configuration > 6) ?
1127 2 : adts.channel_configuration;
1128 } else {
1129 /*we guess it is a ADTS aac files and try to resync from the error*/
1130 int ii;
1131 int adts_err = 0;
1132 faad_log_info("[%s %d]guess it is a ADTS aac files and try to resync\n", __FUNCTION__, __LINE__);
1133 faad_initbits(&ld, buffer, buffer_size);
1134 for (ii = 0; ii < (int)buffer_size; ii++) {
1135 if ((faad_showbits(&ld, 16) & 0xfff6) != 0xFFF0) {
1136 faad_getbits(&ld, 8
1137 DEBUGVAR(0, 0, ""));
1138 } else {
1139 bits = bit2byte(faad_get_processed_bits(&ld));
1140 hDecoder->adts_header_present = 1;
1141 faad_log_info("[%s %d]resync and got ADTS header\n", __FUNCTION__, __LINE__);
1142 adts.old_format = hDecoder->config.useOldADTSFormat;
1143 adts_err = adts_frame(&adts, &ld);
1144 if (adts_err == 5) {
1145 return -1;
1146 }
1147 hDecoder->sf_index = adts.sf_index;
1148 hDecoder->object_type = adts.profile + 1;
1149 faad_log_info("sf index %d,object type %d \n", hDecoder->sf_index, hDecoder->object_type);
1150 if (adts.sf_index >= 0 && adts.sf_index < 12 && adts.channel_configuration > 0 && adts.channel_configuration <= 8) {
1151 hDecoder->last_sf_index = hDecoder->sf_index;
1152 hDecoder->last_ch_configure = adts.channel_configuration;
1153 }
1154 *samplerate = get_sample_rate(hDecoder->sf_index);
1155 if (*samplerate > 96000 || adts.channel_configuration > 6 || hDecoder->sf_index >= 12) {
1156 return -1;
1157 }
1158 *channels = (adts.channel_configuration > 6) ? 2 : adts.channel_configuration;
1159 faad_log_info("[%s %d]resync adts info:FS/%lu object_type/%d chnum/%d\n", __FUNCTION__, __LINE__, *samplerate, hDecoder->object_type, *channels);
1160 break;
1161 }
1162 }
1163 if (ii == (int)buffer_size) {
1164 faad_log_info("[%s %d]sync for adts frame failed\n", __FUNCTION__, __LINE__);
1165 return -1;
1166 }
1167 }
1168 }
1169 if (ld.error) {
1170 faad_endbits(&ld);
1171 return -1;
1172 }
1173 faad_endbits(&ld);
1174
1175#if (defined(PS_DEC) || defined(DRM_PS))
1176 /* check if we have a mono file */
1177 if (*channels == 1) {
1178 /* upMatrix to 2 channels for implicit signalling of PS */
1179 *channels = 2;
1180 }
1181#endif
1182
1183 hDecoder->channelConfiguration = *channels;
1184
1185#ifdef SBR_DEC
1186 /* implicit signalling */
1187 if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
1188 *samplerate *= 2;
1189 hDecoder->forceUpSampling = 1;
1190 } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
1191 hDecoder->downSampledSBR = 1;
1192 }
1193#endif
1194
1195 /* must be done before frameLength is divided by 2 for LD */
1196#ifdef SSR_DEC
1197 if (hDecoder->object_type == SSR) {
1198 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength / SSR_BANDS);
1199 } else
1200#endif
1201 hDecoder->fb = filter_bank_init(hDecoder->frameLength);
1202
1203#ifdef LD_DEC
1204 if (hDecoder->object_type == LD) {
1205 hDecoder->frameLength >>= 1;
1206 }
1207#endif
1208
1209 if (can_decode_ot(hDecoder->object_type) < 0) {
1210 faad_log_info("[%s %d]object_type/%d can not support\n", __FUNCTION__, __LINE__, hDecoder->object_type);
1211 return -1;
1212 }
1213 faad_log_info("[%s %d]aac init finished. cost bits%d\n", __FUNCTION__, __LINE__, bits);
1214 return bits;
1215}
1216
1217/* Init the library using a DecoderSpecificInfo */
1218int NEAACDECAPI NeAACDecInit2(NeAACDecHandle hpDecoder,
1219 unsigned char *pBuffer,
1220 unsigned long SizeOfDecoderSpecificInfo,
1221 unsigned long *samplerate,
1222 unsigned char *channels)
1223{
1224 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
1225 int8_t rc;
1226 mp4AudioSpecificConfig mp4ASC;
1227 faad_log_info("enter NeAACDecInit2 \r\n");
1228 faad_log_info("extra data size %lu\r\n", SizeOfDecoderSpecificInfo);
1229 if ((hDecoder == NULL)
1230 || (pBuffer == NULL)
1231 || (SizeOfDecoderSpecificInfo < 2)
1232 || (samplerate == NULL)
1233 || (channels == NULL)) {
1234 return -1;
1235 }
1236
1237 hDecoder->adif_header_present = 0;
1238 hDecoder->adts_header_present = 0;
1239
1240 /* decode the audio specific config */
1241 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
1242 &(hDecoder->pce), hDecoder->latm_header_present);
1243
1244 /* copy the relevant info to the decoder handle */
1245 *samplerate = mp4ASC.samplingFrequency;
1246 if (mp4ASC.channelsConfiguration) {
1247 *channels = mp4ASC.channelsConfiguration;
1248 } else {
1249 *channels = hDecoder->pce.channels;
1250 hDecoder->pce_set = 1;
1251 }
1252#if (defined(PS_DEC) || defined(DRM_PS))
1253 /* check if we have a mono file */
1254 if (*channels == 1) {
1255 /* upMatrix to 2 channels for implicit signalling of PS */
1256 *channels = 2;
1257 }
1258#endif
1259 hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
1260 hDecoder->object_type = mp4ASC.objectTypeIndex;
1261#ifdef ERROR_RESILIENCE
1262 hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
1263 hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
1264 hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
1265#endif
1266#ifdef SBR_DEC
1267 hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
1268 hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
1269 if (hDecoder->config.dontUpSampleImplicitSBR == 0) {
1270 hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
1271 } else {
1272 hDecoder->forceUpSampling = 0;
1273 }
1274
1275 /* AAC core decoder samplerate is 2 times as low */
1276 if (((hDecoder->sbr_present_flag == 1) && (!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1) {
1277 hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
1278 }
1279#endif
1280
1281 if (rc != 0) {
1282 return rc;
1283 }
1284 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
1285 if (mp4ASC.frameLengthFlag)
1286#ifdef ALLOW_SMALL_FRAMELENGTH
1287 hDecoder->frameLength = 960;
1288#else
1289 return -1;
1290#endif
1291
1292 /* must be done before frameLength is divided by 2 for LD */
1293#ifdef SSR_DEC
1294 if (hDecoder->object_type == SSR) {
1295 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength / SSR_BANDS);
1296 } else
1297#endif
1298 hDecoder->fb = filter_bank_init(hDecoder->frameLength);
1299
1300#ifdef LD_DEC
1301 if (hDecoder->object_type == LD) {
1302 hDecoder->frameLength >>= 1;
1303 }
1304#endif
1305 faad_log_info("aac init2 finished\r\n");
1306 return 0;
1307}
1308
1309#ifdef DRM
1310char NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
1311 unsigned long samplerate,
1312 unsigned char channels)
1313{
1314 NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
1315 if (hDecoder == NULL) {
1316 return 1; /* error */
1317 }
1318
1319 NeAACDecClose(*hDecoder);
1320
1321 *hDecoder = NeAACDecOpen();
1322
1323 /* Special object type defined for DRM */
1324 (*hDecoder)->config.defObjectType = DRM_ER_LC;
1325
1326 (*hDecoder)->config.defSampleRate = samplerate;
1327#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
1328 (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
1329 (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
1330 (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
1331#endif
1332 (*hDecoder)->frameLength = 960;
1333 (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
1334 (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
1335
1336 if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO)) {
1337 (*hDecoder)->channelConfiguration = 2;
1338 } else {
1339 (*hDecoder)->channelConfiguration = 1;
1340 }
1341
1342#ifdef SBR_DEC
1343 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO)) {
1344 (*hDecoder)->sbr_present_flag = 0;
1345 } else {
1346 (*hDecoder)->sbr_present_flag = 1;
1347 }
1348#endif
1349
1350 (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
1351
1352 return 0;
1353}
1354#endif
1355
1356void NEAACDECAPI NeAACDecClose(NeAACDecHandle hpDecoder)
1357{
1358 uint8_t i;
1359 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
1360#ifdef USE_HELIX_AAC_DECODER
1361 if (hAACDecoder) {
1362 AACFreeDecoder(hAACDecoder);
1363 hAACDecoder = NULL;
1364 }
1365#endif
1366 if (hDecoder == NULL) {
1367 return;
1368 }
1369
1370#ifdef PROFILE
1371 //printk("AAC decoder total: %I64d cycles\n", hDecoder->cycles);
1372 //printk("requant: %I64d cycles\n", hDecoder->requant_cycles);
1373 //printk("spectral_data: %I64d cycles\n", hDecoder->spectral_cycles);
1374 //printk("scalefactors: %I64d cycles\n", hDecoder->scalefac_cycles);
1375 //printk("output: %I64d cycles\n", hDecoder->output_cycles);
1376#endif
1377
1378 for (i = 0; i < MAX_CHANNELS; i++) {
1379 if (hDecoder->time_out[i]) {
1380 faad_free(hDecoder->time_out[i]);
1381 }
1382 if (hDecoder->fb_intermed[i]) {
1383 faad_free(hDecoder->fb_intermed[i]);
1384 }
1385#ifdef SSR_DEC
1386 if (hDecoder->ssr_overlap[i]) {
1387 faad_free(hDecoder->ssr_overlap[i]);
1388 }
1389 if (hDecoder->prev_fmd[i]) {
1390 faad_free(hDecoder->prev_fmd[i]);
1391 }
1392#endif
1393#ifdef MAIN_DEC
1394 if (hDecoder->pred_stat[i]) {
1395 faad_free(hDecoder->pred_stat[i]);
1396 }
1397#endif
1398#ifdef LTP_DEC
1399 if (hDecoder->lt_pred_stat[i]) {
1400 faad_free(hDecoder->lt_pred_stat[i]);
1401 }
1402#endif
1403 }
1404
1405#ifdef SSR_DEC
1406 if (hDecoder->object_type == SSR) {
1407 ssr_filter_bank_end(hDecoder->fb);
1408 } else
1409#endif
1410 filter_bank_end(hDecoder->fb);
1411
1412 drc_end(hDecoder->drc);
1413
1414 if (hDecoder->sample_buffer) {
1415 faad_free(hDecoder->sample_buffer);
1416 }
1417
1418
1419 if (hDecoder->sample_buffer_all) {
1420 faad_free(hDecoder->sample_buffer_all);
1421 }
1422
1423#ifdef SBR_DEC
1424 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) {
1425 if (hDecoder->sbr[i]) {
1426 sbrDecodeEnd(hDecoder->sbr[i]);
1427 }
1428 }
1429#endif
1430 //why not free before?
1431 if (hDecoder) {
1432 faad_free(hDecoder);
1433 }
1434}
1435
1436void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
1437{
1438 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
1439 if (hDecoder) {
1440 hDecoder->postSeekResetFlag = 1;
1441
1442 if (frame != -1) {
1443 hDecoder->frame = frame;
1444 }
1445 }
1446}
1447
1448static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
1449{
1450 hInfo->num_front_channels = 0;
1451 hInfo->num_side_channels = 0;
1452 hInfo->num_back_channels = 0;
1453 hInfo->num_lfe_channels = 0;
1454 memset(hInfo->channel_position, 0, MAX_CHANNELS * sizeof(uint8_t));
1455
1456 if (hDecoder->downMatrix) {
1457 hInfo->num_front_channels = 2;
1458 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
1459 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
1460 return;
1461 }
1462
1463 /* check if there is a PCE */
1464 if (hDecoder->pce_set) {
1465 uint8_t i, chpos = 0;
1466 uint8_t chdir, back_center = 0;
1467
1468 hInfo->num_front_channels = hDecoder->pce.num_front_channels;
1469 hInfo->num_side_channels = hDecoder->pce.num_side_channels;
1470 hInfo->num_back_channels = hDecoder->pce.num_back_channels;
1471 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
1472
1473 chdir = hInfo->num_front_channels;
1474 if (chdir & 1) {
1475#if (defined(PS_DEC) || defined(DRM_PS))
1476 /* When PS is enabled output is always stereo */
1477 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
1478 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
1479#else
1480 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
1481 chdir--;
1482#endif
1483 }
1484 for (i = 0; i < chdir; i += 2) {
1485 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
1486 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
1487 }
1488
1489 for (i = 0; i < hInfo->num_side_channels; i += 2) {
1490 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
1491 hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
1492 }
1493
1494 chdir = hInfo->num_back_channels;
1495 if (chdir & 1) {
1496 back_center = 1;
1497 chdir--;
1498 }
1499 for (i = 0; i < chdir; i += 2) {
1500 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
1501 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
1502 }
1503 if (back_center) {
1504 hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
1505 }
1506
1507 for (i = 0; i < hInfo->num_lfe_channels; i++) {
1508 hInfo->channel_position[chpos++] = LFE_CHANNEL;
1509 }
1510
1511 } else {
1512 switch (hDecoder->channelConfiguration) {
1513 case 1:
1514#if (defined(PS_DEC) || defined(DRM_PS))
1515 /* When PS is enabled output is always stereo */
1516 hInfo->num_front_channels = 2;
1517 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
1518 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
1519#else
1520 hInfo->num_front_channels = 1;
1521 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1522#endif
1523 break;
1524 case 2:
1525 hInfo->num_front_channels = 2;
1526 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
1527 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
1528 break;
1529 case 3:
1530 hInfo->num_front_channels = 3;
1531 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1532 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
1533 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
1534 break;
1535 case 4:
1536 hInfo->num_front_channels = 3;
1537 hInfo->num_back_channels = 1;
1538 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1539 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
1540 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
1541 hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
1542 break;
1543 case 5:
1544 hInfo->num_front_channels = 3;
1545 hInfo->num_back_channels = 2;
1546 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1547 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
1548 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
1549 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
1550 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
1551 break;
1552 case 6:
1553 hInfo->num_front_channels = 3;
1554 hInfo->num_back_channels = 2;
1555 hInfo->num_lfe_channels = 1;
1556 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1557 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
1558 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
1559 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
1560 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
1561 hInfo->channel_position[5] = LFE_CHANNEL;
1562 break;
1563 case 7:
1564 hInfo->num_front_channels = 3;
1565 hInfo->num_side_channels = 2;
1566 hInfo->num_back_channels = 2;
1567 hInfo->num_lfe_channels = 1;
1568 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1569 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
1570 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
1571 hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
1572 hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
1573 hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
1574 hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
1575 hInfo->channel_position[7] = LFE_CHANNEL;
1576 break;
1577 default: { /* channelConfiguration == 0 || channelConfiguration > 7 */
1578 uint8_t i;
1579 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
1580 if (ch & 1) { /* there's either a center front or a center back channel */
1581 uint8_t ch1 = (ch - 1) / 2;
1582 if (hDecoder->first_syn_ele == ID_SCE) {
1583 hInfo->num_front_channels = ch1 + 1;
1584 hInfo->num_back_channels = ch1;
1585 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1586 for (i = 1; i <= ch1; i += 2) {
1587 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
1588 hInfo->channel_position[i + 1] = FRONT_CHANNEL_RIGHT;
1589 }
1590 for (i = ch1 + 1; i < ch; i += 2) {
1591 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
1592 hInfo->channel_position[i + 1] = BACK_CHANNEL_RIGHT;
1593 }
1594 } else {
1595 hInfo->num_front_channels = ch1;
1596 hInfo->num_back_channels = ch1 + 1;
1597 for (i = 0; i < ch1; i += 2) {
1598 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
1599 hInfo->channel_position[i + 1] = FRONT_CHANNEL_RIGHT;
1600 }
1601 for (i = ch1; i < ch - 1; i += 2) {
1602 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
1603 hInfo->channel_position[i + 1] = BACK_CHANNEL_RIGHT;
1604 }
1605 hInfo->channel_position[ch - 1] = BACK_CHANNEL_CENTER;
1606 }
1607 } else {
1608 uint8_t ch1 = (ch) / 2;
1609 hInfo->num_front_channels = ch1;
1610 hInfo->num_back_channels = ch1;
1611 if (ch1 & 1) {
1612 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1613 for (i = 1; i <= ch1; i += 2) {
1614 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
1615 hInfo->channel_position[i + 1] = FRONT_CHANNEL_RIGHT;
1616 }
1617 for (i = ch1 + 1; i < ch - 1; i += 2) {
1618 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
1619 hInfo->channel_position[i + 1] = BACK_CHANNEL_RIGHT;
1620 }
1621 hInfo->channel_position[ch - 1] = BACK_CHANNEL_CENTER;
1622 } else {
1623 for (i = 0; i < ch1; i += 2) {
1624 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
1625 hInfo->channel_position[i + 1] = FRONT_CHANNEL_RIGHT;
1626 }
1627 for (i = ch1; i < ch; i += 2) {
1628 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
1629 hInfo->channel_position[i + 1] = BACK_CHANNEL_RIGHT;
1630 }
1631 }
1632 }
1633 hInfo->num_lfe_channels = hDecoder->has_lfe;
1634 for (i = ch; i < hDecoder->fr_channels; i++) {
1635 hInfo->channel_position[i] = LFE_CHANNEL;
1636 }
1637 }
1638 break;
1639 }
1640 }
1641}
1642
1643void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hpDecoder,
1644 NeAACDecFrameInfo *hInfo,
1645 unsigned char *buffer,
1646 unsigned long buffer_size)
1647{
1648 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
1649 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
1650}
1651
1652void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hpDecoder,
1653 NeAACDecFrameInfo *hInfo,
1654 unsigned char *buffer,
1655 unsigned long buffer_size,
1656 void **sample_buffer,
1657 unsigned long sample_buffer_size)
1658{
1659 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
1660 if ((sample_buffer == NULL) || (sample_buffer_size == 0)) {
1661 hInfo->error = 27;
1662 return NULL;
1663 }
1664
1665 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
1666 sample_buffer, sample_buffer_size);
1667}
1668
1669#ifdef DRM
1670
1671#define ERROR_STATE_INIT 6
1672
1673static void conceal_output(NeAACDecStruct *hDecoder, uint16_t frame_len,
1674 uint8_t out_ch, void *sample_buffer)
1675{
1676 return;
1677}
1678#endif
1679
1680static int multi_sub_frame(NeAACDecStruct *hDecoder)
1681{
1682#ifdef NEW_CODE_CHECK_LATM
1683 //int i_frame_size;
1684 decoder_sys_t *p_sys = &hDecoder->dec_sys;
1685
1686 if (hDecoder->latm_header_present && p_sys->latm.i_sub_frames > 1)
1687 return 1;
1688#endif
1689
1690 return 0;
1691}
1692
1693static void* aac_frame_decode(NeAACDecStruct *hDecoder,
1694 NeAACDecFrameInfo *hInfo,
1695 unsigned char *buffer,
1696 unsigned long buffer_size,
1697 void **sample_buffer2,
1698 unsigned long sample_buffer_size)
1699{
1700 uint16_t i;
1701 uint8_t channels = 0;
1702 uint8_t output_channels = 0;
1703 bitfile ld = {0};
1704 uint32_t bitsconsumed;
1705 uint16_t frame_len;
1706 void *sample_buffer;
1707 uint32_t startbit = 0, endbit = 0, payload_bits = 0;
1708 int b_multi_sub_frame;
1709 int mux_length = 0;
1710 short* dec_buffer = hDecoder->dec_buffer;
1711 short* output_buffer = hDecoder->output_buffer;
1712 unsigned char* temp_bufer = hDecoder->temp_bufer;
1713 int temp_size = 0;
1714#ifdef NEW_CODE_CHECK_LATM
1715 int i_frame_size;
1716 decoder_sys_t *p_sys = &hDecoder->dec_sys;
1717#endif
1718#ifdef PROFILE
1719 int64_t count = faad_get_ts();
1720#endif
1721
1722 /* safety checks */
1723 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL)) {
1724 return NULL;
1725 }
1726
1727 frame_len = hDecoder->frameLength;
1728 memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
1729 memset(hDecoder->internal_channel, 0, MAX_CHANNELS * sizeof(hDecoder->internal_channel[0]));
1730
1731#ifdef USE_TIME_LIMIT
1732 if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count) {
1733 hDecoder->TL_count += 1024;
1734 } else {
1735 hInfo->error = (NUM_ERROR_MESSAGES - 1);
1736 goto error;
1737 }
1738#endif
1739
1740
1741 /* check for some common metadata tag types in the bitstream
1742 * No need to return an error
1743 */
1744 /* ID3 */
1745 if (buffer_size >= 128) {
1746 if (memcmp(buffer, "TAG", 3) == 0) {
1747 /* found it */
1748 hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
1749 /* no error, but no output either */
1750 return NULL;
1751 }
1752 }
1753#ifdef NEW_CODE_CHECK_LATM
1754 if (buffer_size > TMP_BUF_SIZE) {
1755 LATM_LOG("input buffer size tooo big %lu, buffer size %d \n", buffer_size,TMP_BUF_SIZE);
1756 //buffer_size = sizeof(temp_bufer);
1757 buffer_size = TMP_BUF_SIZE;
1758 }
1759 if (buffer_size > 0) {
1760 memcpy(temp_bufer, buffer, buffer_size);
1761 temp_size = buffer_size;
1762 buffer = temp_bufer;
1763 }
1764NEXT_CHECK:
1765 if (hDecoder->latm_header_present) {
1766 while (buffer_size >= 7) {
1767 if (buffer[0] == 0x56 && (buffer[1] & 0xe0) == 0xe0) {
1768
1769 break;
1770 }
1771 buffer++;
1772 buffer_size--;
1773 }
1774 if (buffer_size <= 2) {
1775 LATM_LOG("check the loas frame failed\n");
1776 return NULL;
1777 }
1778 /* Check if frame is valid and get frame info */
1779 i_frame_size = ((buffer[1] & 0x1f) << 8) + buffer[2];
1780 //LATM_LOG("i_frame_size %d \n",i_frame_size);
1781 mux_length = i_frame_size + 3;
1782 if (i_frame_size <= 0) {
1783 LATM_LOG("i_frame_size error\n");
1784 return NULL;
1785 }
1786 if ((int)buffer_size < (LOAS_HEADER_SIZE + i_frame_size)) {
1787 hInfo->error = 35;
1788 LATM_LOG("buffer size small then frame size,need more data\n");
1789 return NULL;
1790 }
xingri.gao619aa1a2023-12-01 03:25:38 +00001791#if 0
xingri.gaoc18d4472023-02-28 02:51:02 +00001792 if (buffer[3 + i_frame_size] != 0x56 || (buffer[3 + i_frame_size + 1] & 0xe0) != 0xe0) {
1793
1794 LATM_LOG("emulated sync word (no sync on following frame) \n");
1795 buffer++;
1796 buffer_size--;
1797 goto NEXT_CHECK;
1798 }
1799#endif
1800 buffer += LOAS_HEADER_SIZE; //skip header
1801 buffer_size = buffer_size - LOAS_HEADER_SIZE;
1802 i_frame_size = LOASParse(buffer, i_frame_size, p_sys);
1803 if (i_frame_size <= 0) {
1804 goto NEXT_CHECK;
1805 } else {
1806 // LATM_LOG("latm detected\n");
1807 }
1808
1809 }
1810
1811 b_multi_sub_frame = multi_sub_frame(hDecoder);
1812
1813 /* check if we want to use internal sample_buffer */
1814 if (sample_buffer_size == 0 && b_multi_sub_frame) {
1815 if (hDecoder->sample_buffer_all) {
1816 faad_free(hDecoder->sample_buffer_all);
1817 }
1818 hDecoder->sample_buffer_all = NULL;
1819 }
1820#endif
1821
1822start_decode:
1823
1824 /* initialize the bitstream */
1825 faad_initbits(&ld, buffer, buffer_size);
1826
1827#ifndef NEW_CODE_CHECK_LATM
1828 if (hDecoder->latm_header_present) {
1829 payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
1830 startbit = faad_get_processed_bits(&ld);
1831 if (payload_bits == -1U) {
1832 hInfo->error = 1;
1833 goto error;
1834 }
1835 }
1836#endif
1837#ifdef DRM
1838 if (hDecoder->object_type == DRM_ER_LC) {
1839 /* We do not support stereo right now */
1840 if (0) { //(hDecoder->channelConfiguration == 2)
1841 hInfo->error = 28; // Throw CRC error
1842 goto error;
1843 }
1844
1845 faad_getbits(&ld, 8
1846 DEBUGVAR(1, 1, "NeAACDecDecode(): skip CRC"));
1847 }
1848#endif
1849
1850 if (hDecoder->adts_header_present) {
1851 adts_header adts;
1852
1853 adts.old_format = hDecoder->config.useOldADTSFormat;
1854 if ((hInfo->error = adts_frame(&adts, &ld)) > 0) {
1855 goto error;
1856 }
1857 if (adts.aac_frame_length > buffer_size) {
1858 hInfo->error = 35; //more data needed
1859 audio_codec_print("decoder need more data for adts frame,frame len %d,have %lu \n", adts.aac_frame_length, buffer_size);
1860 if (adts.aac_frame_length > 6 * 768) {
1861 audio_codec_print("adts frame len exceed aac spec \n");
1862 hInfo->error = 36;//
1863 goto error;
1864
1865 }
1866 //here need return to get more input data for decoder
1867 faad_endbits(&ld);
1868 return NULL;
1869 }
1870 if (adts.sf_index >= 12 || adts.channel_configuration > 6) {
1871 audio_codec_print("adts sf/ch error,sf %d,ch config %d \n", adts.sf_index, adts.channel_configuration);
1872 hDecoder->sf_index = 3;
1873 hInfo->error = 12;
1874 goto error;
1875 }
1876 hDecoder->sf_index = adts.sf_index;
1877 if (adts.sf_index != hDecoder->last_sf_index && adts.channel_configuration != hDecoder->last_ch_configure) {
1878 if (adts.sf_index >= 0 && adts.sf_index < 12 && adts.channel_configuration > 0 && adts.channel_configuration <= 8) {
1879 hInfo->error = 34;
1880 audio_codec_print("[%s %d]last_sf_index/%d,Ch/%d,Now %d/%d\n", __FUNCTION__, __LINE__, hDecoder->last_sf_index, hDecoder->last_ch_configure, adts.sf_index, adts.channel_configuration);
1881 hDecoder->last_sf_index = hDecoder->sf_index;
1882 hDecoder->last_ch_configure = adts.channel_configuration;
1883 goto error;
1884 }
1885 }
1886 }
1887
1888#ifdef ANALYSIS
1889 dbg_count = 0;
1890#endif
1891
1892 /* decode the complete bitstream */
1893#ifdef DRM
1894 if (/*(hDecoder->object_type == 6) ||*/ (hDecoder->object_type == DRM_ER_LC)) {
1895 DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
1896 } else {
1897#endif
1898 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
1899#ifdef DRM
1900 }
1901#endif
1902#ifndef NEW_CODE_CHECK_LATM
1903 if (hDecoder->latm_header_present) {
1904 endbit = faad_get_processed_bits(&ld);
1905 if (endbit - startbit > payload_bits)
1906 DEBUG("[%s %d]ERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
1907 __FUNCTION__, __LINE__, endbit - startbit, payload_bits);
1908 if (hDecoder->latm_config.otherDataLenBits > 0) {
1909 faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
1910 }
1911 faad_byte_align(&ld);
1912 }
1913#endif
1914
1915 channels = hDecoder->fr_channels;
1916
1917 if (hInfo->error > 0) {
1918 goto error;
1919 }
1920
1921 /* safety check */
1922 if (channels == 0 || channels > MAX_CHANNELS) {
1923 DEBUG("[%s %d]invalid Channels/%d\n", __FUNCTION__, __LINE__, channels);
1924 hInfo->error = 12;
1925 goto error;
1926 }
1927
1928 /* no more bit reading after this */
1929 bitsconsumed = faad_get_processed_bits(&ld);
1930 hInfo->bytesconsumed = bit2byte(bitsconsumed);
1931 if (mux_length && hDecoder->latm_header_present && !ld.error) {
1932 if (p_sys->latm.i_sub_frames <= 1)
1933 hInfo->bytesconsumed = mux_length;
1934 }
1935 if (ld.error) {
1936 hInfo->error = 14;
1937 goto error;
1938 }
1939 faad_endbits(&ld);
1940
1941
1942 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1943#if 1
1944 && !hDecoder->latm_header_present
1945#endif
1946 ) {
1947 if (hDecoder->channelConfiguration == 0) {
1948 hDecoder->channelConfiguration = channels;
1949 }
1950
1951 if (channels == 8) { /* 7.1 */
1952 hDecoder->channelConfiguration = 7;
1953 }
1954 if (channels == 7) { /* not a standard channelConfiguration */
1955 hDecoder->channelConfiguration = 0;
1956 }
1957 }
1958
1959 if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix) {
1960 hDecoder->downMatrix = 1;
1961 output_channels = 2;
1962 } else {
1963 // output_channels = channels;
1964 if (channels == 6 || channels == 4) {
1965 output_channels = 2;
1966 } else if (channels == 3 && hDecoder->config.downMatrix) {
1967 output_channels = 2;
1968 } else {
1969 output_channels = channels;
1970 }
1971 }
1972
1973#if (defined(PS_DEC) || defined(DRM_PS))
1974 hDecoder->upMatrix = 0;
1975 /* check if we have a mono file */
1976 if (output_channels == 1) {
1977 /* upMatrix to 2 channels for implicit signalling of PS */
1978 hDecoder->upMatrix = 1;
1979 output_channels = 2;
1980 }
1981#endif
1982
1983 /* Make a channel configuration based on either a PCE or a channelConfiguration */
1984 create_channel_config(hDecoder, hInfo);
1985
1986 /* number of samples in this frame */
1987 hInfo->samples = frame_len * output_channels;
1988 /* number of channels in this frame */
1989 hInfo->channels = output_channels;
1990 /* samplerate */
1991 hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1992 /* object type */
1993 hInfo->object_type = hDecoder->object_type;
1994 /* sbr */
1995 hInfo->sbr = NO_SBR;
1996 /* header type */
1997 hInfo->header_type = RAW;
1998 if (hDecoder->adif_header_present) {
1999 hInfo->header_type = ADIF;
2000 }
2001 if (hDecoder->adts_header_present) {
2002 hInfo->header_type = ADTS;
2003 }
2004#if 1
2005 if (hDecoder->latm_header_present) {
2006 hInfo->header_type = LATM;
2007 }
2008#endif
2009#if (defined(PS_DEC) || defined(DRM_PS))
2010 hInfo->ps = hDecoder->ps_used_global;
2011#endif
2012
2013 /* check if frame has channel elements */
2014 if (channels == 0) {
2015 hDecoder->frame++;
2016 return NULL;
2017 }
2018
2019 /* allocate the buffer for the final samples */
2020 if ((hDecoder->sample_buffer == NULL) ||
2021 (hDecoder->alloced_channels != output_channels)) {
2022 static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
2023 sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
2024 sizeof(int16_t), sizeof(int16_t), 0, 0, 0
2025 };
2026 uint8_t stride = str[hDecoder->config.outputFormat - 1];
2027#ifdef SBR_DEC
2028 if (((hDecoder->sbr_present_flag == 1) && (!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1)) {
2029 stride = 2 * stride;
2030 }
2031#endif
2032 /* check if we want to use internal sample_buffer */
2033 if (sample_buffer_size == 0) {
2034 if (hDecoder->sample_buffer) {
2035 faad_free(hDecoder->sample_buffer);
2036 }
2037 hDecoder->sample_buffer = NULL;
2038 hDecoder->sample_buffer = faad_malloc(frame_len * output_channels * stride);
2039 } else if (sample_buffer_size < frame_len * output_channels * stride) {
2040 /* provided sample buffer is not big enough */
2041 hInfo->error = 27;
2042 return NULL;
2043 }
2044 hDecoder->alloced_channels = output_channels;
2045 }
2046
2047 if (sample_buffer_size == 0) {
2048 sample_buffer = hDecoder->sample_buffer;
2049 } else {
2050 sample_buffer = *sample_buffer2;
2051 }
2052
2053#ifdef SBR_DEC
2054 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) {
2055 uint8_t ele;
2056
2057 /* this data is different when SBR is used or when the data is upsampled */
2058 if (!hDecoder->downSampledSBR) {
2059 frame_len *= 2;
2060 hInfo->samples *= 2;
2061 hInfo->samplerate *= 2;
2062 }
2063
2064 /* check if every element was provided with SBR data */
2065 for (ele = 0; ele < hDecoder->fr_ch_ele; ele++) {
2066 if (hDecoder->sbr[ele] == NULL) {
2067 hInfo->error = 25;
2068 goto error;
2069 }
2070 }
2071
2072 /* sbr */
2073 if (hDecoder->sbr_present_flag == 1) {
2074 hInfo->object_type = HE_AAC;
2075 hInfo->sbr = SBR_UPSAMPLED;
2076 } else {
2077 hInfo->sbr = NO_SBR_UPSAMPLED;
2078 }
2079 if (hDecoder->downSampledSBR) {
2080 hInfo->sbr = SBR_DOWNSAMPLED;
2081 }
2082 }
2083#endif
2084
2085 if (b_multi_sub_frame && sample_buffer_size == 0 &&
2086 hDecoder->sample_buffer_all == NULL) {
2087
2088 hDecoder->sample_buffer_all = faad_malloc(p_sys->latm.i_sub_frames * hInfo->samples * 2);
2089 }
2090
2091 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
2092 output_channels, frame_len, hDecoder->config.outputFormat);
2093
2094 if (b_multi_sub_frame && i_frame_size > 0 && sample_buffer_size == 0) {
2095
2096 memcpy(hDecoder->sample_buffer_all,sample_buffer,hInfo->samples * 2);
2097 char * tmp = (char *)(hDecoder->sample_buffer_all);
2098 tmp += hInfo->samples * 2;
2099 i_frame_size -= hInfo->bytesconsumed;
2100 buffer += hInfo->bytesconsumed;
2101 buffer_size -= hInfo->bytesconsumed;
2102 if (i_frame_size > 0)
2103 goto start_decode;
2104 }
2105
2106 if (b_multi_sub_frame && sample_buffer_size == 0) {
2107 // calculate all sub_frames as one samples
2108 hInfo->samples = hInfo->samples * p_sys->latm.i_sub_frames;
2109 char * tmp = (char *)(hDecoder->sample_buffer_all);
2110 tmp -= hInfo->samples * 2;
2111 hInfo->bytesconsumed = mux_length;
2112 return hDecoder->sample_buffer_all;
2113 }
2114
2115
2116#ifdef DRM
2117 //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
2118#endif
2119
2120
2121 hDecoder->postSeekResetFlag = 0;
2122
2123 hDecoder->frame++;
2124#ifdef LD_DEC
2125 if (hDecoder->object_type != LD) {
2126#endif
2127 if (hDecoder->frame <= 1) {
2128 hInfo->samples = 0;
2129 }
2130#ifdef LD_DEC
2131 } else {
2132 /* LD encoders will give lower delay */
2133 if (hDecoder->frame <= 0) {
2134 hInfo->samples = 0;
2135 }
2136 }
2137#endif
2138
2139 /* cleanup */
2140#ifdef ANALYSIS
2141 fflush(stdout);
2142#endif
2143
2144#ifdef PROFILE
2145 count = faad_get_ts() - count;
2146 hDecoder->cycles += count;
2147#endif
2148
2149#ifdef USE_HELIX_AAC_DECODER
2150 /* Channel definitions */
2151#define FRONT_CENTER (0)
2152#define FRONT_LEFT (1)
2153#define FRONT_RIGHT (2)
2154#define SIDE_LEFT (3)
2155#define SIDE_RIGHT (4)
2156#define BACK_LEFT (5)
2157#define LFE_CHANNEL (6)
2158
2159 if (hDecoder->latm_header_present && !hInfo->error) {
2160 unsigned char *dec_buf = buffer;
2161 int dec_size = hInfo->bytesconsumed ;
2162 int err;
2163 int ch_num;
2164 int sample_out;
2165 int sum;
2166 unsigned ch_map_scale[6] = {2, 4, 4, 2, 2, 0}; //full scale == 8
2167 short *ouput = dec_buffer;
2168 unsigned char adts_header[7];
2169 unsigned char *pbuf = NULL;
2170 unsigned char *inbuf = NULL;
2171#ifdef PS_DEC
2172 if (hDecoder->ps_used_global) {
2173 // LATM_LOG("decoder ps channel %d \n",channels);
2174 if (channels == 2) {
2175 channels = 1;
2176 }
2177 }
2178#endif
2179 MakeAdtsHeader(hInfo, adts_header, channels);
2180 pbuf = malloc(7 + dec_size);
2181 if (!pbuf) {
2182 LATM_LOG("malloc decoder buffer failed %d \n", dec_size);
2183 return NULL;
2184 }
2185 dec_size += 7;
2186 memcpy(pbuf, adts_header, 7);
2187 memcpy(pbuf + 7, buffer, hInfo->bytesconsumed);
2188 inbuf = pbuf;
2189 err = AACDecode(hAACDecoder, &inbuf, &dec_size, dec_buffer);
2190 if (pbuf) {
2191 free(pbuf);
2192 pbuf = NULL;
2193 }
2194 if (err == 0) {
2195 AACFrameInfo aacFrameInfo = {0};
2196 AACGetLastFrameInfo(hAACDecoder, &aacFrameInfo);
2197 hInfo->error = 0;
2198 hInfo->bytesconsumed = mux_length;
2199 hInfo->channels = aacFrameInfo.nChans > 2 ? 2 : aacFrameInfo.nChans;
2200 hInfo->samplerate = aacFrameInfo.sampRateOut;;
2201 if (aacFrameInfo.nChans > 2) { //should do downmix to 2ch output.
2202 ch_num = aacFrameInfo.nChans;
2203 sample_out = aacFrameInfo.outputSamps / ch_num * 2 * 2; //ch_num*sample_num*16bit
2204 if (ch_num == 3 || ch_num == 4) {
2205 ch_map_scale[0] = 4; //50%
2206 ch_map_scale[1] = 4;//50%
2207 ch_map_scale[2] = 4;//50%
2208 ch_map_scale[3] = 0;
2209 ch_map_scale[4] = 0;
2210 ch_map_scale[5] = 0;
2211 }
2212 for (i = 0; i < aacFrameInfo.outputSamps / ch_num; i++) {
2213 if (ch_num == 5 || ch_num == 6) {
2214 output_buffer[i * 2] = ((int)(ouput[ch_num * i + FRONT_LEFT]) +
2215 ((int)((((int)ouput[ch_num * i + FRONT_CENTER]) -
2216 ((int)ouput[ch_num * i + SIDE_LEFT]) -
2217 ((int)ouput[ch_num * i + SIDE_RIGHT])) * 707 / 1000)));
2218 output_buffer[2 * i + 1] = ((int)(ouput[ch_num * i + FRONT_RIGHT]) +
2219 ((int)((((int)ouput[ch_num * i + FRONT_CENTER]) +
2220 ((int)ouput[ch_num * i + SIDE_LEFT]) +
2221 ((int)ouput[ch_num * i + SIDE_RIGHT])) * 707 / 1000)));
2222 } else {
2223 sum = ((int)ouput[ch_num * i + FRONT_LEFT] * ch_map_scale[FRONT_LEFT] + (int)ouput[ch_num * i + FRONT_CENTER] * ch_map_scale[FRONT_CENTER] + (int)ouput[ch_num * i + BACK_LEFT] * ch_map_scale[BACK_LEFT]);
2224 output_buffer[i * 2] = sum >> 3;
2225 sum = ((int)ouput[ch_num * i + FRONT_RIGHT] * ch_map_scale[FRONT_RIGHT] + (int)ouput[ch_num * i + FRONT_CENTER] * ch_map_scale[FRONT_CENTER] + (int)ouput[ch_num * i + BACK_LEFT] * ch_map_scale[BACK_LEFT]);
2226 output_buffer[2 * i + 1] = sum >> 3;
2227 }
2228 }
2229 } else {
2230 sample_out = aacFrameInfo.outputSamps * 2; //ch_num*sample_num*16bit
2231 memcpy(output_buffer, dec_buffer, sample_out);
2232
2233 }
2234 hInfo->samples = sample_out / 2;
2235 return output_buffer;
2236
2237 } else {
2238 LATM_LOG("decoder error id %d \n", err);
2239 hInfo->error = err > 0 ? err : -err;
2240 return NULL;
2241 }
2242 }
2243#endif
2244 return sample_buffer;
2245
2246error:
2247
2248
2249#ifdef DRM
2250 hDecoder->error_state = ERROR_STATE_INIT;
2251#endif
2252
2253 /* reset filterbank state */
2254 for (i = 0; i < MAX_CHANNELS; i++) {
2255 if (hDecoder->fb_intermed[i] != NULL) {
2256 memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength * sizeof(real_t));
2257 }
2258 }
2259#ifdef SBR_DEC
2260 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) {
2261 if (hDecoder->sbr[i] != NULL) {
2262 sbrReset(hDecoder->sbr[i]);
2263 }
2264 }
2265#endif
2266 faad_endbits(&ld);
2267 /* cleanup */
2268#ifdef ANALYSIS
2269 fflush(stdout);
2270#endif
2271
2272 return NULL;
2273}
2274
2275int is_latm_aac(NeAACDecHandle hpDecoder)
2276{
2277 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
2278 return hDecoder->latm_header_present;
2279
2280}
2281