00001
00015 #include "contributors.h"
00016
00017 #include "global.h"
00018 #include "vlc.h"
00019 #include "elements.h"
00020
00021
00022
00023 #if TRACE
00024 #define SYMTRACESTRING(s) strncpy(symbol.tracestring,s,TRACESTRING_SIZE)
00025 #else
00026 #define SYMTRACESTRING(s) // do nothing
00027 #endif
00028
00029
00030
00048 int ue_v (char *tracestring, Bitstream *bitstream)
00049 {
00050 SyntaxElement symbol;
00051
00052
00053 symbol.type = SE_HEADER;
00054 symbol.mapping = linfo_ue;
00055 SYMTRACESTRING(tracestring);
00056 readSyntaxElement_VLC (&symbol, bitstream);
00057 p_Dec->UsedBits+=symbol.len;
00058 return symbol.value1;
00059 }
00060
00061
00079 int se_v (char *tracestring, Bitstream *bitstream)
00080 {
00081 SyntaxElement symbol;
00082
00083
00084 symbol.type = SE_HEADER;
00085 symbol.mapping = linfo_se;
00086 SYMTRACESTRING(tracestring);
00087 readSyntaxElement_VLC (&symbol, bitstream);
00088 p_Dec->UsedBits+=symbol.len;
00089 return symbol.value1;
00090 }
00091
00092
00113 int u_v (int LenInBits, char*tracestring, Bitstream *bitstream)
00114 {
00115 SyntaxElement symbol;
00116 symbol.inf = 0;
00117
00118
00119 symbol.type = SE_HEADER;
00120 symbol.mapping = linfo_ue;
00121 symbol.len = LenInBits;
00122 SYMTRACESTRING(tracestring);
00123 readSyntaxElement_FLC (&symbol, bitstream);
00124 p_Dec->UsedBits+=symbol.len;
00125 return symbol.inf;
00126 }
00127
00148 int i_v (int LenInBits, char*tracestring, Bitstream *bitstream)
00149 {
00150 SyntaxElement symbol;
00151
00152 symbol.inf = 0;
00153
00154
00155 symbol.type = SE_HEADER;
00156 symbol.mapping = linfo_ue;
00157 symbol.len = LenInBits;
00158 SYMTRACESTRING(tracestring);
00159 readSyntaxElement_FLC (&symbol, bitstream);
00160 p_Dec->UsedBits+=symbol.len;
00161
00162
00163 symbol.inf = -( symbol.inf & (1 << (LenInBits - 1)) ) | symbol.inf;
00164
00165 return symbol.inf;
00166 }
00167
00168
00186 Boolean u_1 (char *tracestring, Bitstream *bitstream)
00187 {
00188 return (Boolean) u_v (1, tracestring, bitstream);
00189 }
00190
00191
00192
00203 void linfo_ue(int len, int info, int *value1, int *dummy)
00204 {
00205
00206 *value1 = (int) (((unsigned int) 1 << (len >> 1)) + (unsigned int) (info) - 1);
00207 }
00208
00219 void linfo_se(int len, int info, int *value1, int *dummy)
00220 {
00221
00222 unsigned int n = ((unsigned int) 1 << (len >> 1)) + (unsigned int) info - 1;
00223 *value1 = (n + 1) >> 1;
00224 if((n & 0x01) == 0)
00225 *value1 = -*value1;
00226 }
00227
00228
00237 void linfo_cbp_intra_normal(int len,int info,int *cbp, int *dummy)
00238 {
00239 int cbp_idx;
00240
00241 linfo_ue(len, info, &cbp_idx, dummy);
00242 *cbp=NCBP[1][cbp_idx][0];
00243 }
00244
00245
00254 void linfo_cbp_intra_other(int len,int info,int *cbp, int *dummy)
00255 {
00256 int cbp_idx;
00257
00258 linfo_ue(len, info, &cbp_idx, dummy);
00259 *cbp=NCBP[0][cbp_idx][0];
00260 }
00261
00270 void linfo_cbp_inter_normal(int len,int info,int *cbp, int *dummy)
00271 {
00272 int cbp_idx;
00273
00274 linfo_ue(len, info, &cbp_idx, dummy);
00275 *cbp=NCBP[1][cbp_idx][1];
00276 }
00277
00286 void linfo_cbp_inter_other(int len,int info,int *cbp, int *dummy)
00287 {
00288 int cbp_idx;
00289
00290 linfo_ue(len, info, &cbp_idx, dummy);
00291 *cbp=NCBP[0][cbp_idx][1];
00292 }
00293
00302 void linfo_levrun_inter(int len, int info, int *level, int *irun)
00303 {
00304
00305
00306 if (len <= 9)
00307 {
00308 int l2 = imax(0,(len >> 1)-1);
00309 int inf = info >> 1;
00310
00311 *level = NTAB1[l2][inf][0];
00312 *irun = NTAB1[l2][inf][1];
00313 if ((info & 0x01) == 1)
00314 *level = -*level;
00315 }
00316 else
00317 {
00318 *irun = (info & 0x1e) >> 1;
00319 *level = LEVRUN1[*irun] + (info >> 5) + ( 1 << ((len >> 1) - 5));
00320 if ((info & 0x01) == 1)
00321 *level = -*level;
00322 }
00323
00324 if (len == 1)
00325 *level = 0;
00326 }
00327
00328
00337 void linfo_levrun_c2x2(int len, int info, int *level, int *irun)
00338 {
00339 if (len<=5)
00340 {
00341 int l2 = imax(0, (len >> 1) - 1);
00342 int inf = info >> 1;
00343 *level = NTAB3[l2][inf][0];
00344 *irun = NTAB3[l2][inf][1];
00345 if ((info & 0x01) == 1)
00346 *level = -*level;
00347 }
00348 else
00349 {
00350 *irun = (info & 0x06) >> 1;
00351 *level = LEVRUN3[*irun] + (info >> 3) + (1 << ((len >> 1) - 3));
00352 if ((info & 0x01) == 1)
00353 *level = -*level;
00354 }
00355
00356 if (len == 1)
00357 *level = 0;
00358 }
00359
00367 int readSyntaxElement_VLC(SyntaxElement *sym, Bitstream *currStream)
00368 {
00369
00370 sym->len = GetVLCSymbol (currStream->streamBuffer, currStream->frame_bitoffset, &(sym->inf), currStream->bitstream_length);
00371 if (sym->len == -1)
00372 return -1;
00373
00374 currStream->frame_bitoffset += sym->len;
00375 sym->mapping(sym->len, sym->inf, &(sym->value1), &(sym->value2));
00376
00377 #if TRACE
00378 tracebits(sym->tracestring, sym->len, sym->inf, sym->value1);
00379 #endif
00380
00381 return 1;
00382 }
00383
00384
00392 int readSyntaxElement_UVLC(SyntaxElement *sym, ImageParameters *p_Img, struct datapartition *dP)
00393 {
00394 return (readSyntaxElement_VLC(sym, dP->bitstream));
00395 }
00396
00404 int readSyntaxElement_Intra4x4PredictionMode(SyntaxElement *sym, ImageParameters *p_Img, Bitstream *currStream)
00405 {
00406 sym->len = GetVLCSymbol_IntraMode (currStream->streamBuffer, currStream->frame_bitoffset, &(sym->inf), currStream->bitstream_length);
00407
00408 if (sym->len == -1)
00409 return -1;
00410
00411 currStream->frame_bitoffset += sym->len;
00412 sym->value1 = (sym->len == 1) ? -1 : sym->inf;
00413
00414 #if TRACE
00415 tracebits2(sym->tracestring, sym->len, sym->value1);
00416 #endif
00417
00418 return 1;
00419 }
00420
00421 int GetVLCSymbol_IntraMode (byte buffer[],int totbitoffset,int *info, int bytecount)
00422 {
00423 int byteoffset = (totbitoffset >> 3);
00424 int bitoffset = (7 - (totbitoffset & 0x07));
00425 byte *cur_byte = &(buffer[byteoffset]);
00426 int ctr_bit = (*cur_byte & (0x01 << bitoffset));
00427
00428
00429 if (ctr_bit)
00430 {
00431 *info = 0;
00432 return 1;
00433 }
00434
00435 if (byteoffset >= bytecount)
00436 {
00437 return -1;
00438 }
00439 else
00440 {
00441 int inf = (*(cur_byte) << 8) + *(cur_byte + 1);
00442 inf <<= (sizeof(byte) * 8) - bitoffset;
00443 inf = inf & 0xFFFF;
00444 inf >>= (sizeof(byte) * 8) * 2 - 3;
00445
00446 *info = inf;
00447 return 4;
00448 }
00449 }
00450
00451
00467 int more_rbsp_data (byte buffer[],int totbitoffset,int bytecount)
00468 {
00469 long byteoffset = (totbitoffset >> 3);
00470
00471 if (byteoffset < (bytecount - 1))
00472 return TRUE;
00473 else
00474 {
00475 int bitoffset = (7 - (totbitoffset & 0x07));
00476 byte *cur_byte = &(buffer[byteoffset]);
00477
00478 int ctr_bit = ctr_bit = ((*cur_byte)>> (bitoffset--)) & 0x01;
00479
00480
00481
00482
00483 if (ctr_bit==0)
00484 return TRUE;
00485 else
00486 {
00487 int cnt = 0;
00488
00489 while (bitoffset>=0 && !cnt)
00490 {
00491 cnt |= ((*cur_byte)>> (bitoffset--)) & 0x01;
00492 }
00493
00494 return (cnt);
00495 }
00496 }
00497 }
00498
00499
00506 int uvlc_startcode_follows(Slice *currSlice, int dummy)
00507 {
00508 byte dp_Nr = assignSE2partition[currSlice->dp_mode][SE_MBTYPE];
00509 DataPartition *dP = &(currSlice->partArr[dp_Nr]);
00510 Bitstream *currStream = dP->bitstream;
00511 byte *buf = currStream->streamBuffer;
00512
00513 return (!(more_rbsp_data(buf, currStream->frame_bitoffset,currStream->bitstream_length)));
00514 }
00515
00516
00517
00535 int GetVLCSymbol (byte buffer[],int totbitoffset,int *info, int bytecount)
00536 {
00537 long byteoffset = (totbitoffset >> 3);
00538 int bitoffset = (7 - (totbitoffset & 0x07));
00539 int bitcounter = 1;
00540 int len = 0;
00541 byte *cur_byte = &(buffer[byteoffset]);
00542 int ctr_bit = ((*cur_byte) >> (bitoffset)) & 0x01;
00543
00544 while (ctr_bit == 0)
00545 {
00546 len++;
00547 bitcounter++;
00548 bitoffset--;
00549 bitoffset &= 0x07;
00550 cur_byte += (bitoffset == 7);
00551 byteoffset+= (bitoffset == 7);
00552 ctr_bit = ((*cur_byte) >> (bitoffset)) & 0x01;
00553 }
00554
00555 if (byteoffset + ((len + 7) >> 3) > bytecount)
00556 return -1;
00557 else
00558 {
00559
00560 int inf = 0;
00561
00562 while (len--)
00563 {
00564 bitoffset --;
00565 bitoffset &= 0x07;
00566 cur_byte += (bitoffset == 7);
00567 bitcounter++;
00568 inf <<= 1;
00569 inf |= ((*cur_byte) >> (bitoffset)) & 0x01;
00570 }
00571
00572 *info = inf;
00573 return bitcounter;
00574 }
00575 }
00576
00577
00591
00592 static inline int ShowBitsThres (int inf, int numbits)
00593 {
00594 return ((inf) >> ((sizeof(byte) * 24) - (numbits)));
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608 }
00609
00610
00618 int code_from_bitstream_2d(SyntaxElement *sym,
00619 Bitstream *currStream,
00620 const byte *lentab,
00621 const byte *codtab,
00622 int tabwidth,
00623 int tabheight,
00624 int *code)
00625 {
00626 int i, j;
00627 const byte *len = &lentab[0], *cod = &codtab[0];
00628
00629 int *frame_bitoffset = &currStream->frame_bitoffset;
00630 byte *buf = &currStream->streamBuffer[*frame_bitoffset >> 3];
00631
00632
00633 unsigned int inf = ((*buf) << 16) + (*(buf + 1) << 8) + *(buf + 2);
00634 inf <<= (*frame_bitoffset & 0x07);
00635 inf &= 0xFFFFFF;
00636
00637
00638 for (j = 0; j < tabheight; j++)
00639 {
00640 for (i = 0; i < tabwidth; i++)
00641 {
00642 if ((*len == 0) || (ShowBitsThres(inf, (int) *len) != *cod))
00643 {
00644 len++;
00645 cod++;
00646 }
00647 else
00648 {
00649 sym->len = *len;
00650 *frame_bitoffset += *len;
00651 *code = *cod;
00652 sym->value1 = i;
00653 sym->value2 = j;
00654 return 0;
00655 }
00656 }
00657 }
00658 return -1;
00659 }
00660
00661
00668 int readSyntaxElement_FLC(SyntaxElement *sym, Bitstream *currStream)
00669 {
00670 int BitstreamLengthInBits = (currStream->bitstream_length << 3) + 7;
00671
00672 if ((GetBits(currStream->streamBuffer, currStream->frame_bitoffset, &(sym->inf), BitstreamLengthInBits, sym->len)) < 0)
00673 return -1;
00674
00675 sym->value1 = sym->inf;
00676 currStream->frame_bitoffset += sym->len;
00677
00678 #if TRACE
00679 tracebits2(sym->tracestring, sym->len, sym->inf);
00680 #endif
00681
00682 return 1;
00683 }
00684
00685
00686
00694 int readSyntaxElement_NumCoeffTrailingOnes(SyntaxElement *sym,
00695 Bitstream *currStream,
00696 char *type)
00697 {
00698 int frame_bitoffset = currStream->frame_bitoffset;
00699 int BitstreamLengthInBytes = currStream->bitstream_length;
00700 int BitstreamLengthInBits = (BitstreamLengthInBytes << 3) + 7;
00701 byte *buf = currStream->streamBuffer;
00702
00703 static const byte lentab[3][4][17] =
00704 {
00705 {
00706 { 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
00707 { 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
00708 { 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
00709 { 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16},
00710 },
00711 {
00712 { 2, 6, 6, 7, 8, 8, 9,11,11,12,12,12,13,13,13,14,14},
00713 { 0, 2, 5, 6, 6, 7, 8, 9,11,11,12,12,13,13,14,14,14},
00714 { 0, 0, 3, 6, 6, 7, 8, 9,11,11,12,12,13,13,13,14,14},
00715 { 0, 0, 0, 4, 4, 5, 6, 6, 7, 9,11,11,12,13,13,13,14},
00716 },
00717 {
00718 { 4, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9,10,10,10,10},
00719 { 0, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8, 9, 9, 9,10,10,10},
00720 { 0, 0, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,10},
00721 { 0, 0, 0, 4, 4, 4, 4, 4, 5, 6, 7, 8, 8, 9,10,10,10},
00722 },
00723 };
00724
00725 static const byte codtab[3][4][17] =
00726 {
00727 {
00728 { 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7,4},
00729 { 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10,6},
00730 { 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9,5},
00731 { 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12,8},
00732 },
00733 {
00734 { 3,11, 7, 7, 7, 4, 7,15,11,15,11, 8,15,11, 7, 9,7},
00735 { 0, 2, 7,10, 6, 6, 6, 6,14,10,14,10,14,10,11, 8,6},
00736 { 0, 0, 3, 9, 5, 5, 5, 5,13, 9,13, 9,13, 9, 6,10,5},
00737 { 0, 0, 0, 5, 4, 6, 8, 4, 4, 4,12, 8,12,12, 8, 1,4},
00738 },
00739 {
00740 {15,15,11, 8,15,11, 9, 8,15,11,15,11, 8,13, 9, 5,1},
00741 { 0,14,15,12,10, 8,14,10,14,14,10,14,10, 7,12, 8,4},
00742 { 0, 0,13,14,11, 9,13, 9,13,10,13, 9,13, 9,11, 7,3},
00743 { 0, 0, 0,12,11,10, 9, 8,13,12,12,12, 8,12,10, 6,2},
00744 },
00745 };
00746
00747 int retval = 0, code;
00748 int vlcnum = sym->value1;
00749
00750
00751
00752 if (vlcnum == 3)
00753 {
00754
00755
00756 code = ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, 6);
00757 currStream->frame_bitoffset += 6;
00758 sym->value2 = (code & 3);
00759 sym->value1 = (code >> 2);
00760
00761 if (!sym->value1 && sym->value2 == 3)
00762 {
00763
00764 sym->value2 = 0;
00765 }
00766 else
00767 sym->value1++;
00768
00769 sym->len = 6;
00770 }
00771 else
00772 {
00773
00774 retval = code_from_bitstream_2d(sym, currStream, lentab[vlcnum][0], codtab[vlcnum][0], 17, 4, &code);
00775 if (retval)
00776 {
00777 printf("ERROR: failed to find NumCoeff/TrailingOnes\n");
00778 exit(-1);
00779 }
00780 }
00781
00782 #if TRACE
00783 snprintf(sym->tracestring, TRACESTRING_SIZE, "%s # c & tr.1s vlc=%d #c=%d #t1=%d",
00784 type, vlcnum, sym->value1, sym->value2);
00785 tracebits2(sym->tracestring, sym->len, code);
00786 #endif
00787
00788 return retval;
00789 }
00790
00791
00798 int readSyntaxElement_NumCoeffTrailingOnesChromaDC(ImageParameters *p_Img, SyntaxElement *sym, Bitstream *currStream)
00799 {
00800 static const byte lentab[3][4][17] =
00801 {
00802
00803 {{ 2, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00804 { 0, 1, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00805 { 0, 0, 3, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00806 { 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
00807
00808 {{ 1, 7, 7, 9, 9,10,11,12,13, 0, 0, 0, 0, 0, 0, 0, 0},
00809 { 0, 2, 7, 7, 9,10,11,12,12, 0, 0, 0, 0, 0, 0, 0, 0},
00810 { 0, 0, 3, 7, 7, 9,10,11,12, 0, 0, 0, 0, 0, 0, 0, 0},
00811 { 0, 0, 0, 5, 6, 7, 7,10,11, 0, 0, 0, 0, 0, 0, 0, 0}},
00812
00813 {{ 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
00814 { 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
00815 { 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
00816 { 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16}}
00817 };
00818
00819 static const byte codtab[3][4][17] =
00820 {
00821
00822 {{ 1, 7, 4, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00823 { 0, 1, 6, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00824 { 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00825 { 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
00826
00827 {{ 1,15,14, 7, 6, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0},
00828 { 0, 1,13,12, 5, 6, 6, 6, 5, 0, 0, 0, 0, 0, 0, 0, 0},
00829 { 0, 0, 1,11,10, 4, 5, 5, 4, 0, 0, 0, 0, 0, 0, 0, 0},
00830 { 0, 0, 0, 1, 1, 9, 8, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0}},
00831
00832 {{ 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7, 4},
00833 { 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10, 6},
00834 { 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9, 5},
00835 { 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12, 8}}
00836
00837 };
00838
00839 int code;
00840 int yuv = p_Img->active_sps->chroma_format_idc - 1;
00841 int retval = code_from_bitstream_2d(sym, currStream, &lentab[yuv][0][0], &codtab[yuv][0][0], 17, 4, &code);
00842
00843 if (retval)
00844 {
00845 printf("ERROR: failed to find NumCoeff/TrailingOnes ChromaDC\n");
00846 exit(-1);
00847 }
00848
00849 #if TRACE
00850 snprintf(sym->tracestring, TRACESTRING_SIZE, "ChrDC # c & tr.1s #c=%d #t1=%d",
00851 sym->value1, sym->value2);
00852 tracebits2(sym->tracestring, sym->len, code);
00853
00854 #endif
00855
00856 return retval;
00857 }
00858
00865 int readSyntaxElement_Level_VLC0(SyntaxElement *sym, Bitstream *currStream)
00866 {
00867 int frame_bitoffset = currStream->frame_bitoffset;
00868 int BitstreamLengthInBytes = currStream->bitstream_length;
00869 int BitstreamLengthInBits = (BitstreamLengthInBytes << 3) + 7;
00870 byte *buf = currStream->streamBuffer;
00871 int len = 1, sign = 0, level = 0, code = 1;
00872
00873 while (!ShowBits(buf, frame_bitoffset++, BitstreamLengthInBits, 1))
00874 len++;
00875
00876 if (len < 15)
00877 {
00878 sign = (len - 1) & 1;
00879 level = ((len - 1) >> 1) + 1;
00880 }
00881 else if (len == 15)
00882 {
00883
00884 code <<= 4;
00885 code |= ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, 4);
00886 len += 4;
00887 frame_bitoffset += 4;
00888 sign = (code & 0x01);
00889 level = ((code >> 1) & 0x07) + 8;
00890 }
00891 else if (len >= 16)
00892 {
00893
00894 int addbit = (len - 16);
00895 int offset = (2048 << addbit) - 2032;
00896 len -= 4;
00897 code = ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, len);
00898 sign = (code & 0x01);
00899 frame_bitoffset += len;
00900 level = (code >> 1) + offset;
00901
00902 code |= (1 << (len));
00903 len += addbit + 16;
00904 }
00905
00906 sym->inf = (sign) ? -level : level ;
00907 sym->len = len;
00908
00909 #if TRACE
00910 tracebits2(sym->tracestring, sym->len, code);
00911 #endif
00912
00913 currStream->frame_bitoffset = frame_bitoffset;
00914 return 0;
00915 }
00916
00923 int readSyntaxElement_Level_VLCN(SyntaxElement *sym, int vlc, Bitstream *currStream)
00924 {
00925 int frame_bitoffset = currStream->frame_bitoffset;
00926 int BitstreamLengthInBytes = currStream->bitstream_length;
00927 int BitstreamLengthInBits = (BitstreamLengthInBytes << 3) + 7;
00928 byte *buf = currStream->streamBuffer;
00929
00930 int levabs, sign;
00931 int len = 1;
00932 int code = 1, sb;
00933
00934 int shift = vlc - 1;
00935
00936
00937 while (!ShowBits(buf, frame_bitoffset ++, BitstreamLengthInBits, 1))
00938 len++;
00939
00940 frame_bitoffset -= len;
00941
00942 if (len < 16)
00943 {
00944 levabs = ((len - 1) << shift) + 1;
00945
00946
00947 if (shift)
00948 {
00949 sb = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, shift);
00950 code = (code << (shift) )| sb;
00951 levabs += sb;
00952 len += (shift);
00953 }
00954
00955
00956 sign = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, 1);
00957 code = (code << 1)| sign;
00958 len ++;
00959 }
00960 else
00961 {
00962 int addbit = len - 5;
00963 int offset = (1 << addbit) + (15 << shift) - 2047;
00964
00965 sb = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, addbit);
00966 code = (code << addbit ) | sb;
00967 len += addbit;
00968
00969 levabs = sb + offset;
00970
00971
00972 sign = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, 1);
00973
00974 code = (code << 1)| sign;
00975
00976 len++;
00977 }
00978
00979 sym->inf = (sign)? -levabs : levabs;
00980 sym->len = len;
00981
00982 currStream->frame_bitoffset = frame_bitoffset + len;
00983
00984 #if TRACE
00985 tracebits2(sym->tracestring, sym->len, code);
00986 #endif
00987
00988 return 0;
00989 }
00990
00997 int readSyntaxElement_TotalZeros(SyntaxElement *sym, Bitstream *currStream)
00998 {
00999 static const byte lentab[TOTRUN_NUM][16] =
01000 {
01001
01002 { 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
01003 { 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
01004 { 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
01005 { 5,3,4,4,3,3,3,4,3,4,5,5,5},
01006 { 4,4,4,3,3,3,3,3,4,5,4,5},
01007 { 6,5,3,3,3,3,3,3,4,3,6},
01008 { 6,5,3,3,3,2,3,4,3,6},
01009 { 6,4,5,3,2,2,3,3,6},
01010 { 6,6,4,2,2,3,2,5},
01011 { 5,5,3,2,2,2,4},
01012 { 4,4,3,3,1,3},
01013 { 4,4,2,1,3},
01014 { 3,3,1,2},
01015 { 2,2,1},
01016 { 1,1},
01017 };
01018
01019 static const byte codtab[TOTRUN_NUM][16] =
01020 {
01021 {1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
01022 {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
01023 {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
01024 {3,7,5,4,6,5,4,3,3,2,2,1,0},
01025 {5,4,3,7,6,5,4,3,2,1,1,0},
01026 {1,1,7,6,5,4,3,2,1,1,0},
01027 {1,1,5,4,3,3,2,1,1,0},
01028 {1,1,1,3,3,2,2,1,0},
01029 {1,0,1,3,2,1,1,1,},
01030 {1,0,1,3,2,1,1,},
01031 {0,1,1,2,1,3},
01032 {0,1,1,1,1},
01033 {0,1,1,1},
01034 {0,1,1},
01035 {0,1},
01036 };
01037
01038 int code;
01039 int vlcnum = sym->value1;
01040 int retval = code_from_bitstream_2d(sym, currStream, &lentab[vlcnum][0], &codtab[vlcnum][0], 16, 1, &code);
01041
01042 if (retval)
01043 {
01044 printf("ERROR: failed to find Total Zeros !cdc\n");
01045 exit(-1);
01046 }
01047
01048 #if TRACE
01049 tracebits2(sym->tracestring, sym->len, code);
01050 #endif
01051
01052 return retval;
01053 }
01054
01061 int readSyntaxElement_TotalZerosChromaDC(ImageParameters *p_Img, SyntaxElement *sym, Bitstream *currStream)
01062 {
01063 static const byte lentab[3][TOTRUN_NUM][16] =
01064 {
01065
01066 {{ 1,2,3,3},
01067 { 1,2,2},
01068 { 1,1}},
01069
01070 {{ 1,3,3,4,4,4,5,5},
01071 { 3,2,3,3,3,3,3},
01072 { 3,3,2,2,3,3},
01073 { 3,2,2,2,3},
01074 { 2,2,2,2},
01075 { 2,2,1},
01076 { 1,1}},
01077
01078 {{ 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
01079 { 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
01080 { 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
01081 { 5,3,4,4,3,3,3,4,3,4,5,5,5},
01082 { 4,4,4,3,3,3,3,3,4,5,4,5},
01083 { 6,5,3,3,3,3,3,3,4,3,6},
01084 { 6,5,3,3,3,2,3,4,3,6},
01085 { 6,4,5,3,2,2,3,3,6},
01086 { 6,6,4,2,2,3,2,5},
01087 { 5,5,3,2,2,2,4},
01088 { 4,4,3,3,1,3},
01089 { 4,4,2,1,3},
01090 { 3,3,1,2},
01091 { 2,2,1},
01092 { 1,1}}
01093 };
01094
01095 static const byte codtab[3][TOTRUN_NUM][16] =
01096 {
01097
01098 {{ 1,1,1,0},
01099 { 1,1,0},
01100 { 1,0}},
01101
01102 {{ 1,2,3,2,3,1,1,0},
01103 { 0,1,1,4,5,6,7},
01104 { 0,1,1,2,6,7},
01105 { 6,0,1,2,7},
01106 { 0,1,2,3},
01107 { 0,1,1},
01108 { 0,1}},
01109
01110 {{1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
01111 {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
01112 {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
01113 {3,7,5,4,6,5,4,3,3,2,2,1,0},
01114 {5,4,3,7,6,5,4,3,2,1,1,0},
01115 {1,1,7,6,5,4,3,2,1,1,0},
01116 {1,1,5,4,3,3,2,1,1,0},
01117 {1,1,1,3,3,2,2,1,0},
01118 {1,0,1,3,2,1,1,1,},
01119 {1,0,1,3,2,1,1,},
01120 {0,1,1,2,1,3},
01121 {0,1,1,1,1},
01122 {0,1,1,1},
01123 {0,1,1},
01124 {0,1}}
01125 };
01126
01127 int code;
01128 int yuv = p_Img->active_sps->chroma_format_idc - 1;
01129 int vlcnum = sym->value1;
01130 int retval = code_from_bitstream_2d(sym, currStream, &lentab[yuv][vlcnum][0], &codtab[yuv][vlcnum][0], 16, 1, &code);
01131
01132 if (retval)
01133 {
01134 printf("ERROR: failed to find Total Zeros\n");
01135 exit(-1);
01136 }
01137
01138 #if TRACE
01139 tracebits2(sym->tracestring, sym->len, code);
01140 #endif
01141
01142 return retval;
01143 }
01144
01145
01152 int readSyntaxElement_Run(SyntaxElement *sym, Bitstream *currStream)
01153 {
01154 static const byte lentab[TOTRUN_NUM][16] =
01155 {
01156 {1,1},
01157 {1,2,2},
01158 {2,2,2,2},
01159 {2,2,2,3,3},
01160 {2,2,3,3,3,3},
01161 {2,3,3,3,3,3,3},
01162 {3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
01163 };
01164
01165 static const byte codtab[TOTRUN_NUM][16] =
01166 {
01167 {1,0},
01168 {1,1,0},
01169 {3,2,1,0},
01170 {3,2,1,1,0},
01171 {3,2,3,2,1,0},
01172 {3,0,1,3,2,5,4},
01173 {7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
01174 };
01175 int code;
01176 int vlcnum = sym->value1;
01177 int retval = code_from_bitstream_2d(sym, currStream, &lentab[vlcnum][0], &codtab[vlcnum][0], 16, 1, &code);
01178
01179 if (retval)
01180 {
01181 printf("ERROR: failed to find Run\n");
01182 exit(-1);
01183 }
01184
01185 #if TRACE
01186 tracebits2(sym->tracestring, sym->len, code);
01187 #endif
01188
01189 return retval;
01190 }
01191
01192
01211 int GetBits (byte buffer[],int totbitoffset,int *info, int bitcount,
01212 int numbits)
01213 {
01214 if ((totbitoffset + numbits ) > bitcount)
01215 {
01216 return -1;
01217 }
01218 else
01219 {
01220 int bitoffset = 7 - (totbitoffset & 0x07);
01221 int byteoffset = (totbitoffset >> 3);
01222 int bitcounter = numbits;
01223 byte *curbyte = &(buffer[byteoffset]);
01224 int inf = 0;
01225
01226 while (numbits--)
01227 {
01228 inf <<=1;
01229 inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
01230 if (bitoffset == -1 )
01231 {
01232 curbyte++;
01233 bitoffset = 7;
01234 }
01235
01236
01237
01238 }
01239 *info = inf;
01240
01241 return bitcounter;
01242 }
01243 }
01244
01262 int ShowBits (byte buffer[],int totbitoffset,int bitcount, int numbits)
01263 {
01264 if ((totbitoffset + numbits ) > bitcount)
01265 {
01266 return -1;
01267 }
01268 else
01269 {
01270 int bitoffset = 7 - (totbitoffset & 0x07);
01271 int byteoffset = (totbitoffset >> 3);
01272 byte *curbyte = &(buffer[byteoffset]);
01273 int inf = 0;
01274
01275 while (numbits--)
01276 {
01277 inf <<=1;
01278 inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
01279
01280 if (bitoffset == -1 )
01281 {
01282 curbyte++;
01283 bitoffset = 7;
01284 }
01285 }
01286 return inf;
01287 }
01288 }
01289