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 #include "config_components.h"
33
34 #include <math.h>
35 #include <stddef.h>
36
45
52
54
55 #define IMC_BLOCK_SIZE 64
56 #define IMC_FRAME_ID 0x21
59
69
81
85
88
89 /** MDCT tables */
91
94
101
103
106
109
111
112 #define IMC_VLC_BITS 9
113 #define VLC_TABLES_SIZE 9512
114
116
118 {
119 return 3.5 * atan((freq / 7500.0) * (freq / 7500.0)) + 13.0 * atan(freq * 0.00076);
120 }
121
123 {
124 double freqmin[32], freqmid[32], freqmax[32];
125 double scale = sampling_rate / (256.0 * 2.0 * 2.0);
126 double nyquist_freq = sampling_rate * 0.5;
127 double freq, bark, prev_bark = 0, tf, tb;
129
130 for (
i = 0;
i < 32;
i++) {
133
135 tb = bark - prev_bark;
138 }
139 prev_bark = bark;
140
142
143 tf = freq;
144 while (tf < nyquist_freq) {
145 tf += 0.5;
147 if (tb > bark + 0.5)
148 break;
149 }
151
152 tf = freq;
153 while (tf > 0.0) {
154 tf -= 0.5;
156 if (tb <= bark - 0.5)
157 break;
158 }
160 }
161
162 for (
i = 0;
i < 32;
i++) {
164 for (j = 31; j > 0 && freq <= freqmid[j]; j--);
166
168 for (j = 0; j < 32 && freq >= freqmid[j]; j++);
170 }
171 }
172
174 {
176 /* initialize the VLC tables */
177 for (
int i = 0;
i < 4 ;
i++) {
178 for (int j = 0; j < 4; j++) {
183 0, 0);
184 }
185 }
186 }
187
189 {
193 float scale = 1.0f / (16384);
194
197 "Strange sample rate of %i, file likely corrupt or "
198 "needing a new table derivation method.\n",
201 }
202
206 }
207
211 }
212
215
218 }
219
220 /* Build mdct window, a simple sine window normalized with sqrt(2) */
224
225 /* Generate a square root table */
226 for (
i = 0;
i < 30;
i++)
228
231 } else {
236 }
237
241
245
247
249
251
252 return 0;
253 }
254
256 float *flcoeffs2, int *bandWidthT,
257 float *flcoeffs3, float *flcoeffs5)
258 {
262 float snr_limit = 1.e-30;
263 float accum = 0.0;
265
267 flcoeffs5[
i] = workT2[
i] = 0.0;
269 workT1[
i] = flcoeffs1[
i] * flcoeffs1[
i];
270 flcoeffs3[
i] = 2.0 * flcoeffs2[
i];
271 } else {
273 flcoeffs3[
i] = -30000.0;
274 }
275 workT3[
i] = bandWidthT[
i] * workT1[
i] * 0.01;
276 if (workT3[
i] <= snr_limit)
278 }
279
281 for (cnt2 =
i; cnt2 < q->
cyclTab[
i]; cnt2++)
282 flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[
i];
283 workT2[cnt2 - 1] = workT2[cnt2 - 1] + workT3[
i];
284 }
285
287 accum = (workT2[
i - 1] + accum) * q->
weights1[
i - 1];
288 flcoeffs5[
i] += accum;
289 }
290
293
295 for (cnt2 =
i - 1; cnt2 > q->
cyclTab2[
i]; cnt2--)
296 flcoeffs5[cnt2] += workT3[
i];
297 workT2[cnt2+1] += workT3[
i];
298 }
299
300 accum = 0.0;
301
303 accum = (workT2[
i+1] + accum) * q->
weights2[
i];
304 flcoeffs5[
i] += accum;
305 // there is missing code here, but it seems to never be triggered
306 }
307 }
308
309
311 int *levlCoeffs)
312 {
314 int start = 0;
315 const uint8_t *cb_sel;
316 int s = stream_format_code >> 1;
318
320
321 if (stream_format_code & 4)
322 start = 1;
323 if (start)
328 if (levlCoeffs[
i] == 17)
330 }
331 }
332
334 int *levlCoeffs)
335 {
337
342 }
343
345 float *flcoeffs1, float *flcoeffs2)
346 {
349 // maybe some frequency division thingy
350
351 flcoeffs1[0] = 20000.0 /
exp2 (levlCoeffBuf[0] * 0.18945);
// 0.18945 = log2(10) * 0.05703125
352 flcoeffs2[0] =
log2f(flcoeffs1[0]);
354 tmp2 = flcoeffs2[0];
355
361 } else {
364 else if (
level <= 24)
366 else
368
370 tmp2 += 0.83048 *
level;
// 0.83048 = log2(10) * 0.25
373 }
374 }
375 }
376
377
379 float *old_floor, float *flcoeffs1,
380 float *flcoeffs2)
381 {
383 /* FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
384 * and flcoeffs2 old scale factors
385 * might be incomplete due to a missing table that is in the binary code
386 */
389 if (levlCoeffBuf[
i] < 16) {
391 flcoeffs2[
i] = (levlCoeffBuf[
i] - 7) * 0.83048 + flcoeffs2[
i];
// 0.83048 = log2(10) * 0.25
392 } else {
393 flcoeffs1[
i] = old_floor[
i];
394 }
395 }
396 }
397
399 float *flcoeffs1, float *flcoeffs2)
400 {
403
405 flcoeffs1[
pos] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945);
// 0.18945 = log2(10) * 0.05703125
408 tmp2 = flcoeffs2[
pos];
409
410 levlCoeffBuf++;
413 continue;
414 level = *levlCoeffBuf++;
416 flcoeffs2[
i] = tmp2 - 1.4533435415 *
level;
// 1.4533435415 = log2(10) * 0.4375
417 }
418 }
419
420 /**
421 * Perform bit allocation depending on bits available
422 */
424 int stream_format_code,
int freebits,
int flag)
425 {
427 const float limit = -1.e20;
428 float highest = 0.0;
429 int indx;
430 int t1 = 0;
431 int t2 = 1;
432 float summa = 0.0;
433 int iacc = 0;
434 int summer = 0;
435 int rres, cwlen;
436 float lowest = 1.e10;
437 int low_indx = 0;
438 float workT[32];
439 int flg;
440 int found_indx = 0;
441
444
449 }
451 }
453
454 highest = highest * 0.25;
455
457 indx = -1;
459 indx = 0;
460
462 indx = 1;
463
465 indx = 2;
466
467 if (indx == -1)
469
471 }
472
473 if (stream_format_code & 0x2) {
478 }
479
480 for (
i = (stream_format_code & 0x2) ? 4 : 0;
i <
BANDS - 1;
i++) {
483 }
484
485 if (!iacc)
487
489 summa = (summa * 0.5 - freebits) / iacc;
490
491
493 rres = summer - freebits;
494 if ((rres >= -8) && (rres <= 8))
495 break;
496
497 summer = 0;
498 iacc = 0;
499
500 for (j = (stream_format_code & 0x2) ? 4 : 0; j <
BANDS; j++) {
502
505
506 if (cwlen > 0)
508 }
509
510 flg = t2;
511 t2 = 1;
512 if (freebits < summer)
513 t2 = -1;
515 flg = t2;
516 if (flg != t2)
517 t1++;
518
519 summa = (
float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
520 }
521
522 for (
i = (stream_format_code & 0x2) ? 4 : 0;
i <
BANDS;
i++) {
525 }
526
527 if (freebits > summer) {
531 }
532
533 highest = 0.0;
534
535 do {
536 if (highest <= -1.e20)
537 break;
538
539 found_indx = 0;
540 highest = -1.e20;
541
543 if (workT[
i] > highest) {
546 }
547 }
548
549 if (highest > -1.e20) {
550 workT[found_indx] -= 2.0;
552 workT[found_indx] = -1.e20;
553
554 for (j =
band_tab[found_indx]; j <
band_tab[found_indx + 1] && (freebits > summer); j++) {
556 summer++;
557 }
558 }
559 } while (freebits > summer);
560 }
561 if (freebits < summer) {
564 : 1.e20;
565 }
566 if (stream_format_code & 0x2) {
567 workT[0] = 1.e20;
568 workT[1] = 1.e20;
569 workT[2] = 1.e20;
570 workT[3] = 1.e20;
571 }
572 while (freebits < summer) {
573 lowest = 1.e10;
574 low_indx = 0;
576 if (workT[
i] < lowest) {
579 }
580 }
581 // if (lowest >= 1.e10)
582 // break;
583 workT[low_indx] = lowest + 2.0;
584
586 workT[low_indx] = 1.e20;
587
588 for (j =
band_tab[low_indx]; j <
band_tab[low_indx+1] && (freebits < summer); j++) {
591 summer--;
592 }
593 }
594 }
595 }
596 return 0;
597 }
598
600 {
602
607 continue;
608
611
616 }
617 } else {
624 } else {
630 } else {
636 } else { // 101
638 }
639 }
640 }
641 }
642
647 }
648 }
649 }
650 }
651
652 /**
653 * Increase highest' band coefficient sizes as some bits won't be used
654 */
656 int summer)
657 {
658 float workT[32];
659 int corrected = 0;
661 float highest = 0;
662 int found_indx = 0;
663
667 }
668
669 while (corrected < summer) {
670 if (highest <= -1.e20)
671 break;
672
673 highest = -1.e20;
674
676 if (workT[
i] > highest) {
679 }
680 }
681
682 if (highest > -1.e20) {
683 workT[found_indx] -= 2.0;
684 if (++(chctx->
bitsBandT[found_indx]) == 6)
685 workT[found_indx] = -1.e20;
686
687 for (j =
band_tab[found_indx]; j <
band_tab[found_indx+1] && (corrected < summer); j++) {
690 corrected++;
691 }
692 }
693 }
694 }
695 }
696
698 int stream_format_code)
699 {
701 int middle_value, cw_len, max_size;
702 const float *quantizer;
703
708
709 if (cw_len <= 0 || chctx->skipFlags[j])
710 continue;
711
712 max_size = 1 << cw_len;
713 middle_value = max_size >> 1;
714
717
718 if (cw_len >= 4) {
722 else
724 }else{
728 else
730 }
731 }
732 }
733 return 0;
734 }
735
736
739 {
740 int i, j, cw_len, cw;
741
744 continue;
748 cw = 0;
749
753 "Potential problem on band %i, coefficient %i"
754 ": cw_len=%i\n",
i, j, cw_len);
755 } else
757 }
758
760 }
761 }
762 }
763 }
764
766 {
768 int summer;
769
778 }
779
781
784 /* band has flag set and at least one coded coefficient */
788 }
789 }
790
791 /* calculate bits left, bits needed and adjust bit allocation */
792 summer = 0;
793
800 }
801 }
803 }
804 }
806 }
807
809 {
810 int stream_format_code;
811 int imc_hdr,
i, j,
ret;
814 int bitscount;
816
817
818 /* Check the frame header */
820 if (imc_hdr & 0x18) {
824 }
826
827 if (stream_format_code & 0x04)
829
836 }
837
839 if (stream_format_code & 0x1)
841 else
843
844 if (stream_format_code & 0x1)
847 else if (stream_format_code & 0x4)
850 else
853
858 }
859 }
860
862
863 if (stream_format_code & 0x1) {
869 }
870 } else {
874 } else
876 }
877
882
886 }
887
888 bitscount = 0;
889 /* first 4 bands will be assigned 5 bits per coefficient */
890 if (stream_format_code & 0x2) {
891 bitscount += 15;
892
897 for (
i = 1;
i < 4;
i++) {
898 if (stream_format_code & 0x1)
900 else
906 }
907 }
908 }
911 if (!(stream_format_code & 0x2))
912 bitscount += 16;
913 }
914
921 }
922
923 if (stream_format_code & 0x1) {
926 } else {
928 }
929
932
936 }
937
939
941
946 }
947
949
953 memcpy(chctx->
prev_win, q->
temp + 128,
sizeof(
float)*128);
954
955 return 0;
956 }
957
959 int *got_frame_ptr,
AVPacket *avpkt)
960 {
961 const uint8_t *buf = avpkt->
data;
962 int buf_size = avpkt->
size;
964
966
968
970
972
973 if (buf_size < IMC_BLOCK_SIZE * avctx->ch_layout.nb_channels) {
976 }
977
978 /* get output buffer */
982
985
987
989
991
994 }
995
999 }
1000
1001 *got_frame_ptr = 1;
1002
1004 }
1005
1007 {
1009
1012
1013 return 0;
1014 }
1015
1017 {
1019
1022 }
1023
1024 #if CONFIG_IMC_DECODER
1038 };
1039 #endif
1040 #if CONFIG_IAC_DECODER
1054 };
1055 #endif