1 /*!
2  ************************************************************************
3  * \file vlc.c
4  *
5  * \brief
6  *    VLC support functions
7  *
8  * \author
9  *    Main contributors (see contributors.h for copyright, address and affiliation details)
10  *    - Inge Lille-Lang�y               <inge.lille-langoy@telenor.com>
11  *    - Detlev Marpe
12  *    - Gabi Blaettermann
13  ************************************************************************
14  */
15 #include "contributors.h"
16 
17 #include "global.h"
18 #include "vlc.h"
19 #include "elements.h"
20 
21 
22 // A little trick to avoid those horrible #if TRACE all over the source code
23 #if TRACE
24 #define SYMTRACESTRING(s) strncpy(symbol.tracestring,s,TRACESTRING_SIZE)
25 #else
26 #define SYMTRACESTRING(s) // do nothing
27 #endif
28 
29 // Note that all NA values are filled with 0
30 
31 /*!
32  *************************************************************************************
33  * \brief
34  *    read_ue_v, reads an ue(v) syntax element, the length in bits is stored in
35  *    the global p_Dec->UsedBits variable
36  *
37  * \param tracestring
38  *    the string for the trace file
39  *
40  * \param bitstream
41  *    the stream to be read from
42  *
43  * \return
44  *    the value of the coded syntax element
45  *
46  *************************************************************************************
47  */
read_ue_v(char * tracestring,Bitstream * bitstream,int * used_bits)48 int read_ue_v (char *tracestring, Bitstream *bitstream, int *used_bits)
49 {
50   SyntaxElement symbol;
51 
52   //assert (bitstream->streamBuffer != NULL);
53   symbol.type = SE_HEADER;
54   symbol.mapping = linfo_ue;   // Mapping rule
55   SYMTRACESTRING(tracestring);
56   readSyntaxElement_VLC (&symbol, bitstream);
57   *used_bits+=symbol.len;
58   return symbol.value1;
59 }
60 
61 
62 /*!
63  *************************************************************************************
64  * \brief
65  *    read_ue_v, reads an se(v) syntax element, the length in bits is stored in
66  *    the global p_Dec->UsedBits variable
67  *
68  * \param tracestring
69  *    the string for the trace file
70  *
71  * \param bitstream
72  *    the stream to be read from
73  *
74  * \return
75  *    the value of the coded syntax element
76  *
77  *************************************************************************************
78  */
read_se_v(char * tracestring,Bitstream * bitstream,int * used_bits)79 int read_se_v (char *tracestring, Bitstream *bitstream, int *used_bits)
80 {
81   SyntaxElement symbol;
82 
83   //assert (bitstream->streamBuffer != NULL);
84   symbol.type = SE_HEADER;
85   symbol.mapping = linfo_se;   // Mapping rule: signed integer
86   SYMTRACESTRING(tracestring);
87   readSyntaxElement_VLC (&symbol, bitstream);
88   *used_bits+=symbol.len;
89   return symbol.value1;
90 }
91 
92 
93 /*!
94  *************************************************************************************
95  * \brief
96  *    read_ue_v, reads an u(v) syntax element, the length in bits is stored in
97  *    the global p_Dec->UsedBits variable
98  *
99  * \param LenInBits
100  *    length of the syntax element
101  *
102  * \param tracestring
103  *    the string for the trace file
104  *
105  * \param bitstream
106  *    the stream to be read from
107  *
108  * \return
109  *    the value of the coded syntax element
110  *
111  *************************************************************************************
112  */
read_u_v(int LenInBits,char * tracestring,Bitstream * bitstream,int * used_bits)113 int read_u_v (int LenInBits, char*tracestring, Bitstream *bitstream, int *used_bits)
114 {
115   SyntaxElement symbol;
116   symbol.inf = 0;
117 
118   //assert (bitstream->streamBuffer != NULL);
119   symbol.type = SE_HEADER;
120   symbol.mapping = linfo_ue;   // Mapping rule
121   symbol.len = LenInBits;
122   SYMTRACESTRING(tracestring);
123   readSyntaxElement_FLC (&symbol, bitstream);
124   *used_bits+=symbol.len;
125 
126   return symbol.inf;
127 }
128 
129 /*!
130  *************************************************************************************
131  * \brief
132  *    read_i_v, reads an i(v) syntax element, the length in bits is stored in
133  *    the global p_Dec->UsedBits variable
134  *
135  * \param LenInBits
136  *    length of the syntax element
137  *
138  * \param tracestring
139  *    the string for the trace file
140  *
141  * \param bitstream
142  *    the stream to be read from
143  *
144  * \return
145  *    the value of the coded syntax element
146  *
147  *************************************************************************************
148  */
read_i_v(int LenInBits,char * tracestring,Bitstream * bitstream,int * used_bits)149 int read_i_v (int LenInBits, char*tracestring, Bitstream *bitstream, int *used_bits)
150 {
151   SyntaxElement symbol;
152   symbol.inf = 0;
153 
154   //assert (bitstream->streamBuffer != NULL);
155   symbol.type = SE_HEADER;
156   symbol.mapping = linfo_ue;   // Mapping rule
157   symbol.len = LenInBits;
158   SYMTRACESTRING(tracestring);
159   readSyntaxElement_FLC (&symbol, bitstream);
160   *used_bits+=symbol.len;
161 
162   // can be negative
163   symbol.inf = -( symbol.inf & (1 << (LenInBits - 1)) ) | symbol.inf;
164 
165   return symbol.inf;
166 }
167 
168 
169 /*!
170  *************************************************************************************
171  * \brief
172  *    read_ue_v, reads an u(1) syntax element, the length in bits is stored in
173  *    the global p_Dec->UsedBits variable
174  *
175  * \param tracestring
176  *    the string for the trace file
177  *
178  * \param bitstream
179  *    the stream to be read from
180  *
181  * \return
182  *    the value of the coded syntax element
183  *
184  *************************************************************************************
185  */
read_u_1(char * tracestring,Bitstream * bitstream,int * used_bits)186 Boolean read_u_1 (char *tracestring, Bitstream *bitstream, int *used_bits)
187 {
188   return (Boolean) read_u_v (1, tracestring, bitstream, used_bits);
189 }
190 
191 
192 
193 /*!
194  ************************************************************************
195  * \brief
196  *    mapping rule for ue(v) syntax elements
197  * \par Input:
198  *    lenght and info
199  * \par Output:
200  *    number in the code table
201  ************************************************************************
202  */
linfo_ue(int len,int info,int * value1,int * dummy)203 void linfo_ue(int len, int info, int *value1, int *dummy)
204 {
205   //assert ((len >> 1) < 32);
206   *value1 = (int) (((unsigned int) 1 << (len >> 1)) + (unsigned int) (info) - 1);
207 }
208 
209 /*!
210  ************************************************************************
211  * \brief
212  *    mapping rule for se(v) syntax elements
213  * \par Input:
214  *    lenght and info
215  * \par Output:
216  *    signed mvd
217  ************************************************************************
218  */
linfo_se(int len,int info,int * value1,int * dummy)219 void linfo_se(int len,  int info, int *value1, int *dummy)
220 {
221   //assert ((len >> 1) < 32);
222   unsigned int n = ((unsigned int) 1 << (len >> 1)) + (unsigned int) info - 1;
223   *value1 = (n + 1) >> 1;
224   if((n & 0x01) == 0)                           // lsb is signed bit
225     *value1 = -*value1;
226 }
227 
228 
229 /*!
230  ************************************************************************
231  * \par Input:
232  *    length and info
233  * \par Output:
234  *    cbp (intra)
235  ************************************************************************
236  */
linfo_cbp_intra_normal(int len,int info,int * cbp,int * dummy)237 void linfo_cbp_intra_normal(int len,int info,int *cbp, int *dummy)
238 {
239   int cbp_idx;
240 
241   linfo_ue(len, info, &cbp_idx, dummy);
242   *cbp=NCBP[1][cbp_idx][0];
243 }
244 
245 
246 /*!
247  ************************************************************************
248  * \par Input:
249  *    length and info
250  * \par Output:
251  *    cbp (intra)
252  ************************************************************************
253  */
linfo_cbp_intra_other(int len,int info,int * cbp,int * dummy)254 void linfo_cbp_intra_other(int len,int info,int *cbp, int *dummy)
255 {
256   int cbp_idx;
257 
258   linfo_ue(len, info, &cbp_idx, dummy);
259   *cbp=NCBP[0][cbp_idx][0];
260 }
261 
262 /*!
263  ************************************************************************
264  * \par Input:
265  *    length and info
266  * \par Output:
267  *    cbp (inter)
268  ************************************************************************
269  */
linfo_cbp_inter_normal(int len,int info,int * cbp,int * dummy)270 void linfo_cbp_inter_normal(int len,int info,int *cbp, int *dummy)
271 {
272   int cbp_idx;
273 
274   linfo_ue(len, info, &cbp_idx, dummy);
275   *cbp=NCBP[1][cbp_idx][1];
276 }
277 
278 /*!
279  ************************************************************************
280  * \par Input:
281  *    length and info
282  * \par Output:
283  *    cbp (inter)
284  ************************************************************************
285  */
linfo_cbp_inter_other(int len,int info,int * cbp,int * dummy)286 void linfo_cbp_inter_other(int len,int info,int *cbp, int *dummy)
287 {
288   int cbp_idx;
289 
290   linfo_ue(len, info, &cbp_idx, dummy);
291   *cbp=NCBP[0][cbp_idx][1];
292 }
293 
294 /*!
295  ************************************************************************
296  * \par Input:
297  *    length and info
298  * \par Output:
299  *    level, run
300  ************************************************************************
301  */
linfo_levrun_inter(int len,int info,int * level,int * irun)302 void linfo_levrun_inter(int len, int info, int *level, int *irun)
303 {
304   //assert (((len >> 1) - 5) < 32);
305 
306   if (len <= 9)
307   {
308     int l2     = imax(0,(len >> 1)-1);
309     int inf    = info >> 1;
310 
311     *level = NTAB1[l2][inf][0];
312     *irun  = NTAB1[l2][inf][1];
313     if ((info & 0x01) == 1)
314       *level = -*level;                   // make sign
315   }
316   else                                  // if len > 9, skip using the array
317   {
318     *irun  = (info & 0x1e) >> 1;
319     *level = LEVRUN1[*irun] + (info >> 5) + ( 1 << ((len >> 1) - 5));
320     if ((info & 0x01) == 1)
321       *level = -*level;
322   }
323 
324   if (len == 1) // EOB
325     *level = 0;
326 }
327 
328 
329 /*!
330  ************************************************************************
331  * \par Input:
332  *    length and info
333  * \par Output:
334  *    level, run
335  ************************************************************************
336  */
linfo_levrun_c2x2(int len,int info,int * level,int * irun)337 void linfo_levrun_c2x2(int len, int info, int *level, int *irun)
338 {
339   if (len<=5)
340   {
341     int l2     = imax(0, (len >> 1) - 1);
342     int inf    = info >> 1;
343     *level = NTAB3[l2][inf][0];
344     *irun  = NTAB3[l2][inf][1];
345     if ((info & 0x01) == 1)
346       *level = -*level;                 // make sign
347   }
348   else                                  // if len > 5, skip using the array
349   {
350     *irun  = (info & 0x06) >> 1;
351     *level = LEVRUN3[*irun] + (info >> 3) + (1 << ((len >> 1) - 3));
352     if ((info & 0x01) == 1)
353       *level = -*level;
354   }
355 
356   if (len == 1) // EOB
357     *level = 0;
358 }
359 
360 /*!
361  ************************************************************************
362  * \brief
363  *    read next UVLC codeword from UVLC-partition and
364  *    map it to the corresponding syntax element
365  ************************************************************************
366  */
readSyntaxElement_VLC(SyntaxElement * sym,Bitstream * currStream)367 int readSyntaxElement_VLC(SyntaxElement *sym, Bitstream *currStream)
368 {
369 
370   sym->len =  GetVLCSymbol (currStream->streamBuffer, currStream->frame_bitoffset, &(sym->inf), currStream->bitstream_length);
371   if (sym->len == -1)
372     return -1;
373 
374   currStream->frame_bitoffset += sym->len;
375   sym->mapping(sym->len, sym->inf, &(sym->value1), &(sym->value2));
376 
377 #if TRACE
378   tracebits(sym->tracestring, sym->len, sym->inf, sym->value1);
379 #endif
380 
381   return 1;
382 }
383 
384 
385 /*!
386  ************************************************************************
387  * \brief
388  *    read next UVLC codeword from UVLC-partition and
389  *    map it to the corresponding syntax element
390  ************************************************************************
391  */
readSyntaxElement_UVLC(Macroblock * currMB,SyntaxElement * sym,struct datapartition_dec * dP)392 int readSyntaxElement_UVLC(Macroblock *currMB, SyntaxElement *sym, struct datapartition_dec *dP)
393 {
394   return (readSyntaxElement_VLC(sym, dP->bitstream));
395 }
396 
397 /*!
398  ************************************************************************
399  * \brief
400  *    read next VLC codeword for 4x4 Intra Prediction Mode and
401  *    map it to the corresponding Intra Prediction Direction
402  ************************************************************************
403  */
readSyntaxElement_Intra4x4PredictionMode(SyntaxElement * sym,Bitstream * currStream)404 int readSyntaxElement_Intra4x4PredictionMode(SyntaxElement *sym, Bitstream   *currStream)
405 {
406   sym->len = GetVLCSymbol_IntraMode (currStream->streamBuffer, currStream->frame_bitoffset, &(sym->inf), currStream->bitstream_length);
407 
408   if (sym->len == -1)
409     return -1;
410 
411   currStream->frame_bitoffset += sym->len;
412   sym->value1       = (sym->len == 1) ? -1 : sym->inf;
413 
414 #if TRACE
415   tracebits2(sym->tracestring, sym->len, sym->value1);
416 #endif
417 
418   return 1;
419 }
420 
GetVLCSymbol_IntraMode(byte buffer[],int totbitoffset,int * info,int bytecount)421 int GetVLCSymbol_IntraMode (byte buffer[],int totbitoffset,int *info, int bytecount)
422 {
423   int byteoffset = (totbitoffset >> 3);        // byte from start of buffer
424   int bitoffset   = (7 - (totbitoffset & 0x07)); // bit from start of byte
425   byte *cur_byte  = &(buffer[byteoffset]);
426   int ctr_bit     = (*cur_byte & (0x01 << bitoffset));      // control bit for current bit posision
427 
428   //First bit
429   if (ctr_bit)
430   {
431     *info = 0;
432     return 1;
433   }
434 
435   if (byteoffset >= bytecount)
436   {
437     return -1;
438   }
439   else
440   {
441     int inf = (*(cur_byte) << 8) + *(cur_byte + 1);
442     inf <<= (sizeof(byte) * 8) - bitoffset;
443     inf = inf & 0xFFFF;
444     inf >>= (sizeof(byte) * 8) * 2 - 3;
445 
446     *info = inf;
447     return 4;           // return absolute offset in bit from start of frame
448   }
449 }
450 
451 
452 /*!
453  ************************************************************************
454  * \brief
455  *    test if bit buffer contains only stop bit
456  *
457  * \param buffer
458  *    buffer containing VLC-coded data bits
459  * \param totbitoffset
460  *    bit offset from start of partition
461  * \param bytecount
462  *    buffer length
463  * \return
464  *    true if more bits available
465  ************************************************************************
466  */
more_rbsp_data(byte buffer[],int totbitoffset,int bytecount)467 int more_rbsp_data (byte buffer[],int totbitoffset,int bytecount)
468 {
469   long byteoffset = (totbitoffset >> 3);      // byte from start of buffer
470   // there is more until we're in the last byte
471   if (byteoffset < (bytecount - 1))
472     return TRUE;
473   else
474   {
475     int bitoffset   = (7 - (totbitoffset & 0x07));      // bit from start of byte
476     byte *cur_byte  = &(buffer[byteoffset]);
477     // read one bit
478     int ctr_bit     = ctr_bit = ((*cur_byte)>> (bitoffset--)) & 0x01;      // control bit for current bit posision
479 
480     //assert (byteoffset<bytecount);
481 
482     // a stop bit has to be one
483     if (ctr_bit==0)
484       return TRUE;
485     else
486     {
487       int cnt = 0;
488 
489       while (bitoffset>=0 && !cnt)
490       {
491         cnt |= ((*cur_byte)>> (bitoffset--)) & 0x01;   // set up control bit
492       }
493 
494       return (cnt);
495     }
496   }
497 }
498 
499 
500 /*!
501  ************************************************************************
502  * \brief
503  *    Check if there are symbols for the next MB
504  ************************************************************************
505  */
uvlc_startcode_follows(Slice * currSlice,int dummy)506 int uvlc_startcode_follows(Slice *currSlice, int dummy)
507 {
508   byte            dp_Nr = assignSE2partition[currSlice->dp_mode][SE_MBTYPE];
509   DataPartition     *dP = &(currSlice->partArr[dp_Nr]);
510   Bitstream *currStream = dP->bitstream;
511   byte             *buf = currStream->streamBuffer;
512 
513   return (!(more_rbsp_data(buf, currStream->frame_bitoffset,currStream->bitstream_length)));
514 }
515 
516 
517 
518 /*!
519  ************************************************************************
520  * \brief
521  *  read one exp-golomb VLC symbol
522  *
523  * \param buffer
524  *    containing VLC-coded data bits
525  * \param totbitoffset
526  *    bit offset from start of partition
527  * \param  info
528  *    returns the value of the symbol
529  * \param bytecount
530  *    buffer length
531  * \return
532  *    bits read
533  ************************************************************************
534  */
GetVLCSymbol(byte buffer[],int totbitoffset,int * info,int bytecount)535 int GetVLCSymbol (byte buffer[],int totbitoffset,int *info, int bytecount)
536 {
537   long byteoffset = (totbitoffset >> 3);         // byte from start of buffer
538   int  bitoffset  = (7 - (totbitoffset & 0x07)); // bit from start of byte
539   int  bitcounter = 1;
540   int  len        = 0;
541   byte *cur_byte  = &(buffer[byteoffset]);
542   int  ctr_bit    = ((*cur_byte) >> (bitoffset)) & 0x01;  // control bit for current bit posision
543 
544   while (ctr_bit == 0)
545   {                 // find leading 1 bit
546     len++;
547     bitcounter++;
548     bitoffset--;
549     bitoffset &= 0x07;
550     cur_byte  += (bitoffset == 7);
551     byteoffset+= (bitoffset == 7);
552     ctr_bit    = ((*cur_byte) >> (bitoffset)) & 0x01;
553   }
554 
555   if (byteoffset + ((len + 7) >> 3) > bytecount)
556     return -1;
557   else
558   {
559     // make infoword
560     int inf = 0;                          // shortest possible code is 1, then info is always 0
561 
562     while (len--)
563     {
564       bitoffset --;
565       bitoffset &= 0x07;
566       cur_byte  += (bitoffset == 7);
567       bitcounter++;
568       inf <<= 1;
569       inf |= ((*cur_byte) >> (bitoffset)) & 0x01;
570     }
571 
572     *info = inf;
573     return bitcounter;           // return absolute offset in bit from start of frame
574   }
575 }
576 
577 
578 /*!
579  ************************************************************************
580  * \brief
581  *  Reads bits from the bitstream buffer (Threshold based)
582  *
583  * \param inf
584  *    bytes to extract numbits from with bitoffset already applied
585  * \param numbits
586  *    number of bits to read
587  *
588  ************************************************************************
589  */
590 
591 //static inline int ShowBitsThres (int inf, int bitcount, int numbits)
ShowBitsThres(int inf,int numbits)592 static inline int ShowBitsThres (int inf, int numbits)
593 {
594   return ((inf) >> ((sizeof(byte) * 24) - (numbits)));
595   /*
596   if ((numbits + 7) > bitcount)
597   {
598     return -1;
599   }
600   else
601   {
602     //Worst case scenario is that we will need to traverse 3 bytes
603     inf >>= (sizeof(byte)*8)*3 - numbits;
604   }
605 
606   return inf; //Will be a small unsigned integer so will not need any conversion when returning as int
607   */
608 }
609 
610 
611 /*!
612  ************************************************************************
613  * \brief
614  *    code from bitstream (2d tables)
615  ************************************************************************
616  */
617 
code_from_bitstream_2d(SyntaxElement * sym,Bitstream * currStream,const byte * lentab,const byte * codtab,int tabwidth,int tabheight,int * code)618 static int code_from_bitstream_2d(SyntaxElement *sym,
619                                   Bitstream *currStream,
620                                   const byte *lentab,
621                                   const byte *codtab,
622                                   int tabwidth,
623                                   int tabheight,
624                                   int *code)
625 {
626   int i, j;
627   const byte *len = &lentab[0], *cod = &codtab[0];
628 
629   int *frame_bitoffset = &currStream->frame_bitoffset;
630   byte *buf            = &currStream->streamBuffer[*frame_bitoffset >> 3];
631 
632   //Apply bitoffset to three bytes (maximum that may be traversed by ShowBitsThres)
633   unsigned int inf = ((*buf) << 16) + (*(buf + 1) << 8) + *(buf + 2); //Even at the end of a stream we will still be pulling out of allocated memory as alloc is done by MAX_CODED_FRAME_SIZE
634   inf <<= (*frame_bitoffset & 0x07);                                  //Offset is constant so apply before extracting different numbers of bits
635   inf  &= 0xFFFFFF;                                                   //Arithmetic shift so wipe any sign which may be extended inside ShowBitsThres
636 
637   // this VLC decoding method is not optimized for speed
638   for (j = 0; j < tabheight; j++)
639   {
640     for (i = 0; i < tabwidth; i++)
641     {
642       if ((*len == 0) || (ShowBitsThres(inf, (int) *len) != *cod))
643       {
644         ++len;
645         ++cod;
646       }
647       else
648       {
649         sym->len = *len;
650         *frame_bitoffset += *len; // move bitstream pointer
651         *code = *cod;
652         sym->value1 = i;
653         sym->value2 = j;
654         return 0;                 // found code and return
655       }
656     }
657   }
658   return -1;  // failed to find code
659 }
660 
661 
662 /*!
663  ************************************************************************
664  * \brief
665  *    read FLC codeword from UVLC-partition
666  ************************************************************************
667  */
readSyntaxElement_FLC(SyntaxElement * sym,Bitstream * currStream)668 int readSyntaxElement_FLC(SyntaxElement *sym, Bitstream *currStream)
669 {
670   int BitstreamLengthInBits  = (currStream->bitstream_length << 3) + 7;
671 
672   if ((GetBits(currStream->streamBuffer, currStream->frame_bitoffset, &(sym->inf), BitstreamLengthInBits, sym->len)) < 0)
673     return -1;
674 
675   sym->value1 = sym->inf;
676   currStream->frame_bitoffset += sym->len; // move bitstream pointer
677 
678 #if TRACE
679   tracebits2(sym->tracestring, sym->len, sym->inf);
680 #endif
681 
682   return 1;
683 }
684 
685 
686 
687 /*!
688  ************************************************************************
689  * \brief
690  *    read NumCoeff/TrailingOnes codeword from UVLC-partition
691  ************************************************************************
692  */
693 
readSyntaxElement_NumCoeffTrailingOnes(SyntaxElement * sym,Bitstream * currStream,char * type)694 int readSyntaxElement_NumCoeffTrailingOnes(SyntaxElement *sym,
695                                            Bitstream *currStream,
696                                            char *type)
697 {
698   int frame_bitoffset        = currStream->frame_bitoffset;
699   int BitstreamLengthInBytes = currStream->bitstream_length;
700   int BitstreamLengthInBits  = (BitstreamLengthInBytes << 3) + 7;
701   byte *buf                  = currStream->streamBuffer;
702 
703   static const byte lentab[3][4][17] =
704   {
705     {   // 0702
706       { 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
707       { 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
708       { 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
709       { 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16},
710     },
711     {
712       { 2, 6, 6, 7, 8, 8, 9,11,11,12,12,12,13,13,13,14,14},
713       { 0, 2, 5, 6, 6, 7, 8, 9,11,11,12,12,13,13,14,14,14},
714       { 0, 0, 3, 6, 6, 7, 8, 9,11,11,12,12,13,13,13,14,14},
715       { 0, 0, 0, 4, 4, 5, 6, 6, 7, 9,11,11,12,13,13,13,14},
716     },
717     {
718       { 4, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9,10,10,10,10},
719       { 0, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8, 9, 9, 9,10,10,10},
720       { 0, 0, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,10},
721       { 0, 0, 0, 4, 4, 4, 4, 4, 5, 6, 7, 8, 8, 9,10,10,10},
722     },
723   };
724 
725   static const byte codtab[3][4][17] =
726   {
727     {
728       { 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7,4},
729       { 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10,6},
730       { 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9,5},
731       { 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12,8},
732     },
733     {
734       { 3,11, 7, 7, 7, 4, 7,15,11,15,11, 8,15,11, 7, 9,7},
735       { 0, 2, 7,10, 6, 6, 6, 6,14,10,14,10,14,10,11, 8,6},
736       { 0, 0, 3, 9, 5, 5, 5, 5,13, 9,13, 9,13, 9, 6,10,5},
737       { 0, 0, 0, 5, 4, 6, 8, 4, 4, 4,12, 8,12,12, 8, 1,4},
738     },
739     {
740       {15,15,11, 8,15,11, 9, 8,15,11,15,11, 8,13, 9, 5,1},
741       { 0,14,15,12,10, 8,14,10,14,14,10,14,10, 7,12, 8,4},
742       { 0, 0,13,14,11, 9,13, 9,13,10,13, 9,13, 9,11, 7,3},
743       { 0, 0, 0,12,11,10, 9, 8,13,12,12,12, 8,12,10, 6,2},
744     },
745   };
746 
747   int retval = 0, code;
748   int vlcnum = sym->value1;
749   // vlcnum is the index of Table used to code coeff_token
750   // vlcnum==3 means (8<=nC) which uses 6bit FLC
751 
752   if (vlcnum == 3)
753   {
754     // read 6 bit FLC
755     //code = ShowBits(buf, frame_bitoffset, BitstreamLengthInBytes, 6);
756     code = ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, 6);
757     currStream->frame_bitoffset += 6;
758     sym->value2 = (code & 3);
759     sym->value1 = (code >> 2);
760 
761     if (!sym->value1 && sym->value2 == 3)
762     {
763       // #c = 0, #t1 = 3 =>  #c = 0
764       sym->value2 = 0;
765     }
766     else
767       sym->value1++;
768 
769     sym->len = 6;
770   }
771   else
772   {
773     //retval = code_from_bitstream_2d(sym, currStream, &lentab[vlcnum][0][0], &codtab[vlcnum][0][0], 17, 4, &code);
774     retval = code_from_bitstream_2d(sym, currStream, lentab[vlcnum][0], codtab[vlcnum][0], 17, 4, &code);
775     if (retval)
776     {
777       printf("ERROR: failed to find NumCoeff/TrailingOnes\n");
778       exit(-1);
779     }
780   }
781 
782 #if TRACE
783   snprintf(sym->tracestring, TRACESTRING_SIZE, "%s # c & tr.1s vlc=%d #c=%d #t1=%d",
784            type, vlcnum, sym->value1, sym->value2);
785   tracebits2(sym->tracestring, sym->len, code);
786 #endif
787 
788   return retval;
789 }
790 
791 
792 /*!
793  ************************************************************************
794  * \brief
795  *    read NumCoeff/TrailingOnes codeword from UVLC-partition ChromaDC
796  ************************************************************************
797  */
readSyntaxElement_NumCoeffTrailingOnesChromaDC(VideoParameters * p_Vid,SyntaxElement * sym,Bitstream * currStream)798 int readSyntaxElement_NumCoeffTrailingOnesChromaDC(VideoParameters *p_Vid, SyntaxElement *sym,  Bitstream *currStream)
799 {
800   static const byte lentab[3][4][17] =
801   {
802     //YUV420
803     {{ 2, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
804     { 0, 1, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
805     { 0, 0, 3, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
806     { 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
807     //YUV422
808     {{ 1, 7, 7, 9, 9,10,11,12,13, 0, 0, 0, 0, 0, 0, 0, 0},
809     { 0, 2, 7, 7, 9,10,11,12,12, 0, 0, 0, 0, 0, 0, 0, 0},
810     { 0, 0, 3, 7, 7, 9,10,11,12, 0, 0, 0, 0, 0, 0, 0, 0},
811     { 0, 0, 0, 5, 6, 7, 7,10,11, 0, 0, 0, 0, 0, 0, 0, 0}},
812     //YUV444
813     {{ 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
814     { 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
815     { 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
816     { 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16}}
817   };
818 
819   static const byte codtab[3][4][17] =
820   {
821     //YUV420
822     {{ 1, 7, 4, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
823     { 0, 1, 6, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
824     { 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
825     { 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
826     //YUV422
827     {{ 1,15,14, 7, 6, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0},
828     { 0, 1,13,12, 5, 6, 6, 6, 5, 0, 0, 0, 0, 0, 0, 0, 0},
829     { 0, 0, 1,11,10, 4, 5, 5, 4, 0, 0, 0, 0, 0, 0, 0, 0},
830     { 0, 0, 0, 1, 1, 9, 8, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0}},
831     //YUV444
832     {{ 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7, 4},
833     { 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10, 6},
834     { 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9, 5},
835     { 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12, 8}}
836 
837   };
838 
839   int code;
840   int yuv = p_Vid->active_sps->chroma_format_idc - 1;
841   int retval = code_from_bitstream_2d(sym, currStream, &lentab[yuv][0][0], &codtab[yuv][0][0], 17, 4, &code);
842 
843   if (retval)
844   {
845     printf("ERROR: failed to find NumCoeff/TrailingOnes ChromaDC\n");
846     exit(-1);
847   }
848 
849 #if TRACE
850   snprintf(sym->tracestring, TRACESTRING_SIZE, "ChrDC # c & tr.1s  #c=%d #t1=%d",
851     sym->value1, sym->value2);
852   tracebits2(sym->tracestring, sym->len, code);
853 
854 #endif
855 
856   return retval;
857 }
858 
859 /*!
860  ************************************************************************
861  * \brief
862  *    read Level VLC0 codeword from UVLC-partition
863  ************************************************************************
864  */
readSyntaxElement_Level_VLC0(SyntaxElement * sym,Bitstream * currStream)865 int readSyntaxElement_Level_VLC0(SyntaxElement *sym, Bitstream *currStream)
866 {
867   int frame_bitoffset        = currStream->frame_bitoffset;
868   int BitstreamLengthInBytes = currStream->bitstream_length;
869   int BitstreamLengthInBits  = (BitstreamLengthInBytes << 3) + 7;
870   byte *buf                  = currStream->streamBuffer;
871   int len = 1, sign = 0, level = 0, code = 1;
872 
873   while (!ShowBits(buf, frame_bitoffset++, BitstreamLengthInBits, 1))
874     len++;
875 
876   if (len < 15)
877   {
878     sign  = (len - 1) & 1;
879     level = ((len - 1) >> 1) + 1;
880   }
881   else if (len == 15)
882   {
883     // escape code
884     code <<= 4;
885     code |= ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, 4);
886     len  += 4;
887     frame_bitoffset += 4;
888     sign = (code & 0x01);
889     level = ((code >> 1) & 0x07) + 8;
890   }
891   else if (len >= 16)
892   {
893     // escape code
894     int addbit = (len - 16);
895     int offset = (2048 << addbit) - 2032;
896     len   -= 4;
897     code   = ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, len);
898     sign   = (code & 0x01);
899     frame_bitoffset += len;
900     level = (code >> 1) + offset;
901 
902     code |= (1 << (len)); // for display purpose only
903     len += addbit + 16;
904  }
905 
906   sym->inf = (sign) ? -level : level ;
907   sym->len = len;
908 
909 #if TRACE
910   tracebits2(sym->tracestring, sym->len, code);
911 #endif
912 
913   currStream->frame_bitoffset = frame_bitoffset;
914   return 0;
915 }
916 
917 /*!
918  ************************************************************************
919  * \brief
920  *    read Level VLC codeword from UVLC-partition
921  ************************************************************************
922  */
readSyntaxElement_Level_VLCN(SyntaxElement * sym,int vlc,Bitstream * currStream)923 int readSyntaxElement_Level_VLCN(SyntaxElement *sym, int vlc, Bitstream *currStream)
924 {
925   int frame_bitoffset        = currStream->frame_bitoffset;
926   int BitstreamLengthInBytes = currStream->bitstream_length;
927   int BitstreamLengthInBits  = (BitstreamLengthInBytes << 3) + 7;
928   byte *buf                  = currStream->streamBuffer;
929 
930   int levabs, sign;
931   int len = 1;
932   int code = 1, sb;
933 
934   int shift = vlc - 1;
935 
936   // read pre zeros
937   while (!ShowBits(buf, frame_bitoffset ++, BitstreamLengthInBits, 1))
938     len++;
939 
940   frame_bitoffset -= len;
941 
942   if (len < 16)
943   {
944     levabs = ((len - 1) << shift) + 1;
945 
946     // read (vlc-1) bits -> suffix
947     if (shift)
948     {
949       sb =  ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, shift);
950       code = (code << (shift) )| sb;
951       levabs += sb;
952       len += (shift);
953     }
954 
955     // read 1 bit -> sign
956     sign = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, 1);
957     code = (code << 1)| sign;
958     len ++;
959   }
960   else // escape
961   {
962     int addbit = len - 5;
963     int offset = (1 << addbit) + (15 << shift) - 2047;
964 
965     sb = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, addbit);
966     code = (code << addbit ) | sb;
967     len   += addbit;
968 
969     levabs = sb + offset;
970 
971     // read 1 bit -> sign
972     sign = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, 1);
973 
974     code = (code << 1)| sign;
975 
976     len++;
977   }
978 
979   sym->inf = (sign)? -levabs : levabs;
980   sym->len = len;
981 
982   currStream->frame_bitoffset = frame_bitoffset + len;
983 
984 #if TRACE
985   tracebits2(sym->tracestring, sym->len, code);
986 #endif
987 
988   return 0;
989 }
990 
991 /*!
992  ************************************************************************
993  * \brief
994  *    read Total Zeros codeword from UVLC-partition
995  ************************************************************************
996  */
readSyntaxElement_TotalZeros(SyntaxElement * sym,Bitstream * currStream)997 int readSyntaxElement_TotalZeros(SyntaxElement *sym,  Bitstream *currStream)
998 {
999   static const byte lentab[TOTRUN_NUM][16] =
1000   {
1001 
1002     { 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
1003     { 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
1004     { 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
1005     { 5,3,4,4,3,3,3,4,3,4,5,5,5},
1006     { 4,4,4,3,3,3,3,3,4,5,4,5},
1007     { 6,5,3,3,3,3,3,3,4,3,6},
1008     { 6,5,3,3,3,2,3,4,3,6},
1009     { 6,4,5,3,2,2,3,3,6},
1010     { 6,6,4,2,2,3,2,5},
1011     { 5,5,3,2,2,2,4},
1012     { 4,4,3,3,1,3},
1013     { 4,4,2,1,3},
1014     { 3,3,1,2},
1015     { 2,2,1},
1016     { 1,1},
1017   };
1018 
1019   static const byte codtab[TOTRUN_NUM][16] =
1020   {
1021     {1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
1022     {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
1023     {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
1024     {3,7,5,4,6,5,4,3,3,2,2,1,0},
1025     {5,4,3,7,6,5,4,3,2,1,1,0},
1026     {1,1,7,6,5,4,3,2,1,1,0},
1027     {1,1,5,4,3,3,2,1,1,0},
1028     {1,1,1,3,3,2,2,1,0},
1029     {1,0,1,3,2,1,1,1,},
1030     {1,0,1,3,2,1,1,},
1031     {0,1,1,2,1,3},
1032     {0,1,1,1,1},
1033     {0,1,1,1},
1034     {0,1,1},
1035     {0,1},
1036   };
1037 
1038   int code;
1039   int vlcnum = sym->value1;
1040   int retval = code_from_bitstream_2d(sym, currStream, &lentab[vlcnum][0], &codtab[vlcnum][0], 16, 1, &code);
1041 
1042   if (retval)
1043   {
1044     printf("ERROR: failed to find Total Zeros !cdc\n");
1045     exit(-1);
1046   }
1047 
1048 #if TRACE
1049   tracebits2(sym->tracestring, sym->len, code);
1050 #endif
1051 
1052   return retval;
1053 }
1054 
1055 /*!
1056  ************************************************************************
1057  * \brief
1058  *    read Total Zeros Chroma DC codeword from UVLC-partition
1059  ************************************************************************
1060  */
readSyntaxElement_TotalZerosChromaDC(VideoParameters * p_Vid,SyntaxElement * sym,Bitstream * currStream)1061 int readSyntaxElement_TotalZerosChromaDC(VideoParameters *p_Vid, SyntaxElement *sym,  Bitstream *currStream)
1062 {
1063   static const byte lentab[3][TOTRUN_NUM][16] =
1064   {
1065     //YUV420
1066    {{ 1,2,3,3},
1067     { 1,2,2},
1068     { 1,1}},
1069     //YUV422
1070    {{ 1,3,3,4,4,4,5,5},
1071     { 3,2,3,3,3,3,3},
1072     { 3,3,2,2,3,3},
1073     { 3,2,2,2,3},
1074     { 2,2,2,2},
1075     { 2,2,1},
1076     { 1,1}},
1077     //YUV444
1078    {{ 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
1079     { 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
1080     { 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
1081     { 5,3,4,4,3,3,3,4,3,4,5,5,5},
1082     { 4,4,4,3,3,3,3,3,4,5,4,5},
1083     { 6,5,3,3,3,3,3,3,4,3,6},
1084     { 6,5,3,3,3,2,3,4,3,6},
1085     { 6,4,5,3,2,2,3,3,6},
1086     { 6,6,4,2,2,3,2,5},
1087     { 5,5,3,2,2,2,4},
1088     { 4,4,3,3,1,3},
1089     { 4,4,2,1,3},
1090     { 3,3,1,2},
1091     { 2,2,1},
1092     { 1,1}}
1093   };
1094 
1095   static const byte codtab[3][TOTRUN_NUM][16] =
1096   {
1097     //YUV420
1098    {{ 1,1,1,0},
1099     { 1,1,0},
1100     { 1,0}},
1101     //YUV422
1102    {{ 1,2,3,2,3,1,1,0},
1103     { 0,1,1,4,5,6,7},
1104     { 0,1,1,2,6,7},
1105     { 6,0,1,2,7},
1106     { 0,1,2,3},
1107     { 0,1,1},
1108     { 0,1}},
1109     //YUV444
1110    {{1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
1111     {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
1112     {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
1113     {3,7,5,4,6,5,4,3,3,2,2,1,0},
1114     {5,4,3,7,6,5,4,3,2,1,1,0},
1115     {1,1,7,6,5,4,3,2,1,1,0},
1116     {1,1,5,4,3,3,2,1,1,0},
1117     {1,1,1,3,3,2,2,1,0},
1118     {1,0,1,3,2,1,1,1,},
1119     {1,0,1,3,2,1,1,},
1120     {0,1,1,2,1,3},
1121     {0,1,1,1,1},
1122     {0,1,1,1},
1123     {0,1,1},
1124     {0,1}}
1125   };
1126 
1127   int code;
1128   int yuv = p_Vid->active_sps->chroma_format_idc - 1;
1129   int vlcnum = sym->value1;
1130   int retval = code_from_bitstream_2d(sym, currStream, &lentab[yuv][vlcnum][0], &codtab[yuv][vlcnum][0], 16, 1, &code);
1131 
1132   if (retval)
1133   {
1134     printf("ERROR: failed to find Total Zeros\n");
1135     exit(-1);
1136   }
1137 
1138 #if TRACE
1139   tracebits2(sym->tracestring, sym->len, code);
1140 #endif
1141 
1142   return retval;
1143 }
1144 
1145 
1146 /*!
1147  ************************************************************************
1148  * \brief
1149  *    read  Run codeword from UVLC-partition
1150  ************************************************************************
1151  */
readSyntaxElement_Run(SyntaxElement * sym,Bitstream * currStream)1152 int readSyntaxElement_Run(SyntaxElement *sym, Bitstream *currStream)
1153 {
1154   static const byte lentab[TOTRUN_NUM][16] =
1155   {
1156     {1,1},
1157     {1,2,2},
1158     {2,2,2,2},
1159     {2,2,2,3,3},
1160     {2,2,3,3,3,3},
1161     {2,3,3,3,3,3,3},
1162     {3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
1163   };
1164 
1165   static const byte codtab[TOTRUN_NUM][16] =
1166   {
1167     {1,0},
1168     {1,1,0},
1169     {3,2,1,0},
1170     {3,2,1,1,0},
1171     {3,2,3,2,1,0},
1172     {3,0,1,3,2,5,4},
1173     {7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
1174   };
1175   int code;
1176   int vlcnum = sym->value1;
1177   int retval = code_from_bitstream_2d(sym, currStream, &lentab[vlcnum][0], &codtab[vlcnum][0], 16, 1, &code);
1178 
1179   if (retval)
1180   {
1181     printf("ERROR: failed to find Run\n");
1182     exit(-1);
1183   }
1184 
1185 #if TRACE
1186   tracebits2(sym->tracestring, sym->len, code);
1187 #endif
1188 
1189   return retval;
1190 }
1191 
1192 
1193 /*!
1194  ************************************************************************
1195  * \brief
1196  *  Reads bits from the bitstream buffer
1197  *
1198  * \param buffer
1199  *    containing VLC-coded data bits
1200  * \param totbitoffset
1201  *    bit offset from start of partition
1202  * \param info
1203  *    returns value of the read bits
1204  * \param bitcount
1205  *    total bytes in bitstream
1206  * \param numbits
1207  *    number of bits to read
1208  *
1209  ************************************************************************
1210  */
GetBits(byte buffer[],int totbitoffset,int * info,int bitcount,int numbits)1211 int GetBits (byte buffer[],int totbitoffset,int *info, int bitcount,
1212              int numbits)
1213 {
1214   if ((totbitoffset + numbits ) > bitcount)
1215   {
1216     return -1;
1217   }
1218   else
1219   {
1220     int bitoffset  = 7 - (totbitoffset & 0x07); // bit from start of byte
1221     int byteoffset = (totbitoffset >> 3); // byte from start of buffer
1222     int bitcounter = numbits;
1223     byte *curbyte  = &(buffer[byteoffset]);
1224     int inf = 0;
1225 
1226     while (numbits--)
1227     {
1228       inf <<=1;
1229       inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
1230       if (bitoffset == -1 )
1231       { //Move onto next byte to get all of numbits
1232         curbyte++;
1233         bitoffset = 7;
1234       }
1235       // Above conditional could also be avoided using the following:
1236       // curbyte   -= (bitoffset >> 3);
1237       // bitoffset &= 0x07;
1238     }
1239     *info = inf;
1240 
1241     return bitcounter;           // return absolute offset in bit from start of frame
1242   }
1243 }
1244 
1245 /*!
1246  ************************************************************************
1247  * \brief
1248  *  Reads bits from the bitstream buffer
1249  *
1250  * \param buffer
1251  *    buffer containing VLC-coded data bits
1252  * \param totbitoffset
1253  *    bit offset from start of partition
1254  * \param bitcount
1255  *    total bytes in bitstream
1256  * \param numbits
1257  *    number of bits to read
1258  *
1259  ************************************************************************
1260  */
1261 
ShowBits(byte buffer[],int totbitoffset,int bitcount,int numbits)1262 int ShowBits (byte buffer[],int totbitoffset,int bitcount, int numbits)
1263 {
1264   if ((totbitoffset + numbits )  > bitcount)
1265   {
1266     return -1;
1267   }
1268   else
1269   {
1270     int bitoffset  = 7 - (totbitoffset & 0x07); // bit from start of byte
1271     int byteoffset = (totbitoffset >> 3); // byte from start of buffer
1272     byte *curbyte  = &(buffer[byteoffset]);
1273     int inf        = 0;
1274 
1275     while (numbits--)
1276     {
1277       inf <<=1;
1278       inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
1279 
1280       if (bitoffset == -1 )
1281       { //Move onto next byte to get all of numbits
1282         curbyte++;
1283         bitoffset = 7;
1284       }
1285     }
1286     return inf;           // return absolute offset in bit from start of frame
1287   }
1288 }
1289 
1290