blob: 08f1e3d7069b61b358da1c802d92b5db3ba1d58a [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;
yuliang.hu77fab5b2024-07-19 18:32:11 +0800948 memset(&ld, 0 , sizeof(ld));
xingri.gaoc18d4472023-02-28 02:51:02 +0000949 faad_log_info("enter NeAACDecInit \r\n");
950#ifdef NEW_CODE_CHECK_LATM
951 int i_frame_size;
952 if (buffer_size > TMP_BUF_SIZE) {
953 LATM_LOG("init input buffer size too big %lu, buffer size %d \n", buffer_size,TMP_BUF_SIZE);
954 //buffer_size = sizeof(temp_bufer);
955 buffer_size = TMP_BUF_SIZE ;
956 }
957 if (buffer_size > 0) {
958 memcpy(temp_bufer, buffer, buffer_size);
959 temp_size = buffer_size;
960 buffer = temp_bufer;
961 }
962 unsigned char *pbuffer = buffer;
963 int pbuffer_size = buffer_size;
964 decoder_sys_t *p_sys = &hDecoder->dec_sys;
965 latm_mux_t *m = &p_sys->latm;
966 latm_stream_t *st = NULL;
967#endif
968
969 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL)) {
970 return -1;
971 }
972 //memset(latm_payload,0,sizeof(latm_payload));
973 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
974 hDecoder->object_type = hDecoder->config.defObjectType;
975 *samplerate = get_sample_rate(hDecoder->sf_index);
976 *channels = 1;
977 *samplerate = 0;
978 *channels = 0;
979 //int latm_audio = 0;
980 //unsigned byte_cost = 0;
981 if (buffer != NULL) {
982 latm_header *l = &hDecoder->latm_config;
983 faad_initbits(&ld, buffer, buffer_size);
984#ifdef NEW_CODE_CHECK_LATM
985 memset(&hDecoder->dec_sys, 0, sizeof(decoder_sys_t));
986NEXT_CHECK:
987 while (pbuffer_size >= 2) {
988 if (pbuffer[0] == 0x56 && (pbuffer[1] & 0xe0) == 0xe0) { //LOAS sync word detected
989 // LATM_LOG("find LOAS sync word pos %d\n",buffer_size-pbuffer_size);
990 break;
991 }
992 pbuffer++;
993 pbuffer_size--;
994 }
995 if (pbuffer_size < LOAS_HEADER_SIZE) {
996 LATM_LOG("check the loas frame failed\n");
997 *skipbytes = buffer_size-pbuffer_size;
998 goto exit_check;
999 }
1000 /* Check if frame is valid and get frame info */
1001 i_frame_size = ((pbuffer[1] & 0x1f) << 8) + pbuffer[2];
1002 if (i_frame_size <= 0 || i_frame_size > 6 * 768) {
1003 LATM_LOG("i_frame_size/%d error\n",i_frame_size);
1004 pbuffer++;
1005 pbuffer_size--;
1006 goto NEXT_CHECK;
1007 }
1008 if (pbuffer_size < (LOAS_HEADER_SIZE + i_frame_size)) {
xingri.gao619aa1a2023-12-01 03:25:38 +00001009 if (0 == is_latm_external)
1010 {
1011 goto adts_check;
1012 }
xingri.gaoc18d4472023-02-28 02:51:02 +00001013 LATM_LOG("[%s %d]buffer size %d small then frame size %d,\n", __FUNCTION__,__LINE__,pbuffer_size, i_frame_size+LOAS_HEADER_SIZE);
1014 *skipbytes = buffer_size-pbuffer_size;
1015 return -1;
1016 }
xingri.gao619aa1a2023-12-01 03:25:38 +00001017#if 0
xingri.gaoc18d4472023-02-28 02:51:02 +00001018 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
1019 LATM_LOG("emulated sync word no (sync on following frame) \n");
1020 pbuffer++;
1021 pbuffer_size--;
1022 goto NEXT_CHECK;
1023 }
1024#endif
1025 pbuffer += LOAS_HEADER_SIZE; //skip header
1026 pbuffer_size = pbuffer_size - LOAS_HEADER_SIZE;
1027 //parse the playload of one real LOAS aac frame
1028 i_frame_size = LOASParse(pbuffer, i_frame_size, p_sys);
1029 if (i_frame_size <= 0) {
1030 LATM_LOG("[%s %d]invalid i_frame_size/%d ,go on next check!...\n", __FUNCTION__, __LINE__, i_frame_size);
1031 goto NEXT_CHECK;
1032 } else {
1033 LATM_LOG("latm detected\n");
1034 hDecoder->latm_header_present = 1;
1035 }
1036 //assue latm detected. start init code
1037exit_check:
1038 if (m->i_streams > 0) {
1039 st = &m->stream[m->i_streams - 1];
1040 }
1041 memset(l, 0, sizeof(latm_header));
1042 if ((st && st->i_extra) || is_latm_external) {
1043 int32_t x;
1044
1045 hDecoder->latm_header_present = 1;
1046 if (st && st->i_extra) {
1047 x = NeAACDecInit2(hDecoder, st->extra, st->i_extra, samplerate, channels);
1048 } else {
1049 x = -1;
1050 }
1051 if (x != 0) {
1052 hDecoder->latm_header_present = 0;
1053 }
1054#ifdef USE_HELIX_AAC_DECODER
1055 else {
1056 hAACDecoder = AACInitDecoder();
1057 if (!hAACDecoder) {
1058 faad_log_info("fatal error,helix aac decoder init failed\n");
1059 return -1;
1060 } else {
1061 AACDecInfo *aacDecInfo = (AACDecInfo *)hAACDecoder;
1062 if (aacDecInfo) {
1063 aacDecInfo->format = AAC_FF_ADTS;
1064 aacDecInfo->nChans = *channels;
1065 } else {
1066 LATM_LOG("aacDecInfo NULL\n");
1067 return NULL;
1068 }
1069 }
1070 }
1071#endif
1072 LATM_LOG("latm init ret %d \n", x);
1073 return x;
1074 } else
1075#else
1076 int is_latm;
1077 memset(l, 0, sizeof(latm_header));
1078 is_latm = latmCheck(l, &ld);
1079 l->inited = 0;
1080 l->frameLength = 0;
1081 faad_rewindbits(&ld);
1082 if (is_latm && l->ASCbits > 0) {
1083 int32_t x;
1084 hDecoder->latm_header_present = 1;
1085 x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits + 7) / 8, samplerate, channels);
1086 if (x != 0) {
1087 hDecoder->latm_header_present = 0;
1088 }
1089 return x;
1090 } else
1091#endif
xingri.gao619aa1a2023-12-01 03:25:38 +00001092
1093adts_check:
xingri.gaoc18d4472023-02-28 02:51:02 +00001094 /* Check if an ADIF header is present */
1095 if ((buffer[0] == 'A') && (buffer[1] == 'D') &&
1096 (buffer[2] == 'I') && (buffer[3] == 'F')) {
1097 hDecoder->adif_header_present = 1;
1098 faad_log_info("[%s %d]ADIF aac file detected\n", __FUNCTION__, __LINE__);
1099 get_adif_header(&adif, &ld);
1100 faad_byte_align(&ld);
1101
1102 hDecoder->sf_index = adif.pce[0].sf_index;
1103 hDecoder->object_type = adif.pce[0].object_type + 1;
1104
1105 *samplerate = get_sample_rate(hDecoder->sf_index);
1106 *channels = adif.pce[0].channels;
1107
1108 memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
1109 hDecoder->pce_set = 1;
1110
1111 bits = bit2byte(faad_get_processed_bits(&ld));
1112
1113 /* Check if an ADTS header is present */
1114 } else if (faad_showbits(&ld, 12) == 0xfff) {
1115 hDecoder->adts_header_present = 1;
1116
1117 adts.old_format = hDecoder->config.useOldADTSFormat;
1118 adts_frame(&adts, &ld);
1119
1120 hDecoder->sf_index = adts.sf_index;
1121 hDecoder->object_type = adts.profile + 1;
yuliang.hu77fab5b2024-07-19 18:32:11 +08001122 if (adts.sf_index < 12 && adts.channel_configuration > 0 && adts.channel_configuration <= 8) {
xingri.gaoc18d4472023-02-28 02:51:02 +00001123 hDecoder->last_sf_index = hDecoder->sf_index;
1124 hDecoder->last_ch_configure = adts.channel_configuration;
1125 }
1126 *samplerate = get_sample_rate(hDecoder->sf_index);
1127 *channels = (adts.channel_configuration > 6) ?
1128 2 : adts.channel_configuration;
1129 } else {
1130 /*we guess it is a ADTS aac files and try to resync from the error*/
1131 int ii;
1132 int adts_err = 0;
1133 faad_log_info("[%s %d]guess it is a ADTS aac files and try to resync\n", __FUNCTION__, __LINE__);
1134 faad_initbits(&ld, buffer, buffer_size);
1135 for (ii = 0; ii < (int)buffer_size; ii++) {
1136 if ((faad_showbits(&ld, 16) & 0xfff6) != 0xFFF0) {
1137 faad_getbits(&ld, 8
1138 DEBUGVAR(0, 0, ""));
1139 } else {
1140 bits = bit2byte(faad_get_processed_bits(&ld));
1141 hDecoder->adts_header_present = 1;
1142 faad_log_info("[%s %d]resync and got ADTS header\n", __FUNCTION__, __LINE__);
1143 adts.old_format = hDecoder->config.useOldADTSFormat;
1144 adts_err = adts_frame(&adts, &ld);
1145 if (adts_err == 5) {
1146 return -1;
1147 }
1148 hDecoder->sf_index = adts.sf_index;
1149 hDecoder->object_type = adts.profile + 1;
1150 faad_log_info("sf index %d,object type %d \n", hDecoder->sf_index, hDecoder->object_type);
yuliang.hu77fab5b2024-07-19 18:32:11 +08001151 if (adts.sf_index < 12 && adts.channel_configuration > 0 && adts.channel_configuration <= 8) {
xingri.gaoc18d4472023-02-28 02:51:02 +00001152 hDecoder->last_sf_index = hDecoder->sf_index;
1153 hDecoder->last_ch_configure = adts.channel_configuration;
1154 }
1155 *samplerate = get_sample_rate(hDecoder->sf_index);
1156 if (*samplerate > 96000 || adts.channel_configuration > 6 || hDecoder->sf_index >= 12) {
1157 return -1;
1158 }
1159 *channels = (adts.channel_configuration > 6) ? 2 : adts.channel_configuration;
1160 faad_log_info("[%s %d]resync adts info:FS/%lu object_type/%d chnum/%d\n", __FUNCTION__, __LINE__, *samplerate, hDecoder->object_type, *channels);
1161 break;
1162 }
1163 }
1164 if (ii == (int)buffer_size) {
1165 faad_log_info("[%s %d]sync for adts frame failed\n", __FUNCTION__, __LINE__);
1166 return -1;
1167 }
1168 }
1169 }
1170 if (ld.error) {
1171 faad_endbits(&ld);
1172 return -1;
1173 }
1174 faad_endbits(&ld);
1175
1176#if (defined(PS_DEC) || defined(DRM_PS))
1177 /* check if we have a mono file */
1178 if (*channels == 1) {
1179 /* upMatrix to 2 channels for implicit signalling of PS */
1180 *channels = 2;
1181 }
1182#endif
1183
1184 hDecoder->channelConfiguration = *channels;
1185
1186#ifdef SBR_DEC
1187 /* implicit signalling */
1188 if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
1189 *samplerate *= 2;
1190 hDecoder->forceUpSampling = 1;
1191 } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
1192 hDecoder->downSampledSBR = 1;
1193 }
1194#endif
1195
1196 /* must be done before frameLength is divided by 2 for LD */
1197#ifdef SSR_DEC
1198 if (hDecoder->object_type == SSR) {
1199 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength / SSR_BANDS);
1200 } else
1201#endif
1202 hDecoder->fb = filter_bank_init(hDecoder->frameLength);
1203
1204#ifdef LD_DEC
1205 if (hDecoder->object_type == LD) {
1206 hDecoder->frameLength >>= 1;
1207 }
1208#endif
1209
1210 if (can_decode_ot(hDecoder->object_type) < 0) {
1211 faad_log_info("[%s %d]object_type/%d can not support\n", __FUNCTION__, __LINE__, hDecoder->object_type);
1212 return -1;
1213 }
1214 faad_log_info("[%s %d]aac init finished. cost bits%d\n", __FUNCTION__, __LINE__, bits);
1215 return bits;
1216}
1217
1218/* Init the library using a DecoderSpecificInfo */
1219int NEAACDECAPI NeAACDecInit2(NeAACDecHandle hpDecoder,
1220 unsigned char *pBuffer,
1221 unsigned long SizeOfDecoderSpecificInfo,
1222 unsigned long *samplerate,
1223 unsigned char *channels)
1224{
1225 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
1226 int8_t rc;
1227 mp4AudioSpecificConfig mp4ASC;
1228 faad_log_info("enter NeAACDecInit2 \r\n");
1229 faad_log_info("extra data size %lu\r\n", SizeOfDecoderSpecificInfo);
1230 if ((hDecoder == NULL)
1231 || (pBuffer == NULL)
1232 || (SizeOfDecoderSpecificInfo < 2)
1233 || (samplerate == NULL)
1234 || (channels == NULL)) {
1235 return -1;
1236 }
1237
1238 hDecoder->adif_header_present = 0;
1239 hDecoder->adts_header_present = 0;
1240
1241 /* decode the audio specific config */
1242 rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
1243 &(hDecoder->pce), hDecoder->latm_header_present);
1244
1245 /* copy the relevant info to the decoder handle */
1246 *samplerate = mp4ASC.samplingFrequency;
1247 if (mp4ASC.channelsConfiguration) {
1248 *channels = mp4ASC.channelsConfiguration;
1249 } else {
1250 *channels = hDecoder->pce.channels;
1251 hDecoder->pce_set = 1;
1252 }
1253#if (defined(PS_DEC) || defined(DRM_PS))
1254 /* check if we have a mono file */
1255 if (*channels == 1) {
1256 /* upMatrix to 2 channels for implicit signalling of PS */
1257 *channels = 2;
1258 }
1259#endif
1260 hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
1261 hDecoder->object_type = mp4ASC.objectTypeIndex;
1262#ifdef ERROR_RESILIENCE
1263 hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
1264 hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
1265 hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
1266#endif
1267#ifdef SBR_DEC
1268 hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
1269 hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
1270 if (hDecoder->config.dontUpSampleImplicitSBR == 0) {
1271 hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
1272 } else {
1273 hDecoder->forceUpSampling = 0;
1274 }
1275
1276 /* AAC core decoder samplerate is 2 times as low */
1277 if (((hDecoder->sbr_present_flag == 1) && (!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1) {
1278 hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
1279 }
1280#endif
1281
1282 if (rc != 0) {
1283 return rc;
1284 }
1285 hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
1286 if (mp4ASC.frameLengthFlag)
1287#ifdef ALLOW_SMALL_FRAMELENGTH
1288 hDecoder->frameLength = 960;
1289#else
1290 return -1;
1291#endif
1292
1293 /* must be done before frameLength is divided by 2 for LD */
1294#ifdef SSR_DEC
1295 if (hDecoder->object_type == SSR) {
1296 hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength / SSR_BANDS);
1297 } else
1298#endif
1299 hDecoder->fb = filter_bank_init(hDecoder->frameLength);
1300
1301#ifdef LD_DEC
1302 if (hDecoder->object_type == LD) {
1303 hDecoder->frameLength >>= 1;
1304 }
1305#endif
1306 faad_log_info("aac init2 finished\r\n");
1307 return 0;
1308}
1309
1310#ifdef DRM
1311char NEAACDECAPI NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
1312 unsigned long samplerate,
1313 unsigned char channels)
1314{
1315 NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
1316 if (hDecoder == NULL) {
1317 return 1; /* error */
1318 }
1319
1320 NeAACDecClose(*hDecoder);
1321
1322 *hDecoder = NeAACDecOpen();
1323
1324 /* Special object type defined for DRM */
1325 (*hDecoder)->config.defObjectType = DRM_ER_LC;
1326
1327 (*hDecoder)->config.defSampleRate = samplerate;
1328#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
1329 (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
1330 (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
1331 (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
1332#endif
1333 (*hDecoder)->frameLength = 960;
1334 (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
1335 (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
1336
1337 if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO)) {
1338 (*hDecoder)->channelConfiguration = 2;
1339 } else {
1340 (*hDecoder)->channelConfiguration = 1;
1341 }
1342
1343#ifdef SBR_DEC
1344 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO)) {
1345 (*hDecoder)->sbr_present_flag = 0;
1346 } else {
1347 (*hDecoder)->sbr_present_flag = 1;
1348 }
1349#endif
1350
1351 (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
1352
1353 return 0;
1354}
1355#endif
1356
1357void NEAACDECAPI NeAACDecClose(NeAACDecHandle hpDecoder)
1358{
1359 uint8_t i;
1360 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
1361#ifdef USE_HELIX_AAC_DECODER
1362 if (hAACDecoder) {
1363 AACFreeDecoder(hAACDecoder);
1364 hAACDecoder = NULL;
1365 }
1366#endif
1367 if (hDecoder == NULL) {
1368 return;
1369 }
1370
1371#ifdef PROFILE
1372 //printk("AAC decoder total: %I64d cycles\n", hDecoder->cycles);
1373 //printk("requant: %I64d cycles\n", hDecoder->requant_cycles);
1374 //printk("spectral_data: %I64d cycles\n", hDecoder->spectral_cycles);
1375 //printk("scalefactors: %I64d cycles\n", hDecoder->scalefac_cycles);
1376 //printk("output: %I64d cycles\n", hDecoder->output_cycles);
1377#endif
1378
1379 for (i = 0; i < MAX_CHANNELS; i++) {
1380 if (hDecoder->time_out[i]) {
1381 faad_free(hDecoder->time_out[i]);
1382 }
1383 if (hDecoder->fb_intermed[i]) {
1384 faad_free(hDecoder->fb_intermed[i]);
1385 }
1386#ifdef SSR_DEC
1387 if (hDecoder->ssr_overlap[i]) {
1388 faad_free(hDecoder->ssr_overlap[i]);
1389 }
1390 if (hDecoder->prev_fmd[i]) {
1391 faad_free(hDecoder->prev_fmd[i]);
1392 }
1393#endif
1394#ifdef MAIN_DEC
1395 if (hDecoder->pred_stat[i]) {
1396 faad_free(hDecoder->pred_stat[i]);
1397 }
1398#endif
1399#ifdef LTP_DEC
1400 if (hDecoder->lt_pred_stat[i]) {
1401 faad_free(hDecoder->lt_pred_stat[i]);
1402 }
1403#endif
1404 }
1405
1406#ifdef SSR_DEC
1407 if (hDecoder->object_type == SSR) {
1408 ssr_filter_bank_end(hDecoder->fb);
1409 } else
1410#endif
1411 filter_bank_end(hDecoder->fb);
1412
1413 drc_end(hDecoder->drc);
1414
1415 if (hDecoder->sample_buffer) {
1416 faad_free(hDecoder->sample_buffer);
1417 }
1418
1419
1420 if (hDecoder->sample_buffer_all) {
1421 faad_free(hDecoder->sample_buffer_all);
1422 }
1423
1424#ifdef SBR_DEC
1425 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) {
1426 if (hDecoder->sbr[i]) {
1427 sbrDecodeEnd(hDecoder->sbr[i]);
1428 }
1429 }
1430#endif
1431 //why not free before?
1432 if (hDecoder) {
1433 faad_free(hDecoder);
1434 }
1435}
1436
1437void NEAACDECAPI NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
1438{
1439 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
1440 if (hDecoder) {
1441 hDecoder->postSeekResetFlag = 1;
1442
1443 if (frame != -1) {
1444 hDecoder->frame = frame;
1445 }
1446 }
1447}
1448
1449static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
1450{
1451 hInfo->num_front_channels = 0;
1452 hInfo->num_side_channels = 0;
1453 hInfo->num_back_channels = 0;
1454 hInfo->num_lfe_channels = 0;
1455 memset(hInfo->channel_position, 0, MAX_CHANNELS * sizeof(uint8_t));
1456
1457 if (hDecoder->downMatrix) {
1458 hInfo->num_front_channels = 2;
1459 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
1460 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
1461 return;
1462 }
1463
1464 /* check if there is a PCE */
1465 if (hDecoder->pce_set) {
1466 uint8_t i, chpos = 0;
1467 uint8_t chdir, back_center = 0;
1468
1469 hInfo->num_front_channels = hDecoder->pce.num_front_channels;
1470 hInfo->num_side_channels = hDecoder->pce.num_side_channels;
1471 hInfo->num_back_channels = hDecoder->pce.num_back_channels;
1472 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
1473
1474 chdir = hInfo->num_front_channels;
1475 if (chdir & 1) {
1476#if (defined(PS_DEC) || defined(DRM_PS))
1477 /* When PS is enabled output is always stereo */
1478 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
1479 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
1480#else
1481 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
1482 chdir--;
1483#endif
1484 }
1485 for (i = 0; i < chdir; i += 2) {
1486 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
1487 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
1488 }
1489
1490 for (i = 0; i < hInfo->num_side_channels; i += 2) {
1491 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT;
1492 hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT;
1493 }
1494
1495 chdir = hInfo->num_back_channels;
1496 if (chdir & 1) {
1497 back_center = 1;
1498 chdir--;
1499 }
1500 for (i = 0; i < chdir; i += 2) {
1501 hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT;
1502 hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT;
1503 }
1504 if (back_center) {
1505 hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
1506 }
1507
1508 for (i = 0; i < hInfo->num_lfe_channels; i++) {
1509 hInfo->channel_position[chpos++] = LFE_CHANNEL;
1510 }
1511
1512 } else {
1513 switch (hDecoder->channelConfiguration) {
1514 case 1:
1515#if (defined(PS_DEC) || defined(DRM_PS))
1516 /* When PS is enabled output is always stereo */
1517 hInfo->num_front_channels = 2;
1518 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
1519 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
1520#else
1521 hInfo->num_front_channels = 1;
1522 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1523#endif
1524 break;
1525 case 2:
1526 hInfo->num_front_channels = 2;
1527 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
1528 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
1529 break;
1530 case 3:
1531 hInfo->num_front_channels = 3;
1532 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1533 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
1534 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
1535 break;
1536 case 4:
1537 hInfo->num_front_channels = 3;
1538 hInfo->num_back_channels = 1;
1539 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1540 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
1541 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
1542 hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
1543 break;
1544 case 5:
1545 hInfo->num_front_channels = 3;
1546 hInfo->num_back_channels = 2;
1547 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1548 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
1549 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
1550 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
1551 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
1552 break;
1553 case 6:
1554 hInfo->num_front_channels = 3;
1555 hInfo->num_back_channels = 2;
1556 hInfo->num_lfe_channels = 1;
1557 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1558 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
1559 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
1560 hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
1561 hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
1562 hInfo->channel_position[5] = LFE_CHANNEL;
1563 break;
1564 case 7:
1565 hInfo->num_front_channels = 3;
1566 hInfo->num_side_channels = 2;
1567 hInfo->num_back_channels = 2;
1568 hInfo->num_lfe_channels = 1;
1569 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1570 hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
1571 hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
1572 hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
1573 hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
1574 hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
1575 hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
1576 hInfo->channel_position[7] = LFE_CHANNEL;
1577 break;
1578 default: { /* channelConfiguration == 0 || channelConfiguration > 7 */
1579 uint8_t i;
1580 uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
1581 if (ch & 1) { /* there's either a center front or a center back channel */
1582 uint8_t ch1 = (ch - 1) / 2;
1583 if (hDecoder->first_syn_ele == ID_SCE) {
1584 hInfo->num_front_channels = ch1 + 1;
1585 hInfo->num_back_channels = ch1;
1586 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1587 for (i = 1; i <= ch1; i += 2) {
1588 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
1589 hInfo->channel_position[i + 1] = FRONT_CHANNEL_RIGHT;
1590 }
1591 for (i = ch1 + 1; i < ch; i += 2) {
1592 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
1593 hInfo->channel_position[i + 1] = BACK_CHANNEL_RIGHT;
1594 }
1595 } else {
1596 hInfo->num_front_channels = ch1;
1597 hInfo->num_back_channels = ch1 + 1;
1598 for (i = 0; i < ch1; i += 2) {
1599 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
1600 hInfo->channel_position[i + 1] = FRONT_CHANNEL_RIGHT;
1601 }
1602 for (i = ch1; i < ch - 1; i += 2) {
1603 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
1604 hInfo->channel_position[i + 1] = BACK_CHANNEL_RIGHT;
1605 }
1606 hInfo->channel_position[ch - 1] = BACK_CHANNEL_CENTER;
1607 }
1608 } else {
1609 uint8_t ch1 = (ch) / 2;
1610 hInfo->num_front_channels = ch1;
1611 hInfo->num_back_channels = ch1;
1612 if (ch1 & 1) {
1613 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
1614 for (i = 1; i <= ch1; i += 2) {
1615 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
1616 hInfo->channel_position[i + 1] = FRONT_CHANNEL_RIGHT;
1617 }
1618 for (i = ch1 + 1; i < ch - 1; i += 2) {
1619 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
1620 hInfo->channel_position[i + 1] = BACK_CHANNEL_RIGHT;
1621 }
1622 hInfo->channel_position[ch - 1] = BACK_CHANNEL_CENTER;
1623 } else {
1624 for (i = 0; i < ch1; i += 2) {
1625 hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
1626 hInfo->channel_position[i + 1] = FRONT_CHANNEL_RIGHT;
1627 }
1628 for (i = ch1; i < ch; i += 2) {
1629 hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
1630 hInfo->channel_position[i + 1] = BACK_CHANNEL_RIGHT;
1631 }
1632 }
1633 }
1634 hInfo->num_lfe_channels = hDecoder->has_lfe;
1635 for (i = ch; i < hDecoder->fr_channels; i++) {
1636 hInfo->channel_position[i] = LFE_CHANNEL;
1637 }
1638 }
1639 break;
1640 }
1641 }
1642}
1643
1644void* NEAACDECAPI NeAACDecDecode(NeAACDecHandle hpDecoder,
1645 NeAACDecFrameInfo *hInfo,
1646 unsigned char *buffer,
1647 unsigned long buffer_size)
1648{
1649 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
1650 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
1651}
1652
1653void* NEAACDECAPI NeAACDecDecode2(NeAACDecHandle hpDecoder,
1654 NeAACDecFrameInfo *hInfo,
1655 unsigned char *buffer,
1656 unsigned long buffer_size,
1657 void **sample_buffer,
1658 unsigned long sample_buffer_size)
1659{
1660 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
1661 if ((sample_buffer == NULL) || (sample_buffer_size == 0)) {
1662 hInfo->error = 27;
1663 return NULL;
1664 }
1665
1666 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
1667 sample_buffer, sample_buffer_size);
1668}
1669
1670#ifdef DRM
1671
1672#define ERROR_STATE_INIT 6
1673
1674static void conceal_output(NeAACDecStruct *hDecoder, uint16_t frame_len,
1675 uint8_t out_ch, void *sample_buffer)
1676{
1677 return;
1678}
1679#endif
1680
1681static int multi_sub_frame(NeAACDecStruct *hDecoder)
1682{
1683#ifdef NEW_CODE_CHECK_LATM
1684 //int i_frame_size;
1685 decoder_sys_t *p_sys = &hDecoder->dec_sys;
1686
1687 if (hDecoder->latm_header_present && p_sys->latm.i_sub_frames > 1)
1688 return 1;
1689#endif
1690
1691 return 0;
1692}
1693
1694static void* aac_frame_decode(NeAACDecStruct *hDecoder,
1695 NeAACDecFrameInfo *hInfo,
1696 unsigned char *buffer,
1697 unsigned long buffer_size,
1698 void **sample_buffer2,
1699 unsigned long sample_buffer_size)
1700{
1701 uint16_t i;
1702 uint8_t channels = 0;
1703 uint8_t output_channels = 0;
1704 bitfile ld = {0};
1705 uint32_t bitsconsumed;
1706 uint16_t frame_len;
1707 void *sample_buffer;
1708 uint32_t startbit = 0, endbit = 0, payload_bits = 0;
1709 int b_multi_sub_frame;
1710 int mux_length = 0;
1711 short* dec_buffer = hDecoder->dec_buffer;
1712 short* output_buffer = hDecoder->output_buffer;
1713 unsigned char* temp_bufer = hDecoder->temp_bufer;
1714 int temp_size = 0;
1715#ifdef NEW_CODE_CHECK_LATM
1716 int i_frame_size;
1717 decoder_sys_t *p_sys = &hDecoder->dec_sys;
1718#endif
1719#ifdef PROFILE
1720 int64_t count = faad_get_ts();
1721#endif
1722
1723 /* safety checks */
1724 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL)) {
1725 return NULL;
1726 }
1727
1728 frame_len = hDecoder->frameLength;
1729 memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
1730 memset(hDecoder->internal_channel, 0, MAX_CHANNELS * sizeof(hDecoder->internal_channel[0]));
1731
1732#ifdef USE_TIME_LIMIT
1733 if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count) {
1734 hDecoder->TL_count += 1024;
1735 } else {
1736 hInfo->error = (NUM_ERROR_MESSAGES - 1);
1737 goto error;
1738 }
1739#endif
1740
1741
1742 /* check for some common metadata tag types in the bitstream
1743 * No need to return an error
1744 */
1745 /* ID3 */
1746 if (buffer_size >= 128) {
1747 if (memcmp(buffer, "TAG", 3) == 0) {
1748 /* found it */
1749 hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
1750 /* no error, but no output either */
1751 return NULL;
1752 }
1753 }
1754#ifdef NEW_CODE_CHECK_LATM
1755 if (buffer_size > TMP_BUF_SIZE) {
1756 LATM_LOG("input buffer size tooo big %lu, buffer size %d \n", buffer_size,TMP_BUF_SIZE);
1757 //buffer_size = sizeof(temp_bufer);
1758 buffer_size = TMP_BUF_SIZE;
1759 }
1760 if (buffer_size > 0) {
1761 memcpy(temp_bufer, buffer, buffer_size);
1762 temp_size = buffer_size;
1763 buffer = temp_bufer;
1764 }
1765NEXT_CHECK:
1766 if (hDecoder->latm_header_present) {
1767 while (buffer_size >= 7) {
1768 if (buffer[0] == 0x56 && (buffer[1] & 0xe0) == 0xe0) {
1769
1770 break;
1771 }
1772 buffer++;
1773 buffer_size--;
1774 }
1775 if (buffer_size <= 2) {
1776 LATM_LOG("check the loas frame failed\n");
1777 return NULL;
1778 }
1779 /* Check if frame is valid and get frame info */
1780 i_frame_size = ((buffer[1] & 0x1f) << 8) + buffer[2];
1781 //LATM_LOG("i_frame_size %d \n",i_frame_size);
1782 mux_length = i_frame_size + 3;
1783 if (i_frame_size <= 0) {
1784 LATM_LOG("i_frame_size error\n");
1785 return NULL;
1786 }
1787 if ((int)buffer_size < (LOAS_HEADER_SIZE + i_frame_size)) {
1788 hInfo->error = 35;
1789 LATM_LOG("buffer size small then frame size,need more data\n");
1790 return NULL;
1791 }
xingri.gao619aa1a2023-12-01 03:25:38 +00001792#if 0
xingri.gaoc18d4472023-02-28 02:51:02 +00001793 if (buffer[3 + i_frame_size] != 0x56 || (buffer[3 + i_frame_size + 1] & 0xe0) != 0xe0) {
1794
1795 LATM_LOG("emulated sync word (no sync on following frame) \n");
1796 buffer++;
1797 buffer_size--;
1798 goto NEXT_CHECK;
1799 }
1800#endif
1801 buffer += LOAS_HEADER_SIZE; //skip header
1802 buffer_size = buffer_size - LOAS_HEADER_SIZE;
1803 i_frame_size = LOASParse(buffer, i_frame_size, p_sys);
1804 if (i_frame_size <= 0) {
1805 goto NEXT_CHECK;
1806 } else {
1807 // LATM_LOG("latm detected\n");
1808 }
1809
1810 }
1811
1812 b_multi_sub_frame = multi_sub_frame(hDecoder);
1813
1814 /* check if we want to use internal sample_buffer */
1815 if (sample_buffer_size == 0 && b_multi_sub_frame) {
1816 if (hDecoder->sample_buffer_all) {
1817 faad_free(hDecoder->sample_buffer_all);
1818 }
1819 hDecoder->sample_buffer_all = NULL;
1820 }
1821#endif
1822
1823start_decode:
1824
1825 /* initialize the bitstream */
1826 faad_initbits(&ld, buffer, buffer_size);
1827
1828#ifndef NEW_CODE_CHECK_LATM
1829 if (hDecoder->latm_header_present) {
1830 payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
1831 startbit = faad_get_processed_bits(&ld);
1832 if (payload_bits == -1U) {
1833 hInfo->error = 1;
1834 goto error;
1835 }
1836 }
1837#endif
1838#ifdef DRM
1839 if (hDecoder->object_type == DRM_ER_LC) {
1840 /* We do not support stereo right now */
1841 if (0) { //(hDecoder->channelConfiguration == 2)
1842 hInfo->error = 28; // Throw CRC error
1843 goto error;
1844 }
1845
1846 faad_getbits(&ld, 8
1847 DEBUGVAR(1, 1, "NeAACDecDecode(): skip CRC"));
1848 }
1849#endif
1850
1851 if (hDecoder->adts_header_present) {
1852 adts_header adts;
1853
1854 adts.old_format = hDecoder->config.useOldADTSFormat;
1855 if ((hInfo->error = adts_frame(&adts, &ld)) > 0) {
1856 goto error;
1857 }
1858 if (adts.aac_frame_length > buffer_size) {
1859 hInfo->error = 35; //more data needed
1860 audio_codec_print("decoder need more data for adts frame,frame len %d,have %lu \n", adts.aac_frame_length, buffer_size);
1861 if (adts.aac_frame_length > 6 * 768) {
1862 audio_codec_print("adts frame len exceed aac spec \n");
1863 hInfo->error = 36;//
1864 goto error;
1865
1866 }
1867 //here need return to get more input data for decoder
1868 faad_endbits(&ld);
1869 return NULL;
1870 }
1871 if (adts.sf_index >= 12 || adts.channel_configuration > 6) {
1872 audio_codec_print("adts sf/ch error,sf %d,ch config %d \n", adts.sf_index, adts.channel_configuration);
1873 hDecoder->sf_index = 3;
1874 hInfo->error = 12;
1875 goto error;
1876 }
1877 hDecoder->sf_index = adts.sf_index;
1878 if (adts.sf_index != hDecoder->last_sf_index && adts.channel_configuration != hDecoder->last_ch_configure) {
yuliang.hu77fab5b2024-07-19 18:32:11 +08001879 if (adts.sf_index < 12 && adts.channel_configuration > 0 && adts.channel_configuration <= 8) {
xingri.gaoc18d4472023-02-28 02:51:02 +00001880 hInfo->error = 34;
1881 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);
1882 hDecoder->last_sf_index = hDecoder->sf_index;
1883 hDecoder->last_ch_configure = adts.channel_configuration;
1884 goto error;
1885 }
1886 }
1887 }
1888
1889#ifdef ANALYSIS
1890 dbg_count = 0;
1891#endif
1892
1893 /* decode the complete bitstream */
1894#ifdef DRM
1895 if (/*(hDecoder->object_type == 6) ||*/ (hDecoder->object_type == DRM_ER_LC)) {
1896 DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
1897 } else {
1898#endif
1899 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
1900#ifdef DRM
1901 }
1902#endif
1903#ifndef NEW_CODE_CHECK_LATM
1904 if (hDecoder->latm_header_present) {
1905 endbit = faad_get_processed_bits(&ld);
1906 if (endbit - startbit > payload_bits)
1907 DEBUG("[%s %d]ERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
1908 __FUNCTION__, __LINE__, endbit - startbit, payload_bits);
1909 if (hDecoder->latm_config.otherDataLenBits > 0) {
1910 faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
1911 }
1912 faad_byte_align(&ld);
1913 }
1914#endif
1915
1916 channels = hDecoder->fr_channels;
1917
1918 if (hInfo->error > 0) {
1919 goto error;
1920 }
1921
1922 /* safety check */
1923 if (channels == 0 || channels > MAX_CHANNELS) {
1924 DEBUG("[%s %d]invalid Channels/%d\n", __FUNCTION__, __LINE__, channels);
1925 hInfo->error = 12;
1926 goto error;
1927 }
1928
1929 /* no more bit reading after this */
1930 bitsconsumed = faad_get_processed_bits(&ld);
1931 hInfo->bytesconsumed = bit2byte(bitsconsumed);
1932 if (mux_length && hDecoder->latm_header_present && !ld.error) {
1933 if (p_sys->latm.i_sub_frames <= 1)
1934 hInfo->bytesconsumed = mux_length;
1935 }
1936 if (ld.error) {
1937 hInfo->error = 14;
1938 goto error;
1939 }
1940 faad_endbits(&ld);
1941
1942
1943 if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1944#if 1
1945 && !hDecoder->latm_header_present
1946#endif
1947 ) {
1948 if (hDecoder->channelConfiguration == 0) {
1949 hDecoder->channelConfiguration = channels;
1950 }
1951
1952 if (channels == 8) { /* 7.1 */
1953 hDecoder->channelConfiguration = 7;
1954 }
1955 if (channels == 7) { /* not a standard channelConfiguration */
1956 hDecoder->channelConfiguration = 0;
1957 }
1958 }
1959
1960 if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix) {
1961 hDecoder->downMatrix = 1;
1962 output_channels = 2;
1963 } else {
1964 // output_channels = channels;
1965 if (channels == 6 || channels == 4) {
1966 output_channels = 2;
1967 } else if (channels == 3 && hDecoder->config.downMatrix) {
1968 output_channels = 2;
1969 } else {
1970 output_channels = channels;
1971 }
1972 }
1973
1974#if (defined(PS_DEC) || defined(DRM_PS))
1975 hDecoder->upMatrix = 0;
1976 /* check if we have a mono file */
1977 if (output_channels == 1) {
1978 /* upMatrix to 2 channels for implicit signalling of PS */
1979 hDecoder->upMatrix = 1;
1980 output_channels = 2;
1981 }
1982#endif
1983
1984 /* Make a channel configuration based on either a PCE or a channelConfiguration */
1985 create_channel_config(hDecoder, hInfo);
1986
1987 /* number of samples in this frame */
1988 hInfo->samples = frame_len * output_channels;
1989 /* number of channels in this frame */
1990 hInfo->channels = output_channels;
1991 /* samplerate */
1992 hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1993 /* object type */
1994 hInfo->object_type = hDecoder->object_type;
1995 /* sbr */
1996 hInfo->sbr = NO_SBR;
1997 /* header type */
1998 hInfo->header_type = RAW;
1999 if (hDecoder->adif_header_present) {
2000 hInfo->header_type = ADIF;
2001 }
2002 if (hDecoder->adts_header_present) {
2003 hInfo->header_type = ADTS;
2004 }
2005#if 1
2006 if (hDecoder->latm_header_present) {
2007 hInfo->header_type = LATM;
2008 }
2009#endif
2010#if (defined(PS_DEC) || defined(DRM_PS))
2011 hInfo->ps = hDecoder->ps_used_global;
2012#endif
2013
2014 /* check if frame has channel elements */
2015 if (channels == 0) {
2016 hDecoder->frame++;
2017 return NULL;
2018 }
2019
2020 /* allocate the buffer for the final samples */
2021 if ((hDecoder->sample_buffer == NULL) ||
2022 (hDecoder->alloced_channels != output_channels)) {
2023 static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
2024 sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
2025 sizeof(int16_t), sizeof(int16_t), 0, 0, 0
2026 };
2027 uint8_t stride = str[hDecoder->config.outputFormat - 1];
2028#ifdef SBR_DEC
2029 if (((hDecoder->sbr_present_flag == 1) && (!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1)) {
2030 stride = 2 * stride;
2031 }
2032#endif
2033 /* check if we want to use internal sample_buffer */
2034 if (sample_buffer_size == 0) {
2035 if (hDecoder->sample_buffer) {
2036 faad_free(hDecoder->sample_buffer);
2037 }
2038 hDecoder->sample_buffer = NULL;
2039 hDecoder->sample_buffer = faad_malloc(frame_len * output_channels * stride);
2040 } else if (sample_buffer_size < frame_len * output_channels * stride) {
2041 /* provided sample buffer is not big enough */
2042 hInfo->error = 27;
2043 return NULL;
2044 }
2045 hDecoder->alloced_channels = output_channels;
2046 }
2047
2048 if (sample_buffer_size == 0) {
2049 sample_buffer = hDecoder->sample_buffer;
2050 } else {
2051 sample_buffer = *sample_buffer2;
2052 }
2053
2054#ifdef SBR_DEC
2055 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) {
2056 uint8_t ele;
2057
2058 /* this data is different when SBR is used or when the data is upsampled */
2059 if (!hDecoder->downSampledSBR) {
2060 frame_len *= 2;
2061 hInfo->samples *= 2;
2062 hInfo->samplerate *= 2;
2063 }
2064
2065 /* check if every element was provided with SBR data */
2066 for (ele = 0; ele < hDecoder->fr_ch_ele; ele++) {
2067 if (hDecoder->sbr[ele] == NULL) {
2068 hInfo->error = 25;
2069 goto error;
2070 }
2071 }
2072
2073 /* sbr */
2074 if (hDecoder->sbr_present_flag == 1) {
2075 hInfo->object_type = HE_AAC;
2076 hInfo->sbr = SBR_UPSAMPLED;
2077 } else {
2078 hInfo->sbr = NO_SBR_UPSAMPLED;
2079 }
2080 if (hDecoder->downSampledSBR) {
2081 hInfo->sbr = SBR_DOWNSAMPLED;
2082 }
2083 }
2084#endif
2085
2086 if (b_multi_sub_frame && sample_buffer_size == 0 &&
2087 hDecoder->sample_buffer_all == NULL) {
2088
2089 hDecoder->sample_buffer_all = faad_malloc(p_sys->latm.i_sub_frames * hInfo->samples * 2);
2090 }
2091
2092 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
2093 output_channels, frame_len, hDecoder->config.outputFormat);
2094
2095 if (b_multi_sub_frame && i_frame_size > 0 && sample_buffer_size == 0) {
2096
2097 memcpy(hDecoder->sample_buffer_all,sample_buffer,hInfo->samples * 2);
2098 char * tmp = (char *)(hDecoder->sample_buffer_all);
2099 tmp += hInfo->samples * 2;
2100 i_frame_size -= hInfo->bytesconsumed;
2101 buffer += hInfo->bytesconsumed;
2102 buffer_size -= hInfo->bytesconsumed;
2103 if (i_frame_size > 0)
2104 goto start_decode;
2105 }
2106
2107 if (b_multi_sub_frame && sample_buffer_size == 0) {
2108 // calculate all sub_frames as one samples
2109 hInfo->samples = hInfo->samples * p_sys->latm.i_sub_frames;
2110 char * tmp = (char *)(hDecoder->sample_buffer_all);
2111 tmp -= hInfo->samples * 2;
2112 hInfo->bytesconsumed = mux_length;
2113 return hDecoder->sample_buffer_all;
2114 }
2115
2116
2117#ifdef DRM
2118 //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
2119#endif
2120
2121
2122 hDecoder->postSeekResetFlag = 0;
2123
2124 hDecoder->frame++;
2125#ifdef LD_DEC
2126 if (hDecoder->object_type != LD) {
2127#endif
2128 if (hDecoder->frame <= 1) {
2129 hInfo->samples = 0;
2130 }
2131#ifdef LD_DEC
2132 } else {
2133 /* LD encoders will give lower delay */
2134 if (hDecoder->frame <= 0) {
2135 hInfo->samples = 0;
2136 }
2137 }
2138#endif
2139
2140 /* cleanup */
2141#ifdef ANALYSIS
2142 fflush(stdout);
2143#endif
2144
2145#ifdef PROFILE
2146 count = faad_get_ts() - count;
2147 hDecoder->cycles += count;
2148#endif
2149
2150#ifdef USE_HELIX_AAC_DECODER
2151 /* Channel definitions */
2152#define FRONT_CENTER (0)
2153#define FRONT_LEFT (1)
2154#define FRONT_RIGHT (2)
2155#define SIDE_LEFT (3)
2156#define SIDE_RIGHT (4)
2157#define BACK_LEFT (5)
2158#define LFE_CHANNEL (6)
2159
2160 if (hDecoder->latm_header_present && !hInfo->error) {
2161 unsigned char *dec_buf = buffer;
2162 int dec_size = hInfo->bytesconsumed ;
2163 int err;
2164 int ch_num;
2165 int sample_out;
2166 int sum;
2167 unsigned ch_map_scale[6] = {2, 4, 4, 2, 2, 0}; //full scale == 8
2168 short *ouput = dec_buffer;
2169 unsigned char adts_header[7];
2170 unsigned char *pbuf = NULL;
2171 unsigned char *inbuf = NULL;
2172#ifdef PS_DEC
2173 if (hDecoder->ps_used_global) {
2174 // LATM_LOG("decoder ps channel %d \n",channels);
2175 if (channels == 2) {
2176 channels = 1;
2177 }
2178 }
2179#endif
2180 MakeAdtsHeader(hInfo, adts_header, channels);
2181 pbuf = malloc(7 + dec_size);
2182 if (!pbuf) {
2183 LATM_LOG("malloc decoder buffer failed %d \n", dec_size);
2184 return NULL;
2185 }
2186 dec_size += 7;
2187 memcpy(pbuf, adts_header, 7);
2188 memcpy(pbuf + 7, buffer, hInfo->bytesconsumed);
2189 inbuf = pbuf;
2190 err = AACDecode(hAACDecoder, &inbuf, &dec_size, dec_buffer);
2191 if (pbuf) {
2192 free(pbuf);
2193 pbuf = NULL;
2194 }
2195 if (err == 0) {
2196 AACFrameInfo aacFrameInfo = {0};
2197 AACGetLastFrameInfo(hAACDecoder, &aacFrameInfo);
2198 hInfo->error = 0;
2199 hInfo->bytesconsumed = mux_length;
2200 hInfo->channels = aacFrameInfo.nChans > 2 ? 2 : aacFrameInfo.nChans;
2201 hInfo->samplerate = aacFrameInfo.sampRateOut;;
2202 if (aacFrameInfo.nChans > 2) { //should do downmix to 2ch output.
2203 ch_num = aacFrameInfo.nChans;
2204 sample_out = aacFrameInfo.outputSamps / ch_num * 2 * 2; //ch_num*sample_num*16bit
2205 if (ch_num == 3 || ch_num == 4) {
2206 ch_map_scale[0] = 4; //50%
2207 ch_map_scale[1] = 4;//50%
2208 ch_map_scale[2] = 4;//50%
2209 ch_map_scale[3] = 0;
2210 ch_map_scale[4] = 0;
2211 ch_map_scale[5] = 0;
2212 }
2213 for (i = 0; i < aacFrameInfo.outputSamps / ch_num; i++) {
2214 if (ch_num == 5 || ch_num == 6) {
2215 output_buffer[i * 2] = ((int)(ouput[ch_num * i + FRONT_LEFT]) +
2216 ((int)((((int)ouput[ch_num * i + FRONT_CENTER]) -
2217 ((int)ouput[ch_num * i + SIDE_LEFT]) -
2218 ((int)ouput[ch_num * i + SIDE_RIGHT])) * 707 / 1000)));
2219 output_buffer[2 * i + 1] = ((int)(ouput[ch_num * i + FRONT_RIGHT]) +
2220 ((int)((((int)ouput[ch_num * i + FRONT_CENTER]) +
2221 ((int)ouput[ch_num * i + SIDE_LEFT]) +
2222 ((int)ouput[ch_num * i + SIDE_RIGHT])) * 707 / 1000)));
2223 } else {
2224 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]);
2225 output_buffer[i * 2] = sum >> 3;
2226 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]);
2227 output_buffer[2 * i + 1] = sum >> 3;
2228 }
2229 }
2230 } else {
2231 sample_out = aacFrameInfo.outputSamps * 2; //ch_num*sample_num*16bit
2232 memcpy(output_buffer, dec_buffer, sample_out);
2233
2234 }
2235 hInfo->samples = sample_out / 2;
2236 return output_buffer;
2237
2238 } else {
2239 LATM_LOG("decoder error id %d \n", err);
2240 hInfo->error = err > 0 ? err : -err;
2241 return NULL;
2242 }
2243 }
2244#endif
2245 return sample_buffer;
2246
2247error:
2248
2249
2250#ifdef DRM
2251 hDecoder->error_state = ERROR_STATE_INIT;
2252#endif
2253
2254 /* reset filterbank state */
2255 for (i = 0; i < MAX_CHANNELS; i++) {
2256 if (hDecoder->fb_intermed[i] != NULL) {
2257 memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength * sizeof(real_t));
2258 }
2259 }
2260#ifdef SBR_DEC
2261 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) {
2262 if (hDecoder->sbr[i] != NULL) {
2263 sbrReset(hDecoder->sbr[i]);
2264 }
2265 }
2266#endif
2267 faad_endbits(&ld);
2268 /* cleanup */
2269#ifdef ANALYSIS
2270 fflush(stdout);
2271#endif
2272
2273 return NULL;
2274}
2275
2276int is_latm_aac(NeAACDecHandle hpDecoder)
2277{
2278 NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
2279 return hDecoder->latm_header_present;
2280
2281}
2282