annotate imc.c @ 11830:8a545fab1a90 libavcodec

Add WebM muxer Patch by James Zern <jzern at google>
author conrad
date Fri, 04 Jun 2010 22:40:54 +0000
parents 7dd2a45249a9
children 8b28e74de2c0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
1 /*
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
2 * IMC compatible decoder
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
3 * Copyright (c) 2002-2004 Maxim Poliakovski
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
4 * Copyright (c) 2006 Benjamin Larsson
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
5 * Copyright (c) 2006 Konstantin Shishkov
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
6 *
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
7 * This file is part of FFmpeg.
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
8 *
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
9 * FFmpeg is free software; you can redistribute it and/or
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
10 * modify it under the terms of the GNU Lesser General Public
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
11 * License as published by the Free Software Foundation; either
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
12 * version 2.1 of the License, or (at your option) any later version.
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
13 *
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
14 * FFmpeg is distributed in the hope that it will be useful,
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
17 * Lesser General Public License for more details.
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
18 *
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
19 * You should have received a copy of the GNU Lesser General Public
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
20 * License along with FFmpeg; if not, write to the Free Software
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
22 */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
23
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
24 /**
11644
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 11560
diff changeset
25 * @file
7dd2a45249a9 Remove explicit filename from Doxygen @file commands.
diego
parents: 11560
diff changeset
26 * IMC - Intel Music Coder
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
27 * A mdct based codec using a 256 points large transform
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
28 * divied into 32 bands with some mix of scale factors.
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
29 * Only mono is supported.
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
30 *
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
31 */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
32
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
33
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
34 #include <math.h>
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
35 #include <stddef.h>
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
36 #include <stdio.h>
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
37
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
38 #define ALT_BITSTREAM_READER
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
39 #include "avcodec.h"
9428
0dce4fe6e6f3 Rename bitstream.h to get_bits.h.
stefano
parents: 9355
diff changeset
40 #include "get_bits.h"
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
41 #include "dsputil.h"
11370
4b3da727d832 Move FFT parts from dsputil.h to fft.h
mru
parents: 11369
diff changeset
42 #include "fft.h"
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
43
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
44 #include "imcdata.h"
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
45
6307
e0601aa8ebef IMC decoder always operates on 64-byte blocks
kostya
parents: 6218
diff changeset
46 #define IMC_BLOCK_SIZE 64
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
47 #define IMC_FRAME_ID 0x21
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
48 #define BANDS 32
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
49 #define COEFFS 256
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
50
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
51 typedef struct {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
52 float old_floor[BANDS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
53 float flcoeffs1[BANDS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
54 float flcoeffs2[BANDS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
55 float flcoeffs3[BANDS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
56 float flcoeffs4[BANDS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
57 float flcoeffs5[BANDS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
58 float flcoeffs6[BANDS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
59 float CWdecoded[COEFFS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
60
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
61 /** MDCT tables */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
62 //@{
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
63 float mdct_sine_window[COEFFS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
64 float post_cos[COEFFS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
65 float post_sin[COEFFS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
66 float pre_coef1[COEFFS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
67 float pre_coef2[COEFFS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
68 float last_fft_im[COEFFS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
69 //@}
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
70
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
71 int bandWidthT[BANDS]; ///< codewords per band
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
72 int bitsBandT[BANDS]; ///< how many bits per codeword in band
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
73 int CWlengthT[COEFFS]; ///< how many bits in each codeword
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
74 int levlCoeffBuf[BANDS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
75 int bandFlagsBuf[BANDS]; ///< flags for each band
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
76 int sumLenArr[BANDS]; ///< bits for all coeffs in band
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
77 int skipFlagRaw[BANDS]; ///< skip flags are stored in raw form or not
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
78 int skipFlagBits[BANDS]; ///< bits used to code skip flags
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
79 int skipFlagCount[BANDS]; ///< skipped coeffients per band
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
80 int skipFlags[COEFFS]; ///< skip coefficient decoding or not
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
81 int codewords[COEFFS]; ///< raw codewords read from bitstream
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
82 float sqrt_tab[30];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
83 GetBitContext gb;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
84 int decoder_reset;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
85 float one_div_log2;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
86
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
87 DSPContext dsp;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
88 FFTContext fft;
11369
98970e51365a Remove DECLARE_ALIGNED_{8,16} macros
mru
parents: 10961
diff changeset
89 DECLARE_ALIGNED(16, FFTComplex, samples)[COEFFS/2];
98970e51365a Remove DECLARE_ALIGNED_{8,16} macros
mru
parents: 10961
diff changeset
90 DECLARE_ALIGNED(16, float, out_samples)[COEFFS];
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
91 } IMCContext;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
92
7250
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
93 static VLC huffman_vlc[4][4];
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
94
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
95 #define VLC_TABLES_SIZE 9512
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
96
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
97 static const int vlc_offsets[17] = {
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
98 0, 640, 1156, 1732, 2308, 2852, 3396, 3924,
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
99 4452, 5220, 5860, 6628, 7268, 7908, 8424, 8936, VLC_TABLES_SIZE};
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
100
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
101 static VLC_TYPE vlc_tables[VLC_TABLES_SIZE][2];
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
102
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6332
diff changeset
103 static av_cold int imc_decode_init(AVCodecContext * avctx)
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
104 {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
105 int i, j;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
106 IMCContext *q = avctx->priv_data;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
107 double r1, r2;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
108
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
109 q->decoder_reset = 1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
110
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
111 for(i = 0; i < BANDS; i++)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
112 q->old_floor[i] = 1.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
113
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
114 /* Build mdct window, a simple sine window normalized with sqrt(2) */
7094
b0820b8bd4dd Add generic ff_sine_window_init function and implement in codecs appropriately
superdump
parents: 7040
diff changeset
115 ff_sine_window_init(q->mdct_sine_window, COEFFS);
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
116 for(i = 0; i < COEFFS; i++)
7094
b0820b8bd4dd Add generic ff_sine_window_init function and implement in codecs appropriately
superdump
parents: 7040
diff changeset
117 q->mdct_sine_window[i] *= sqrt(2.0);
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
118 for(i = 0; i < COEFFS/2; i++){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
119 q->post_cos[i] = cos(i / 256.0 * M_PI);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
120 q->post_sin[i] = sin(i / 256.0 * M_PI);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
121
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
122 r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
123 r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
124
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
125 if (i & 0x1)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
126 {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
127 q->pre_coef1[i] = (r1 + r2) * sqrt(2.0);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
128 q->pre_coef2[i] = -(r1 - r2) * sqrt(2.0);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
129 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
130 else
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
131 {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
132 q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
133 q->pre_coef2[i] = (r1 - r2) * sqrt(2.0);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
134 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
135
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
136 q->last_fft_im[i] = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
137 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
138
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
139 /* Generate a square root table */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
140
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
141 for(i = 0; i < 30; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
142 q->sqrt_tab[i] = sqrt(i);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
143 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
144
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
145 /* initialize the VLC tables */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
146 for(i = 0; i < 4 ; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
147 for(j = 0; j < 4; j++) {
8154
6d2beae655f0 Silence warning in imc decoder
banan
parents: 8153
diff changeset
148 huffman_vlc[i][j].table = &vlc_tables[vlc_offsets[i * 4 + j]];
7250
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
149 huffman_vlc[i][j].table_allocated = vlc_offsets[i * 4 + j + 1] - vlc_offsets[i * 4 + j];
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
150 init_vlc(&huffman_vlc[i][j], 9, imc_huffman_sizes[i],
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
151 imc_huffman_lens[i][j], 1, 1,
7250
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
152 imc_huffman_bits[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
153 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
154 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
155 q->one_div_log2 = 1/log(2);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
156
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
157 ff_fft_init(&q->fft, 7, 1);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
158 dsputil_init(&q->dsp, avctx);
7451
85ab7655ad4d Modify all codecs to report their supported input and output sample format(s).
pross
parents: 7250
diff changeset
159 avctx->sample_fmt = SAMPLE_FMT_S16;
8153
ef9f95604644 Set channel_layout
banan
parents: 7451
diff changeset
160 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
161 return 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
162 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
163
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
164 static void imc_calculate_coeffs(IMCContext* q, float* flcoeffs1, float* flcoeffs2, int* bandWidthT,
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
165 float* flcoeffs3, float* flcoeffs5)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
166 {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
167 float workT1[BANDS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
168 float workT2[BANDS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
169 float workT3[BANDS];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
170 float snr_limit = 1.e-30;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
171 float accum = 0.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
172 int i, cnt2;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
173
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
174 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
175 flcoeffs5[i] = workT2[i] = 0.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
176 if (bandWidthT[i]){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
177 workT1[i] = flcoeffs1[i] * flcoeffs1[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
178 flcoeffs3[i] = 2.0 * flcoeffs2[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
179 } else {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
180 workT1[i] = 0.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
181 flcoeffs3[i] = -30000.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
182 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
183 workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
184 if (workT3[i] <= snr_limit)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
185 workT3[i] = 0.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
186 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
187
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
188 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
189 for(cnt2 = i; cnt2 < cyclTab[i]; cnt2++)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
190 flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
191 workT2[cnt2-1] = workT2[cnt2-1] + workT3[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
192 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
193
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
194 for(i = 1; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
195 accum = (workT2[i-1] + accum) * imc_weights1[i-1];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
196 flcoeffs5[i] += accum;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
197 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
198
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
199 for(i = 0; i < BANDS; i++)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
200 workT2[i] = 0.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
201
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
202 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
203 for(cnt2 = i-1; cnt2 > cyclTab2[i]; cnt2--)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
204 flcoeffs5[cnt2] += workT3[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
205 workT2[cnt2+1] += workT3[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
206 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
207
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
208 accum = 0.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
209
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
210 for(i = BANDS-2; i >= 0; i--) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
211 accum = (workT2[i+1] + accum) * imc_weights2[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
212 flcoeffs5[i] += accum;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
213 //there is missing code here, but it seems to never be triggered
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
214 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
215 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
216
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
217
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
218 static void imc_read_level_coeffs(IMCContext* q, int stream_format_code, int* levlCoeffs)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
219 {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
220 int i;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
221 VLC *hufftab[4];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
222 int start = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
223 const uint8_t *cb_sel;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
224 int s;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
225
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
226 s = stream_format_code >> 1;
7250
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
227 hufftab[0] = &huffman_vlc[s][0];
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
228 hufftab[1] = &huffman_vlc[s][1];
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
229 hufftab[2] = &huffman_vlc[s][2];
b3c980b12aaa Use new style static VLC tables for IMC decoder.
reimar
parents: 7094
diff changeset
230 hufftab[3] = &huffman_vlc[s][3];
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
231 cb_sel = imc_cb_select[s];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
232
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
233 if(stream_format_code & 4)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
234 start = 1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
235 if(start)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
236 levlCoeffs[0] = get_bits(&q->gb, 7);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
237 for(i = start; i < BANDS; i++){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
238 levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table, hufftab[cb_sel[i]]->bits, 2);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
239 if(levlCoeffs[i] == 17)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
240 levlCoeffs[i] += get_bits(&q->gb, 4);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
241 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
242 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
243
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
244 static void imc_decode_level_coefficients(IMCContext* q, int* levlCoeffBuf, float* flcoeffs1,
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
245 float* flcoeffs2)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
246 {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
247 int i, level;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
248 float tmp, tmp2;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
249 //maybe some frequency division thingy
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
250
4198
18a371831f05 Remove log2() usage.
banan
parents: 4170
diff changeset
251 flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
18a371831f05 Remove log2() usage.
banan
parents: 4170
diff changeset
252 flcoeffs2[0] = log(flcoeffs1[0])/log(2);
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
253 tmp = flcoeffs1[0];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
254 tmp2 = flcoeffs2[0];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
255
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
256 for(i = 1; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
257 level = levlCoeffBuf[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
258 if (level == 16) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
259 flcoeffs1[i] = 1.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
260 flcoeffs2[i] = 0.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
261 } else {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
262 if (level < 17)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
263 level -=7;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
264 else if (level <= 24)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
265 level -=32;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
266 else
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
267 level -=16;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
268
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
269 tmp *= imc_exp_tab[15 + level];
4198
18a371831f05 Remove log2() usage.
banan
parents: 4170
diff changeset
270 tmp2 += 0.83048 * level; // 0.83048 = log2(10) * 0.25
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
271 flcoeffs1[i] = tmp;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
272 flcoeffs2[i] = tmp2;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
273 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
274 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
275 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
276
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
277
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
278 static void imc_decode_level_coefficients2(IMCContext* q, int* levlCoeffBuf, float* old_floor, float* flcoeffs1,
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
279 float* flcoeffs2) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
280 int i;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
281 //FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
282 // and flcoeffs2 old scale factors
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
283 // might be incomplete due to a missing table that is in the binary code
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
284 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
285 flcoeffs1[i] = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
286 if(levlCoeffBuf[i] < 16) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
287 flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
4198
18a371831f05 Remove log2() usage.
banan
parents: 4170
diff changeset
288 flcoeffs2[i] = (levlCoeffBuf[i]-7) * 0.83048 + flcoeffs2[i]; // 0.83048 = log2(10) * 0.25
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
289 } else {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
290 flcoeffs1[i] = old_floor[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
291 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
292 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
293 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
294
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
295 /**
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
296 * Perform bit allocation depending on bits available
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
297 */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
298 static int bit_allocation (IMCContext* q, int stream_format_code, int freebits, int flag) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
299 int i, j;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
300 const float limit = -1.e20;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
301 float highest = 0.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
302 int indx;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
303 int t1 = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
304 int t2 = 1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
305 float summa = 0.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
306 int iacc = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
307 int summer = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
308 int rres, cwlen;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
309 float lowest = 1.e10;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
310 int low_indx = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
311 float workT[32];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
312 int flg;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
313 int found_indx = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
314
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
315 for(i = 0; i < BANDS; i++)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
316 highest = FFMAX(highest, q->flcoeffs1[i]);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
317
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
318 for(i = 0; i < BANDS-1; i++) {
4212
68288509a159 Missed one log2().
banan
parents: 4198
diff changeset
319 q->flcoeffs4[i] = q->flcoeffs3[i] - log(q->flcoeffs5[i])/log(2);
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
320 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
321 q->flcoeffs4[BANDS - 1] = limit;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
322
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
323 highest = highest * 0.25;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
324
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
325 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
326 indx = -1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
327 if ((band_tab[i+1] - band_tab[i]) == q->bandWidthT[i])
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
328 indx = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
329
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
330 if ((band_tab[i+1] - band_tab[i]) > q->bandWidthT[i])
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
331 indx = 1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
332
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
333 if (((band_tab[i+1] - band_tab[i])/2) >= q->bandWidthT[i])
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
334 indx = 2;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
335
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
336 if (indx == -1)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
337 return -1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
338
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
339 q->flcoeffs4[i] = q->flcoeffs4[i] + xTab[(indx*2 + (q->flcoeffs1[i] < highest)) * 2 + flag];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
340 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
341
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
342 if (stream_format_code & 0x2) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
343 q->flcoeffs4[0] = limit;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
344 q->flcoeffs4[1] = limit;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
345 q->flcoeffs4[2] = limit;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
346 q->flcoeffs4[3] = limit;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
347 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
348
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
349 for(i = (stream_format_code & 0x2)?4:0; i < BANDS-1; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
350 iacc += q->bandWidthT[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
351 summa += q->bandWidthT[i] * q->flcoeffs4[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
352 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
353 q->bandWidthT[BANDS-1] = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
354 summa = (summa * 0.5 - freebits) / iacc;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
355
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
356
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
357 for(i = 0; i < BANDS/2; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
358 rres = summer - freebits;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
359 if((rres >= -8) && (rres <= 8)) break;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
360
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
361 summer = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
362 iacc = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
363
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
364 for(j = (stream_format_code & 0x2)?4:0; j < BANDS; j++) {
4594
a96d905dcbaa Add av_ prefix to clip functions
reimar
parents: 4212
diff changeset
365 cwlen = av_clip((int)((q->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
366
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
367 q->bitsBandT[j] = cwlen;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
368 summer += q->bandWidthT[j] * cwlen;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
369
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
370 if (cwlen > 0)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
371 iacc += q->bandWidthT[j];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
372 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
373
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
374 flg = t2;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
375 t2 = 1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
376 if (freebits < summer)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
377 t2 = -1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
378 if (i == 0)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
379 flg = t2;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
380 if(flg != t2)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
381 t1++;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
382
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
383 summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
384 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
385
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
386 for(i = (stream_format_code & 0x2)?4:0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
387 for(j = band_tab[i]; j < band_tab[i+1]; j++)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
388 q->CWlengthT[j] = q->bitsBandT[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
389 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
390
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
391 if (freebits > summer) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
392 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
393 workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
394 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
395
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
396 highest = 0.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
397
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
398 do{
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
399 if (highest <= -1.e20)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
400 break;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
401
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
402 found_indx = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
403 highest = -1.e20;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
404
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
405 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
406 if (workT[i] > highest) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
407 highest = workT[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
408 found_indx = i;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
409 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
410 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
411
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
412 if (highest > -1.e20) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
413 workT[found_indx] -= 2.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
414 if (++(q->bitsBandT[found_indx]) == 6)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
415 workT[found_indx] = -1.e20;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
416
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
417 for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (freebits > summer); j++){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
418 q->CWlengthT[j]++;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
419 summer++;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
420 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
421 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
422 }while (freebits > summer);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
423 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
424 if (freebits < summer) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
425 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
426 workT[i] = q->bitsBandT[i] ? (q->bitsBandT[i] * -2 + q->flcoeffs4[i] + 1.585) : 1.e20;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
427 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
428 if (stream_format_code & 0x2) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
429 workT[0] = 1.e20;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
430 workT[1] = 1.e20;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
431 workT[2] = 1.e20;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
432 workT[3] = 1.e20;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
433 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
434 while (freebits < summer){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
435 lowest = 1.e10;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
436 low_indx = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
437 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
438 if (workT[i] < lowest) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
439 lowest = workT[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
440 low_indx = i;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
441 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
442 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
443 //if(lowest >= 1.e10) break;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
444 workT[low_indx] = lowest + 2.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
445
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
446 if (!(--q->bitsBandT[low_indx]))
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
447 workT[low_indx] = 1.e20;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
448
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
449 for(j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
450 if(q->CWlengthT[j] > 0){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
451 q->CWlengthT[j]--;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
452 summer--;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
453 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
454 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
455 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
456 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
457 return 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
458 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
459
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
460 static void imc_get_skip_coeff(IMCContext* q) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
461 int i, j;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
462
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
463 memset(q->skipFlagBits, 0, sizeof(q->skipFlagBits));
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
464 memset(q->skipFlagCount, 0, sizeof(q->skipFlagCount));
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
465 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
466 if (!q->bandFlagsBuf[i] || !q->bandWidthT[i])
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
467 continue;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
468
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
469 if (!q->skipFlagRaw[i]) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
470 q->skipFlagBits[i] = band_tab[i+1] - band_tab[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
471
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
472 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
5513
9f8219a3b86f use get_bits1(..) instead get_bits(.., 1)
alex
parents: 5215
diff changeset
473 if ((q->skipFlags[j] = get_bits1(&q->gb)))
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
474 q->skipFlagCount[i]++;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
475 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
476 } else {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
477 for(j = band_tab[i]; j < (band_tab[i+1]-1); j += 2) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
478 if(!get_bits1(&q->gb)){//0
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
479 q->skipFlagBits[i]++;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
480 q->skipFlags[j]=1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
481 q->skipFlags[j+1]=1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
482 q->skipFlagCount[i] += 2;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
483 }else{
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
484 if(get_bits1(&q->gb)){//11
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
485 q->skipFlagBits[i] +=2;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
486 q->skipFlags[j]=0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
487 q->skipFlags[j+1]=1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
488 q->skipFlagCount[i]++;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
489 }else{
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
490 q->skipFlagBits[i] +=3;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
491 q->skipFlags[j+1]=0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
492 if(!get_bits1(&q->gb)){//100
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
493 q->skipFlags[j]=1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
494 q->skipFlagCount[i]++;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
495 }else{//101
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
496 q->skipFlags[j]=0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
497 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
498 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
499 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
500 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
501
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
502 if (j < band_tab[i+1]) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
503 q->skipFlagBits[i]++;
5513
9f8219a3b86f use get_bits1(..) instead get_bits(.., 1)
alex
parents: 5215
diff changeset
504 if ((q->skipFlags[j] = get_bits1(&q->gb)))
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
505 q->skipFlagCount[i]++;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
506 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
507 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
508 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
509 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
510
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
511 /**
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
512 * Increase highest' band coefficient sizes as some bits won't be used
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
513 */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
514 static void imc_adjust_bit_allocation (IMCContext* q, int summer) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
515 float workT[32];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
516 int corrected = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
517 int i, j;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
518 float highest = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
519 int found_indx=0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
520
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
521 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
522 workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
523 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
524
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
525 while (corrected < summer) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
526 if(highest <= -1.e20)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
527 break;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
528
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
529 highest = -1.e20;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
530
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
531 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
532 if (workT[i] > highest) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
533 highest = workT[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
534 found_indx = i;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
535 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
536 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
537
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
538 if (highest > -1.e20) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
539 workT[found_indx] -= 2.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
540 if (++(q->bitsBandT[found_indx]) == 6)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
541 workT[found_indx] = -1.e20;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
542
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
543 for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
544 if (!q->skipFlags[j] && (q->CWlengthT[j] < 6)) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
545 q->CWlengthT[j]++;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
546 corrected++;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
547 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
548 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
549 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
550 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
551 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
552
4170
f97a2081b5b1 make some symbols static
mru
parents: 4107
diff changeset
553 static void imc_imdct256(IMCContext *q) {
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
554 int i;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
555 float re, im;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
556
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
557 /* prerotation */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
558 for(i=0; i < COEFFS/2; i++){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
559 q->samples[i].re = -(q->pre_coef1[i] * q->CWdecoded[COEFFS-1-i*2]) -
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
560 (q->pre_coef2[i] * q->CWdecoded[i*2]);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
561 q->samples[i].im = (q->pre_coef2[i] * q->CWdecoded[COEFFS-1-i*2]) -
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
562 (q->pre_coef1[i] * q->CWdecoded[i*2]);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
563 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
564
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
565 /* FFT */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
566 ff_fft_permute(&q->fft, q->samples);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
567 ff_fft_calc (&q->fft, q->samples);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
568
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
569 /* postrotation, window and reorder */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
570 for(i = 0; i < COEFFS/2; i++){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
571 re = (q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
572 im = (-q->samples[i].im * q->post_cos[i]) - (q->samples[i].re * q->post_sin[i]);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
573 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);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
574 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);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
575 q->last_fft_im[i] = im;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
576 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
577 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
578
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
579 static int inverse_quant_coeff (IMCContext* q, int stream_format_code) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
580 int i, j;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
581 int middle_value, cw_len, max_size;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
582 const float* quantizer;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
583
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
584 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
585 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
586 q->CWdecoded[j] = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
587 cw_len = q->CWlengthT[j];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
588
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
589 if (cw_len <= 0 || q->skipFlags[j])
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
590 continue;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
591
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
592 max_size = 1 << cw_len;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
593 middle_value = max_size >> 1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
594
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
595 if (q->codewords[j] >= max_size || q->codewords[j] < 0)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
596 return -1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
597
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
598 if (cw_len >= 4){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
599 quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
600 if (q->codewords[j] >= middle_value)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
601 q->CWdecoded[j] = quantizer[q->codewords[j] - 8] * q->flcoeffs6[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
602 else
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
603 q->CWdecoded[j] = -quantizer[max_size - q->codewords[j] - 8 - 1] * q->flcoeffs6[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
604 }else{
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
605 quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (q->bandFlagsBuf[i] << 1)];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
606 if (q->codewords[j] >= middle_value)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
607 q->CWdecoded[j] = quantizer[q->codewords[j] - 1] * q->flcoeffs6[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
608 else
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
609 q->CWdecoded[j] = -quantizer[max_size - 2 - q->codewords[j]] * q->flcoeffs6[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
610 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
611 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
612 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
613 return 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
614 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
615
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
616
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
617 static int imc_get_coeffs (IMCContext* q) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
618 int i, j, cw_len, cw;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
619
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
620 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
621 if(!q->sumLenArr[i]) continue;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
622 if (q->bandFlagsBuf[i] || q->bandWidthT[i]) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
623 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
624 cw_len = q->CWlengthT[j];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
625 cw = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
626
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
627 if (get_bits_count(&q->gb) + cw_len > 512){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
628 //av_log(NULL,0,"Band %i coeff %i cw_len %i\n",i,j,cw_len);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
629 return -1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
630 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
631
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
632 if(cw_len && (!q->bandFlagsBuf[i] || !q->skipFlags[j]))
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
633 cw = get_bits(&q->gb, cw_len);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
634
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
635 q->codewords[j] = cw;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
636 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
637 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
638 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
639 return 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
640 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
641
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
642 static int imc_decode_frame(AVCodecContext * avctx,
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
643 void *data, int *data_size,
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8718
diff changeset
644 AVPacket *avpkt)
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
645 {
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8718
diff changeset
646 const uint8_t *buf = avpkt->data;
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8718
diff changeset
647 int buf_size = avpkt->size;
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
648
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
649 IMCContext *q = avctx->priv_data;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
650
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
651 int stream_format_code;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
652 int imc_hdr, i, j;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
653 int flag;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
654 int bits, summer;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
655 int counter, bitscount;
6308
195356fc7230 Do not modify input data
kostya
parents: 6307
diff changeset
656 uint16_t buf16[IMC_BLOCK_SIZE / 2];
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
657
6332
f2c6708aebf9 Check that we have enough input data in IMC decoder.
reimar
parents: 6308
diff changeset
658 if (buf_size < IMC_BLOCK_SIZE) {
f2c6708aebf9 Check that we have enough input data in IMC decoder.
reimar
parents: 6308
diff changeset
659 av_log(avctx, AV_LOG_ERROR, "imc frame too small!\n");
f2c6708aebf9 Check that we have enough input data in IMC decoder.
reimar
parents: 6308
diff changeset
660 return -1;
f2c6708aebf9 Check that we have enough input data in IMC decoder.
reimar
parents: 6308
diff changeset
661 }
6307
e0601aa8ebef IMC decoder always operates on 64-byte blocks
kostya
parents: 6218
diff changeset
662 for(i = 0; i < IMC_BLOCK_SIZE / 2; i++)
6308
195356fc7230 Do not modify input data
kostya
parents: 6307
diff changeset
663 buf16[i] = bswap_16(((const uint16_t*)buf)[i]);
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
664
6308
195356fc7230 Do not modify input data
kostya
parents: 6307
diff changeset
665 init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
666
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
667 /* Check the frame header */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
668 imc_hdr = get_bits(&q->gb, 9);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
669 if (imc_hdr != IMC_FRAME_ID) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
670 av_log(avctx, AV_LOG_ERROR, "imc frame header check failed!\n");
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
671 av_log(avctx, AV_LOG_ERROR, "got %x instead of 0x21.\n", imc_hdr);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
672 return -1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
673 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
674 stream_format_code = get_bits(&q->gb, 3);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
675
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
676 if(stream_format_code & 1){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
677 av_log(avctx, AV_LOG_ERROR, "Stream code format %X is not supported\n", stream_format_code);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
678 return -1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
679 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
680
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
681 // av_log(avctx, AV_LOG_DEBUG, "stream_format_code = %d\n", stream_format_code);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
682
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
683 if (stream_format_code & 0x04)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
684 q->decoder_reset = 1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
685
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
686 if(q->decoder_reset) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
687 memset(q->out_samples, 0, sizeof(q->out_samples));
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
688 for(i = 0; i < BANDS; i++)q->old_floor[i] = 1.0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
689 for(i = 0; i < COEFFS; i++)q->CWdecoded[i] = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
690 q->decoder_reset = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
691 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
692
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
693 flag = get_bits1(&q->gb);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
694 imc_read_level_coeffs(q, stream_format_code, q->levlCoeffBuf);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
695
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
696 if (stream_format_code & 0x4)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
697 imc_decode_level_coefficients(q, q->levlCoeffBuf, q->flcoeffs1, q->flcoeffs2);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
698 else
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
699 imc_decode_level_coefficients2(q, q->levlCoeffBuf, q->old_floor, q->flcoeffs1, q->flcoeffs2);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
700
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
701 memcpy(q->old_floor, q->flcoeffs1, 32 * sizeof(float));
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
702
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
703 counter = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
704 for (i=0 ; i<BANDS ; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
705 if (q->levlCoeffBuf[i] == 16) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
706 q->bandWidthT[i] = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
707 counter++;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
708 } else
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
709 q->bandWidthT[i] = band_tab[i+1] - band_tab[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
710 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
711 memset(q->bandFlagsBuf, 0, BANDS * sizeof(int));
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
712 for(i = 0; i < BANDS-1; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
713 if (q->bandWidthT[i])
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
714 q->bandFlagsBuf[i] = get_bits1(&q->gb);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
715 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
716
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
717 imc_calculate_coeffs(q, q->flcoeffs1, q->flcoeffs2, q->bandWidthT, q->flcoeffs3, q->flcoeffs5);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
718
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
719 bitscount = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
720 /* first 4 bands will be assigned 5 bits per coefficient */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
721 if (stream_format_code & 0x2) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
722 bitscount += 15;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
723
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
724 q->bitsBandT[0] = 5;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
725 q->CWlengthT[0] = 5;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
726 q->CWlengthT[1] = 5;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
727 q->CWlengthT[2] = 5;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
728 for(i = 1; i < 4; i++){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
729 bits = (q->levlCoeffBuf[i] == 16) ? 0 : 5;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
730 q->bitsBandT[i] = bits;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
731 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
732 q->CWlengthT[j] = bits;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
733 bitscount += bits;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
734 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
735 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
736 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
737
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
738 if(bit_allocation (q, stream_format_code, 512 - bitscount - get_bits_count(&q->gb), flag) < 0) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
739 av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
740 q->decoder_reset = 1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
741 return -1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
742 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
743
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
744 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
745 q->sumLenArr[i] = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
746 q->skipFlagRaw[i] = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
747 for(j = band_tab[i]; j < band_tab[i+1]; j++)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
748 q->sumLenArr[i] += q->CWlengthT[j];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
749 if (q->bandFlagsBuf[i])
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
750 if( (((band_tab[i+1] - band_tab[i]) * 1.5) > q->sumLenArr[i]) && (q->sumLenArr[i] > 0))
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
751 q->skipFlagRaw[i] = 1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
752 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
753
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
754 imc_get_skip_coeff(q);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
755
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
756 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
757 q->flcoeffs6[i] = q->flcoeffs1[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
758 /* band has flag set and at least one coded coefficient */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
759 if (q->bandFlagsBuf[i] && (band_tab[i+1] - band_tab[i]) != q->skipFlagCount[i]){
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
760 q->flcoeffs6[i] *= q->sqrt_tab[band_tab[i+1] - band_tab[i]] /
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
761 q->sqrt_tab[(band_tab[i+1] - band_tab[i] - q->skipFlagCount[i])];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
762 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
763 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
764
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
765 /* calculate bits left, bits needed and adjust bit allocation */
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
766 bits = summer = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
767
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
768 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
769 if (q->bandFlagsBuf[i]) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
770 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
771 if(q->skipFlags[j]) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
772 summer += q->CWlengthT[j];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
773 q->CWlengthT[j] = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
774 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
775 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
776 bits += q->skipFlagBits[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
777 summer -= q->skipFlagBits[i];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
778 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
779 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
780 imc_adjust_bit_allocation(q, summer);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
781
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
782 for(i = 0; i < BANDS; i++) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
783 q->sumLenArr[i] = 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
784
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
785 for(j = band_tab[i]; j < band_tab[i+1]; j++)
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
786 if (!q->skipFlags[j])
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
787 q->sumLenArr[i] += q->CWlengthT[j];
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
788 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
789
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
790 memset(q->codewords, 0, sizeof(q->codewords));
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
791
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
792 if(imc_get_coeffs(q) < 0) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
793 av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n");
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
794 q->decoder_reset = 1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
795 return 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
796 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
797
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
798 if(inverse_quant_coeff(q, stream_format_code) < 0) {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
799 av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
800 q->decoder_reset = 1;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
801 return 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
802 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
803
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
804 memset(q->skipFlags, 0, sizeof(q->skipFlags));
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
805
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
806 imc_imdct256(q);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
807
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
808 q->dsp.float_to_int16(data, q->out_samples, COEFFS);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
809
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
810 *data_size = COEFFS * sizeof(int16_t);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
811
6307
e0601aa8ebef IMC decoder always operates on 64-byte blocks
kostya
parents: 6218
diff changeset
812 return IMC_BLOCK_SIZE;
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
813 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
814
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
815
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6332
diff changeset
816 static av_cold int imc_decode_close(AVCodecContext * avctx)
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
817 {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
818 IMCContext *q = avctx->priv_data;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
819
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
820 ff_fft_end(&q->fft);
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
821 return 0;
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
822 }
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
823
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
824
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
825 AVCodec imc_decoder = {
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
826 .name = "imc",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 11370
diff changeset
827 .type = AVMEDIA_TYPE_AUDIO,
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
828 .id = CODEC_ID_IMC,
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
829 .priv_data_size = sizeof(IMCContext),
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
830 .init = imc_decode_init,
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
831 .close = imc_decode_close,
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
832 .decode = imc_decode_frame,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6731
diff changeset
833 .long_name = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),
4106
21aa7e801c7b IMC decoder
kostya
parents:
diff changeset
834 };