1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2004 G.C. Pascutto, Ahead Software AG, http://www.nero.com
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.18 2004/09/04 14:56:28 menno Exp $
26 **/
27 
28 #include "common.h"
29 #include "structs.h"
30 
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include "specrec.h"
35 #include "huffman.h"
36 
37 /* ISO/IEC 14496-3/Amd.1
38  * 8.5.3.3: Huffman Codeword Reordering for AAC spectral data (HCR)
39  *
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.
48 */
49 
50 #ifdef ERROR_RESILIENCE
51 
52 /* 8.5.3.3.1 Pre-sorting */
53 
54 #define NUM_CB      6
55 #define NUM_CB_ER   22
56 #define MAX_CB      32
57 #define VCB11_FIRST 16
58 #define VCB11_LAST  31
59 
60 static const uint8_t PreSortCB_STD[NUM_CB] =
61     { 11, 9, 7, 5, 3, 1};
62 
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};
65 
66 /* 8.5.3.3.2 Derivation of segment width */
67 
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};
70 
71 #define segmentWidth(cb)    min(maxCwLen[cb], ics->length_of_longest_codeword)
72 
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};
76 
77 typedef struct
78 {
79     uint8_t     cb;
80     uint8_t     decoded;
81     uint16_t	sp_offset;
82     bits_t      bits;
83 } codeword_t;
84 
85 /* rewind and reverse */
86 /* 32 bit version */
rewrev_word(uint32_t v,const uint8_t len)87 static uint32_t rewrev_word(uint32_t v, const uint8_t len)
88 {
89     /* 32 bit reverse */
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]);
95 
96     /* shift off low bits */
97     v >>= (32 - len);
98 
99     return v;
100 }
101 
102 /* 64 bit version */
rewrev_lword(uint32_t * hi,uint32_t * lo,const uint8_t len)103 static void rewrev_lword(uint32_t *hi, uint32_t *lo, const uint8_t len)
104 {
105     if (len <= 32) {
106         *hi = 0;
107         *lo = rewrev_word(*lo, len);
108     } else
109     {
110         uint32_t t = *hi, v = *lo;
111 
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]);
123 
124         /* last 32<>32 bit swap is implicit below */
125 
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);
129     }
130 }
131 
132 
133 /* bits_t version */
rewrev_bits(bits_t * bits)134 static void rewrev_bits(bits_t *bits)
135 {
136     if (bits->len == 0) return;
137     rewrev_lword(&bits->bufb, &bits->bufa,  bits->len);
138 }
139 
140 
141 /* merge bits of a to b */
concat_bits(bits_t * b,bits_t * a)142 static void concat_bits(bits_t *b, bits_t *a)
143 {
144     uint32_t bl, bh, al, ah;
145 
146     if (a->len == 0) return;
147 
148     al = a->bufa;
149     ah = a->bufb;
150 
151     if (b->len > 32)
152     {
153         /* maskoff superfluous high b bits */
154         bl = b->bufa;
155         bh = b->bufb & ((1 << (b->len-32)) - 1);
156         /* left shift a b->len bits */
157         ah = al << (b->len - 32);
158         al = 0;
159     } else {
160         bl = b->bufa & ((1 << (b->len)) - 1);
161         bh = 0;
162         ah = (ah << (b->len)) | (al >> (32 - b->len));
163         al = al << b->len;
164     }
165 
166     /* merge */
167     b->bufa = bl | al;
168     b->bufb = bh | ah;
169 
170     b->len += a->len;
171 }
172 
is_good_cb(uint8_t this_CB,uint8_t this_sec_CB)173 uint8_t is_good_cb(uint8_t this_CB, uint8_t this_sec_CB)
174 {
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))
177     {
178         if (this_CB < ESC_HCB)
179         {
180             /* normal codebook pairs */
181             return ((this_sec_CB == this_CB) || (this_sec_CB == this_CB + 1));
182         } else
183         {
184             /* escape codebook */
185             return (this_sec_CB == this_CB);
186         }
187     }
188     return 0;
189 }
190 
read_segment(bits_t * segment,uint8_t segwidth,bitfile * ld)191 void read_segment(bits_t *segment, uint8_t segwidth, bitfile *ld)
192 {
193     segment->len = segwidth;
194 
195      if (segwidth > 32)
196      {
197         segment->bufb = faad_getbits(ld, segwidth - 32);
198         segment->bufa = faad_getbits(ld, 32);
199 
200     } else {
201         segment->bufa = faad_getbits(ld, segwidth);
202         segment->bufb = 0;
203     }
204 }
205 
fill_in_codeword(codeword_t * codeword,uint16_t index,uint16_t sp,uint8_t cb)206 void fill_in_codeword(codeword_t *codeword, uint16_t index, uint16_t sp, uint8_t cb)
207 {
208     codeword[index].sp_offset = sp;
209     codeword[index].cb = cb;
210     codeword[index].decoded = 0;
211     codeword[index].bits.len = 0;
212 }
213 
reordered_spectral_data(NeAACDecHandle hDecoder,ic_stream * ics,bitfile * ld,int16_t * spectral_data)214 uint8_t reordered_spectral_data(NeAACDecHandle hDecoder, ic_stream *ics,
215                                 bitfile *ld, int16_t *spectral_data)
216 {
217     uint16_t PCWs_done;
218     uint16_t numberOfSegments, numberOfSets, numberOfCodewords;
219 
220     codeword_t codeword[512];
221     bits_t segment[512];
222 
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;
226 
227     const uint16_t nshort = hDecoder->frameLength/8;
228     const uint16_t sp_data_len = ics->length_of_reordered_spectral_data;
229 
230     const uint8_t *PreSortCb;
231 
232     /* no data (e.g. silence) */
233     if (sp_data_len == 0)
234         return 0;
235 
236     /* since there is spectral data, at least one codeword has nonzero length */
237     if (ics->length_of_longest_codeword == 0)
238         return 10;
239 
240     if (sp_data_len < ics->length_of_longest_codeword)
241         return 10;
242 
243     sp_offset[0] = 0;
244     for (g = 1; g < ics->num_window_groups; g++)
245     {
246         sp_offset[g] = sp_offset[g-1] + nshort*ics->window_group_length[g-1];
247     }
248 
249     PCWs_done = 0;
250     numberOfSegments = 0;
251     numberOfCodewords = 0;
252     bitsread = 0;
253 
254     /* VCB11 code books in use */
255     if (hDecoder->aacSectionDataResilienceFlag)
256     {
257         PreSortCb = PreSortCB_ER;
258         last_CB = NUM_CB_ER;
259     } else
260     {
261         PreSortCb = PreSortCB_STD;
262         last_CB = NUM_CB;
263     }
264 
265     /* step 1: decode PCW's (set 0), and stuff data in easier-to-use format */
266     for (sortloop = 0; sortloop < last_CB; sortloop++)
267     {
268         /* select codebook to process this pass */
269         this_CB = PreSortCb[sortloop];
270 
271         /* loop over sfbs */
272         for (sfb = 0; sfb < ics->max_sfb; sfb++)
273         {
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++)
276             {
277                 for(g = 0; g < ics->num_window_groups; g++)
278                 {
279                     for (i = 0; i < ics->num_sec[g]; i++)
280                     {
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))
283                         {
284                             /* check whether codebook used here is the one we want to process */
285                             this_sec_CB = ics->sect_cb[g][i];
286 
287                             if (is_good_cb(this_CB, this_sec_CB))
288                             {
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);
294                                 uint16_t cws;
295 
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++)
298                                 {
299                                     uint16_t sp = sp_offset[g] + ics->sect_sfb_offset[g][sfb] + inc * (cws + w_idx*group_cws_count);
300 
301                                     /* read and decode PCW */
302                                     if (!PCWs_done)
303                                     {
304                                         /* read in normal segments */
305                                         if (bitsread + segwidth <= sp_data_len)
306                                         {
307                                             read_segment(&segment[numberOfSegments], segwidth, ld);
308                                             bitsread += segwidth;
309 
310                                             huffman_spectral_data_2(this_sec_CB, &segment[numberOfSegments], &spectral_data[sp]);
311 
312                                             /* keep leftover bits */
313                                             rewrev_bits(&segment[numberOfSegments]);
314 
315                                             numberOfSegments++;
316                                         } else {
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)
321                                             {
322                                                 const uint8_t additional_bits = sp_data_len - bitsread;
323 
324                                                 read_segment(&segment[numberOfSegments], additional_bits, ld);
325                                                 segment[numberOfSegments].len += segment[numberOfSegments-1].len;
326                                                 rewrev_bits(&segment[numberOfSegments]);
327 
328                                                 if (segment[numberOfSegments-1].len > 32)
329                                                 {
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);
334                                                 } else {
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;
338                                                 }
339                                                 segment[numberOfSegments-1].len += additional_bits;
340                                             }
341                                             bitsread = sp_data_len;
342                                             PCWs_done = 1;
343 
344                                             fill_in_codeword(codeword, 0, sp, this_sec_CB);
345                                         }
346                                     } else {
347                                         fill_in_codeword(codeword, numberOfCodewords - numberOfSegments, sp, this_sec_CB);
348                                     }
349                                     numberOfCodewords++;
350                                 }
351                             }
352                         }
353                     }
354                  }
355              }
356          }
357     }
358 
359     if (numberOfSegments == 0)
360         return 10;
361 
362     numberOfSets = numberOfCodewords / numberOfSegments;
363 
364     /* step 2: decode nonPCWs */
365     for (set = 1; set <= numberOfSets; set++)
366     {
367         uint16_t trial;
368 
369         for (trial = 0; trial < numberOfSegments; trial++)
370         {
371             uint16_t codewordBase;
372 
373             for (codewordBase = 0; codewordBase < numberOfSegments; codewordBase++)
374             {
375                 const uint16_t segment_idx = (trial + codewordBase) % numberOfSegments;
376                 const uint16_t codeword_idx = codewordBase + set*numberOfSegments - numberOfSegments;
377 
378                 /* data up */
379                 if (codeword_idx >= numberOfCodewords - numberOfSegments) break;
380 
381                 if (!codeword[codeword_idx].decoded && segment[segment_idx].len > 0)
382                 {
383                     uint8_t tmplen;
384 
385                     if (codeword[codeword_idx].bits.len != 0)
386                         concat_bits(&segment[segment_idx], &codeword[codeword_idx].bits);
387 
388                     tmplen = segment[segment_idx].len;
389 
390                     if (huffman_spectral_data_2(codeword[codeword_idx].cb, &segment[segment_idx],
391                                                &spectral_data[codeword[codeword_idx].sp_offset]) >= 0)
392                     {
393                         codeword[codeword_idx].decoded = 1;
394                     } else
395                     {
396                         codeword[codeword_idx].bits = segment[segment_idx];
397                         codeword[codeword_idx].bits.len = tmplen;
398                     }
399 
400                 }
401             }
402         }
403         for (i = 0; i < numberOfSegments; i++)
404             rewrev_bits(&segment[i]);
405     }
406 
407     return 0;
408 }
409 #endif
410