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