Add a missing check for a NULL return value.
[mplayer/glamo.git] / libfaad2 / bits.c
blob34efbd4f711bffdf54aebd5e52612815a1573f0e
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, 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.
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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id: bits.c,v 1.39 2004/09/04 14:56:27 menno Exp $
26 **/
28 #include "common.h"
29 #include "structs.h"
31 #include <stdlib.h>
32 #include <string.h>
33 #include "bits.h"
35 /* initialize buffer, call once before first getbits or showbits */
36 void faad_initbits(bitfile *ld, const void *_buffer, const uint32_t buffer_size)
38 uint32_t tmp;
40 if (ld == NULL)
41 return;
43 memset(ld, 0, sizeof(bitfile));
45 if (buffer_size == 0 || _buffer == NULL)
47 ld->error = 1;
48 ld->no_more_reading = 1;
49 return;
52 ld->buffer = faad_malloc((buffer_size+12)*sizeof(uint8_t));
53 memset(ld->buffer, 0, (buffer_size+12)*sizeof(uint8_t));
54 memcpy(ld->buffer, _buffer, buffer_size*sizeof(uint8_t));
56 ld->buffer_size = buffer_size;
58 tmp = getdword((uint32_t*)ld->buffer);
59 ld->bufa = tmp;
61 tmp = getdword((uint32_t*)ld->buffer + 1);
62 ld->bufb = tmp;
64 ld->start = (uint32_t*)ld->buffer;
65 ld->tail = ((uint32_t*)ld->buffer + 2);
67 ld->bits_left = 32;
69 ld->bytes_used = 0;
70 ld->no_more_reading = 0;
71 ld->error = 0;
74 void faad_endbits(bitfile *ld)
76 if (ld)
78 if (ld->buffer)
80 faad_free(ld->buffer);
81 ld->buffer = NULL;
86 uint32_t faad_get_processed_bits(bitfile *ld)
88 return (uint32_t)(8 * (4*(ld->tail - ld->start) - 4) - (ld->bits_left));
91 uint8_t faad_byte_align(bitfile *ld)
93 uint8_t remainder = (uint8_t)((32 - ld->bits_left) % 8);
95 if (remainder)
97 faad_flushbits(ld, 8 - remainder);
98 return (8 - remainder);
100 return 0;
103 void faad_flushbits_ex(bitfile *ld, uint32_t bits)
105 uint32_t tmp;
107 ld->bufa = ld->bufb;
108 if (ld->no_more_reading == 0)
110 tmp = getdword(ld->tail);
111 ld->tail++;
112 } else {
113 tmp = 0;
115 ld->bufb = tmp;
116 ld->bits_left += (32 - bits);
117 ld->bytes_used += 4;
118 if (ld->bytes_used == ld->buffer_size)
119 ld->no_more_reading = 1;
120 if (ld->bytes_used > ld->buffer_size)
121 ld->error = 1;
124 /* rewind to beginning */
125 void faad_rewindbits(bitfile *ld)
127 uint32_t tmp;
129 tmp = ld->start[0];
130 #ifndef ARCH_IS_BIG_ENDIAN
131 BSWAP(tmp);
132 #endif
133 ld->bufa = tmp;
135 tmp = ld->start[1];
136 #ifndef ARCH_IS_BIG_ENDIAN
137 BSWAP(tmp);
138 #endif
139 ld->bufb = tmp;
140 ld->bits_left = 32;
141 ld->tail = &ld->start[2];
142 ld->bytes_used = 0;
143 ld->no_more_reading = 0;
146 uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits
147 DEBUGDEC)
149 uint16_t i;
150 uint8_t temp;
151 uint16_t bytes = (uint16_t)bits / 8;
152 uint8_t remainder = (uint8_t)bits % 8;
154 uint8_t *buffer = (uint8_t*)faad_malloc((bytes+1)*sizeof(uint8_t));
156 for (i = 0; i < bytes; i++)
158 buffer[i] = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(print,var,dbg));
161 if (remainder)
163 temp = (uint8_t)faad_getbits(ld, remainder DEBUGVAR(print,var,dbg)) << (8-remainder);
165 buffer[bytes] = temp;
168 return buffer;
171 #ifdef DRM
172 /* return the original data buffer */
173 void *faad_origbitbuffer(bitfile *ld)
175 return (void*)ld->start;
178 /* return the original data buffer size */
179 uint32_t faad_origbitbuffer_size(bitfile *ld)
181 return ld->buffer_size;
183 #endif
185 /* reversed bit reading routines, used for RVLC and HCR */
186 void faad_initbits_rev(bitfile *ld, void *buffer,
187 uint32_t bits_in_buffer)
189 uint32_t tmp;
190 int32_t index;
192 ld->buffer_size = bit2byte(bits_in_buffer);
194 index = (bits_in_buffer+31)/32 - 1;
196 ld->start = (uint32_t*)buffer + index - 2;
198 tmp = getdword((uint32_t*)buffer + index);
199 ld->bufa = tmp;
201 tmp = getdword((uint32_t*)buffer + index - 1);
202 ld->bufb = tmp;
204 ld->tail = (uint32_t*)buffer + index;
206 ld->bits_left = bits_in_buffer % 32;
207 if (ld->bits_left == 0)
208 ld->bits_left = 32;
210 ld->bytes_used = 0;
211 ld->no_more_reading = 0;
212 ld->error = 0;