forked from jp9000/OBS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.c
1226 lines (1114 loc) · 43.9 KB
/
huffman.c
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********
This software module was originally developed by Dolby
Laboratories and edited by Sony Corporation
in the course of development of the MPEG-2 NBC/MPEG-4
Audio standard ISO/IEC13818-7, 14496-1, 2 and 3. This software module is an
implementation of a part of one or more MPEG-2 NBC/MPEG-4 Audio tools as
specified by the MPEG-2 NBC/MPEG-4 Audio standard. ISO/IEC gives users of the
MPEG-2NBC/MPEG-4 Audio standards free license to this software module
or modifications thereof for use in hardware or software products
claiming conformance to the MPEG-2 NBC/MPEG-4 Audio standards. Those
intending to use this software module in hardware or software products
are advised that this use may infringe existing patents. The original
developer of this software module, the subsequent
editors and their companies, and ISO/IEC have no liability for use of
this software module or modifications thereof in an
implementation. Copyright is not released for non MPEG-2 NBC/MPEG-4
Audio conforming products. The original developer retains full right to
use the code for the developer's own purpose, assign or donate the code to a
third party and to inhibit third party from using the code for non
MPEG-2 NBC/MPEG-4 Audio conforming products. This copyright notice
must be included in all copies or derivative works. Copyright 1996.
***********/
/*
* $Id: huffman.c,v 1.11 2005/02/02 07:53:20 sur Exp $
*/
#include <math.h>
#include <stdlib.h>
#include "huffman.h"
#include "coder.h"
#include "bitstream.h"
#include "util.h"
#include "hufftab.h"
void HuffmanInit(CoderInfo *coderInfo, unsigned int numChannels)
{
unsigned int channel;
for (channel = 0; channel < numChannels; channel++) {
coderInfo[channel].data = (int*)AllocMemory(5*FRAME_LEN*sizeof(int));
coderInfo[channel].len = (int*)AllocMemory(5*FRAME_LEN*sizeof(int));
#ifdef DRM
coderInfo[channel].num_data_cw = (int*)AllocMemory(FRAME_LEN*sizeof(int));
#endif
}
}
void HuffmanEnd(CoderInfo *coderInfo, unsigned int numChannels)
{
unsigned int channel;
for (channel = 0; channel < numChannels; channel++) {
if (coderInfo[channel].data) FreeMemory(coderInfo[channel].data);
if (coderInfo[channel].len) FreeMemory(coderInfo[channel].len);
#ifdef DRM
if (coderInfo[channel].num_data_cw) FreeMemory(coderInfo[channel].num_data_cw);
#endif
}
}
int BitSearch(CoderInfo *coderInfo,
int *quant) /* Quantized spectral values */
/*
This function inputs a vector of quantized spectral data, quant[][], and returns a vector,
'book_vector[]' that describes how to group together the scalefactor bands into a smaller
number of sections. There are MAX_SCFAC_BANDS elements in book_vector (equal to 49 in the
case of long blocks and 112 for short blocks), and each element has a huffman codebook
number assigned to it.
For a quick and simple algorithm, this function performs a binary
search across the sfb's (scale factor bands). On the first approach, it calculates the
needed amount of bits if every sfb were its own section and transmitted its own huffman
codebook value side information (equal to 9 bits for a long block, 7 for a short). The
next iteration combines adjacent sfb's, and calculates the bit rate for length two sfb
sections. If any wider two-sfb section requires fewer bits than the sum of the two
single-sfb sections (below it in the binary tree), then the wider section will be chosen.
This process occurs until the sections are split into three uniform parts, each with an
equal amount of sfb's contained.
The binary tree is stored as a two-dimensional array. Since this tree is not full, (there
are only 49 nodes, not 2^6 = 64), the numbering is a little complicated. If the tree were
full, the top node would be 1. It's children would be 2 and 3. But, since this tree
is not full, the top row of three nodes are numbered {4,5,6}. The row below it is
{8,9,10,11,12,13}, and so on.
The binary tree is called bit_stats[112][3]. There are 112 total nodes (some are not
used since it's not full). bit_stats[x][0] holds the bit totals needed for the sfb sectioning
strategy represented by the node x in the tree. bit_stats[x][1] holds the optimal huffman
codebook table that minimizes the bit rate, given the sectioning boundaries dictated by node x.
*/
{
int i,j,k;
int hop;
int min_book_choice[112][3];
int bit_stats[240][3];
int total_bit_count;
int levels;
int pow2levels;
int fracpow2lev;
/* Set local pointer to coderInfo book_vector */
int* book_vector = coderInfo -> book_vector;
levels = (int) ((log((double)coderInfo->nr_of_sfb)/log((double)2.0))+1);
/* #define SLOW */
#ifdef SLOW
for(i = 0; i < 5; i++) {
#else
i = 0;
#endif
hop = 1 << i;
NoiselessBitCount(coderInfo, quant, hop, min_book_choice);
/* load up the (not-full) binary search tree with the min_book_choice values */
k=0;
total_bit_count = 0;
pow2levels = 1 << (levels - i);
fracpow2lev = pow2levels + (coderInfo->nr_of_sfb >> i);
for (j=pow2levels; j < fracpow2lev; j++)
{
bit_stats[j][0] = min_book_choice[k][0]; /* the minimum bit cost for this section */
bit_stats[j][1] = min_book_choice[k][1]; /* used with this huffman book number */
#ifdef SLOW
if (i>0){ /* not on the lowest level, grouping more than one signle scalefactor band per section*/
if (bit_stats[j][0] < bit_stats[2*j][0] + bit_stats[2*j+1][0]){
/* it is cheaper to combine surrounding sfb secionts into one larger huffman book section */
for(n=k;n<k+hop;n++) { /* write the optimal huffman book value for the new larger section */
if ( (book_vector[n]!=INTENSITY_HCB)&&(book_vector[n]!=INTENSITY_HCB2) ) { /* Don't merge with IS bands */
book_vector[n] = bit_stats[j][1];
}
}
} else { /* it was cheaper to transmit the smaller huffman table sections */
bit_stats[j][0] = bit_stats[2*j][0] + bit_stats[2*j+1][0];
}
} else
#endif
{ /* during the first stage of the iteration, all sfb's are individual sections */
if ( (book_vector[k]!=INTENSITY_HCB)&&(book_vector[k]!=INTENSITY_HCB2) ) {
book_vector[k] = bit_stats[j][1]; /* initially, set all sfb's to their own optimal section table values */
}
}
total_bit_count = total_bit_count + bit_stats[j][0];
k=k+hop;
}
#ifdef SLOW
}
#endif
/* book_vector[k] = book_vector[k-1]; */
return(total_bit_count);
}
int NoiselessBitCount(CoderInfo *coderInfo,
int *quant,
int hop,
int min_book_choice[112][3])
{
int i,j,k;
/*
This function inputs:
- the quantized spectral data, 'quant[]';
- all of the huffman codebooks, 'huff[][]';
- the size of the sections, in scalefactor bands (SFB's), 'hop';
- an empty matrix, min_book_choice[][] passed to it;
This function outputs:
- the matrix, min_book_choice. It is a two dimensional matrix, with its
rows corresponding to spectral sections. The 0th column corresponds to
the bits needed to code a section with 'hop' scalefactors bands wide, all using
the same huffman codebook. The 1st column contains the huffman codebook number
that allows the minimum number of bits to be used.
Other notes:
- Initally, the dynamic range is calculated for each spectral section. The section
can only be entropy coded with books that have an equal or greater dynamic range
than the section's spectral data. The exception to this is for the 11th ESC codebook.
If the dynamic range is larger than 16, then an escape code is appended after the
table 11 codeword which encodes the larger value explicity in a pseudo-non-uniform
quantization method.
*/
int max_sb_coeff;
int book_choice[12][2];
int total_bits_cost = 0;
int offset, length, end;
int q;
/* set local pointer to sfb_offset */
int *sfb_offset = coderInfo->sfb_offset;
int nr_of_sfb = coderInfo->nr_of_sfb;
/* each section is 'hop' scalefactor bands wide */
for (i=0; i < nr_of_sfb; i=i+hop){
#ifdef SLOW
if ((i+hop) > nr_of_sfb)
q = nr_of_sfb;
else
#endif
q = i+hop;
{
/* find the maximum absolute value in the current spectral section, to see what tables are available to use */
max_sb_coeff = 0;
for (j=sfb_offset[i]; j<sfb_offset[q]; j++){ /* snl */
if (ABS(quant[j]) > max_sb_coeff)
max_sb_coeff = ABS(quant[j]);
}
j = 0;
offset = sfb_offset[i];
#ifdef SLOW
if ((i+hop) > nr_of_sfb){
end = sfb_offset[nr_of_sfb];
} else
#endif
end = sfb_offset[q];
length = end - offset;
/* all spectral coefficients in this section are zero */
if (max_sb_coeff == 0) {
book_choice[j][0] = CalcBits(coderInfo,0,quant,offset,length);
book_choice[j++][1] = 0;
}
else { /* if the section does have non-zero coefficients */
if(max_sb_coeff < 2){
book_choice[j][0] = CalcBits(coderInfo,1,quant,offset,length);
book_choice[j++][1] = 1;
book_choice[j][0] = CalcBits(coderInfo,2,quant,offset,length);
book_choice[j++][1] = 2;
book_choice[j][0] = CalcBits(coderInfo,3,quant,offset,length);
book_choice[j++][1] = 3;
}
else if (max_sb_coeff < 3){
book_choice[j][0] = CalcBits(coderInfo,3,quant,offset,length);
book_choice[j++][1] = 3;
book_choice[j][0] = CalcBits(coderInfo,4,quant,offset,length);
book_choice[j++][1] = 4;
book_choice[j][0] = CalcBits(coderInfo,5,quant,offset,length);
book_choice[j++][1] = 5;
}
else if (max_sb_coeff < 5){
book_choice[j][0] = CalcBits(coderInfo,5,quant,offset,length);
book_choice[j++][1] = 5;
book_choice[j][0] = CalcBits(coderInfo,6,quant,offset,length);
book_choice[j++][1] = 6;
book_choice[j][0] = CalcBits(coderInfo,7,quant,offset,length);
book_choice[j++][1] = 7;
}
else if (max_sb_coeff < 8){
book_choice[j][0] = CalcBits(coderInfo,7,quant,offset,length);
book_choice[j++][1] = 7;
book_choice[j][0] = CalcBits(coderInfo,8,quant,offset,length);
book_choice[j++][1] = 8;
book_choice[j][0] = CalcBits(coderInfo,9,quant,offset,length);
book_choice[j++][1] = 9;
}
else if (max_sb_coeff < 13){
book_choice[j][0] = CalcBits(coderInfo,9,quant,offset,length);
book_choice[j++][1] = 9;
book_choice[j][0] = CalcBits(coderInfo,10,quant,offset,length);
book_choice[j++][1] = 10;
}
/* (max_sb_coeff >= 13), choose table 11 */
else {
book_choice[j][0] = CalcBits(coderInfo,11,quant,offset,length);
book_choice[j++][1] = 11;
}
}
/* find the minimum bit cost and table number for huffman coding this scalefactor section */
min_book_choice[i][1] = book_choice[0][1];
min_book_choice[i][0] = book_choice[0][0];
for(k=1;k<j;k++){
if (book_choice[k][0] < min_book_choice[i][0]){
min_book_choice[i][1] = book_choice[k][1];
min_book_choice[i][0] = book_choice[k][0];
}
}
total_bits_cost += min_book_choice[i][0];
}
}
return(total_bits_cost);
}
static int CalculateEscSequence(int input, int *len_esc_sequence)
/*
This function takes an element that is larger than 16 and generates the base10 value of the
equivalent escape sequence. It returns the escape sequence in the variable, 'output'. It
also passed the length of the escape sequence through the parameter, 'len_esc_sequence'.
*/
{
float x,y;
int output;
int N;
N = -1;
y = (float)ABS(input);
x = y / 16;
while (x >= 1) {
N++;
x = x/2;
}
*len_esc_sequence = 2*N + 5; /* the length of the escape sequence in bits */
output = (int)((pow(2,N) - 1)*pow(2,N+5) + y - pow(2,N+4));
return(output);
}
int OutputBits(CoderInfo *coderInfo,
#ifdef DRM
int *book, /* we need to change book for VCB11 */
#else
int book,
#endif
int *quant,
int offset,
int length)
{
/*
This function inputs
- a specific codebook number, 'book'
- the quantized spectral data, 'quant[][]'
- the offset into the spectral data to begin scanning, 'offset'
- the 'length' of the segment to huffman code
-> therefore, the segment quant[offset] to quant[offset+length-1]
is huffman coded.
This function outputs
- the number of bits required, 'bits' using the prescribed codebook, book applied to
the given segment of spectral data.
There are three parameters that are passed back and forth into this function. data[]
and len[] are one-dimensional arrays that store the codebook values and their respective
bit lengths. These are used when packing the data for the bitstream in OutputBits(). The
index into these arrays is 'coderInfo->spectral_count''. It gets incremented internally in this
function as counter, then passed to the outside through outside_counter. The next time
OutputBits() is called, counter starts at the value it left off from the previous call.
*/
int esc_sequence;
int len_esc;
int index;
int bits=0;
int tmp;
int codebook,i,j;
int counter;
/* Set up local pointers to coderInfo elements data and len */
int* data= coderInfo->data;
int* len= coderInfo->len;
#ifdef DRM
int* num_data = coderInfo->num_data_cw;
int cur_cw_len;
int max_esc_sequ = 0;
#endif
counter = coderInfo->spectral_count;
#ifdef DRM
switch (*book) {
#else
switch (book) {
#endif
case 0:
case INTENSITY_HCB2:
case INTENSITY_HCB:
#ifdef DRM
for(i=offset;i<offset+length;i=i+4){
#endif
/* This case also applies to intensity stereo encoding */
coderInfo->data[counter] = 0;
coderInfo->len[counter++] = 0;
coderInfo->spectral_count = counter; /* send the current count back to the outside world */
#ifdef DRM
num_data[coderInfo->cur_cw++] = 1;
}
#endif
return(bits);
case 1:
for(i=offset;i<offset+length;i=i+4){
index = 27*quant[i] + 9*quant[i+1] + 3*quant[i+2] + quant[i+3] + 40;
codebook = huff1[index][LASTINTAB];
tmp = huff1[index][FIRSTINTAB];
bits += tmp;
data[counter] = codebook;
len[counter++] = tmp;
#ifdef DRM
num_data[coderInfo->cur_cw++] = 1;
coderInfo->iLenReordSpData += tmp;
if (tmp > coderInfo->iLenLongestCW)
coderInfo->iLenLongestCW = tmp;
#endif
}
coderInfo->spectral_count = counter; /* send the current count back to the outside world */
return(bits);
case 2:
for(i=offset;i<offset+length;i=i+4){
index = 27*quant[i] + 9*quant[i+1] + 3*quant[i+2] + quant[i+3] + 40;
codebook = huff2[index][LASTINTAB];
tmp = huff2[index][FIRSTINTAB];
bits += tmp;
data[counter] = codebook;
len[counter++] = tmp;
#ifdef DRM
num_data[coderInfo->cur_cw++] = 1;
coderInfo->iLenReordSpData += tmp;
if (tmp > coderInfo->iLenLongestCW)
coderInfo->iLenLongestCW = tmp;
#endif
}
coderInfo->spectral_count = counter; /* send the current count back to the outside world */
return(bits);
case 3:
for(i=offset;i<offset+length;i=i+4){
index = 27*ABS(quant[i]) + 9*ABS(quant[i+1]) + 3*ABS(quant[i+2]) + ABS(quant[i+3]);
codebook = huff3[index][LASTINTAB];
tmp = huff3[index][FIRSTINTAB];
bits = bits + tmp;
data[counter] = codebook;
len[counter++] = tmp;
#ifdef DRM
num_data[coderInfo->cur_cw] = 1;
cur_cw_len = tmp;
#endif
for(j=0;j<4;j++){
if(quant[i+j] > 0) { /* send out '0' if a positive value */
data[counter] = 0;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
} else
if(quant[i+j] < 0) { /* send out '1' if a negative value */
data[counter] = 1;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
}
}
#ifdef DRM
coderInfo->iLenReordSpData += cur_cw_len;
if (cur_cw_len > coderInfo->iLenLongestCW)
coderInfo->iLenLongestCW = cur_cw_len;
coderInfo->cur_cw++;
#endif
}
coderInfo->spectral_count = counter; /* send the current count back to the outside world */
return(bits);
case 4:
for(i=offset;i<offset+length;i=i+4){
index = 27*ABS(quant[i]) + 9*ABS(quant[i+1]) + 3*ABS(quant[i+2]) + ABS(quant[i+3]);
codebook = huff4[index][LASTINTAB];
tmp = huff4[index][FIRSTINTAB];
bits = bits + tmp;
data[counter] = codebook;
len[counter++] = tmp;
#ifdef DRM
num_data[coderInfo->cur_cw] = 1;
cur_cw_len = tmp;
#endif
for(j=0;j<4;j++){
if(quant[i+j] > 0) { /* send out '0' if a positive value */
data[counter] = 0;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
} else
if(quant[i+j] < 0) { /* send out '1' if a negative value */
data[counter] = 1;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
}
}
#ifdef DRM
coderInfo->iLenReordSpData += cur_cw_len;
if (cur_cw_len > coderInfo->iLenLongestCW)
coderInfo->iLenLongestCW = cur_cw_len;
coderInfo->cur_cw++;
#endif
}
coderInfo->spectral_count = counter; /* send the current count back to the outside world */
return(bits);
case 5:
for(i=offset;i<offset+length;i=i+2){
index = 9*(quant[i]) + (quant[i+1]) + 40;
codebook = huff5[index][LASTINTAB];
tmp = huff5[index][FIRSTINTAB];
bits = bits + tmp;
data[counter] = codebook;
len[counter++] = tmp;
#ifdef DRM
num_data[coderInfo->cur_cw++] = 1;
coderInfo->iLenReordSpData += tmp;
if (tmp > coderInfo->iLenLongestCW)
coderInfo->iLenLongestCW = tmp;
#endif
}
coderInfo->spectral_count = counter; /* send the current count back to the outside world */
return(bits);
case 6:
for(i=offset;i<offset+length;i=i+2){
index = 9*(quant[i]) + (quant[i+1]) + 40;
codebook = huff6[index][LASTINTAB];
tmp = huff6[index][FIRSTINTAB];
bits = bits + tmp;
data[counter] = codebook;
len[counter++] = tmp;
#ifdef DRM
num_data[coderInfo->cur_cw++] = 1;
coderInfo->iLenReordSpData += tmp;
if (tmp > coderInfo->iLenLongestCW)
coderInfo->iLenLongestCW = tmp;
#endif
}
coderInfo->spectral_count = counter; /* send the current count back to the outside world */
return(bits);
case 7:
for(i=offset;i<offset+length;i=i+2){
index = 8*ABS(quant[i]) + ABS(quant[i+1]);
codebook = huff7[index][LASTINTAB];
tmp = huff7[index][FIRSTINTAB];
bits = bits + tmp;
data[counter] = codebook;
len[counter++] = tmp;
#ifdef DRM
num_data[coderInfo->cur_cw] = 1;
cur_cw_len = tmp;
#endif
for(j=0;j<2;j++){
if(quant[i+j] > 0) { /* send out '0' if a positive value */
data[counter] = 0;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
} else
if(quant[i+j] < 0) { /* send out '1' if a negative value */
data[counter] = 1;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
}
}
#ifdef DRM
coderInfo->iLenReordSpData += cur_cw_len;
if (cur_cw_len > coderInfo->iLenLongestCW)
coderInfo->iLenLongestCW = cur_cw_len;
coderInfo->cur_cw++;
#endif
}
coderInfo->spectral_count = counter; /* send the current count back to the outside world */
return(bits);
case 8:
for(i=offset;i<offset+length;i=i+2){
index = 8*ABS(quant[i]) + ABS(quant[i+1]);
codebook = huff8[index][LASTINTAB];
tmp = huff8[index][FIRSTINTAB];
bits = bits + tmp;
data[counter] = codebook;
len[counter++] = tmp;
#ifdef DRM
num_data[coderInfo->cur_cw] = 1;
cur_cw_len = tmp;
#endif
for(j=0;j<2;j++){
if(quant[i+j] > 0) { /* send out '0' if a positive value */
data[counter] = 0;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
} else
if(quant[i+j] < 0) { /* send out '1' if a negative value */
data[counter] = 1;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
}
}
#ifdef DRM
coderInfo->iLenReordSpData += cur_cw_len;
if (cur_cw_len > coderInfo->iLenLongestCW)
coderInfo->iLenLongestCW = cur_cw_len;
coderInfo->cur_cw++;
#endif
}
coderInfo->spectral_count = counter; /* send the current count back to the outside world */
return(bits);
case 9:
for(i=offset;i<offset+length;i=i+2){
index = 13*ABS(quant[i]) + ABS(quant[i+1]);
codebook = huff9[index][LASTINTAB];
tmp = huff9[index][FIRSTINTAB];
bits = bits + tmp;
data[counter] = codebook;
len[counter++] = tmp;
#ifdef DRM
num_data[coderInfo->cur_cw] = 1;
cur_cw_len = tmp;
#endif
for(j=0;j<2;j++){
if(quant[i+j] > 0) { /* send out '0' if a positive value */
data[counter] = 0;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
} else
if(quant[i+j] < 0) { /* send out '1' if a negative value */
data[counter] = 1;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
}
}
#ifdef DRM
coderInfo->iLenReordSpData += cur_cw_len;
if (cur_cw_len > coderInfo->iLenLongestCW)
coderInfo->iLenLongestCW = cur_cw_len;
coderInfo->cur_cw++;
#endif
}
coderInfo->spectral_count = counter; /* send the current count back to the outside world */
return(bits);
case 10:
for(i=offset;i<offset+length;i=i+2){
index = 13*ABS(quant[i]) + ABS(quant[i+1]);
codebook = huff10[index][LASTINTAB];
tmp = huff10[index][FIRSTINTAB];
bits = bits + tmp;
data[counter] = codebook;
len[counter++] = tmp;
#ifdef DRM
num_data[coderInfo->cur_cw] = 1;
cur_cw_len = tmp;
#endif
for(j=0;j<2;j++){
if(quant[i+j] > 0) { /* send out '0' if a positive value */
data[counter] = 0;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
} else
if(quant[i+j] < 0) { /* send out '1' if a negative value */
data[counter] = 1;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
}
}
#ifdef DRM
coderInfo->iLenReordSpData += cur_cw_len;
if (cur_cw_len > coderInfo->iLenLongestCW)
coderInfo->iLenLongestCW = cur_cw_len;
coderInfo->cur_cw++;
#endif
}
coderInfo->spectral_count = counter; /* send the current count back to the outside world */
return(bits);
case 11:
/* First, calculate the indecies into the huffman tables */
for(i=offset;i<offset+length;i=i+2){
if ((ABS(quant[i]) >= 16) && (ABS(quant[i+1]) >= 16)) { /* both codewords were above 16 */
/* first, code the orignal pair, with the larger value saturated to +/- 16 */
index = 17*16 + 16;
}
else if (ABS(quant[i]) >= 16) { /* the first codeword was above 16, not the second one */
/* first, code the orignal pair, with the larger value saturated to +/- 16 */
index = 17*16 + ABS(quant[i+1]);
}
else if (ABS(quant[i+1]) >= 16) { /* the second codeword was above 16, not the first one */
index = 17*ABS(quant[i]) + 16;
}
else { /* there were no values above 16, so no escape sequences */
index = 17*ABS(quant[i]) + ABS(quant[i+1]);
}
/* write out the codewords */
tmp = huff11[index][FIRSTINTAB];
codebook = huff11[index][LASTINTAB];
bits += tmp;
data[counter] = codebook;
len[counter++] = tmp;
#ifdef DRM
num_data[coderInfo->cur_cw] = 1;
cur_cw_len = tmp;
#endif
/* Take care of the sign bits */
for(j=0;j<2;j++){
if(quant[i+j] > 0) { /* send out '0' if a positive value */
data[counter] = 0;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
} else
if(quant[i+j] < 0) { /* send out '1' if a negative value */
data[counter] = 1;
len[counter++] = 1;
bits += 1;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += 1;
#endif
}
}
/* write out the escape sequences */
if ((ABS(quant[i]) >= 16) && (ABS(quant[i+1]) >= 16)) { /* both codewords were above 16 */
/* code and transmit the first escape_sequence */
esc_sequence = CalculateEscSequence(quant[i],&len_esc);
bits += len_esc;
data[counter] = esc_sequence;
len[counter++] = len_esc;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += len_esc;
if (esc_sequence > max_esc_sequ)
max_esc_sequ = esc_sequence;
#endif
/* then code and transmit the second escape_sequence */
esc_sequence = CalculateEscSequence(quant[i+1],&len_esc);
bits += len_esc;
data[counter] = esc_sequence;
len[counter++] = len_esc;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += len_esc;
if (esc_sequence > max_esc_sequ)
max_esc_sequ = esc_sequence;
#endif
}
else if (ABS(quant[i]) >= 16) { /* the first codeword was above 16, not the second one */
/* code and transmit the escape_sequence */
esc_sequence = CalculateEscSequence(quant[i],&len_esc);
bits += len_esc;
data[counter] = esc_sequence;
len[counter++] = len_esc;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += len_esc;
if (esc_sequence > max_esc_sequ)
max_esc_sequ = esc_sequence;
#endif
}
else if (ABS(quant[i+1]) >= 16) { /* the second codeword was above 16, not the first one */
/* code and transmit the escape_sequence */
esc_sequence = CalculateEscSequence(quant[i+1],&len_esc);
bits += len_esc;
data[counter] = esc_sequence;
len[counter++] = len_esc;
#ifdef DRM
num_data[coderInfo->cur_cw]++;
cur_cw_len += len_esc;
if (esc_sequence > max_esc_sequ)
max_esc_sequ = esc_sequence;
#endif
}
#ifdef DRM
coderInfo->iLenReordSpData += cur_cw_len;
if (cur_cw_len > coderInfo->iLenLongestCW)
coderInfo->iLenLongestCW = cur_cw_len;
coderInfo->cur_cw++;
#endif
}
coderInfo->spectral_count = counter; /* send the current count back to the outside world */
#ifdef DRM
/* VCB11: check which codebook should be used using max escape sequence */
/* 8.5.3.1.3, table 157 */
if (max_esc_sequ <= 15)
*book = 16;
else if (max_esc_sequ <= 31)
*book = 17;
else if (max_esc_sequ <= 47)
*book = 18;
else if (max_esc_sequ <= 63)
*book = 19;
else if (max_esc_sequ <= 95)
*book = 20;
else if (max_esc_sequ <= 127)
*book = 21;
else if (max_esc_sequ <= 159)
*book = 22;
else if (max_esc_sequ <= 191)
*book = 23;
else if (max_esc_sequ <= 223)
*book = 24;
else if (max_esc_sequ <= 255)
*book = 25;
else if (max_esc_sequ <= 319)
*book = 26;
else if (max_esc_sequ <= 383)
*book = 27;
else if (max_esc_sequ <= 511)
*book = 28;
else if (max_esc_sequ <= 767)
*book = 29;
else if (max_esc_sequ <= 1023)
*book = 30;
else if (max_esc_sequ <= 2047)
*book = 31;
/* else: codebook 11 -> it is already 11 */
#endif
return(bits);
}
return 0;
}
int CalcBits(CoderInfo *coderInfo,
int book,
int *quant,
int offset,
int length)
{
/*
This function inputs
- a specific codebook number, 'book'
- the quantized spectral data, 'quant[]'
- the offset into the spectral data to begin scanning, 'offset'
- the 'length' of the segment to huffman code
-> therefore, the segment quant[offset] to quant[offset+length-1]
is huffman coded.
This function outputs
- the number of bits required, 'bits' using the prescribed codebook, book applied to
the given segment of spectral data.
*/
int len_esc;
int index;
int bits = 0;
int i, j;
switch (book) {
case 1:
for(i=offset;i<offset+length;i=i+4){
index = 27*quant[i] + 9*quant[i+1] + 3*quant[i+2] + quant[i+3] + 40;
bits += huff1[index][FIRSTINTAB];
}
return (bits);
case 2:
for(i=offset;i<offset+length;i=i+4){
index = 27*quant[i] + 9*quant[i+1] + 3*quant[i+2] + quant[i+3] + 40;
bits += huff2[index][FIRSTINTAB];
}
return (bits);
case 3:
for(i=offset;i<offset+length;i=i+4){
index = 27*ABS(quant[i]) + 9*ABS(quant[i+1]) + 3*ABS(quant[i+2]) + ABS(quant[i+3]);
bits += huff3[index][FIRSTINTAB];
for(j=0;j<4;j++){
if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
}
}
return (bits);
case 4:
for(i=offset;i<offset+length;i=i+4){
index = 27*ABS(quant[i]) + 9*ABS(quant[i+1]) + 3*ABS(quant[i+2]) + ABS(quant[i+3]);
bits += huff4[index][FIRSTINTAB];
for(j=0;j<4;j++){
if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
}
}
return (bits);
case 5:
for(i=offset;i<offset+length;i=i+2){
index = 9*(quant[i]) + (quant[i+1]) + 40;
bits += huff5[index][FIRSTINTAB];
}
return (bits);
case 6:
for(i=offset;i<offset+length;i=i+2){
index = 9*(quant[i]) + (quant[i+1]) + 40;
bits += huff6[index][FIRSTINTAB];
}
return (bits);
case 7:
for(i=offset;i<offset+length;i=i+2){
index = 8*ABS(quant[i]) + ABS(quant[i+1]);
bits += huff7[index][FIRSTINTAB];
for(j=0;j<2;j++){
if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
}
}
return (bits);
case 8:
for(i=offset;i<offset+length;i=i+2){
index = 8*ABS(quant[i]) + ABS(quant[i+1]);
bits += huff8[index][FIRSTINTAB];
for(j=0;j<2;j++){
if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
}
}
return (bits);
case 9:
for(i=offset;i<offset+length;i=i+2){
index = 13*ABS(quant[i]) + ABS(quant[i+1]);
bits += huff9[index][FIRSTINTAB];
for(j=0;j<2;j++){
if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
}
}
return (bits);
case 10:
for(i=offset;i<offset+length;i=i+2){
index = 13*ABS(quant[i]) + ABS(quant[i+1]);
bits += huff10[index][FIRSTINTAB];
for(j=0;j<2;j++){
if(quant[i+j] != 0) bits += 1; /* only for non-zero spectral coefficients */
}
}
return (bits);
case 11:
/* First, calculate the indecies into the huffman tables */
for(i=offset;i<offset+length;i=i+2){
if ((ABS(quant[i]) >= 16) && (ABS(quant[i+1]) >= 16)) { /* both codewords were above 16 */
/* first, code the orignal pair, with the larger value saturated to +/- 16 */
index = 17*16 + 16;
} else if (ABS(quant[i]) >= 16) { /* the first codeword was above 16, not the second one */
/* first, code the orignal pair, with the larger value saturated to +/- 16 */
index = 17*16 + ABS(quant[i+1]);
} else if (ABS(quant[i+1]) >= 16) { /* the second codeword was above 16, not the first one */
index = 17*ABS(quant[i]) + 16;
} else { /* there were no values above 16, so no escape sequences */