1/*
2 ** Copyright 2003-2010, VisualOn, Inc.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16/*******************************************************************************
17	File:		bit_cnt.c
18
19	Content:	Huffman Bitcounter & coder functions
20
21*******************************************************************************/
22
23#include "bit_cnt.h"
24#include "aac_rom.h"
25
26#define HI_LTAB(a) (a>>8)
27#define LO_LTAB(a) (a & 0xff)
28
29#define EXPAND(a)  ((((Word32)(a&0xff00)) << 8)|(Word32)(a&0xff))
30
31
32/*****************************************************************************
33*
34* function name: count1_2_3_4_5_6_7_8_9_10_11
35* description:  counts tables 1-11
36* returns:
37* input:        quantized spectrum
38* output:       bitCount for tables 1-11
39*
40*****************************************************************************/
41
42static void count1_2_3_4_5_6_7_8_9_10_11(const Word16 *values,
43                                         const Word16  width,
44                                         Word16       *bitCount)
45{
46  Word32 t0,t1,t2,t3,i;
47  Word32 bc1_2,bc3_4,bc5_6,bc7_8,bc9_10;
48  Word16 bc11,sc;
49
50  bc1_2=0;
51  bc3_4=0;
52  bc5_6=0;
53  bc7_8=0;
54  bc9_10=0;
55  bc11=0;
56  sc=0;
57
58  for(i=0;i<width;i+=4){
59
60    t0= values[i+0];
61    t1= values[i+1];
62    t2= values[i+2];
63    t3= values[i+3];
64
65    /* 1,2 */
66
67    bc1_2 = bc1_2 + EXPAND(huff_ltab1_2[t0+1][t1+1][t2+1][t3+1]);
68
69    /* 5,6 */
70    bc5_6 = bc5_6 + EXPAND(huff_ltab5_6[t0+4][t1+4]);
71    bc5_6 = bc5_6 + EXPAND(huff_ltab5_6[t2+4][t3+4]);
72
73    t0=ABS(t0);
74    t1=ABS(t1);
75    t2=ABS(t2);
76    t3=ABS(t3);
77
78
79    bc3_4 = bc3_4 + EXPAND(huff_ltab3_4[t0][t1][t2][t3]);
80
81    bc7_8 = bc7_8 + EXPAND(huff_ltab7_8[t0][t1]);
82    bc7_8 = bc7_8 + EXPAND(huff_ltab7_8[t2][t3]);
83
84    bc9_10 = bc9_10 + EXPAND(huff_ltab9_10[t0][t1]);
85    bc9_10 = bc9_10 + EXPAND(huff_ltab9_10[t2][t3]);
86
87    bc11 = bc11 + huff_ltab11[t0][t1];
88    bc11 = bc11 + huff_ltab11[t2][t3];
89
90
91    sc = sc + (t0>0) + (t1>0) + (t2>0) + (t3>0);
92  }
93
94  bitCount[1]=extract_h(bc1_2);
95  bitCount[2]=extract_l(bc1_2);
96  bitCount[3]=extract_h(bc3_4) + sc;
97  bitCount[4]=extract_l(bc3_4) + sc;
98  bitCount[5]=extract_h(bc5_6);
99  bitCount[6]=extract_l(bc5_6);
100  bitCount[7]=extract_h(bc7_8) + sc;
101  bitCount[8]=extract_l(bc7_8) + sc;
102  bitCount[9]=extract_h(bc9_10) + sc;
103  bitCount[10]=extract_l(bc9_10) + sc;
104  bitCount[11]=bc11 + sc;
105}
106
107
108/*****************************************************************************
109*
110* function name: count3_4_5_6_7_8_9_10_11
111* description:  counts tables 3-11
112* returns:
113* input:        quantized spectrum
114* output:       bitCount for tables 3-11
115*
116*****************************************************************************/
117
118static void count3_4_5_6_7_8_9_10_11(const Word16 *values,
119                                     const Word16  width,
120                                     Word16       *bitCount)
121{
122  Word32 t0,t1,t2,t3, i;
123  Word32 bc3_4,bc5_6,bc7_8,bc9_10;
124  Word16 bc11,sc;
125
126  bc3_4=0;
127  bc5_6=0;
128  bc7_8=0;
129  bc9_10=0;
130  bc11=0;
131  sc=0;
132
133  for(i=0;i<width;i+=4){
134
135    t0= values[i+0];
136    t1= values[i+1];
137    t2= values[i+2];
138    t3= values[i+3];
139
140    /*
141      5,6
142    */
143    bc5_6 = bc5_6 + EXPAND(huff_ltab5_6[t0+4][t1+4]);
144    bc5_6 = bc5_6 + EXPAND(huff_ltab5_6[t2+4][t3+4]);
145
146    t0=ABS(t0);
147    t1=ABS(t1);
148    t2=ABS(t2);
149    t3=ABS(t3);
150
151
152    bc3_4 = bc3_4 + EXPAND(huff_ltab3_4[t0][t1][t2][t3]);
153
154    bc7_8 = bc7_8 + EXPAND(huff_ltab7_8[t0][t1]);
155    bc7_8 = bc7_8 + EXPAND(huff_ltab7_8[t2][t3]);
156
157    bc9_10 = bc9_10 + EXPAND(huff_ltab9_10[t0][t1]);
158    bc9_10 = bc9_10 + EXPAND(huff_ltab9_10[t2][t3]);
159
160    bc11 = bc11 + huff_ltab11[t0][t1];
161    bc11 = bc11 + huff_ltab11[t2][t3];
162
163
164    sc = sc + (t0>0) + (t1>0) + (t2>0) + (t3>0);
165  }
166
167  bitCount[1]=INVALID_BITCOUNT;
168  bitCount[2]=INVALID_BITCOUNT;
169  bitCount[3]=extract_h(bc3_4) + sc;
170  bitCount[4]=extract_l(bc3_4) + sc;
171  bitCount[5]=extract_h(bc5_6);
172  bitCount[6]=extract_l(bc5_6);
173  bitCount[7]=extract_h(bc7_8) + sc;
174  bitCount[8]=extract_l(bc7_8) + sc;
175  bitCount[9]=extract_h(bc9_10) + sc;
176  bitCount[10]=extract_l(bc9_10) + sc;
177  bitCount[11]=bc11 + sc;
178
179}
180
181
182
183/*****************************************************************************
184*
185* function name: count5_6_7_8_9_10_11
186* description:  counts tables 5-11
187* returns:
188* input:        quantized spectrum
189* output:       bitCount for tables 5-11
190*
191*****************************************************************************/
192static void count5_6_7_8_9_10_11(const Word16 *values,
193                                 const Word16  width,
194                                 Word16       *bitCount)
195{
196
197  Word32 t0,t1,i;
198  Word32 bc5_6,bc7_8,bc9_10;
199  Word16 bc11,sc;
200
201  bc5_6=0;
202  bc7_8=0;
203  bc9_10=0;
204  bc11=0;
205  sc=0;
206
207  for(i=0;i<width;i+=2){
208
209    t0 = values[i+0];
210    t1 = values[i+1];
211
212    bc5_6 = bc5_6 + EXPAND(huff_ltab5_6[t0+4][t1+4]);
213
214    t0=ABS(t0);
215    t1=ABS(t1);
216
217    bc7_8 = bc7_8 + EXPAND(huff_ltab7_8[t0][t1]);
218    bc9_10 = bc9_10 + EXPAND(huff_ltab9_10[t0][t1]);
219    bc11 = bc11 + huff_ltab11[t0][t1];
220
221
222    sc = sc + (t0>0) + (t1>0);
223  }
224  bitCount[1]=INVALID_BITCOUNT;
225  bitCount[2]=INVALID_BITCOUNT;
226  bitCount[3]=INVALID_BITCOUNT;
227  bitCount[4]=INVALID_BITCOUNT;
228  bitCount[5]=extract_h(bc5_6);
229  bitCount[6]=extract_l(bc5_6);
230  bitCount[7]=extract_h(bc7_8) + sc;
231  bitCount[8]=extract_l(bc7_8) + sc;
232  bitCount[9]=extract_h(bc9_10) + sc;
233  bitCount[10]=extract_l(bc9_10) + sc;
234  bitCount[11]=bc11 + sc;
235
236}
237
238
239/*****************************************************************************
240*
241* function name: count7_8_9_10_11
242* description:  counts tables 7-11
243* returns:
244* input:        quantized spectrum
245* output:       bitCount for tables 7-11
246*
247*****************************************************************************/
248
249static void count7_8_9_10_11(const Word16 *values,
250                             const Word16  width,
251                             Word16       *bitCount)
252{
253  Word32 t0,t1, i;
254  Word32 bc7_8,bc9_10;
255  Word16 bc11,sc;
256
257  bc7_8=0;
258  bc9_10=0;
259  bc11=0;
260  sc=0;
261
262  for(i=0;i<width;i+=2){
263
264    t0=ABS(values[i+0]);
265    t1=ABS(values[i+1]);
266
267    bc7_8 = bc7_8 + EXPAND(huff_ltab7_8[t0][t1]);
268    bc9_10 = bc9_10 + EXPAND(huff_ltab9_10[t0][t1]);
269    bc11 = bc11 + huff_ltab11[t0][t1];
270
271
272    sc = sc + (t0>0) + (t1>0);
273  }
274  bitCount[1]=INVALID_BITCOUNT;
275  bitCount[2]=INVALID_BITCOUNT;
276  bitCount[3]=INVALID_BITCOUNT;
277  bitCount[4]=INVALID_BITCOUNT;
278  bitCount[5]=INVALID_BITCOUNT;
279  bitCount[6]=INVALID_BITCOUNT;
280  bitCount[7]=extract_h(bc7_8) + sc;
281  bitCount[8]=extract_l(bc7_8) + sc;
282  bitCount[9]=extract_h(bc9_10) + sc;
283  bitCount[10]=extract_l(bc9_10) + sc;
284  bitCount[11]=bc11 + sc;
285
286}
287
288/*****************************************************************************
289*
290* function name: count9_10_11
291* description:  counts tables 9-11
292* returns:
293* input:        quantized spectrum
294* output:       bitCount for tables 9-11
295*
296*****************************************************************************/
297static void count9_10_11(const Word16 *values,
298                         const Word16  width,
299                         Word16       *bitCount)
300{
301
302  Word32 t0,t1,i;
303  Word32 bc9_10;
304  Word16 bc11,sc;
305
306  bc9_10=0;
307  bc11=0;
308  sc=0;
309
310  for(i=0;i<width;i+=2){
311
312    t0=ABS(values[i+0]);
313    t1=ABS(values[i+1]);
314
315
316    bc9_10 += EXPAND(huff_ltab9_10[t0][t1]);
317    bc11 = bc11 + huff_ltab11[t0][t1];
318
319
320    sc = sc + (t0>0) + (t1>0);
321  }
322  bitCount[1]=INVALID_BITCOUNT;
323  bitCount[2]=INVALID_BITCOUNT;
324  bitCount[3]=INVALID_BITCOUNT;
325  bitCount[4]=INVALID_BITCOUNT;
326  bitCount[5]=INVALID_BITCOUNT;
327  bitCount[6]=INVALID_BITCOUNT;
328  bitCount[7]=INVALID_BITCOUNT;
329  bitCount[8]=INVALID_BITCOUNT;
330  bitCount[9]=extract_h(bc9_10) + sc;
331  bitCount[10]=extract_l(bc9_10) + sc;
332  bitCount[11]=bc11 + sc;
333
334}
335
336/*****************************************************************************
337*
338* function name: count11
339* description:  counts table 11
340* returns:
341* input:        quantized spectrum
342* output:       bitCount for table 11
343*
344*****************************************************************************/
345 static void count11(const Word16 *values,
346                    const Word16  width,
347                    Word16        *bitCount)
348{
349  Word32 t0,t1,i;
350  Word16 bc11,sc;
351
352  bc11=0;
353  sc=0;
354  for(i=0;i<width;i+=2){
355    t0=ABS(values[i+0]);
356    t1=ABS(values[i+1]);
357    bc11 = bc11 + huff_ltab11[t0][t1];
358
359
360    sc = sc + (t0>0) + (t1>0);
361  }
362
363  bitCount[1]=INVALID_BITCOUNT;
364  bitCount[2]=INVALID_BITCOUNT;
365  bitCount[3]=INVALID_BITCOUNT;
366  bitCount[4]=INVALID_BITCOUNT;
367  bitCount[5]=INVALID_BITCOUNT;
368  bitCount[6]=INVALID_BITCOUNT;
369  bitCount[7]=INVALID_BITCOUNT;
370  bitCount[8]=INVALID_BITCOUNT;
371  bitCount[9]=INVALID_BITCOUNT;
372  bitCount[10]=INVALID_BITCOUNT;
373  bitCount[11]=bc11 + sc;
374}
375
376/*****************************************************************************
377*
378* function name: countEsc
379* description:  counts table 11 (with Esc)
380* returns:
381* input:        quantized spectrum
382* output:       bitCount for tables 11 (with Esc)
383*
384*****************************************************************************/
385
386static void countEsc(const Word16 *values,
387                     const Word16  width,
388                     Word16       *bitCount)
389{
390  Word32 t0,t1,t00,t01,i;
391  Word16 bc11,ec,sc;
392
393  bc11=0;
394  sc=0;
395  ec=0;
396  for(i=0;i<width;i+=2){
397    t0=ABS(values[i+0]);
398    t1=ABS(values[i+1]);
399
400
401    sc = sc + (t0>0) + (t1>0);
402
403    t00 = min(t0,16);
404    t01 = min(t1,16);
405    bc11 = bc11 + huff_ltab11[t00][t01];
406
407
408    if(t0 >= 16){
409      ec = ec + 5;
410      while(sub(t0=(t0 >> 1), 16) >= 0) {
411        ec = ec + 2;
412      }
413    }
414
415
416    if(t1 >= 16){
417      ec = ec + 5;
418      while(sub(t1=(t1 >> 1), 16) >= 0) {
419        ec = ec + 2;
420      }
421    }
422  }
423  bitCount[1]=INVALID_BITCOUNT;
424  bitCount[2]=INVALID_BITCOUNT;
425  bitCount[3]=INVALID_BITCOUNT;
426  bitCount[4]=INVALID_BITCOUNT;
427  bitCount[5]=INVALID_BITCOUNT;
428  bitCount[6]=INVALID_BITCOUNT;
429  bitCount[7]=INVALID_BITCOUNT;
430  bitCount[8]=INVALID_BITCOUNT;
431  bitCount[9]=INVALID_BITCOUNT;
432  bitCount[10]=INVALID_BITCOUNT;
433  bitCount[11]=bc11 + sc + ec;
434}
435
436
437typedef void (*COUNT_FUNCTION)(const Word16 *values,
438                               const Word16  width,
439                               Word16       *bitCount);
440
441static COUNT_FUNCTION countFuncTable[CODE_BOOK_ESC_LAV+1] =
442  {
443
444    count1_2_3_4_5_6_7_8_9_10_11,  /* 0  */
445    count1_2_3_4_5_6_7_8_9_10_11,  /* 1  */
446    count3_4_5_6_7_8_9_10_11,      /* 2  */
447    count5_6_7_8_9_10_11,          /* 3  */
448    count5_6_7_8_9_10_11,          /* 4  */
449    count7_8_9_10_11,              /* 5  */
450    count7_8_9_10_11,              /* 6  */
451    count7_8_9_10_11,              /* 7  */
452    count9_10_11,                  /* 8  */
453    count9_10_11,                  /* 9  */
454    count9_10_11,                  /* 10 */
455    count9_10_11,                  /* 11 */
456    count9_10_11,                  /* 12 */
457    count11,                       /* 13 */
458    count11,                       /* 14 */
459    count11,                       /* 15 */
460    countEsc                       /* 16 */
461  };
462
463/*****************************************************************************
464*
465* function name: bitCount
466* description:  count bits
467*
468*****************************************************************************/
469Word16 bitCount(const Word16 *values,
470                const Word16  width,
471                Word16        maxVal,
472                Word16       *bitCount)
473{
474  /*
475    check if we can use codebook 0
476  */
477
478  if(maxVal == 0)
479    bitCount[0] = 0;
480  else
481    bitCount[0] = INVALID_BITCOUNT;
482
483  maxVal = min(maxVal, CODE_BOOK_ESC_LAV);
484  countFuncTable[maxVal](values,width,bitCount);
485
486  return(0);
487}
488
489/*****************************************************************************
490*
491* function name: codeValues
492* description:  write huffum bits
493*
494*****************************************************************************/
495Word16 codeValues(Word16 *values, Word16 width, Word16 codeBook, HANDLE_BIT_BUF hBitstream)
496{
497
498  Word32 i, t0, t1, t2, t3, t00, t01;
499  UWord16 codeWord, codeLength;
500  Word16 sign, signLength;
501
502
503  switch (codeBook) {
504    case CODE_BOOK_ZERO_NO:
505      break;
506
507    case CODE_BOOK_1_NO:
508      for(i=0; i<width; i+=4) {
509        t0         = values[i+0];
510        t1         = values[i+1];
511        t2         = values[i+2];
512        t3         = values[i+3];
513        codeWord   = huff_ctab1[t0+1][t1+1][t2+1][t3+1];
514        codeLength = HI_LTAB(huff_ltab1_2[t0+1][t1+1][t2+1][t3+1]);
515        WriteBits(hBitstream, codeWord, codeLength);
516      }
517      break;
518
519    case CODE_BOOK_2_NO:
520      for(i=0; i<width; i+=4) {
521        t0         = values[i+0];
522        t1         = values[i+1];
523        t2         = values[i+2];
524        t3         = values[i+3];
525        codeWord   = huff_ctab2[t0+1][t1+1][t2+1][t3+1];
526        codeLength = LO_LTAB(huff_ltab1_2[t0+1][t1+1][t2+1][t3+1]);
527        WriteBits(hBitstream,codeWord,codeLength);
528      }
529      break;
530
531    case CODE_BOOK_3_NO:
532      for(i=0; i<width; i+=4) {
533        sign=0;
534        signLength=0;
535        t0 = values[i+0];
536
537        if(t0 != 0){
538          signLength = signLength + 1;
539          sign = sign << 1;
540
541          if(t0 < 0){
542            sign|=1;
543            t0=-t0;
544          }
545        }
546        t1 = values[i+1];
547
548        if(t1 != 0){
549          signLength = signLength + 1;
550          sign = sign << 1;
551
552          if(t1 < 0){
553            sign|=1;
554            t1=-t1;
555          }
556        }
557        t2 = values[i+2];
558
559        if(t2 != 0){
560          signLength = signLength + 1;
561          sign = sign << 1;
562
563          if(t2 < 0){
564            sign|=1;
565            t2=-t2;
566          }
567        }
568        t3 = values[i+3];
569        if(t3 != 0){
570          signLength = signLength + 1;
571          sign = sign << 1;
572
573          if(t3 < 0){
574            sign|=1;
575            t3=-t3;
576          }
577        }
578
579        codeWord   = huff_ctab3[t0][t1][t2][t3];
580        codeLength = HI_LTAB(huff_ltab3_4[t0][t1][t2][t3]);
581        WriteBits(hBitstream,codeWord,codeLength);
582        WriteBits(hBitstream,sign,signLength);
583      }
584      break;
585
586    case CODE_BOOK_4_NO:
587      for(i=0; i<width; i+=4) {
588        sign=0;
589        signLength=0;
590        t0 = values[i+0];
591
592        if(t0 != 0){
593          signLength = signLength + 1;
594          sign = sign << 1;
595          if(t0 < 0){
596            sign|=1;
597            t0=-t0;
598          }
599        }
600        t1 = values[i+1];
601
602        if(t1 != 0){
603          signLength = signLength + 1;
604          sign = sign << 1;
605
606          if(t1 < 0){
607            sign|=1;
608            t1=-t1;
609          }
610        }
611        t2 = values[i+2];
612
613        if(t2 != 0){
614          signLength = signLength + 1;
615          sign = sign << 1;
616
617          if(t2 < 0){
618            sign|=1;
619            t2=-t2;
620          }
621        }
622        t3 = values[i+3];
623
624        if(t3 != 0){
625          signLength = signLength + 1;
626          sign = sign << 1;
627
628          if(t3 < 0){
629            sign|=1;
630            t3=-t3;
631          }
632        }
633        codeWord   = huff_ctab4[t0][t1][t2][t3];
634        codeLength = LO_LTAB(huff_ltab3_4[t0][t1][t2][t3]);
635        WriteBits(hBitstream,codeWord,codeLength);
636        WriteBits(hBitstream,sign,signLength);
637      }
638      break;
639
640    case CODE_BOOK_5_NO:
641      for(i=0; i<width; i+=2) {
642        t0         = values[i+0];
643        t1         = values[i+1];
644        codeWord   = huff_ctab5[t0+4][t1+4];
645        codeLength = HI_LTAB(huff_ltab5_6[t0+4][t1+4]);
646        WriteBits(hBitstream,codeWord,codeLength);
647      }
648      break;
649
650    case CODE_BOOK_6_NO:
651      for(i=0; i<width; i+=2) {
652        t0         = values[i+0];
653        t1         = values[i+1];
654        codeWord   = huff_ctab6[t0+4][t1+4];
655        codeLength = LO_LTAB(huff_ltab5_6[t0+4][t1+4]);
656        WriteBits(hBitstream,codeWord,codeLength);
657      }
658      break;
659
660    case CODE_BOOK_7_NO:
661      for(i=0; i<width; i+=2){
662        sign=0;
663        signLength=0;
664        t0 = values[i+0];
665
666        if(t0 != 0){
667          signLength = signLength + 1;
668          sign = sign << 1;
669
670          if(t0 < 0){
671            sign|=1;
672            t0=-t0;
673          }
674        }
675
676        t1 = values[i+1];
677
678        if(t1 != 0){
679          signLength = signLength + 1;
680          sign = sign << 1;
681
682          if(t1 < 0){
683            sign|=1;
684            t1=-t1;
685          }
686        }
687        codeWord   = huff_ctab7[t0][t1];
688        codeLength = HI_LTAB(huff_ltab7_8[t0][t1]);
689        WriteBits(hBitstream,codeWord,codeLength);
690        WriteBits(hBitstream,sign,signLength);
691      }
692      break;
693
694    case CODE_BOOK_8_NO:
695      for(i=0; i<width; i+=2) {
696        sign=0;
697        signLength=0;
698        t0 = values[i+0];
699
700        if(t0 != 0){
701          signLength = signLength + 1;
702          sign = sign << 1;
703
704          if(t0 < 0){
705            sign|=1;
706            t0=-t0;
707          }
708        }
709
710        t1 = values[i+1];
711
712        if(t1 != 0){
713          signLength = signLength + 1;
714          sign = sign << 1;
715
716          if(t1 < 0){
717            sign|=1;
718            t1=-t1;
719          }
720        }
721        codeWord   = huff_ctab8[t0][t1];
722        codeLength = LO_LTAB(huff_ltab7_8[t0][t1]);
723        WriteBits(hBitstream,codeWord,codeLength);
724        WriteBits(hBitstream,sign,signLength);
725      }
726      break;
727
728    case CODE_BOOK_9_NO:
729      for(i=0; i<width; i+=2) {
730        sign=0;
731        signLength=0;
732        t0 = values[i+0];
733
734        if(t0 != 0){
735          signLength = signLength + 1;
736          sign = sign << 1;
737
738          if(t0 < 0){
739            sign|=1;
740            t0=-t0;
741          }
742        }
743
744        t1 = values[i+1];
745
746        if(t1 != 0){
747          signLength = signLength + 1;
748          sign = sign << 1;
749
750          if(t1 < 0){
751            sign|=1;
752            t1=-t1;
753          }
754        }
755        codeWord   = huff_ctab9[t0][t1];
756        codeLength = HI_LTAB(huff_ltab9_10[t0][t1]);
757        WriteBits(hBitstream,codeWord,codeLength);
758        WriteBits(hBitstream,sign,signLength);
759      }
760      break;
761
762    case CODE_BOOK_10_NO:
763      for(i=0; i<width; i+=2) {
764        sign=0;
765        signLength=0;
766        t0 = values[i+0];
767
768        if(t0 != 0){
769          signLength = signLength + 1;
770          sign = sign << 1;
771
772          if(t0 < 0){
773            sign|=1;
774            t0=-t0;
775          }
776        }
777
778        t1 = values[i+1];
779
780        if(t1 != 0){
781          signLength = signLength + 1;
782          sign = sign << 1;
783
784          if(t1 < 0){
785            sign|=1;
786            t1=-t1;
787          }
788        }
789        codeWord   = huff_ctab10[t0][t1];
790        codeLength = LO_LTAB(huff_ltab9_10[t0][t1]);
791        WriteBits(hBitstream,codeWord,codeLength);
792        WriteBits(hBitstream,sign,signLength);
793      }
794      break;
795
796    case CODE_BOOK_ESC_NO:
797      for(i=0; i<width; i+=2) {
798        sign=0;
799        signLength=0;
800        t0 = values[i+0];
801
802        if(t0 != 0){
803          signLength = signLength + 1;
804          sign = sign << 1;
805
806          if(t0 < 0){
807            sign|=1;
808            t0=-t0;
809          }
810        }
811
812        t1 = values[i+1];
813
814        if(t1 != 0){
815          signLength = signLength + 1;
816          sign = sign << 1;
817
818          if(t1 < 0){
819            sign|=1;
820            t1=-t1;
821          }
822        }
823        t00 = min(t0,16);
824        t01 = min(t1,16);
825
826        codeWord   = huff_ctab11[t00][t01];
827        codeLength = huff_ltab11[t00][t01];
828        WriteBits(hBitstream,codeWord,codeLength);
829        WriteBits(hBitstream,sign,signLength);
830
831        if(t0 >= 16){
832          Word16 n, p;
833          n=0;
834          p=t0;
835          while(sub(p=(p >> 1), 16) >= 0){
836
837            WriteBits(hBitstream,1,1);
838            n = n + 1;
839          }
840          WriteBits(hBitstream,0,1);
841          n = n + 4;
842          WriteBits(hBitstream,(t0 - (1 << n)),n);
843        }
844
845        if(t1 >= 16){
846          Word16 n, p;
847          n=0;
848          p=t1;
849          while(sub(p=(p >> 1), 16) >= 0){
850
851            WriteBits(hBitstream,1,1);
852            n = n + 1;
853          }
854          WriteBits(hBitstream,0,1);
855          n = n + 4;
856          WriteBits(hBitstream,(t1 - (1 << n)),n);
857        }
858      }
859      break;
860
861    default:
862      break;
863  }
864  return(0);
865}
866
867Word16 bitCountScalefactorDelta(Word16 delta)
868{
869  return(huff_ltabscf[delta+CODE_BOOK_SCF_LAV]);
870}
871
872Word16 codeScalefactorDelta(Word16 delta, HANDLE_BIT_BUF hBitstream)
873{
874  Word32 codeWord;
875  Word16 codeLength;
876
877
878  if(delta > CODE_BOOK_SCF_LAV || delta < -CODE_BOOK_SCF_LAV)
879    return(1);
880
881  codeWord   = huff_ctabscf[delta + CODE_BOOK_SCF_LAV];
882  codeLength = huff_ltabscf[delta + CODE_BOOK_SCF_LAV];
883  WriteBits(hBitstream,codeWord,codeLength);
884  return(0);
885}
886