bits.h

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 [email protected].
00029 **
00030 ** $Id: bits.h,v 1.2 2005/11/01 21:41:43 gabest Exp $
00031 **/
00032 
00033 #ifndef __BITS_H__
00034 #define __BITS_H__
00035 
00036 #ifdef __cplusplus
00037 extern "C" {
00038 #endif
00039 
00040 #include "analysis.h"
00041 #ifdef ANALYSIS
00042 #include <stdio.h>
00043 #endif
00044 
00045 #define BYTE_NUMBIT 8
00046 #define bit2byte(a) ((a+7)/BYTE_NUMBIT)
00047 
00048 typedef struct _bitfile
00049 {
00050     /* bit input */
00051     uint32_t bufa;
00052     uint32_t bufb;
00053     uint32_t bits_left;
00054     uint32_t buffer_size; /* size of the buffer in bytes */
00055     uint32_t bytes_used;
00056     uint8_t no_more_reading;
00057     uint8_t error;
00058     uint32_t *tail;
00059     uint32_t *start;
00060     void *buffer;
00061 } bitfile;
00062 
00063 
00064 #if defined (_WIN32) && !defined(_WIN32_WCE) && !defined(__MINGW32__)
00065 #define BSWAP(a) __asm mov eax,a __asm bswap eax __asm mov a, eax
00066 #elif defined(LINUX) || defined(DJGPP) || defined(__MINGW32__)
00067 #define BSWAP(a) __asm__ ( "bswapl %0\n" : "=r" (a) : "0" (a) )
00068 #else
00069 #define BSWAP(a) \
00070     ((a) = ( ((a)&0xff)<<24) | (((a)&0xff00)<<8) | (((a)>>8)&0xff00) | (((a)>>24)&0xff))
00071 #endif
00072 
00073 static uint32_t bitmask[] = {
00074     0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF,
00075     0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF,
00076     0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF,
00077     0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF,
00078     0xFFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF
00079     /* added bitmask 32, correct?!?!?! */
00080     , 0xFFFFFFFF
00081 };
00082 
00083 void faad_initbits(bitfile *ld, const void *buffer, const uint32_t buffer_size);
00084 void faad_endbits(bitfile *ld);
00085 void faad_initbits_rev(bitfile *ld, void *buffer,
00086                        uint32_t bits_in_buffer);
00087 uint8_t faad_byte_align(bitfile *ld);
00088 uint32_t faad_get_processed_bits(bitfile *ld);
00089 void faad_flushbits_ex(bitfile *ld, uint32_t bits);
00090 void faad_rewindbits(bitfile *ld);
00091 uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits
00092                        DEBUGDEC);
00093 #ifdef DRM
00094 void *faad_origbitbuffer(bitfile *ld);
00095 uint32_t faad_origbitbuffer_size(bitfile *ld);
00096 #endif
00097 
00098 /* circumvent memory alignment errors on ARM */
00099 static INLINE uint32_t getdword(void *mem)
00100 {
00101 #ifdef ARM
00102     uint32_t tmp;
00103 #ifndef ARCH_IS_BIG_ENDIAN
00104     ((uint8_t*)&tmp)[0] = ((uint8_t*)mem)[3];
00105     ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[2];
00106     ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[1];
00107     ((uint8_t*)&tmp)[3] = ((uint8_t*)mem)[0];
00108 #else
00109     ((uint8_t*)&tmp)[0] = ((uint8_t*)mem)[0];
00110     ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[1];
00111     ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[2];
00112     ((uint8_t*)&tmp)[3] = ((uint8_t*)mem)[3];
00113 #endif
00114 
00115     return tmp;
00116 #else
00117     uint32_t tmp;
00118     tmp = *(uint32_t*)mem;
00119 #ifndef ARCH_IS_BIG_ENDIAN
00120     BSWAP(tmp);
00121 #endif
00122     return tmp;
00123 #endif
00124 }
00125 
00126 static INLINE uint32_t faad_showbits(bitfile *ld, uint32_t bits)
00127 {
00128     if (bits <= ld->bits_left)
00129     {
00130         return (ld->bufa >> (ld->bits_left - bits)) & bitmask[bits];
00131     }
00132 
00133     bits -= ld->bits_left;
00134     return ((ld->bufa & bitmask[ld->bits_left]) << bits) | (ld->bufb >> (32 - bits));
00135 }
00136 
00137 static INLINE void faad_flushbits(bitfile *ld, uint32_t bits)
00138 {
00139     /* do nothing if error */
00140     if (ld->error != 0)
00141         return;
00142 
00143     if (bits < ld->bits_left)
00144     {
00145         ld->bits_left -= bits;
00146     } else {
00147         faad_flushbits_ex(ld, bits);
00148     }
00149 }
00150 
00151 /* return next n bits (right adjusted) */
00152 static INLINE uint32_t faad_getbits(bitfile *ld, uint32_t n DEBUGDEC)
00153 {
00154     uint32_t ret;
00155 
00156     if (ld->no_more_reading || n == 0)
00157         return 0;
00158 
00159     ret = faad_showbits(ld, n);
00160     faad_flushbits(ld, n);
00161 
00162 #ifdef ANALYSIS
00163     if (print)
00164         fprintf(stdout, "%4d %2d bits, val: %4d, variable: %d %s\n", dbg_count++, n, ret, var, dbg);
00165 #endif
00166 
00167     return ret;
00168 }
00169 
00170 static INLINE uint8_t faad_get1bit(bitfile *ld DEBUGDEC)
00171 {
00172     uint8_t r;
00173 
00174     if (ld->bits_left > 0)
00175     {
00176         ld->bits_left--;
00177         r = (uint8_t)((ld->bufa >> ld->bits_left) & 1);
00178         return r;
00179     }
00180 
00181     /* bits_left == 0 */
00182 #if 0
00183     r = (uint8_t)(ld->bufb >> 31);
00184     faad_flushbits_ex(ld, 1);
00185 #else
00186     r = (uint8_t)faad_getbits(ld, 1);
00187 #endif
00188     return r;
00189 }
00190 
00191 /* reversed bitreading routines */
00192 static INLINE uint32_t faad_showbits_rev(bitfile *ld, uint32_t bits)
00193 {
00194     uint8_t i;
00195     uint32_t B = 0;
00196 
00197     if (bits <= ld->bits_left)
00198     {
00199         for (i = 0; i < bits; i++)
00200         {
00201             if (ld->bufa & (1 << (i + (32 - ld->bits_left))))
00202                 B |= (1 << (bits - i - 1));
00203         }
00204         return B;
00205     } else {
00206         for (i = 0; i < ld->bits_left; i++)
00207         {
00208             if (ld->bufa & (1 << (i + (32 - ld->bits_left))))
00209                 B |= (1 << (bits - i - 1));
00210         }
00211         for (i = 0; i < bits - ld->bits_left; i++)
00212         {
00213             if (ld->bufb & (1 << (i + (32-ld->bits_left))))
00214                 B |= (1 << (bits - ld->bits_left - i - 1));
00215         }
00216         return B;
00217     }
00218 }
00219 
00220 static INLINE void faad_flushbits_rev(bitfile *ld, uint32_t bits)
00221 {
00222     /* do nothing if error */
00223     if (ld->error != 0)
00224         return;
00225 
00226     if (bits < ld->bits_left)
00227     {
00228         ld->bits_left -= bits;
00229     } else {
00230         uint32_t tmp;
00231 
00232         ld->bufa = ld->bufb;
00233         tmp = getdword(ld->start);
00234         ld->bufb = tmp;
00235         ld->start--;
00236         ld->bits_left += (32 - bits);
00237 
00238         ld->bytes_used += 4;
00239         if (ld->bytes_used == ld->buffer_size)
00240             ld->no_more_reading = 1;
00241         if (ld->bytes_used > ld->buffer_size)
00242             ld->error = 1;
00243     }
00244 }
00245 
00246 static INLINE uint32_t faad_getbits_rev(bitfile *ld, uint32_t n
00247                                         DEBUGDEC)
00248 {
00249     uint32_t ret;
00250 
00251     if (ld->no_more_reading)
00252         return 0;
00253 
00254     if (n == 0)
00255         return 0;
00256 
00257     ret = faad_showbits_rev(ld, n);
00258     faad_flushbits_rev(ld, n);
00259 
00260 #ifdef ANALYSIS
00261     if (print)
00262         fprintf(stdout, "%4d %2d bits, val: %4d, variable: %d %s\n", dbg_count++, n, ret, var, dbg);
00263 #endif
00264 
00265     return ret;
00266 }
00267 
00268 #ifdef DRM
00269 static uint8_t faad_check_CRC(bitfile *ld, uint16_t len)
00270 {
00271     uint8_t CRC;
00272     uint16_t r=255;  /* Initialize to all ones */
00273 
00274     /* CRC polynome used x^8 + x^4 + x^3 + x^2 +1 */
00275 #define GPOLY 0435
00276 
00277     faad_rewindbits(ld);
00278 
00279     CRC = (uint8_t) ~faad_getbits(ld, 8
00280         DEBUGVAR(1,999,"faad_check_CRC(): CRC"));          /* CRC is stored inverted */
00281 
00282     for (; len>0; len--)
00283     {
00284         r = ( (r << 1) ^ (( ( faad_get1bit(ld
00285             DEBUGVAR(1,998,""))  & 1) ^ ((r >> 7) & 1)) * GPOLY )) & 0xFF;
00286     }
00287 
00288     if (r != CRC)
00289   //  if (0)
00290     {
00291         return 28;
00292     } else {
00293         return 0;
00294     }
00295 }
00296 
00297 static uint8_t tabFlipbits[256] = {
00298     0,128,64,192,32,160,96,224,16,144,80,208,48,176,112,240,
00299     8,136,72,200,40,168,104,232,24,152,88,216,56,184,120,248,
00300     4,132,68,196,36,164,100,228,20,148,84,212,52,180,116,244,
00301     12,140,76,204,44,172,108,236,28,156,92,220,60,188,124,252,
00302     2,130,66,194,34,162,98,226,18,146,82,210,50,178,114,242,
00303     10,138,74,202,42,170,106,234,26,154,90,218,58,186,122,250,
00304     6,134,70,198,38,166,102,230,22,150,86,214,54,182,118,246,
00305     14,142,78,206,46,174,110,238,30,158,94,222,62,190,126,254,
00306     1,129,65,193,33,161,97,225,17,145,81,209,49,177,113,241,
00307     9,137,73,201,41,169,105,233,25,153,89,217,57,185,121,249,
00308     5,133,69,197,37,165,101,229,21,149,85,213,53,181,117,245,
00309     13,141,77,205,45,173,109,237,29,157,93,221,61,189,125,253,
00310     3,131,67,195,35,163,99,227,19,147,83,211,51,179,115,243,
00311     11,139,75,203,43,171,107,235,27,155,91,219,59,187,123,251,
00312     7,135,71,199,39,167,103,231,23,151,87,215,55,183,119,247,
00313     15,143,79,207,47,175,111,239,31,159,95,223,63,191,127,255
00314 };
00315 #endif
00316 
00317 #ifdef ERROR_RESILIENCE
00318 
00319 /* Modified bit reading functions for HCR */
00320 
00321 typedef struct
00322 {
00323     /* bit input */
00324     uint32_t bufa;
00325     uint32_t bufb;
00326     int8_t len;
00327 } bits_t;
00328 
00329 
00330 static INLINE uint32_t showbits_hcr(bits_t *ld, uint8_t bits)
00331 {
00332     if (bits == 0) return 0;
00333     if (ld->len <= 32)
00334     {
00335         /* huffman_spectral_data_2 needs to read more than may be available, bits maybe
00336            > ld->len, deliver 0 than */
00337         if (ld->len >= bits)
00338             return ((ld->bufa >> (ld->len - bits)) & (0xFFFFFFFF >> (32 - bits)));
00339         else
00340             return ((ld->bufa << (bits - ld->len)) & (0xFFFFFFFF >> (32 - bits)));
00341     } else {
00342         if ((ld->len - bits) < 32)
00343         {
00344             return ( (ld->bufb & (0xFFFFFFFF >> (64 - ld->len))) << (bits - ld->len + 32)) |
00345                 (ld->bufa >> (ld->len - bits));
00346         } else {
00347             return ((ld->bufb >> (ld->len - bits - 32)) & (0xFFFFFFFF >> (32 - bits)));
00348         }
00349     }
00350 }
00351 
00352 /* return 1 if position is outside of buffer, 0 otherwise */
00353 static INLINE int8_t flushbits_hcr( bits_t *ld, uint8_t bits)
00354 {
00355     ld->len -= bits;
00356 
00357     if (ld->len <0)
00358     {
00359         ld->len = 0;
00360         return 1;
00361     } else {
00362         return 0;
00363     }
00364 }
00365 
00366 static INLINE int8_t getbits_hcr(bits_t *ld, uint8_t n, uint32_t *result)
00367 {
00368     *result = showbits_hcr(ld, n);
00369     return flushbits_hcr(ld, n);
00370 }
00371 
00372 static INLINE int8_t get1bit_hcr(bits_t *ld, uint8_t *result)
00373 {
00374     uint32_t res;
00375     int8_t ret;
00376 
00377     ret = getbits_hcr(ld, 1, &res);
00378     *result = (int8_t)(res & 1);
00379     return ret;
00380 }
00381 
00382 #endif
00383 
00384 
00385 #ifdef __cplusplus
00386 }
00387 #endif
00388 #endif

Generated on Tue Dec 13 14:47:28 2005 for guliverkli by  doxygen 1.4.5