Mercurial > mplayer.hg
annotate libfaad2/hcr.c @ 12929:e31bab90b91c
Preliminary Support for building MPlayer with Intel C++ compiler.
Still needs some work to use the proper optimization parameters.
I haven't yet fixed the loader/dmo/dshow code so compile those dirs using
make -C loader CC=gcc [...] for the meantime.
To try use CC=icc ./configure with ICC 8.0 or later.
author | atmos4 |
---|---|
date | Mon, 02 Aug 2004 04:24:36 +0000 |
parents | d81145997036 |
children | 6d50ef45a058 |
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 ** | |
12625
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
25 ** Initially modified for use with MPlayer by Arpad Gereöffy on 2003/08/30 |
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
26 ** $Id: hcr.c,v 1.3 2004/06/02 22:59:03 diego Exp $ |
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
27 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ |
10725 | 28 **/ |
29 | |
30 #include "common.h" | |
31 #include "structs.h" | |
32 | |
33 #include <stdlib.h> | |
34 #include <string.h> | |
35 | |
36 #include "syntax.h" | |
37 #include "specrec.h" | |
38 #include "bits.h" | |
39 #include "pulse.h" | |
40 #include "analysis.h" | |
41 #include "bits.h" | |
10989 | 42 #include "huffman.h" |
10725 | 43 |
44 /* Implements the HCR11 tool as described in ISO/IEC 14496-3/Amd.1, 8.5.3.3 */ | |
45 | |
46 #ifdef ERROR_RESILIENCE | |
47 | |
48 /* rewind len (max. 32) bits so that the MSB becomes LSB */ | |
49 | |
50 static uint32_t rewind_word( uint32_t W, uint8_t len) | |
51 { | |
52 uint8_t i; | |
53 uint32_t tmp_W=0; | |
54 | |
55 for ( i=0; i<len; i++ ) | |
56 { | |
57 tmp_W<<=1; | |
58 if (W & (1<<i)) tmp_W |= 1; | |
59 } | |
60 return tmp_W; | |
61 } | |
62 | |
63 static void rewind_lword( uint32_t *highW, uint32_t *lowW, uint8_t len) | |
64 { | |
65 uint32_t tmp_lW=0; | |
66 | |
67 if (len > 32) | |
68 { | |
69 tmp_lW = rewind_word( (*highW << (64-len)) | (*lowW >> (len-32)), 32); | |
70 *highW = rewind_word( *lowW << (64-len) , 32); | |
71 *lowW = tmp_lW; | |
72 } else { | |
73 *highW = 0; | |
74 *lowW = rewind_word( *lowW, len); | |
75 } | |
76 } | |
77 | |
78 /* Takes a codeword as stored in r, rewinds the remaining bits and stores it back */ | |
79 static void rewind_bits(bits_t * r) | |
80 { | |
81 uint32_t hw, lw; | |
82 | |
83 if (r->len == 0) return; | |
84 | |
85 if (r->len >32) | |
86 { | |
87 lw = r->bufa; | |
88 hw = r->bufb & (0xFFFFFFFF >> (64 - r->len)); | |
89 rewind_lword( &hw, &lw, r->len ); | |
90 r->bufa = lw; | |
91 r->bufb = hw; | |
92 | |
93 } else { | |
10989 | 94 lw = showbits_hcr(r, r->len ); |
10725 | 95 r->bufa = rewind_word( lw, r->len); |
96 r->bufb = 0; | |
97 } | |
98 } | |
99 | |
100 /* takes codewords from a and b, concatenate them and store them in b */ | |
101 static void concat_bits( bits_t * a, bits_t * b) | |
102 { | |
103 uint32_t hwa, lwa, hwb, lwb; | |
104 | |
105 if (a->len == 0) return; | |
106 | |
107 if (a->len >32) | |
108 { | |
109 lwa = a->bufa; | |
110 hwa = a->bufb & (0xFFFFFFFF >> (64 - a->len)); | |
111 } else { | |
10989 | 112 lwa = showbits_hcr(a, a->len ); |
10725 | 113 hwa = 0; |
114 } | |
115 if (b->len >=32) { | |
116 lwb = b->bufa; | |
117 hwb = (b->bufb & (0xFFFFFFFF >> (64 - b->len)) ) | ( lwa << (b->len - 32)); | |
118 } else { | |
10989 | 119 lwb = showbits_hcr(b, b->len ) | (lwa << (b->len)); |
10725 | 120 hwb = (lwa >> (32 - b->len)) | (hwa << (b->len)); |
121 } | |
122 | |
123 b->bufa = lwb; | |
124 b->bufb = hwb; | |
125 b->len += a->len; | |
126 } | |
127 | |
128 /* 8.5.3.3.1 */ | |
129 | |
130 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}; | |
131 static const uint8_t PresortedCodebook[] = { 11, 9, 7, 5, 3, 1}; | |
132 | |
133 static const uint8_t maxCwLen[32] = {0, 11, 9, 20, 16, 13, 11, 14, 12, 17, 14, 49, | |
134 0, 0, 0, 0, 14, 17, 21, 21, 25, 25, 29, 29, 29, 29, 33, 33, 33, 37, 37, 41}; | |
135 | |
136 typedef struct | |
137 { | |
138 bits_t bits; | |
139 uint8_t decoded; | |
140 uint16_t sp_offset; | |
141 uint8_t cb; | |
142 } codeword_state; | |
143 | |
144 | |
145 #define segmentWidth( codebook ) min( maxCwLen[codebook], ics->length_of_longest_codeword ) | |
146 | |
147 uint8_t reordered_spectral_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld, | |
148 int16_t *spectral_data) | |
149 { | |
150 uint16_t sp_offset[8]; | |
151 uint16_t g,i, presort; | |
152 uint16_t NrCodeWords=0, numberOfSegments=0, BitsRead=0; | |
153 uint8_t numberOfSets, set; | |
154 codeword_state Codewords[ 1024 ]; // FIXME max length? PCWs are not stored, so index is Codewordnr - numberOfSegments!, maybe malloc()? | |
155 bits_t Segment[ 512 ]; | |
156 | |
157 uint8_t PCW_decoded=0; | |
158 uint16_t nshort = hDecoder->frameLength/8; | |
159 | |
160 | |
12527 | 161 /*memset (spectral_data, 0, hDecoder->frameLength*sizeof(uint16_t));*/ |
10725 | 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 |