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 **
|
10989
|
25 ** $Id: hcr.c,v 1.1 2003/08/30 22:30:21 arpi Exp $
|
10725
|
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"
|
10989
|
41 #include "huffman.h"
|
10725
|
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 /* rewind len (max. 32) bits so that the MSB becomes LSB */
|
|
48
|
|
49 static uint32_t rewind_word( uint32_t W, uint8_t len)
|
|
50 {
|
|
51 uint8_t i;
|
|
52 uint32_t tmp_W=0;
|
|
53
|
|
54 for ( i=0; i<len; i++ )
|
|
55 {
|
|
56 tmp_W<<=1;
|
|
57 if (W & (1<<i)) tmp_W |= 1;
|
|
58 }
|
|
59 return tmp_W;
|
|
60 }
|
|
61
|
|
62 static void rewind_lword( uint32_t *highW, uint32_t *lowW, uint8_t len)
|
|
63 {
|
|
64 uint32_t tmp_lW=0;
|
|
65
|
|
66 if (len > 32)
|
|
67 {
|
|
68 tmp_lW = rewind_word( (*highW << (64-len)) | (*lowW >> (len-32)), 32);
|
|
69 *highW = rewind_word( *lowW << (64-len) , 32);
|
|
70 *lowW = tmp_lW;
|
|
71 } else {
|
|
72 *highW = 0;
|
|
73 *lowW = rewind_word( *lowW, len);
|
|
74 }
|
|
75 }
|
|
76
|
|
77 /* Takes a codeword as stored in r, rewinds the remaining bits and stores it back */
|
|
78 static void rewind_bits(bits_t * r)
|
|
79 {
|
|
80 uint32_t hw, lw;
|
|
81
|
|
82 if (r->len == 0) return;
|
|
83
|
|
84 if (r->len >32)
|
|
85 {
|
|
86 lw = r->bufa;
|
|
87 hw = r->bufb & (0xFFFFFFFF >> (64 - r->len));
|
|
88 rewind_lword( &hw, &lw, r->len );
|
|
89 r->bufa = lw;
|
|
90 r->bufb = hw;
|
|
91
|
|
92 } else {
|
10989
|
93 lw = showbits_hcr(r, r->len );
|
10725
|
94 r->bufa = rewind_word( lw, r->len);
|
|
95 r->bufb = 0;
|
|
96 }
|
|
97 }
|
|
98
|
|
99 /* takes codewords from a and b, concatenate them and store them in b */
|
|
100 static void concat_bits( bits_t * a, bits_t * b)
|
|
101 {
|
|
102 uint32_t hwa, lwa, hwb, lwb;
|
|
103
|
|
104 if (a->len == 0) return;
|
|
105
|
|
106 if (a->len >32)
|
|
107 {
|
|
108 lwa = a->bufa;
|
|
109 hwa = a->bufb & (0xFFFFFFFF >> (64 - a->len));
|
|
110 } else {
|
10989
|
111 lwa = showbits_hcr(a, a->len );
|
10725
|
112 hwa = 0;
|
|
113 }
|
|
114 if (b->len >=32) {
|
|
115 lwb = b->bufa;
|
|
116 hwb = (b->bufb & (0xFFFFFFFF >> (64 - b->len)) ) | ( lwa << (b->len - 32));
|
|
117 } else {
|
10989
|
118 lwb = showbits_hcr(b, b->len ) | (lwa << (b->len));
|
10725
|
119 hwb = (lwa >> (32 - b->len)) | (hwa << (b->len));
|
|
120 }
|
|
121
|
|
122 b->bufa = lwb;
|
|
123 b->bufb = hwb;
|
|
124 b->len += a->len;
|
|
125 }
|
|
126
|
|
127 /* 8.5.3.3.1 */
|
|
128
|
|
129 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};
|
|
130 static const uint8_t PresortedCodebook[] = { 11, 9, 7, 5, 3, 1};
|
|
131
|
|
132 static const uint8_t maxCwLen[32] = {0, 11, 9, 20, 16, 13, 11, 14, 12, 17, 14, 49,
|
|
133 0, 0, 0, 0, 14, 17, 21, 21, 25, 25, 29, 29, 29, 29, 33, 33, 33, 37, 37, 41};
|
|
134
|
|
135 typedef struct
|
|
136 {
|
|
137 bits_t bits;
|
|
138 uint8_t decoded;
|
|
139 uint16_t sp_offset;
|
|
140 uint8_t cb;
|
|
141 } codeword_state;
|
|
142
|
|
143
|
|
144 #define segmentWidth( codebook ) min( maxCwLen[codebook], ics->length_of_longest_codeword )
|
|
145
|
|
146 uint8_t reordered_spectral_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
|
|
147 int16_t *spectral_data)
|
|
148 {
|
|
149 uint16_t sp_offset[8];
|
|
150 uint16_t g,i, presort;
|
|
151 uint16_t NrCodeWords=0, numberOfSegments=0, BitsRead=0;
|
|
152 uint8_t numberOfSets, set;
|
|
153 codeword_state Codewords[ 1024 ]; // FIXME max length? PCWs are not stored, so index is Codewordnr - numberOfSegments!, maybe malloc()?
|
|
154 bits_t Segment[ 512 ];
|
|
155
|
|
156 uint8_t PCW_decoded=0;
|
|
157 uint16_t segment_index=0, codeword_index=0;
|
|
158 uint16_t nshort = hDecoder->frameLength/8;
|
|
159
|
|
160
|
|
161 memset (spectral_data, 0, hDecoder->frameLength*sizeof(uint16_t));
|
|
162
|
|
163 if (ics->length_of_reordered_spectral_data == 0)
|
|
164 return 0; /* nothing to do */
|
|
165
|
|
166 /* if we have a corrupted bitstream this can happen... */
|
|
167 if ((ics->length_of_longest_codeword == 0) ||
|
|
168 (ics->length_of_reordered_spectral_data <
|
|
169 ics->length_of_longest_codeword) ||
|
|
170 (ics->max_sfb == 0))
|
|
171 {
|
|
172 return 10; /* this is not good... */
|
|
173 }
|
|
174
|
|
175 /* store the offset into the spectral data for all the window groups because we can't do it later */
|
|
176
|
|
177 sp_offset[0] = 0;
|
|
178 for (g=1; g < ics->num_window_groups; g++)
|
|
179 {
|
|
180 sp_offset[g] = sp_offset[g-1] + nshort*ics->window_group_length[g-1];
|
|
181 }
|
|
182
|
|
183 /* All data is sorted according to the codebook used */
|
|
184 for (presort = 0; presort < (hDecoder->aacSectionDataResilienceFlag ? 22 : 6); presort++)
|
|
185 {
|
|
186 uint8_t sfb;
|
|
187
|
|
188 /* next codebook that has to be processed according to presorting */
|
|
189 uint8_t nextCB = hDecoder->aacSectionDataResilienceFlag ? PresortedCodebook_VCB11[ presort ] : PresortedCodebook[ presort ];
|
|
190
|
|
191 /* Data belonging to the same spectral unit and having the same codebook comes in consecutive codewords.
|
|
192 This is done by scanning all sfbs for possible codewords. For sfbs with more than 4 elements this has to be
|
|
193 repeated */
|
|
194
|
|
195 for (sfb=0; sfb<ics->max_sfb; sfb ++)
|
|
196 {
|
|
197 uint8_t sect_cb, w;
|
|
198
|
|
199 for (w=0; w< (ics->swb_offset[sfb+1] - ics->swb_offset[sfb]); w+=4)
|
|
200 {
|
|
201 for(g = 0; g < ics->num_window_groups; g++)
|
|
202 {
|
|
203 for (i = 0; i < ics->num_sec[g]; i++)
|
|
204 {
|
|
205 sect_cb = ics->sect_cb[g][i];
|
|
206
|
|
207 if (
|
|
208 /* process only sections that are due now */
|
|
209 (( sect_cb == nextCB ) || (( nextCB < ESC_HCB ) && ( sect_cb == nextCB+1)) ) &&
|
|
210
|
|
211 /* process only sfb's that are due now */
|
|
212 ((ics->sect_start[g][i] <= sfb) && (ics->sect_end[g][i] > sfb))
|
|
213 )
|
|
214 {
|
|
215 if ((sect_cb != ZERO_HCB) &&
|
|
216 (sect_cb != NOISE_HCB) &&
|
|
217 (sect_cb != INTENSITY_HCB) &&
|
|
218 (sect_cb != INTENSITY_HCB2))
|
|
219 {
|
|
220 uint8_t inc = (sect_cb < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN;
|
|
221 uint16_t k;
|
|
222
|
|
223 uint32_t hw, lw;
|
|
224
|
|
225 for (k=0; (k < (4/inc)*ics->window_group_length[g]) &&
|
|
226 ( (k+w*ics->window_group_length[g]/inc) < (ics->sect_sfb_offset[g][sfb+1] - ics->sect_sfb_offset[g][sfb])); k++)
|
|
227 {
|
|
228 uint16_t sp = sp_offset[g] + ics->sect_sfb_offset[g][sfb] + inc*(k+w*ics->window_group_length[g]/inc);
|
|
229
|
|
230 if (!PCW_decoded)
|
|
231 {
|
|
232 /* if we haven't yet read until the end of the buffer, we can directly decode the so-called PCWs */
|
|
233 if ((BitsRead + segmentWidth( sect_cb ))<= ics->length_of_reordered_spectral_data)
|
|
234 {
|
|
235 Segment[ numberOfSegments ].len = segmentWidth( sect_cb );
|
|
236
|
|
237 if (segmentWidth( sect_cb ) > 32)
|
|
238 {
|
|
239 Segment[ numberOfSegments ].bufb = faad_showbits(ld, segmentWidth( sect_cb ) - 32);
|
|
240 faad_flushbits(ld, segmentWidth( sect_cb) - 32);
|
|
241 Segment[ numberOfSegments ].bufa = faad_showbits(ld, 32),
|
|
242 faad_flushbits(ld, 32 );
|
|
243
|
|
244 } else {
|
|
245 Segment[ numberOfSegments ].bufa = faad_showbits(ld, segmentWidth( sect_cb ));
|
|
246 Segment[ numberOfSegments ].bufb = 0;
|
|
247 faad_flushbits(ld, segmentWidth( sect_cb) );
|
|
248 }
|
|
249
|
|
250 huffman_spectral_data_2(sect_cb, &Segment[ numberOfSegments ], &spectral_data[sp]);
|
|
251
|
|
252 BitsRead += segmentWidth( sect_cb );
|
|
253
|
|
254 /* skip to next segment, but store left bits in new buffer */
|
|
255 rewind_bits( &Segment[ numberOfSegments ]);
|
|
256
|
|
257 numberOfSegments++;
|
|
258 } else {
|
|
259
|
|
260 /* the last segment is extended until length_of_reordered_spectral_data */
|
|
261
|
|
262 if (BitsRead < ics->length_of_reordered_spectral_data)
|
|
263 {
|
|
264
|
|
265 uint8_t additional_bits = (ics->length_of_reordered_spectral_data - BitsRead);
|
|
266
|
|
267 if ( additional_bits > 32)
|
|
268 {
|
|
269 hw = faad_showbits(ld, additional_bits - 32);
|
|
270 faad_flushbits(ld, additional_bits - 32);
|
|
271 lw = faad_showbits(ld, 32);
|
|
272 faad_flushbits(ld, 32 );
|
|
273 } else {
|
|
274 lw = faad_showbits(ld, additional_bits);
|
|
275 hw = 0;
|
|
276 faad_flushbits(ld, additional_bits );
|
|
277 }
|
|
278 rewind_lword( &hw, &lw, additional_bits + Segment[ numberOfSegments-1 ].len );
|
|
279 if (Segment[ numberOfSegments-1 ].len > 32)
|
|
280 {
|
|
281 Segment[ numberOfSegments-1 ].bufb = hw +
|
10989
|
282 showbits_hcr(&Segment[ numberOfSegments-1 ], Segment[ numberOfSegments-1 ].len - 32);
|
10725
|
283 Segment[ numberOfSegments-1 ].bufa = lw +
|
10989
|
284 showbits_hcr(&Segment[ numberOfSegments-1 ], 32);
|
10725
|
285 } else {
|
|
286 Segment[ numberOfSegments-1 ].bufa = lw +
|
10989
|
287 showbits_hcr(&Segment[ numberOfSegments-1 ], Segment[ numberOfSegments-1 ].len);
|
10725
|
288 Segment[ numberOfSegments-1 ].bufb = hw;
|
|
289 }
|
|
290 Segment[ numberOfSegments-1 ].len += additional_bits;
|
|
291 }
|
|
292 BitsRead = ics->length_of_reordered_spectral_data;
|
|
293 PCW_decoded = 1;
|
|
294
|
|
295 Codewords[ 0 ].sp_offset = sp;
|
|
296 Codewords[ 0 ].cb = sect_cb;
|
|
297 Codewords[ 0 ].decoded = 0;
|
|
298 Codewords[ 0 ].bits.len = 0;
|
|
299 }
|
|
300 } else {
|
|
301 Codewords[ NrCodeWords - numberOfSegments ].sp_offset = sp;
|
|
302 Codewords[ NrCodeWords - numberOfSegments ].cb = sect_cb;
|
|
303 Codewords[ NrCodeWords - numberOfSegments ].decoded = 0;
|
|
304 Codewords[ NrCodeWords - numberOfSegments ].bits.len = 0;
|
|
305
|
|
306 } /* PCW decoded */
|
|
307 NrCodeWords++;
|
|
308 } /* of k */
|
|
309 }
|
|
310 }
|
|
311 } /* of i */
|
|
312 } /* of g */
|
|
313 } /* of w */
|
|
314 } /* of sfb */
|
|
315 } /* of presort */
|
|
316
|
|
317 /* Avoid divide by zero */
|
|
318 if (numberOfSegments == 0)
|
|
319 return 10; /* this is not good... */
|
|
320
|
|
321 numberOfSets = NrCodeWords / numberOfSegments;
|
|
322
|
|
323 /* second step: decode nonPCWs */
|
|
324
|
|
325 for (set = 1; set <= numberOfSets; set++)
|
|
326 {
|
|
327 uint16_t trial;
|
|
328
|
|
329 for (trial = 0; trial < numberOfSegments; trial++)
|
|
330 {
|
|
331 uint16_t codewordBase;
|
|
332 uint16_t set_decoded=numberOfSegments;
|
|
333
|
|
334 if (set == numberOfSets)
|
|
335 set_decoded = NrCodeWords - set*numberOfSegments; /* last set is shorter than the rest */
|
|
336
|
|
337 for (codewordBase = 0; codewordBase < numberOfSegments; codewordBase++)
|
|
338 {
|
|
339 uint16_t segment_index = (trial + codewordBase) % numberOfSegments;
|
|
340 uint16_t codeword_index = codewordBase + set*numberOfSegments - numberOfSegments;
|
|
341
|
|
342 if ((codeword_index + numberOfSegments) >= NrCodeWords)
|
|
343 break;
|
|
344 if (!Codewords[ codeword_index ].decoded)
|
|
345 {
|
|
346 if ( Segment[ segment_index ].len > 0)
|
|
347 {
|
|
348 uint8_t tmplen;
|
|
349
|
|
350 if (Codewords[ codeword_index ].bits.len != 0)
|
|
351 {
|
|
352 /* on the first trial the data is only stored in Segment[], not in Codewords[].
|
|
353 On next trials first collect the data stored for this codeword and
|
|
354 concatenate the new data from Segment[] */
|
|
355
|
|
356 concat_bits( &Codewords[ codeword_index ].bits, &Segment[ segment_index ]);
|
|
357 /* Now everthing is stored in Segment[] */
|
|
358 }
|
|
359 tmplen = Segment[ segment_index ].len;
|
|
360 if ( huffman_spectral_data_2(Codewords[ codeword_index ].cb, &Segment[ segment_index ],
|
|
361 &spectral_data[ Codewords[ codeword_index ].sp_offset ]) >=0)
|
|
362 {
|
|
363 /* CW did fit into segment */
|
|
364
|
|
365 Codewords[ codeword_index ].decoded = 1;
|
|
366 set_decoded--;
|
|
367 } else {
|
|
368
|
|
369 /* CW did not fit, so store for later use */
|
|
370
|
|
371 Codewords[ codeword_index ].bits.len = tmplen;
|
|
372 Codewords[ codeword_index ].bits.bufa = Segment[ segment_index ].bufa;
|
|
373 Codewords[ codeword_index ].bits.bufb = Segment[ segment_index ].bufb;
|
|
374 }
|
|
375 }
|
|
376 }
|
|
377 } /* of codewordBase */
|
|
378
|
|
379 if (set_decoded == 0) break; /* no undecoded codewords left in this set */
|
|
380
|
|
381 } /* of trial */
|
|
382
|
|
383 /* rewind all bits in remaining segments with len>0 */
|
|
384 for (i=0; i < numberOfSegments; i++)
|
|
385 rewind_bits( &Segment[ i ] );
|
|
386 }
|
|
387
|
|
388 #if 0
|
|
389 {
|
|
390 int i, r=0, c=0;
|
|
391 for (i=0; i< numberOfSegments; i++)
|
|
392 r += Segment[ i ].len;
|
|
393 if (r != 0)
|
|
394 {
|
|
395 printf("reordered_spectral_data: %d bits remaining!\n", r);
|
|
396 }
|
|
397 for (i=0; i< NrCodeWords - numberOfSegments; i++)
|
|
398 {
|
|
399 if (Codewords[ i ].decoded == 0)
|
|
400 {
|
|
401 c++;
|
|
402 }
|
|
403 }
|
|
404 if (c != 0)
|
|
405 {
|
|
406 printf("reordered_spectral_data: %d Undecoded Codewords remaining!\n",c );
|
|
407 }
|
|
408 if ((r !=0) || (c!=0)) return 10;
|
|
409 }
|
|
410 #endif
|
|
411
|
|
412 return 0;
|
|
413 }
|
|
414 #endif
|