1 /*
2 * DCA encoder
3 * Copyright (C) 2008-2012 Alexander E. Patrakov
4 * 2010 Benjamin Larsson
5 * 2011 Xiang Wang
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
33
34 #define MAX_CHANNELS 6
35 #define DCA_MAX_FRAME_SIZE 16384
36 #define DCA_HEADER_SIZE 13
37 #define DCA_LFE_SAMPLES 8
38
39 #define DCA_SUBBANDS 32
41 #define SUBSUBFRAMES 2
42 #define SUBBAND_SAMPLES (SUBFRAMES * SUBSUBFRAMES * 8)
44
60
76
84
85 /* Transfer function of outer and middle ear, Hz -> dB */
86 static double hom(
double f)
87 {
88 double f1 = f / 1000;
89
90 return -3.64 * pow(f1, -0.8)
91 + 6.8 * exp(-0.6 * (f1 - 3.4) * (f1 - 3.4))
92 - 6.0 * exp(-0.15 * (f1 - 8.7) * (f1 - 8.7))
93 - 0.0006 * (f1 * f1) * (f1 * f1);
94 }
95
97 {
98 double h = (f -
fc[i]) /
erb[i];
99
100 h = 1 + h * h;
101 h = 1 / (h * h);
102 return 20 * log10(h);
103 }
104
106 {
109 int i, min_frame_bits;
110
117
118 if (!layout) {
120 "encoder will guess the layout, but it "
121 "might be incorrect.\n");
123 }
124 switch (layout) {
130 default:
133 }
134
137
138 for (i = 0; i < 9; i++) {
140 break;
141 }
142 if (i == 9)
145
149 }
151 ;
158
160
162
164 int j, k;
165
166 for (i = 0; i < 2048; i++) {
169 }
170
171 /* FIXME: probably incorrect */
172 for (i = 0; i < 256; i++) {
175 }
176
177 for (i = 0; i < 512; i++) {
180 }
181
182 for (i = 0; i < 9; i++) {
183 for (j = 0; j <
AUBANDS; j++) {
184 for (k = 0; k < 256; k++) {
186
188 }
189 }
190 }
191
192 for (i = 0; i < 256; i++) {
193 double add = 1 + pow(10, -0.01 * i);
195 }
196 for (j = 0; j < 8; j++) {
197 double accum = 0;
198 for (i = 0; i < 512; i++) {
200 accum += reconst * cos(2 *
M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
201 }
203 }
204 for (j = 0; j < 8; j++) {
205 double accum = 0;
206 for (i = 0; i < 512; i++) {
208 accum += reconst * cos(2 *
M_PI * (i + 0.5 - 256) * (j + 0.5) / 512);
209 }
211 }
212 }
213 return 0;
214 }
215
217 {
219 }
220
222 {
223 return cos_t(x - 512);
224 }
225
227 {
228 return (a + 1) >> 1;
229 }
230
232 {
233 int64_t
r = (int64_t)a * b + 0x80000000ULL;
234 return r >> 32;
235 }
236
238 {
239 int ch, subs, i, k, j;
240
242 /* History is copied because it is also needed for PSY */
244 int hist_start = 0;
245
246 for (i = 0; i < 512; i++)
248
253
254 /* Calculate the convolutions at once */
255 for (i = 0; i < 64; i++)
256 accum[i] = 0;
257
258 for (k = 0, i = hist_start, j = 0;
259 i < 512; k = (k + 1) & 63, i++, j++)
261 for (i = 0; i < hist_start; k = (k + 1) & 63, i++, j++)
263
264 for (k = 16; k < 32; k++)
265 accum[k] = accum[k] - accum[31 - k];
266 for (k = 32; k < 48; k++)
267 accum[k] = accum[k] + accum[95 - k];
268
269 for (band = 0; band < 32; band++) {
270 resp = 0;
271 for (i = 16; i < 48; i++) {
272 int s = (2 * band + 1) * (2 * (i + 16) + 1);
274 }
275
276 c->
subband[subs][
band][ch] = ((band + 1) & 2) ? -resp : resp;
277 }
278
279 /* Copy in 32 new samples from input */
280 for (i = 0; i < 32; i++)
281 hist[i + hist_start] = input[(subs * 32 + i) * c->
channels + ch];
282 hist_start = (hist_start + 32) & 511;
283 }
284 }
285 }
286
288 {
289 /* FIXME: make 128x LFE downsampling possible */
290 int i, j, lfes;
293 int hist_start = 0;
294
295 for (i = 0; i < 512; i++)
297
299 /* Calculate the convolution */
300 accum = 0;
301
302 for (i = hist_start, j = 0; i < 512; i++, j++)
304 for (i = 0; i < hist_start; i++, j++)
306
308
309 /* Copy in 64 new samples from input */
310 for (i = 0; i < 64; i++)
311 hist[i + hist_start] = input[(lfes * 64 + i) * c->
channels + c->
channels - 1];
312
313 hist_start = (hist_start + 64) & 511;
314 }
315 }
316
321
323 {
325 int i, j, k, l;
326
327 /* do two transforms in parallel */
328 for (i = 0; i < 256; i++) {
329 /* Apply the Hann window */
330 rin[i].
re =
mul32(in[2 * i], 0x3fffffff - (
cos_t(8 * i + 2) >> 1));
331 rin[i].
im =
mul32(in[2 * i + 1], 0x3fffffff - (
cos_t(8 * i + 6) >> 1));
332 }
333 /* pre-rotation */
334 for (i = 0; i < 256; i++) {
339 }
340
341 for (j = 256, l = 1; j != 1; j >>= 1, l <<= 1) {
342 for (k = 0; k < 256; k += j) {
343 for (i = k; i < k + j / 2; i++) {
346
347 sum.
re = buf[i].
re + buf[i + j / 2].
re;
348 sum.
im = buf[i].
im + buf[i + j / 2].
im;
349
350 diff.
re = buf[i].
re - buf[i + j / 2].
re;
351 diff.
im = buf[i].
im - buf[i + j / 2].
im;
352
355
360 }
361 }
362 }
363 /* post-rotation */
364 for (i = 0; i < 256; i++) {
370 }
371 for (i = 0; i < 256; i++) {
372 /* separate the results of the two transforms */
374
375 o1.
re = rout[i].
re - rout[255 - i].
re;
376 o1.
im = rout[i].
im + rout[255 - i].
im;
377
378 o2.
re = rout[i].
im - rout[255 - i].
im;
379 o2.
im = -rout[i].
re - rout[255 - i].
re;
380
381 /* combine them into one long transform */
386 }
387 }
388
390 {
392
393 res = 0;
394 if (in < 0)
396 for (i = 1024; i > 0; i >>= 1) {
398 res += i;
399 }
401 }
402
404 {
405 if (a < b)
407
408 if (a - b >= 256)
411 }
412
415 {
422 int i, j;
423
425
426 for (j = 0; j < 256; j++) {
428 out_cb_unnorm[j] = -2047; /* and can only grow */
429 }
430
431 for (i = 0; i <
AUBANDS; i++) {
432 denom = ca_cb; /* and can only grow */
433 for (j = 0; j < 256; j++)
434 denom =
add_cb(denom, power[j] +
auf[samplerate_index][i][j]);
435 for (j = 0; j < 256; j++)
436 out_cb_unnorm[j] =
add_cb(out_cb_unnorm[j],
437 -denom +
auf[samplerate_index][i][j]);
438 }
439
440 for (j = 0; j < 256; j++)
441 out_cb[j] =
add_cb(out_cb[j], -out_cb_unnorm[j] - ca_cb - cs_cb);
442 }
443
447
450 {
451 int f;
452
453 if (band == 0) {
454 for (f = 0; f < 4; f++)
455 walk(c, 0, 0, f, 0, -2047, channel, arg);
456 } else {
457 for (f = 0; f < 8; f++)
458 walk(c, band, band - 1, 8 * band - 4 + f,
460 }
461 }
462
465 {
466 int f;
467
468 if (band == 31) {
469 for (f = 0; f < 4; f++)
470 walk(c, 31, 31, 256 - 4 + f, 0, -2047, channel, arg);
471 } else {
472 for (f = 0; f < 8; f++)
473 walk(c, band, band + 1, 8 * band + 4 + f,
475 }
476 }
477
481 {
483
484 if (value < c->band_masking_cb[band1])
486 }
487
489 {
490 int i, k,
band, ch, ssf;
492
493 for (i = 0; i < 256; i++)
496
499 for (i = 0, k = 128 + 256 * ssf; k < 512; i++, k++)
501 for (k -= 512; i < 512; i++, k++)
502 data[i] = input[k * c->
channels + ch];
504 }
505 for (i = 0; i < 256; i++) {
507
512 }
513
514 for (band = 0; band < 32; band++) {
518 }
519 }
520
522 {
524
525 for (band = 0; band < 32; band++)
529
532 if (m < s)
534 }
536 }
537
541
546 }
547 }
548
550 #define USED_1ABITS 1
551 #define USED_NABITS 2
552 #define USED_26ABITS 4
553
555 {
557
561
562 /* attempt to guess the bit distribution based on the prevoius frame */
564 for (band = 0; band < 32; band++) {
566
567 if (snr_cb >= 1312) {
570 } else if (snr_cb >= 222) {
573 } else if (snr_cb >= 0) {
576 } else {
579 }
580 }
581 }
582
583 for (band = 0; band < 32; band++)
586 }
587
589 }
590
592 {
593 /* Find the bounds where the binary search should work */
594 int low, high, down;
595 int used_abits = 0;
596
602 low = high;
605 }
606 } else {
608 high = low;
610 goto out;
/* The requested bitrate is too high, pad with zeros */
613 }
614 }
615
616 /* Now do a binary search between low and high to see what fits */
617 for (down =
snr_fudge >> 1; down; down >>= 1) {
620 high -= down;
621 }
627 }
628
630 {
631 int k, ch;
632
633 for (k = 0; k < 512; k++)
634 for (ch = 0; ch < c->
channels; ch++)
636 }
637
639 {
641
643 value = value >> quant.
e;
645 }
646
648 {
650 int our_nscale, try_remove;
652
655
656 our_nscale = 127;
658
659 for (try_remove = 64; try_remove > 0; try_remove >>= 1) {
661 continue;
665 continue;
666 our_nscale -= try_remove;
667 }
668
669 if (our_nscale >= 125)
670 our_nscale = 124;
671
675
676 return our_nscale;
677 }
678
680 {
682
683 for (band = 0; band < 32; band++)
687 &c->
quant[band][ch]);
688
691 }
692
694 {
696
698 for (band = 0; band < 32; band++)
701 }
702
704 {
705 /* SYNC */
708
709 /* Frame type: normal */
711
712 /* Deficit sample count: none */
714
715 /* CRC is not present */
717
718 /* Number of PCM sample blocks */
720
721 /* Primary frame byte size */
723
724 /* Audio channel arrangement */
726
727 /* Core audio sampling frequency */
729
730 /* Transmission bit rate */
732
733 /* Embedded down mix: disabled */
735
736 /* Embedded dynamic range flag: not present */
738
739 /* Embedded time stamp flag: not present */
741
742 /* Auxiliary data flag: not present */
744
745 /* HDCD source: no */
747
748 /* Extension audio ID: N/A */
750
751 /* Extended audio data: not present */
753
754 /* Audio sync word insertion flag: after each sub-frame */
756
757 /* Low frequency effects flag: not present or 64x subsampling */
759
760 /* Predictor history switch flag: on */
762
763 /* No CRC */
764 /* Multirate interpolator switch: non-perfect reconstruction */
766
767 /* Encoder software revision: 7 */
769
770 /* Copy history: 0 */
772
773 /* Source PCM resolution: 16 bits, not DTS ES */
775
776 /* Front sum/difference coding: no */
778
779 /* Surrounds sum/difference coding: no */
781
782 /* Dialog normalization: 0 dB */
784 }
785
787 {
788 static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
789 static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
790
791 int ch, i;
792 /* Number of subframes */
794
795 /* Number of primary audio channels */
797
798 /* Subband activity count */
801
802 /* High frequency VQ start subband */
805
806 /* Joint intensity coding index: 0, 0 */
809
810 /* Transient mode codebook: A4, A4 (arbitrary) */
813
814 /* Scale factor code book: 7 bit linear, 7-bit sqrt table (for each channel) */
817
818 /* Bit allocation quantizer select: linear 5-bit */
821
822 /* Quantization index codebook select: dummy data
823 to avoid transmission of scale factor adjustment */
824 for (i = 1; i < 11; i++)
827
828 /* Scale factor adjustment index: not transmitted */
829 /* Audio header CRC check word: not transmitted */
830 }
831
833 {
834 if (c->
abits[band][ch] <= 7) {
835 int sum, i, j;
836 for (i = 0; i < 8; i += 4) {
837 sum = 0;
838 for (j = 3; j >= 0; j--) {
842 }
844 }
845 } else {
846 int i;
847 for (i = 0; i < 8; i++) {
851 }
852 }
853 }
854
856 {
858
859 /* Subsubframes count */
861
862 /* Partial subsubframe sample count: dummy */
864
865 /* Prediction mode: no ADPCM, in each channel and subband */
869
870 /* Prediction VQ addres: not transmitted */
871 /* Bit allocation index */
875
877 /* Transition mode: none for each channel and subband */
881 }
882
883 /* Scale factors */
887
888 /* Joint subband scale factor codebook select: not transmitted */
889 /* Scale factors for joint subband coding: not transmitted */
890 /* Stereo down-mix coefficients: not transmitted */
891 /* Dynamic range coefficient: not transmitted */
892 /* Stde information CRC check word: not transmitted */
893 /* VQ encoded high frequency subbands: not transmitted */
894
895 /* LFE data: 8 samples and scalefactor */
900 }
901
902 /* Audio data (subsubframes) */
907
908 /* DSYNC */
910 }
911
914 {
918
921
923
927
934
940
942
946 *got_packet_ptr = 1;
947 return 0;
948 }
949
951 { "b", "1411200" },
952 { NULL },
953 };
954
972 0 },
974 };