10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
|
3 ** Copyright (C) 2002 A. Kurpiers
|
|
4 **
|
|
5 ** This program is free software; you can redistribute it and/or modify
|
|
6 ** it under the terms of the GNU General Public License as published by
|
|
7 ** the Free Software Foundation; either version 2 of the License, or
|
|
8 ** (at your option) any later version.
|
|
9 **
|
|
10 ** This program is distributed in the hope that it will be useful,
|
|
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 ** GNU General Public License for more details.
|
|
14 **
|
|
15 ** You should have received a copy of the GNU General Public License
|
|
16 ** along with this program; if not, write to the Free Software
|
|
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 **
|
|
19 ** Any non-GPL usage of this software or parts of this software is strictly
|
|
20 ** forbidden.
|
|
21 **
|
|
22 ** Commercial non-GPL licensing of this software is possible.
|
|
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
24 **
|
|
25 ** $Id: hcr.c,v 1.5 2003/07/29 08:20:12 menno Exp $
|
|
26 **/
|
|
27
|
|
28
|
|
29 #include "common.h"
|
|
30 #include "structs.h"
|
|
31
|
|
32 #include <stdlib.h>
|
|
33 #include <string.h>
|
|
34
|
|
35 #include "syntax.h"
|
|
36 #include "specrec.h"
|
|
37 #include "bits.h"
|
|
38 #include "pulse.h"
|
|
39 #include "analysis.h"
|
|
40 #include "bits.h"
|
|
41 #include "codebook/hcb.h"
|
|
42
|
|
43 /* Implements the HCR11 tool as described in ISO/IEC 14496-3/Amd.1, 8.5.3.3 */
|
|
44
|
|
45 #ifdef ERROR_RESILIENCE
|
|
46
|
|
47 typedef struct
|
|
48 {
|
|
49 /* bit input */
|
|
50 uint32_t bufa;
|
|
51 uint32_t bufb;
|
|
52 int8_t len;
|
|
53 } bits_t;
|
|
54
|
|
55
|
|
56 static INLINE uint32_t showbits(bits_t *ld, uint8_t bits)
|
|
57 {
|
|
58 if (bits == 0) return 0;
|
|
59 if (ld->len <= 32){
|
|
60 /* huffman_spectral_data_2 needs to read more than may be available, bits maybe
|
|
61 > ld->len, deliver 0 than */
|
|
62 if (ld->len >= bits)
|
|
63 return ((ld->bufa >> (ld->len - bits)) & (0xFFFFFFFF >> (32 - bits)));
|
|
64 else
|
|
65 return ((ld->bufa << (bits - ld->len)) & (0xFFFFFFFF >> (32 - bits)));
|
|
66 } else {
|
|
67 if ((ld->len - bits) < 32)
|
|
68 {
|
|
69 return ( (ld->bufb & (0xFFFFFFFF >> (64 - ld->len))) << (bits - ld->len + 32)) |
|
|
70 (ld->bufa >> (ld->len - bits));
|
|
71 } else {
|
|
72 return ((ld->bufb >> (ld->len - bits - 32)) & (0xFFFFFFFF >> (32 - bits)));
|
|
73 }
|
|
74 }
|
|
75 }
|
|
76
|
|
77 /* return 1 if position is outside of buffer, 0 otherwise */
|
|
78 static INLINE int8_t flushbits( bits_t *ld, uint8_t bits)
|
|
79 {
|
|
80 ld->len -= bits;
|
|
81
|
|
82 if (ld->len <0)
|
|
83 {
|
|
84 ld->len = 0;
|
|
85 return 1;
|
|
86 } else {
|
|
87 return 0;
|
|
88 }
|
|
89 }
|
|
90
|
|
91
|
|
92 static INLINE int8_t getbits(bits_t *ld, uint8_t n, uint32_t *result)
|
|
93 {
|
|
94 *result = showbits(ld, n);
|
|
95 return flushbits(ld, n);
|
|
96 }
|
|
97
|
|
98 static INLINE int8_t get1bit(bits_t *ld, uint8_t *result)
|
|
99 {
|
|
100 uint32_t res;
|
|
101 int8_t ret;
|
|
102
|
|
103 ret = getbits(ld, 1, &res);
|
|
104 *result = (int8_t)(res & 1);
|
|
105 return ret;
|
|
106 }
|
|
107
|
|
108 /* Special version of huffman_spectral_data adapted from huffman.h
|
|
109 Will not read from a bitfile but a bits_t structure.
|
|
110 Will keep track of the bits decoded and return the number of bits remaining.
|
|
111 Do not read more than ld->len, return -1 if codeword would be longer */
|
|
112
|
|
113 static int8_t huffman_spectral_data_2(uint8_t cb, bits_t *ld, int16_t *sp )
|
|
114 {
|
|
115 uint32_t cw;
|
|
116 uint16_t offset = 0;
|
|
117 uint8_t extra_bits;
|
|
118 uint8_t i;
|
|
119 uint8_t save_cb = cb;
|
|
120
|
|
121
|
|
122 switch (cb)
|
|
123 {
|
|
124 case 1: /* 2-step method for data quadruples */
|
|
125 case 2:
|
|
126 case 4:
|
|
127
|
|
128 cw = showbits(ld, hcbN[cb]);
|
|
129 offset = hcb_table[cb][cw].offset;
|
|
130 extra_bits = hcb_table[cb][cw].extra_bits;
|
|
131
|
|
132 if (extra_bits)
|
|
133 {
|
|
134 /* we know for sure it's more than hcbN[cb] bits long */
|
|
135 if ( flushbits(ld, hcbN[cb]) ) return -1;
|
|
136 offset += (uint16_t)showbits(ld, extra_bits);
|
|
137 if ( flushbits(ld, hcb_2_quad_table[cb][offset].bits - hcbN[cb]) ) return -1;
|
|
138 } else {
|
|
139 if ( flushbits(ld, hcb_2_quad_table[cb][offset].bits) ) return -1;
|
|
140 }
|
|
141
|
|
142 sp[0] = hcb_2_quad_table[cb][offset].x;
|
|
143 sp[1] = hcb_2_quad_table[cb][offset].y;
|
|
144 sp[2] = hcb_2_quad_table[cb][offset].v;
|
|
145 sp[3] = hcb_2_quad_table[cb][offset].w;
|
|
146 break;
|
|
147
|
|
148 case 6: /* 2-step method for data pairs */
|
|
149 case 8:
|
|
150 case 10:
|
|
151 case 11:
|
|
152 /* VCB11 uses codebook 11 */
|
|
153 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
|
|
154 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
|
|
155
|
|
156 /* TODO: If ER is used, some extra error checking should be done */
|
|
157 if (cb >= 16)
|
|
158 cb = 11;
|
|
159
|
|
160 cw = showbits(ld, hcbN[cb]);
|
|
161 offset = hcb_table[cb][cw].offset;
|
|
162 extra_bits = hcb_table[cb][cw].extra_bits;
|
|
163
|
|
164 if (extra_bits)
|
|
165 {
|
|
166 /* we know for sure it's more than hcbN[cb] bits long */
|
|
167 if ( flushbits(ld, hcbN[cb]) ) return -1;
|
|
168 offset += (uint16_t)showbits(ld, extra_bits);
|
|
169 if ( flushbits(ld, hcb_2_pair_table[cb][offset].bits - hcbN[cb]) ) return -1;
|
|
170 } else {
|
|
171 if ( flushbits(ld, hcb_2_pair_table[cb][offset].bits) ) return -1;
|
|
172 }
|
|
173 sp[0] = hcb_2_pair_table[cb][offset].x;
|
|
174 sp[1] = hcb_2_pair_table[cb][offset].y;
|
|
175 break;
|
|
176
|
|
177 case 3: /* binary search for data quadruples */
|
|
178
|
|
179 while (!hcb3[offset].is_leaf)
|
|
180 {
|
|
181 uint8_t b;
|
|
182
|
|
183 if ( get1bit(ld, &b) ) return -1;
|
|
184 offset += hcb3[offset].data[b];
|
|
185 }
|
|
186
|
|
187 sp[0] = hcb3[offset].data[0];
|
|
188 sp[1] = hcb3[offset].data[1];
|
|
189 sp[2] = hcb3[offset].data[2];
|
|
190 sp[3] = hcb3[offset].data[3];
|
|
191
|
|
192 break;
|
|
193
|
|
194 case 5: /* binary search for data pairs */
|
|
195 case 7:
|
|
196 case 9:
|
|
197
|
|
198 while (!hcb_bin_table[cb][offset].is_leaf)
|
|
199 {
|
|
200 uint8_t b;
|
|
201
|
|
202 if (get1bit(ld, &b) ) return -1;
|
|
203 offset += hcb_bin_table[cb][offset].data[b];
|
|
204 }
|
|
205
|
|
206 sp[0] = hcb_bin_table[cb][offset].data[0];
|
|
207 sp[1] = hcb_bin_table[cb][offset].data[1];
|
|
208
|
|
209 break;
|
|
210 }
|
|
211
|
|
212 /* decode sign bits */
|
|
213 if (unsigned_cb[cb]) {
|
|
214
|
|
215 for(i = 0; i < ((cb < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN); i++)
|
|
216 {
|
|
217 if(sp[i])
|
|
218 {
|
|
219 uint8_t b;
|
|
220 if ( get1bit(ld, &b) ) return -1;
|
|
221 if (b != 0) {
|
|
222 sp[i] = -sp[i];
|
|
223 }
|
|
224 }
|
|
225 }
|
|
226 }
|
|
227
|
|
228 /* decode huffman escape bits */
|
|
229 if ((cb == ESC_HCB) || (cb >= 16))
|
|
230 {
|
|
231 uint8_t k;
|
|
232 for (k = 0; k < 2; k++)
|
|
233 {
|
|
234 if ((sp[k] == 16) || (sp[k] == -16))
|
|
235 {
|
|
236 uint8_t neg, i;
|
|
237 int32_t j;
|
|
238 uint32_t off;
|
|
239
|
|
240 neg = (sp[k] < 0) ? 1 : 0;
|
|
241
|
|
242 for (i = 4; ; i++)
|
|
243 {
|
|
244 uint8_t b;
|
|
245 if (get1bit(ld, &b))
|
|
246 return -1;
|
|
247 if (b == 0)
|
|
248 break;
|
|
249 }
|
|
250 // TODO: here we would need to test "off" if VCB11 is used!
|
|
251 if (getbits(ld, i, &off))
|
|
252 return -1;
|
|
253 j = off + (1<<i);
|
|
254 sp[k] = (int16_t)((neg) ? -j : j);
|
|
255 }
|
|
256 }
|
|
257 }
|
|
258 return ld->len;
|
|
259 }
|
|
260
|
|
261 /* rewind len (max. 32) bits so that the MSB becomes LSB */
|
|
262
|
|
263 static uint32_t rewind_word( uint32_t W, uint8_t len)
|
|
264 {
|
|
265 uint8_t i;
|
|
266 uint32_t tmp_W=0;
|
|
267
|
|
268 for ( i=0; i<len; i++ )
|
|
269 {
|
|
270 tmp_W<<=1;
|
|
271 if (W & (1<<i)) tmp_W |= 1;
|
|
272 }
|
|
273 return tmp_W;
|
|
274 }
|
|
275
|
|
276 static void rewind_lword( uint32_t *highW, uint32_t *lowW, uint8_t len)
|
|
277 {
|
|
278 uint32_t tmp_lW=0;
|
|
279
|
|
280 if (len > 32)
|
|
281 {
|
|
282 tmp_lW = rewind_word( (*highW << (64-len)) | (*lowW >> (len-32)), 32);
|
|
283 *highW = rewind_word( *lowW << (64-len) , 32);
|
|
284 *lowW = tmp_lW;
|
|
285 } else {
|
|
286 *highW = 0;
|
|
287 *lowW = rewind_word( *lowW, len);
|
|
288 }
|
|
289 }
|
|
290
|
|
291 /* Takes a codeword as stored in r, rewinds the remaining bits and stores it back */
|
|
292 static void rewind_bits(bits_t * r)
|
|
293 {
|
|
294 uint32_t hw, lw;
|
|
295
|
|
296 if (r->len == 0) return;
|
|
297
|
|
298 if (r->len >32)
|
|
299 {
|
|
300 lw = r->bufa;
|
|
301 hw = r->bufb & (0xFFFFFFFF >> (64 - r->len));
|
|
302 rewind_lword( &hw, &lw, r->len );
|
|
303 r->bufa = lw;
|
|
304 r->bufb = hw;
|
|
305
|
|
306 } else {
|
|
307 lw = showbits(r, r->len );
|
|
308 r->bufa = rewind_word( lw, r->len);
|
|
309 r->bufb = 0;
|
|
310 }
|
|
311 }
|
|
312
|
|
313 /* takes codewords from a and b, concatenate them and store them in b */
|
|
314 static void concat_bits( bits_t * a, bits_t * b)
|
|
315 {
|
|
316 uint32_t hwa, lwa, hwb, lwb;
|
|
317
|
|
318 if (a->len == 0) return;
|
|
319
|
|
320 if (a->len >32)
|
|
321 {
|
|
322 lwa = a->bufa;
|
|
323 hwa = a->bufb & (0xFFFFFFFF >> (64 - a->len));
|
|
324 } else {
|
|
325 lwa = showbits(a, a->len );
|
|
326 hwa = 0;
|
|
327 }
|
|
328 if (b->len >=32) {
|
|
329 lwb = b->bufa;
|
|
330 hwb = (b->bufb & (0xFFFFFFFF >> (64 - b->len)) ) | ( lwa << (b->len - 32));
|
|
331 } else {
|
|
332 lwb = showbits(b, b->len ) | (lwa << (b->len));
|
|
333 hwb = (lwa >> (32 - b->len)) | (hwa << (b->len));
|
|
334 }
|
|
335
|
|
336 b->bufa = lwb;
|
|
337 b->bufb = hwb;
|
|
338 b->len += a->len;
|
|
339 }
|
|
340
|
|
341 /* 8.5.3.3.1 */
|
|
342
|
|
343 static const uint8_t PresortedCodebook_VCB11[] = { 11, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 9, 7, 5, 3, 1};
|
|
344 static const uint8_t PresortedCodebook[] = { 11, 9, 7, 5, 3, 1};
|
|
345
|
|
346 static const uint8_t maxCwLen[32] = {0, 11, 9, 20, 16, 13, 11, 14, 12, 17, 14, 49,
|
|
347 0, 0, 0, 0, 14, 17, 21, 21, 25, 25, 29, 29, 29, 29, 33, 33, 33, 37, 37, 41};
|
|
348
|
|
349 typedef struct
|
|
350 {
|
|
351 bits_t bits;
|
|
352 uint8_t decoded;
|
|
353 uint16_t sp_offset;
|
|
354 uint8_t cb;
|
|
355 } codeword_state;
|
|
356
|
|
357
|
|
358 #define segmentWidth( codebook ) min( maxCwLen[codebook], ics->length_of_longest_codeword )
|
|
359
|
|
360 uint8_t reordered_spectral_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
|
|
361 int16_t *spectral_data)
|
|
362 {
|
|
363 uint16_t sp_offset[8];
|
|
364 uint16_t g,i, presort;
|
|
365 uint16_t NrCodeWords=0, numberOfSegments=0, BitsRead=0;
|
|
366 uint8_t numberOfSets, set;
|
|
367 codeword_state Codewords[ 1024 ]; // FIXME max length? PCWs are not stored, so index is Codewordnr - numberOfSegments!, maybe malloc()?
|
|
368 bits_t Segment[ 512 ];
|
|
369
|
|
370 uint8_t PCW_decoded=0;
|
|
371 uint16_t segment_index=0, codeword_index=0;
|
|
372 uint16_t nshort = hDecoder->frameLength/8;
|
|
373
|
|
374
|
|
375 memset (spectral_data, 0, hDecoder->frameLength*sizeof(uint16_t));
|
|
376
|
|
377 if (ics->length_of_reordered_spectral_data == 0)
|
|
378 return 0; /* nothing to do */
|
|
379
|
|
380 /* if we have a corrupted bitstream this can happen... */
|
|
381 if ((ics->length_of_longest_codeword == 0) ||
|
|
382 (ics->length_of_reordered_spectral_data <
|
|
383 ics->length_of_longest_codeword) ||
|
|
384 (ics->max_sfb == 0))
|
|
385 {
|
|
386 return 10; /* this is not good... */
|
|
387 }
|
|
388
|
|
389 /* store the offset into the spectral data for all the window groups because we can't do it later */
|
|
390
|
|
391 sp_offset[0] = 0;
|
|
392 for (g=1; g < ics->num_window_groups; g++)
|
|
393 {
|
|
394 sp_offset[g] = sp_offset[g-1] + nshort*ics->window_group_length[g-1];
|
|
395 }
|
|
396
|
|
397 /* All data is sorted according to the codebook used */
|
|
398 for (presort = 0; presort < (hDecoder->aacSectionDataResilienceFlag ? 22 : 6); presort++)
|
|
399 {
|
|
400 uint8_t sfb;
|
|
401
|
|
402 /* next codebook that has to be processed according to presorting */
|
|
403 uint8_t nextCB = hDecoder->aacSectionDataResilienceFlag ? PresortedCodebook_VCB11[ presort ] : PresortedCodebook[ presort ];
|
|
404
|
|
405 /* Data belonging to the same spectral unit and having the same codebook comes in consecutive codewords.
|
|
406 This is done by scanning all sfbs for possible codewords. For sfbs with more than 4 elements this has to be
|
|
407 repeated */
|
|
408
|
|
409 for (sfb=0; sfb<ics->max_sfb; sfb ++)
|
|
410 {
|
|
411 uint8_t sect_cb, w;
|
|
412
|
|
413 for (w=0; w< (ics->swb_offset[sfb+1] - ics->swb_offset[sfb]); w+=4)
|
|
414 {
|
|
415 for(g = 0; g < ics->num_window_groups; g++)
|
|
416 {
|
|
417 for (i = 0; i < ics->num_sec[g]; i++)
|
|
418 {
|
|
419 sect_cb = ics->sect_cb[g][i];
|
|
420
|
|
421 if (
|
|
422 /* process only sections that are due now */
|
|
423 (( sect_cb == nextCB ) || (( nextCB < ESC_HCB ) && ( sect_cb == nextCB+1)) ) &&
|
|
424
|
|
425 /* process only sfb's that are due now */
|
|
426 ((ics->sect_start[g][i] <= sfb) && (ics->sect_end[g][i] > sfb))
|
|
427 )
|
|
428 {
|
|
429 if ((sect_cb != ZERO_HCB) &&
|
|
430 (sect_cb != NOISE_HCB) &&
|
|
431 (sect_cb != INTENSITY_HCB) &&
|
|
432 (sect_cb != INTENSITY_HCB2))
|
|
433 {
|
|
434 uint8_t inc = (sect_cb < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN;
|
|
435 uint16_t k;
|
|
436
|
|
437 uint32_t hw, lw;
|
|
438
|
|
439 for (k=0; (k < (4/inc)*ics->window_group_length[g]) &&
|
|
440 ( (k+w*ics->window_group_length[g]/inc) < (ics->sect_sfb_offset[g][sfb+1] - ics->sect_sfb_offset[g][sfb])); k++)
|
|
441 {
|
|
442 uint16_t sp = sp_offset[g] + ics->sect_sfb_offset[g][sfb] + inc*(k+w*ics->window_group_length[g]/inc);
|
|
443
|
|
444 if (!PCW_decoded)
|
|
445 {
|
|
446 /* if we haven't yet read until the end of the buffer, we can directly decode the so-called PCWs */
|
|
447 if ((BitsRead + segmentWidth( sect_cb ))<= ics->length_of_reordered_spectral_data)
|
|
448 {
|
|
449 Segment[ numberOfSegments ].len = segmentWidth( sect_cb );
|
|
450
|
|
451 if (segmentWidth( sect_cb ) > 32)
|
|
452 {
|
|
453 Segment[ numberOfSegments ].bufb = faad_showbits(ld, segmentWidth( sect_cb ) - 32);
|
|
454 faad_flushbits(ld, segmentWidth( sect_cb) - 32);
|
|
455 Segment[ numberOfSegments ].bufa = faad_showbits(ld, 32),
|
|
456 faad_flushbits(ld, 32 );
|
|
457
|
|
458 } else {
|
|
459 Segment[ numberOfSegments ].bufa = faad_showbits(ld, segmentWidth( sect_cb ));
|
|
460 Segment[ numberOfSegments ].bufb = 0;
|
|
461 faad_flushbits(ld, segmentWidth( sect_cb) );
|
|
462 }
|
|
463
|
|
464 huffman_spectral_data_2(sect_cb, &Segment[ numberOfSegments ], &spectral_data[sp]);
|
|
465
|
|
466 BitsRead += segmentWidth( sect_cb );
|
|
467
|
|
468 /* skip to next segment, but store left bits in new buffer */
|
|
469 rewind_bits( &Segment[ numberOfSegments ]);
|
|
470
|
|
471 numberOfSegments++;
|
|
472 } else {
|
|
473
|
|
474 /* the last segment is extended until length_of_reordered_spectral_data */
|
|
475
|
|
476 if (BitsRead < ics->length_of_reordered_spectral_data)
|
|
477 {
|
|
478
|
|
479 uint8_t additional_bits = (ics->length_of_reordered_spectral_data - BitsRead);
|
|
480
|
|
481 if ( additional_bits > 32)
|
|
482 {
|
|
483 hw = faad_showbits(ld, additional_bits - 32);
|
|
484 faad_flushbits(ld, additional_bits - 32);
|
|
485 lw = faad_showbits(ld, 32);
|
|
486 faad_flushbits(ld, 32 );
|
|
487 } else {
|
|
488 lw = faad_showbits(ld, additional_bits);
|
|
489 hw = 0;
|
|
490 faad_flushbits(ld, additional_bits );
|
|
491 }
|
|
492 rewind_lword( &hw, &lw, additional_bits + Segment[ numberOfSegments-1 ].len );
|
|
493 if (Segment[ numberOfSegments-1 ].len > 32)
|
|
494 {
|
|
495 Segment[ numberOfSegments-1 ].bufb = hw +
|
|
496 showbits(&Segment[ numberOfSegments-1 ], Segment[ numberOfSegments-1 ].len - 32);
|
|
497 Segment[ numberOfSegments-1 ].bufa = lw +
|
|
498 showbits(&Segment[ numberOfSegments-1 ], 32);
|
|
499 } else {
|
|
500 Segment[ numberOfSegments-1 ].bufa = lw +
|
|
501 showbits(&Segment[ numberOfSegments-1 ], Segment[ numberOfSegments-1 ].len);
|
|
502 Segment[ numberOfSegments-1 ].bufb = hw;
|
|
503 }
|
|
504 Segment[ numberOfSegments-1 ].len += additional_bits;
|
|
505 }
|
|
506 BitsRead = ics->length_of_reordered_spectral_data;
|
|
507 PCW_decoded = 1;
|
|
508
|
|
509 Codewords[ 0 ].sp_offset = sp;
|
|
510 Codewords[ 0 ].cb = sect_cb;
|
|
511 Codewords[ 0 ].decoded = 0;
|
|
512 Codewords[ 0 ].bits.len = 0;
|
|
513 }
|
|
514 } else {
|
|
515 Codewords[ NrCodeWords - numberOfSegments ].sp_offset = sp;
|
|
516 Codewords[ NrCodeWords - numberOfSegments ].cb = sect_cb;
|
|
517 Codewords[ NrCodeWords - numberOfSegments ].decoded = 0;
|
|
518 Codewords[ NrCodeWords - numberOfSegments ].bits.len = 0;
|
|
519
|
|
520 } /* PCW decoded */
|
|
521 NrCodeWords++;
|
|
522 } /* of k */
|
|
523 }
|
|
524 }
|
|
525 } /* of i */
|
|
526 } /* of g */
|
|
527 } /* of w */
|
|
528 } /* of sfb */
|
|
529 } /* of presort */
|
|
530
|
|
531 /* Avoid divide by zero */
|
|
532 if (numberOfSegments == 0)
|
|
533 return 10; /* this is not good... */
|
|
534
|
|
535 numberOfSets = NrCodeWords / numberOfSegments;
|
|
536
|
|
537 /* second step: decode nonPCWs */
|
|
538
|
|
539 for (set = 1; set <= numberOfSets; set++)
|
|
540 {
|
|
541 uint16_t trial;
|
|
542
|
|
543 for (trial = 0; trial < numberOfSegments; trial++)
|
|
544 {
|
|
545 uint16_t codewordBase;
|
|
546 uint16_t set_decoded=numberOfSegments;
|
|
547
|
|
548 if (set == numberOfSets)
|
|
549 set_decoded = NrCodeWords - set*numberOfSegments; /* last set is shorter than the rest */
|
|
550
|
|
551 for (codewordBase = 0; codewordBase < numberOfSegments; codewordBase++)
|
|
552 {
|
|
553 uint16_t segment_index = (trial + codewordBase) % numberOfSegments;
|
|
554 uint16_t codeword_index = codewordBase + set*numberOfSegments - numberOfSegments;
|
|
555
|
|
556 if ((codeword_index + numberOfSegments) >= NrCodeWords)
|
|
557 break;
|
|
558 if (!Codewords[ codeword_index ].decoded)
|
|
559 {
|
|
560 if ( Segment[ segment_index ].len > 0)
|
|
561 {
|
|
562 uint8_t tmplen;
|
|
563
|
|
564 if (Codewords[ codeword_index ].bits.len != 0)
|
|
565 {
|
|
566 /* on the first trial the data is only stored in Segment[], not in Codewords[].
|
|
567 On next trials first collect the data stored for this codeword and
|
|
568 concatenate the new data from Segment[] */
|
|
569
|
|
570 concat_bits( &Codewords[ codeword_index ].bits, &Segment[ segment_index ]);
|
|
571 /* Now everthing is stored in Segment[] */
|
|
572 }
|
|
573 tmplen = Segment[ segment_index ].len;
|
|
574 if ( huffman_spectral_data_2(Codewords[ codeword_index ].cb, &Segment[ segment_index ],
|
|
575 &spectral_data[ Codewords[ codeword_index ].sp_offset ]) >=0)
|
|
576 {
|
|
577 /* CW did fit into segment */
|
|
578
|
|
579 Codewords[ codeword_index ].decoded = 1;
|
|
580 set_decoded--;
|
|
581 } else {
|
|
582
|
|
583 /* CW did not fit, so store for later use */
|
|
584
|
|
585 Codewords[ codeword_index ].bits.len = tmplen;
|
|
586 Codewords[ codeword_index ].bits.bufa = Segment[ segment_index ].bufa;
|
|
587 Codewords[ codeword_index ].bits.bufb = Segment[ segment_index ].bufb;
|
|
588 }
|
|
589 }
|
|
590 }
|
|
591 } /* of codewordBase */
|
|
592
|
|
593 if (set_decoded == 0) break; /* no undecoded codewords left in this set */
|
|
594
|
|
595 } /* of trial */
|
|
596
|
|
597 /* rewind all bits in remaining segments with len>0 */
|
|
598 for (i=0; i < numberOfSegments; i++)
|
|
599 rewind_bits( &Segment[ i ] );
|
|
600 }
|
|
601
|
|
602 #if 0
|
|
603 {
|
|
604 int i, r=0, c=0;
|
|
605 for (i=0; i< numberOfSegments; i++)
|
|
606 r += Segment[ i ].len;
|
|
607 if (r != 0)
|
|
608 {
|
|
609 printf("reordered_spectral_data: %d bits remaining!\n", r);
|
|
610 }
|
|
611 for (i=0; i< NrCodeWords - numberOfSegments; i++)
|
|
612 {
|
|
613 if (Codewords[ i ].decoded == 0)
|
|
614 {
|
|
615 c++;
|
|
616 }
|
|
617 }
|
|
618 if (c != 0)
|
|
619 {
|
|
620 printf("reordered_spectral_data: %d Undecoded Codewords remaining!\n",c );
|
|
621 }
|
|
622 if ((r !=0) || (c!=0)) return 10;
|
|
623 }
|
|
624 #endif
|
|
625
|
|
626 return 0;
|
|
627 }
|
|
628 #endif
|