Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
52867 views
1
/*
2
* AAC decoder
3
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
4
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
5
* Copyright (c) 2008-2013 Alex Converse <[email protected]>
6
*
7
* AAC LATM decoder
8
* Copyright (c) 2008-2010 Paul Kendall <[email protected]>
9
* Copyright (c) 2010 Janne Grunau <[email protected]>
10
*
11
* AAC decoder fixed-point implementation
12
* Copyright (c) 2013
13
* MIPS Technologies, Inc., California.
14
*
15
* This file is part of FFmpeg.
16
*
17
* FFmpeg is free software; you can redistribute it and/or
18
* modify it under the terms of the GNU Lesser General Public
19
* License as published by the Free Software Foundation; either
20
* version 2.1 of the License, or (at your option) any later version.
21
*
22
* FFmpeg is distributed in the hope that it will be useful,
23
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25
* Lesser General Public License for more details.
26
*
27
* You should have received a copy of the GNU Lesser General Public
28
* License along with FFmpeg; if not, write to the Free Software
29
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30
*/
31
32
/**
33
* @file
34
* AAC decoder
35
* @author Oded Shimon ( ods15 ods15 dyndns org )
36
* @author Maxim Gavrilov ( maxim.gavrilov gmail com )
37
*
38
* AAC decoder fixed-point implementation
39
* @author Stanislav Ocovaj ( stanislav.ocovaj imgtec com )
40
* @author Nedeljko Babic ( nedeljko.babic imgtec com )
41
*/
42
43
/*
44
* supported tools
45
*
46
* Support? Name
47
* N (code in SoC repo) gain control
48
* Y block switching
49
* Y window shapes - standard
50
* N window shapes - Low Delay
51
* Y filterbank - standard
52
* N (code in SoC repo) filterbank - Scalable Sample Rate
53
* Y Temporal Noise Shaping
54
* Y Long Term Prediction
55
* Y intensity stereo
56
* Y channel coupling
57
* Y frequency domain prediction
58
* Y Perceptual Noise Substitution
59
* Y Mid/Side stereo
60
* N Scalable Inverse AAC Quantization
61
* N Frequency Selective Switch
62
* N upsampling filter
63
* Y quantization & coding - AAC
64
* N quantization & coding - TwinVQ
65
* N quantization & coding - BSAC
66
* N AAC Error Resilience tools
67
* N Error Resilience payload syntax
68
* N Error Protection tool
69
* N CELP
70
* N Silence Compression
71
* N HVXC
72
* N HVXC 4kbits/s VR
73
* N Structured Audio tools
74
* N Structured Audio Sample Bank Format
75
* N MIDI
76
* N Harmonic and Individual Lines plus Noise
77
* N Text-To-Speech Interface
78
* Y Spectral Band Replication
79
* Y (not in this code) Layer-1
80
* Y (not in this code) Layer-2
81
* Y (not in this code) Layer-3
82
* N SinuSoidal Coding (Transient, Sinusoid, Noise)
83
* Y Parametric Stereo
84
* N Direct Stream Transfer
85
* Y (not in fixed point code) Enhanced AAC Low Delay (ER AAC ELD)
86
*
87
* Note: - HE AAC v1 comprises LC AAC with Spectral Band Replication.
88
* - HE AAC v2 comprises LC AAC with Spectral Band Replication and
89
Parametric Stereo.
90
*/
91
92
#include "libavutil/thread.h"
93
94
static VLC vlc_scalefactors;
95
static VLC vlc_spectral[11];
96
97
static int output_configure(AACContext *ac,
98
uint8_t layout_map[MAX_ELEM_ID*4][3], int tags,
99
enum OCStatus oc_type, int get_new_frame);
100
101
#define overread_err "Input buffer exhausted before END element found\n"
102
103
static int count_channels(uint8_t (*layout)[3], int tags)
104
{
105
int i, sum = 0;
106
for (i = 0; i < tags; i++) {
107
int syn_ele = layout[i][0];
108
int pos = layout[i][2];
109
sum += (1 + (syn_ele == TYPE_CPE)) *
110
(pos != AAC_CHANNEL_OFF && pos != AAC_CHANNEL_CC);
111
}
112
return sum;
113
}
114
115
/**
116
* Check for the channel element in the current channel position configuration.
117
* If it exists, make sure the appropriate element is allocated and map the
118
* channel order to match the internal FFmpeg channel layout.
119
*
120
* @param che_pos current channel position configuration
121
* @param type channel element type
122
* @param id channel element id
123
* @param channels count of the number of channels in the configuration
124
*
125
* @return Returns error status. 0 - OK, !0 - error
126
*/
127
static av_cold int che_configure(AACContext *ac,
128
enum ChannelPosition che_pos,
129
int type, int id, int *channels)
130
{
131
if (*channels >= MAX_CHANNELS)
132
return AVERROR_INVALIDDATA;
133
if (che_pos) {
134
if (!ac->che[type][id]) {
135
if (!(ac->che[type][id] = av_mallocz(sizeof(ChannelElement))))
136
return AVERROR(ENOMEM);
137
AAC_RENAME(ff_aac_sbr_ctx_init)(ac, &ac->che[type][id]->sbr);
138
}
139
if (type != TYPE_CCE) {
140
if (*channels >= MAX_CHANNELS - (type == TYPE_CPE || (type == TYPE_SCE && ac->oc[1].m4ac.ps == 1))) {
141
av_log(ac->avctx, AV_LOG_ERROR, "Too many channels\n");
142
return AVERROR_INVALIDDATA;
143
}
144
ac->output_element[(*channels)++] = &ac->che[type][id]->ch[0];
145
if (type == TYPE_CPE ||
146
(type == TYPE_SCE && ac->oc[1].m4ac.ps == 1)) {
147
ac->output_element[(*channels)++] = &ac->che[type][id]->ch[1];
148
}
149
}
150
} else {
151
if (ac->che[type][id])
152
AAC_RENAME(ff_aac_sbr_ctx_close)(&ac->che[type][id]->sbr);
153
av_freep(&ac->che[type][id]);
154
}
155
return 0;
156
}
157
158
static int frame_configure_elements(AVCodecContext *avctx)
159
{
160
AACContext *ac = avctx->priv_data;
161
int type, id, ch, ret;
162
163
/* set channel pointers to internal buffers by default */
164
for (type = 0; type < 4; type++) {
165
for (id = 0; id < MAX_ELEM_ID; id++) {
166
ChannelElement *che = ac->che[type][id];
167
if (che) {
168
che->ch[0].ret = che->ch[0].ret_buf;
169
che->ch[1].ret = che->ch[1].ret_buf;
170
}
171
}
172
}
173
174
/* get output buffer */
175
av_frame_unref(ac->frame);
176
if (!avctx->channels)
177
return 1;
178
179
ac->frame->nb_samples = 2048;
180
if ((ret = ff_get_buffer(avctx, ac->frame, 0)) < 0)
181
return ret;
182
183
/* map output channel pointers to AVFrame data */
184
for (ch = 0; ch < avctx->channels; ch++) {
185
if (ac->output_element[ch])
186
ac->output_element[ch]->ret = (INTFLOAT *)ac->frame->extended_data[ch];
187
}
188
189
return 0;
190
}
191
192
struct elem_to_channel {
193
uint64_t av_position;
194
uint8_t syn_ele;
195
uint8_t elem_id;
196
uint8_t aac_position;
197
};
198
199
static int assign_pair(struct elem_to_channel e2c_vec[MAX_ELEM_ID],
200
uint8_t (*layout_map)[3], int offset, uint64_t left,
201
uint64_t right, int pos)
202
{
203
if (layout_map[offset][0] == TYPE_CPE) {
204
e2c_vec[offset] = (struct elem_to_channel) {
205
.av_position = left | right,
206
.syn_ele = TYPE_CPE,
207
.elem_id = layout_map[offset][1],
208
.aac_position = pos
209
};
210
return 1;
211
} else {
212
e2c_vec[offset] = (struct elem_to_channel) {
213
.av_position = left,
214
.syn_ele = TYPE_SCE,
215
.elem_id = layout_map[offset][1],
216
.aac_position = pos
217
};
218
e2c_vec[offset + 1] = (struct elem_to_channel) {
219
.av_position = right,
220
.syn_ele = TYPE_SCE,
221
.elem_id = layout_map[offset + 1][1],
222
.aac_position = pos
223
};
224
return 2;
225
}
226
}
227
228
static int count_paired_channels(uint8_t (*layout_map)[3], int tags, int pos,
229
int *current)
230
{
231
int num_pos_channels = 0;
232
int first_cpe = 0;
233
int sce_parity = 0;
234
int i;
235
for (i = *current; i < tags; i++) {
236
if (layout_map[i][2] != pos)
237
break;
238
if (layout_map[i][0] == TYPE_CPE) {
239
if (sce_parity) {
240
if (pos == AAC_CHANNEL_FRONT && !first_cpe) {
241
sce_parity = 0;
242
} else {
243
return -1;
244
}
245
}
246
num_pos_channels += 2;
247
first_cpe = 1;
248
} else {
249
num_pos_channels++;
250
sce_parity ^= 1;
251
}
252
}
253
if (sce_parity &&
254
((pos == AAC_CHANNEL_FRONT && first_cpe) || pos == AAC_CHANNEL_SIDE))
255
return -1;
256
*current = i;
257
return num_pos_channels;
258
}
259
260
static uint64_t sniff_channel_order(uint8_t (*layout_map)[3], int tags)
261
{
262
int i, n, total_non_cc_elements;
263
struct elem_to_channel e2c_vec[4 * MAX_ELEM_ID] = { { 0 } };
264
int num_front_channels, num_side_channels, num_back_channels;
265
uint64_t layout;
266
267
if (FF_ARRAY_ELEMS(e2c_vec) < tags)
268
return 0;
269
270
i = 0;
271
num_front_channels =
272
count_paired_channels(layout_map, tags, AAC_CHANNEL_FRONT, &i);
273
if (num_front_channels < 0)
274
return 0;
275
num_side_channels =
276
count_paired_channels(layout_map, tags, AAC_CHANNEL_SIDE, &i);
277
if (num_side_channels < 0)
278
return 0;
279
num_back_channels =
280
count_paired_channels(layout_map, tags, AAC_CHANNEL_BACK, &i);
281
if (num_back_channels < 0)
282
return 0;
283
284
if (num_side_channels == 0 && num_back_channels >= 4) {
285
num_side_channels = 2;
286
num_back_channels -= 2;
287
}
288
289
i = 0;
290
if (num_front_channels & 1) {
291
e2c_vec[i] = (struct elem_to_channel) {
292
.av_position = AV_CH_FRONT_CENTER,
293
.syn_ele = TYPE_SCE,
294
.elem_id = layout_map[i][1],
295
.aac_position = AAC_CHANNEL_FRONT
296
};
297
i++;
298
num_front_channels--;
299
}
300
if (num_front_channels >= 4) {
301
i += assign_pair(e2c_vec, layout_map, i,
302
AV_CH_FRONT_LEFT_OF_CENTER,
303
AV_CH_FRONT_RIGHT_OF_CENTER,
304
AAC_CHANNEL_FRONT);
305
num_front_channels -= 2;
306
}
307
if (num_front_channels >= 2) {
308
i += assign_pair(e2c_vec, layout_map, i,
309
AV_CH_FRONT_LEFT,
310
AV_CH_FRONT_RIGHT,
311
AAC_CHANNEL_FRONT);
312
num_front_channels -= 2;
313
}
314
while (num_front_channels >= 2) {
315
i += assign_pair(e2c_vec, layout_map, i,
316
UINT64_MAX,
317
UINT64_MAX,
318
AAC_CHANNEL_FRONT);
319
num_front_channels -= 2;
320
}
321
322
if (num_side_channels >= 2) {
323
i += assign_pair(e2c_vec, layout_map, i,
324
AV_CH_SIDE_LEFT,
325
AV_CH_SIDE_RIGHT,
326
AAC_CHANNEL_FRONT);
327
num_side_channels -= 2;
328
}
329
while (num_side_channels >= 2) {
330
i += assign_pair(e2c_vec, layout_map, i,
331
UINT64_MAX,
332
UINT64_MAX,
333
AAC_CHANNEL_SIDE);
334
num_side_channels -= 2;
335
}
336
337
while (num_back_channels >= 4) {
338
i += assign_pair(e2c_vec, layout_map, i,
339
UINT64_MAX,
340
UINT64_MAX,
341
AAC_CHANNEL_BACK);
342
num_back_channels -= 2;
343
}
344
if (num_back_channels >= 2) {
345
i += assign_pair(e2c_vec, layout_map, i,
346
AV_CH_BACK_LEFT,
347
AV_CH_BACK_RIGHT,
348
AAC_CHANNEL_BACK);
349
num_back_channels -= 2;
350
}
351
if (num_back_channels) {
352
e2c_vec[i] = (struct elem_to_channel) {
353
.av_position = AV_CH_BACK_CENTER,
354
.syn_ele = TYPE_SCE,
355
.elem_id = layout_map[i][1],
356
.aac_position = AAC_CHANNEL_BACK
357
};
358
i++;
359
num_back_channels--;
360
}
361
362
if (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
363
e2c_vec[i] = (struct elem_to_channel) {
364
.av_position = AV_CH_LOW_FREQUENCY,
365
.syn_ele = TYPE_LFE,
366
.elem_id = layout_map[i][1],
367
.aac_position = AAC_CHANNEL_LFE
368
};
369
i++;
370
}
371
while (i < tags && layout_map[i][2] == AAC_CHANNEL_LFE) {
372
e2c_vec[i] = (struct elem_to_channel) {
373
.av_position = UINT64_MAX,
374
.syn_ele = TYPE_LFE,
375
.elem_id = layout_map[i][1],
376
.aac_position = AAC_CHANNEL_LFE
377
};
378
i++;
379
}
380
381
// Must choose a stable sort
382
total_non_cc_elements = n = i;
383
do {
384
int next_n = 0;
385
for (i = 1; i < n; i++)
386
if (e2c_vec[i - 1].av_position > e2c_vec[i].av_position) {
387
FFSWAP(struct elem_to_channel, e2c_vec[i - 1], e2c_vec[i]);
388
next_n = i;
389
}
390
n = next_n;
391
} while (n > 0);
392
393
layout = 0;
394
for (i = 0; i < total_non_cc_elements; i++) {
395
layout_map[i][0] = e2c_vec[i].syn_ele;
396
layout_map[i][1] = e2c_vec[i].elem_id;
397
layout_map[i][2] = e2c_vec[i].aac_position;
398
if (e2c_vec[i].av_position != UINT64_MAX) {
399
layout |= e2c_vec[i].av_position;
400
}
401
}
402
403
return layout;
404
}
405
406
/**
407
* Save current output configuration if and only if it has been locked.
408
*/
409
static void push_output_configuration(AACContext *ac) {
410
if (ac->oc[1].status == OC_LOCKED || ac->oc[0].status == OC_NONE) {
411
ac->oc[0] = ac->oc[1];
412
}
413
ac->oc[1].status = OC_NONE;
414
}
415
416
/**
417
* Restore the previous output configuration if and only if the current
418
* configuration is unlocked.
419
*/
420
static void pop_output_configuration(AACContext *ac) {
421
if (ac->oc[1].status != OC_LOCKED && ac->oc[0].status != OC_NONE) {
422
ac->oc[1] = ac->oc[0];
423
ac->avctx->channels = ac->oc[1].channels;
424
ac->avctx->channel_layout = ac->oc[1].channel_layout;
425
output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
426
ac->oc[1].status, 0);
427
}
428
}
429
430
/**
431
* Configure output channel order based on the current program
432
* configuration element.
433
*
434
* @return Returns error status. 0 - OK, !0 - error
435
*/
436
static int output_configure(AACContext *ac,
437
uint8_t layout_map[MAX_ELEM_ID * 4][3], int tags,
438
enum OCStatus oc_type, int get_new_frame)
439
{
440
AVCodecContext *avctx = ac->avctx;
441
int i, channels = 0, ret;
442
uint64_t layout = 0;
443
uint8_t id_map[TYPE_END][MAX_ELEM_ID] = {{ 0 }};
444
uint8_t type_counts[TYPE_END] = { 0 };
445
446
if (ac->oc[1].layout_map != layout_map) {
447
memcpy(ac->oc[1].layout_map, layout_map, tags * sizeof(layout_map[0]));
448
ac->oc[1].layout_map_tags = tags;
449
}
450
for (i = 0; i < tags; i++) {
451
int type = layout_map[i][0];
452
int id = layout_map[i][1];
453
id_map[type][id] = type_counts[type]++;
454
if (id_map[type][id] >= MAX_ELEM_ID) {
455
avpriv_request_sample(ac->avctx, "Remapped id too large\n");
456
return AVERROR_PATCHWELCOME;
457
}
458
}
459
// Try to sniff a reasonable channel order, otherwise output the
460
// channels in the order the PCE declared them.
461
if (avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE)
462
layout = sniff_channel_order(layout_map, tags);
463
for (i = 0; i < tags; i++) {
464
int type = layout_map[i][0];
465
int id = layout_map[i][1];
466
int iid = id_map[type][id];
467
int position = layout_map[i][2];
468
// Allocate or free elements depending on if they are in the
469
// current program configuration.
470
ret = che_configure(ac, position, type, iid, &channels);
471
if (ret < 0)
472
return ret;
473
ac->tag_che_map[type][id] = ac->che[type][iid];
474
}
475
if (ac->oc[1].m4ac.ps == 1 && channels == 2) {
476
if (layout == AV_CH_FRONT_CENTER) {
477
layout = AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT;
478
} else {
479
layout = 0;
480
}
481
}
482
483
if (layout) avctx->channel_layout = layout;
484
ac->oc[1].channel_layout = layout;
485
avctx->channels = ac->oc[1].channels = channels;
486
ac->oc[1].status = oc_type;
487
488
if (get_new_frame) {
489
if ((ret = frame_configure_elements(ac->avctx)) < 0)
490
return ret;
491
}
492
493
return 0;
494
}
495
496
static void flush(AVCodecContext *avctx)
497
{
498
AACContext *ac= avctx->priv_data;
499
int type, i, j;
500
501
for (type = 3; type >= 0; type--) {
502
for (i = 0; i < MAX_ELEM_ID; i++) {
503
ChannelElement *che = ac->che[type][i];
504
if (che) {
505
for (j = 0; j <= 1; j++) {
506
memset(che->ch[j].saved, 0, sizeof(che->ch[j].saved));
507
}
508
}
509
}
510
}
511
}
512
513
/**
514
* Set up channel positions based on a default channel configuration
515
* as specified in table 1.17.
516
*
517
* @return Returns error status. 0 - OK, !0 - error
518
*/
519
static int set_default_channel_config(AVCodecContext *avctx,
520
uint8_t (*layout_map)[3],
521
int *tags,
522
int channel_config)
523
{
524
if (channel_config < 1 || (channel_config > 7 && channel_config < 11) ||
525
channel_config > 12) {
526
av_log(avctx, AV_LOG_ERROR,
527
"invalid default channel configuration (%d)\n",
528
channel_config);
529
return AVERROR_INVALIDDATA;
530
}
531
*tags = tags_per_config[channel_config];
532
memcpy(layout_map, aac_channel_layout_map[channel_config - 1],
533
*tags * sizeof(*layout_map));
534
535
/*
536
* AAC specification has 7.1(wide) as a default layout for 8-channel streams.
537
* However, at least Nero AAC encoder encodes 7.1 streams using the default
538
* channel config 7, mapping the side channels of the original audio stream
539
* to the second AAC_CHANNEL_FRONT pair in the AAC stream. Similarly, e.g. FAAD
540
* decodes the second AAC_CHANNEL_FRONT pair as side channels, therefore decoding
541
* the incorrect streams as if they were correct (and as the encoder intended).
542
*
543
* As actual intended 7.1(wide) streams are very rare, default to assuming a
544
* 7.1 layout was intended.
545
*/
546
if (channel_config == 7 && avctx->strict_std_compliance < FF_COMPLIANCE_STRICT) {
547
av_log(avctx, AV_LOG_INFO, "Assuming an incorrectly encoded 7.1 channel layout"
548
" instead of a spec-compliant 7.1(wide) layout, use -strict %d to decode"
549
" according to the specification instead.\n", FF_COMPLIANCE_STRICT);
550
layout_map[2][2] = AAC_CHANNEL_SIDE;
551
}
552
553
return 0;
554
}
555
556
static ChannelElement *get_che(AACContext *ac, int type, int elem_id)
557
{
558
/* For PCE based channel configurations map the channels solely based
559
* on tags. */
560
if (!ac->oc[1].m4ac.chan_config) {
561
return ac->tag_che_map[type][elem_id];
562
}
563
// Allow single CPE stereo files to be signalled with mono configuration.
564
if (!ac->tags_mapped && type == TYPE_CPE &&
565
ac->oc[1].m4ac.chan_config == 1) {
566
uint8_t layout_map[MAX_ELEM_ID*4][3];
567
int layout_map_tags;
568
push_output_configuration(ac);
569
570
av_log(ac->avctx, AV_LOG_DEBUG, "mono with CPE\n");
571
572
if (set_default_channel_config(ac->avctx, layout_map,
573
&layout_map_tags, 2) < 0)
574
return NULL;
575
if (output_configure(ac, layout_map, layout_map_tags,
576
OC_TRIAL_FRAME, 1) < 0)
577
return NULL;
578
579
ac->oc[1].m4ac.chan_config = 2;
580
ac->oc[1].m4ac.ps = 0;
581
}
582
// And vice-versa
583
if (!ac->tags_mapped && type == TYPE_SCE &&
584
ac->oc[1].m4ac.chan_config == 2) {
585
uint8_t layout_map[MAX_ELEM_ID * 4][3];
586
int layout_map_tags;
587
push_output_configuration(ac);
588
589
av_log(ac->avctx, AV_LOG_DEBUG, "stereo with SCE\n");
590
591
if (set_default_channel_config(ac->avctx, layout_map,
592
&layout_map_tags, 1) < 0)
593
return NULL;
594
if (output_configure(ac, layout_map, layout_map_tags,
595
OC_TRIAL_FRAME, 1) < 0)
596
return NULL;
597
598
ac->oc[1].m4ac.chan_config = 1;
599
if (ac->oc[1].m4ac.sbr)
600
ac->oc[1].m4ac.ps = -1;
601
}
602
/* For indexed channel configurations map the channels solely based
603
* on position. */
604
switch (ac->oc[1].m4ac.chan_config) {
605
case 12:
606
case 7:
607
if (ac->tags_mapped == 3 && type == TYPE_CPE) {
608
ac->tags_mapped++;
609
return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][2];
610
}
611
case 11:
612
if (ac->tags_mapped == 2 &&
613
ac->oc[1].m4ac.chan_config == 11 &&
614
type == TYPE_SCE) {
615
ac->tags_mapped++;
616
return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
617
}
618
case 6:
619
/* Some streams incorrectly code 5.1 audio as
620
* SCE[0] CPE[0] CPE[1] SCE[1]
621
* instead of
622
* SCE[0] CPE[0] CPE[1] LFE[0].
623
* If we seem to have encountered such a stream, transfer
624
* the LFE[0] element to the SCE[1]'s mapping */
625
if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
626
if (!ac->warned_remapping_once && (type != TYPE_LFE || elem_id != 0)) {
627
av_log(ac->avctx, AV_LOG_WARNING,
628
"This stream seems to incorrectly report its last channel as %s[%d], mapping to LFE[0]\n",
629
type == TYPE_SCE ? "SCE" : "LFE", elem_id);
630
ac->warned_remapping_once++;
631
}
632
ac->tags_mapped++;
633
return ac->tag_che_map[type][elem_id] = ac->che[TYPE_LFE][0];
634
}
635
case 5:
636
if (ac->tags_mapped == 2 && type == TYPE_CPE) {
637
ac->tags_mapped++;
638
return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][1];
639
}
640
case 4:
641
/* Some streams incorrectly code 4.0 audio as
642
* SCE[0] CPE[0] LFE[0]
643
* instead of
644
* SCE[0] CPE[0] SCE[1].
645
* If we seem to have encountered such a stream, transfer
646
* the SCE[1] element to the LFE[0]'s mapping */
647
if (ac->tags_mapped == tags_per_config[ac->oc[1].m4ac.chan_config] - 1 && (type == TYPE_LFE || type == TYPE_SCE)) {
648
if (!ac->warned_remapping_once && (type != TYPE_SCE || elem_id != 1)) {
649
av_log(ac->avctx, AV_LOG_WARNING,
650
"This stream seems to incorrectly report its last channel as %s[%d], mapping to SCE[1]\n",
651
type == TYPE_SCE ? "SCE" : "LFE", elem_id);
652
ac->warned_remapping_once++;
653
}
654
ac->tags_mapped++;
655
return ac->tag_che_map[type][elem_id] = ac->che[TYPE_SCE][1];
656
}
657
if (ac->tags_mapped == 2 &&
658
ac->oc[1].m4ac.chan_config == 4 &&
659
type == TYPE_SCE) {
660
ac->tags_mapped++;
661
return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][1];
662
}
663
case 3:
664
case 2:
665
if (ac->tags_mapped == (ac->oc[1].m4ac.chan_config != 2) &&
666
type == TYPE_CPE) {
667
ac->tags_mapped++;
668
return ac->tag_che_map[TYPE_CPE][elem_id] = ac->che[TYPE_CPE][0];
669
} else if (ac->oc[1].m4ac.chan_config == 2) {
670
return NULL;
671
}
672
case 1:
673
if (!ac->tags_mapped && type == TYPE_SCE) {
674
ac->tags_mapped++;
675
return ac->tag_che_map[TYPE_SCE][elem_id] = ac->che[TYPE_SCE][0];
676
}
677
default:
678
return NULL;
679
}
680
}
681
682
/**
683
* Decode an array of 4 bit element IDs, optionally interleaved with a
684
* stereo/mono switching bit.
685
*
686
* @param type speaker type/position for these channels
687
*/
688
static void decode_channel_map(uint8_t layout_map[][3],
689
enum ChannelPosition type,
690
GetBitContext *gb, int n)
691
{
692
while (n--) {
693
enum RawDataBlockType syn_ele;
694
switch (type) {
695
case AAC_CHANNEL_FRONT:
696
case AAC_CHANNEL_BACK:
697
case AAC_CHANNEL_SIDE:
698
syn_ele = get_bits1(gb);
699
break;
700
case AAC_CHANNEL_CC:
701
skip_bits1(gb);
702
syn_ele = TYPE_CCE;
703
break;
704
case AAC_CHANNEL_LFE:
705
syn_ele = TYPE_LFE;
706
break;
707
default:
708
// AAC_CHANNEL_OFF has no channel map
709
av_assert0(0);
710
}
711
layout_map[0][0] = syn_ele;
712
layout_map[0][1] = get_bits(gb, 4);
713
layout_map[0][2] = type;
714
layout_map++;
715
}
716
}
717
718
/**
719
* Decode program configuration element; reference: table 4.2.
720
*
721
* @return Returns error status. 0 - OK, !0 - error
722
*/
723
static int decode_pce(AVCodecContext *avctx, MPEG4AudioConfig *m4ac,
724
uint8_t (*layout_map)[3],
725
GetBitContext *gb)
726
{
727
int num_front, num_side, num_back, num_lfe, num_assoc_data, num_cc;
728
int sampling_index;
729
int comment_len;
730
int tags;
731
732
skip_bits(gb, 2); // object_type
733
734
sampling_index = get_bits(gb, 4);
735
if (m4ac->sampling_index != sampling_index)
736
av_log(avctx, AV_LOG_WARNING,
737
"Sample rate index in program config element does not "
738
"match the sample rate index configured by the container.\n");
739
740
num_front = get_bits(gb, 4);
741
num_side = get_bits(gb, 4);
742
num_back = get_bits(gb, 4);
743
num_lfe = get_bits(gb, 2);
744
num_assoc_data = get_bits(gb, 3);
745
num_cc = get_bits(gb, 4);
746
747
if (get_bits1(gb))
748
skip_bits(gb, 4); // mono_mixdown_tag
749
if (get_bits1(gb))
750
skip_bits(gb, 4); // stereo_mixdown_tag
751
752
if (get_bits1(gb))
753
skip_bits(gb, 3); // mixdown_coeff_index and pseudo_surround
754
755
if (get_bits_left(gb) < 4 * (num_front + num_side + num_back + num_lfe + num_assoc_data + num_cc)) {
756
av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
757
return -1;
758
}
759
decode_channel_map(layout_map , AAC_CHANNEL_FRONT, gb, num_front);
760
tags = num_front;
761
decode_channel_map(layout_map + tags, AAC_CHANNEL_SIDE, gb, num_side);
762
tags += num_side;
763
decode_channel_map(layout_map + tags, AAC_CHANNEL_BACK, gb, num_back);
764
tags += num_back;
765
decode_channel_map(layout_map + tags, AAC_CHANNEL_LFE, gb, num_lfe);
766
tags += num_lfe;
767
768
skip_bits_long(gb, 4 * num_assoc_data);
769
770
decode_channel_map(layout_map + tags, AAC_CHANNEL_CC, gb, num_cc);
771
tags += num_cc;
772
773
align_get_bits(gb);
774
775
/* comment field, first byte is length */
776
comment_len = get_bits(gb, 8) * 8;
777
if (get_bits_left(gb) < comment_len) {
778
av_log(avctx, AV_LOG_ERROR, "decode_pce: " overread_err);
779
return AVERROR_INVALIDDATA;
780
}
781
skip_bits_long(gb, comment_len);
782
return tags;
783
}
784
785
/**
786
* Decode GA "General Audio" specific configuration; reference: table 4.1.
787
*
788
* @param ac pointer to AACContext, may be null
789
* @param avctx pointer to AVCCodecContext, used for logging
790
*
791
* @return Returns error status. 0 - OK, !0 - error
792
*/
793
static int decode_ga_specific_config(AACContext *ac, AVCodecContext *avctx,
794
GetBitContext *gb,
795
MPEG4AudioConfig *m4ac,
796
int channel_config)
797
{
798
int extension_flag, ret, ep_config, res_flags;
799
uint8_t layout_map[MAX_ELEM_ID*4][3];
800
int tags = 0;
801
802
if (get_bits1(gb)) { // frameLengthFlag
803
avpriv_request_sample(avctx, "960/120 MDCT window");
804
return AVERROR_PATCHWELCOME;
805
}
806
m4ac->frame_length_short = 0;
807
808
if (get_bits1(gb)) // dependsOnCoreCoder
809
skip_bits(gb, 14); // coreCoderDelay
810
extension_flag = get_bits1(gb);
811
812
if (m4ac->object_type == AOT_AAC_SCALABLE ||
813
m4ac->object_type == AOT_ER_AAC_SCALABLE)
814
skip_bits(gb, 3); // layerNr
815
816
if (channel_config == 0) {
817
skip_bits(gb, 4); // element_instance_tag
818
tags = decode_pce(avctx, m4ac, layout_map, gb);
819
if (tags < 0)
820
return tags;
821
} else {
822
if ((ret = set_default_channel_config(avctx, layout_map,
823
&tags, channel_config)))
824
return ret;
825
}
826
827
if (count_channels(layout_map, tags) > 1) {
828
m4ac->ps = 0;
829
} else if (m4ac->sbr == 1 && m4ac->ps == -1)
830
m4ac->ps = 1;
831
832
if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
833
return ret;
834
835
if (extension_flag) {
836
switch (m4ac->object_type) {
837
case AOT_ER_BSAC:
838
skip_bits(gb, 5); // numOfSubFrame
839
skip_bits(gb, 11); // layer_length
840
break;
841
case AOT_ER_AAC_LC:
842
case AOT_ER_AAC_LTP:
843
case AOT_ER_AAC_SCALABLE:
844
case AOT_ER_AAC_LD:
845
res_flags = get_bits(gb, 3);
846
if (res_flags) {
847
avpriv_report_missing_feature(avctx,
848
"AAC data resilience (flags %x)",
849
res_flags);
850
return AVERROR_PATCHWELCOME;
851
}
852
break;
853
}
854
skip_bits1(gb); // extensionFlag3 (TBD in version 3)
855
}
856
switch (m4ac->object_type) {
857
case AOT_ER_AAC_LC:
858
case AOT_ER_AAC_LTP:
859
case AOT_ER_AAC_SCALABLE:
860
case AOT_ER_AAC_LD:
861
ep_config = get_bits(gb, 2);
862
if (ep_config) {
863
avpriv_report_missing_feature(avctx,
864
"epConfig %d", ep_config);
865
return AVERROR_PATCHWELCOME;
866
}
867
}
868
return 0;
869
}
870
871
static int decode_eld_specific_config(AACContext *ac, AVCodecContext *avctx,
872
GetBitContext *gb,
873
MPEG4AudioConfig *m4ac,
874
int channel_config)
875
{
876
int ret, ep_config, res_flags;
877
uint8_t layout_map[MAX_ELEM_ID*4][3];
878
int tags = 0;
879
const int ELDEXT_TERM = 0;
880
881
m4ac->ps = 0;
882
m4ac->sbr = 0;
883
#if USE_FIXED
884
if (get_bits1(gb)) { // frameLengthFlag
885
avpriv_request_sample(avctx, "960/120 MDCT window");
886
return AVERROR_PATCHWELCOME;
887
}
888
#else
889
m4ac->frame_length_short = get_bits1(gb);
890
#endif
891
res_flags = get_bits(gb, 3);
892
if (res_flags) {
893
avpriv_report_missing_feature(avctx,
894
"AAC data resilience (flags %x)",
895
res_flags);
896
return AVERROR_PATCHWELCOME;
897
}
898
899
if (get_bits1(gb)) { // ldSbrPresentFlag
900
avpriv_report_missing_feature(avctx,
901
"Low Delay SBR");
902
return AVERROR_PATCHWELCOME;
903
}
904
905
while (get_bits(gb, 4) != ELDEXT_TERM) {
906
int len = get_bits(gb, 4);
907
if (len == 15)
908
len += get_bits(gb, 8);
909
if (len == 15 + 255)
910
len += get_bits(gb, 16);
911
if (get_bits_left(gb) < len * 8 + 4) {
912
av_log(avctx, AV_LOG_ERROR, overread_err);
913
return AVERROR_INVALIDDATA;
914
}
915
skip_bits_long(gb, 8 * len);
916
}
917
918
if ((ret = set_default_channel_config(avctx, layout_map,
919
&tags, channel_config)))
920
return ret;
921
922
if (ac && (ret = output_configure(ac, layout_map, tags, OC_GLOBAL_HDR, 0)))
923
return ret;
924
925
ep_config = get_bits(gb, 2);
926
if (ep_config) {
927
avpriv_report_missing_feature(avctx,
928
"epConfig %d", ep_config);
929
return AVERROR_PATCHWELCOME;
930
}
931
return 0;
932
}
933
934
/**
935
* Decode audio specific configuration; reference: table 1.13.
936
*
937
* @param ac pointer to AACContext, may be null
938
* @param avctx pointer to AVCCodecContext, used for logging
939
* @param m4ac pointer to MPEG4AudioConfig, used for parsing
940
* @param data pointer to buffer holding an audio specific config
941
* @param bit_size size of audio specific config or data in bits
942
* @param sync_extension look for an appended sync extension
943
*
944
* @return Returns error status or number of consumed bits. <0 - error
945
*/
946
static int decode_audio_specific_config(AACContext *ac,
947
AVCodecContext *avctx,
948
MPEG4AudioConfig *m4ac,
949
const uint8_t *data, int64_t bit_size,
950
int sync_extension)
951
{
952
GetBitContext gb;
953
int i, ret;
954
955
if (bit_size < 0 || bit_size > INT_MAX) {
956
av_log(avctx, AV_LOG_ERROR, "Audio specific config size is invalid\n");
957
return AVERROR_INVALIDDATA;
958
}
959
960
ff_dlog(avctx, "audio specific config size %d\n", (int)bit_size >> 3);
961
for (i = 0; i < bit_size >> 3; i++)
962
ff_dlog(avctx, "%02x ", data[i]);
963
ff_dlog(avctx, "\n");
964
965
if ((ret = init_get_bits(&gb, data, bit_size)) < 0)
966
return ret;
967
968
if ((i = avpriv_mpeg4audio_get_config(m4ac, data, bit_size,
969
sync_extension)) < 0)
970
return AVERROR_INVALIDDATA;
971
if (m4ac->sampling_index > 12) {
972
av_log(avctx, AV_LOG_ERROR,
973
"invalid sampling rate index %d\n",
974
m4ac->sampling_index);
975
return AVERROR_INVALIDDATA;
976
}
977
if (m4ac->object_type == AOT_ER_AAC_LD &&
978
(m4ac->sampling_index < 3 || m4ac->sampling_index > 7)) {
979
av_log(avctx, AV_LOG_ERROR,
980
"invalid low delay sampling rate index %d\n",
981
m4ac->sampling_index);
982
return AVERROR_INVALIDDATA;
983
}
984
985
skip_bits_long(&gb, i);
986
987
switch (m4ac->object_type) {
988
case AOT_AAC_MAIN:
989
case AOT_AAC_LC:
990
case AOT_AAC_LTP:
991
case AOT_ER_AAC_LC:
992
case AOT_ER_AAC_LD:
993
if ((ret = decode_ga_specific_config(ac, avctx, &gb,
994
m4ac, m4ac->chan_config)) < 0)
995
return ret;
996
break;
997
case AOT_ER_AAC_ELD:
998
if ((ret = decode_eld_specific_config(ac, avctx, &gb,
999
m4ac, m4ac->chan_config)) < 0)
1000
return ret;
1001
break;
1002
default:
1003
avpriv_report_missing_feature(avctx,
1004
"Audio object type %s%d",
1005
m4ac->sbr == 1 ? "SBR+" : "",
1006
m4ac->object_type);
1007
return AVERROR(ENOSYS);
1008
}
1009
1010
ff_dlog(avctx,
1011
"AOT %d chan config %d sampling index %d (%d) SBR %d PS %d\n",
1012
m4ac->object_type, m4ac->chan_config, m4ac->sampling_index,
1013
m4ac->sample_rate, m4ac->sbr,
1014
m4ac->ps);
1015
1016
return get_bits_count(&gb);
1017
}
1018
1019
/**
1020
* linear congruential pseudorandom number generator
1021
*
1022
* @param previous_val pointer to the current state of the generator
1023
*
1024
* @return Returns a 32-bit pseudorandom integer
1025
*/
1026
static av_always_inline int lcg_random(unsigned previous_val)
1027
{
1028
union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
1029
return v.s;
1030
}
1031
1032
static void reset_all_predictors(PredictorState *ps)
1033
{
1034
int i;
1035
for (i = 0; i < MAX_PREDICTORS; i++)
1036
reset_predict_state(&ps[i]);
1037
}
1038
1039
static int sample_rate_idx (int rate)
1040
{
1041
if (92017 <= rate) return 0;
1042
else if (75132 <= rate) return 1;
1043
else if (55426 <= rate) return 2;
1044
else if (46009 <= rate) return 3;
1045
else if (37566 <= rate) return 4;
1046
else if (27713 <= rate) return 5;
1047
else if (23004 <= rate) return 6;
1048
else if (18783 <= rate) return 7;
1049
else if (13856 <= rate) return 8;
1050
else if (11502 <= rate) return 9;
1051
else if (9391 <= rate) return 10;
1052
else return 11;
1053
}
1054
1055
static void reset_predictor_group(PredictorState *ps, int group_num)
1056
{
1057
int i;
1058
for (i = group_num - 1; i < MAX_PREDICTORS; i += 30)
1059
reset_predict_state(&ps[i]);
1060
}
1061
1062
#define AAC_INIT_VLC_STATIC(num, size) \
1063
INIT_VLC_STATIC(&vlc_spectral[num], 8, ff_aac_spectral_sizes[num], \
1064
ff_aac_spectral_bits[num], sizeof(ff_aac_spectral_bits[num][0]), \
1065
sizeof(ff_aac_spectral_bits[num][0]), \
1066
ff_aac_spectral_codes[num], sizeof(ff_aac_spectral_codes[num][0]), \
1067
sizeof(ff_aac_spectral_codes[num][0]), \
1068
size);
1069
1070
static void aacdec_init(AACContext *ac);
1071
1072
static av_cold void aac_static_table_init(void)
1073
{
1074
AAC_INIT_VLC_STATIC( 0, 304);
1075
AAC_INIT_VLC_STATIC( 1, 270);
1076
AAC_INIT_VLC_STATIC( 2, 550);
1077
AAC_INIT_VLC_STATIC( 3, 300);
1078
AAC_INIT_VLC_STATIC( 4, 328);
1079
AAC_INIT_VLC_STATIC( 5, 294);
1080
AAC_INIT_VLC_STATIC( 6, 306);
1081
AAC_INIT_VLC_STATIC( 7, 268);
1082
AAC_INIT_VLC_STATIC( 8, 510);
1083
AAC_INIT_VLC_STATIC( 9, 366);
1084
AAC_INIT_VLC_STATIC(10, 462);
1085
1086
AAC_RENAME(ff_aac_sbr_init)();
1087
1088
ff_aac_tableinit();
1089
1090
INIT_VLC_STATIC(&vlc_scalefactors, 7,
1091
FF_ARRAY_ELEMS(ff_aac_scalefactor_code),
1092
ff_aac_scalefactor_bits,
1093
sizeof(ff_aac_scalefactor_bits[0]),
1094
sizeof(ff_aac_scalefactor_bits[0]),
1095
ff_aac_scalefactor_code,
1096
sizeof(ff_aac_scalefactor_code[0]),
1097
sizeof(ff_aac_scalefactor_code[0]),
1098
352);
1099
1100
// window initialization
1101
AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_long_1024), 4.0, 1024);
1102
AAC_RENAME(ff_kbd_window_init)(AAC_RENAME(ff_aac_kbd_short_128), 6.0, 128);
1103
AAC_RENAME(ff_init_ff_sine_windows)(10);
1104
AAC_RENAME(ff_init_ff_sine_windows)( 9);
1105
AAC_RENAME(ff_init_ff_sine_windows)( 7);
1106
1107
AAC_RENAME(cbrt_tableinit)();
1108
}
1109
1110
static AVOnce aac_table_init = AV_ONCE_INIT;
1111
1112
static av_cold int aac_decode_init(AVCodecContext *avctx)
1113
{
1114
AACContext *ac = avctx->priv_data;
1115
int ret;
1116
1117
ret = ff_thread_once(&aac_table_init, &aac_static_table_init);
1118
if (ret != 0)
1119
return AVERROR_UNKNOWN;
1120
1121
ac->avctx = avctx;
1122
ac->oc[1].m4ac.sample_rate = avctx->sample_rate;
1123
1124
aacdec_init(ac);
1125
#if USE_FIXED
1126
avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
1127
#else
1128
avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1129
#endif /* USE_FIXED */
1130
1131
if (avctx->extradata_size > 0) {
1132
if ((ret = decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
1133
avctx->extradata,
1134
avctx->extradata_size * 8LL,
1135
1)) < 0)
1136
return ret;
1137
} else {
1138
int sr, i;
1139
uint8_t layout_map[MAX_ELEM_ID*4][3];
1140
int layout_map_tags;
1141
1142
sr = sample_rate_idx(avctx->sample_rate);
1143
ac->oc[1].m4ac.sampling_index = sr;
1144
ac->oc[1].m4ac.channels = avctx->channels;
1145
ac->oc[1].m4ac.sbr = -1;
1146
ac->oc[1].m4ac.ps = -1;
1147
1148
for (i = 0; i < FF_ARRAY_ELEMS(ff_mpeg4audio_channels); i++)
1149
if (ff_mpeg4audio_channels[i] == avctx->channels)
1150
break;
1151
if (i == FF_ARRAY_ELEMS(ff_mpeg4audio_channels)) {
1152
i = 0;
1153
}
1154
ac->oc[1].m4ac.chan_config = i;
1155
1156
if (ac->oc[1].m4ac.chan_config) {
1157
int ret = set_default_channel_config(avctx, layout_map,
1158
&layout_map_tags, ac->oc[1].m4ac.chan_config);
1159
if (!ret)
1160
output_configure(ac, layout_map, layout_map_tags,
1161
OC_GLOBAL_HDR, 0);
1162
else if (avctx->err_recognition & AV_EF_EXPLODE)
1163
return AVERROR_INVALIDDATA;
1164
}
1165
}
1166
1167
if (avctx->channels > MAX_CHANNELS) {
1168
av_log(avctx, AV_LOG_ERROR, "Too many channels\n");
1169
return AVERROR_INVALIDDATA;
1170
}
1171
1172
#if USE_FIXED
1173
ac->fdsp = avpriv_alloc_fixed_dsp(avctx->flags & AV_CODEC_FLAG_BITEXACT);
1174
#else
1175
ac->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
1176
#endif /* USE_FIXED */
1177
if (!ac->fdsp) {
1178
return AVERROR(ENOMEM);
1179
}
1180
1181
ac->random_state = 0x1f2e3d4c;
1182
1183
AAC_RENAME_32(ff_mdct_init)(&ac->mdct, 11, 1, 1.0 / RANGE15(1024.0));
1184
AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ld, 10, 1, 1.0 / RANGE15(512.0));
1185
AAC_RENAME_32(ff_mdct_init)(&ac->mdct_small, 8, 1, 1.0 / RANGE15(128.0));
1186
AAC_RENAME_32(ff_mdct_init)(&ac->mdct_ltp, 11, 0, RANGE15(-2.0));
1187
#if !USE_FIXED
1188
ret = ff_imdct15_init(&ac->mdct480, 5);
1189
if (ret < 0)
1190
return ret;
1191
#endif
1192
1193
return 0;
1194
}
1195
1196
/**
1197
* Skip data_stream_element; reference: table 4.10.
1198
*/
1199
static int skip_data_stream_element(AACContext *ac, GetBitContext *gb)
1200
{
1201
int byte_align = get_bits1(gb);
1202
int count = get_bits(gb, 8);
1203
if (count == 255)
1204
count += get_bits(gb, 8);
1205
if (byte_align)
1206
align_get_bits(gb);
1207
1208
if (get_bits_left(gb) < 8 * count) {
1209
av_log(ac->avctx, AV_LOG_ERROR, "skip_data_stream_element: "overread_err);
1210
return AVERROR_INVALIDDATA;
1211
}
1212
skip_bits_long(gb, 8 * count);
1213
return 0;
1214
}
1215
1216
static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
1217
GetBitContext *gb)
1218
{
1219
int sfb;
1220
if (get_bits1(gb)) {
1221
ics->predictor_reset_group = get_bits(gb, 5);
1222
if (ics->predictor_reset_group == 0 ||
1223
ics->predictor_reset_group > 30) {
1224
av_log(ac->avctx, AV_LOG_ERROR,
1225
"Invalid Predictor Reset Group.\n");
1226
return AVERROR_INVALIDDATA;
1227
}
1228
}
1229
for (sfb = 0; sfb < FFMIN(ics->max_sfb, ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index]); sfb++) {
1230
ics->prediction_used[sfb] = get_bits1(gb);
1231
}
1232
return 0;
1233
}
1234
1235
/**
1236
* Decode Long Term Prediction data; reference: table 4.xx.
1237
*/
1238
static void decode_ltp(LongTermPrediction *ltp,
1239
GetBitContext *gb, uint8_t max_sfb)
1240
{
1241
int sfb;
1242
1243
ltp->lag = get_bits(gb, 11);
1244
ltp->coef = ltp_coef[get_bits(gb, 3)];
1245
for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
1246
ltp->used[sfb] = get_bits1(gb);
1247
}
1248
1249
/**
1250
* Decode Individual Channel Stream info; reference: table 4.6.
1251
*/
1252
static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
1253
GetBitContext *gb)
1254
{
1255
const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
1256
const int aot = m4ac->object_type;
1257
const int sampling_index = m4ac->sampling_index;
1258
if (aot != AOT_ER_AAC_ELD) {
1259
if (get_bits1(gb)) {
1260
av_log(ac->avctx, AV_LOG_ERROR, "Reserved bit set.\n");
1261
if (ac->avctx->err_recognition & AV_EF_BITSTREAM)
1262
return AVERROR_INVALIDDATA;
1263
}
1264
ics->window_sequence[1] = ics->window_sequence[0];
1265
ics->window_sequence[0] = get_bits(gb, 2);
1266
if (aot == AOT_ER_AAC_LD &&
1267
ics->window_sequence[0] != ONLY_LONG_SEQUENCE) {
1268
av_log(ac->avctx, AV_LOG_ERROR,
1269
"AAC LD is only defined for ONLY_LONG_SEQUENCE but "
1270
"window sequence %d found.\n", ics->window_sequence[0]);
1271
ics->window_sequence[0] = ONLY_LONG_SEQUENCE;
1272
return AVERROR_INVALIDDATA;
1273
}
1274
ics->use_kb_window[1] = ics->use_kb_window[0];
1275
ics->use_kb_window[0] = get_bits1(gb);
1276
}
1277
ics->num_window_groups = 1;
1278
ics->group_len[0] = 1;
1279
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1280
int i;
1281
ics->max_sfb = get_bits(gb, 4);
1282
for (i = 0; i < 7; i++) {
1283
if (get_bits1(gb)) {
1284
ics->group_len[ics->num_window_groups - 1]++;
1285
} else {
1286
ics->num_window_groups++;
1287
ics->group_len[ics->num_window_groups - 1] = 1;
1288
}
1289
}
1290
ics->num_windows = 8;
1291
ics->swb_offset = ff_swb_offset_128[sampling_index];
1292
ics->num_swb = ff_aac_num_swb_128[sampling_index];
1293
ics->tns_max_bands = ff_tns_max_bands_128[sampling_index];
1294
ics->predictor_present = 0;
1295
} else {
1296
ics->max_sfb = get_bits(gb, 6);
1297
ics->num_windows = 1;
1298
if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD) {
1299
if (m4ac->frame_length_short) {
1300
ics->swb_offset = ff_swb_offset_480[sampling_index];
1301
ics->num_swb = ff_aac_num_swb_480[sampling_index];
1302
ics->tns_max_bands = ff_tns_max_bands_480[sampling_index];
1303
} else {
1304
ics->swb_offset = ff_swb_offset_512[sampling_index];
1305
ics->num_swb = ff_aac_num_swb_512[sampling_index];
1306
ics->tns_max_bands = ff_tns_max_bands_512[sampling_index];
1307
}
1308
if (!ics->num_swb || !ics->swb_offset)
1309
return AVERROR_BUG;
1310
} else {
1311
ics->swb_offset = ff_swb_offset_1024[sampling_index];
1312
ics->num_swb = ff_aac_num_swb_1024[sampling_index];
1313
ics->tns_max_bands = ff_tns_max_bands_1024[sampling_index];
1314
}
1315
if (aot != AOT_ER_AAC_ELD) {
1316
ics->predictor_present = get_bits1(gb);
1317
ics->predictor_reset_group = 0;
1318
}
1319
if (ics->predictor_present) {
1320
if (aot == AOT_AAC_MAIN) {
1321
if (decode_prediction(ac, ics, gb)) {
1322
goto fail;
1323
}
1324
} else if (aot == AOT_AAC_LC ||
1325
aot == AOT_ER_AAC_LC) {
1326
av_log(ac->avctx, AV_LOG_ERROR,
1327
"Prediction is not allowed in AAC-LC.\n");
1328
goto fail;
1329
} else {
1330
if (aot == AOT_ER_AAC_LD) {
1331
av_log(ac->avctx, AV_LOG_ERROR,
1332
"LTP in ER AAC LD not yet implemented.\n");
1333
return AVERROR_PATCHWELCOME;
1334
}
1335
if ((ics->ltp.present = get_bits(gb, 1)))
1336
decode_ltp(&ics->ltp, gb, ics->max_sfb);
1337
}
1338
}
1339
}
1340
1341
if (ics->max_sfb > ics->num_swb) {
1342
av_log(ac->avctx, AV_LOG_ERROR,
1343
"Number of scalefactor bands in group (%d) "
1344
"exceeds limit (%d).\n",
1345
ics->max_sfb, ics->num_swb);
1346
goto fail;
1347
}
1348
1349
return 0;
1350
fail:
1351
ics->max_sfb = 0;
1352
return AVERROR_INVALIDDATA;
1353
}
1354
1355
/**
1356
* Decode band types (section_data payload); reference: table 4.46.
1357
*
1358
* @param band_type array of the used band type
1359
* @param band_type_run_end array of the last scalefactor band of a band type run
1360
*
1361
* @return Returns error status. 0 - OK, !0 - error
1362
*/
1363
static int decode_band_types(AACContext *ac, enum BandType band_type[120],
1364
int band_type_run_end[120], GetBitContext *gb,
1365
IndividualChannelStream *ics)
1366
{
1367
int g, idx = 0;
1368
const int bits = (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) ? 3 : 5;
1369
for (g = 0; g < ics->num_window_groups; g++) {
1370
int k = 0;
1371
while (k < ics->max_sfb) {
1372
uint8_t sect_end = k;
1373
int sect_len_incr;
1374
int sect_band_type = get_bits(gb, 4);
1375
if (sect_band_type == 12) {
1376
av_log(ac->avctx, AV_LOG_ERROR, "invalid band type\n");
1377
return AVERROR_INVALIDDATA;
1378
}
1379
do {
1380
sect_len_incr = get_bits(gb, bits);
1381
sect_end += sect_len_incr;
1382
if (get_bits_left(gb) < 0) {
1383
av_log(ac->avctx, AV_LOG_ERROR, "decode_band_types: "overread_err);
1384
return AVERROR_INVALIDDATA;
1385
}
1386
if (sect_end > ics->max_sfb) {
1387
av_log(ac->avctx, AV_LOG_ERROR,
1388
"Number of bands (%d) exceeds limit (%d).\n",
1389
sect_end, ics->max_sfb);
1390
return AVERROR_INVALIDDATA;
1391
}
1392
} while (sect_len_incr == (1 << bits) - 1);
1393
for (; k < sect_end; k++) {
1394
band_type [idx] = sect_band_type;
1395
band_type_run_end[idx++] = sect_end;
1396
}
1397
}
1398
}
1399
return 0;
1400
}
1401
1402
/**
1403
* Decode scalefactors; reference: table 4.47.
1404
*
1405
* @param global_gain first scalefactor value as scalefactors are differentially coded
1406
* @param band_type array of the used band type
1407
* @param band_type_run_end array of the last scalefactor band of a band type run
1408
* @param sf array of scalefactors or intensity stereo positions
1409
*
1410
* @return Returns error status. 0 - OK, !0 - error
1411
*/
1412
static int decode_scalefactors(AACContext *ac, INTFLOAT sf[120], GetBitContext *gb,
1413
unsigned int global_gain,
1414
IndividualChannelStream *ics,
1415
enum BandType band_type[120],
1416
int band_type_run_end[120])
1417
{
1418
int g, i, idx = 0;
1419
int offset[3] = { global_gain, global_gain - NOISE_OFFSET, 0 };
1420
int clipped_offset;
1421
int noise_flag = 1;
1422
for (g = 0; g < ics->num_window_groups; g++) {
1423
for (i = 0; i < ics->max_sfb;) {
1424
int run_end = band_type_run_end[idx];
1425
if (band_type[idx] == ZERO_BT) {
1426
for (; i < run_end; i++, idx++)
1427
sf[idx] = FIXR(0.);
1428
} else if ((band_type[idx] == INTENSITY_BT) ||
1429
(band_type[idx] == INTENSITY_BT2)) {
1430
for (; i < run_end; i++, idx++) {
1431
offset[2] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
1432
clipped_offset = av_clip(offset[2], -155, 100);
1433
if (offset[2] != clipped_offset) {
1434
avpriv_request_sample(ac->avctx,
1435
"If you heard an audible artifact, there may be a bug in the decoder. "
1436
"Clipped intensity stereo position (%d -> %d)",
1437
offset[2], clipped_offset);
1438
}
1439
#if USE_FIXED
1440
sf[idx] = 100 - clipped_offset;
1441
#else
1442
sf[idx] = ff_aac_pow2sf_tab[-clipped_offset + POW_SF2_ZERO];
1443
#endif /* USE_FIXED */
1444
}
1445
} else if (band_type[idx] == NOISE_BT) {
1446
for (; i < run_end; i++, idx++) {
1447
if (noise_flag-- > 0)
1448
offset[1] += get_bits(gb, NOISE_PRE_BITS) - NOISE_PRE;
1449
else
1450
offset[1] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
1451
clipped_offset = av_clip(offset[1], -100, 155);
1452
if (offset[1] != clipped_offset) {
1453
avpriv_request_sample(ac->avctx,
1454
"If you heard an audible artifact, there may be a bug in the decoder. "
1455
"Clipped noise gain (%d -> %d)",
1456
offset[1], clipped_offset);
1457
}
1458
#if USE_FIXED
1459
sf[idx] = -(100 + clipped_offset);
1460
#else
1461
sf[idx] = -ff_aac_pow2sf_tab[clipped_offset + POW_SF2_ZERO];
1462
#endif /* USE_FIXED */
1463
}
1464
} else {
1465
for (; i < run_end; i++, idx++) {
1466
offset[0] += get_vlc2(gb, vlc_scalefactors.table, 7, 3) - SCALE_DIFF_ZERO;
1467
if (offset[0] > 255U) {
1468
av_log(ac->avctx, AV_LOG_ERROR,
1469
"Scalefactor (%d) out of range.\n", offset[0]);
1470
return AVERROR_INVALIDDATA;
1471
}
1472
#if USE_FIXED
1473
sf[idx] = -offset[0];
1474
#else
1475
sf[idx] = -ff_aac_pow2sf_tab[offset[0] - 100 + POW_SF2_ZERO];
1476
#endif /* USE_FIXED */
1477
}
1478
}
1479
}
1480
}
1481
return 0;
1482
}
1483
1484
/**
1485
* Decode pulse data; reference: table 4.7.
1486
*/
1487
static int decode_pulses(Pulse *pulse, GetBitContext *gb,
1488
const uint16_t *swb_offset, int num_swb)
1489
{
1490
int i, pulse_swb;
1491
pulse->num_pulse = get_bits(gb, 2) + 1;
1492
pulse_swb = get_bits(gb, 6);
1493
if (pulse_swb >= num_swb)
1494
return -1;
1495
pulse->pos[0] = swb_offset[pulse_swb];
1496
pulse->pos[0] += get_bits(gb, 5);
1497
if (pulse->pos[0] >= swb_offset[num_swb])
1498
return -1;
1499
pulse->amp[0] = get_bits(gb, 4);
1500
for (i = 1; i < pulse->num_pulse; i++) {
1501
pulse->pos[i] = get_bits(gb, 5) + pulse->pos[i - 1];
1502
if (pulse->pos[i] >= swb_offset[num_swb])
1503
return -1;
1504
pulse->amp[i] = get_bits(gb, 4);
1505
}
1506
return 0;
1507
}
1508
1509
/**
1510
* Decode Temporal Noise Shaping data; reference: table 4.48.
1511
*
1512
* @return Returns error status. 0 - OK, !0 - error
1513
*/
1514
static int decode_tns(AACContext *ac, TemporalNoiseShaping *tns,
1515
GetBitContext *gb, const IndividualChannelStream *ics)
1516
{
1517
int w, filt, i, coef_len, coef_res, coef_compress;
1518
const int is8 = ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1519
const int tns_max_order = is8 ? 7 : ac->oc[1].m4ac.object_type == AOT_AAC_MAIN ? 20 : 12;
1520
for (w = 0; w < ics->num_windows; w++) {
1521
if ((tns->n_filt[w] = get_bits(gb, 2 - is8))) {
1522
coef_res = get_bits1(gb);
1523
1524
for (filt = 0; filt < tns->n_filt[w]; filt++) {
1525
int tmp2_idx;
1526
tns->length[w][filt] = get_bits(gb, 6 - 2 * is8);
1527
1528
if ((tns->order[w][filt] = get_bits(gb, 5 - 2 * is8)) > tns_max_order) {
1529
av_log(ac->avctx, AV_LOG_ERROR,
1530
"TNS filter order %d is greater than maximum %d.\n",
1531
tns->order[w][filt], tns_max_order);
1532
tns->order[w][filt] = 0;
1533
return AVERROR_INVALIDDATA;
1534
}
1535
if (tns->order[w][filt]) {
1536
tns->direction[w][filt] = get_bits1(gb);
1537
coef_compress = get_bits1(gb);
1538
coef_len = coef_res + 3 - coef_compress;
1539
tmp2_idx = 2 * coef_compress + coef_res;
1540
1541
for (i = 0; i < tns->order[w][filt]; i++)
1542
tns->coef[w][filt][i] = tns_tmp2_map[tmp2_idx][get_bits(gb, coef_len)];
1543
}
1544
}
1545
}
1546
}
1547
return 0;
1548
}
1549
1550
/**
1551
* Decode Mid/Side data; reference: table 4.54.
1552
*
1553
* @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
1554
* [1] mask is decoded from bitstream; [2] mask is all 1s;
1555
* [3] reserved for scalable AAC
1556
*/
1557
static void decode_mid_side_stereo(ChannelElement *cpe, GetBitContext *gb,
1558
int ms_present)
1559
{
1560
int idx;
1561
int max_idx = cpe->ch[0].ics.num_window_groups * cpe->ch[0].ics.max_sfb;
1562
if (ms_present == 1) {
1563
for (idx = 0; idx < max_idx; idx++)
1564
cpe->ms_mask[idx] = get_bits1(gb);
1565
} else if (ms_present == 2) {
1566
memset(cpe->ms_mask, 1, max_idx * sizeof(cpe->ms_mask[0]));
1567
}
1568
}
1569
1570
/**
1571
* Decode spectral data; reference: table 4.50.
1572
* Dequantize and scale spectral data; reference: 4.6.3.3.
1573
*
1574
* @param coef array of dequantized, scaled spectral data
1575
* @param sf array of scalefactors or intensity stereo positions
1576
* @param pulse_present set if pulses are present
1577
* @param pulse pointer to pulse data struct
1578
* @param band_type array of the used band type
1579
*
1580
* @return Returns error status. 0 - OK, !0 - error
1581
*/
1582
static int decode_spectrum_and_dequant(AACContext *ac, INTFLOAT coef[1024],
1583
GetBitContext *gb, const INTFLOAT sf[120],
1584
int pulse_present, const Pulse *pulse,
1585
const IndividualChannelStream *ics,
1586
enum BandType band_type[120])
1587
{
1588
int i, k, g, idx = 0;
1589
const int c = 1024 / ics->num_windows;
1590
const uint16_t *offsets = ics->swb_offset;
1591
INTFLOAT *coef_base = coef;
1592
1593
for (g = 0; g < ics->num_windows; g++)
1594
memset(coef + g * 128 + offsets[ics->max_sfb], 0,
1595
sizeof(INTFLOAT) * (c - offsets[ics->max_sfb]));
1596
1597
for (g = 0; g < ics->num_window_groups; g++) {
1598
unsigned g_len = ics->group_len[g];
1599
1600
for (i = 0; i < ics->max_sfb; i++, idx++) {
1601
const unsigned cbt_m1 = band_type[idx] - 1;
1602
INTFLOAT *cfo = coef + offsets[i];
1603
int off_len = offsets[i + 1] - offsets[i];
1604
int group;
1605
1606
if (cbt_m1 >= INTENSITY_BT2 - 1) {
1607
for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1608
memset(cfo, 0, off_len * sizeof(*cfo));
1609
}
1610
} else if (cbt_m1 == NOISE_BT - 1) {
1611
for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1612
#if !USE_FIXED
1613
float scale;
1614
#endif /* !USE_FIXED */
1615
INTFLOAT band_energy;
1616
1617
for (k = 0; k < off_len; k++) {
1618
ac->random_state = lcg_random(ac->random_state);
1619
#if USE_FIXED
1620
cfo[k] = ac->random_state >> 3;
1621
#else
1622
cfo[k] = ac->random_state;
1623
#endif /* USE_FIXED */
1624
}
1625
1626
#if USE_FIXED
1627
band_energy = ac->fdsp->scalarproduct_fixed(cfo, cfo, off_len);
1628
band_energy = fixed_sqrt(band_energy, 31);
1629
noise_scale(cfo, sf[idx], band_energy, off_len);
1630
#else
1631
band_energy = ac->fdsp->scalarproduct_float(cfo, cfo, off_len);
1632
scale = sf[idx] / sqrtf(band_energy);
1633
ac->fdsp->vector_fmul_scalar(cfo, cfo, scale, off_len);
1634
#endif /* USE_FIXED */
1635
}
1636
} else {
1637
#if !USE_FIXED
1638
const float *vq = ff_aac_codebook_vector_vals[cbt_m1];
1639
#endif /* !USE_FIXED */
1640
const uint16_t *cb_vector_idx = ff_aac_codebook_vector_idx[cbt_m1];
1641
VLC_TYPE (*vlc_tab)[2] = vlc_spectral[cbt_m1].table;
1642
OPEN_READER(re, gb);
1643
1644
switch (cbt_m1 >> 1) {
1645
case 0:
1646
for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1647
INTFLOAT *cf = cfo;
1648
int len = off_len;
1649
1650
do {
1651
int code;
1652
unsigned cb_idx;
1653
1654
UPDATE_CACHE(re, gb);
1655
GET_VLC(code, re, gb, vlc_tab, 8, 2);
1656
cb_idx = cb_vector_idx[code];
1657
#if USE_FIXED
1658
cf = DEC_SQUAD(cf, cb_idx);
1659
#else
1660
cf = VMUL4(cf, vq, cb_idx, sf + idx);
1661
#endif /* USE_FIXED */
1662
} while (len -= 4);
1663
}
1664
break;
1665
1666
case 1:
1667
for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1668
INTFLOAT *cf = cfo;
1669
int len = off_len;
1670
1671
do {
1672
int code;
1673
unsigned nnz;
1674
unsigned cb_idx;
1675
uint32_t bits;
1676
1677
UPDATE_CACHE(re, gb);
1678
GET_VLC(code, re, gb, vlc_tab, 8, 2);
1679
cb_idx = cb_vector_idx[code];
1680
nnz = cb_idx >> 8 & 15;
1681
bits = nnz ? GET_CACHE(re, gb) : 0;
1682
LAST_SKIP_BITS(re, gb, nnz);
1683
#if USE_FIXED
1684
cf = DEC_UQUAD(cf, cb_idx, bits);
1685
#else
1686
cf = VMUL4S(cf, vq, cb_idx, bits, sf + idx);
1687
#endif /* USE_FIXED */
1688
} while (len -= 4);
1689
}
1690
break;
1691
1692
case 2:
1693
for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1694
INTFLOAT *cf = cfo;
1695
int len = off_len;
1696
1697
do {
1698
int code;
1699
unsigned cb_idx;
1700
1701
UPDATE_CACHE(re, gb);
1702
GET_VLC(code, re, gb, vlc_tab, 8, 2);
1703
cb_idx = cb_vector_idx[code];
1704
#if USE_FIXED
1705
cf = DEC_SPAIR(cf, cb_idx);
1706
#else
1707
cf = VMUL2(cf, vq, cb_idx, sf + idx);
1708
#endif /* USE_FIXED */
1709
} while (len -= 2);
1710
}
1711
break;
1712
1713
case 3:
1714
case 4:
1715
for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1716
INTFLOAT *cf = cfo;
1717
int len = off_len;
1718
1719
do {
1720
int code;
1721
unsigned nnz;
1722
unsigned cb_idx;
1723
unsigned sign;
1724
1725
UPDATE_CACHE(re, gb);
1726
GET_VLC(code, re, gb, vlc_tab, 8, 2);
1727
cb_idx = cb_vector_idx[code];
1728
nnz = cb_idx >> 8 & 15;
1729
sign = nnz ? SHOW_UBITS(re, gb, nnz) << (cb_idx >> 12) : 0;
1730
LAST_SKIP_BITS(re, gb, nnz);
1731
#if USE_FIXED
1732
cf = DEC_UPAIR(cf, cb_idx, sign);
1733
#else
1734
cf = VMUL2S(cf, vq, cb_idx, sign, sf + idx);
1735
#endif /* USE_FIXED */
1736
} while (len -= 2);
1737
}
1738
break;
1739
1740
default:
1741
for (group = 0; group < (AAC_SIGNE)g_len; group++, cfo+=128) {
1742
#if USE_FIXED
1743
int *icf = cfo;
1744
int v;
1745
#else
1746
float *cf = cfo;
1747
uint32_t *icf = (uint32_t *) cf;
1748
#endif /* USE_FIXED */
1749
int len = off_len;
1750
1751
do {
1752
int code;
1753
unsigned nzt, nnz;
1754
unsigned cb_idx;
1755
uint32_t bits;
1756
int j;
1757
1758
UPDATE_CACHE(re, gb);
1759
GET_VLC(code, re, gb, vlc_tab, 8, 2);
1760
1761
if (!code) {
1762
*icf++ = 0;
1763
*icf++ = 0;
1764
continue;
1765
}
1766
1767
cb_idx = cb_vector_idx[code];
1768
nnz = cb_idx >> 12;
1769
nzt = cb_idx >> 8;
1770
bits = SHOW_UBITS(re, gb, nnz) << (32-nnz);
1771
LAST_SKIP_BITS(re, gb, nnz);
1772
1773
for (j = 0; j < 2; j++) {
1774
if (nzt & 1<<j) {
1775
uint32_t b;
1776
int n;
1777
/* The total length of escape_sequence must be < 22 bits according
1778
to the specification (i.e. max is 111111110xxxxxxxxxxxx). */
1779
UPDATE_CACHE(re, gb);
1780
b = GET_CACHE(re, gb);
1781
b = 31 - av_log2(~b);
1782
1783
if (b > 8) {
1784
av_log(ac->avctx, AV_LOG_ERROR, "error in spectral data, ESC overflow\n");
1785
return AVERROR_INVALIDDATA;
1786
}
1787
1788
SKIP_BITS(re, gb, b + 1);
1789
b += 4;
1790
n = (1 << b) + SHOW_UBITS(re, gb, b);
1791
LAST_SKIP_BITS(re, gb, b);
1792
#if USE_FIXED
1793
v = n;
1794
if (bits & 1U<<31)
1795
v = -v;
1796
*icf++ = v;
1797
#else
1798
*icf++ = cbrt_tab[n] | (bits & 1U<<31);
1799
#endif /* USE_FIXED */
1800
bits <<= 1;
1801
} else {
1802
#if USE_FIXED
1803
v = cb_idx & 15;
1804
if (bits & 1U<<31)
1805
v = -v;
1806
*icf++ = v;
1807
#else
1808
unsigned v = ((const uint32_t*)vq)[cb_idx & 15];
1809
*icf++ = (bits & 1U<<31) | v;
1810
#endif /* USE_FIXED */
1811
bits <<= !!v;
1812
}
1813
cb_idx >>= 4;
1814
}
1815
} while (len -= 2);
1816
#if !USE_FIXED
1817
ac->fdsp->vector_fmul_scalar(cfo, cfo, sf[idx], off_len);
1818
#endif /* !USE_FIXED */
1819
}
1820
}
1821
1822
CLOSE_READER(re, gb);
1823
}
1824
}
1825
coef += g_len << 7;
1826
}
1827
1828
if (pulse_present) {
1829
idx = 0;
1830
for (i = 0; i < pulse->num_pulse; i++) {
1831
INTFLOAT co = coef_base[ pulse->pos[i] ];
1832
while (offsets[idx + 1] <= pulse->pos[i])
1833
idx++;
1834
if (band_type[idx] != NOISE_BT && sf[idx]) {
1835
INTFLOAT ico = -pulse->amp[i];
1836
#if USE_FIXED
1837
if (co) {
1838
ico = co + (co > 0 ? -ico : ico);
1839
}
1840
coef_base[ pulse->pos[i] ] = ico;
1841
#else
1842
if (co) {
1843
co /= sf[idx];
1844
ico = co / sqrtf(sqrtf(fabsf(co))) + (co > 0 ? -ico : ico);
1845
}
1846
coef_base[ pulse->pos[i] ] = cbrtf(fabsf(ico)) * ico * sf[idx];
1847
#endif /* USE_FIXED */
1848
}
1849
}
1850
}
1851
#if USE_FIXED
1852
coef = coef_base;
1853
idx = 0;
1854
for (g = 0; g < ics->num_window_groups; g++) {
1855
unsigned g_len = ics->group_len[g];
1856
1857
for (i = 0; i < ics->max_sfb; i++, idx++) {
1858
const unsigned cbt_m1 = band_type[idx] - 1;
1859
int *cfo = coef + offsets[i];
1860
int off_len = offsets[i + 1] - offsets[i];
1861
int group;
1862
1863
if (cbt_m1 < NOISE_BT - 1) {
1864
for (group = 0; group < (int)g_len; group++, cfo+=128) {
1865
ac->vector_pow43(cfo, off_len);
1866
ac->subband_scale(cfo, cfo, sf[idx], 34, off_len);
1867
}
1868
}
1869
}
1870
coef += g_len << 7;
1871
}
1872
#endif /* USE_FIXED */
1873
return 0;
1874
}
1875
1876
/**
1877
* Apply AAC-Main style frequency domain prediction.
1878
*/
1879
static void apply_prediction(AACContext *ac, SingleChannelElement *sce)
1880
{
1881
int sfb, k;
1882
1883
if (!sce->ics.predictor_initialized) {
1884
reset_all_predictors(sce->predictor_state);
1885
sce->ics.predictor_initialized = 1;
1886
}
1887
1888
if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
1889
for (sfb = 0;
1890
sfb < ff_aac_pred_sfb_max[ac->oc[1].m4ac.sampling_index];
1891
sfb++) {
1892
for (k = sce->ics.swb_offset[sfb];
1893
k < sce->ics.swb_offset[sfb + 1];
1894
k++) {
1895
predict(&sce->predictor_state[k], &sce->coeffs[k],
1896
sce->ics.predictor_present &&
1897
sce->ics.prediction_used[sfb]);
1898
}
1899
}
1900
if (sce->ics.predictor_reset_group)
1901
reset_predictor_group(sce->predictor_state,
1902
sce->ics.predictor_reset_group);
1903
} else
1904
reset_all_predictors(sce->predictor_state);
1905
}
1906
1907
/**
1908
* Decode an individual_channel_stream payload; reference: table 4.44.
1909
*
1910
* @param common_window Channels have independent [0], or shared [1], Individual Channel Stream information.
1911
* @param scale_flag scalable [1] or non-scalable [0] AAC (Unused until scalable AAC is implemented.)
1912
*
1913
* @return Returns error status. 0 - OK, !0 - error
1914
*/
1915
static int decode_ics(AACContext *ac, SingleChannelElement *sce,
1916
GetBitContext *gb, int common_window, int scale_flag)
1917
{
1918
Pulse pulse;
1919
TemporalNoiseShaping *tns = &sce->tns;
1920
IndividualChannelStream *ics = &sce->ics;
1921
INTFLOAT *out = sce->coeffs;
1922
int global_gain, eld_syntax, er_syntax, pulse_present = 0;
1923
int ret;
1924
1925
eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1926
er_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_LC ||
1927
ac->oc[1].m4ac.object_type == AOT_ER_AAC_LTP ||
1928
ac->oc[1].m4ac.object_type == AOT_ER_AAC_LD ||
1929
ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
1930
1931
/* This assignment is to silence a GCC warning about the variable being used
1932
* uninitialized when in fact it always is.
1933
*/
1934
pulse.num_pulse = 0;
1935
1936
global_gain = get_bits(gb, 8);
1937
1938
if (!common_window && !scale_flag) {
1939
if (decode_ics_info(ac, ics, gb) < 0)
1940
return AVERROR_INVALIDDATA;
1941
}
1942
1943
if ((ret = decode_band_types(ac, sce->band_type,
1944
sce->band_type_run_end, gb, ics)) < 0)
1945
return ret;
1946
if ((ret = decode_scalefactors(ac, sce->sf, gb, global_gain, ics,
1947
sce->band_type, sce->band_type_run_end)) < 0)
1948
return ret;
1949
1950
pulse_present = 0;
1951
if (!scale_flag) {
1952
if (!eld_syntax && (pulse_present = get_bits1(gb))) {
1953
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1954
av_log(ac->avctx, AV_LOG_ERROR,
1955
"Pulse tool not allowed in eight short sequence.\n");
1956
return AVERROR_INVALIDDATA;
1957
}
1958
if (decode_pulses(&pulse, gb, ics->swb_offset, ics->num_swb)) {
1959
av_log(ac->avctx, AV_LOG_ERROR,
1960
"Pulse data corrupt or invalid.\n");
1961
return AVERROR_INVALIDDATA;
1962
}
1963
}
1964
tns->present = get_bits1(gb);
1965
if (tns->present && !er_syntax)
1966
if (decode_tns(ac, tns, gb, ics) < 0)
1967
return AVERROR_INVALIDDATA;
1968
if (!eld_syntax && get_bits1(gb)) {
1969
avpriv_request_sample(ac->avctx, "SSR");
1970
return AVERROR_PATCHWELCOME;
1971
}
1972
// I see no textual basis in the spec for this occurring after SSR gain
1973
// control, but this is what both reference and real implmentations do
1974
if (tns->present && er_syntax)
1975
if (decode_tns(ac, tns, gb, ics) < 0)
1976
return AVERROR_INVALIDDATA;
1977
}
1978
1979
if (decode_spectrum_and_dequant(ac, out, gb, sce->sf, pulse_present,
1980
&pulse, ics, sce->band_type) < 0)
1981
return AVERROR_INVALIDDATA;
1982
1983
if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN && !common_window)
1984
apply_prediction(ac, sce);
1985
1986
return 0;
1987
}
1988
1989
/**
1990
* Mid/Side stereo decoding; reference: 4.6.8.1.3.
1991
*/
1992
static void apply_mid_side_stereo(AACContext *ac, ChannelElement *cpe)
1993
{
1994
const IndividualChannelStream *ics = &cpe->ch[0].ics;
1995
INTFLOAT *ch0 = cpe->ch[0].coeffs;
1996
INTFLOAT *ch1 = cpe->ch[1].coeffs;
1997
int g, i, group, idx = 0;
1998
const uint16_t *offsets = ics->swb_offset;
1999
for (g = 0; g < ics->num_window_groups; g++) {
2000
for (i = 0; i < ics->max_sfb; i++, idx++) {
2001
if (cpe->ms_mask[idx] &&
2002
cpe->ch[0].band_type[idx] < NOISE_BT &&
2003
cpe->ch[1].band_type[idx] < NOISE_BT) {
2004
#if USE_FIXED
2005
for (group = 0; group < ics->group_len[g]; group++) {
2006
ac->fdsp->butterflies_fixed(ch0 + group * 128 + offsets[i],
2007
ch1 + group * 128 + offsets[i],
2008
offsets[i+1] - offsets[i]);
2009
#else
2010
for (group = 0; group < ics->group_len[g]; group++) {
2011
ac->fdsp->butterflies_float(ch0 + group * 128 + offsets[i],
2012
ch1 + group * 128 + offsets[i],
2013
offsets[i+1] - offsets[i]);
2014
#endif /* USE_FIXED */
2015
}
2016
}
2017
}
2018
ch0 += ics->group_len[g] * 128;
2019
ch1 += ics->group_len[g] * 128;
2020
}
2021
}
2022
2023
/**
2024
* intensity stereo decoding; reference: 4.6.8.2.3
2025
*
2026
* @param ms_present Indicates mid/side stereo presence. [0] mask is all 0s;
2027
* [1] mask is decoded from bitstream; [2] mask is all 1s;
2028
* [3] reserved for scalable AAC
2029
*/
2030
static void apply_intensity_stereo(AACContext *ac,
2031
ChannelElement *cpe, int ms_present)
2032
{
2033
const IndividualChannelStream *ics = &cpe->ch[1].ics;
2034
SingleChannelElement *sce1 = &cpe->ch[1];
2035
INTFLOAT *coef0 = cpe->ch[0].coeffs, *coef1 = cpe->ch[1].coeffs;
2036
const uint16_t *offsets = ics->swb_offset;
2037
int g, group, i, idx = 0;
2038
int c;
2039
INTFLOAT scale;
2040
for (g = 0; g < ics->num_window_groups; g++) {
2041
for (i = 0; i < ics->max_sfb;) {
2042
if (sce1->band_type[idx] == INTENSITY_BT ||
2043
sce1->band_type[idx] == INTENSITY_BT2) {
2044
const int bt_run_end = sce1->band_type_run_end[idx];
2045
for (; i < bt_run_end; i++, idx++) {
2046
c = -1 + 2 * (sce1->band_type[idx] - 14);
2047
if (ms_present)
2048
c *= 1 - 2 * cpe->ms_mask[idx];
2049
scale = c * sce1->sf[idx];
2050
for (group = 0; group < ics->group_len[g]; group++)
2051
#if USE_FIXED
2052
ac->subband_scale(coef1 + group * 128 + offsets[i],
2053
coef0 + group * 128 + offsets[i],
2054
scale,
2055
23,
2056
offsets[i + 1] - offsets[i]);
2057
#else
2058
ac->fdsp->vector_fmul_scalar(coef1 + group * 128 + offsets[i],
2059
coef0 + group * 128 + offsets[i],
2060
scale,
2061
offsets[i + 1] - offsets[i]);
2062
#endif /* USE_FIXED */
2063
}
2064
} else {
2065
int bt_run_end = sce1->band_type_run_end[idx];
2066
idx += bt_run_end - i;
2067
i = bt_run_end;
2068
}
2069
}
2070
coef0 += ics->group_len[g] * 128;
2071
coef1 += ics->group_len[g] * 128;
2072
}
2073
}
2074
2075
/**
2076
* Decode a channel_pair_element; reference: table 4.4.
2077
*
2078
* @return Returns error status. 0 - OK, !0 - error
2079
*/
2080
static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
2081
{
2082
int i, ret, common_window, ms_present = 0;
2083
int eld_syntax = ac->oc[1].m4ac.object_type == AOT_ER_AAC_ELD;
2084
2085
common_window = eld_syntax || get_bits1(gb);
2086
if (common_window) {
2087
if (decode_ics_info(ac, &cpe->ch[0].ics, gb))
2088
return AVERROR_INVALIDDATA;
2089
i = cpe->ch[1].ics.use_kb_window[0];
2090
cpe->ch[1].ics = cpe->ch[0].ics;
2091
cpe->ch[1].ics.use_kb_window[1] = i;
2092
if (cpe->ch[1].ics.predictor_present &&
2093
(ac->oc[1].m4ac.object_type != AOT_AAC_MAIN))
2094
if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
2095
decode_ltp(&cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
2096
ms_present = get_bits(gb, 2);
2097
if (ms_present == 3) {
2098
av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
2099
return AVERROR_INVALIDDATA;
2100
} else if (ms_present)
2101
decode_mid_side_stereo(cpe, gb, ms_present);
2102
}
2103
if ((ret = decode_ics(ac, &cpe->ch[0], gb, common_window, 0)))
2104
return ret;
2105
if ((ret = decode_ics(ac, &cpe->ch[1], gb, common_window, 0)))
2106
return ret;
2107
2108
if (common_window) {
2109
if (ms_present)
2110
apply_mid_side_stereo(ac, cpe);
2111
if (ac->oc[1].m4ac.object_type == AOT_AAC_MAIN) {
2112
apply_prediction(ac, &cpe->ch[0]);
2113
apply_prediction(ac, &cpe->ch[1]);
2114
}
2115
}
2116
2117
apply_intensity_stereo(ac, cpe, ms_present);
2118
return 0;
2119
}
2120
2121
static const float cce_scale[] = {
2122
1.09050773266525765921, //2^(1/8)
2123
1.18920711500272106672, //2^(1/4)
2124
M_SQRT2,
2125
2,
2126
};
2127
2128
/**
2129
* Decode coupling_channel_element; reference: table 4.8.
2130
*
2131
* @return Returns error status. 0 - OK, !0 - error
2132
*/
2133
static int decode_cce(AACContext *ac, GetBitContext *gb, ChannelElement *che)
2134
{
2135
int num_gain = 0;
2136
int c, g, sfb, ret;
2137
int sign;
2138
INTFLOAT scale;
2139
SingleChannelElement *sce = &che->ch[0];
2140
ChannelCoupling *coup = &che->coup;
2141
2142
coup->coupling_point = 2 * get_bits1(gb);
2143
coup->num_coupled = get_bits(gb, 3);
2144
for (c = 0; c <= coup->num_coupled; c++) {
2145
num_gain++;
2146
coup->type[c] = get_bits1(gb) ? TYPE_CPE : TYPE_SCE;
2147
coup->id_select[c] = get_bits(gb, 4);
2148
if (coup->type[c] == TYPE_CPE) {
2149
coup->ch_select[c] = get_bits(gb, 2);
2150
if (coup->ch_select[c] == 3)
2151
num_gain++;
2152
} else
2153
coup->ch_select[c] = 2;
2154
}
2155
coup->coupling_point += get_bits1(gb) || (coup->coupling_point >> 1);
2156
2157
sign = get_bits(gb, 1);
2158
scale = AAC_RENAME(cce_scale)[get_bits(gb, 2)];
2159
2160
if ((ret = decode_ics(ac, sce, gb, 0, 0)))
2161
return ret;
2162
2163
for (c = 0; c < num_gain; c++) {
2164
int idx = 0;
2165
int cge = 1;
2166
int gain = 0;
2167
INTFLOAT gain_cache = FIXR10(1.);
2168
if (c) {
2169
cge = coup->coupling_point == AFTER_IMDCT ? 1 : get_bits1(gb);
2170
gain = cge ? get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60: 0;
2171
gain_cache = GET_GAIN(scale, gain);
2172
}
2173
if (coup->coupling_point == AFTER_IMDCT) {
2174
coup->gain[c][0] = gain_cache;
2175
} else {
2176
for (g = 0; g < sce->ics.num_window_groups; g++) {
2177
for (sfb = 0; sfb < sce->ics.max_sfb; sfb++, idx++) {
2178
if (sce->band_type[idx] != ZERO_BT) {
2179
if (!cge) {
2180
int t = get_vlc2(gb, vlc_scalefactors.table, 7, 3) - 60;
2181
if (t) {
2182
int s = 1;
2183
t = gain += t;
2184
if (sign) {
2185
s -= 2 * (t & 0x1);
2186
t >>= 1;
2187
}
2188
gain_cache = GET_GAIN(scale, t) * s;
2189
}
2190
}
2191
coup->gain[c][idx] = gain_cache;
2192
}
2193
}
2194
}
2195
}
2196
}
2197
return 0;
2198
}
2199
2200
/**
2201
* Parse whether channels are to be excluded from Dynamic Range Compression; reference: table 4.53.
2202
*
2203
* @return Returns number of bytes consumed.
2204
*/
2205
static int decode_drc_channel_exclusions(DynamicRangeControl *che_drc,
2206
GetBitContext *gb)
2207
{
2208
int i;
2209
int num_excl_chan = 0;
2210
2211
do {
2212
for (i = 0; i < 7; i++)
2213
che_drc->exclude_mask[num_excl_chan++] = get_bits1(gb);
2214
} while (num_excl_chan < MAX_CHANNELS - 7 && get_bits1(gb));
2215
2216
return num_excl_chan / 7;
2217
}
2218
2219
/**
2220
* Decode dynamic range information; reference: table 4.52.
2221
*
2222
* @return Returns number of bytes consumed.
2223
*/
2224
static int decode_dynamic_range(DynamicRangeControl *che_drc,
2225
GetBitContext *gb)
2226
{
2227
int n = 1;
2228
int drc_num_bands = 1;
2229
int i;
2230
2231
/* pce_tag_present? */
2232
if (get_bits1(gb)) {
2233
che_drc->pce_instance_tag = get_bits(gb, 4);
2234
skip_bits(gb, 4); // tag_reserved_bits
2235
n++;
2236
}
2237
2238
/* excluded_chns_present? */
2239
if (get_bits1(gb)) {
2240
n += decode_drc_channel_exclusions(che_drc, gb);
2241
}
2242
2243
/* drc_bands_present? */
2244
if (get_bits1(gb)) {
2245
che_drc->band_incr = get_bits(gb, 4);
2246
che_drc->interpolation_scheme = get_bits(gb, 4);
2247
n++;
2248
drc_num_bands += che_drc->band_incr;
2249
for (i = 0; i < drc_num_bands; i++) {
2250
che_drc->band_top[i] = get_bits(gb, 8);
2251
n++;
2252
}
2253
}
2254
2255
/* prog_ref_level_present? */
2256
if (get_bits1(gb)) {
2257
che_drc->prog_ref_level = get_bits(gb, 7);
2258
skip_bits1(gb); // prog_ref_level_reserved_bits
2259
n++;
2260
}
2261
2262
for (i = 0; i < drc_num_bands; i++) {
2263
che_drc->dyn_rng_sgn[i] = get_bits1(gb);
2264
che_drc->dyn_rng_ctl[i] = get_bits(gb, 7);
2265
n++;
2266
}
2267
2268
return n;
2269
}
2270
2271
static int decode_fill(AACContext *ac, GetBitContext *gb, int len) {
2272
uint8_t buf[256];
2273
int i, major, minor;
2274
2275
if (len < 13+7*8)
2276
goto unknown;
2277
2278
get_bits(gb, 13); len -= 13;
2279
2280
for(i=0; i+1<sizeof(buf) && len>=8; i++, len-=8)
2281
buf[i] = get_bits(gb, 8);
2282
2283
buf[i] = 0;
2284
if (ac->avctx->debug & FF_DEBUG_PICT_INFO)
2285
av_log(ac->avctx, AV_LOG_DEBUG, "FILL:%s\n", buf);
2286
2287
if (sscanf(buf, "libfaac %d.%d", &major, &minor) == 2){
2288
ac->avctx->internal->skip_samples = 1024;
2289
}
2290
2291
unknown:
2292
skip_bits_long(gb, len);
2293
2294
return 0;
2295
}
2296
2297
/**
2298
* Decode extension data (incomplete); reference: table 4.51.
2299
*
2300
* @param cnt length of TYPE_FIL syntactic element in bytes
2301
*
2302
* @return Returns number of bytes consumed
2303
*/
2304
static int decode_extension_payload(AACContext *ac, GetBitContext *gb, int cnt,
2305
ChannelElement *che, enum RawDataBlockType elem_type)
2306
{
2307
int crc_flag = 0;
2308
int res = cnt;
2309
int type = get_bits(gb, 4);
2310
2311
if (ac->avctx->debug & FF_DEBUG_STARTCODE)
2312
av_log(ac->avctx, AV_LOG_DEBUG, "extension type: %d len:%d\n", type, cnt);
2313
2314
switch (type) { // extension type
2315
case EXT_SBR_DATA_CRC:
2316
crc_flag++;
2317
case EXT_SBR_DATA:
2318
if (!che) {
2319
av_log(ac->avctx, AV_LOG_ERROR, "SBR was found before the first channel element.\n");
2320
return res;
2321
} else if (!ac->oc[1].m4ac.sbr) {
2322
av_log(ac->avctx, AV_LOG_ERROR, "SBR signaled to be not-present but was found in the bitstream.\n");
2323
skip_bits_long(gb, 8 * cnt - 4);
2324
return res;
2325
} else if (ac->oc[1].m4ac.sbr == -1 && ac->oc[1].status == OC_LOCKED) {
2326
av_log(ac->avctx, AV_LOG_ERROR, "Implicit SBR was found with a first occurrence after the first frame.\n");
2327
skip_bits_long(gb, 8 * cnt - 4);
2328
return res;
2329
} else if (ac->oc[1].m4ac.ps == -1 && ac->oc[1].status < OC_LOCKED && ac->avctx->channels == 1) {
2330
ac->oc[1].m4ac.sbr = 1;
2331
ac->oc[1].m4ac.ps = 1;
2332
ac->avctx->profile = FF_PROFILE_AAC_HE_V2;
2333
output_configure(ac, ac->oc[1].layout_map, ac->oc[1].layout_map_tags,
2334
ac->oc[1].status, 1);
2335
} else {
2336
ac->oc[1].m4ac.sbr = 1;
2337
ac->avctx->profile = FF_PROFILE_AAC_HE;
2338
}
2339
res = AAC_RENAME(ff_decode_sbr_extension)(ac, &che->sbr, gb, crc_flag, cnt, elem_type);
2340
break;
2341
case EXT_DYNAMIC_RANGE:
2342
res = decode_dynamic_range(&ac->che_drc, gb);
2343
break;
2344
case EXT_FILL:
2345
decode_fill(ac, gb, 8 * cnt - 4);
2346
break;
2347
case EXT_FILL_DATA:
2348
case EXT_DATA_ELEMENT:
2349
default:
2350
skip_bits_long(gb, 8 * cnt - 4);
2351
break;
2352
};
2353
return res;
2354
}
2355
2356
/**
2357
* Decode Temporal Noise Shaping filter coefficients and apply all-pole filters; reference: 4.6.9.3.
2358
*
2359
* @param decode 1 if tool is used normally, 0 if tool is used in LTP.
2360
* @param coef spectral coefficients
2361
*/
2362
static void apply_tns(INTFLOAT coef[1024], TemporalNoiseShaping *tns,
2363
IndividualChannelStream *ics, int decode)
2364
{
2365
const int mmm = FFMIN(ics->tns_max_bands, ics->max_sfb);
2366
int w, filt, m, i;
2367
int bottom, top, order, start, end, size, inc;
2368
INTFLOAT lpc[TNS_MAX_ORDER];
2369
INTFLOAT tmp[TNS_MAX_ORDER+1];
2370
2371
for (w = 0; w < ics->num_windows; w++) {
2372
bottom = ics->num_swb;
2373
for (filt = 0; filt < tns->n_filt[w]; filt++) {
2374
top = bottom;
2375
bottom = FFMAX(0, top - tns->length[w][filt]);
2376
order = tns->order[w][filt];
2377
if (order == 0)
2378
continue;
2379
2380
// tns_decode_coef
2381
AAC_RENAME(compute_lpc_coefs)(tns->coef[w][filt], order, lpc, 0, 0, 0);
2382
2383
start = ics->swb_offset[FFMIN(bottom, mmm)];
2384
end = ics->swb_offset[FFMIN( top, mmm)];
2385
if ((size = end - start) <= 0)
2386
continue;
2387
if (tns->direction[w][filt]) {
2388
inc = -1;
2389
start = end - 1;
2390
} else {
2391
inc = 1;
2392
}
2393
start += w * 128;
2394
2395
if (decode) {
2396
// ar filter
2397
for (m = 0; m < size; m++, start += inc)
2398
for (i = 1; i <= FFMIN(m, order); i++)
2399
coef[start] -= AAC_MUL26(coef[start - i * inc], lpc[i - 1]);
2400
} else {
2401
// ma filter
2402
for (m = 0; m < size; m++, start += inc) {
2403
tmp[0] = coef[start];
2404
for (i = 1; i <= FFMIN(m, order); i++)
2405
coef[start] += AAC_MUL26(tmp[i], lpc[i - 1]);
2406
for (i = order; i > 0; i--)
2407
tmp[i] = tmp[i - 1];
2408
}
2409
}
2410
}
2411
}
2412
}
2413
2414
/**
2415
* Apply windowing and MDCT to obtain the spectral
2416
* coefficient from the predicted sample by LTP.
2417
*/
2418
static void windowing_and_mdct_ltp(AACContext *ac, INTFLOAT *out,
2419
INTFLOAT *in, IndividualChannelStream *ics)
2420
{
2421
const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
2422
const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
2423
const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
2424
const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
2425
2426
if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
2427
ac->fdsp->vector_fmul(in, in, lwindow_prev, 1024);
2428
} else {
2429
memset(in, 0, 448 * sizeof(*in));
2430
ac->fdsp->vector_fmul(in + 448, in + 448, swindow_prev, 128);
2431
}
2432
if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
2433
ac->fdsp->vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
2434
} else {
2435
ac->fdsp->vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
2436
memset(in + 1024 + 576, 0, 448 * sizeof(*in));
2437
}
2438
ac->mdct_ltp.mdct_calc(&ac->mdct_ltp, out, in);
2439
}
2440
2441
/**
2442
* Apply the long term prediction
2443
*/
2444
static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
2445
{
2446
const LongTermPrediction *ltp = &sce->ics.ltp;
2447
const uint16_t *offsets = sce->ics.swb_offset;
2448
int i, sfb;
2449
2450
if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
2451
INTFLOAT *predTime = sce->ret;
2452
INTFLOAT *predFreq = ac->buf_mdct;
2453
int16_t num_samples = 2048;
2454
2455
if (ltp->lag < 1024)
2456
num_samples = ltp->lag + 1024;
2457
for (i = 0; i < num_samples; i++)
2458
predTime[i] = AAC_MUL30(sce->ltp_state[i + 2048 - ltp->lag], ltp->coef);
2459
memset(&predTime[i], 0, (2048 - i) * sizeof(*predTime));
2460
2461
ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
2462
2463
if (sce->tns.present)
2464
ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);
2465
2466
for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
2467
if (ltp->used[sfb])
2468
for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
2469
sce->coeffs[i] += predFreq[i];
2470
}
2471
}
2472
2473
/**
2474
* Update the LTP buffer for next frame
2475
*/
2476
static void update_ltp(AACContext *ac, SingleChannelElement *sce)
2477
{
2478
IndividualChannelStream *ics = &sce->ics;
2479
INTFLOAT *saved = sce->saved;
2480
INTFLOAT *saved_ltp = sce->coeffs;
2481
const INTFLOAT *lwindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
2482
const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
2483
int i;
2484
2485
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2486
memcpy(saved_ltp, saved, 512 * sizeof(*saved_ltp));
2487
memset(saved_ltp + 576, 0, 448 * sizeof(*saved_ltp));
2488
ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2489
2490
for (i = 0; i < 64; i++)
2491
saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
2492
} else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2493
memcpy(saved_ltp, ac->buf_mdct + 512, 448 * sizeof(*saved_ltp));
2494
memset(saved_ltp + 576, 0, 448 * sizeof(*saved_ltp));
2495
ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960, &swindow[64], 64);
2496
2497
for (i = 0; i < 64; i++)
2498
saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], swindow[63 - i]);
2499
} else { // LONG_STOP or ONLY_LONG
2500
ac->fdsp->vector_fmul_reverse(saved_ltp, ac->buf_mdct + 512, &lwindow[512], 512);
2501
2502
for (i = 0; i < 512; i++)
2503
saved_ltp[i + 512] = AAC_MUL31(ac->buf_mdct[1023 - i], lwindow[511 - i]);
2504
}
2505
2506
memcpy(sce->ltp_state, sce->ltp_state+1024, 1024 * sizeof(*sce->ltp_state));
2507
memcpy(sce->ltp_state+1024, sce->ret, 1024 * sizeof(*sce->ltp_state));
2508
memcpy(sce->ltp_state+2048, saved_ltp, 1024 * sizeof(*sce->ltp_state));
2509
}
2510
2511
/**
2512
* Conduct IMDCT and windowing.
2513
*/
2514
static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
2515
{
2516
IndividualChannelStream *ics = &sce->ics;
2517
INTFLOAT *in = sce->coeffs;
2518
INTFLOAT *out = sce->ret;
2519
INTFLOAT *saved = sce->saved;
2520
const INTFLOAT *swindow = ics->use_kb_window[0] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
2521
const INTFLOAT *lwindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_long_1024) : AAC_RENAME(ff_sine_1024);
2522
const INTFLOAT *swindow_prev = ics->use_kb_window[1] ? AAC_RENAME(ff_aac_kbd_short_128) : AAC_RENAME(ff_sine_128);
2523
INTFLOAT *buf = ac->buf_mdct;
2524
INTFLOAT *temp = ac->temp;
2525
int i;
2526
2527
// imdct
2528
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2529
for (i = 0; i < 1024; i += 128)
2530
ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
2531
} else {
2532
ac->mdct.imdct_half(&ac->mdct, buf, in);
2533
#if USE_FIXED
2534
for (i=0; i<1024; i++)
2535
buf[i] = (buf[i] + 4) >> 3;
2536
#endif /* USE_FIXED */
2537
}
2538
2539
/* window overlapping
2540
* NOTE: To simplify the overlapping code, all 'meaningless' short to long
2541
* and long to short transitions are considered to be short to short
2542
* transitions. This leaves just two cases (long to long and short to short)
2543
* with a little special sauce for EIGHT_SHORT_SEQUENCE.
2544
*/
2545
if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
2546
(ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
2547
ac->fdsp->vector_fmul_window( out, saved, buf, lwindow_prev, 512);
2548
} else {
2549
memcpy( out, saved, 448 * sizeof(*out));
2550
2551
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2552
ac->fdsp->vector_fmul_window(out + 448 + 0*128, saved + 448, buf + 0*128, swindow_prev, 64);
2553
ac->fdsp->vector_fmul_window(out + 448 + 1*128, buf + 0*128 + 64, buf + 1*128, swindow, 64);
2554
ac->fdsp->vector_fmul_window(out + 448 + 2*128, buf + 1*128 + 64, buf + 2*128, swindow, 64);
2555
ac->fdsp->vector_fmul_window(out + 448 + 3*128, buf + 2*128 + 64, buf + 3*128, swindow, 64);
2556
ac->fdsp->vector_fmul_window(temp, buf + 3*128 + 64, buf + 4*128, swindow, 64);
2557
memcpy( out + 448 + 4*128, temp, 64 * sizeof(*out));
2558
} else {
2559
ac->fdsp->vector_fmul_window(out + 448, saved + 448, buf, swindow_prev, 64);
2560
memcpy( out + 576, buf + 64, 448 * sizeof(*out));
2561
}
2562
}
2563
2564
// buffer update
2565
if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
2566
memcpy( saved, temp + 64, 64 * sizeof(*saved));
2567
ac->fdsp->vector_fmul_window(saved + 64, buf + 4*128 + 64, buf + 5*128, swindow, 64);
2568
ac->fdsp->vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
2569
ac->fdsp->vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
2570
memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(*saved));
2571
} else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
2572
memcpy( saved, buf + 512, 448 * sizeof(*saved));
2573
memcpy( saved + 448, buf + 7*128 + 64, 64 * sizeof(*saved));
2574
} else { // LONG_STOP or ONLY_LONG
2575
memcpy( saved, buf + 512, 512 * sizeof(*saved));
2576
}
2577
}
2578
2579
static void imdct_and_windowing_ld(AACContext *ac, SingleChannelElement *sce)
2580
{
2581
IndividualChannelStream *ics = &sce->ics;
2582
INTFLOAT *in = sce->coeffs;
2583
INTFLOAT *out = sce->ret;
2584
INTFLOAT *saved = sce->saved;
2585
INTFLOAT *buf = ac->buf_mdct;
2586
#if USE_FIXED
2587
int i;
2588
#endif /* USE_FIXED */
2589
2590
// imdct
2591
ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
2592
2593
#if USE_FIXED
2594
for (i = 0; i < 1024; i++)
2595
buf[i] = (buf[i] + 2) >> 2;
2596
#endif /* USE_FIXED */
2597
2598
// window overlapping
2599
if (ics->use_kb_window[1]) {
2600
// AAC LD uses a low overlap sine window instead of a KBD window
2601
memcpy(out, saved, 192 * sizeof(*out));
2602
ac->fdsp->vector_fmul_window(out + 192, saved + 192, buf, AAC_RENAME(ff_sine_128), 64);
2603
memcpy( out + 320, buf + 64, 192 * sizeof(*out));
2604
} else {
2605
ac->fdsp->vector_fmul_window(out, saved, buf, AAC_RENAME(ff_sine_512), 256);
2606
}
2607
2608
// buffer update
2609
memcpy(saved, buf + 256, 256 * sizeof(*saved));
2610
}
2611
2612
static void imdct_and_windowing_eld(AACContext *ac, SingleChannelElement *sce)
2613
{
2614
INTFLOAT *in = sce->coeffs;
2615
INTFLOAT *out = sce->ret;
2616
INTFLOAT *saved = sce->saved;
2617
INTFLOAT *buf = ac->buf_mdct;
2618
int i;
2619
const int n = ac->oc[1].m4ac.frame_length_short ? 480 : 512;
2620
const int n2 = n >> 1;
2621
const int n4 = n >> 2;
2622
const INTFLOAT *const window = n == 480 ? AAC_RENAME(ff_aac_eld_window_480) :
2623
AAC_RENAME(ff_aac_eld_window_512);
2624
2625
// Inverse transform, mapped to the conventional IMDCT by
2626
// Chivukula, R.K.; Reznik, Y.A.; Devarajan, V.,
2627
// "Efficient algorithms for MPEG-4 AAC-ELD, AAC-LD and AAC-LC filterbanks,"
2628
// International Conference on Audio, Language and Image Processing, ICALIP 2008.
2629
// URL: http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4590245&isnumber=4589950
2630
for (i = 0; i < n2; i+=2) {
2631
INTFLOAT temp;
2632
temp = in[i ]; in[i ] = -in[n - 1 - i]; in[n - 1 - i] = temp;
2633
temp = -in[i + 1]; in[i + 1] = in[n - 2 - i]; in[n - 2 - i] = temp;
2634
}
2635
#if !USE_FIXED
2636
if (n == 480)
2637
ac->mdct480->imdct_half(ac->mdct480, buf, in, 1, -1.f/(16*1024*960));
2638
else
2639
#endif
2640
ac->mdct.imdct_half(&ac->mdct_ld, buf, in);
2641
2642
#if USE_FIXED
2643
for (i = 0; i < 1024; i++)
2644
buf[i] = (buf[i] + 1) >> 1;
2645
#endif /* USE_FIXED */
2646
2647
for (i = 0; i < n; i+=2) {
2648
buf[i] = -buf[i];
2649
}
2650
// Like with the regular IMDCT at this point we still have the middle half
2651
// of a transform but with even symmetry on the left and odd symmetry on
2652
// the right
2653
2654
// window overlapping
2655
// The spec says to use samples [0..511] but the reference decoder uses
2656
// samples [128..639].
2657
for (i = n4; i < n2; i ++) {
2658
out[i - n4] = AAC_MUL31( buf[ n2 - 1 - i] , window[i - n4]) +
2659
AAC_MUL31( saved[ i + n2] , window[i + n - n4]) +
2660
AAC_MUL31(-saved[n + n2 - 1 - i] , window[i + 2*n - n4]) +
2661
AAC_MUL31(-saved[ 2*n + n2 + i] , window[i + 3*n - n4]);
2662
}
2663
for (i = 0; i < n2; i ++) {
2664
out[n4 + i] = AAC_MUL31( buf[ i] , window[i + n2 - n4]) +
2665
AAC_MUL31(-saved[ n - 1 - i] , window[i + n2 + n - n4]) +
2666
AAC_MUL31(-saved[ n + i] , window[i + n2 + 2*n - n4]) +
2667
AAC_MUL31( saved[2*n + n - 1 - i] , window[i + n2 + 3*n - n4]);
2668
}
2669
for (i = 0; i < n4; i ++) {
2670
out[n2 + n4 + i] = AAC_MUL31( buf[ i + n2] , window[i + n - n4]) +
2671
AAC_MUL31(-saved[n2 - 1 - i] , window[i + 2*n - n4]) +
2672
AAC_MUL31(-saved[n + n2 + i] , window[i + 3*n - n4]);
2673
}
2674
2675
// buffer update
2676
memmove(saved + n, saved, 2 * n * sizeof(*saved));
2677
memcpy( saved, buf, n * sizeof(*saved));
2678
}
2679
2680
/**
2681
* channel coupling transformation interface
2682
*
2683
* @param apply_coupling_method pointer to (in)dependent coupling function
2684
*/
2685
static void apply_channel_coupling(AACContext *ac, ChannelElement *cc,
2686
enum RawDataBlockType type, int elem_id,
2687
enum CouplingPoint coupling_point,
2688
void (*apply_coupling_method)(AACContext *ac, SingleChannelElement *target, ChannelElement *cce, int index))
2689
{
2690
int i, c;
2691
2692
for (i = 0; i < MAX_ELEM_ID; i++) {
2693
ChannelElement *cce = ac->che[TYPE_CCE][i];
2694
int index = 0;
2695
2696
if (cce && cce->coup.coupling_point == coupling_point) {
2697
ChannelCoupling *coup = &cce->coup;
2698
2699
for (c = 0; c <= coup->num_coupled; c++) {
2700
if (coup->type[c] == type && coup->id_select[c] == elem_id) {
2701
if (coup->ch_select[c] != 1) {
2702
apply_coupling_method(ac, &cc->ch[0], cce, index);
2703
if (coup->ch_select[c] != 0)
2704
index++;
2705
}
2706
if (coup->ch_select[c] != 2)
2707
apply_coupling_method(ac, &cc->ch[1], cce, index++);
2708
} else
2709
index += 1 + (coup->ch_select[c] == 3);
2710
}
2711
}
2712
}
2713
}
2714
2715
/**
2716
* Convert spectral data to samples, applying all supported tools as appropriate.
2717
*/
2718
static void spectral_to_sample(AACContext *ac, int samples)
2719
{
2720
int i, type;
2721
void (*imdct_and_window)(AACContext *ac, SingleChannelElement *sce);
2722
switch (ac->oc[1].m4ac.object_type) {
2723
case AOT_ER_AAC_LD:
2724
imdct_and_window = imdct_and_windowing_ld;
2725
break;
2726
case AOT_ER_AAC_ELD:
2727
imdct_and_window = imdct_and_windowing_eld;
2728
break;
2729
default:
2730
imdct_and_window = ac->imdct_and_windowing;
2731
}
2732
for (type = 3; type >= 0; type--) {
2733
for (i = 0; i < MAX_ELEM_ID; i++) {
2734
ChannelElement *che = ac->che[type][i];
2735
if (che && che->present) {
2736
if (type <= TYPE_CPE)
2737
apply_channel_coupling(ac, che, type, i, BEFORE_TNS, AAC_RENAME(apply_dependent_coupling));
2738
if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
2739
if (che->ch[0].ics.predictor_present) {
2740
if (che->ch[0].ics.ltp.present)
2741
ac->apply_ltp(ac, &che->ch[0]);
2742
if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
2743
ac->apply_ltp(ac, &che->ch[1]);
2744
}
2745
}
2746
if (che->ch[0].tns.present)
2747
ac->apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
2748
if (che->ch[1].tns.present)
2749
ac->apply_tns(che->ch[1].coeffs, &che->ch[1].tns, &che->ch[1].ics, 1);
2750
if (type <= TYPE_CPE)
2751
apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, AAC_RENAME(apply_dependent_coupling));
2752
if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
2753
imdct_and_window(ac, &che->ch[0]);
2754
if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2755
ac->update_ltp(ac, &che->ch[0]);
2756
if (type == TYPE_CPE) {
2757
imdct_and_window(ac, &che->ch[1]);
2758
if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP)
2759
ac->update_ltp(ac, &che->ch[1]);
2760
}
2761
if (ac->oc[1].m4ac.sbr > 0) {
2762
AAC_RENAME(ff_sbr_apply)(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
2763
}
2764
}
2765
if (type <= TYPE_CCE)
2766
apply_channel_coupling(ac, che, type, i, AFTER_IMDCT, AAC_RENAME(apply_independent_coupling));
2767
2768
#if USE_FIXED
2769
{
2770
int j;
2771
/* preparation for resampler */
2772
for(j = 0; j<samples; j++){
2773
che->ch[0].ret[j] = (int32_t)av_clipl_int32((int64_t)che->ch[0].ret[j]<<7)+0x8000;
2774
if(type == TYPE_CPE)
2775
che->ch[1].ret[j] = (int32_t)av_clipl_int32((int64_t)che->ch[1].ret[j]<<7)+0x8000;
2776
}
2777
}
2778
#endif /* USE_FIXED */
2779
che->present = 0;
2780
} else if (che) {
2781
av_log(ac->avctx, AV_LOG_VERBOSE, "ChannelElement %d.%d missing \n", type, i);
2782
}
2783
}
2784
}
2785
}
2786
2787
static int parse_adts_frame_header(AACContext *ac, GetBitContext *gb)
2788
{
2789
int size;
2790
AACADTSHeaderInfo hdr_info;
2791
uint8_t layout_map[MAX_ELEM_ID*4][3];
2792
int layout_map_tags, ret;
2793
2794
size = avpriv_aac_parse_header(gb, &hdr_info);
2795
if (size > 0) {
2796
if (!ac->warned_num_aac_frames && hdr_info.num_aac_frames != 1) {
2797
// This is 2 for "VLB " audio in NSV files.
2798
// See samples/nsv/vlb_audio.
2799
avpriv_report_missing_feature(ac->avctx,
2800
"More than one AAC RDB per ADTS frame");
2801
ac->warned_num_aac_frames = 1;
2802
}
2803
push_output_configuration(ac);
2804
if (hdr_info.chan_config) {
2805
ac->oc[1].m4ac.chan_config = hdr_info.chan_config;
2806
if ((ret = set_default_channel_config(ac->avctx,
2807
layout_map,
2808
&layout_map_tags,
2809
hdr_info.chan_config)) < 0)
2810
return ret;
2811
if ((ret = output_configure(ac, layout_map, layout_map_tags,
2812
FFMAX(ac->oc[1].status,
2813
OC_TRIAL_FRAME), 0)) < 0)
2814
return ret;
2815
} else {
2816
ac->oc[1].m4ac.chan_config = 0;
2817
/**
2818
* dual mono frames in Japanese DTV can have chan_config 0
2819
* WITHOUT specifying PCE.
2820
* thus, set dual mono as default.
2821
*/
2822
if (ac->dmono_mode && ac->oc[0].status == OC_NONE) {
2823
layout_map_tags = 2;
2824
layout_map[0][0] = layout_map[1][0] = TYPE_SCE;
2825
layout_map[0][2] = layout_map[1][2] = AAC_CHANNEL_FRONT;
2826
layout_map[0][1] = 0;
2827
layout_map[1][1] = 1;
2828
if (output_configure(ac, layout_map, layout_map_tags,
2829
OC_TRIAL_FRAME, 0))
2830
return -7;
2831
}
2832
}
2833
ac->oc[1].m4ac.sample_rate = hdr_info.sample_rate;
2834
ac->oc[1].m4ac.sampling_index = hdr_info.sampling_index;
2835
ac->oc[1].m4ac.object_type = hdr_info.object_type;
2836
ac->oc[1].m4ac.frame_length_short = 0;
2837
if (ac->oc[0].status != OC_LOCKED ||
2838
ac->oc[0].m4ac.chan_config != hdr_info.chan_config ||
2839
ac->oc[0].m4ac.sample_rate != hdr_info.sample_rate) {
2840
ac->oc[1].m4ac.sbr = -1;
2841
ac->oc[1].m4ac.ps = -1;
2842
}
2843
if (!hdr_info.crc_absent)
2844
skip_bits(gb, 16);
2845
}
2846
return size;
2847
}
2848
2849
static int aac_decode_er_frame(AVCodecContext *avctx, void *data,
2850
int *got_frame_ptr, GetBitContext *gb)
2851
{
2852
AACContext *ac = avctx->priv_data;
2853
const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;
2854
ChannelElement *che;
2855
int err, i;
2856
int samples = m4ac->frame_length_short ? 960 : 1024;
2857
int chan_config = m4ac->chan_config;
2858
int aot = m4ac->object_type;
2859
2860
if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)
2861
samples >>= 1;
2862
2863
ac->frame = data;
2864
2865
if ((err = frame_configure_elements(avctx)) < 0)
2866
return err;
2867
2868
// The FF_PROFILE_AAC_* defines are all object_type - 1
2869
// This may lead to an undefined profile being signaled
2870
ac->avctx->profile = aot - 1;
2871
2872
ac->tags_mapped = 0;
2873
2874
if (chan_config < 0 || (chan_config >= 8 && chan_config < 11) || chan_config >= 13) {
2875
avpriv_request_sample(avctx, "Unknown ER channel configuration %d",
2876
chan_config);
2877
return AVERROR_INVALIDDATA;
2878
}
2879
for (i = 0; i < tags_per_config[chan_config]; i++) {
2880
const int elem_type = aac_channel_layout_map[chan_config-1][i][0];
2881
const int elem_id = aac_channel_layout_map[chan_config-1][i][1];
2882
if (!(che=get_che(ac, elem_type, elem_id))) {
2883
av_log(ac->avctx, AV_LOG_ERROR,
2884
"channel element %d.%d is not allocated\n",
2885
elem_type, elem_id);
2886
return AVERROR_INVALIDDATA;
2887
}
2888
che->present = 1;
2889
if (aot != AOT_ER_AAC_ELD)
2890
skip_bits(gb, 4);
2891
switch (elem_type) {
2892
case TYPE_SCE:
2893
err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2894
break;
2895
case TYPE_CPE:
2896
err = decode_cpe(ac, gb, che);
2897
break;
2898
case TYPE_LFE:
2899
err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2900
break;
2901
}
2902
if (err < 0)
2903
return err;
2904
}
2905
2906
spectral_to_sample(ac, samples);
2907
2908
if (!ac->frame->data[0] && samples) {
2909
av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
2910
return AVERROR_INVALIDDATA;
2911
}
2912
2913
ac->frame->nb_samples = samples;
2914
ac->frame->sample_rate = avctx->sample_rate;
2915
*got_frame_ptr = 1;
2916
2917
skip_bits_long(gb, get_bits_left(gb));
2918
return 0;
2919
}
2920
2921
static int aac_decode_frame_int(AVCodecContext *avctx, void *data,
2922
int *got_frame_ptr, GetBitContext *gb, AVPacket *avpkt)
2923
{
2924
AACContext *ac = avctx->priv_data;
2925
ChannelElement *che = NULL, *che_prev = NULL;
2926
enum RawDataBlockType elem_type, elem_type_prev = TYPE_END;
2927
int err, elem_id;
2928
int samples = 0, multiplier, audio_found = 0, pce_found = 0;
2929
int is_dmono, sce_count = 0;
2930
2931
ac->frame = data;
2932
2933
if (show_bits(gb, 12) == 0xfff) {
2934
if ((err = parse_adts_frame_header(ac, gb)) < 0) {
2935
av_log(avctx, AV_LOG_ERROR, "Error decoding AAC frame header.\n");
2936
goto fail;
2937
}
2938
if (ac->oc[1].m4ac.sampling_index > 12) {
2939
av_log(ac->avctx, AV_LOG_ERROR, "invalid sampling rate index %d\n", ac->oc[1].m4ac.sampling_index);
2940
err = AVERROR_INVALIDDATA;
2941
goto fail;
2942
}
2943
}
2944
2945
if ((err = frame_configure_elements(avctx)) < 0)
2946
goto fail;
2947
2948
// The FF_PROFILE_AAC_* defines are all object_type - 1
2949
// This may lead to an undefined profile being signaled
2950
ac->avctx->profile = ac->oc[1].m4ac.object_type - 1;
2951
2952
ac->tags_mapped = 0;
2953
// parse
2954
while ((elem_type = get_bits(gb, 3)) != TYPE_END) {
2955
elem_id = get_bits(gb, 4);
2956
2957
if (avctx->debug & FF_DEBUG_STARTCODE)
2958
av_log(avctx, AV_LOG_DEBUG, "Elem type:%x id:%x\n", elem_type, elem_id);
2959
2960
if (!avctx->channels && elem_type != TYPE_PCE) {
2961
err = AVERROR_INVALIDDATA;
2962
goto fail;
2963
}
2964
2965
if (elem_type < TYPE_DSE) {
2966
if (!(che=get_che(ac, elem_type, elem_id))) {
2967
av_log(ac->avctx, AV_LOG_ERROR, "channel element %d.%d is not allocated\n",
2968
elem_type, elem_id);
2969
err = AVERROR_INVALIDDATA;
2970
goto fail;
2971
}
2972
samples = 1024;
2973
che->present = 1;
2974
}
2975
2976
switch (elem_type) {
2977
2978
case TYPE_SCE:
2979
err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2980
audio_found = 1;
2981
sce_count++;
2982
break;
2983
2984
case TYPE_CPE:
2985
err = decode_cpe(ac, gb, che);
2986
audio_found = 1;
2987
break;
2988
2989
case TYPE_CCE:
2990
err = decode_cce(ac, gb, che);
2991
break;
2992
2993
case TYPE_LFE:
2994
err = decode_ics(ac, &che->ch[0], gb, 0, 0);
2995
audio_found = 1;
2996
break;
2997
2998
case TYPE_DSE:
2999
err = skip_data_stream_element(ac, gb);
3000
break;
3001
3002
case TYPE_PCE: {
3003
uint8_t layout_map[MAX_ELEM_ID*4][3];
3004
int tags;
3005
push_output_configuration(ac);
3006
tags = decode_pce(avctx, &ac->oc[1].m4ac, layout_map, gb);
3007
if (tags < 0) {
3008
err = tags;
3009
break;
3010
}
3011
if (pce_found) {
3012
av_log(avctx, AV_LOG_ERROR,
3013
"Not evaluating a further program_config_element as this construct is dubious at best.\n");
3014
} else {
3015
err = output_configure(ac, layout_map, tags, OC_TRIAL_PCE, 1);
3016
if (!err)
3017
ac->oc[1].m4ac.chan_config = 0;
3018
pce_found = 1;
3019
}
3020
break;
3021
}
3022
3023
case TYPE_FIL:
3024
if (elem_id == 15)
3025
elem_id += get_bits(gb, 8) - 1;
3026
if (get_bits_left(gb) < 8 * elem_id) {
3027
av_log(avctx, AV_LOG_ERROR, "TYPE_FIL: "overread_err);
3028
err = AVERROR_INVALIDDATA;
3029
goto fail;
3030
}
3031
while (elem_id > 0)
3032
elem_id -= decode_extension_payload(ac, gb, elem_id, che_prev, elem_type_prev);
3033
err = 0; /* FIXME */
3034
break;
3035
3036
default:
3037
err = AVERROR_BUG; /* should not happen, but keeps compiler happy */
3038
break;
3039
}
3040
3041
che_prev = che;
3042
elem_type_prev = elem_type;
3043
3044
if (err)
3045
goto fail;
3046
3047
if (get_bits_left(gb) < 3) {
3048
av_log(avctx, AV_LOG_ERROR, overread_err);
3049
err = AVERROR_INVALIDDATA;
3050
goto fail;
3051
}
3052
}
3053
3054
if (!avctx->channels) {
3055
*got_frame_ptr = 0;
3056
return 0;
3057
}
3058
3059
multiplier = (ac->oc[1].m4ac.sbr == 1) ? ac->oc[1].m4ac.ext_sample_rate > ac->oc[1].m4ac.sample_rate : 0;
3060
samples <<= multiplier;
3061
3062
spectral_to_sample(ac, samples);
3063
3064
if (ac->oc[1].status && audio_found) {
3065
avctx->sample_rate = ac->oc[1].m4ac.sample_rate << multiplier;
3066
avctx->frame_size = samples;
3067
ac->oc[1].status = OC_LOCKED;
3068
}
3069
3070
if (multiplier) {
3071
int side_size;
3072
const uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_SKIP_SAMPLES, &side_size);
3073
if (side && side_size>=4)
3074
AV_WL32(side, 2*AV_RL32(side));
3075
}
3076
3077
if (!ac->frame->data[0] && samples) {
3078
av_log(avctx, AV_LOG_ERROR, "no frame data found\n");
3079
err = AVERROR_INVALIDDATA;
3080
goto fail;
3081
}
3082
3083
if (samples) {
3084
ac->frame->nb_samples = samples;
3085
ac->frame->sample_rate = avctx->sample_rate;
3086
} else
3087
av_frame_unref(ac->frame);
3088
*got_frame_ptr = !!samples;
3089
3090
/* for dual-mono audio (SCE + SCE) */
3091
is_dmono = ac->dmono_mode && sce_count == 2 &&
3092
ac->oc[1].channel_layout == (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT);
3093
if (is_dmono) {
3094
if (ac->dmono_mode == 1)
3095
((AVFrame *)data)->data[1] =((AVFrame *)data)->data[0];
3096
else if (ac->dmono_mode == 2)
3097
((AVFrame *)data)->data[0] =((AVFrame *)data)->data[1];
3098
}
3099
3100
return 0;
3101
fail:
3102
pop_output_configuration(ac);
3103
return err;
3104
}
3105
3106
static int aac_decode_frame(AVCodecContext *avctx, void *data,
3107
int *got_frame_ptr, AVPacket *avpkt)
3108
{
3109
AACContext *ac = avctx->priv_data;
3110
const uint8_t *buf = avpkt->data;
3111
int buf_size = avpkt->size;
3112
GetBitContext gb;
3113
int buf_consumed;
3114
int buf_offset;
3115
int err;
3116
int new_extradata_size;
3117
const uint8_t *new_extradata = av_packet_get_side_data(avpkt,
3118
AV_PKT_DATA_NEW_EXTRADATA,
3119
&new_extradata_size);
3120
int jp_dualmono_size;
3121
const uint8_t *jp_dualmono = av_packet_get_side_data(avpkt,
3122
AV_PKT_DATA_JP_DUALMONO,
3123
&jp_dualmono_size);
3124
3125
if (new_extradata && 0) {
3126
av_free(avctx->extradata);
3127
avctx->extradata = av_mallocz(new_extradata_size +
3128
AV_INPUT_BUFFER_PADDING_SIZE);
3129
if (!avctx->extradata)
3130
return AVERROR(ENOMEM);
3131
avctx->extradata_size = new_extradata_size;
3132
memcpy(avctx->extradata, new_extradata, new_extradata_size);
3133
push_output_configuration(ac);
3134
if (decode_audio_specific_config(ac, ac->avctx, &ac->oc[1].m4ac,
3135
avctx->extradata,
3136
avctx->extradata_size*8LL, 1) < 0) {
3137
pop_output_configuration(ac);
3138
return AVERROR_INVALIDDATA;
3139
}
3140
}
3141
3142
ac->dmono_mode = 0;
3143
if (jp_dualmono && jp_dualmono_size > 0)
3144
ac->dmono_mode = 1 + *jp_dualmono;
3145
if (ac->force_dmono_mode >= 0)
3146
ac->dmono_mode = ac->force_dmono_mode;
3147
3148
if (INT_MAX / 8 <= buf_size)
3149
return AVERROR_INVALIDDATA;
3150
3151
if ((err = init_get_bits8(&gb, buf, buf_size)) < 0)
3152
return err;
3153
3154
switch (ac->oc[1].m4ac.object_type) {
3155
case AOT_ER_AAC_LC:
3156
case AOT_ER_AAC_LTP:
3157
case AOT_ER_AAC_LD:
3158
case AOT_ER_AAC_ELD:
3159
err = aac_decode_er_frame(avctx, data, got_frame_ptr, &gb);
3160
break;
3161
default:
3162
err = aac_decode_frame_int(avctx, data, got_frame_ptr, &gb, avpkt);
3163
}
3164
if (err < 0)
3165
return err;
3166
3167
buf_consumed = (get_bits_count(&gb) + 7) >> 3;
3168
for (buf_offset = buf_consumed; buf_offset < buf_size; buf_offset++)
3169
if (buf[buf_offset])
3170
break;
3171
3172
return buf_size > buf_offset ? buf_consumed : buf_size;
3173
}
3174
3175
static av_cold int aac_decode_close(AVCodecContext *avctx)
3176
{
3177
AACContext *ac = avctx->priv_data;
3178
int i, type;
3179
3180
for (i = 0; i < MAX_ELEM_ID; i++) {
3181
for (type = 0; type < 4; type++) {
3182
if (ac->che[type][i])
3183
AAC_RENAME(ff_aac_sbr_ctx_close)(&ac->che[type][i]->sbr);
3184
av_freep(&ac->che[type][i]);
3185
}
3186
}
3187
3188
ff_mdct_end(&ac->mdct);
3189
ff_mdct_end(&ac->mdct_small);
3190
ff_mdct_end(&ac->mdct_ld);
3191
ff_mdct_end(&ac->mdct_ltp);
3192
#if !USE_FIXED
3193
ff_imdct15_uninit(&ac->mdct480);
3194
#endif
3195
av_freep(&ac->fdsp);
3196
return 0;
3197
}
3198
3199
static void aacdec_init(AACContext *c)
3200
{
3201
c->imdct_and_windowing = imdct_and_windowing;
3202
c->apply_ltp = apply_ltp;
3203
c->apply_tns = apply_tns;
3204
c->windowing_and_mdct_ltp = windowing_and_mdct_ltp;
3205
c->update_ltp = update_ltp;
3206
#if USE_FIXED
3207
c->vector_pow43 = vector_pow43;
3208
c->subband_scale = subband_scale;
3209
#endif
3210
3211
#if !USE_FIXED
3212
if(ARCH_MIPS)
3213
ff_aacdec_init_mips(c);
3214
#endif /* !USE_FIXED */
3215
}
3216
/**
3217
* AVOptions for Japanese DTV specific extensions (ADTS only)
3218
*/
3219
#define AACDEC_FLAGS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
3220
static const AVOption options[] = {
3221
{"dual_mono_mode", "Select the channel to decode for dual mono",
3222
offsetof(AACContext, force_dmono_mode), AV_OPT_TYPE_INT, {.i64=-1}, -1, 2,
3223
AACDEC_FLAGS, "dual_mono_mode"},
3224
3225
{"auto", "autoselection", 0, AV_OPT_TYPE_CONST, {.i64=-1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3226
{"main", "Select Main/Left channel", 0, AV_OPT_TYPE_CONST, {.i64= 1}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3227
{"sub" , "Select Sub/Right channel", 0, AV_OPT_TYPE_CONST, {.i64= 2}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3228
{"both", "Select both channels", 0, AV_OPT_TYPE_CONST, {.i64= 0}, INT_MIN, INT_MAX, AACDEC_FLAGS, "dual_mono_mode"},
3229
3230
{NULL},
3231
};
3232
3233
static const AVClass aac_decoder_class = {
3234
.class_name = "AAC decoder",
3235
.item_name = av_default_item_name,
3236
.option = options,
3237
.version = LIBAVUTIL_VERSION_INT,
3238
};
3239
3240