diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/libcook/cook.c | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/libcook/cook.c')
-rw-r--r-- | lib/rbcodec/codecs/libcook/cook.c | 907 |
1 files changed, 907 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libcook/cook.c b/lib/rbcodec/codecs/libcook/cook.c new file mode 100644 index 0000000000..29a1bab7d6 --- /dev/null +++ b/lib/rbcodec/codecs/libcook/cook.c | |||
@@ -0,0 +1,907 @@ | |||
1 | /* | ||
2 | * COOK compatible decoder | ||
3 | * Copyright (c) 2003 Sascha Sommer | ||
4 | * Copyright (c) 2005 Benjamin Larsson | ||
5 | * | ||
6 | * This file is part of FFmpeg. | ||
7 | * | ||
8 | * FFmpeg is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU Lesser General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2.1 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * FFmpeg is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with FFmpeg; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | |||
23 | /** | ||
24 | * @file cook.c | ||
25 | * Cook compatible decoder. Bastardization of the G.722.1 standard. | ||
26 | * This decoder handles RealNetworks, RealAudio G2 data. | ||
27 | * Cook is identified by the codec name cook in RM files. | ||
28 | * | ||
29 | * To use this decoder, a calling application must supply the extradata | ||
30 | * bytes provided from the RM container; 8+ bytes for mono streams and | ||
31 | * 16+ for stereo streams (maybe more). | ||
32 | * | ||
33 | * Codec technicalities (all this assume a buffer length of 1024): | ||
34 | * Cook works with several different techniques to achieve its compression. | ||
35 | * In the timedomain the buffer is divided into 8 pieces and quantized. If | ||
36 | * two neighboring pieces have different quantization index a smooth | ||
37 | * quantization curve is used to get a smooth overlap between the different | ||
38 | * pieces. | ||
39 | * To get to the transformdomain Cook uses a modulated lapped transform. | ||
40 | * The transform domain has 50 subbands with 20 elements each. This | ||
41 | * means only a maximum of 50*20=1000 coefficients are used out of the 1024 | ||
42 | * available. | ||
43 | */ | ||
44 | |||
45 | #include <math.h> | ||
46 | #include <stddef.h> | ||
47 | #include <stdio.h> | ||
48 | #include <limits.h> | ||
49 | #include <string.h> | ||
50 | #include "codeclib.h" | ||
51 | |||
52 | #include "cook.h" | ||
53 | #include "cookdata.h" | ||
54 | |||
55 | /* the different Cook versions */ | ||
56 | #define MONO 0x1000001 | ||
57 | #define STEREO 0x1000002 | ||
58 | #define JOINT_STEREO 0x1000003 | ||
59 | #define MC_COOK 0x2000000 //multichannel Cook, not supported | ||
60 | |||
61 | #define SUBBAND_SIZE 20 | ||
62 | #define MAX_SUBPACKETS 5 | ||
63 | //#define COOKDEBUG | ||
64 | #ifndef COOKDEBUG | ||
65 | #undef DEBUGF | ||
66 | #define DEBUGF(...) | ||
67 | #endif | ||
68 | |||
69 | /** | ||
70 | * Random bit stream generator. | ||
71 | */ | ||
72 | static inline int cook_random(COOKContext *q) | ||
73 | { | ||
74 | q->random_state = | ||
75 | q->random_state * 214013 + 2531011; /* typical RNG numbers */ | ||
76 | |||
77 | return (q->random_state/0x1000000)&1; /*>>31*/ | ||
78 | } | ||
79 | #include "cook_fixpoint.h" | ||
80 | |||
81 | /* debug functions */ | ||
82 | |||
83 | #ifdef COOKDEBUG | ||
84 | static void dump_int_table(int* table, int size, int delimiter) { | ||
85 | int i=0; | ||
86 | DEBUGF("\n[%d]: ",i); | ||
87 | for (i=0 ; i<size ; i++) { | ||
88 | DEBUGF("%d, ", table[i]); | ||
89 | if ((i+1)%delimiter == 0) DEBUGF("\n[%d]: ",i+1); | ||
90 | } | ||
91 | } | ||
92 | |||
93 | static void dump_short_table(short* table, int size, int delimiter) { | ||
94 | int i=0; | ||
95 | DEBUGF("\n[%d]: ",i); | ||
96 | for (i=0 ; i<size ; i++) { | ||
97 | DEBUGF("%d, ", table[i]); | ||
98 | if ((i+1)%delimiter == 0) DEBUGF("\n[%d]: ",i+1); | ||
99 | } | ||
100 | } | ||
101 | |||
102 | #endif | ||
103 | |||
104 | /*************** init functions ***************/ | ||
105 | /* Codebook sizes (11586 * 4 bytes in total) */ | ||
106 | /* Used for envelope_quant_index[]. */ | ||
107 | static VLC_TYPE vlcbuf00[ 520][2] IBSS_ATTR_COOK_LARGE_IRAM; | ||
108 | static VLC_TYPE vlcbuf01[ 640][2] IBSS_ATTR_COOK_LARGE_IRAM; | ||
109 | static VLC_TYPE vlcbuf02[ 544][2] IBSS_ATTR_COOK_LARGE_IRAM; | ||
110 | static VLC_TYPE vlcbuf03[ 528][2] IBSS_ATTR_COOK_VLCBUF; | ||
111 | static VLC_TYPE vlcbuf04[ 544][2] IBSS_ATTR_COOK_VLCBUF; | ||
112 | static VLC_TYPE vlcbuf05[ 544][2] IBSS_ATTR_COOK_VLCBUF; | ||
113 | static VLC_TYPE vlcbuf06[ 640][2] IBSS_ATTR_COOK_VLCBUF; | ||
114 | static VLC_TYPE vlcbuf07[ 576][2] IBSS_ATTR_COOK_VLCBUF; | ||
115 | static VLC_TYPE vlcbuf08[ 528][2] IBSS_ATTR_COOK_VLCBUF; | ||
116 | static VLC_TYPE vlcbuf09[ 544][2] IBSS_ATTR_COOK_VLCBUF; | ||
117 | static VLC_TYPE vlcbuf10[ 544][2] IBSS_ATTR_COOK_VLCBUF; | ||
118 | static VLC_TYPE vlcbuf11[ 640][2] IBSS_ATTR_COOK_VLCBUF; | ||
119 | static VLC_TYPE vlcbuf12[ 544][2] IBSS_ATTR_COOK_LARGE_IRAM; | ||
120 | /* Used for sqvh[]. */ | ||
121 | static VLC_TYPE vlcbuf13[ 622][2] IBSS_ATTR_COOK_LARGE_IRAM; | ||
122 | static VLC_TYPE vlcbuf14[ 308][2] IBSS_ATTR_COOK_LARGE_IRAM; | ||
123 | static VLC_TYPE vlcbuf15[ 280][2] IBSS_ATTR_COOK_LARGE_IRAM; | ||
124 | static VLC_TYPE vlcbuf16[1456][2] IBSS_ATTR_COOK_LARGE_IRAM; | ||
125 | static VLC_TYPE vlcbuf17[ 694][2] IBSS_ATTR_COOK_LARGE_IRAM; | ||
126 | static VLC_TYPE vlcbuf18[ 698][2] IBSS_ATTR_COOK_LARGE_IRAM; | ||
127 | static VLC_TYPE vlcbuf19[ 104][2] IBSS_ATTR_COOK_LARGE_IRAM; | ||
128 | /* Used for ccpl. */ | ||
129 | static VLC_TYPE vlcbuf20[ 88][2] IBSS_ATTR_COOK_VLCBUF; | ||
130 | |||
131 | /* Code book sizes (11586 entries in total) */ | ||
132 | static int env_size[13] = {520,640,544, 528,544,544,640,576,528,544,544,640,544}; | ||
133 | static int sqvh_size[7] = {622,308,280,1456,694,698,104}; | ||
134 | static int ccpl_size = 88; | ||
135 | |||
136 | |||
137 | static int init_cook_vlc_tables(COOKContext *q) { | ||
138 | int i, result = 0; | ||
139 | |||
140 | /* Set pointers for codebooks. */ | ||
141 | q->envelope_quant_index[ 0].table = vlcbuf00; | ||
142 | q->envelope_quant_index[ 1].table = vlcbuf01; | ||
143 | q->envelope_quant_index[ 2].table = vlcbuf02; | ||
144 | q->envelope_quant_index[ 3].table = vlcbuf03; | ||
145 | q->envelope_quant_index[ 4].table = vlcbuf04; | ||
146 | q->envelope_quant_index[ 5].table = vlcbuf05; | ||
147 | q->envelope_quant_index[ 6].table = vlcbuf06; | ||
148 | q->envelope_quant_index[ 7].table = vlcbuf07; | ||
149 | q->envelope_quant_index[ 8].table = vlcbuf08; | ||
150 | q->envelope_quant_index[ 9].table = vlcbuf09; | ||
151 | q->envelope_quant_index[10].table = vlcbuf10; | ||
152 | q->envelope_quant_index[11].table = vlcbuf11; | ||
153 | q->envelope_quant_index[12].table = vlcbuf12; | ||
154 | q->sqvh[0].table = vlcbuf13; | ||
155 | q->sqvh[1].table = vlcbuf14; | ||
156 | q->sqvh[2].table = vlcbuf15; | ||
157 | q->sqvh[3].table = vlcbuf16; | ||
158 | q->sqvh[4].table = vlcbuf17; | ||
159 | q->sqvh[5].table = vlcbuf18; | ||
160 | q->sqvh[6].table = vlcbuf19; | ||
161 | q->ccpl.table = vlcbuf20; | ||
162 | |||
163 | /* Init envelope VLC (13 books) */ | ||
164 | for (i=0 ; i<13 ; i++) { | ||
165 | q->envelope_quant_index[i].table_allocated = env_size[i]; | ||
166 | result |= init_vlc (&q->envelope_quant_index[i], 9, 24, | ||
167 | envelope_quant_index_huffbits[i], 1, 1, | ||
168 | envelope_quant_index_huffcodes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); | ||
169 | } | ||
170 | |||
171 | /* Init subband VLC (7 books) */ | ||
172 | for (i=0 ; i<7 ; i++) { | ||
173 | q->sqvh[i].table_allocated = sqvh_size[i]; | ||
174 | result |= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i], | ||
175 | cvh_huffbits[i], 1, 1, | ||
176 | cvh_huffcodes[i], 2, 2, INIT_VLC_USE_NEW_STATIC); | ||
177 | } | ||
178 | |||
179 | /* Init Joint-Stereo VLC (1 book) */ | ||
180 | if (q->nb_channels==2 && q->joint_stereo==1){ | ||
181 | q->ccpl.table_allocated = ccpl_size; | ||
182 | result |= init_vlc (&q->ccpl, 6, (1<<q->js_vlc_bits)-1, | ||
183 | ccpl_huffbits[q->js_vlc_bits-2], 1, 1, | ||
184 | ccpl_huffcodes[q->js_vlc_bits-2], 2, 2, INIT_VLC_USE_NEW_STATIC); | ||
185 | DEBUGF("Joint-stereo VLC used.\n"); | ||
186 | } | ||
187 | |||
188 | DEBUGF("VLC tables initialized. Result = %d\n",result); | ||
189 | return result; | ||
190 | } | ||
191 | /*************** init functions end ***********/ | ||
192 | |||
193 | /** | ||
194 | * Cook indata decoding, every 32 bits are XORed with 0x37c511f2. | ||
195 | * Why? No idea, some checksum/error detection method maybe. | ||
196 | * | ||
197 | * Out buffer size: extra bytes are needed to cope with | ||
198 | * padding/misalignment. | ||
199 | * Subpackets passed to the decoder can contain two, consecutive | ||
200 | * half-subpackets, of identical but arbitrary size. | ||
201 | * 1234 1234 1234 1234 extraA extraB | ||
202 | * Case 1: AAAA BBBB 0 0 | ||
203 | * Case 2: AAAA ABBB BB-- 3 3 | ||
204 | * Case 3: AAAA AABB BBBB 2 2 | ||
205 | * Case 4: AAAA AAAB BBBB BB-- 1 5 | ||
206 | * | ||
207 | * Nice way to waste CPU cycles. | ||
208 | * | ||
209 | * @param inbuffer pointer to byte array of indata | ||
210 | * @param out pointer to byte array of outdata | ||
211 | * @param bytes number of bytes | ||
212 | */ | ||
213 | #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes)+3) % 4) | ||
214 | #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes))) | ||
215 | |||
216 | static inline int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes){ | ||
217 | int i, off; | ||
218 | uint32_t c; | ||
219 | const uint32_t* buf; | ||
220 | uint32_t* obuf = (uint32_t*) out; | ||
221 | /* FIXME: 64 bit platforms would be able to do 64 bits at a time. | ||
222 | * I'm too lazy though, should be something like | ||
223 | * for(i=0 ; i<bitamount/64 ; i++) | ||
224 | * (int64_t)out[i] = 0x37c511f237c511f2^be2me_64(int64_t)in[i]); | ||
225 | * Buffer alignment needs to be checked. */ | ||
226 | |||
227 | off = (intptr_t)inbuffer & 3; | ||
228 | buf = (const uint32_t*) (inbuffer - off); | ||
229 | c = be2me_32((0x37c511f2 >> (off*8)) | (0x37c511f2 << (32-(off*8)))); | ||
230 | bytes += 3 + off; | ||
231 | for (i = 0; i < bytes/4; i++) | ||
232 | obuf[i] = c ^ buf[i]; | ||
233 | |||
234 | return off; | ||
235 | } | ||
236 | |||
237 | /** | ||
238 | * Fill the gain array for the timedomain quantization. | ||
239 | * | ||
240 | * @param q pointer to the COOKContext | ||
241 | * @param gaininfo[9] array of gain indexes | ||
242 | */ | ||
243 | |||
244 | static void decode_gain_info(GetBitContext *gb, int *gaininfo) | ||
245 | { | ||
246 | int i, n; | ||
247 | |||
248 | while (get_bits1(gb)) {} | ||
249 | n = get_bits_count(gb) - 1; //amount of elements*2 to update | ||
250 | |||
251 | i = 0; | ||
252 | while (n--) { | ||
253 | int index = get_bits(gb, 3); | ||
254 | int gain = get_bits1(gb) ? (int)get_bits(gb, 4) - 7 : -1; | ||
255 | |||
256 | while (i <= index) gaininfo[i++] = gain; | ||
257 | } | ||
258 | while (i <= 8) gaininfo[i++] = 0; | ||
259 | } | ||
260 | |||
261 | /** | ||
262 | * Create the quant index table needed for the envelope. | ||
263 | * | ||
264 | * @param q pointer to the COOKContext | ||
265 | * @param quant_index_table pointer to the array | ||
266 | */ | ||
267 | |||
268 | static void decode_envelope(COOKContext *q, int* quant_index_table) { | ||
269 | int i,j, vlc_index; | ||
270 | |||
271 | quant_index_table[0]= get_bits(&q->gb,6) - 6; //This is used later in categorize | ||
272 | |||
273 | for (i=1 ; i < q->total_subbands ; i++){ | ||
274 | vlc_index=i; | ||
275 | if (i >= q->js_subband_start * 2) { | ||
276 | vlc_index-=q->js_subband_start; | ||
277 | } else { | ||
278 | vlc_index/=2; | ||
279 | if(vlc_index < 1) vlc_index = 1; | ||
280 | } | ||
281 | if (vlc_index>13) vlc_index = 13; //the VLC tables >13 are identical to No. 13 | ||
282 | |||
283 | j = get_vlc2(&q->gb, q->envelope_quant_index[vlc_index-1].table, | ||
284 | q->envelope_quant_index[vlc_index-1].bits,2); | ||
285 | quant_index_table[i] = quant_index_table[i-1] + j - 12; //differential encoding | ||
286 | } | ||
287 | } | ||
288 | |||
289 | /** | ||
290 | * Calculate the category and category_index vector. | ||
291 | * | ||
292 | * @param q pointer to the COOKContext | ||
293 | * @param quant_index_table pointer to the array | ||
294 | * @param category pointer to the category array | ||
295 | * @param category_index pointer to the category_index array | ||
296 | */ | ||
297 | |||
298 | static void categorize(COOKContext *q, int* quant_index_table, | ||
299 | int* category, int* category_index){ | ||
300 | int exp_idx, bias, tmpbias1, tmpbias2, bits_left, num_bits, index, v, i, j; | ||
301 | int exp_index2[102]; | ||
302 | int exp_index1[102]; | ||
303 | |||
304 | int tmp_categorize_array[128*2]; | ||
305 | int tmp_categorize_array1_idx=q->numvector_size; | ||
306 | int tmp_categorize_array2_idx=q->numvector_size; | ||
307 | |||
308 | bits_left = q->bits_per_subpacket - get_bits_count(&q->gb); | ||
309 | |||
310 | if(bits_left > q->samples_per_channel) { | ||
311 | bits_left = q->samples_per_channel + | ||
312 | ((bits_left - q->samples_per_channel)*5)/8; | ||
313 | //av_log(q->avctx, AV_LOG_ERROR, "bits_left = %d\n",bits_left); | ||
314 | } | ||
315 | |||
316 | memset(&exp_index1,0,102*sizeof(int)); | ||
317 | memset(&exp_index2,0,102*sizeof(int)); | ||
318 | memset(&tmp_categorize_array,0,128*2*sizeof(int)); | ||
319 | |||
320 | bias=-32; | ||
321 | |||
322 | /* Estimate bias. */ | ||
323 | for (i=32 ; i>0 ; i=i/2){ | ||
324 | num_bits = 0; | ||
325 | index = 0; | ||
326 | for (j=q->total_subbands ; j>0 ; j--){ | ||
327 | exp_idx = av_clip((i - quant_index_table[index] + bias) / 2, 0, 7); | ||
328 | index++; | ||
329 | num_bits+=expbits_tab[exp_idx]; | ||
330 | } | ||
331 | if(num_bits >= bits_left - 32){ | ||
332 | bias+=i; | ||
333 | } | ||
334 | } | ||
335 | |||
336 | /* Calculate total number of bits. */ | ||
337 | num_bits=0; | ||
338 | for (i=0 ; i<q->total_subbands ; i++) { | ||
339 | exp_idx = av_clip((bias - quant_index_table[i]) / 2, 0, 7); | ||
340 | num_bits += expbits_tab[exp_idx]; | ||
341 | exp_index1[i] = exp_idx; | ||
342 | exp_index2[i] = exp_idx; | ||
343 | } | ||
344 | tmpbias1 = tmpbias2 = num_bits; | ||
345 | |||
346 | for (j = 1 ; j < q->numvector_size ; j++) { | ||
347 | if (tmpbias1 + tmpbias2 > 2*bits_left) { /* ---> */ | ||
348 | int max = -999999; | ||
349 | index=-1; | ||
350 | for (i=0 ; i<q->total_subbands ; i++){ | ||
351 | if (exp_index1[i] < 7) { | ||
352 | v = (-2*exp_index1[i]) - quant_index_table[i] + bias; | ||
353 | if ( v >= max) { | ||
354 | max = v; | ||
355 | index = i; | ||
356 | } | ||
357 | } | ||
358 | } | ||
359 | if(index==-1)break; | ||
360 | tmp_categorize_array[tmp_categorize_array1_idx++] = index; | ||
361 | tmpbias1 -= expbits_tab[exp_index1[index]] - | ||
362 | expbits_tab[exp_index1[index]+1]; | ||
363 | ++exp_index1[index]; | ||
364 | } else { /* <--- */ | ||
365 | int min = 999999; | ||
366 | index=-1; | ||
367 | for (i=0 ; i<q->total_subbands ; i++){ | ||
368 | if(exp_index2[i] > 0){ | ||
369 | v = (-2*exp_index2[i])-quant_index_table[i]+bias; | ||
370 | if ( v < min) { | ||
371 | min = v; | ||
372 | index = i; | ||
373 | } | ||
374 | } | ||
375 | } | ||
376 | if(index == -1)break; | ||
377 | tmp_categorize_array[--tmp_categorize_array2_idx] = index; | ||
378 | tmpbias2 -= expbits_tab[exp_index2[index]] - | ||
379 | expbits_tab[exp_index2[index]-1]; | ||
380 | --exp_index2[index]; | ||
381 | } | ||
382 | } | ||
383 | memcpy(category, exp_index2, sizeof(int) * q->total_subbands ); | ||
384 | memcpy(category_index, tmp_categorize_array+tmp_categorize_array2_idx, sizeof(int) * (q->numvector_size-1) ); | ||
385 | } | ||
386 | |||
387 | |||
388 | /** | ||
389 | * Expand the category vector. | ||
390 | * | ||
391 | * @param q pointer to the COOKContext | ||
392 | * @param category pointer to the category array | ||
393 | * @param category_index pointer to the category_index array | ||
394 | */ | ||
395 | |||
396 | static inline void expand_category(COOKContext *q, int* category, | ||
397 | int* category_index){ | ||
398 | int i; | ||
399 | for(i=0 ; i<q->num_vectors ; i++){ | ||
400 | ++category[category_index[i]]; | ||
401 | } | ||
402 | } | ||
403 | |||
404 | /** | ||
405 | * Unpack the subband_coef_index and subband_coef_sign vectors. | ||
406 | * | ||
407 | * @param q pointer to the COOKContext | ||
408 | * @param category pointer to the category array | ||
409 | * @param subband_coef_index array of indexes to quant_centroid_tab | ||
410 | * @param subband_coef_sign signs of coefficients | ||
411 | */ | ||
412 | |||
413 | static int unpack_SQVH(COOKContext *q, int category, int* subband_coef_index, | ||
414 | int* subband_coef_sign) { | ||
415 | int i,j; | ||
416 | int vlc, vd ,tmp, result; | ||
417 | |||
418 | vd = vd_tab[category]; | ||
419 | result = 0; | ||
420 | for(i=0 ; i<vpr_tab[category] ; i++) | ||
421 | { | ||
422 | vlc = get_vlc2(&q->gb, q->sqvh[category].table, q->sqvh[category].bits, 3); | ||
423 | if (q->bits_per_subpacket < get_bits_count(&q->gb)) | ||
424 | { | ||
425 | vlc = 0; | ||
426 | result = 1; | ||
427 | memset(subband_coef_index, 0, sizeof(int)*vd); | ||
428 | memset(subband_coef_sign, 0, sizeof(int)*vd); | ||
429 | subband_coef_index+=vd; | ||
430 | subband_coef_sign+=vd; | ||
431 | } | ||
432 | else | ||
433 | { | ||
434 | for(j=vd-1 ; j>=0 ; j--){ | ||
435 | tmp = (vlc * invradix_tab[category])/0x100000; | ||
436 | subband_coef_index[j] = vlc - tmp * (kmax_tab[category]+1); | ||
437 | vlc = tmp; | ||
438 | } | ||
439 | |||
440 | for(j=0 ; j<vd ; j++) | ||
441 | { | ||
442 | if (*subband_coef_index++) { | ||
443 | if(get_bits_count(&q->gb) < q->bits_per_subpacket) { | ||
444 | *subband_coef_sign++ = get_bits1(&q->gb); | ||
445 | } else { | ||
446 | result=1; | ||
447 | *subband_coef_sign++=0; | ||
448 | } | ||
449 | } else { | ||
450 | *subband_coef_sign++=0; | ||
451 | } | ||
452 | } | ||
453 | } | ||
454 | } | ||
455 | return result; | ||
456 | } | ||
457 | |||
458 | |||
459 | /** | ||
460 | * Fill the mlt_buffer with mlt coefficients. | ||
461 | * | ||
462 | * @param q pointer to the COOKContext | ||
463 | * @param category pointer to the category array | ||
464 | * @param quant_index_table pointer to the array | ||
465 | * @param mlt_buffer pointer to mlt coefficients | ||
466 | */ | ||
467 | |||
468 | static void decode_vectors(COOKContext* q, int* category, | ||
469 | int *quant_index_table, REAL_T* mlt_buffer) | ||
470 | ICODE_ATTR_COOK_DECODE; | ||
471 | static void decode_vectors(COOKContext* q, int* category, | ||
472 | int *quant_index_table, REAL_T* mlt_buffer){ | ||
473 | /* A zero in this table means that the subband coefficient is | ||
474 | random noise coded. */ | ||
475 | int subband_coef_index[SUBBAND_SIZE]; | ||
476 | /* A zero in this table means that the subband coefficient is a | ||
477 | positive multiplicator. */ | ||
478 | int subband_coef_sign[SUBBAND_SIZE]; | ||
479 | int band, j; | ||
480 | int index=0; | ||
481 | |||
482 | for(band=0 ; band<q->total_subbands ; band++){ | ||
483 | index = category[band]; | ||
484 | if(category[band] < 7){ | ||
485 | if(unpack_SQVH(q, category[band], subband_coef_index, subband_coef_sign)){ | ||
486 | index=7; | ||
487 | for(j=0 ; j<q->total_subbands ; j++) category[band+j]=7; | ||
488 | } | ||
489 | } | ||
490 | if(index>=7) { | ||
491 | memset(subband_coef_index, 0, sizeof(subband_coef_index)); | ||
492 | memset(subband_coef_sign, 0, sizeof(subband_coef_sign)); | ||
493 | } | ||
494 | scalar_dequant_math(q, index, quant_index_table[band], | ||
495 | subband_coef_index, subband_coef_sign, | ||
496 | &mlt_buffer[band * SUBBAND_SIZE]); | ||
497 | } | ||
498 | |||
499 | if(q->total_subbands*SUBBAND_SIZE >= q->samples_per_channel){ | ||
500 | return; | ||
501 | } /* FIXME: should this be removed, or moved into loop above? */ | ||
502 | } | ||
503 | |||
504 | |||
505 | /** | ||
506 | * function for decoding mono data | ||
507 | * | ||
508 | * @param q pointer to the COOKContext | ||
509 | * @param mlt_buffer pointer to mlt coefficients | ||
510 | */ | ||
511 | |||
512 | static void mono_decode(COOKContext *q, REAL_T* mlt_buffer) ICODE_ATTR_COOK_DECODE; | ||
513 | static void mono_decode(COOKContext *q, REAL_T* mlt_buffer) { | ||
514 | |||
515 | int category_index[128]; | ||
516 | int quant_index_table[102]; | ||
517 | int category[128]; | ||
518 | |||
519 | memset(&category, 0, 128*sizeof(int)); | ||
520 | memset(&category_index, 0, 128*sizeof(int)); | ||
521 | |||
522 | decode_envelope(q, quant_index_table); | ||
523 | q->num_vectors = get_bits(&q->gb,q->log2_numvector_size); | ||
524 | categorize(q, quant_index_table, category, category_index); | ||
525 | expand_category(q, category, category_index); | ||
526 | decode_vectors(q, category, quant_index_table, mlt_buffer); | ||
527 | } | ||
528 | |||
529 | /** | ||
530 | * function for getting the jointstereo coupling information | ||
531 | * | ||
532 | * @param q pointer to the COOKContext | ||
533 | * @param decouple_tab decoupling array | ||
534 | * | ||
535 | */ | ||
536 | |||
537 | static void decouple_info(COOKContext *q, int* decouple_tab){ | ||
538 | int length, i; | ||
539 | |||
540 | if(get_bits1(&q->gb)) { | ||
541 | if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return; | ||
542 | |||
543 | length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1; | ||
544 | for (i=0 ; i<length ; i++) { | ||
545 | decouple_tab[cplband[q->js_subband_start] + i] = get_vlc2(&q->gb, q->ccpl.table, q->ccpl.bits, 2); | ||
546 | } | ||
547 | return; | ||
548 | } | ||
549 | |||
550 | if(cplband[q->js_subband_start] > cplband[q->subbands-1]) return; | ||
551 | |||
552 | length = cplband[q->subbands-1] - cplband[q->js_subband_start] + 1; | ||
553 | for (i=0 ; i<length ; i++) { | ||
554 | decouple_tab[cplband[q->js_subband_start] + i] = get_bits(&q->gb, q->js_vlc_bits); | ||
555 | } | ||
556 | return; | ||
557 | } | ||
558 | |||
559 | /** | ||
560 | * function for decoding joint stereo data | ||
561 | * | ||
562 | * @param q pointer to the COOKContext | ||
563 | * @param mlt_buffer1 pointer to left channel mlt coefficients | ||
564 | * @param mlt_buffer2 pointer to right channel mlt coefficients | ||
565 | */ | ||
566 | |||
567 | static void joint_decode(COOKContext *q, REAL_T* mlt_buffer1, | ||
568 | REAL_T* mlt_buffer2) { | ||
569 | int i; | ||
570 | int decouple_tab[SUBBAND_SIZE]; | ||
571 | REAL_T *decode_buffer = q->decode_buffer_0; | ||
572 | int idx; | ||
573 | |||
574 | memset(decouple_tab, 0, sizeof(decouple_tab)); | ||
575 | memset(decode_buffer, 0, sizeof(q->decode_buffer_0)); | ||
576 | |||
577 | /* Make sure the buffers are zeroed out. */ | ||
578 | memset(mlt_buffer1,0, 1024*sizeof(REAL_T)); | ||
579 | memset(mlt_buffer2,0, 1024*sizeof(REAL_T)); | ||
580 | decouple_info(q, decouple_tab); | ||
581 | mono_decode(q, decode_buffer); | ||
582 | |||
583 | /* The two channels are stored interleaved in decode_buffer. */ | ||
584 | REAL_T * mlt_buffer1_end = mlt_buffer1 + (q->js_subband_start*SUBBAND_SIZE); | ||
585 | while(mlt_buffer1 < mlt_buffer1_end) | ||
586 | { | ||
587 | memcpy(mlt_buffer1,decode_buffer,sizeof(REAL_T)*SUBBAND_SIZE); | ||
588 | memcpy(mlt_buffer2,decode_buffer+20,sizeof(REAL_T)*SUBBAND_SIZE); | ||
589 | mlt_buffer1 += 20; | ||
590 | mlt_buffer2 += 20; | ||
591 | decode_buffer += 40; | ||
592 | } | ||
593 | |||
594 | /* When we reach js_subband_start (the higher frequencies) | ||
595 | the coefficients are stored in a coupling scheme. */ | ||
596 | idx = (1 << q->js_vlc_bits) - 1; | ||
597 | for (i=q->js_subband_start ; i<q->subbands ; i++) { | ||
598 | int i1 = decouple_tab[cplband[i]]; | ||
599 | int i2 = idx - i1 - 1; | ||
600 | mlt_buffer1_end = mlt_buffer1 + SUBBAND_SIZE; | ||
601 | while(mlt_buffer1 < mlt_buffer1_end) | ||
602 | { | ||
603 | *mlt_buffer1++ = cplscale_math(*decode_buffer, q->js_vlc_bits, i1); | ||
604 | *mlt_buffer2++ = cplscale_math(*decode_buffer++, q->js_vlc_bits, i2); | ||
605 | } | ||
606 | mlt_buffer1 += (20-SUBBAND_SIZE); | ||
607 | mlt_buffer2 += (20-SUBBAND_SIZE); | ||
608 | decode_buffer += (20-SUBBAND_SIZE); | ||
609 | } | ||
610 | } | ||
611 | |||
612 | /** | ||
613 | * First part of subpacket decoding: | ||
614 | * decode raw stream bytes and read gain info. | ||
615 | * | ||
616 | * @param q pointer to the COOKContext | ||
617 | * @param inbuffer pointer to raw stream data | ||
618 | * @param gain_ptr array of current/prev gain pointers | ||
619 | */ | ||
620 | |||
621 | #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0) | ||
622 | |||
623 | static inline void | ||
624 | decode_bytes_and_gain(COOKContext *q, const uint8_t *inbuffer, | ||
625 | cook_gains *gains_ptr) | ||
626 | { | ||
627 | int offset; | ||
628 | |||
629 | offset = decode_bytes(inbuffer, q->decoded_bytes_buffer, | ||
630 | q->bits_per_subpacket/8); | ||
631 | init_get_bits(&q->gb, q->decoded_bytes_buffer + offset, | ||
632 | q->bits_per_subpacket); | ||
633 | decode_gain_info(&q->gb, gains_ptr->now); | ||
634 | |||
635 | /* Swap current and previous gains */ | ||
636 | FFSWAP(int *, gains_ptr->now, gains_ptr->previous); | ||
637 | } | ||
638 | |||
639 | /** | ||
640 | * Final part of subpacket decoding: | ||
641 | * Apply modulated lapped transform, gain compensation, | ||
642 | * clip and convert to integer. | ||
643 | * | ||
644 | * @param q pointer to the COOKContext | ||
645 | * @param decode_buffer pointer to the mlt coefficients | ||
646 | * @param gain_ptr array of current/prev gain pointers | ||
647 | * @param previous_buffer pointer to the previous buffer to be used for overlapping | ||
648 | * @param out pointer to the output buffer | ||
649 | * @param chan 0: left or single channel, 1: right channel | ||
650 | */ | ||
651 | |||
652 | static void | ||
653 | mlt_compensate_output(COOKContext *q, REAL_T *decode_buffer, | ||
654 | cook_gains *gains, REAL_T *previous_buffer, | ||
655 | int32_t *out, int chan) | ||
656 | { | ||
657 | REAL_T *buffer = q->mono_mdct_output; | ||
658 | int i; | ||
659 | imlt_math(q, decode_buffer); | ||
660 | |||
661 | /* Overlap with the previous block. */ | ||
662 | overlap_math(q, gains->previous[0], previous_buffer); | ||
663 | |||
664 | /* Apply gain profile */ | ||
665 | for (i = 0; i < 8; i++) { | ||
666 | if (gains->now[i] || gains->now[i + 1]) | ||
667 | interpolate_math(q, &buffer[q->samples_per_channel/8 * i], | ||
668 | gains->now[i], gains->now[i + 1]); | ||
669 | } | ||
670 | |||
671 | /* Save away the current to be previous block. */ | ||
672 | memcpy(previous_buffer, buffer+q->samples_per_channel, | ||
673 | sizeof(REAL_T)*q->samples_per_channel); | ||
674 | |||
675 | /* Copy output to non-interleaved sample buffer */ | ||
676 | memcpy(out + (chan * q->samples_per_channel), buffer, | ||
677 | sizeof(REAL_T)*q->samples_per_channel); | ||
678 | } | ||
679 | |||
680 | |||
681 | /** | ||
682 | * Cook subpacket decoding. This function returns one decoded subpacket, | ||
683 | * usually 1024 samples per channel. | ||
684 | * | ||
685 | * @param q pointer to the COOKContext | ||
686 | * @param inbuffer pointer to the inbuffer | ||
687 | * @param sub_packet_size subpacket size | ||
688 | * @param outbuffer pointer to the outbuffer | ||
689 | */ | ||
690 | |||
691 | |||
692 | static int decode_subpacket(COOKContext *q, const uint8_t *inbuffer, | ||
693 | int sub_packet_size, int32_t *outbuffer) { | ||
694 | /* packet dump */ | ||
695 | // for (i=0 ; i<sub_packet_size ; i++) { | ||
696 | // DEBUGF("%02x", inbuffer[i]); | ||
697 | // } | ||
698 | // DEBUGF("\n"); | ||
699 | |||
700 | decode_bytes_and_gain(q, inbuffer, &q->gains1); | ||
701 | |||
702 | if (q->joint_stereo) { | ||
703 | joint_decode(q, q->decode_buffer_1, q->decode_buffer_2); | ||
704 | } else { | ||
705 | mono_decode(q, q->decode_buffer_1); | ||
706 | |||
707 | if (q->nb_channels == 2) { | ||
708 | decode_bytes_and_gain(q, inbuffer + sub_packet_size/2, &q->gains2); | ||
709 | mono_decode(q, q->decode_buffer_2); | ||
710 | } | ||
711 | } | ||
712 | |||
713 | mlt_compensate_output(q, q->decode_buffer_1, &q->gains1, | ||
714 | q->mono_previous_buffer1, outbuffer, 0); | ||
715 | |||
716 | if (q->nb_channels == 2) { | ||
717 | if (q->joint_stereo) { | ||
718 | mlt_compensate_output(q, q->decode_buffer_2, &q->gains1, | ||
719 | q->mono_previous_buffer2, outbuffer, 1); | ||
720 | } else { | ||
721 | mlt_compensate_output(q, q->decode_buffer_2, &q->gains2, | ||
722 | q->mono_previous_buffer2, outbuffer, 1); | ||
723 | } | ||
724 | } | ||
725 | return q->samples_per_frame * sizeof(int32_t); | ||
726 | } | ||
727 | |||
728 | |||
729 | /** | ||
730 | * Cook frame decoding | ||
731 | * | ||
732 | * @param rmctx pointer to the RMContext | ||
733 | */ | ||
734 | |||
735 | int cook_decode_frame(RMContext *rmctx,COOKContext *q, | ||
736 | int32_t *outbuffer, int *data_size, | ||
737 | const uint8_t *inbuffer, int buf_size) { | ||
738 | //COOKContext *q = avctx->priv_data; | ||
739 | //COOKContext *q; | ||
740 | |||
741 | if (buf_size < rmctx->block_align) | ||
742 | return buf_size; | ||
743 | |||
744 | *data_size = decode_subpacket(q, inbuffer, rmctx->block_align, outbuffer); | ||
745 | |||
746 | /* Discard the first two frames: no valid audio. */ | ||
747 | if (rmctx->frame_number < 2) *data_size = 0; | ||
748 | |||
749 | return rmctx->block_align; | ||
750 | } | ||
751 | |||
752 | #ifdef COOKDEBUG | ||
753 | static void dump_cook_context(COOKContext *q) | ||
754 | { | ||
755 | //int i=0; | ||
756 | #define PRINT(a,b) DEBUGF(" %s = %d\n", a, b); | ||
757 | DEBUGF("COOKextradata\n"); | ||
758 | DEBUGF("cookversion=%x\n",q->cookversion); | ||
759 | if (q->cookversion > STEREO) { | ||
760 | PRINT("js_subband_start",q->js_subband_start); | ||
761 | PRINT("js_vlc_bits",q->js_vlc_bits); | ||
762 | } | ||
763 | PRINT("nb_channels",q->nb_channels); | ||
764 | PRINT("bit_rate",q->bit_rate); | ||
765 | PRINT("sample_rate",q->sample_rate); | ||
766 | PRINT("samples_per_channel",q->samples_per_channel); | ||
767 | PRINT("samples_per_frame",q->samples_per_frame); | ||
768 | PRINT("subbands",q->subbands); | ||
769 | PRINT("random_state",q->random_state); | ||
770 | PRINT("js_subband_start",q->js_subband_start); | ||
771 | PRINT("log2_numvector_size",q->log2_numvector_size); | ||
772 | PRINT("numvector_size",q->numvector_size); | ||
773 | PRINT("total_subbands",q->total_subbands); | ||
774 | } | ||
775 | #endif | ||
776 | |||
777 | /** | ||
778 | * Cook initialization | ||
779 | */ | ||
780 | |||
781 | int cook_decode_init(RMContext *rmctx, COOKContext *q) | ||
782 | { | ||
783 | #if defined(CPU_COLDFIRE) | ||
784 | coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_SATURATE); | ||
785 | #endif | ||
786 | /* cook extradata */ | ||
787 | q->cookversion = rm_get_uint32be(rmctx->codec_extradata); | ||
788 | q->samples_per_frame = rm_get_uint16be(&rmctx->codec_extradata[4]); | ||
789 | q->subbands = rm_get_uint16be(&rmctx->codec_extradata[6]); | ||
790 | q->extradata_size = rmctx->extradata_size; | ||
791 | if (q->extradata_size >= 16){ | ||
792 | q->js_subband_start = rm_get_uint16be(&rmctx->codec_extradata[12]); | ||
793 | q->js_vlc_bits = rm_get_uint16be(&rmctx->codec_extradata[14]); | ||
794 | } | ||
795 | |||
796 | /* Take data from the RMContext (RM container). */ | ||
797 | q->sample_rate = rmctx->sample_rate; | ||
798 | q->nb_channels = rmctx->nb_channels; | ||
799 | q->bit_rate = rmctx->bit_rate; | ||
800 | |||
801 | /* Initialize RNG. */ | ||
802 | q->random_state = 0; | ||
803 | |||
804 | /* Initialize extradata related variables. */ | ||
805 | q->samples_per_channel = q->samples_per_frame >> (q->nb_channels-1); | ||
806 | q->bits_per_subpacket = rmctx->block_align * 8; | ||
807 | |||
808 | /* Initialize default data states. */ | ||
809 | q->log2_numvector_size = 5; | ||
810 | q->total_subbands = q->subbands; | ||
811 | |||
812 | /* Initialize version-dependent variables */ | ||
813 | DEBUGF("q->cookversion=%x\n",q->cookversion); | ||
814 | q->joint_stereo = 0; | ||
815 | switch (q->cookversion) { | ||
816 | case MONO: | ||
817 | if (q->nb_channels != 1) { | ||
818 | DEBUGF("Container channels != 1, report sample!\n"); | ||
819 | return -1; | ||
820 | } | ||
821 | DEBUGF("MONO\n"); | ||
822 | break; | ||
823 | case STEREO: | ||
824 | if (q->nb_channels != 1) { | ||
825 | q->bits_per_subpacket = q->bits_per_subpacket/2; | ||
826 | } | ||
827 | DEBUGF("STEREO\n"); | ||
828 | break; | ||
829 | case JOINT_STEREO: | ||
830 | if (q->nb_channels != 2) { | ||
831 | DEBUGF("Container channels != 2, report sample!\n"); | ||
832 | return -1; | ||
833 | } | ||
834 | DEBUGF("JOINT_STEREO\n"); | ||
835 | if (q->extradata_size >= 16){ | ||
836 | q->total_subbands = q->subbands + q->js_subband_start; | ||
837 | q->joint_stereo = 1; | ||
838 | } | ||
839 | if (q->samples_per_channel > 256) { | ||
840 | q->log2_numvector_size = 6; | ||
841 | } | ||
842 | if (q->samples_per_channel > 512) { | ||
843 | q->log2_numvector_size = 7; | ||
844 | } | ||
845 | break; | ||
846 | case MC_COOK: | ||
847 | DEBUGF("MC_COOK not supported!\n"); | ||
848 | return -1; | ||
849 | break; | ||
850 | default: | ||
851 | DEBUGF("Unknown Cook version, report sample!\n"); | ||
852 | return -1; | ||
853 | break; | ||
854 | } | ||
855 | |||
856 | /* Initialize variable relations */ | ||
857 | q->numvector_size = (1 << q->log2_numvector_size); | ||
858 | q->mdct_nbits = av_log2(q->samples_per_channel)+1; | ||
859 | |||
860 | /* Generate tables */ | ||
861 | if (init_cook_vlc_tables(q) != 0) | ||
862 | return -1; | ||
863 | |||
864 | |||
865 | if(rmctx->block_align >= UINT16_MAX/2) | ||
866 | return -1; | ||
867 | |||
868 | q->gains1.now = q->gain_1; | ||
869 | q->gains1.previous = q->gain_2; | ||
870 | q->gains2.now = q->gain_3; | ||
871 | q->gains2.previous = q->gain_4; | ||
872 | |||
873 | |||
874 | /* Initialize COOK signal arithmetic handling */ | ||
875 | /* | ||
876 | if (1) { | ||
877 | q->scalar_dequant = scalar_dequant_math; | ||
878 | q->interpolate = interpolate_math; | ||
879 | } | ||
880 | */ | ||
881 | |||
882 | /* Try to catch some obviously faulty streams, othervise it might be exploitable */ | ||
883 | if (q->total_subbands > 53) { | ||
884 | DEBUGF("total_subbands > 53, report sample!\n"); | ||
885 | return -1; | ||
886 | } | ||
887 | if (q->subbands > 50) { | ||
888 | DEBUGF("subbands > 50, report sample!\n"); | ||
889 | return -1; | ||
890 | } | ||
891 | if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) { | ||
892 | } else { | ||
893 | DEBUGF("unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel); | ||
894 | return -1; | ||
895 | } | ||
896 | if ((q->js_vlc_bits > 6) || (q->js_vlc_bits < 0)) { | ||
897 | DEBUGF("q->js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->js_vlc_bits); | ||
898 | return -1; | ||
899 | } | ||
900 | |||
901 | |||
902 | #ifdef COOKDEBUG | ||
903 | dump_cook_context(q); | ||
904 | #endif | ||
905 | return 0; | ||
906 | } | ||
907 | |||