- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# rvlc.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: rvlc.c,v 1.2 2005/11/01 21:41:43 gabest Exp $
    00031 **/
    00032
    00033 /* RVLC scalefactor decoding
    00034  *
    00035  * RVLC works like this:
    00036  *  1. Only symmetric huffman codewords are used
    00037  *  2. Total length of the scalefactor data is stored in the bitsream
    00038  *  3. Scalefactors are DPCM coded
    00039  *  4. Next to the starting value for DPCM the ending value is also stored
    00040  *
    00041  * With all this it is possible to read the scalefactor data from 2 sides.
    00042  * If there is a bit error in the scalefactor data it is possible to start
    00043  * decoding from the other end of the data, to find all but 1 scalefactor.
    00044  */
    00045
    00046 #include "common.h"
    00047 #include "structs.h"
    00048
    00049 #include <stdlib.h>
    00050
    00051 #include "syntax.h"
    00052 #include "bits.h"
    00053 #include "rvlc.h"
    00054
    00055
    00056 #ifdef ERROR_RESILIENCE
    00057
    00058 //#define PRINT_RVLC
    00059
    00060 /* static function declarations */
    00061 static uint8_t rvlc_decode_sf_forward(ic_stream *ics,
    00062                                       bitfile *ld_sf,
    00063                                       bitfile *ld_esc,
    00064                                       uint8_t *is_used);
    00065 #if 0
    00066 static uint8_t rvlc_decode_sf_reverse(ic_stream *ics,
    00067                                       bitfile *ld_sf,
    00068                                       bitfile *ld_esc,
    00069                                       uint8_t is_used);
    00070 #endif
    00071 static int8_t rvlc_huffman_sf(bitfile *ld_sf, bitfile *ld_esc,
    00072                               int8_t direction);
    00073 static int8_t rvlc_huffman_esc(bitfile *ld_esc, int8_t direction);
    00074
    00075
    00076 uint8_t rvlc_scale_factor_data(ic_stream *ics, bitfile *ld)
    00077 {
    00078     uint8_t bits = 9;
    00079
    00080     ics->sf_concealment = faad_get1bit(ld
    00081         DEBUGVAR(1,149,"rvlc_scale_factor_data(): sf_concealment"));
    00082     ics->rev_global_gain = (uint8_t)faad_getbits(ld, 8
    00083         DEBUGVAR(1,150,"rvlc_scale_factor_data(): rev_global_gain"));
    00084
    00085     if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
    00086         bits = 11;
    00087
    00088     /* the number of bits used for the huffman codewords */
    00089     ics->length_of_rvlc_sf = (uint16_t)faad_getbits(ld, bits
    00090         DEBUGVAR(1,151,"rvlc_scale_factor_data(): length_of_rvlc_sf"));
    00091
    00092     if (ics->noise_used)
    00093     {
    00094         ics->dpcm_noise_nrg = (uint16_t)faad_getbits(ld, 9
    00095             DEBUGVAR(1,152,"rvlc_scale_factor_data(): dpcm_noise_nrg"));
    00096
    00097         ics->length_of_rvlc_sf -= 9;
    00098     }
    00099
    00100     ics->sf_escapes_present = faad_get1bit(ld
    00101         DEBUGVAR(1,153,"rvlc_scale_factor_data(): sf_escapes_present"));
    00102
    00103     if (ics->sf_escapes_present)
    00104     {
    00105         ics->length_of_rvlc_escapes = (uint8_t)faad_getbits(ld, 8
    00106             DEBUGVAR(1,154,"rvlc_scale_factor_data(): length_of_rvlc_escapes"));
    00107     }
    00108
    00109     if (ics->noise_used)
    00110     {
    00111         ics->dpcm_noise_last_position = (uint16_t)faad_getbits(ld, 9
    00112             DEBUGVAR(1,155,"rvlc_scale_factor_data(): dpcm_noise_last_position"));
    00113     }
    00114
    00115     return 0;
    00116 }
    00117
    00118 uint8_t rvlc_decode_scale_factors(ic_stream *ics, bitfile *ld)
    00119 {
    00120     uint8_t result;
    00121     uint8_t intensity_used = 0;
    00122     uint8_t *rvlc_sf_buffer = NULL;
    00123     uint8_t *rvlc_esc_buffer = NULL;
    00124     bitfile ld_rvlc_sf, ld_rvlc_esc;
    00125 //    bitfile ld_rvlc_sf_rev, ld_rvlc_esc_rev;
    00126
    00127     if (ics->length_of_rvlc_sf > 0)
    00128     {
    00129         /* We read length_of_rvlc_sf bits here to put it in a
    00130            seperate bitfile.
    00131         */
    00132         rvlc_sf_buffer = faad_getbitbuffer(ld, ics->length_of_rvlc_sf
    00133             DEBUGVAR(1,156,"rvlc_decode_scale_factors(): bitbuffer: length_of_rvlc_sf"));
    00134
    00135         faad_initbits(&ld_rvlc_sf, (void*)rvlc_sf_buffer, bit2byte(ics->length_of_rvlc_sf));
    00136 //        faad_initbits_rev(&ld_rvlc_sf_rev, (void*)rvlc_sf_buffer,
    00137 //            ics->length_of_rvlc_sf);
    00138     }
    00139
    00140     if (ics->sf_escapes_present)
    00141     {
    00142         /* We read length_of_rvlc_escapes bits here to put it in a
    00143            seperate bitfile.
    00144         */
    00145         rvlc_esc_buffer = faad_getbitbuffer(ld, ics->length_of_rvlc_escapes
    00146             DEBUGVAR(1,157,"rvlc_decode_scale_factors(): bitbuffer: length_of_rvlc_escapes"));
    00147
    00148         faad_initbits(&ld_rvlc_esc, (void*)rvlc_esc_buffer, bit2byte(ics->length_of_rvlc_escapes));
    00149 //        faad_initbits_rev(&ld_rvlc_esc_rev, (void*)rvlc_esc_buffer,
    00150 //            ics->length_of_rvlc_escapes);
    00151     }
    00152
    00153     /* decode the rvlc scale factors and escapes */
    00154     result = rvlc_decode_sf_forward(ics, &ld_rvlc_sf,
    00155         &ld_rvlc_esc, &intensity_used);
    00156 //    result = rvlc_decode_sf_reverse(ics, &ld_rvlc_sf_rev,
    00157 //        &ld_rvlc_esc_rev, intensity_used);
    00158
    00159
    00160     if (rvlc_esc_buffer) faad_free(rvlc_esc_buffer);
    00161     if (rvlc_sf_buffer) faad_free(rvlc_sf_buffer);
    00162
    00163     if (ics->length_of_rvlc_sf > 0)
    00164         faad_endbits(&ld_rvlc_sf);
    00165     if (ics->sf_escapes_present)
    00166         faad_endbits(&ld_rvlc_esc);
    00167
    00168     return result;
    00169 }
    00170
    00171 static uint8_t rvlc_decode_sf_forward(ic_stream *ics, bitfile *ld_sf, bitfile *ld_esc,
    00172                                       uint8_t *intensity_used)
    00173 {
    00174     int8_t g, sfb;
    00175     int8_t t = 0;
    00176     int8_t error = 0;
    00177     int8_t noise_pcm_flag = 1;
    00178
    00179     int16_t scale_factor = ics->global_gain;
    00180     int16_t is_position = 0;
    00181     int16_t noise_energy = ics->global_gain - 90 - 256;
    00182
    00183 #ifdef PRINT_RVLC
    00184     printf("\nglobal_gain: %d\n", ics->global_gain);
    00185 #endif
    00186
    00187     for (g = 0; g < ics->num_window_groups; g++)
    00188     {
    00189         for (sfb = 0; sfb < ics->max_sfb; sfb++)
    00190         {
    00191             if (error)
    00192             {
    00193                 ics->scale_factors[g][sfb] = 0;
    00194             } else {
    00195                 switch (ics->sfb_cb[g][sfb])
    00196                 {
    00197                 case ZERO_HCB: /* zero book */
    00198                     ics->scale_factors[g][sfb] = 0;
    00199                     break;
    00200                 case INTENSITY_HCB: /* intensity books */
    00201                 case INTENSITY_HCB2:
    00202
    00203                     *intensity_used = 1;
    00204
    00205                     /* decode intensity position */
    00206                     t = rvlc_huffman_sf(ld_sf, ld_esc, +1);
    00207
    00208                     is_position += t;
    00209                     ics->scale_factors[g][sfb] = is_position;
    00210
    00211                     break;
    00212                 case NOISE_HCB: /* noise books */
    00213
    00214                     /* decode noise energy */
    00215                     if (noise_pcm_flag)
    00216                     {
    00217                         int16_t n = ics->dpcm_noise_nrg;
    00218                         noise_pcm_flag = 0;
    00219                         noise_energy += n;
    00220                     } else {
    00221                         t = rvlc_huffman_sf(ld_sf, ld_esc, +1);
    00222                         noise_energy += t;
    00223                     }
    00224
    00225                     ics->scale_factors[g][sfb] = noise_energy;
    00226
    00227                     break;
    00228                 default: /* spectral books */
    00229
    00230                     /* decode scale factor */
    00231                     t = rvlc_huffman_sf(ld_sf, ld_esc, +1);
    00232
    00233                     scale_factor += t;
    00234                     if (scale_factor < 0)
    00235                         return 4;
    00236
    00237                     ics->scale_factors[g][sfb] = scale_factor;
    00238
    00239                     break;
    00240                 }
    00241 #ifdef PRINT_RVLC
    00242                 printf("%3d:%4d%4d\n", sfb, ics->sfb_cb[g][sfb],
    00243                     ics->scale_factors[g][sfb]);
    00244 #endif
    00245                 if (t == 99)
    00246                 {
    00247                     error = 1;
    00248                 }
    00249             }
    00250         }
    00251     }
    00252 #ifdef PRINT_RVLC
    00253     printf("\n\n");
    00254 #endif
    00255
    00256     return 0;
    00257 }
    00258
    00259 #if 0 // not used right now, doesn't work correctly yet
    00260 static uint8_t rvlc_decode_sf_reverse(ic_stream *ics, bitfile *ld_sf, bitfile *ld_esc,
    00261                                       uint8_t intensity_used)
    00262 {
    00263     int8_t g, sfb;
    00264     int8_t t = 0;
    00265     int8_t error = 0;
    00266     int8_t noise_pcm_flag = 1, is_pcm_flag = 1, sf_pcm_flag = 1;
    00267
    00268     int16_t scale_factor = ics->rev_global_gain;
    00269     int16_t is_position = 0;
    00270     int16_t noise_energy = ics->rev_global_gain;
    00271
    00272 #ifdef PRINT_RVLC
    00273     printf("\nrev_global_gain: %d\n", ics->rev_global_gain);
    00274 #endif
    00275
    00276     if (intensity_used)
    00277     {
    00278         is_position = rvlc_huffman_sf(ld_sf, ld_esc, -1);
    00279 #ifdef PRINT_RVLC
    00280         printf("is_position: %d\n", is_position);
    00281 #endif
    00282     }
    00283
    00284     for (g = ics->num_window_groups-1; g >= 0; g--)
    00285     {
    00286         for (sfb = ics->max_sfb-1; sfb >= 0; sfb--)
    00287         {
    00288             if (error)
    00289             {
    00290                 ics->scale_factors[g][sfb] = 0;
    00291             } else {
    00292                 switch (ics->sfb_cb[g][sfb])
    00293                 {
    00294                 case ZERO_HCB: /* zero book */
    00295                     ics->scale_factors[g][sfb] = 0;
    00296                     break;
    00297                 case INTENSITY_HCB: /* intensity books */
    00298                 case INTENSITY_HCB2:
    00299
    00300                     if (is_pcm_flag)
    00301                     {
    00302                         is_pcm_flag = 0;
    00303                         ics->scale_factors[g][sfb] = is_position;
    00304                     } else {
    00305                         t = rvlc_huffman_sf(ld_sf, ld_esc, -1);
    00306                         is_position -= t;
    00307
    00308                         ics->scale_factors[g][sfb] = (uint8_t)is_position;
    00309                     }
    00310                     break;
    00311                 case NOISE_HCB: /* noise books */
    00312
    00313                     /* decode noise energy */
    00314                     if (noise_pcm_flag)
    00315                     {
    00316                         noise_pcm_flag = 0;
    00317                         noise_energy = ics->dpcm_noise_last_position;
    00318                     } else {
    00319                         t = rvlc_huffman_sf(ld_sf, ld_esc, -1);
    00320                         noise_energy -= t;
    00321                     }
    00322
    00323                     ics->scale_factors[g][sfb] = (uint8_t)noise_energy;
    00324                     break;
    00325                 default: /* spectral books */
    00326
    00327                     if (sf_pcm_flag || (sfb == 0))
    00328                     {
    00329                         sf_pcm_flag = 0;
    00330                         if (sfb == 0)
    00331                             scale_factor = ics->global_gain;
    00332                     } else {
    00333                         /* decode scale factor */
    00334                         t = rvlc_huffman_sf(ld_sf, ld_esc, -1);
    00335                         scale_factor -= t;
    00336                     }
    00337
    00338                     if (scale_factor < 0)
    00339                         return 4;
    00340
    00341                     ics->scale_factors[g][sfb] = (uint8_t)scale_factor;
    00342                     break;
    00343                 }
    00344 #ifdef PRINT_RVLC
    00345                 printf("%3d:%4d%4d\n", sfb, ics->sfb_cb[g][sfb],
    00346                     ics->scale_factors[g][sfb]);
    00347 #endif
    00348                 if (t == 99)
    00349                 {
    00350                     error = 1;
    00351                 }
    00352             }
    00353         }
    00354     }
    00355
    00356 #ifdef PRINT_RVLC
    00357     printf("\n\n");
    00358 #endif
    00359
    00360     return 0;
    00361 }
    00362 #endif
    00363
    00364 /* index == 99 means not allowed codeword */
    00365 static rvlc_huff_table book_rvlc[] = {
    00366     /*index  length  codeword */
    00367     {  0, 1,   0 }, /*         0 */
    00368     { -1, 3,   5 }, /*       101 */
    00369     {  1, 3,   7 }, /*       111 */
    00370     { -2, 4,   9 }, /*      1001 */
    00371     { -3, 5,  17 }, /*     10001 */
    00372     {  2, 5,  27 }, /*     11011 */
    00373     { -4, 6,  33 }, /*    100001 */
    00374     { 99, 6,  50 }, /*    110010 */
    00375     {  3, 6,  51 }, /*    110011 */
    00376     { 99, 6,  52 }, /*    110100 */
    00377     { -7, 7,  65 }, /*   1000001 */
    00378     { 99, 7,  96 }, /*   1100000 */
    00379     { 99, 7,  98 }, /*   1100010 */
    00380     {  7, 7,  99 }, /*   1100011 */
    00381     {  4, 7, 107 }, /*   1101011 */
    00382     { -5, 8, 129 }, /*  10000001 */
    00383     { 99, 8, 194 }, /*  11000010 */
    00384     {  5, 8, 195 }, /*  11000011 */
    00385     { 99, 8, 212 }, /*  11010100 */
    00386     { 99, 9, 256 }, /* 100000000 */
    00387     { -6, 9, 257 }, /* 100000001 */
    00388     { 99, 9, 426 }, /* 110101010 */
    00389     {  6, 9, 427 }, /* 110101011 */
    00390     { 99, 10,  0 } /* Shouldn't come this far */
    00391 };
    00392
    00393 static rvlc_huff_table book_escape[] = {
    00394     /*index  length  codeword */
    00395     { 1, 2, 0 },
    00396     { 0, 2, 2 },
    00397     { 3, 3, 2 },
    00398     { 2, 3, 6 },
    00399     { 4, 4, 14 },
    00400     { 7, 5, 13 },
    00401     { 6, 5, 15 },
    00402     { 5, 5, 31 },
    00403     { 11, 6, 24 },
    00404     { 10, 6, 25 },
    00405     { 9, 6, 29 },
    00406     { 8, 6, 61 },
    00407     { 13, 7, 56  },
    00408     { 12, 7, 120 },
    00409     { 15, 8, 114 },
    00410     { 14, 8, 242 },
    00411     { 17, 9, 230 },
    00412     { 16, 9, 486 },
    00413     { 19, 10, 463  },
    00414     { 18, 10, 974  },
    00415     { 22, 11, 925  },
    00416     { 20, 11, 1950 },
    00417     { 21, 11, 1951 },
    00418     { 23, 12, 1848 },
    00419     { 25, 13, 3698 },
    00420     { 24, 14, 7399 },
    00421     { 26, 15, 14797 },
    00422     { 49, 19, 236736 },
    00423     { 50, 19, 236737 },
    00424     { 51, 19, 236738 },
    00425     { 52, 19, 236739 },
    00426     { 53, 19, 236740 },
    00427     { 27, 20, 473482 },
    00428     { 28, 20, 473483 },
    00429     { 29, 20, 473484 },
    00430     { 30, 20, 473485 },
    00431     { 31, 20, 473486 },
    00432     { 32, 20, 473487 },
    00433     { 33, 20, 473488 },
    00434     { 34, 20, 473489 },
    00435     { 35, 20, 473490 },
    00436     { 36, 20, 473491 },
    00437     { 37, 20, 473492 },
    00438     { 38, 20, 473493 },
    00439     { 39, 20, 473494 },
    00440     { 40, 20, 473495 },
    00441     { 41, 20, 473496 },
    00442     { 42, 20, 473497 },
    00443     { 43, 20, 473498 },
    00444     { 44, 20, 473499 },
    00445     { 45, 20, 473500 },
    00446     { 46, 20, 473501 },
    00447     { 47, 20, 473502 },
    00448     { 48, 20, 473503 },
    00449     { 99, 21,  0 } /* Shouldn't come this far */
    00450 };
    00451
    00452 static int8_t rvlc_huffman_sf(bitfile *ld_sf, bitfile *ld_esc,
    00453                               int8_t direction)
    00454 {
    00455     uint8_t i, j;
    00456     int8_t index;
    00457     uint32_t cw;
    00458     rvlc_huff_table *h = book_rvlc;
    00459
    00460     i = h->len;
    00461     if (direction > 0)
    00462         cw = faad_getbits(ld_sf, i DEBUGVAR(1,0,""));
    00463     else
    00464         cw = faad_getbits_rev(ld_sf, i DEBUGVAR(1,0,""));
    00465
    00466     while ((cw != h->cw)
    00467         && (i < 10))
    00468     {
    00469         h++;
    00470         j = h->len-i;
    00471         i += j;
    00472         cw <<= j;
    00473         if (direction > 0)
    00474             cw |= faad_getbits(ld_sf, j DEBUGVAR(1,0,""));
    00475         else
    00476             cw |= faad_getbits_rev(ld_sf, j DEBUGVAR(1,0,""));
    00477     }
    00478
    00479     index = h->index;
    00480
    00481     if (index == +ESC_VAL)
    00482     {
    00483         int8_t esc = rvlc_huffman_esc(ld_esc, direction);
    00484         if (esc == 99)
    00485             return 99;
    00486         index += esc;
    00487 #ifdef PRINT_RVLC
    00488         printf("esc: %d - ", esc);
    00489 #endif
    00490     }
    00491     if (index == -ESC_VAL)
    00492     {
    00493         int8_t esc = rvlc_huffman_esc(ld_esc, direction);
    00494         if (esc == 99)
    00495             return 99;
    00496         index -= esc;
    00497 #ifdef PRINT_RVLC
    00498         printf("esc: %d - ", esc);
    00499 #endif
    00500     }
    00501
    00502     return index;
    00503 }
    00504
    00505 static int8_t rvlc_huffman_esc(bitfile *ld,
    00506                                int8_t direction)
    00507 {
    00508     uint8_t i, j;
    00509     uint32_t cw;
    00510     rvlc_huff_table *h = book_escape;
    00511
    00512     i = h->len;
    00513     if (direction > 0)
    00514         cw = faad_getbits(ld, i DEBUGVAR(1,0,""));
    00515     else
    00516         cw = faad_getbits_rev(ld, i DEBUGVAR(1,0,""));
    00517
    00518     while ((cw != h->cw)
    00519         && (i < 21))
    00520     {
    00521         h++;
    00522         j = h->len-i;
    00523         i += j;
    00524         cw <<= j;
    00525         if (direction > 0)
    00526             cw |= faad_getbits(ld, j DEBUGVAR(1,0,""));
    00527         else
    00528             cw |= faad_getbits_rev(ld, j DEBUGVAR(1,0,""));
    00529     }
    00530
    00531     return h->index;
    00532 }
    00533
    00534 #endif
    00535