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
34 #include <math.h>
35 #include <stddef.h>
36 #include <stdio.h>
37
48
50
51 #define IMC_BLOCK_SIZE 64
52 #define IMC_FRAME_ID 0x21
55
65
77
79
82
85
86 /** MDCT tables */
87 //@{
93 //@}
94
97
103
105
109
111
112 #define VLC_TABLES_SIZE 9512
113
115 0, 640, 1156, 1732, 2308, 2852, 3396, 3924,
117 };
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;
132 int i, j;
133
134 for (i = 0; i < 32; i++) {
137
138 if (i > 0) {
139 tb = bark - prev_bark;
140 q->
weights1[i - 1] = pow(10.0, -1.0 * tb);
141 q->
weights2[i - 1] = pow(10.0, -2.7 * tb);
142 }
143 prev_bark = bark;
144
145 freqmid[i] = freq;
146
148 while (
tf < nyquist_freq) {
151 if (tb > bark + 0.5)
152 break;
153 }
155
160 if (tb <= bark - 0.5)
161 break;
162 }
164 }
165
166 for (i = 0; i < 32; i++) {
167 freq = freqmax[i];
168 for (j = 31; j > 0 && freq <= freqmid[j]; j--);
170
171 freq = freqmin[i];
172 for (j = 0; j < 32 && freq >= freqmid[j]; j++);
174 }
175 }
176
178 {
181 double r1, r2;
182
185 "Strange sample rate of %i, file likely corrupt or "
186 "needing a new table derivation method.\n",
189 }
190
193
197 }
198
199 for (j = 0; j < avctx->
channels; j++) {
201
202 for (i = 0; i <
BANDS; i++)
204
207 }
208
209 /* Build mdct window, a simple sine window normalized with sqrt(2) */
211 for (i = 0; i <
COEFFS; i++)
213 for (i = 0; i < COEFFS / 2; i++) {
214 q->
post_cos[i] = (1.0f / 32768) * cos(i / 256.0 *
M_PI);
215 q->
post_sin[i] = (1.0f / 32768) * sin(i / 256.0 *
M_PI);
216
217 r1 = sin((i * 4.0 + 1.0) / 1024.0 *
M_PI);
218 r2 = cos((i * 4.0 + 1.0) / 1024.0 *
M_PI);
219
220 if (i & 0x1) {
222 q->
pre_coef2[i] = -(r1 - r2) * sqrt(2.0);
223 } else {
224 q->
pre_coef1[i] = -(r1 + r2) * sqrt(2.0);
226 }
227 }
228
229 /* Generate a square root table */
230
231 for (i = 0; i < 30; i++)
233
234 /* initialize the VLC tables */
235 for (i = 0; i < 4 ; i++) {
236 for (j = 0; j < 4; j++) {
238 huffman_vlc[i][j].
table_allocated = vlc_offsets[i * 4 + j + 1] - vlc_offsets[i * 4 + j];
242 }
243 }
244
247 } else {
252 }
253
257 }
262
264 }
265
269
270 return 0;
271 }
272
274 float *flcoeffs2, int *bandWidthT,
275 float *flcoeffs3, float *flcoeffs5)
276 {
280 float snr_limit = 1.e-30;
281 float accum = 0.0;
282 int i, cnt2;
283
284 for (i = 0; i <
BANDS; i++) {
285 flcoeffs5[i] = workT2[i] = 0.0;
286 if (bandWidthT[i]) {
287 workT1[i] = flcoeffs1[i] * flcoeffs1[i];
288 flcoeffs3[i] = 2.0 * flcoeffs2[i];
289 } else {
290 workT1[i] = 0.0;
291 flcoeffs3[i] = -30000.0;
292 }
293 workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
294 if (workT3[i] <= snr_limit)
295 workT3[i] = 0.0;
296 }
297
298 for (i = 0; i <
BANDS; i++) {
299 for (cnt2 = i; cnt2 < q->
cyclTab[i]; cnt2++)
300 flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
301 workT2[cnt2 - 1] = workT2[cnt2 - 1] + workT3[i];
302 }
303
304 for (i = 1; i <
BANDS; i++) {
305 accum = (workT2[i - 1] + accum) * q->
weights1[i - 1];
306 flcoeffs5[i] += accum;
307 }
308
309 for (i = 0; i <
BANDS; i++)
310 workT2[i] = 0.0;
311
312 for (i = 0; i <
BANDS; i++) {
313 for (cnt2 = i - 1; cnt2 > q->
cyclTab2[i]; cnt2--)
314 flcoeffs5[cnt2] += workT3[i];
315 workT2[cnt2+1] += workT3[i];
316 }
317
318 accum = 0.0;
319
320 for (i = BANDS-2; i >= 0; i--) {
321 accum = (workT2[i+1] + accum) * q->
weights2[i];
322 flcoeffs5[i] += accum;
323 // there is missing code here, but it seems to never be triggered
324 }
325 }
326
327
329 int *levlCoeffs)
330 {
331 int i;
336
337 s = stream_format_code >> 1;
338 hufftab[0] = &huffman_vlc[
s][0];
339 hufftab[1] = &huffman_vlc[
s][1];
340 hufftab[2] = &huffman_vlc[
s][2];
341 hufftab[3] = &huffman_vlc[
s][3];
343
344 if (stream_format_code & 4)
345 start = 1;
346 if (start)
348 for (i = start; i <
BANDS; i++) {
350 hufftab[cb_sel[i]]->
bits, 2);
351 if (levlCoeffs[i] == 17)
353 }
354 }
355
357 int *levlCoeffs)
358 {
359 int i;
360
363 for (i = 1; i <
BANDS; i++)
365 }
366
368 float *flcoeffs1, float *flcoeffs2)
369 {
371 float tmp, tmp2;
372 // maybe some frequency division thingy
373
374 flcoeffs1[0] = 20000.0 /
exp2 (levlCoeffBuf[0] * 0.18945);
// 0.18945 = log2(10) * 0.05703125
375 flcoeffs2[0] =
log2f(flcoeffs1[0]);
376 tmp = flcoeffs1[0];
377 tmp2 = flcoeffs2[0];
378
379 for (i = 1; i <
BANDS; i++) {
380 level = levlCoeffBuf[i];
381 if (level == 16) {
382 flcoeffs1[i] = 1.0;
383 flcoeffs2[i] = 0.0;
384 } else {
385 if (level < 17)
386 level -= 7;
387 else if (level <= 24)
388 level -= 32;
389 else
390 level -= 16;
391
393 tmp2 += 0.83048 *
level;
// 0.83048 = log2(10) * 0.25
394 flcoeffs1[i] = tmp;
395 flcoeffs2[i] = tmp2;
396 }
397 }
398 }
399
400
402 float *old_floor, float *flcoeffs1,
403 float *flcoeffs2)
404 {
405 int i;
406 /* FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
407 * and flcoeffs2 old scale factors
408 * might be incomplete due to a missing table that is in the binary code
409 */
410 for (i = 0; i <
BANDS; i++) {
411 flcoeffs1[i] = 0;
412 if (levlCoeffBuf[i] < 16) {
413 flcoeffs1[i] =
imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
414 flcoeffs2[i] = (levlCoeffBuf[i] - 7) * 0.83048 + flcoeffs2[i]; // 0.83048 = log2(10) * 0.25
415 } else {
416 flcoeffs1[i] = old_floor[i];
417 }
418 }
419 }
420
422 float *flcoeffs1, float *flcoeffs2)
423 {
425 float tmp, tmp2;
426
428 flcoeffs1[pos] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
429 flcoeffs2[pos] =
log2f(flcoeffs1[0]);
430 tmp = flcoeffs1[pos];
431 tmp2 = flcoeffs2[pos];
432
433 levlCoeffBuf++;
434 for (i = 0; i <
BANDS; i++) {
435 if (i == pos)
436 continue;
437 level = *levlCoeffBuf++;
438 flcoeffs1[i] = tmp *
powf(10.0, -level * 0.4375);
//todo tab
439 flcoeffs2[i] = tmp2 - 1.4533435415 *
level;
// 1.4533435415 = log2(10) * 0.4375
440 }
441 }
442
443 /**
444 * Perform bit allocation depending on bits available
445 */
447 int stream_format_code, int freebits, int flag)
448 {
449 int i, j;
450 const float limit = -1.e20;
451 float highest = 0.0;
452 int indx;
455 float summa = 0.0;
456 int iacc = 0;
457 int summer = 0;
458 int rres, cwlen;
459 float lowest = 1.e10;
460 int low_indx = 0;
461 float workT[32];
462 int flg;
463 int found_indx = 0;
464
465 for (i = 0; i <
BANDS; i++)
467
468 for (i = 0; i < BANDS - 1; i++) {
472 }
474 }
476
477 highest = highest * 0.25;
478
479 for (i = 0; i <
BANDS; i++) {
480 indx = -1;
482 indx = 0;
483
485 indx = 1;
486
488 indx = 2;
489
490 if (indx == -1)
492
494 }
495
496 if (stream_format_code & 0x2) {
501 }
502
503 for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS - 1; i++) {
506 }
507
508 if (!iacc)
510
512 summa = (summa * 0.5 - freebits) / iacc;
513
514
515 for (i = 0; i < BANDS / 2; i++) {
516 rres = summer - freebits;
517 if ((rres >= -8) && (rres <= 8))
518 break;
519
520 summer = 0;
521 iacc = 0;
522
523 for (j = (stream_format_code & 0x2) ? 4 : 0; j <
BANDS; j++) {
524 cwlen = av_clipf(((chctx->
flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
525
528
529 if (cwlen > 0)
531 }
532
534 t2 = 1;
535 if (freebits < summer)
536 t2 = -1;
537 if (i == 0)
539 if (flg != t2)
540 t1++;
541
542 summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
543 }
544
545 for (i = (stream_format_code & 0x2) ? 4 : 0; i <
BANDS; i++) {
548 }
549
550 if (freebits > summer) {
551 for (i = 0; i <
BANDS; i++) {
552 workT[i] = (chctx->
bitsBandT[i] == 6) ? -1.e20
554 }
555
556 highest = 0.0;
557
558 do {
559 if (highest <= -1.e20)
560 break;
561
562 found_indx = 0;
563 highest = -1.e20;
564
565 for (i = 0; i <
BANDS; i++) {
566 if (workT[i] > highest) {
567 highest = workT[i];
568 found_indx = i;
569 }
570 }
571
572 if (highest > -1.e20) {
573 workT[found_indx] -= 2.0;
575 workT[found_indx] = -1.e20;
576
577 for (j =
band_tab[found_indx]; j < band_tab[found_indx + 1] && (freebits > summer); j++) {
579 summer++;
580 }
581 }
582 } while (freebits > summer);
583 }
584 if (freebits < summer) {
585 for (i = 0; i <
BANDS; i++) {
587 : 1.e20;
588 }
589 if (stream_format_code & 0x2) {
590 workT[0] = 1.e20;
591 workT[1] = 1.e20;
592 workT[2] = 1.e20;
593 workT[3] = 1.e20;
594 }
595 while (freebits < summer) {
596 lowest = 1.e10;
597 low_indx = 0;
598 for (i = 0; i <
BANDS; i++) {
599 if (workT[i] < lowest) {
600 lowest = workT[i];
601 low_indx = i;
602 }
603 }
604 // if (lowest >= 1.e10)
605 // break;
606 workT[low_indx] = lowest + 2.0;
607
609 workT[low_indx] = 1.e20;
610
611 for (j =
band_tab[low_indx]; j <
band_tab[low_indx+1] && (freebits < summer); j++) {
614 summer--;
615 }
616 }
617 }
618 }
619 return 0;
620 }
621
623 {
624 int i, j;
625
628 for (i = 0; i <
BANDS; i++) {
630 continue;
631
634
635 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
639 }
640 } else {
647 } else {
653 } else {
659 } else { // 101
661 }
662 }
663 }
664 }
665
666 if (j < band_tab[i + 1]) {
670 }
671 }
672 }
673 }
674
675 /**
676 * Increase highest' band coefficient sizes as some bits won't be used
677 */
679 int summer)
680 {
681 float workT[32];
682 int corrected = 0;
683 int i, j;
684 float highest = 0;
685 int found_indx = 0;
686
687 for (i = 0; i <
BANDS; i++) {
688 workT[i] = (chctx->
bitsBandT[i] == 6) ? -1.e20
690 }
691
692 while (corrected < summer) {
693 if (highest <= -1.e20)
694 break;
695
696 highest = -1.e20;
697
698 for (i = 0; i <
BANDS; i++) {
699 if (workT[i] > highest) {
700 highest = workT[i];
701 found_indx = i;
702 }
703 }
704
705 if (highest > -1.e20) {
706 workT[found_indx] -= 2.0;
707 if (++(chctx->
bitsBandT[found_indx]) == 6)
708 workT[found_indx] = -1.e20;
709
710 for (j =
band_tab[found_indx]; j <
band_tab[found_indx+1] && (corrected < summer); j++) {
713 corrected++;
714 }
715 }
716 }
717 }
718 }
719
721 {
722 int i;
726
727 /* prerotation */
728 for (i = 0; i <
COEFFS / 2; i++) {
733 }
734
735 /* FFT */
738
739 /* postrotation, window and reorder */
740 for (i = 0; i <
COEFFS / 2; i++) {
747 dst1 += 2;
748 dst2 -= 2;
750 }
751 }
752
754 int stream_format_code)
755 {
756 int i, j;
757 int middle_value, cw_len, max_size;
758 const float *quantizer;
759
760 for (i = 0; i <
BANDS; i++) {
764
765 if (cw_len <= 0 || chctx->skipFlags[j])
766 continue;
767
768 max_size = 1 << cw_len;
769 middle_value = max_size >> 1;
770
773
774 if (cw_len >= 4) {
778 else
780 }else{
784 else
786 }
787 }
788 }
789 return 0;
790 }
791
792
795 {
796 int i, j, cw_len, cw;
797
798 for (i = 0; i <
BANDS; i++) {
800 continue;
804 cw = 0;
805
809 "Potential problem on band %i, coefficient %i"
810 ": cw_len=%i\n", i, j, cw_len);
811 } else
813 }
814
816 }
817 }
818 }
819 }
820
822 {
823 int i, j;
825
826 for (i = 0; i <
BANDS; i++) {
832 if ((((band_tab[i + 1] - band_tab[i]) * 1.5) > chctx->
sumLenArr[i]) && (chctx->
sumLenArr[i] > 0))
834 }
835
837
838 for (i = 0; i <
BANDS; i++) {
840 /* band has flag set and at least one coded coefficient */
844 }
845 }
846
847 /* calculate bits left, bits needed and adjust bit allocation */
848 bits = summer = 0;
849
850 for (i = 0; i <
BANDS; i++) {
856 }
857 }
860 }
861 }
863 }
864
866 {
867 int stream_format_code;
868 int imc_hdr, i, j,
ret;
869 int flag;
871 int counter, bitscount;
873
874
875 /* Check the frame header */
877 if (imc_hdr & 0x18) {
881 }
883
884 if (stream_format_code & 0x04)
886
888 for (i = 0; i <
BANDS; i++)
893 }
894
896 if (stream_format_code & 0x1)
898 else
900
901 if (stream_format_code & 0x1)
904 else if (stream_format_code & 0x4)
907 else
910
911 for(i=0; i<
BANDS; i++) {
915 }
916 }
917
919
920 counter = 0;
921 if (stream_format_code & 0x1) {
922 for (i = 0; i <
BANDS; i++) {
927 }
928 } else {
929 for (i = 0; i <
BANDS; i++) {
932 counter++;
933 } else
935 }
936
938 for (i = 0; i < BANDS - 1; i++)
941
945 }
946
947 bitscount = 0;
948 /* first 4 bands will be assigned 5 bits per coefficient */
949 if (stream_format_code & 0x2) {
950 bitscount += 15;
951
956 for (i = 1; i < 4; i++) {
957 if (stream_format_code & 0x1)
958 bits = 5;
959 else
965 }
966 }
967 }
970 if (!(stream_format_code & 0x2))
971 bitscount += 16;
972 }
973
976 flag)) < 0) {
980 }
981
982 if (stream_format_code & 0x1) {
983 for (i = 0; i <
BANDS; i++)
985 } else {
987 }
988
989 for (i = 0; i <
BANDS; i++) {
991
995 }
996
998
1000
1005 }
1006
1008
1010
1011 return 0;
1012 }
1013
1015 int *got_frame_ptr,
AVPacket *avpkt)
1016 {
1019 int buf_size = avpkt->
size;
1021
1023
1025
1026 if (buf_size < IMC_BLOCK_SIZE * avctx->channels) {
1029 }
1030
1031 /* get output buffer */
1035
1036 for (i = 0; i < avctx->
channels; i++) {
1038
1040
1042
1044
1047 }
1048
1052 }
1053
1054 *got_frame_ptr = 1;
1055
1057 }
1058
1060 {
1062
1065
1066 return 0;
1067 }
1068
1070 {
1072
1075 }
1076
1077 #if CONFIG_IMC_DECODER
1091 };
1092 #endif
1093 #if CONFIG_IAC_DECODER
1107 };
1108 #endif