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