1 /* this file is a part of amp software, (C) tomislav uzelac 1996,1997
2 */
3
4 /* huffman.c huffman decoding
5 *
6 * Created by: tomislav uzelac Mar,Apr 1996
7 * Last modified by: tomislav uzelac Mar 8 97
8 */
9 #include "audio.h"
10 #include "getbits.h"
11
12 #define HUFFMAN
13 #include "huffman.h"
14
viewbits(int n)15 static inline unsigned int viewbits(int n)
16 {
17 unsigned int pos,ret_value;
18
19 pos = data >> 3;
20 ret_value = buffer[pos] << 24 |
21 buffer[pos+1] << 16 |
22 buffer[pos+2] << 8 |
23 buffer[pos+3];
24 ret_value <<= data & 7;
25 ret_value >>= 32 - n;
26
27 return ret_value;
28 }
29
sackbits(int n)30 static inline void sackbits(int n)
31 {
32 data += n;
33 data &= 8*BUFFER_SIZE-1;
34 }
35
36 /* huffman_decode() is supposed to be faster now
37 * decodes one codeword and returns no. of bits
38 */
huffman_decode(int tbl,int * x,int * y)39 static inline int huffman_decode(int tbl,int *x,int *y)
40 {
41 unsigned int chunk;
42 register unsigned int *h_tab;
43 register unsigned int lag = 0;
44 register unsigned int half_lag;
45 int len;
46
47 h_tab=tables[tbl];
48 chunk=viewbits(19);
49
50 h_tab += h_cue[tbl][chunk >> (19-NC_O)];
51
52 len=(*h_tab>>8)&0x1f;
53
54 /* check for an immediate hit, so we can decode those short codes very fast
55 */
56 if ((*h_tab>>(32-len)) != (chunk>>(19-len))) {
57 if (chunk >> (19-NC_O) < N_CUE-1)
58 lag=(h_cue[tbl][(chunk >> (19-NC_O))+1] -
59 h_cue[tbl][chunk >> (19-NC_O)]);
60 else {
61 /* we strongly depend on h_cue[N_CUE-1] to point to
62 * the last entry in the huffman table, so we should
63 * noT get here anyway. if it didn't, we'd have to
64 * have another table with huffman tables lengths, and
65 * it would be a mess. just in case, scream&shout.
66 */
67 /*printf(" h_cue clobbered. this is a bug. blip.\n");*/
68 _exit (-1);
69 }
70 chunk <<= 32-19;
71 chunk |= 0x1ff;
72
73 half_lag = lag >> 1;
74
75 h_tab += half_lag;
76 lag -= half_lag;
77
78 while (lag > 1) {
79 half_lag = lag >> 1;
80
81 if (*h_tab < chunk)
82 h_tab += half_lag;
83 else
84 h_tab -= half_lag;
85
86 lag -= half_lag;
87 }
88
89 len=(*h_tab>>8)&0x1f;
90 if ((*h_tab>>(32-len)) != (chunk>>(32-len))) {
91 if (*h_tab > chunk)
92 h_tab--;
93 else
94 h_tab++;
95
96 len=(*h_tab>>8)&0x1f;
97 }
98 }
99 sackbits(len);
100 *x=(*h_tab>>4)&0xf;
101 *y=*h_tab&0xf;
102 return len;
103 }
104
_qsign(int x,int * q)105 static inline int _qsign(int x,int *q)
106 {
107 int ret_value=0,i;
108 for (i=3;i>=0;i--)
109 if ((x>>i) & 1) {
110 if (getbits(1)) *q++=-1;
111 else *q++=1;
112 ret_value++;
113 }
114 else *q++=0;
115 return ret_value;
116 }
117
decode_huffman_data(struct SIDE_INFO * info,int gr,int ch,int ssize)118 int decode_huffman_data(struct SIDE_INFO *info,int gr,int ch,int ssize)
119 {
120 int l,i,cnt,x,y;
121 int q[4],r[3],linbits[3],tr[4]={0,0,0,0};
122 int big_value = info->big_values[gr][ch] << 1;
123
124 for (l=0;l<3;l++) {
125 tr[l]=info->table_select[gr][ch][l];
126 linbits[l]=t_linbits[info->table_select[gr][ch][l]];
127 }
128
129 tr[3]=32+info->count1table_select[gr][ch];
130
131 /* we have to be careful here because big_values are not necessarily
132 * aligned with sfb boundaries
133 */
134 if (!info->window_switching_flag[gr][ch] && info->block_type[gr][ch]==0) {
135
136 /* this code needed some cleanup
137 */
138 r[0]=t_l[info->region0_count[gr][ch]] + 1;
139 if (r[0] > big_value)
140 r[0]=r[1]=big_value;
141 else {
142 r[1]=t_l[ info->region0_count[gr][ch] + info->region1_count[gr][ch] + 1 ] + 1;
143 if (r[1] > big_value)
144 r[1]=big_value;
145 }
146 r[2]=big_value;
147
148 } else {
149
150 if (info->block_type[gr][ch]==2 && info->mixed_block_flag[gr][ch]==0)
151 r[0]=3*(t_s[2]+1);
152 else
153 r[0]=t_l[7]+1;
154
155 if (r[0] > big_value)
156 r[0]=big_value;
157
158 r[1]=r[2]=big_value;
159 }
160
161 l=0; cnt=0;
162 for (i=0;i<3;i++) {
163 for (;l<r[i];l+=2) {
164 int j = linbits[i];
165
166 cnt+=huffman_decode(tr[i],&x,&y);
167
168 if (x==15 && j>0) {
169 x+=getbits(j);
170 cnt+=j;
171 }
172 if (x) {
173 if (getbits(1)) x=-x;
174 cnt++;
175 }
176 if (y==15 && j>0) {
177 y+=getbits(j);
178 cnt+=j;
179 }
180 if (y) {
181 if (getbits(1)) y=-y;
182 cnt++;
183 }
184
185 is[ch][l]=x;
186 is[ch][l+1]=y;
187 }
188 }
189 while ((cnt < info->part2_3_length[gr][ch]-ssize) && (l<576)) {
190 cnt+=huffman_decode(tr[3],&x,&y);
191 cnt+=_qsign(x,q);
192 for (i=0;i<4;i++) is[ch][l+i]=q[i]; /* ziher je ziher, is[578]*/
193 l+=4;
194 }
195
196 /* set position to start of the next gr/ch
197 */
198 if (cnt != info->part2_3_length[gr][ch] - ssize ) {
199 data-=cnt-(info->part2_3_length[gr][ch] - ssize);
200 data&= 8*BUFFER_SIZE - 1;
201 }
202 if (l<576) non_zero[ch]=l;
203 else non_zero[ch]=576;
204 /* zero out everything else
205 */
206 for (;l<576;l++) is[ch][l]=0;
207 return 1;
208 }
209