huf.c
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
Huffman Decoding Unit Simulation Rich Webb 2/28/94
Version #1: rww 2/28/94
Basic table structure and stream parsing.
*/
#include <values.h>
#include <stdio.h>
#include "huf.h"
#include "huf_mpeg1_ac_tabs.h"
#include "huf_mpeg1_mbh_tabs.h"
int huf_start_tab = AC_START;
int huf_cur_tab = AC_FIRST;
HufControl **huf_tabs = &(huf_mpeg1_ac_tabs[0]);
#define HUF_READY (0)
#define HUF_STOPPED (1)
int huf_status = HUF_READY;
int huf_trace = 1;
int huf_skip_sign = 0;
unsigned int huf_fpeek( int );
unsigned int huf_fread( int );
unsigned int huf_peek( int );
unsigned int huf_read( int );
int huf_set_next_ac_tab = AC_FIRST;
void
huf_set_trace( int val )
{
huf_trace = val;
}
void
huf_set_ac_first( void )
{
huf_set_next_ac_tab = AC_FIRST;
}
void
huf_set_ac_start( void )
{
huf_set_next_ac_tab = AC_START;
}
int
huf_check_ac_first( void )
{
return( huf_set_next_ac_tab == AC_FIRST );
}
int
huf_set_state( int state )
{
}
#define NEXTBITS(x) ((bits>>(32-x)) & ((1<<x)-1))
#define BIT_BUFSIZE (1024*1024/(sizeof(BIT_BUFTYPE)))
FILE *huf_file;
char *huf_filename;
unsigned int
huf_fpeek( int len )
{
int nbits;
int val;
int tmp_cur_word;
int tmp_cur_bits;
tmp_cur_word = in_cur_word;
tmp_cur_bits = in_cur_bits;
if( (tmp_cur_bits + len) >= BITS(BIT_BUFTYPE) )
{
if( (in_cur_used+1) == in_buf_len )
return( -1 );
nbits = BITS(BIT_BUFTYPE) - tmp_cur_bits;
if( nbits == BITS(BIT_BUFTYPE) )
val = tmp_cur_word;
else
val = (tmp_cur_word & ((1<<nbits)-1)) << (len-nbits);
tmp_cur_word = in_bit_buf[in_cur_used];
len -= nbits;
if( len > 0 )
val |= (tmp_cur_word >> (BITS(BIT_BUFTYPE) - len))
& ((1<<len)-1);
} else
val = (tmp_cur_word >> (BITS(BIT_BUFTYPE) - tmp_cur_bits - len))
& ((1<<len)-1);
return( val );
}
int
huf_parse( void )
{
HufControl *tab;
HufControl cont;
int bits, out_val, level, run;
int nb = 0;
out_val = huf_fpeek( 20 );
run = 16;
if( out_val < 0 )
return( out_val );
do {
bits = (out_val >> run) & 0xf ;
if( bits < 0 )
return( bits );
tab = huf_tabs[ huf_cur_tab ];
cont = tab[bits];
if( (!cont.stop) || cont.end ) {
nb += cont.nbits + 1;
run -= cont.nbits + 1;
};
huf_cur_tab = cont.value;
} while( (cont.end == 0) && (cont.stop == 0) );
out_val = cont.value;
if( cont.stop ) {
if( cont.sign ) {
if( (huf_fread(nb+1)&1) )
out_val = -out_val;
} else
huf_fread(nb);
return( out_val );
};
level = out_val >> HUF_LEVEL_SHIFT;
run = out_val & ((1<<HUF_LEVEL_SHIFT) - 1);
if( cont.sign ) {
if( (huf_fread(nb+1)&1) )
level = -level;
} else
huf_fread(nb);
huf_cur_tab = huf_start_tab;
return( (level<<16) + run );
}
int
huf_align( int len )
{
int val = 0;
if( (len = ((BITS(BIT_BUFTYPE)-in_cur_bits)%len)) )
val = huf_read( len );
if( val == 0 )
return( -1-len );
for(len=0;len<32;len++)
if( val & (1<<len) )
return(len);
return(32);
}
#include <string.h>
#include <malloc.h>
char *strdup( char * );
int in_cur_word=0,
in_buf_len=0;
int in_cur_bits=0,
in_cur_used=0;
BIT_BUFTYPE *in_bit_buf = (BIT_BUFTYPE *) NULL;
huf_init( char *filename )
{
huf_filename = strdup( filename );
huf_file = fopen( filename, "r" );
in_bit_buf = (BIT_BUFTYPE *) malloc( BIT_BUFSIZE * sizeof(BIT_BUFTYPE));
in_buf_len = fread( in_bit_buf, sizeof(BIT_BUFTYPE), BIT_BUFSIZE,
huf_file);
in_cur_bits = 0;
in_cur_used = 0;
in_cur_word = in_bit_buf[in_cur_used++];
if( huf_trace > 1 )
fprintf(stderr, "HUF_INIT: Filling buffer with %d bits\n",
in_buf_len*BITS(BIT_BUFTYPE) );
}
/* BITwise I/O routines */
unsigned int
huf_peek( int len )
{
int nbits;
int val;
char *b2s( int, int );
int tmp_cur_word;
int tmp_cur_bits;
if( len == 0 )
{
fprintf(stderr,"HUF_PEEK: Tried to input %d bits\n", len);
return( 0 );
};
if( len > BITS(BIT_BUFTYPE) )
{
fprintf(stderr,"HUF_PEEK: Tried to input %d bits (max=%d)\n",
len, BITS(BIT_BUFTYPE) );
return( 0 );
};
if( in_bit_buf == NULL )
{
fprintf(stderr,"HUF_PEEK: Don't call this first!\n" );
return( -1 );
};
val = 0;
tmp_cur_word = in_cur_word;
tmp_cur_bits = in_cur_bits;
while( (tmp_cur_bits + len) >= BITS(BIT_BUFTYPE) )
{
nbits = BITS(BIT_BUFTYPE) - tmp_cur_bits;
if( nbits == BITS(BIT_BUFTYPE) )
val = tmp_cur_word;
else
val = (tmp_cur_word & ((1<<nbits)-1)) << (len-nbits);
tmp_cur_word = in_bit_buf[in_cur_used];
tmp_cur_bits = 0;
len -= nbits;
if( (in_cur_used+1) == in_buf_len )
return( -1 );
};
if( len > 0 )
val |= (tmp_cur_word >> (BITS(BIT_BUFTYPE) - tmp_cur_bits - len))
& ((1<<len)-1);
return( val );
}
unsigned int
huf_fread( int len )
{
int nbits, ilen;
int val;
char *b2s(int, int);
if( huf_trace )
fprintf(stderr,"HGET: Recv <");
ilen = 0;
val = 0;
while( (in_cur_bits + len) >= BITS(BIT_BUFTYPE) )
{
nbits = BITS(BIT_BUFTYPE) - in_cur_bits;
if( nbits == BITS(BIT_BUFTYPE) )
val = in_cur_word;
else
val = (in_cur_word & ((1<<nbits)-1)) << (len-nbits);
if( huf_trace )
fprintf(stderr,"%s%s", b2s((val>>(len-nbits)),nbits),
(len-nbits)?" | ":">");
in_cur_word = in_bit_buf[in_cur_used++];
in_cur_bits = 0;
len -= nbits;
ilen += nbits;
if( in_cur_used == in_buf_len )
{
in_buf_len = fread( in_bit_buf, sizeof(BIT_BUFTYPE),
BIT_BUFSIZE, huf_file);
if( in_buf_len == 0 )
{
free( in_bit_buf );
in_bit_buf = NULL;
fprintf(stderr,"HUF Input EOF <%s>\n",
huf_filename );
return(-1);
};
in_cur_used = 0;
/* in_cur_word = in_bit_buf[in_cur_used++]; */
/* huf_trace = trace_out = trace_bs = 1; */
if( huf_trace > 1 )
fprintf(stderr, "HGET: Filling buffer with %d bits\n",
in_buf_len*BITS(BIT_BUFTYPE) );
};
};
if( len > 0 )
{
val |= (in_cur_word >> (BITS(BIT_BUFTYPE) - in_cur_bits - len))
& ((1<<len)-1);
in_cur_bits += len;
ilen += len;
if( huf_trace )
fprintf(stderr,"%s>", b2s(val,len));
};
if( huf_trace )
fprintf(stderr," (val=0x%x,len=%d)\n", val, ilen );
return( val );
}
unsigned int
huf_read( int len )
{
extern FILE *huf_file;
int nbits, ilen;
int val;
char *b2s(int, int);
if( len == 0 )
{
fprintf(stderr,"HGET: Tried to input %d bits\n", len);
return( 0 );
};
if( len > BITS(BIT_BUFTYPE) )
{
fprintf(stderr,"HGET: Tried to input %d bits (max=%d)\n",
len, BITS(BIT_BUFTYPE) );
return( 0 );
};
if( in_bit_buf == NULL )
{
in_bit_buf = (BIT_BUFTYPE *) malloc( BIT_BUFSIZE * sizeof(BIT_BUFTYPE));
in_buf_len = fread( in_bit_buf, sizeof(BIT_BUFTYPE), BIT_BUFSIZE,
huf_file);
in_cur_bits = 0;
in_cur_used = 0;
in_cur_word = in_bit_buf[in_cur_used++];
if( huf_trace > 1 )
fprintf(stderr, "HGET: Filling buffer with %d bits\n",
in_buf_len*BITS(BIT_BUFTYPE) );
}
if( huf_trace )
fprintf(stderr,"HGET: Recv <");
ilen = 0;
val = 0;
while( (in_cur_bits + len) >= BITS(BIT_BUFTYPE) )
{
nbits = BITS(BIT_BUFTYPE) - in_cur_bits;
if( nbits == BITS(BIT_BUFTYPE) )
val = in_cur_word;
else
val = (in_cur_word & ((1<<nbits)-1)) << (len-nbits);
in_cur_word = in_bit_buf[in_cur_used++];
in_cur_bits = 0;
if( huf_trace )
fprintf(stderr,"%s%s", b2s((val>>(len-nbits)),nbits),
(len-nbits)?" | ":">");
len -= nbits;
ilen += nbits;
if( in_cur_used == in_buf_len )
{
in_buf_len = fread( in_bit_buf, sizeof(BIT_BUFTYPE),
BIT_BUFSIZE, huf_file);
if( in_buf_len == 0 )
{
free( in_bit_buf );
in_bit_buf = NULL;
fprintf(stderr,"HUF Input EOF <%s>\n",
huf_filename );
return(-1);
};
in_cur_used = 0;
/* in_cur_word = in_bit_buf[in_cur_used++]; */
/* huf_trace = trace_out = trace_bs = 1; */
if( huf_trace > 1 )
fprintf(stderr, "HGET: Filling buffer with %d bits\n",
in_buf_len*BITS(BIT_BUFTYPE) );
};
};
if( len > 0 )
{
val |= (in_cur_word >> (BITS(BIT_BUFTYPE) - in_cur_bits - len))
& ((1<<len)-1);
in_cur_bits += len;
ilen += len;
if( huf_trace )
fprintf(stderr,"%s>", b2s(val,len));
};
if( huf_trace )
fprintf(stderr," (val=0x%x,len=%d)\n", val, ilen );
return( val );
}
char *
b2s( int val, int len )
{
static char buf[150];
unsigned int mask;
int i;
mask = 1<<(len-1);
for(i=0;i<len;i++,mask>>=1)
buf[i] = (val & mask)?'1':'0';
buf[len] = '\0';
return( buf );
}
#if 0
int
huf_slow_parse()
{
HufControl *tab;
HufControl cont;
int bits, out_val, level, run;
do {
bits = huf_peek( 4 );
if( bits < 0 )
return( bits );
tab = huf_tabs[ huf_cur_tab ];
cont = tab[bits];
if( (cont.stop) && (cont.end == 0) ) {
if( huf_trace )
fprintf(stderr,"HUF_PARSE: Don't consume bits on error %d\n",
cont.value );
} else {
bits = huf_read( cont.nbits + 1 );
if( bits < 0 )
return( bits );
};
huf_cur_tab = cont.value;
} while( (cont.end == 0) && (cont.stop == 0) );
out_val = cont.value;
if( cont.stop ) {
huf_status = HUF_STOPPED;
if( huf_skip_sign == 0 )
if( cont.sign && huf_read(1) )
out_val = -out_val;
return( out_val );
};
level = out_val >> HUF_LEVEL_SHIFT;
run = out_val & ((1<<HUF_LEVEL_SHIFT) - 1);
if( huf_skip_sign == 0 )
if( cont.sign && huf_read(1) )
level = -level;
huf_cur_tab = huf_start_tab;
return( (level<<16) + run );
}
int
huf_slower_parse()
{
HufControl *tab;
HufControl cont;
int bits, out_val, level, run;
int nb;
int fbits;
fbits = huf_fpeek( 31 );
if( fbits < 0 )
return( fbits );
fbits <<=1;
nb = 0;
do {
bits = fbits>>28;
bits &= 0xf;
tab = huf_tabs[ huf_cur_tab ];
cont = tab[bits];
if( (cont.stop) && (cont.end == 0) ) {
if( huf_trace )
fprintf(stderr,"HUF_PARSE: Don't consume bits on error %d\n",
cont.value );
} else {
fbits <<= cont.nbits + 1;
nb += cont.nbits + 1;
};
huf_cur_tab = cont.value;
} while( (cont.end == 0) && (cont.stop == 0) );
out_val = cont.value;
if( cont.stop ) {
huf_status = HUF_STOPPED;
if( huf_skip_sign == 0 )
if( cont.sign ) {
nb++;
if( fbits < 0 )
out_val = -out_val;
};
huf_fread( nb );
return( out_val );
};
level = out_val >> HUF_LEVEL_SHIFT;
run = out_val & ((1<<HUF_LEVEL_SHIFT) - 1);
if( huf_skip_sign == 0 ) {
nb++;
if( cont.sign && (fbits<0) )
level = -level;
};
huf_cur_tab = huf_start_tab;
huf_fread( nb );
return( (level<<16) + run );
}
#endif