1 /*
2 * IMC compatible decoder
3 * Copyright (c) 2002-2004 Maxim Poliakovski
4 * Copyright (c) 2006 Benjamin Larsson
5 * Copyright (c) 2006 Konstantin Shishkov
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
24 /**
25 * @file
26 * IMC - Intel Music Coder
27 * A mdct based codec using a 256 points large transform
28 * divided into 32 bands with some mix of scale factors.
29 * Only mono is supported.
30 */
31
32
33 #include <math.h>
34 #include <stddef.h>
35 #include <stdio.h>
36
43
50
52
53 #define IMC_BLOCK_SIZE 64
54 #define IMC_FRAME_ID 0x21
57
67
79
81
84
87
88 /** MDCT tables */
89 //@{
95 //@}
96
99
105
107
110
113
115
116 #define IMC_VLC_BITS 9
117 #define VLC_TABLES_SIZE 9512
118
120
122 {
123 return 3.5 * atan((freq / 7500.0) * (freq / 7500.0)) + 13.0 * atan(freq * 0.00076);
124 }
125
127 {
128 double freqmin[32], freqmid[32], freqmax[32];
129 double scale = sampling_rate / (256.0 * 2.0 * 2.0);
130 double nyquist_freq = sampling_rate * 0.5;
131 double freq, bark, prev_bark = 0,
tf,
tb;
133
134 for (
i = 0;
i < 32;
i++) {
137
139 tb = bark - prev_bark;
142 }
143 prev_bark = bark;
144
146
148 while (
tf < nyquist_freq) {
152 break;
153 }
155
160 if (
tb <= bark - 0.5)
161 break;
162 }
164 }
165
166 for (
i = 0;
i < 32;
i++) {
168 for (j = 31; j > 0 && freq <= freqmid[j]; j--);
170
172 for (j = 0; j < 32 && freq >= freqmid[j]; j++);
174 }
175 }
176
178 {
179 /* initialize the VLC tables */
180 for (
int i = 0,
offset = 0;
i < 4 ;
i++) {
181 for (int j = 0; j < 4; j++) {
189 }
190 }
191 }
192
194 {
199 double r1, r2;
200
203 "Strange sample rate of %i, file likely corrupt or "
204 "needing a new table derivation method.\n",
207 }
208
211
215 }
216
217 for (j = 0; j < avctx->
channels; j++) {
219
222
225 }
226
227 /* Build mdct window, a simple sine window normalized with sqrt(2) */
234
235 r1 = sin((
i * 4.0 + 1.0) / 1024.0 *
M_PI);
236 r2 = cos((
i * 4.0 + 1.0) / 1024.0 *
M_PI);
237
241 } else {
244 }
245 }
246
247 /* Generate a square root table */
248
249 for (
i = 0;
i < 30;
i++)
251
254 } else {
259 }
260
262 if (!fdsp)
269 }
271
275
277
278 return 0;
279 }
280
282 float *flcoeffs2, int *bandWidthT,
283 float *flcoeffs3, float *flcoeffs5)
284 {
288 float snr_limit = 1.e-30;
289 float accum = 0.0;
291
293 flcoeffs5[
i] = workT2[
i] = 0.0;
295 workT1[
i] = flcoeffs1[
i] * flcoeffs1[
i];
296 flcoeffs3[
i] = 2.0 * flcoeffs2[
i];
297 } else {
299 flcoeffs3[
i] = -30000.0;
300 }
301 workT3[
i] = bandWidthT[
i] * workT1[
i] * 0.01;
302 if (workT3[
i] <= snr_limit)
304 }
305
307 for (cnt2 =
i; cnt2 < q->
cyclTab[
i]; cnt2++)
308 flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[
i];
309 workT2[cnt2 - 1] = workT2[cnt2 - 1] + workT3[
i];
310 }
311
313 accum = (workT2[
i - 1] + accum) * q->
weights1[
i - 1];
314 flcoeffs5[
i] += accum;
315 }
316
319
321 for (cnt2 =
i - 1; cnt2 > q->
cyclTab2[
i]; cnt2--)
322 flcoeffs5[cnt2] += workT3[
i];
323 workT2[cnt2+1] += workT3[
i];
324 }
325
326 accum = 0.0;
327
329 accum = (workT2[
i+1] + accum) * q->
weights2[
i];
330 flcoeffs5[
i] += accum;
331 // there is missing code here, but it seems to never be triggered
332 }
333 }
334
335
337 int *levlCoeffs)
338 {
341 int start = 0;
342 const uint8_t *cb_sel;
344
345 s = stream_format_code >> 1;
351
352 if (stream_format_code & 4)
353 start = 1;
354 if (start)
359 if (levlCoeffs[
i] == 17)
361 }
362 }
363
365 int *levlCoeffs)
366 {
368
373 }
374
376 float *flcoeffs1, float *flcoeffs2)
377 {
380 // maybe some frequency division thingy
381
382 flcoeffs1[0] = 20000.0 /
exp2 (levlCoeffBuf[0] * 0.18945);
// 0.18945 = log2(10) * 0.05703125
383 flcoeffs2[0] =
log2f(flcoeffs1[0]);
385 tmp2 = flcoeffs2[0];
386
392 } else {
395 else if (
level <= 24)
397 else
399
401 tmp2 += 0.83048 *
level;
// 0.83048 = log2(10) * 0.25
404 }
405 }
406 }
407
408
410 float *old_floor, float *flcoeffs1,
411 float *flcoeffs2)
412 {
414 /* FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
415 * and flcoeffs2 old scale factors
416 * might be incomplete due to a missing table that is in the binary code
417 */
420 if (levlCoeffBuf[
i] < 16) {
422 flcoeffs2[
i] = (levlCoeffBuf[
i] - 7) * 0.83048 + flcoeffs2[
i];
// 0.83048 = log2(10) * 0.25
423 } else {
424 flcoeffs1[
i] = old_floor[
i];
425 }
426 }
427 }
428
430 float *flcoeffs1, float *flcoeffs2)
431 {
434
436 flcoeffs1[
pos] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945);
// 0.18945 = log2(10) * 0.05703125
439 tmp2 = flcoeffs2[
pos];
440
441 levlCoeffBuf++;
444 continue;
445 level = *levlCoeffBuf++;
447 flcoeffs2[
i] = tmp2 - 1.4533435415 *
level;
// 1.4533435415 = log2(10) * 0.4375
448 }
449 }
450
451 /**
452 * Perform bit allocation depending on bits available
453 */
455 int stream_format_code,
int freebits,
int flag)
456 {
458 const float limit = -1.e20;
459 float highest = 0.0;
460 int indx;
463 float summa = 0.0;
464 int iacc = 0;
465 int summer = 0;
466 int rres, cwlen;
467 float lowest = 1.e10;
468 int low_indx = 0;
469 float workT[32];
470 int flg;
471 int found_indx = 0;
472
475
480 }
482 }
484
485 highest = highest * 0.25;
486
488 indx = -1;
490 indx = 0;
491
493 indx = 1;
494
496 indx = 2;
497
498 if (indx == -1)
500
502 }
503
504 if (stream_format_code & 0x2) {
509 }
510
511 for (
i = (stream_format_code & 0x2) ? 4 : 0;
i <
BANDS - 1;
i++) {
514 }
515
516 if (!iacc)
518
520 summa = (summa * 0.5 - freebits) / iacc;
521
522
524 rres = summer - freebits;
525 if ((rres >= -8) && (rres <= 8))
526 break;
527
528 summer = 0;
529 iacc = 0;
530
531 for (j = (stream_format_code & 0x2) ? 4 : 0; j <
BANDS; j++) {
533
536
537 if (cwlen > 0)
539 }
540
543 if (freebits < summer)
549
550 summa = (float)(summer - freebits) / ((
t1 + 1) * iacc) + summa;
551 }
552
553 for (
i = (stream_format_code & 0x2) ? 4 : 0;
i <
BANDS;
i++) {
556 }
557
558 if (freebits > summer) {
562 }
563
564 highest = 0.0;
565
566 do {
567 if (highest <= -1.e20)
568 break;
569
570 found_indx = 0;
571 highest = -1.e20;
572
574 if (workT[
i] > highest) {
577 }
578 }
579
580 if (highest > -1.e20) {
581 workT[found_indx] -= 2.0;
583 workT[found_indx] = -1.e20;
584
585 for (j =
band_tab[found_indx]; j <
band_tab[found_indx + 1] && (freebits > summer); j++) {
587 summer++;
588 }
589 }
590 } while (freebits > summer);
591 }
592 if (freebits < summer) {
595 : 1.e20;
596 }
597 if (stream_format_code & 0x2) {
598 workT[0] = 1.e20;
599 workT[1] = 1.e20;
600 workT[2] = 1.e20;
601 workT[3] = 1.e20;
602 }
603 while (freebits < summer) {
604 lowest = 1.e10;
605 low_indx = 0;
607 if (workT[
i] < lowest) {
610 }
611 }
612 // if (lowest >= 1.e10)
613 // break;
614 workT[low_indx] = lowest + 2.0;
615
617 workT[low_indx] = 1.e20;
618
619 for (j =
band_tab[low_indx]; j <
band_tab[low_indx+1] && (freebits < summer); j++) {
622 summer--;
623 }
624 }
625 }
626 }
627 return 0;
628 }
629
631 {
633
638 continue;
639
642
647 }
648 } else {
655 } else {
661 } else {
667 } else { // 101
669 }
670 }
671 }
672 }
673
678 }
679 }
680 }
681 }
682
683 /**
684 * Increase highest' band coefficient sizes as some bits won't be used
685 */
687 int summer)
688 {
689 float workT[32];
690 int corrected = 0;
692 float highest = 0;
693 int found_indx = 0;
694
698 }
699
700 while (corrected < summer) {
701 if (highest <= -1.e20)
702 break;
703
704 highest = -1.e20;
705
707 if (workT[
i] > highest) {
710 }
711 }
712
713 if (highest > -1.e20) {
714 workT[found_indx] -= 2.0;
715 if (++(chctx->
bitsBandT[found_indx]) == 6)
716 workT[found_indx] = -1.e20;
717
718 for (j =
band_tab[found_indx]; j <
band_tab[found_indx+1] && (corrected < summer); j++) {
721 corrected++;
722 }
723 }
724 }
725 }
726 }
727
729 {
734
735 /* prerotation */
741 }
742
743 /* FFT */
746
747 /* postrotation, window and reorder */
755 dst1 += 2;
756 dst2 -= 2;
758 }
759 }
760
762 int stream_format_code)
763 {
765 int middle_value, cw_len, max_size;
766 const float *quantizer;
767
772
773 if (cw_len <= 0 || chctx->skipFlags[j])
774 continue;
775
776 max_size = 1 << cw_len;
777 middle_value = max_size >> 1;
778
781
782 if (cw_len >= 4) {
786 else
788 }else{
792 else
794 }
795 }
796 }
797 return 0;
798 }
799
800
803 {
804 int i, j, cw_len, cw;
805
808 continue;
812 cw = 0;
813
817 "Potential problem on band %i, coefficient %i"
818 ": cw_len=%i\n",
i, j, cw_len);
819 } else
821 }
822
824 }
825 }
826 }
827 }
828
830 {
832 int summer;
833
842 }
843
845
848 /* band has flag set and at least one coded coefficient */
852 }
853 }
854
855 /* calculate bits left, bits needed and adjust bit allocation */
856 summer = 0;
857
864 }
865 }
867 }
868 }
870 }
871
873 {
874 int stream_format_code;
875 int imc_hdr,
i, j,
ret;
878 int counter, bitscount;
880
881
882 /* Check the frame header */
884 if (imc_hdr & 0x18) {
888 }
890
891 if (stream_format_code & 0x04)
893
900 }
901
903 if (stream_format_code & 0x1)
905 else
907
908 if (stream_format_code & 0x1)
911 else if (stream_format_code & 0x4)
914 else
917
922 }
923 }
924
926
927 counter = 0;
928 if (stream_format_code & 0x1) {
934 }
935 } else {
939 counter++;
940 } else
942 }
943
948
952 }
953
954 bitscount = 0;
955 /* first 4 bands will be assigned 5 bits per coefficient */
956 if (stream_format_code & 0x2) {
957 bitscount += 15;
958
963 for (
i = 1;
i < 4;
i++) {
964 if (stream_format_code & 0x1)
966 else
972 }
973 }
974 }
977 if (!(stream_format_code & 0x2))
978 bitscount += 16;
979 }
980
987 }
988
989 if (stream_format_code & 0x1) {
992 } else {
994 }
995
998
1002 }
1003
1005
1007
1012 }
1013
1015
1017
1018 return 0;
1019 }
1020
1022 int *got_frame_ptr,
AVPacket *avpkt)
1023 {
1025 const uint8_t *buf = avpkt->
data;
1026 int buf_size = avpkt->
size;
1028
1030
1032
1034
1035 if (buf_size < IMC_BLOCK_SIZE * avctx->
channels) {
1038 }
1039
1040 /* get output buffer */
1044
1047
1049
1051
1053
1056 }
1057
1061 }
1062
1063 *got_frame_ptr = 1;
1064
1066 }
1067
1069 {
1071
1073
1074 return 0;
1075 }
1076
1078 {
1080
1083 }
1084
1085 #if CONFIG_IMC_DECODER
1100 };
1101 #endif
1102 #if CONFIG_IAC_DECODER
1117 };
1118 #endif