2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2004 G.C. Pascutto, Ahead Software AG, http://www.nero.com
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.
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.
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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id: hcr.c,v 1.18 2004/09/04 14:56:28 menno Exp $
37 /* ISO/IEC 14496-3/Amd.1
38 * 8.5.3.3: Huffman Codeword Reordering for AAC spectral data (HCR)
40 * HCR devides the spectral data in known fixed size segments, and
41 * sorts it by the importance of the data. The importance is firstly
42 * the (lower) position in the spectrum, and secondly the largest
43 * value in the used codebook.
44 * The most important data is written at the start of each segment
45 * (at known positions), the remaining data is interleaved inbetween,
46 * with the writing direction alternating.
47 * Data length is not increased.
50 #ifdef ERROR_RESILIENCE
52 /* 8.5.3.3.1 Pre-sorting */
57 #define VCB11_FIRST 16
60 static const uint8_t PreSortCB_STD
[NUM_CB
] =
63 static const uint8_t PreSortCB_ER
[NUM_CB_ER
] =
64 { 11, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 9, 7, 5, 3, 1};
66 /* 8.5.3.3.2 Derivation of segment width */
68 static const uint8_t maxCwLen
[MAX_CB
] = {0, 11, 9, 20, 16, 13, 11, 14, 12, 17, 14, 49,
69 0, 0, 0, 0, 14, 17, 21, 21, 25, 25, 29, 29, 29, 29, 33, 33, 33, 37, 37, 41};
71 #define segmentWidth(cb) min(maxCwLen[cb], ics->length_of_longest_codeword)
73 /* bit-twiddling helpers */
74 static const uint8_t S
[] = {1, 2, 4, 8, 16};
75 static const uint32_t B
[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};
85 /* rewind and reverse */
87 static uint32_t rewrev_word(uint32_t v
, const uint8_t len
)
90 v
= ((v
>> S
[0]) & B
[0]) | ((v
<< S
[0]) & ~B
[0]);
91 v
= ((v
>> S
[1]) & B
[1]) | ((v
<< S
[1]) & ~B
[1]);
92 v
= ((v
>> S
[2]) & B
[2]) | ((v
<< S
[2]) & ~B
[2]);
93 v
= ((v
>> S
[3]) & B
[3]) | ((v
<< S
[3]) & ~B
[3]);
94 v
= ((v
>> S
[4]) & B
[4]) | ((v
<< S
[4]) & ~B
[4]);
96 /* shift off low bits */
103 static void rewrev_lword(uint32_t *hi
, uint32_t *lo
, const uint8_t len
)
107 *lo
= rewrev_word(*lo
, len
);
110 uint32_t t
= *hi
, v
= *lo
;
112 /* double 32 bit reverse */
113 v
= ((v
>> S
[0]) & B
[0]) | ((v
<< S
[0]) & ~B
[0]);
114 t
= ((t
>> S
[0]) & B
[0]) | ((t
<< S
[0]) & ~B
[0]);
115 v
= ((v
>> S
[1]) & B
[1]) | ((v
<< S
[1]) & ~B
[1]);
116 t
= ((t
>> S
[1]) & B
[1]) | ((t
<< S
[1]) & ~B
[1]);
117 v
= ((v
>> S
[2]) & B
[2]) | ((v
<< S
[2]) & ~B
[2]);
118 t
= ((t
>> S
[2]) & B
[2]) | ((t
<< S
[2]) & ~B
[2]);
119 v
= ((v
>> S
[3]) & B
[3]) | ((v
<< S
[3]) & ~B
[3]);
120 t
= ((t
>> S
[3]) & B
[3]) | ((t
<< S
[3]) & ~B
[3]);
121 v
= ((v
>> S
[4]) & B
[4]) | ((v
<< S
[4]) & ~B
[4]);
122 t
= ((t
>> S
[4]) & B
[4]) | ((t
<< S
[4]) & ~B
[4]);
124 /* last 32<>32 bit swap is implicit below */
126 /* shift off low bits (this is really only one 64 bit shift) */
127 *lo
= (t
>> (64 - len
)) | (v
<< (len
- 32));
128 *hi
= v
>> (64 - len
);
134 static void rewrev_bits(bits_t
*bits
)
136 if (bits
->len
== 0) return;
137 rewrev_lword(&bits
->bufb
, &bits
->bufa
, bits
->len
);
141 /* merge bits of a to b */
142 static void concat_bits(bits_t
*b
, bits_t
*a
)
144 uint32_t bl
, bh
, al
, ah
;
146 if (a
->len
== 0) return;
153 /* maskoff superfluous high b bits */
155 bh
= b
->bufb
& ((1 << (b
->len
-32)) - 1);
156 /* left shift a b->len bits */
157 ah
= al
<< (b
->len
- 32);
160 bl
= b
->bufa
& ((1 << (b
->len
)) - 1);
162 ah
= (ah
<< (b
->len
)) | (al
>> (32 - b
->len
));
173 uint8_t is_good_cb(uint8_t this_CB
, uint8_t this_sec_CB
)
175 /* only want spectral data CB's */
176 if ((this_sec_CB
> ZERO_HCB
&& this_sec_CB
<= ESC_HCB
) || (this_sec_CB
>= VCB11_FIRST
&& this_sec_CB
<= VCB11_LAST
))
178 if (this_CB
< ESC_HCB
)
180 /* normal codebook pairs */
181 return ((this_sec_CB
== this_CB
) || (this_sec_CB
== this_CB
+ 1));
184 /* escape codebook */
185 return (this_sec_CB
== this_CB
);
191 void read_segment(bits_t
*segment
, uint8_t segwidth
, bitfile
*ld
)
193 segment
->len
= segwidth
;
197 segment
->bufb
= faad_getbits(ld
, segwidth
- 32);
198 segment
->bufa
= faad_getbits(ld
, 32);
201 segment
->bufa
= faad_getbits(ld
, segwidth
);
206 void fill_in_codeword(codeword_t
*codeword
, uint16_t index
, uint16_t sp
, uint8_t cb
)
208 codeword
[index
].sp_offset
= sp
;
209 codeword
[index
].cb
= cb
;
210 codeword
[index
].decoded
= 0;
211 codeword
[index
].bits
.len
= 0;
214 uint8_t reordered_spectral_data(NeAACDecHandle hDecoder
, ic_stream
*ics
,
215 bitfile
*ld
, int16_t *spectral_data
)
218 uint16_t numberOfSegments
, numberOfSets
, numberOfCodewords
;
220 codeword_t codeword
[512];
223 uint16_t sp_offset
[8];
224 uint16_t g
, i
, sortloop
, set
, bitsread
;
225 uint8_t w_idx
, sfb
, this_CB
, last_CB
, this_sec_CB
;
227 const uint16_t nshort
= hDecoder
->frameLength
/8;
228 const uint16_t sp_data_len
= ics
->length_of_reordered_spectral_data
;
230 const uint8_t *PreSortCb
;
232 /* no data (e.g. silence) */
233 if (sp_data_len
== 0)
236 /* since there is spectral data, at least one codeword has nonzero length */
237 if (ics
->length_of_longest_codeword
== 0)
240 if (sp_data_len
< ics
->length_of_longest_codeword
)
244 for (g
= 1; g
< ics
->num_window_groups
; g
++)
246 sp_offset
[g
] = sp_offset
[g
-1] + nshort
*ics
->window_group_length
[g
-1];
250 numberOfSegments
= 0;
251 numberOfCodewords
= 0;
254 /* VCB11 code books in use */
255 if (hDecoder
->aacSectionDataResilienceFlag
)
257 PreSortCb
= PreSortCB_ER
;
261 PreSortCb
= PreSortCB_STD
;
265 /* step 1: decode PCW's (set 0), and stuff data in easier-to-use format */
266 for (sortloop
= 0; sortloop
< last_CB
; sortloop
++)
268 /* select codebook to process this pass */
269 this_CB
= PreSortCb
[sortloop
];
272 for (sfb
= 0; sfb
< ics
->max_sfb
; sfb
++)
274 /* loop over all in this sfb, 4 lines per loop */
275 for (w_idx
= 0; 4*w_idx
< (ics
->swb_offset
[sfb
+1] - ics
->swb_offset
[sfb
]); w_idx
++)
277 for(g
= 0; g
< ics
->num_window_groups
; g
++)
279 for (i
= 0; i
< ics
->num_sec
[g
]; i
++)
281 /* check whether sfb used here is the one we want to process */
282 if ((ics
->sect_start
[g
][i
] <= sfb
) && (ics
->sect_end
[g
][i
] > sfb
))
284 /* check whether codebook used here is the one we want to process */
285 this_sec_CB
= ics
->sect_cb
[g
][i
];
287 if (is_good_cb(this_CB
, this_sec_CB
))
289 /* precalculate some stuff */
290 uint16_t sect_sfb_size
= ics
->sect_sfb_offset
[g
][sfb
+1] - ics
->sect_sfb_offset
[g
][sfb
];
291 uint8_t inc
= (this_sec_CB
< FIRST_PAIR_HCB
) ? QUAD_LEN
: PAIR_LEN
;
292 uint16_t group_cws_count
= (4*ics
->window_group_length
[g
])/inc
;
293 uint8_t segwidth
= segmentWidth(this_sec_CB
);
296 /* read codewords until end of sfb or end of window group (shouldn't only 1 trigger?) */
297 for (cws
= 0; (cws
< group_cws_count
) && ((cws
+ w_idx
*group_cws_count
) < sect_sfb_size
); cws
++)
299 uint16_t sp
= sp_offset
[g
] + ics
->sect_sfb_offset
[g
][sfb
] + inc
* (cws
+ w_idx
*group_cws_count
);
301 /* read and decode PCW */
304 /* read in normal segments */
305 if (bitsread
+ segwidth
<= sp_data_len
)
307 read_segment(&segment
[numberOfSegments
], segwidth
, ld
);
308 bitsread
+= segwidth
;
310 huffman_spectral_data_2(this_sec_CB
, &segment
[numberOfSegments
], &spectral_data
[sp
]);
312 /* keep leftover bits */
313 rewrev_bits(&segment
[numberOfSegments
]);
317 /* remaining stuff after last segment, we unfortunately couldn't read
318 this in earlier because it might not fit in 64 bits. since we already
319 decoded (and removed) the PCW it is now guaranteed to fit */
320 if (bitsread
< sp_data_len
)
322 const uint8_t additional_bits
= sp_data_len
- bitsread
;
324 read_segment(&segment
[numberOfSegments
], additional_bits
, ld
);
325 segment
[numberOfSegments
].len
+= segment
[numberOfSegments
-1].len
;
326 rewrev_bits(&segment
[numberOfSegments
]);
328 if (segment
[numberOfSegments
-1].len
> 32)
330 segment
[numberOfSegments
-1].bufb
= segment
[numberOfSegments
].bufb
+
331 showbits_hcr(&segment
[numberOfSegments
-1], segment
[numberOfSegments
-1].len
- 32);
332 segment
[numberOfSegments
-1].bufa
= segment
[numberOfSegments
].bufa
+
333 showbits_hcr(&segment
[numberOfSegments
-1], 32);
335 segment
[numberOfSegments
-1].bufa
= segment
[numberOfSegments
].bufa
+
336 showbits_hcr(&segment
[numberOfSegments
-1], segment
[numberOfSegments
-1].len
);
337 segment
[numberOfSegments
-1].bufb
= segment
[numberOfSegments
].bufb
;
339 segment
[numberOfSegments
-1].len
+= additional_bits
;
341 bitsread
= sp_data_len
;
344 fill_in_codeword(codeword
, 0, sp
, this_sec_CB
);
347 fill_in_codeword(codeword
, numberOfCodewords
- numberOfSegments
, sp
, this_sec_CB
);
359 if (numberOfSegments
== 0)
362 numberOfSets
= numberOfCodewords
/ numberOfSegments
;
364 /* step 2: decode nonPCWs */
365 for (set
= 1; set
<= numberOfSets
; set
++)
369 for (trial
= 0; trial
< numberOfSegments
; trial
++)
371 uint16_t codewordBase
;
373 for (codewordBase
= 0; codewordBase
< numberOfSegments
; codewordBase
++)
375 const uint16_t segment_idx
= (trial
+ codewordBase
) % numberOfSegments
;
376 const uint16_t codeword_idx
= codewordBase
+ set
*numberOfSegments
- numberOfSegments
;
379 if (codeword_idx
>= numberOfCodewords
- numberOfSegments
) break;
381 if (!codeword
[codeword_idx
].decoded
&& segment
[segment_idx
].len
> 0)
385 if (codeword
[codeword_idx
].bits
.len
!= 0)
386 concat_bits(&segment
[segment_idx
], &codeword
[codeword_idx
].bits
);
388 tmplen
= segment
[segment_idx
].len
;
390 if (huffman_spectral_data_2(codeword
[codeword_idx
].cb
, &segment
[segment_idx
],
391 &spectral_data
[codeword
[codeword_idx
].sp_offset
]) >= 0)
393 codeword
[codeword_idx
].decoded
= 1;
396 codeword
[codeword_idx
].bits
= segment
[segment_idx
];
397 codeword
[codeword_idx
].bits
.len
= tmplen
;
403 for (i
= 0; i
< numberOfSegments
; i
++)
404 rewrev_bits(&segment
[i
]);