annotate libfaad2/bits.c @ 10880:ba9557e864c0

vo_gl2 port to win32 patch by Tristan Seligmann <mithrandi-mplayer-dev-eng at mithrandi.za.net>
author faust3
date Fri, 19 Sep 2003 14:33:51 +0000
parents e989150f8216
children 3185f64f6350
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10725
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
1 /*
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
4 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
5 ** This program is free software; you can redistribute it and/or modify
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
6 ** it under the terms of the GNU General Public License as published by
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
7 ** the Free Software Foundation; either version 2 of the License, or
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
8 ** (at your option) any later version.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
9 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
10 ** This program is distributed in the hope that it will be useful,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
13 ** GNU General Public License for more details.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
14 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
15 ** You should have received a copy of the GNU General Public License
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
16 ** along with this program; if not, write to the Free Software
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
18 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
19 ** Any non-GPL usage of this software or parts of this software is strictly
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
20 ** forbidden.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
21 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
22 ** Commercial non-GPL licensing of this software is possible.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
24 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
25 ** $Id: bits.c,v 1.22 2003/07/29 08:20:12 menno Exp $
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
26 **/
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
27
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
28 #include "common.h"
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
29 #include "structs.h"
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
30
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
31 #include <stdlib.h>
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
32 #include <string.h>
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
33 #include "bits.h"
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
34
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
35 /* initialize buffer, call once before first getbits or showbits */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
36 void faad_initbits(bitfile *ld, void *_buffer, uint32_t buffer_size)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
37 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
38 uint32_t tmp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
39
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
40 ld->buffer = malloc((buffer_size+12)*sizeof(uint8_t));
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
41 memset(ld->buffer, 0, (buffer_size+12)*sizeof(uint8_t));
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
42 memcpy(ld->buffer, _buffer, buffer_size*sizeof(uint8_t));
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
43
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
44 ld->buffer_size = buffer_size;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
45
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
46 tmp = getdword((uint32_t*)ld->buffer);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
47 #ifndef ARCH_IS_BIG_ENDIAN
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
48 BSWAP(tmp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
49 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
50 ld->bufa = tmp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
51
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
52 tmp = getdword((uint32_t*)ld->buffer + 1);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
53 #ifndef ARCH_IS_BIG_ENDIAN
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
54 BSWAP(tmp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
55 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
56 ld->bufb = tmp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
57
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
58 ld->start = (uint32_t*)ld->buffer;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
59 ld->tail = ((uint32_t*)ld->buffer + 2);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
60
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
61 ld->bits_left = 32;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
62
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
63 ld->bytes_used = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
64 ld->no_more_reading = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
65 ld->error = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
66 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
67
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
68 void faad_endbits(bitfile *ld)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
69 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
70 if (ld)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
71 if (ld->buffer) free(ld->buffer);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
72 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
73
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
74
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
75 uint32_t faad_get_processed_bits(bitfile *ld)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
76 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
77 return 8 * (4*(ld->tail - ld->start) - 4) - (ld->bits_left);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
78 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
79
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
80 uint8_t faad_byte_align(bitfile *ld)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
81 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
82 uint8_t remainder = (uint8_t)((32 - ld->bits_left) % 8);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
83
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
84 if (remainder)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
85 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
86 faad_flushbits(ld, 8 - remainder);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
87 return (8 - remainder);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
88 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
89 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
90 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
91
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
92 /* rewind to beginning */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
93 void faad_rewindbits(bitfile *ld)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
94 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
95 uint32_t tmp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
96
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
97 tmp = ld->start[0];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
98 #ifndef ARCH_IS_BIG_ENDIAN
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
99 BSWAP(tmp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
100 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
101 ld->bufa = tmp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
102
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
103 tmp = ld->start[1];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
104 #ifndef ARCH_IS_BIG_ENDIAN
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
105 BSWAP(tmp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
106 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
107 ld->bufb = tmp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
108 ld->bits_left = 32;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
109 ld->tail = &ld->start[2];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
110 ld->bytes_used = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
111 ld->no_more_reading = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
112 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
113
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
114 uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
115 DEBUGDEC)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
116 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
117 uint16_t i;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
118 uint8_t temp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
119 uint16_t bytes = (uint16_t)bits / 8;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
120 uint8_t remainder = (uint8_t)bits % 8;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
121
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
122 uint8_t *buffer = (uint8_t*)malloc((bytes+1)*sizeof(uint8_t));
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
123
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
124 for (i = 0; i < bytes; i++)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
125 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
126 buffer[i] = (uint8_t)faad_getbits(ld, 8 DEBUGVAR(print,var,dbg));
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
127 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
128
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
129 if (remainder)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
130 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
131 temp = (uint8_t)faad_getbits(ld, remainder DEBUGVAR(print,var,dbg)) << (8-remainder);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
132
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
133 buffer[bytes] = temp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
134 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
135
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
136 return buffer;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
137 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
138
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
139 /* reversed bit reading routines, used for RVLC and HCR */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
140 void faad_initbits_rev(bitfile *ld, void *buffer,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
141 uint32_t bits_in_buffer)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
142 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
143 uint32_t tmp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
144 int32_t index;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
145
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
146 ld->buffer_size = bit2byte(bits_in_buffer);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
147
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
148 index = (bits_in_buffer+31)/32 - 1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
149
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
150 ld->start = (uint32_t*)buffer + index - 2;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
151
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
152 tmp = getdword((uint32_t*)buffer + index);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
153 #ifndef ARCH_IS_BIG_ENDIAN
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
154 BSWAP(tmp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
155 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
156 ld->bufa = tmp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
157
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
158 tmp = getdword((uint32_t*)buffer + index - 1);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
159 #ifndef ARCH_IS_BIG_ENDIAN
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
160 BSWAP(tmp);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
161 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
162 ld->bufb = tmp;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
163
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
164 ld->tail = (uint32_t*)buffer + index;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
165
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
166 ld->bits_left = bits_in_buffer % 32;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
167 if (ld->bits_left == 0)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
168 ld->bits_left = 32;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
169
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
170 ld->bytes_used = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
171 ld->no_more_reading = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
172 ld->error = 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
173 }