imc.c
Go to the documentation of this file.
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 Libav.
8  *
9  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
34 #include <math.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 
38 #include "avcodec.h"
39 #include "get_bits.h"
40 #include "dsputil.h"
41 #include "fft.h"
42 #include "libavutil/audioconvert.h"
43 #include "sinewin.h"
44 
45 #include "imcdata.h"
46 
47 #define IMC_BLOCK_SIZE 64
48 #define IMC_FRAME_ID 0x21
49 #define BANDS 32
50 #define COEFFS 256
51 
52 typedef struct {
54 
55  float old_floor[BANDS];
56  float flcoeffs1[BANDS];
57  float flcoeffs2[BANDS];
58  float flcoeffs3[BANDS];
59  float flcoeffs4[BANDS];
60  float flcoeffs5[BANDS];
61  float flcoeffs6[BANDS];
62  float CWdecoded[COEFFS];
63 
66  float mdct_sine_window[COEFFS];
67  float post_cos[COEFFS];
68  float post_sin[COEFFS];
69  float pre_coef1[COEFFS];
70  float pre_coef2[COEFFS];
71  float last_fft_im[COEFFS];
73 
74  int bandWidthT[BANDS];
75  int bitsBandT[BANDS];
76  int CWlengthT[COEFFS];
77  int levlCoeffBuf[BANDS];
78  int bandFlagsBuf[BANDS];
79  int sumLenArr[BANDS];
80  int skipFlagRaw[BANDS];
81  int skipFlagBits[BANDS];
82  int skipFlagCount[BANDS];
83  int skipFlags[COEFFS];
84  int codewords[COEFFS];
85  float sqrt_tab[30];
88  float one_div_log2;
89 
93  float *out_samples;
94 } IMCContext;
95 
96 static VLC huffman_vlc[4][4];
97 
98 #define VLC_TABLES_SIZE 9512
99 
100 static const int vlc_offsets[17] = {
101  0, 640, 1156, 1732, 2308, 2852, 3396, 3924,
102  4452, 5220, 5860, 6628, 7268, 7908, 8424, 8936, VLC_TABLES_SIZE};
103 
105 
107 {
108  int i, j, ret;
109  IMCContext *q = avctx->priv_data;
110  double r1, r2;
111 
112  if (avctx->channels != 1) {
113  av_log_ask_for_sample(avctx, "Number of channels is not supported\n");
114  return AVERROR_PATCHWELCOME;
115  }
116 
117  q->decoder_reset = 1;
118 
119  for(i = 0; i < BANDS; i++)
120  q->old_floor[i] = 1.0;
121 
122  /* Build mdct window, a simple sine window normalized with sqrt(2) */
124  for(i = 0; i < COEFFS; i++)
125  q->mdct_sine_window[i] *= sqrt(2.0);
126  for(i = 0; i < COEFFS/2; i++){
127  q->post_cos[i] = (1.0f / 32768) * cos(i / 256.0 * M_PI);
128  q->post_sin[i] = (1.0f / 32768) * sin(i / 256.0 * M_PI);
129 
130  r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI);
131  r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI);
132 
133  if (i & 0x1)
134  {
135  q->pre_coef1[i] = (r1 + r2) * sqrt(2.0);
136  q->pre_coef2[i] = -(r1 - r2) * sqrt(2.0);
137  }
138  else
139  {
140  q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0);
141  q->pre_coef2[i] = (r1 - r2) * sqrt(2.0);
142  }
143 
144  q->last_fft_im[i] = 0;
145  }
146 
147  /* Generate a square root table */
148 
149  for(i = 0; i < 30; i++) {
150  q->sqrt_tab[i] = sqrt(i);
151  }
152 
153  /* initialize the VLC tables */
154  for(i = 0; i < 4 ; i++) {
155  for(j = 0; j < 4; j++) {
156  huffman_vlc[i][j].table = &vlc_tables[vlc_offsets[i * 4 + j]];
157  huffman_vlc[i][j].table_allocated = vlc_offsets[i * 4 + j + 1] - vlc_offsets[i * 4 + j];
158  init_vlc(&huffman_vlc[i][j], 9, imc_huffman_sizes[i],
159  imc_huffman_lens[i][j], 1, 1,
161  }
162  }
163  q->one_div_log2 = 1/log(2);
164 
165  if ((ret = ff_fft_init(&q->fft, 7, 1))) {
166  av_log(avctx, AV_LOG_INFO, "FFT init failed\n");
167  return ret;
168  }
169  dsputil_init(&q->dsp, avctx);
170  avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
172 
174  avctx->coded_frame = &q->frame;
175 
176  return 0;
177 }
178 
179 static void imc_calculate_coeffs(IMCContext* q, float* flcoeffs1, float* flcoeffs2, int* bandWidthT,
180  float* flcoeffs3, float* flcoeffs5)
181 {
182  float workT1[BANDS];
183  float workT2[BANDS];
184  float workT3[BANDS];
185  float snr_limit = 1.e-30;
186  float accum = 0.0;
187  int i, cnt2;
188 
189  for(i = 0; i < BANDS; i++) {
190  flcoeffs5[i] = workT2[i] = 0.0;
191  if (bandWidthT[i]){
192  workT1[i] = flcoeffs1[i] * flcoeffs1[i];
193  flcoeffs3[i] = 2.0 * flcoeffs2[i];
194  } else {
195  workT1[i] = 0.0;
196  flcoeffs3[i] = -30000.0;
197  }
198  workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
199  if (workT3[i] <= snr_limit)
200  workT3[i] = 0.0;
201  }
202 
203  for(i = 0; i < BANDS; i++) {
204  for(cnt2 = i; cnt2 < cyclTab[i]; cnt2++)
205  flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
206  workT2[cnt2-1] = workT2[cnt2-1] + workT3[i];
207  }
208 
209  for(i = 1; i < BANDS; i++) {
210  accum = (workT2[i-1] + accum) * imc_weights1[i-1];
211  flcoeffs5[i] += accum;
212  }
213 
214  for(i = 0; i < BANDS; i++)
215  workT2[i] = 0.0;
216 
217  for(i = 0; i < BANDS; i++) {
218  for(cnt2 = i-1; cnt2 > cyclTab2[i]; cnt2--)
219  flcoeffs5[cnt2] += workT3[i];
220  workT2[cnt2+1] += workT3[i];
221  }
222 
223  accum = 0.0;
224 
225  for(i = BANDS-2; i >= 0; i--) {
226  accum = (workT2[i+1] + accum) * imc_weights2[i];
227  flcoeffs5[i] += accum;
228  //there is missing code here, but it seems to never be triggered
229  }
230 }
231 
232 
233 static void imc_read_level_coeffs(IMCContext* q, int stream_format_code, int* levlCoeffs)
234 {
235  int i;
236  VLC *hufftab[4];
237  int start = 0;
238  const uint8_t *cb_sel;
239  int s;
240 
241  s = stream_format_code >> 1;
242  hufftab[0] = &huffman_vlc[s][0];
243  hufftab[1] = &huffman_vlc[s][1];
244  hufftab[2] = &huffman_vlc[s][2];
245  hufftab[3] = &huffman_vlc[s][3];
246  cb_sel = imc_cb_select[s];
247 
248  if(stream_format_code & 4)
249  start = 1;
250  if(start)
251  levlCoeffs[0] = get_bits(&q->gb, 7);
252  for(i = start; i < BANDS; i++){
253  levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table, hufftab[cb_sel[i]]->bits, 2);
254  if(levlCoeffs[i] == 17)
255  levlCoeffs[i] += get_bits(&q->gb, 4);
256  }
257 }
258 
259 static void imc_decode_level_coefficients(IMCContext* q, int* levlCoeffBuf, float* flcoeffs1,
260  float* flcoeffs2)
261 {
262  int i, level;
263  float tmp, tmp2;
264  //maybe some frequency division thingy
265 
266  flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
267  flcoeffs2[0] = log(flcoeffs1[0])/log(2);
268  tmp = flcoeffs1[0];
269  tmp2 = flcoeffs2[0];
270 
271  for(i = 1; i < BANDS; i++) {
272  level = levlCoeffBuf[i];
273  if (level == 16) {
274  flcoeffs1[i] = 1.0;
275  flcoeffs2[i] = 0.0;
276  } else {
277  if (level < 17)
278  level -=7;
279  else if (level <= 24)
280  level -=32;
281  else
282  level -=16;
283 
284  tmp *= imc_exp_tab[15 + level];
285  tmp2 += 0.83048 * level; // 0.83048 = log2(10) * 0.25
286  flcoeffs1[i] = tmp;
287  flcoeffs2[i] = tmp2;
288  }
289  }
290 }
291 
292 
293 static void imc_decode_level_coefficients2(IMCContext* q, int* levlCoeffBuf, float* old_floor, float* flcoeffs1,
294  float* flcoeffs2) {
295  int i;
296  //FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
297  // and flcoeffs2 old scale factors
298  // might be incomplete due to a missing table that is in the binary code
299  for(i = 0; i < BANDS; i++) {
300  flcoeffs1[i] = 0;
301  if(levlCoeffBuf[i] < 16) {
302  flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
303  flcoeffs2[i] = (levlCoeffBuf[i]-7) * 0.83048 + flcoeffs2[i]; // 0.83048 = log2(10) * 0.25
304  } else {
305  flcoeffs1[i] = old_floor[i];
306  }
307  }
308 }
309 
313 static int bit_allocation (IMCContext* q, int stream_format_code, int freebits, int flag) {
314  int i, j;
315  const float limit = -1.e20;
316  float highest = 0.0;
317  int indx;
318  int t1 = 0;
319  int t2 = 1;
320  float summa = 0.0;
321  int iacc = 0;
322  int summer = 0;
323  int rres, cwlen;
324  float lowest = 1.e10;
325  int low_indx = 0;
326  float workT[32];
327  int flg;
328  int found_indx = 0;
329 
330  for(i = 0; i < BANDS; i++)
331  highest = FFMAX(highest, q->flcoeffs1[i]);
332 
333  for(i = 0; i < BANDS-1; i++) {
334  q->flcoeffs4[i] = q->flcoeffs3[i] - log(q->flcoeffs5[i])/log(2);
335  }
336  q->flcoeffs4[BANDS - 1] = limit;
337 
338  highest = highest * 0.25;
339 
340  for(i = 0; i < BANDS; i++) {
341  indx = -1;
342  if ((band_tab[i+1] - band_tab[i]) == q->bandWidthT[i])
343  indx = 0;
344 
345  if ((band_tab[i+1] - band_tab[i]) > q->bandWidthT[i])
346  indx = 1;
347 
348  if (((band_tab[i+1] - band_tab[i])/2) >= q->bandWidthT[i])
349  indx = 2;
350 
351  if (indx == -1)
352  return AVERROR_INVALIDDATA;
353 
354  q->flcoeffs4[i] = q->flcoeffs4[i] + xTab[(indx*2 + (q->flcoeffs1[i] < highest)) * 2 + flag];
355  }
356 
357  if (stream_format_code & 0x2) {
358  q->flcoeffs4[0] = limit;
359  q->flcoeffs4[1] = limit;
360  q->flcoeffs4[2] = limit;
361  q->flcoeffs4[3] = limit;
362  }
363 
364  for(i = (stream_format_code & 0x2)?4:0; i < BANDS-1; i++) {
365  iacc += q->bandWidthT[i];
366  summa += q->bandWidthT[i] * q->flcoeffs4[i];
367  }
368 
369  if (!iacc)
370  return AVERROR_INVALIDDATA;
371 
372  q->bandWidthT[BANDS-1] = 0;
373  summa = (summa * 0.5 - freebits) / iacc;
374 
375 
376  for(i = 0; i < BANDS/2; i++) {
377  rres = summer - freebits;
378  if((rres >= -8) && (rres <= 8)) break;
379 
380  summer = 0;
381  iacc = 0;
382 
383  for(j = (stream_format_code & 0x2)?4:0; j < BANDS; j++) {
384  cwlen = av_clipf(((q->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
385 
386  q->bitsBandT[j] = cwlen;
387  summer += q->bandWidthT[j] * cwlen;
388 
389  if (cwlen > 0)
390  iacc += q->bandWidthT[j];
391  }
392 
393  flg = t2;
394  t2 = 1;
395  if (freebits < summer)
396  t2 = -1;
397  if (i == 0)
398  flg = t2;
399  if(flg != t2)
400  t1++;
401 
402  summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
403  }
404 
405  for(i = (stream_format_code & 0x2)?4:0; i < BANDS; i++) {
406  for(j = band_tab[i]; j < band_tab[i+1]; j++)
407  q->CWlengthT[j] = q->bitsBandT[i];
408  }
409 
410  if (freebits > summer) {
411  for(i = 0; i < BANDS; i++) {
412  workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415);
413  }
414 
415  highest = 0.0;
416 
417  do{
418  if (highest <= -1.e20)
419  break;
420 
421  found_indx = 0;
422  highest = -1.e20;
423 
424  for(i = 0; i < BANDS; i++) {
425  if (workT[i] > highest) {
426  highest = workT[i];
427  found_indx = i;
428  }
429  }
430 
431  if (highest > -1.e20) {
432  workT[found_indx] -= 2.0;
433  if (++(q->bitsBandT[found_indx]) == 6)
434  workT[found_indx] = -1.e20;
435 
436  for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (freebits > summer); j++){
437  q->CWlengthT[j]++;
438  summer++;
439  }
440  }
441  }while (freebits > summer);
442  }
443  if (freebits < summer) {
444  for(i = 0; i < BANDS; i++) {
445  workT[i] = q->bitsBandT[i] ? (q->bitsBandT[i] * -2 + q->flcoeffs4[i] + 1.585) : 1.e20;
446  }
447  if (stream_format_code & 0x2) {
448  workT[0] = 1.e20;
449  workT[1] = 1.e20;
450  workT[2] = 1.e20;
451  workT[3] = 1.e20;
452  }
453  while (freebits < summer){
454  lowest = 1.e10;
455  low_indx = 0;
456  for(i = 0; i < BANDS; i++) {
457  if (workT[i] < lowest) {
458  lowest = workT[i];
459  low_indx = i;
460  }
461  }
462  //if(lowest >= 1.e10) break;
463  workT[low_indx] = lowest + 2.0;
464 
465  if (!(--q->bitsBandT[low_indx]))
466  workT[low_indx] = 1.e20;
467 
468  for(j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++){
469  if(q->CWlengthT[j] > 0){
470  q->CWlengthT[j]--;
471  summer--;
472  }
473  }
474  }
475  }
476  return 0;
477 }
478 
480  int i, j;
481 
482  memset(q->skipFlagBits, 0, sizeof(q->skipFlagBits));
483  memset(q->skipFlagCount, 0, sizeof(q->skipFlagCount));
484  for(i = 0; i < BANDS; i++) {
485  if (!q->bandFlagsBuf[i] || !q->bandWidthT[i])
486  continue;
487 
488  if (!q->skipFlagRaw[i]) {
489  q->skipFlagBits[i] = band_tab[i+1] - band_tab[i];
490 
491  for(j = band_tab[i]; j < band_tab[i+1]; j++) {
492  if ((q->skipFlags[j] = get_bits1(&q->gb)))
493  q->skipFlagCount[i]++;
494  }
495  } else {
496  for(j = band_tab[i]; j < (band_tab[i+1]-1); j += 2) {
497  if(!get_bits1(&q->gb)){//0
498  q->skipFlagBits[i]++;
499  q->skipFlags[j]=1;
500  q->skipFlags[j+1]=1;
501  q->skipFlagCount[i] += 2;
502  }else{
503  if(get_bits1(&q->gb)){//11
504  q->skipFlagBits[i] +=2;
505  q->skipFlags[j]=0;
506  q->skipFlags[j+1]=1;
507  q->skipFlagCount[i]++;
508  }else{
509  q->skipFlagBits[i] +=3;
510  q->skipFlags[j+1]=0;
511  if(!get_bits1(&q->gb)){//100
512  q->skipFlags[j]=1;
513  q->skipFlagCount[i]++;
514  }else{//101
515  q->skipFlags[j]=0;
516  }
517  }
518  }
519  }
520 
521  if (j < band_tab[i+1]) {
522  q->skipFlagBits[i]++;
523  if ((q->skipFlags[j] = get_bits1(&q->gb)))
524  q->skipFlagCount[i]++;
525  }
526  }
527  }
528 }
529 
533 static void imc_adjust_bit_allocation (IMCContext* q, int summer) {
534  float workT[32];
535  int corrected = 0;
536  int i, j;
537  float highest = 0;
538  int found_indx=0;
539 
540  for(i = 0; i < BANDS; i++) {
541  workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415);
542  }
543 
544  while (corrected < summer) {
545  if(highest <= -1.e20)
546  break;
547 
548  highest = -1.e20;
549 
550  for(i = 0; i < BANDS; i++) {
551  if (workT[i] > highest) {
552  highest = workT[i];
553  found_indx = i;
554  }
555  }
556 
557  if (highest > -1.e20) {
558  workT[found_indx] -= 2.0;
559  if (++(q->bitsBandT[found_indx]) == 6)
560  workT[found_indx] = -1.e20;
561 
562  for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
563  if (!q->skipFlags[j] && (q->CWlengthT[j] < 6)) {
564  q->CWlengthT[j]++;
565  corrected++;
566  }
567  }
568  }
569  }
570 }
571 
572 static void imc_imdct256(IMCContext *q) {
573  int i;
574  float re, im;
575 
576  /* prerotation */
577  for(i=0; i < COEFFS/2; i++){
578  q->samples[i].re = -(q->pre_coef1[i] * q->CWdecoded[COEFFS-1-i*2]) -
579  (q->pre_coef2[i] * q->CWdecoded[i*2]);
580  q->samples[i].im = (q->pre_coef2[i] * q->CWdecoded[COEFFS-1-i*2]) -
581  (q->pre_coef1[i] * q->CWdecoded[i*2]);
582  }
583 
584  /* FFT */
585  q->fft.fft_permute(&q->fft, q->samples);
586  q->fft.fft_calc (&q->fft, q->samples);
587 
588  /* postrotation, window and reorder */
589  for(i = 0; i < COEFFS/2; i++){
590  re = (q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
591  im = (-q->samples[i].im * q->post_cos[i]) - (q->samples[i].re * q->post_sin[i]);
592  q->out_samples[i*2] = (q->mdct_sine_window[COEFFS-1-i*2] * q->last_fft_im[i]) + (q->mdct_sine_window[i*2] * re);
593  q->out_samples[COEFFS-1-i*2] = (q->mdct_sine_window[i*2] * q->last_fft_im[i]) - (q->mdct_sine_window[COEFFS-1-i*2] * re);
594  q->last_fft_im[i] = im;
595  }
596 }
597 
598 static int inverse_quant_coeff (IMCContext* q, int stream_format_code) {
599  int i, j;
600  int middle_value, cw_len, max_size;
601  const float* quantizer;
602 
603  for(i = 0; i < BANDS; i++) {
604  for(j = band_tab[i]; j < band_tab[i+1]; j++) {
605  q->CWdecoded[j] = 0;
606  cw_len = q->CWlengthT[j];
607 
608  if (cw_len <= 0 || q->skipFlags[j])
609  continue;
610 
611  max_size = 1 << cw_len;
612  middle_value = max_size >> 1;
613 
614  if (q->codewords[j] >= max_size || q->codewords[j] < 0)
615  return AVERROR_INVALIDDATA;
616 
617  if (cw_len >= 4){
618  quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
619  if (q->codewords[j] >= middle_value)
620  q->CWdecoded[j] = quantizer[q->codewords[j] - 8] * q->flcoeffs6[i];
621  else
622  q->CWdecoded[j] = -quantizer[max_size - q->codewords[j] - 8 - 1] * q->flcoeffs6[i];
623  }else{
624  quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (q->bandFlagsBuf[i] << 1)];
625  if (q->codewords[j] >= middle_value)
626  q->CWdecoded[j] = quantizer[q->codewords[j] - 1] * q->flcoeffs6[i];
627  else
628  q->CWdecoded[j] = -quantizer[max_size - 2 - q->codewords[j]] * q->flcoeffs6[i];
629  }
630  }
631  }
632  return 0;
633 }
634 
635 
636 static int imc_get_coeffs (IMCContext* q) {
637  int i, j, cw_len, cw;
638 
639  for(i = 0; i < BANDS; i++) {
640  if(!q->sumLenArr[i]) continue;
641  if (q->bandFlagsBuf[i] || q->bandWidthT[i]) {
642  for(j = band_tab[i]; j < band_tab[i+1]; j++) {
643  cw_len = q->CWlengthT[j];
644  cw = 0;
645 
646  if (get_bits_count(&q->gb) + cw_len > 512){
647 //av_log(NULL,0,"Band %i coeff %i cw_len %i\n",i,j,cw_len);
648  return AVERROR_INVALIDDATA;
649  }
650 
651  if(cw_len && (!q->bandFlagsBuf[i] || !q->skipFlags[j]))
652  cw = get_bits(&q->gb, cw_len);
653 
654  q->codewords[j] = cw;
655  }
656  }
657  }
658  return 0;
659 }
660 
661 static int imc_decode_frame(AVCodecContext * avctx, void *data,
662  int *got_frame_ptr, AVPacket *avpkt)
663 {
664  const uint8_t *buf = avpkt->data;
665  int buf_size = avpkt->size;
666 
667  IMCContext *q = avctx->priv_data;
668 
669  int stream_format_code;
670  int imc_hdr, i, j, ret;
671  int flag;
672  int bits, summer;
673  int counter, bitscount;
674  LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2]);
675 
676  if (buf_size < IMC_BLOCK_SIZE) {
677  av_log(avctx, AV_LOG_ERROR, "imc frame too small!\n");
678  return AVERROR_INVALIDDATA;
679  }
680 
681  /* get output buffer */
682  q->frame.nb_samples = COEFFS;
683  if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {
684  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
685  return ret;
686  }
687  q->out_samples = (float *)q->frame.data[0];
688 
689  q->dsp.bswap16_buf(buf16, (const uint16_t*)buf, IMC_BLOCK_SIZE / 2);
690 
691  init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
692 
693  /* Check the frame header */
694  imc_hdr = get_bits(&q->gb, 9);
695  if (imc_hdr != IMC_FRAME_ID) {
696  av_log(avctx, AV_LOG_ERROR, "imc frame header check failed!\n");
697  av_log(avctx, AV_LOG_ERROR, "got %x instead of 0x21.\n", imc_hdr);
698  return AVERROR_INVALIDDATA;
699  }
700  stream_format_code = get_bits(&q->gb, 3);
701 
702  if(stream_format_code & 1){
703  av_log(avctx, AV_LOG_ERROR, "Stream code format %X is not supported\n", stream_format_code);
704  return AVERROR_INVALIDDATA;
705  }
706 
707 // av_log(avctx, AV_LOG_DEBUG, "stream_format_code = %d\n", stream_format_code);
708 
709  if (stream_format_code & 0x04)
710  q->decoder_reset = 1;
711 
712  if(q->decoder_reset) {
713  memset(q->out_samples, 0, sizeof(q->out_samples));
714  for(i = 0; i < BANDS; i++)q->old_floor[i] = 1.0;
715  for(i = 0; i < COEFFS; i++)q->CWdecoded[i] = 0;
716  q->decoder_reset = 0;
717  }
718 
719  flag = get_bits1(&q->gb);
720  imc_read_level_coeffs(q, stream_format_code, q->levlCoeffBuf);
721 
722  if (stream_format_code & 0x4)
724  else
726 
727  memcpy(q->old_floor, q->flcoeffs1, 32 * sizeof(float));
728 
729  counter = 0;
730  for (i=0 ; i<BANDS ; i++) {
731  if (q->levlCoeffBuf[i] == 16) {
732  q->bandWidthT[i] = 0;
733  counter++;
734  } else
735  q->bandWidthT[i] = band_tab[i+1] - band_tab[i];
736  }
737  memset(q->bandFlagsBuf, 0, BANDS * sizeof(int));
738  for(i = 0; i < BANDS-1; i++) {
739  if (q->bandWidthT[i])
740  q->bandFlagsBuf[i] = get_bits1(&q->gb);
741  }
742 
744 
745  bitscount = 0;
746  /* first 4 bands will be assigned 5 bits per coefficient */
747  if (stream_format_code & 0x2) {
748  bitscount += 15;
749 
750  q->bitsBandT[0] = 5;
751  q->CWlengthT[0] = 5;
752  q->CWlengthT[1] = 5;
753  q->CWlengthT[2] = 5;
754  for(i = 1; i < 4; i++){
755  bits = (q->levlCoeffBuf[i] == 16) ? 0 : 5;
756  q->bitsBandT[i] = bits;
757  for(j = band_tab[i]; j < band_tab[i+1]; j++) {
758  q->CWlengthT[j] = bits;
759  bitscount += bits;
760  }
761  }
762  }
763 
764  if((ret = bit_allocation (q, stream_format_code,
765  512 - bitscount - get_bits_count(&q->gb), flag)) < 0) {
766  av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
767  q->decoder_reset = 1;
768  return ret;
769  }
770 
771  for(i = 0; i < BANDS; i++) {
772  q->sumLenArr[i] = 0;
773  q->skipFlagRaw[i] = 0;
774  for(j = band_tab[i]; j < band_tab[i+1]; j++)
775  q->sumLenArr[i] += q->CWlengthT[j];
776  if (q->bandFlagsBuf[i])
777  if( (((band_tab[i+1] - band_tab[i]) * 1.5) > q->sumLenArr[i]) && (q->sumLenArr[i] > 0))
778  q->skipFlagRaw[i] = 1;
779  }
780 
782 
783  for(i = 0; i < BANDS; i++) {
784  q->flcoeffs6[i] = q->flcoeffs1[i];
785  /* band has flag set and at least one coded coefficient */
786  if (q->bandFlagsBuf[i] && (band_tab[i+1] - band_tab[i]) != q->skipFlagCount[i]){
787  q->flcoeffs6[i] *= q->sqrt_tab[band_tab[i+1] - band_tab[i]] /
788  q->sqrt_tab[(band_tab[i+1] - band_tab[i] - q->skipFlagCount[i])];
789  }
790  }
791 
792  /* calculate bits left, bits needed and adjust bit allocation */
793  bits = summer = 0;
794 
795  for(i = 0; i < BANDS; i++) {
796  if (q->bandFlagsBuf[i]) {
797  for(j = band_tab[i]; j < band_tab[i+1]; j++) {
798  if(q->skipFlags[j]) {
799  summer += q->CWlengthT[j];
800  q->CWlengthT[j] = 0;
801  }
802  }
803  bits += q->skipFlagBits[i];
804  summer -= q->skipFlagBits[i];
805  }
806  }
807  imc_adjust_bit_allocation(q, summer);
808 
809  for(i = 0; i < BANDS; i++) {
810  q->sumLenArr[i] = 0;
811 
812  for(j = band_tab[i]; j < band_tab[i+1]; j++)
813  if (!q->skipFlags[j])
814  q->sumLenArr[i] += q->CWlengthT[j];
815  }
816 
817  memset(q->codewords, 0, sizeof(q->codewords));
818 
819  if(imc_get_coeffs(q) < 0) {
820  av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n");
821  q->decoder_reset = 1;
822  return AVERROR_INVALIDDATA;
823  }
824 
825  if(inverse_quant_coeff(q, stream_format_code) < 0) {
826  av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
827  q->decoder_reset = 1;
828  return AVERROR_INVALIDDATA;
829  }
830 
831  memset(q->skipFlags, 0, sizeof(q->skipFlags));
832 
833  imc_imdct256(q);
834 
835  *got_frame_ptr = 1;
836  *(AVFrame *)data = q->frame;
837 
838  return IMC_BLOCK_SIZE;
839 }
840 
841 
843 {
844  IMCContext *q = avctx->priv_data;
845 
846  ff_fft_end(&q->fft);
847 
848  return 0;
849 }
850 
851 
853  .name = "imc",
854  .type = AVMEDIA_TYPE_AUDIO,
855  .id = CODEC_ID_IMC,
856  .priv_data_size = sizeof(IMCContext),
860  .capabilities = CODEC_CAP_DR1,
861  .long_name = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),
862 };