- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# bits.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: bits.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 #include "bits.h"
    00039
    00040 /* initialize buffer, call once before first getbits or showbits */
    00041 void faad_initbits(bitfile *ld, const void *_buffer, const uint32_t buffer_size)
    00042 {
    00043     uint32_t tmp;
    00044
    00045     if (ld == NULL)
    00046         return;
    00047
    00048     memset(ld, 0, sizeof(bitfile));
    00049
    00050     if (buffer_size == 0 || _buffer == NULL)
    00051     {
    00052         ld->error = 1;
    00053         ld->no_more_reading = 1;
    00054         return;
    00055     }
    00056
    00057     ld->buffer = faad_malloc((buffer_size+12)*sizeof(uint8_t));
    00058     memset(ld->buffer, 0, (buffer_size+12)*sizeof(uint8_t));
    00059     memcpy(ld->buffer, _buffer, buffer_size*sizeof(uint8_t));
    00060
    00061     ld->buffer_size = buffer_size;
    00062
    00063     tmp = getdword((uint32_t*)ld->buffer);
    00064     ld->bufa = tmp;
    00065
    00066     tmp = getdword((uint32_t*)ld->buffer + 1);
    00067     ld->bufb = tmp;
    00068
    00069     ld->start = (uint32_t*)ld->buffer;
    00070     ld->tail = ((uint32_t*)ld->buffer + 2);
    00071
    00072     ld->bits_left = 32;
    00073
    00074     ld->bytes_used = 0;
    00075     ld->no_more_reading = 0;
    00076     ld->error = 0;
    00077 }
    00078
    00079 void faad_endbits(bitfile *ld)
    00080 {
    00081     if (ld)
    00082     {
    00083         if (ld->buffer)
    00084         {
    00085             faad_free(ld->buffer);
    00086             ld->buffer = NULL;
    00087         }
    00088     }
    00089 }
    00090
    00091 uint32_t faad_get_processed_bits(bitfile *ld)
    00092 {
    00093     return (uint32_t)(8 * (4*(ld->tail - ld->start) - 4) - (ld->bits_left));
    00094 }
    00095
    00096 uint8_t faad_byte_align(bitfile *ld)
    00097 {
    00098     uint8_t remainder = (uint8_t)((32 - ld->bits_left) % 8);
    00099
    00100     if (remainder)
    00101     {
    00102         faad_flushbits(ld, 8 - remainder);
    00103         return (8 - remainder);
    00104     }
    00105     return 0;
    00106 }
    00107
    00108 void faad_flushbits_ex(bitfile *ld, uint32_t bits)
    00109 {
    00110     uint32_t tmp;
    00111
    00112     ld->bufa = ld->bufb;
    00113     if (ld->no_more_reading == 0)
    00114     {
    00115         tmp = getdword(ld->tail);
    00116         ld->tail++;
    00117     } else {
    00118         tmp = 0;
    00119     }
    00120     ld->bufb = tmp;
    00121     ld->bits_left += (32 - bits);
    00122     ld->bytes_used += 4;
    00123     if (ld->bytes_used == ld->buffer_size)
    00124         ld->no_more_reading = 1;
    00125     if (ld->bytes_used > ld->buffer_size)
    00126         ld->error = 1;
    00127 }
    00128
    00129 /* rewind to beginning */
    00130 void faad_rewindbits(bitfile *ld)
    00131 {
    00132     uint32_t tmp;
    00133
    00134     tmp = ld->start[0];
    00135 #ifndef ARCH_IS_BIG_ENDIAN
    00136     BSWAP(tmp);
    00137 #endif
    00138     ld->bufa = tmp;
    00139
    00140     tmp = ld->start[1];
    00141 #ifndef ARCH_IS_BIG_ENDIAN
    00142     BSWAP(tmp);
    00143 #endif
    00144     ld->bufb = tmp;
    00145     ld->bits_left = 32;
    00146     ld->tail = &ld->start[2];
    00147     ld->bytes_used = 0;
    00148     ld->no_more_reading = 0;
    00149 }
    00150
    00151 uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits
    00152                        DEBUGDEC)
    00153 {
    00154     uint16_t i;
    00155     uint8_t temp;
    00156     uint16_t bytes = (uint16_t)bits / 8;
    00157     uint8_t remainder = (uint8_t)bits % 8;
    00158
    00159     uint8_t *buffer = (uint8_t*)faad_malloc((bytes+1)*sizeof(uint8_t));
    00160
    00161     for (i = 0; i < bytes; i++)
    00162     {
    00163         buffer[i] = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(print,var,dbg));
    00164     }
    00165
    00166     if (remainder)
    00167     {
    00168         temp = (uint8_t)faad_getbits(ld, remainder DEBUGVAR(print,var,dbg)) << (8-remainder);
    00169
    00170         buffer[bytes] = temp;
    00171     }
    00172
    00173     return buffer;
    00174 }
    00175
    00176 #ifdef DRM
    00177 /* return the original data buffer */
    00178 void *faad_origbitbuffer(bitfile *ld)
    00179 {
    00180     return (void*)ld->start;
    00181 }
    00182
    00183 /* return the original data buffer size */
    00184 uint32_t faad_origbitbuffer_size(bitfile *ld)
    00185 {
    00186     return ld->buffer_size;
    00187 }
    00188 #endif
    00189
    00190 /* reversed bit reading routines, used for RVLC and HCR */
    00191 void faad_initbits_rev(bitfile *ld, void *buffer,
    00192                        uint32_t bits_in_buffer)
    00193 {
    00194     uint32_t tmp;
    00195     int32_t index;
    00196
    00197     ld->buffer_size = bit2byte(bits_in_buffer);
    00198
    00199     index = (bits_in_buffer+31)/32 - 1;
    00200
    00201     ld->start = (uint32_t*)buffer + index - 2;
    00202
    00203     tmp = getdword((uint32_t*)buffer + index);
    00204     ld->bufa = tmp;
    00205
    00206     tmp = getdword((uint32_t*)buffer + index - 1);
    00207     ld->bufb = tmp;
    00208
    00209     ld->tail = (uint32_t*)buffer + index;
    00210
    00211     ld->bits_left = bits_in_buffer % 32;
    00212     if (ld->bits_left == 0)
    00213         ld->bits_left = 32;
    00214
    00215     ld->bytes_used = 0;
    00216     ld->no_more_reading = 0;
    00217     ld->error = 0;
    00218 }