forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dca_core.c
2446 lines (2043 loc) · 83.8 KB
/
dca_core.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2016 foo86
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "dcaadpcm.h"
#include "dcadec.h"
#include "dcadata.h"
#include "dcahuff.h"
#include "dcamath.h"
#include "dca_syncwords.h"
#if ARCH_ARM
#include "arm/dca.h"
#endif
enum HeaderType {
HEADER_CORE,
HEADER_XCH,
HEADER_XXCH
};
static const int8_t prm_ch_to_spkr_map[DCA_AMODE_COUNT][5] = {
{ DCA_SPEAKER_C, -1, -1, -1, -1 },
{ DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
{ DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
{ DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
{ DCA_SPEAKER_L, DCA_SPEAKER_R, -1, -1, -1 },
{ DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , -1, -1 },
{ DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Cs, -1, -1 },
{ DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R , DCA_SPEAKER_Cs, -1 },
{ DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs, -1 },
{ DCA_SPEAKER_C, DCA_SPEAKER_L, DCA_SPEAKER_R, DCA_SPEAKER_Ls, DCA_SPEAKER_Rs }
};
static const uint8_t audio_mode_ch_mask[DCA_AMODE_COUNT] = {
DCA_SPEAKER_LAYOUT_MONO,
DCA_SPEAKER_LAYOUT_STEREO,
DCA_SPEAKER_LAYOUT_STEREO,
DCA_SPEAKER_LAYOUT_STEREO,
DCA_SPEAKER_LAYOUT_STEREO,
DCA_SPEAKER_LAYOUT_3_0,
DCA_SPEAKER_LAYOUT_2_1,
DCA_SPEAKER_LAYOUT_3_1,
DCA_SPEAKER_LAYOUT_2_2,
DCA_SPEAKER_LAYOUT_5POINT0
};
static const uint8_t block_code_nbits[7] = {
7, 10, 12, 13, 15, 17, 19
};
static int dca_get_vlc(GetBitContext *s, DCAVLC *v, int i)
{
return get_vlc2(s, v->vlc[i].table, v->vlc[i].bits, v->max_depth) + v->offset;
}
static void get_array(GetBitContext *s, int32_t *array, int size, int n)
{
int i;
for (i = 0; i < size; i++)
array[i] = get_sbits(s, n);
}
// 5.3.1 - Bit stream header
static int parse_frame_header(DCACoreDecoder *s)
{
DCACoreFrameHeader h = { 0 };
int err = ff_dca_parse_core_frame_header(&h, &s->gb);
if (err < 0) {
switch (err) {
case DCA_PARSE_ERROR_DEFICIT_SAMPLES:
av_log(s->avctx, AV_LOG_ERROR, "Deficit samples are not supported\n");
return h.normal_frame ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
case DCA_PARSE_ERROR_PCM_BLOCKS:
av_log(s->avctx, AV_LOG_ERROR, "Unsupported number of PCM sample blocks (%d)\n", h.npcmblocks);
return (h.npcmblocks < 6 || h.normal_frame) ? AVERROR_INVALIDDATA : AVERROR_PATCHWELCOME;
case DCA_PARSE_ERROR_FRAME_SIZE:
av_log(s->avctx, AV_LOG_ERROR, "Invalid core frame size (%d bytes)\n", h.frame_size);
return AVERROR_INVALIDDATA;
case DCA_PARSE_ERROR_AMODE:
av_log(s->avctx, AV_LOG_ERROR, "Unsupported audio channel arrangement (%d)\n", h.audio_mode);
return AVERROR_PATCHWELCOME;
case DCA_PARSE_ERROR_SAMPLE_RATE:
av_log(s->avctx, AV_LOG_ERROR, "Invalid core audio sampling frequency\n");
return AVERROR_INVALIDDATA;
case DCA_PARSE_ERROR_RESERVED_BIT:
av_log(s->avctx, AV_LOG_ERROR, "Reserved bit set\n");
return AVERROR_INVALIDDATA;
case DCA_PARSE_ERROR_LFE_FLAG:
av_log(s->avctx, AV_LOG_ERROR, "Invalid low frequency effects flag\n");
return AVERROR_INVALIDDATA;
case DCA_PARSE_ERROR_PCM_RES:
av_log(s->avctx, AV_LOG_ERROR, "Invalid source PCM resolution\n");
return AVERROR_INVALIDDATA;
default:
av_log(s->avctx, AV_LOG_ERROR, "Unknown core frame header error\n");
return AVERROR_INVALIDDATA;
}
}
s->crc_present = h.crc_present;
s->npcmblocks = h.npcmblocks;
s->frame_size = h.frame_size;
s->audio_mode = h.audio_mode;
s->sample_rate = avpriv_dca_sample_rates[h.sr_code];
s->bit_rate = ff_dca_bit_rates[h.br_code];
s->drc_present = h.drc_present;
s->ts_present = h.ts_present;
s->aux_present = h.aux_present;
s->ext_audio_type = h.ext_audio_type;
s->ext_audio_present = h.ext_audio_present;
s->sync_ssf = h.sync_ssf;
s->lfe_present = h.lfe_present;
s->predictor_history = h.predictor_history;
s->filter_perfect = h.filter_perfect;
s->source_pcm_res = ff_dca_bits_per_sample[h.pcmr_code];
s->es_format = h.pcmr_code & 1;
s->sumdiff_front = h.sumdiff_front;
s->sumdiff_surround = h.sumdiff_surround;
return 0;
}
// 5.3.2 - Primary audio coding header
static int parse_coding_header(DCACoreDecoder *s, enum HeaderType header, int xch_base)
{
int n, ch, nchannels, header_size = 0, header_pos = get_bits_count(&s->gb);
unsigned int mask, index;
if (get_bits_left(&s->gb) < 0)
return AVERROR_INVALIDDATA;
switch (header) {
case HEADER_CORE:
// Number of subframes
s->nsubframes = get_bits(&s->gb, 4) + 1;
// Number of primary audio channels
s->nchannels = get_bits(&s->gb, 3) + 1;
if (s->nchannels != ff_dca_channels[s->audio_mode]) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid number of primary audio channels (%d) for audio channel arrangement (%d)\n", s->nchannels, s->audio_mode);
return AVERROR_INVALIDDATA;
}
av_assert1(s->nchannels <= DCA_CHANNELS - 2);
s->ch_mask = audio_mode_ch_mask[s->audio_mode];
// Add LFE channel if present
if (s->lfe_present)
s->ch_mask |= DCA_SPEAKER_MASK_LFE1;
break;
case HEADER_XCH:
s->nchannels = ff_dca_channels[s->audio_mode] + 1;
av_assert1(s->nchannels <= DCA_CHANNELS - 1);
s->ch_mask |= DCA_SPEAKER_MASK_Cs;
break;
case HEADER_XXCH:
// Channel set header length
header_size = get_bits(&s->gb, 7) + 1;
// Check CRC
if (s->xxch_crc_present
&& ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH channel set header checksum\n");
return AVERROR_INVALIDDATA;
}
// Number of channels in a channel set
nchannels = get_bits(&s->gb, 3) + 1;
if (nchannels > DCA_XXCH_CHANNELS_MAX) {
avpriv_request_sample(s->avctx, "%d XXCH channels", nchannels);
return AVERROR_PATCHWELCOME;
}
s->nchannels = ff_dca_channels[s->audio_mode] + nchannels;
av_assert1(s->nchannels <= DCA_CHANNELS);
// Loudspeaker layout mask
mask = get_bits_long(&s->gb, s->xxch_mask_nbits - DCA_SPEAKER_Cs);
s->xxch_spkr_mask = mask << DCA_SPEAKER_Cs;
if (av_popcount(s->xxch_spkr_mask) != nchannels) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH speaker layout mask (%#x)\n", s->xxch_spkr_mask);
return AVERROR_INVALIDDATA;
}
if (s->xxch_core_mask & s->xxch_spkr_mask) {
av_log(s->avctx, AV_LOG_ERROR, "XXCH speaker layout mask (%#x) overlaps with core (%#x)\n", s->xxch_spkr_mask, s->xxch_core_mask);
return AVERROR_INVALIDDATA;
}
// Combine core and XXCH masks together
s->ch_mask = s->xxch_core_mask | s->xxch_spkr_mask;
// Downmix coefficients present in stream
if (get_bits1(&s->gb)) {
int *coeff_ptr = s->xxch_dmix_coeff;
// Downmix already performed by encoder
s->xxch_dmix_embedded = get_bits1(&s->gb);
// Downmix scale factor
index = get_bits(&s->gb, 6) * 4 - FF_DCA_DMIXTABLE_OFFSET - 3;
if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix scale index (%d)\n", index);
return AVERROR_INVALIDDATA;
}
s->xxch_dmix_scale_inv = ff_dca_inv_dmixtable[index];
// Downmix channel mapping mask
for (ch = 0; ch < nchannels; ch++) {
mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
if ((mask & s->xxch_core_mask) != mask) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix channel mapping mask (%#x)\n", mask);
return AVERROR_INVALIDDATA;
}
s->xxch_dmix_mask[ch] = mask;
}
// Downmix coefficients
for (ch = 0; ch < nchannels; ch++) {
for (n = 0; n < s->xxch_mask_nbits; n++) {
if (s->xxch_dmix_mask[ch] & (1U << n)) {
int code = get_bits(&s->gb, 7);
int sign = (code >> 6) - 1;
if (code &= 63) {
index = code * 4 - 3;
if (index >= FF_DCA_DMIXTABLE_SIZE) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH downmix coefficient index (%d)\n", index);
return AVERROR_INVALIDDATA;
}
*coeff_ptr++ = (ff_dca_dmixtable[index] ^ sign) - sign;
} else {
*coeff_ptr++ = 0;
}
}
}
}
} else {
s->xxch_dmix_embedded = 0;
}
break;
}
// Subband activity count
for (ch = xch_base; ch < s->nchannels; ch++) {
s->nsubbands[ch] = get_bits(&s->gb, 5) + 2;
if (s->nsubbands[ch] > DCA_SUBBANDS) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid subband activity count\n");
return AVERROR_INVALIDDATA;
}
}
// High frequency VQ start subband
for (ch = xch_base; ch < s->nchannels; ch++)
s->subband_vq_start[ch] = get_bits(&s->gb, 5) + 1;
// Joint intensity coding index
for (ch = xch_base; ch < s->nchannels; ch++) {
if ((n = get_bits(&s->gb, 3)) && header == HEADER_XXCH)
n += xch_base - 1;
if (n > s->nchannels) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid joint intensity coding index\n");
return AVERROR_INVALIDDATA;
}
s->joint_intensity_index[ch] = n;
}
// Transient mode code book
for (ch = xch_base; ch < s->nchannels; ch++)
s->transition_mode_sel[ch] = get_bits(&s->gb, 2);
// Scale factor code book
for (ch = xch_base; ch < s->nchannels; ch++) {
s->scale_factor_sel[ch] = get_bits(&s->gb, 3);
if (s->scale_factor_sel[ch] == 7) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor code book\n");
return AVERROR_INVALIDDATA;
}
}
// Bit allocation quantizer select
for (ch = xch_base; ch < s->nchannels; ch++) {
s->bit_allocation_sel[ch] = get_bits(&s->gb, 3);
if (s->bit_allocation_sel[ch] == 7) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation quantizer select\n");
return AVERROR_INVALIDDATA;
}
}
// Quantization index codebook select
for (n = 0; n < DCA_CODE_BOOKS; n++)
for (ch = xch_base; ch < s->nchannels; ch++)
s->quant_index_sel[ch][n] = get_bits(&s->gb, ff_dca_quant_index_sel_nbits[n]);
// Scale factor adjustment index
for (n = 0; n < DCA_CODE_BOOKS; n++)
for (ch = xch_base; ch < s->nchannels; ch++)
if (s->quant_index_sel[ch][n] < ff_dca_quant_index_group_size[n])
s->scale_factor_adj[ch][n] = ff_dca_scale_factor_adj[get_bits(&s->gb, 2)];
if (header == HEADER_XXCH) {
// Reserved
// Byte align
// CRC16 of channel set header
if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set header\n");
return AVERROR_INVALIDDATA;
}
} else {
// Audio header CRC check word
if (s->crc_present)
skip_bits(&s->gb, 16);
}
return 0;
}
static inline int parse_scale(DCACoreDecoder *s, int *scale_index, int sel)
{
const uint32_t *scale_table;
unsigned int scale_size;
// Select the root square table
if (sel > 5) {
scale_table = ff_dca_scale_factor_quant7;
scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
} else {
scale_table = ff_dca_scale_factor_quant6;
scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
}
// If Huffman code was used, the difference of scales was encoded
if (sel < 5)
*scale_index += dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
else
*scale_index = get_bits(&s->gb, sel + 1);
// Look up scale factor from the root square table
if ((unsigned int)*scale_index >= scale_size) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid scale factor index\n");
return AVERROR_INVALIDDATA;
}
return scale_table[*scale_index];
}
static inline int parse_joint_scale(DCACoreDecoder *s, int sel)
{
int scale_index;
// Absolute value was encoded even when Huffman code was used
if (sel < 5)
scale_index = dca_get_vlc(&s->gb, &ff_dca_vlc_scale_factor, sel);
else
scale_index = get_bits(&s->gb, sel + 1);
// Bias by 64
scale_index += 64;
// Look up joint scale factor
if ((unsigned int)scale_index >= FF_ARRAY_ELEMS(ff_dca_joint_scale_factors)) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor index\n");
return AVERROR_INVALIDDATA;
}
return ff_dca_joint_scale_factors[scale_index];
}
// 5.4.1 - Primary audio coding side information
static int parse_subframe_header(DCACoreDecoder *s, int sf,
enum HeaderType header, int xch_base)
{
int ch, band, ret;
if (get_bits_left(&s->gb) < 0)
return AVERROR_INVALIDDATA;
if (header == HEADER_CORE) {
// Subsubframe count
s->nsubsubframes[sf] = get_bits(&s->gb, 2) + 1;
// Partial subsubframe sample count
skip_bits(&s->gb, 3);
}
// Prediction mode
for (ch = xch_base; ch < s->nchannels; ch++)
for (band = 0; band < s->nsubbands[ch]; band++)
s->prediction_mode[ch][band] = get_bits1(&s->gb);
// Prediction coefficients VQ address
for (ch = xch_base; ch < s->nchannels; ch++)
for (band = 0; band < s->nsubbands[ch]; band++)
if (s->prediction_mode[ch][band])
s->prediction_vq_index[ch][band] = get_bits(&s->gb, 12);
// Bit allocation index
for (ch = xch_base; ch < s->nchannels; ch++) {
int sel = s->bit_allocation_sel[ch];
for (band = 0; band < s->subband_vq_start[ch]; band++) {
int abits;
if (sel < 5)
abits = dca_get_vlc(&s->gb, &ff_dca_vlc_bit_allocation, sel);
else
abits = get_bits(&s->gb, sel - 1);
if (abits > DCA_ABITS_MAX) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid bit allocation index\n");
return AVERROR_INVALIDDATA;
}
s->bit_allocation[ch][band] = abits;
}
}
// Transition mode
for (ch = xch_base; ch < s->nchannels; ch++) {
// Clear transition mode for all subbands
memset(s->transition_mode[sf][ch], 0, sizeof(s->transition_mode[0][0]));
// Transient possible only if more than one subsubframe
if (s->nsubsubframes[sf] > 1) {
int sel = s->transition_mode_sel[ch];
for (band = 0; band < s->subband_vq_start[ch]; band++)
if (s->bit_allocation[ch][band])
s->transition_mode[sf][ch][band] = dca_get_vlc(&s->gb, &ff_dca_vlc_transition_mode, sel);
}
}
// Scale factors
for (ch = xch_base; ch < s->nchannels; ch++) {
int sel = s->scale_factor_sel[ch];
int scale_index = 0;
// Extract scales for subbands up to VQ
for (band = 0; band < s->subband_vq_start[ch]; band++) {
if (s->bit_allocation[ch][band]) {
if ((ret = parse_scale(s, &scale_index, sel)) < 0)
return ret;
s->scale_factors[ch][band][0] = ret;
if (s->transition_mode[sf][ch][band]) {
if ((ret = parse_scale(s, &scale_index, sel)) < 0)
return ret;
s->scale_factors[ch][band][1] = ret;
}
} else {
s->scale_factors[ch][band][0] = 0;
}
}
// High frequency VQ subbands
for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++) {
if ((ret = parse_scale(s, &scale_index, sel)) < 0)
return ret;
s->scale_factors[ch][band][0] = ret;
}
}
// Joint subband codebook select
for (ch = xch_base; ch < s->nchannels; ch++) {
if (s->joint_intensity_index[ch]) {
s->joint_scale_sel[ch] = get_bits(&s->gb, 3);
if (s->joint_scale_sel[ch] == 7) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid joint scale factor code book\n");
return AVERROR_INVALIDDATA;
}
}
}
// Scale factors for joint subband coding
for (ch = xch_base; ch < s->nchannels; ch++) {
int src_ch = s->joint_intensity_index[ch] - 1;
if (src_ch >= 0) {
int sel = s->joint_scale_sel[ch];
for (band = s->nsubbands[ch]; band < s->nsubbands[src_ch]; band++) {
if ((ret = parse_joint_scale(s, sel)) < 0)
return ret;
s->joint_scale_factors[ch][band] = ret;
}
}
}
// Dynamic range coefficient
if (s->drc_present && header == HEADER_CORE)
skip_bits(&s->gb, 8);
// Side information CRC check word
if (s->crc_present)
skip_bits(&s->gb, 16);
return 0;
}
#ifndef decode_blockcodes
static inline int decode_blockcodes(int code1, int code2, int levels, int32_t *audio)
{
int offset = (levels - 1) / 2;
int n, div;
for (n = 0; n < DCA_SUBBAND_SAMPLES / 2; n++) {
div = FASTDIV(code1, levels);
audio[n] = code1 - div * levels - offset;
code1 = div;
}
for (; n < DCA_SUBBAND_SAMPLES; n++) {
div = FASTDIV(code2, levels);
audio[n] = code2 - div * levels - offset;
code2 = div;
}
return code1 | code2;
}
#endif
static inline int parse_block_codes(DCACoreDecoder *s, int32_t *audio, int abits)
{
// Extract block code indices from the bit stream
int code1 = get_bits(&s->gb, block_code_nbits[abits - 1]);
int code2 = get_bits(&s->gb, block_code_nbits[abits - 1]);
int levels = ff_dca_quant_levels[abits];
// Look up samples from the block code book
if (decode_blockcodes(code1, code2, levels, audio)) {
av_log(s->avctx, AV_LOG_ERROR, "Failed to decode block code(s)\n");
return AVERROR_INVALIDDATA;
}
return 0;
}
static inline int parse_huffman_codes(DCACoreDecoder *s, int32_t *audio, int abits, int sel)
{
int i;
// Extract Huffman codes from the bit stream
for (i = 0; i < DCA_SUBBAND_SAMPLES; i++)
audio[i] = dca_get_vlc(&s->gb, &ff_dca_vlc_quant_index[abits - 1], sel);
return 1;
}
static inline int extract_audio(DCACoreDecoder *s, int32_t *audio, int abits, int ch)
{
av_assert1(abits >= 0 && abits <= DCA_ABITS_MAX);
if (abits == 0) {
// No bits allocated
memset(audio, 0, DCA_SUBBAND_SAMPLES * sizeof(*audio));
return 0;
}
if (abits <= DCA_CODE_BOOKS) {
int sel = s->quant_index_sel[ch][abits - 1];
if (sel < ff_dca_quant_index_group_size[abits - 1]) {
// Huffman codes
return parse_huffman_codes(s, audio, abits, sel);
}
if (abits <= 7) {
// Block codes
return parse_block_codes(s, audio, abits);
}
}
// No further encoding
get_array(&s->gb, audio, DCA_SUBBAND_SAMPLES, abits - 3);
return 0;
}
static inline void inverse_adpcm(int32_t **subband_samples,
const int16_t *vq_index,
const int8_t *prediction_mode,
int sb_start, int sb_end,
int ofs, int len)
{
int i, j;
for (i = sb_start; i < sb_end; i++) {
if (prediction_mode[i]) {
const int pred_id = vq_index[i];
int32_t *ptr = subband_samples[i] + ofs;
for (j = 0; j < len; j++) {
int32_t x = ff_dcaadpcm_predict(pred_id, ptr + j - DCA_ADPCM_COEFFS);
ptr[j] = clip23(ptr[j] + x);
}
}
}
}
// 5.5 - Primary audio data arrays
static int parse_subframe_audio(DCACoreDecoder *s, int sf, enum HeaderType header,
int xch_base, int *sub_pos, int *lfe_pos)
{
int32_t audio[16], scale;
int n, ssf, ofs, ch, band;
// Check number of subband samples in this subframe
int nsamples = s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES;
if (*sub_pos + nsamples > s->npcmblocks) {
av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
return AVERROR_INVALIDDATA;
}
if (get_bits_left(&s->gb) < 0)
return AVERROR_INVALIDDATA;
// VQ encoded subbands
for (ch = xch_base; ch < s->nchannels; ch++) {
int32_t vq_index[DCA_SUBBANDS];
for (band = s->subband_vq_start[ch]; band < s->nsubbands[ch]; band++)
// Extract the VQ address from the bit stream
vq_index[band] = get_bits(&s->gb, 10);
if (s->subband_vq_start[ch] < s->nsubbands[ch]) {
s->dcadsp->decode_hf(s->subband_samples[ch], vq_index,
ff_dca_high_freq_vq, s->scale_factors[ch],
s->subband_vq_start[ch], s->nsubbands[ch],
*sub_pos, nsamples);
}
}
// Low frequency effect data
if (s->lfe_present && header == HEADER_CORE) {
unsigned int index;
// Determine number of LFE samples in this subframe
int nlfesamples = 2 * s->lfe_present * s->nsubsubframes[sf];
av_assert1((unsigned int)nlfesamples <= FF_ARRAY_ELEMS(audio));
// Extract LFE samples from the bit stream
get_array(&s->gb, audio, nlfesamples, 8);
// Extract scale factor index from the bit stream
index = get_bits(&s->gb, 8);
if (index >= FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7)) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid LFE scale factor index\n");
return AVERROR_INVALIDDATA;
}
// Look up the 7-bit root square quantization table
scale = ff_dca_scale_factor_quant7[index];
// Account for quantizer step size which is 0.035
scale = mul23(4697620 /* 0.035 * (1 << 27) */, scale);
// Scale and take the LFE samples
for (n = 0, ofs = *lfe_pos; n < nlfesamples; n++, ofs++)
s->lfe_samples[ofs] = clip23(audio[n] * scale >> 4);
// Advance LFE sample pointer for the next subframe
*lfe_pos = ofs;
}
// Audio data
for (ssf = 0, ofs = *sub_pos; ssf < s->nsubsubframes[sf]; ssf++) {
for (ch = xch_base; ch < s->nchannels; ch++) {
if (get_bits_left(&s->gb) < 0)
return AVERROR_INVALIDDATA;
// Not high frequency VQ subbands
for (band = 0; band < s->subband_vq_start[ch]; band++) {
int ret, trans_ssf, abits = s->bit_allocation[ch][band];
int32_t step_size;
// Extract bits from the bit stream
if ((ret = extract_audio(s, audio, abits, ch)) < 0)
return ret;
// Select quantization step size table and look up
// quantization step size
if (s->bit_rate == 3)
step_size = ff_dca_lossless_quant[abits];
else
step_size = ff_dca_lossy_quant[abits];
// Identify transient location
trans_ssf = s->transition_mode[sf][ch][band];
// Determine proper scale factor
if (trans_ssf == 0 || ssf < trans_ssf)
scale = s->scale_factors[ch][band][0];
else
scale = s->scale_factors[ch][band][1];
// Adjust scale factor when SEL indicates Huffman code
if (ret > 0) {
int64_t adj = s->scale_factor_adj[ch][abits - 1];
scale = clip23(adj * scale >> 22);
}
ff_dca_core_dequantize(s->subband_samples[ch][band] + ofs,
audio, step_size, scale, 0, DCA_SUBBAND_SAMPLES);
}
}
// DSYNC
if ((ssf == s->nsubsubframes[sf] - 1 || s->sync_ssf) && get_bits(&s->gb, 16) != 0xffff) {
av_log(s->avctx, AV_LOG_ERROR, "DSYNC check failed\n");
return AVERROR_INVALIDDATA;
}
ofs += DCA_SUBBAND_SAMPLES;
}
// Inverse ADPCM
for (ch = xch_base; ch < s->nchannels; ch++) {
inverse_adpcm(s->subband_samples[ch], s->prediction_vq_index[ch],
s->prediction_mode[ch], 0, s->nsubbands[ch],
*sub_pos, nsamples);
}
// Joint subband coding
for (ch = xch_base; ch < s->nchannels; ch++) {
int src_ch = s->joint_intensity_index[ch] - 1;
if (src_ch >= 0) {
s->dcadsp->decode_joint(s->subband_samples[ch], s->subband_samples[src_ch],
s->joint_scale_factors[ch], s->nsubbands[ch],
s->nsubbands[src_ch], *sub_pos, nsamples);
}
}
// Advance subband sample pointer for the next subframe
*sub_pos = ofs;
return 0;
}
static void erase_adpcm_history(DCACoreDecoder *s)
{
int ch, band;
// Erase ADPCM history from previous frame if
// predictor history switch was disabled
for (ch = 0; ch < DCA_CHANNELS; ch++)
for (band = 0; band < DCA_SUBBANDS; band++)
AV_ZERO128(s->subband_samples[ch][band] - DCA_ADPCM_COEFFS);
emms_c();
}
static int alloc_sample_buffer(DCACoreDecoder *s)
{
int nchsamples = DCA_ADPCM_COEFFS + s->npcmblocks;
int nframesamples = nchsamples * DCA_CHANNELS * DCA_SUBBANDS;
int nlfesamples = DCA_LFE_HISTORY + s->npcmblocks / 2;
unsigned int size = s->subband_size;
int ch, band;
// Reallocate subband sample buffer
av_fast_mallocz(&s->subband_buffer, &s->subband_size,
(nframesamples + nlfesamples) * sizeof(int32_t));
if (!s->subband_buffer)
return AVERROR(ENOMEM);
if (size != s->subband_size) {
for (ch = 0; ch < DCA_CHANNELS; ch++)
for (band = 0; band < DCA_SUBBANDS; band++)
s->subband_samples[ch][band] = s->subband_buffer +
(ch * DCA_SUBBANDS + band) * nchsamples + DCA_ADPCM_COEFFS;
s->lfe_samples = s->subband_buffer + nframesamples;
}
if (!s->predictor_history)
erase_adpcm_history(s);
return 0;
}
static int parse_frame_data(DCACoreDecoder *s, enum HeaderType header, int xch_base)
{
int sf, ch, ret, band, sub_pos, lfe_pos;
if ((ret = parse_coding_header(s, header, xch_base)) < 0)
return ret;
for (sf = 0, sub_pos = 0, lfe_pos = DCA_LFE_HISTORY; sf < s->nsubframes; sf++) {
if ((ret = parse_subframe_header(s, sf, header, xch_base)) < 0)
return ret;
if ((ret = parse_subframe_audio(s, sf, header, xch_base, &sub_pos, &lfe_pos)) < 0)
return ret;
}
for (ch = xch_base; ch < s->nchannels; ch++) {
// Determine number of active subbands for this channel
int nsubbands = s->nsubbands[ch];
if (s->joint_intensity_index[ch])
nsubbands = FFMAX(nsubbands, s->nsubbands[s->joint_intensity_index[ch] - 1]);
// Update history for ADPCM
for (band = 0; band < nsubbands; band++) {
int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
AV_COPY128(samples, samples + s->npcmblocks);
}
// Clear inactive subbands
for (; band < DCA_SUBBANDS; band++) {
int32_t *samples = s->subband_samples[ch][band] - DCA_ADPCM_COEFFS;
memset(samples, 0, (DCA_ADPCM_COEFFS + s->npcmblocks) * sizeof(int32_t));
}
}
emms_c();
return 0;
}
static int parse_xch_frame(DCACoreDecoder *s)
{
int ret;
if (s->ch_mask & DCA_SPEAKER_MASK_Cs) {
av_log(s->avctx, AV_LOG_ERROR, "XCH with Cs speaker already present\n");
return AVERROR_INVALIDDATA;
}
if ((ret = parse_frame_data(s, HEADER_XCH, s->nchannels)) < 0)
return ret;
// Seek to the end of core frame, don't trust XCH frame size
if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
av_log(s->avctx, AV_LOG_ERROR, "Read past end of XCH frame\n");
return AVERROR_INVALIDDATA;
}
return 0;
}
static int parse_xxch_frame(DCACoreDecoder *s)
{
int xxch_nchsets, xxch_frame_size;
int ret, mask, header_size, header_pos = get_bits_count(&s->gb);
// XXCH sync word
if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XXCH) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH sync word\n");
return AVERROR_INVALIDDATA;
}
// XXCH frame header length
header_size = get_bits(&s->gb, 6) + 1;
// Check XXCH frame header CRC
if (ff_dca_check_crc(s->avctx, &s->gb, header_pos + 32, header_pos + header_size * 8)) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid XXCH frame header checksum\n");
return AVERROR_INVALIDDATA;
}
// CRC presence flag for channel set header
s->xxch_crc_present = get_bits1(&s->gb);
// Number of bits for loudspeaker mask
s->xxch_mask_nbits = get_bits(&s->gb, 5) + 1;
if (s->xxch_mask_nbits <= DCA_SPEAKER_Cs) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XXCH speaker mask (%d)\n", s->xxch_mask_nbits);
return AVERROR_INVALIDDATA;
}
// Number of channel sets
xxch_nchsets = get_bits(&s->gb, 2) + 1;
if (xxch_nchsets > 1) {
avpriv_request_sample(s->avctx, "%d XXCH channel sets", xxch_nchsets);
return AVERROR_PATCHWELCOME;
}
// Channel set 0 data byte size
xxch_frame_size = get_bits(&s->gb, 14) + 1;
// Core loudspeaker activity mask
s->xxch_core_mask = get_bits_long(&s->gb, s->xxch_mask_nbits);
// Validate the core mask
mask = s->ch_mask;
if ((mask & DCA_SPEAKER_MASK_Ls) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Lss))
mask = (mask & ~DCA_SPEAKER_MASK_Ls) | DCA_SPEAKER_MASK_Lss;
if ((mask & DCA_SPEAKER_MASK_Rs) && (s->xxch_core_mask & DCA_SPEAKER_MASK_Rss))
mask = (mask & ~DCA_SPEAKER_MASK_Rs) | DCA_SPEAKER_MASK_Rss;
if (mask != s->xxch_core_mask) {
av_log(s->avctx, AV_LOG_ERROR, "XXCH core speaker activity mask (%#x) disagrees with core (%#x)\n", s->xxch_core_mask, mask);
return AVERROR_INVALIDDATA;
}
// Reserved
// Byte align
// CRC16 of XXCH frame header
if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH frame header\n");
return AVERROR_INVALIDDATA;
}
// Parse XXCH channel set 0
if ((ret = parse_frame_data(s, HEADER_XXCH, s->nchannels)) < 0)
return ret;
if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8 + xxch_frame_size * 8)) {
av_log(s->avctx, AV_LOG_ERROR, "Read past end of XXCH channel set\n");
return AVERROR_INVALIDDATA;
}
return 0;
}
static int parse_xbr_subframe(DCACoreDecoder *s, int xbr_base_ch, int xbr_nchannels,
int *xbr_nsubbands, int xbr_transition_mode, int sf, int *sub_pos)
{
int xbr_nabits[DCA_CHANNELS];
int xbr_bit_allocation[DCA_CHANNELS][DCA_SUBBANDS];
int xbr_scale_nbits[DCA_CHANNELS];
int32_t xbr_scale_factors[DCA_CHANNELS][DCA_SUBBANDS][2];
int ssf, ch, band, ofs;
// Check number of subband samples in this subframe
if (*sub_pos + s->nsubsubframes[sf] * DCA_SUBBAND_SAMPLES > s->npcmblocks) {
av_log(s->avctx, AV_LOG_ERROR, "Subband sample buffer overflow\n");
return AVERROR_INVALIDDATA;
}
if (get_bits_left(&s->gb) < 0)
return AVERROR_INVALIDDATA;
// Number of bits for XBR bit allocation index
for (ch = xbr_base_ch; ch < xbr_nchannels; ch++)
xbr_nabits[ch] = get_bits(&s->gb, 2) + 2;
// XBR bit allocation index
for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
for (band = 0; band < xbr_nsubbands[ch]; band++) {
xbr_bit_allocation[ch][band] = get_bits(&s->gb, xbr_nabits[ch]);
if (xbr_bit_allocation[ch][band] > DCA_ABITS_MAX) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR bit allocation index\n");
return AVERROR_INVALIDDATA;
}
}
}
// Number of bits for scale indices
for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
xbr_scale_nbits[ch] = get_bits(&s->gb, 3);
if (!xbr_scale_nbits[ch]) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid number of bits for XBR scale factor index\n");
return AVERROR_INVALIDDATA;
}
}
// XBR scale factors
for (ch = xbr_base_ch; ch < xbr_nchannels; ch++) {
const uint32_t *scale_table;
int scale_size;
// Select the root square table
if (s->scale_factor_sel[ch] > 5) {
scale_table = ff_dca_scale_factor_quant7;
scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant7);
} else {
scale_table = ff_dca_scale_factor_quant6;
scale_size = FF_ARRAY_ELEMS(ff_dca_scale_factor_quant6);
}
// Parse scale factor indices and look up scale factors from the root
// square table
for (band = 0; band < xbr_nsubbands[ch]; band++) {
if (xbr_bit_allocation[ch][band]) {
int scale_index = get_bits(&s->gb, xbr_scale_nbits[ch]);
if (scale_index >= scale_size) {
av_log(s->avctx, AV_LOG_ERROR, "Invalid XBR scale factor index\n");
return AVERROR_INVALIDDATA;
}
xbr_scale_factors[ch][band][0] = scale_table[scale_index];