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