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