- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# hcr.c

    00001 /*
    00002 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
    00003 ** Copyright (C) 2003-2005 M. Bakker, Ahead Software AG, http://www.nero.com
    00004 **
    00005 ** This program is free software; you can redistribute it and/or modify
    00006 ** it under the terms of the GNU General Public License as published by
    00007 ** the Free Software Foundation; either version 2 of the License, or
    00008 ** (at your option) any later version.
    00009 **
    00010 ** This program is distributed in the hope that it will be useful,
    00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00013 ** GNU General Public License for more details.
    00014 **
    00015 ** You should have received a copy of the GNU General Public License
    00016 ** along with this program; if not, write to the Free Software
    00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00018 **
    00019 ** Any non-GPL usage of this software or parts of this software is strictly
    00020 ** forbidden.
    00021 **
    00022 ** Software using this code must display the following message visibly in the
    00023 ** software:
    00024 ** "FAAD2 AAC/HE-AAC/HE-AACv2/DRM decoder (c) Ahead Software, www.nero.com"
    00025 ** in, for example, the about-box or help/startup screen.
    00026 **
    00027 ** Commercial non-GPL licensing of this software is possible.
    00028 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
    00029 **
    00030 ** $Id: hcr.c,v 1.2 2005/11/01 21:41:43 gabest Exp $
    00031 **/
    00032
    00033 #include "common.h"
    00034 #include "structs.h"
    00035
    00036 #include <stdlib.h>
    00037 #include <string.h>
    00038
    00039 #include "specrec.h"
    00040 #include "huffman.h"
    00041
    00042 /* ISO/IEC 14496-3/Amd.1
    00043  * 8.5.3.3: Huffman Codeword Reordering for AAC spectral data (HCR)
    00044  *
    00045  * HCR devides the spectral data in known fixed size segments, and
    00046  * sorts it by the importance of the data. The importance is firstly
    00047  * the (lower) position in the spectrum, and secondly the largest
    00048  * value in the used codebook.
    00049  * The most important data is written at the start of each segment
    00050  * (at known positions), the remaining data is interleaved inbetween,
    00051  * with the writing direction alternating.
    00052  * Data length is not increased.
    00053 */
    00054
    00055 #ifdef ERROR_RESILIENCE
    00056
    00057 /* 8.5.3.3.1 Pre-sorting */
    00058
    00059 #define NUM_CB      6
    00060 #define NUM_CB_ER   22
    00061 #define MAX_CB      32
    00062 #define VCB11_FIRST 16
    00063 #define VCB11_LAST  31
    00064
    00065 static const uint8_t PreSortCB_STD[NUM_CB] =
    00066     { 11, 9, 7, 5, 3, 1};
    00067
    00068 static const uint8_t PreSortCB_ER[NUM_CB_ER] =
    00069     { 11, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 9, 7, 5, 3, 1};
    00070
    00071 /* 8.5.3.3.2 Derivation of segment width */
    00072
    00073 static const uint8_t maxCwLen[MAX_CB] = {0, 11, 9, 20, 16, 13, 11, 14, 12, 17, 14, 49,
    00074     0, 0, 0, 0, 14, 17, 21, 21, 25, 25, 29, 29, 29, 29, 33, 33, 33, 37, 37, 41};
    00075
    00076 #define segmentWidth(cb)    min(maxCwLen[cb], ics->length_of_longest_codeword)
    00077
    00078 /* bit-twiddling helpers */
    00079 static const uint8_t  S[] = {1, 2, 4, 8, 16};
    00080 static const uint32_t B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};
    00081
    00082 typedef struct
    00083 {
    00084     uint8_t     cb;
    00085     uint8_t     decoded;
    00086     uint16_t    sp_offset;
    00087     bits_t      bits;
    00088 } codeword_t;
    00089
    00090 /* rewind and reverse */
    00091 /* 32 bit version */
    00092 static uint32_t rewrev_word(uint32_t v, const uint8_t len)
    00093 {
    00094     /* 32 bit reverse */
    00095     v = ((v >> S[0]) & B[0]) | ((v << S[0]) & ~B[0]);
    00096     v = ((v >> S[1]) & B[1]) | ((v << S[1]) & ~B[1]);
    00097     v = ((v >> S[2]) & B[2]) | ((v << S[2]) & ~B[2]);
    00098     v = ((v >> S[3]) & B[3]) | ((v << S[3]) & ~B[3]);
    00099     v = ((v >> S[4]) & B[4]) | ((v << S[4]) & ~B[4]);
    00100
    00101     /* shift off low bits */
    00102     v >>= (32 - len);
    00103
    00104     return v;
    00105 }
    00106
    00107 /* 64 bit version */
    00108 static void rewrev_lword(uint32_t *hi, uint32_t *lo, const uint8_t len)
    00109 {
    00110     if (len <= 32) {
    00111         *hi = 0;
    00112         *lo = rewrev_word(*lo, len);
    00113     } else
    00114     {
    00115         uint32_t t = *hi, v = *lo;
    00116
    00117         /* double 32 bit reverse */
    00118         v = ((v >> S[0]) & B[0]) | ((v << S[0]) & ~B[0]);
    00119         t = ((t >> S[0]) & B[0]) | ((t << S[0]) & ~B[0]);
    00120         v = ((v >> S[1]) & B[1]) | ((v << S[1]) & ~B[1]);
    00121         t = ((t >> S[1]) & B[1]) | ((t << S[1]) & ~B[1]);
    00122         v = ((v >> S[2]) & B[2]) | ((v << S[2]) & ~B[2]);
    00123         t = ((t >> S[2]) & B[2]) | ((t << S[2]) & ~B[2]);
    00124         v = ((v >> S[3]) & B[3]) | ((v << S[3]) & ~B[3]);
    00125         t = ((t >> S[3]) & B[3]) | ((t << S[3]) & ~B[3]);
    00126         v = ((v >> S[4]) & B[4]) | ((v << S[4]) & ~B[4]);
    00127         t = ((t >> S[4]) & B[4]) | ((t << S[4]) & ~B[4]);
    00128
    00129         /* last 32<>32 bit swap is implicit below */
    00130
    00131         /* shift off low bits (this is really only one 64 bit shift) */
    00132         *lo = (t >> (64 - len)) | (v << (len - 32));
    00133         *hi = v >> (64 - len);
    00134     }
    00135 }
    00136
    00137
    00138 /* bits_t version */
    00139 static void rewrev_bits(bits_t *bits)
    00140 {
    00141     if (bits->len == 0) return;
    00142     rewrev_lword(&bits->bufb, &bits->bufa,  bits->len);
    00143 }
    00144
    00145
    00146 /* merge bits of a to b */
    00147 static void concat_bits(bits_t *b, bits_t *a)
    00148 {
    00149     uint32_t bl, bh, al, ah;
    00150
    00151     if (a->len == 0) return;
    00152
    00153     al = a->bufa;
    00154     ah = a->bufb;
    00155
    00156     if (b->len > 32)
    00157     {
    00158         /* maskoff superfluous high b bits */
    00159         bl = b->bufa;
    00160         bh = b->bufb & ((1 << (b->len-32)) - 1);
    00161         /* left shift a b->len bits */
    00162         ah = al << (b->len - 32);
    00163         al = 0;
    00164     } else {
    00165         bl = b->bufa & ((1 << (b->len)) - 1);
    00166         bh = 0;
    00167         ah = (ah << (b->len)) | (al >> (32 - b->len));
    00168         al = al << b->len;
    00169     }
    00170
    00171     /* merge */
    00172     b->bufa = bl | al;
    00173     b->bufb = bh | ah;
    00174
    00175     b->len += a->len;
    00176 }
    00177
    00178 uint8_t is_good_cb(uint8_t this_CB, uint8_t this_sec_CB)
    00179 {
    00180     /* only want spectral data CB's */
    00181     if ((this_sec_CB > ZERO_HCB && this_sec_CB <= ESC_HCB) || (this_sec_CB >= VCB11_FIRST && this_sec_CB <= VCB11_LAST))
    00182     {
    00183         if (this_CB < ESC_HCB)
    00184         {
    00185             /* normal codebook pairs */
    00186             return ((this_sec_CB == this_CB) || (this_sec_CB == this_CB + 1));
    00187         } else
    00188         {
    00189             /* escape codebook */
    00190             return (this_sec_CB == this_CB);
    00191         }
    00192     }
    00193     return 0;
    00194 }
    00195
    00196 void read_segment(bits_t *segment, uint8_t segwidth, bitfile *ld)
    00197 {
    00198     segment->len = segwidth;
    00199
    00200      if (segwidth > 32)
    00201      {
    00202         segment->bufb = faad_getbits(ld, segwidth - 32);
    00203         segment->bufa = faad_getbits(ld, 32);
    00204
    00205     } else {
    00206         segment->bufa = faad_getbits(ld, segwidth);
    00207         segment->bufb = 0;
    00208     }
    00209 }
    00210
    00211 void fill_in_codeword(codeword_t *codeword, uint16_t index, uint16_t sp, uint8_t cb)
    00212 {
    00213     codeword[index].sp_offset = sp;
    00214     codeword[index].cb = cb;
    00215     codeword[index].decoded = 0;
    00216     codeword[index].bits.len = 0;
    00217 }
    00218
    00219 uint8_t reordered_spectral_data(NeAACDecHandle hDecoder, ic_stream *ics,
    00220                                 bitfile *ld, int16_t *spectral_data)
    00221 {
    00222     uint16_t PCWs_done;
    00223     uint16_t numberOfSegments, numberOfSets, numberOfCodewords;
    00224
    00225     codeword_t codeword[512];
    00226     bits_t segment[512];
    00227
    00228     uint16_t sp_offset[8];
    00229     uint16_t g, i, sortloop, set, bitsread;
    00230     uint16_t bitsleft, codewordsleft;
    00231     uint8_t w_idx, sfb, this_CB, last_CB, this_sec_CB;
    00232
    00233     const uint16_t nshort = hDecoder->frameLength/8;
    00234     const uint16_t sp_data_len = ics->length_of_reordered_spectral_data;
    00235
    00236     const uint8_t *PreSortCb;
    00237
    00238     /* no data (e.g. silence) */
    00239     if (sp_data_len == 0)
    00240         return 0;
    00241
    00242     /* since there is spectral data, at least one codeword has nonzero length */
    00243     if (ics->length_of_longest_codeword == 0)
    00244         return 10;
    00245
    00246     if (sp_data_len < ics->length_of_longest_codeword)
    00247         return 10;
    00248
    00249     sp_offset[0] = 0;
    00250     for (g = 1; g < ics->num_window_groups; g++)
    00251     {
    00252         sp_offset[g] = sp_offset[g-1] + nshort*ics->window_group_length[g-1];
    00253     }
    00254
    00255     PCWs_done = 0;
    00256     numberOfSegments = 0;
    00257     numberOfCodewords = 0;
    00258     bitsread = 0;
    00259
    00260     /* VCB11 code books in use */
    00261     if (hDecoder->aacSectionDataResilienceFlag)
    00262     {
    00263         PreSortCb = PreSortCB_ER;
    00264         last_CB = NUM_CB_ER;
    00265     } else
    00266     {
    00267         PreSortCb = PreSortCB_STD;
    00268         last_CB = NUM_CB;
    00269     }
    00270
    00271     /* step 1: decode PCW's (set 0), and stuff data in easier-to-use format */
    00272     for (sortloop = 0; sortloop < last_CB; sortloop++)
    00273     {
    00274         /* select codebook to process this pass */
    00275         this_CB = PreSortCb[sortloop];
    00276
    00277         /* loop over sfbs */
    00278         for (sfb = 0; sfb < ics->max_sfb; sfb++)
    00279         {
    00280             /* loop over all in this sfb, 4 lines per loop */
    00281             for (w_idx = 0; 4*w_idx < (ics->swb_offset[sfb+1] - ics->swb_offset[sfb]); w_idx++)
    00282             {
    00283                 for(g = 0; g < ics->num_window_groups; g++)
    00284                 {
    00285                     for (i = 0; i < ics->num_sec[g]; i++)
    00286                     {
    00287                         /* check whether sfb used here is the one we want to process */
    00288                         if ((ics->sect_start[g][i] <= sfb) && (ics->sect_end[g][i] > sfb))
    00289                         {
    00290                             /* check whether codebook used here is the one we want to process */
    00291                             this_sec_CB = ics->sect_cb[g][i];
    00292
    00293                             if (is_good_cb(this_CB, this_sec_CB))
    00294                             {
    00295                                 /* precalculate some stuff */
    00296                                 uint16_t sect_sfb_size = ics->sect_sfb_offset[g][sfb+1] - ics->sect_sfb_offset[g][sfb];
    00297                                 uint8_t inc = (this_sec_CB < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN;
    00298                                 uint16_t group_cws_count = (4*ics->window_group_length[g])/inc;
    00299                                 uint8_t segwidth = segmentWidth(this_sec_CB);
    00300                                 uint16_t cws;
    00301
    00302                                 /* read codewords until end of sfb or end of window group (shouldn't only 1 trigger?) */
    00303                                 for (cws = 0; (cws < group_cws_count) && ((cws + w_idx*group_cws_count) < sect_sfb_size); cws++)
    00304                                 {
    00305                                     uint16_t sp = sp_offset[g] + ics->sect_sfb_offset[g][sfb] + inc * (cws + w_idx*group_cws_count);
    00306
    00307                                     /* read and decode PCW */
    00308                                     if (!PCWs_done)
    00309                                     {
    00310                                         /* read in normal segments */
    00311                                         if (bitsread + segwidth <= sp_data_len)
    00312                                         {
    00313                                             read_segment(&segment[numberOfSegments], segwidth, ld);
    00314                                             bitsread += segwidth;
    00315
    00316                                             huffman_spectral_data_2(this_sec_CB, &segment[numberOfSegments], &spectral_data[sp]);
    00317
    00318                                             /* keep leftover bits */
    00319                                             rewrev_bits(&segment[numberOfSegments]);
    00320
    00321                                             numberOfSegments++;
    00322                                         } else {
    00323                                             /* remaining stuff after last segment, we unfortunately couldn't read
    00324                                                this in earlier because it might not fit in 64 bits. since we already
    00325                                                decoded (and removed) the PCW it is now guaranteed to fit */
    00326                                             if (bitsread < sp_data_len)
    00327                                             {
    00328                                                 const uint8_t additional_bits = sp_data_len - bitsread;
    00329
    00330                                                 read_segment(&segment[numberOfSegments], additional_bits, ld);
    00331                                                 segment[numberOfSegments].len += segment[numberOfSegments-1].len;
    00332                                                 rewrev_bits(&segment[numberOfSegments]);
    00333
    00334                                                 if (segment[numberOfSegments-1].len > 32)
    00335                                                 {
    00336                                                     segment[numberOfSegments-1].bufb = segment[numberOfSegments].bufb +
    00337                                                         showbits_hcr(&segment[numberOfSegments-1], segment[numberOfSegments-1].len - 32);
    00338                                                     segment[numberOfSegments-1].bufa = segment[numberOfSegments].bufa +
    00339                                                         showbits_hcr(&segment[numberOfSegments-1], 32);
    00340                                                 } else {
    00341                                                     segment[numberOfSegments-1].bufa = segment[numberOfSegments].bufa +
    00342                                                         showbits_hcr(&segment[numberOfSegments-1], segment[numberOfSegments-1].len);
    00343                                                     segment[numberOfSegments-1].bufb = segment[numberOfSegments].bufb;
    00344                                                 }
    00345                                                 segment[numberOfSegments-1].len += additional_bits;
    00346                                             }
    00347                                             bitsread = sp_data_len;
    00348                                             PCWs_done = 1;
    00349
    00350                                             fill_in_codeword(codeword, 0, sp, this_sec_CB);
    00351                                         }
    00352                                     } else {
    00353                                         fill_in_codeword(codeword, numberOfCodewords - numberOfSegments, sp, this_sec_CB);
    00354                                     }
    00355                                     numberOfCodewords++;
    00356                                 }
    00357                             }
    00358                         }
    00359                     }
    00360                  }
    00361              }
    00362          }
    00363     }
    00364
    00365     if (numberOfSegments == 0)
    00366         return 10;
    00367
    00368     numberOfSets = numberOfCodewords / numberOfSegments;
    00369
    00370     /* step 2: decode nonPCWs */
    00371     for (set = 1; set <= numberOfSets; set++)
    00372     {
    00373         uint16_t trial;
    00374
    00375         for (trial = 0; trial < numberOfSegments; trial++)
    00376         {
    00377             uint16_t codewordBase;
    00378
    00379             for (codewordBase = 0; codewordBase < numberOfSegments; codewordBase++)
    00380             {
    00381                 const uint16_t segment_idx = (trial + codewordBase) % numberOfSegments;
    00382                 const uint16_t codeword_idx = codewordBase + set*numberOfSegments - numberOfSegments;
    00383
    00384                 /* data up */
    00385                 if (codeword_idx >= numberOfCodewords - numberOfSegments) break;
    00386
    00387                 if (!codeword[codeword_idx].decoded && segment[segment_idx].len > 0)
    00388                 {
    00389                     uint8_t tmplen;
    00390
    00391                     if (codeword[codeword_idx].bits.len != 0)
    00392                         concat_bits(&segment[segment_idx], &codeword[codeword_idx].bits);
    00393
    00394                     tmplen = segment[segment_idx].len;
    00395
    00396                     if (huffman_spectral_data_2(codeword[codeword_idx].cb, &segment[segment_idx],
    00397                                                &spectral_data[codeword[codeword_idx].sp_offset]) >= 0)
    00398                     {
    00399                         codeword[codeword_idx].decoded = 1;
    00400                     } else
    00401                     {
    00402                         codeword[codeword_idx].bits = segment[segment_idx];
    00403                         codeword[codeword_idx].bits.len = tmplen;
    00404                     }
    00405
    00406                 }
    00407             }
    00408         }
    00409         for (i = 0; i < numberOfSegments; i++)
    00410             rewrev_bits(&segment[i]);
    00411     }
    00412
    00413     bitsleft = 0;
    00414
    00415     for (i = 0; i < numberOfSegments && !bitsleft; i++)
    00416         bitsleft += segment[i].len;
    00417
    00418     if (bitsleft) return 10;
    00419
    00420     codewordsleft = 0;
    00421
    00422     for (i = 0; (i < numberOfCodewords - numberOfSegments) && (!codewordsleft); i++)
    00423         if (!codeword[i].decoded)
    00424                 codewordsleft++;
    00425
    00426     if (codewordsleft) return 10;
    00427
    00428     return 0;
    00429
    00430 }
    00431 #endif