comparison libfaad2/hcr.c @ 10989:3185f64f6350

synced with current cvs
author alex
date Fri, 03 Oct 2003 22:23:26 +0000
parents e989150f8216
children 4a370c80fe5c
comparison
equal deleted inserted replaced
10988:c2bff70784d5 10989:3185f64f6350
20 ** forbidden. 20 ** forbidden.
21 ** 21 **
22 ** Commercial non-GPL licensing of this software is possible. 22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. 23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24 ** 24 **
25 ** $Id: hcr.c,v 1.5 2003/07/29 08:20:12 menno Exp $ 25 ** $Id: hcr.c,v 1.1 2003/08/30 22:30:21 arpi Exp $
26 **/ 26 **/
27 27
28 28
29 #include "common.h" 29 #include "common.h"
30 #include "structs.h" 30 #include "structs.h"
36 #include "specrec.h" 36 #include "specrec.h"
37 #include "bits.h" 37 #include "bits.h"
38 #include "pulse.h" 38 #include "pulse.h"
39 #include "analysis.h" 39 #include "analysis.h"
40 #include "bits.h" 40 #include "bits.h"
41 #include "codebook/hcb.h" 41 #include "huffman.h"
42 42
43 /* Implements the HCR11 tool as described in ISO/IEC 14496-3/Amd.1, 8.5.3.3 */ 43 /* Implements the HCR11 tool as described in ISO/IEC 14496-3/Amd.1, 8.5.3.3 */
44 44
45 #ifdef ERROR_RESILIENCE 45 #ifdef ERROR_RESILIENCE
46
47 typedef struct
48 {
49 /* bit input */
50 uint32_t bufa;
51 uint32_t bufb;
52 int8_t len;
53 } bits_t;
54
55
56 static INLINE uint32_t showbits(bits_t *ld, uint8_t bits)
57 {
58 if (bits == 0) return 0;
59 if (ld->len <= 32){
60 /* huffman_spectral_data_2 needs to read more than may be available, bits maybe
61 > ld->len, deliver 0 than */
62 if (ld->len >= bits)
63 return ((ld->bufa >> (ld->len - bits)) & (0xFFFFFFFF >> (32 - bits)));
64 else
65 return ((ld->bufa << (bits - ld->len)) & (0xFFFFFFFF >> (32 - bits)));
66 } else {
67 if ((ld->len - bits) < 32)
68 {
69 return ( (ld->bufb & (0xFFFFFFFF >> (64 - ld->len))) << (bits - ld->len + 32)) |
70 (ld->bufa >> (ld->len - bits));
71 } else {
72 return ((ld->bufb >> (ld->len - bits - 32)) & (0xFFFFFFFF >> (32 - bits)));
73 }
74 }
75 }
76
77 /* return 1 if position is outside of buffer, 0 otherwise */
78 static INLINE int8_t flushbits( bits_t *ld, uint8_t bits)
79 {
80 ld->len -= bits;
81
82 if (ld->len <0)
83 {
84 ld->len = 0;
85 return 1;
86 } else {
87 return 0;
88 }
89 }
90
91
92 static INLINE int8_t getbits(bits_t *ld, uint8_t n, uint32_t *result)
93 {
94 *result = showbits(ld, n);
95 return flushbits(ld, n);
96 }
97
98 static INLINE int8_t get1bit(bits_t *ld, uint8_t *result)
99 {
100 uint32_t res;
101 int8_t ret;
102
103 ret = getbits(ld, 1, &res);
104 *result = (int8_t)(res & 1);
105 return ret;
106 }
107
108 /* Special version of huffman_spectral_data adapted from huffman.h
109 Will not read from a bitfile but a bits_t structure.
110 Will keep track of the bits decoded and return the number of bits remaining.
111 Do not read more than ld->len, return -1 if codeword would be longer */
112
113 static int8_t huffman_spectral_data_2(uint8_t cb, bits_t *ld, int16_t *sp )
114 {
115 uint32_t cw;
116 uint16_t offset = 0;
117 uint8_t extra_bits;
118 uint8_t i;
119 uint8_t save_cb = cb;
120
121
122 switch (cb)
123 {
124 case 1: /* 2-step method for data quadruples */
125 case 2:
126 case 4:
127
128 cw = showbits(ld, hcbN[cb]);
129 offset = hcb_table[cb][cw].offset;
130 extra_bits = hcb_table[cb][cw].extra_bits;
131
132 if (extra_bits)
133 {
134 /* we know for sure it's more than hcbN[cb] bits long */
135 if ( flushbits(ld, hcbN[cb]) ) return -1;
136 offset += (uint16_t)showbits(ld, extra_bits);
137 if ( flushbits(ld, hcb_2_quad_table[cb][offset].bits - hcbN[cb]) ) return -1;
138 } else {
139 if ( flushbits(ld, hcb_2_quad_table[cb][offset].bits) ) return -1;
140 }
141
142 sp[0] = hcb_2_quad_table[cb][offset].x;
143 sp[1] = hcb_2_quad_table[cb][offset].y;
144 sp[2] = hcb_2_quad_table[cb][offset].v;
145 sp[3] = hcb_2_quad_table[cb][offset].w;
146 break;
147
148 case 6: /* 2-step method for data pairs */
149 case 8:
150 case 10:
151 case 11:
152 /* VCB11 uses codebook 11 */
153 case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
154 case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
155
156 /* TODO: If ER is used, some extra error checking should be done */
157 if (cb >= 16)
158 cb = 11;
159
160 cw = showbits(ld, hcbN[cb]);
161 offset = hcb_table[cb][cw].offset;
162 extra_bits = hcb_table[cb][cw].extra_bits;
163
164 if (extra_bits)
165 {
166 /* we know for sure it's more than hcbN[cb] bits long */
167 if ( flushbits(ld, hcbN[cb]) ) return -1;
168 offset += (uint16_t)showbits(ld, extra_bits);
169 if ( flushbits(ld, hcb_2_pair_table[cb][offset].bits - hcbN[cb]) ) return -1;
170 } else {
171 if ( flushbits(ld, hcb_2_pair_table[cb][offset].bits) ) return -1;
172 }
173 sp[0] = hcb_2_pair_table[cb][offset].x;
174 sp[1] = hcb_2_pair_table[cb][offset].y;
175 break;
176
177 case 3: /* binary search for data quadruples */
178
179 while (!hcb3[offset].is_leaf)
180 {
181 uint8_t b;
182
183 if ( get1bit(ld, &b) ) return -1;
184 offset += hcb3[offset].data[b];
185 }
186
187 sp[0] = hcb3[offset].data[0];
188 sp[1] = hcb3[offset].data[1];
189 sp[2] = hcb3[offset].data[2];
190 sp[3] = hcb3[offset].data[3];
191
192 break;
193
194 case 5: /* binary search for data pairs */
195 case 7:
196 case 9:
197
198 while (!hcb_bin_table[cb][offset].is_leaf)
199 {
200 uint8_t b;
201
202 if (get1bit(ld, &b) ) return -1;
203 offset += hcb_bin_table[cb][offset].data[b];
204 }
205
206 sp[0] = hcb_bin_table[cb][offset].data[0];
207 sp[1] = hcb_bin_table[cb][offset].data[1];
208
209 break;
210 }
211
212 /* decode sign bits */
213 if (unsigned_cb[cb]) {
214
215 for(i = 0; i < ((cb < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN); i++)
216 {
217 if(sp[i])
218 {
219 uint8_t b;
220 if ( get1bit(ld, &b) ) return -1;
221 if (b != 0) {
222 sp[i] = -sp[i];
223 }
224 }
225 }
226 }
227
228 /* decode huffman escape bits */
229 if ((cb == ESC_HCB) || (cb >= 16))
230 {
231 uint8_t k;
232 for (k = 0; k < 2; k++)
233 {
234 if ((sp[k] == 16) || (sp[k] == -16))
235 {
236 uint8_t neg, i;
237 int32_t j;
238 uint32_t off;
239
240 neg = (sp[k] < 0) ? 1 : 0;
241
242 for (i = 4; ; i++)
243 {
244 uint8_t b;
245 if (get1bit(ld, &b))
246 return -1;
247 if (b == 0)
248 break;
249 }
250 // TODO: here we would need to test "off" if VCB11 is used!
251 if (getbits(ld, i, &off))
252 return -1;
253 j = off + (1<<i);
254 sp[k] = (int16_t)((neg) ? -j : j);
255 }
256 }
257 }
258 return ld->len;
259 }
260 46
261 /* rewind len (max. 32) bits so that the MSB becomes LSB */ 47 /* rewind len (max. 32) bits so that the MSB becomes LSB */
262 48
263 static uint32_t rewind_word( uint32_t W, uint8_t len) 49 static uint32_t rewind_word( uint32_t W, uint8_t len)
264 { 50 {
302 rewind_lword( &hw, &lw, r->len ); 88 rewind_lword( &hw, &lw, r->len );
303 r->bufa = lw; 89 r->bufa = lw;
304 r->bufb = hw; 90 r->bufb = hw;
305 91
306 } else { 92 } else {
307 lw = showbits(r, r->len ); 93 lw = showbits_hcr(r, r->len );
308 r->bufa = rewind_word( lw, r->len); 94 r->bufa = rewind_word( lw, r->len);
309 r->bufb = 0; 95 r->bufb = 0;
310 } 96 }
311 } 97 }
312 98
320 if (a->len >32) 106 if (a->len >32)
321 { 107 {
322 lwa = a->bufa; 108 lwa = a->bufa;
323 hwa = a->bufb & (0xFFFFFFFF >> (64 - a->len)); 109 hwa = a->bufb & (0xFFFFFFFF >> (64 - a->len));
324 } else { 110 } else {
325 lwa = showbits(a, a->len ); 111 lwa = showbits_hcr(a, a->len );
326 hwa = 0; 112 hwa = 0;
327 } 113 }
328 if (b->len >=32) { 114 if (b->len >=32) {
329 lwb = b->bufa; 115 lwb = b->bufa;
330 hwb = (b->bufb & (0xFFFFFFFF >> (64 - b->len)) ) | ( lwa << (b->len - 32)); 116 hwb = (b->bufb & (0xFFFFFFFF >> (64 - b->len)) ) | ( lwa << (b->len - 32));
331 } else { 117 } else {
332 lwb = showbits(b, b->len ) | (lwa << (b->len)); 118 lwb = showbits_hcr(b, b->len ) | (lwa << (b->len));
333 hwb = (lwa >> (32 - b->len)) | (hwa << (b->len)); 119 hwb = (lwa >> (32 - b->len)) | (hwa << (b->len));
334 } 120 }
335 121
336 b->bufa = lwb; 122 b->bufa = lwb;
337 b->bufb = hwb; 123 b->bufb = hwb;
491 } 277 }
492 rewind_lword( &hw, &lw, additional_bits + Segment[ numberOfSegments-1 ].len ); 278 rewind_lword( &hw, &lw, additional_bits + Segment[ numberOfSegments-1 ].len );
493 if (Segment[ numberOfSegments-1 ].len > 32) 279 if (Segment[ numberOfSegments-1 ].len > 32)
494 { 280 {
495 Segment[ numberOfSegments-1 ].bufb = hw + 281 Segment[ numberOfSegments-1 ].bufb = hw +
496 showbits(&Segment[ numberOfSegments-1 ], Segment[ numberOfSegments-1 ].len - 32); 282 showbits_hcr(&Segment[ numberOfSegments-1 ], Segment[ numberOfSegments-1 ].len - 32);
497 Segment[ numberOfSegments-1 ].bufa = lw + 283 Segment[ numberOfSegments-1 ].bufa = lw +
498 showbits(&Segment[ numberOfSegments-1 ], 32); 284 showbits_hcr(&Segment[ numberOfSegments-1 ], 32);
499 } else { 285 } else {
500 Segment[ numberOfSegments-1 ].bufa = lw + 286 Segment[ numberOfSegments-1 ].bufa = lw +
501 showbits(&Segment[ numberOfSegments-1 ], Segment[ numberOfSegments-1 ].len); 287 showbits_hcr(&Segment[ numberOfSegments-1 ], Segment[ numberOfSegments-1 ].len);
502 Segment[ numberOfSegments-1 ].bufb = hw; 288 Segment[ numberOfSegments-1 ].bufb = hw;
503 } 289 }
504 Segment[ numberOfSegments-1 ].len += additional_bits; 290 Segment[ numberOfSegments-1 ].len += additional_bits;
505 } 291 }
506 BitsRead = ics->length_of_reordered_spectral_data; 292 BitsRead = ics->length_of_reordered_spectral_data;