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 int UsedBits;
00030
00031
00032
00034 static const byte NCBP[2][48][2]=
00035 {
00036 {
00037 {15, 0},{ 0, 1},{ 7, 2},{11, 4},{13, 8},{14, 3},{ 3, 5},{ 5,10},{10,12},{12,15},{ 1, 7},{ 2,11},
00038 { 4,13},{ 8,14},{ 6, 6},{ 9, 9},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},
00039 { 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},
00040 { 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0}
00041 },
00042 {
00043 {47, 0},{31,16},{15, 1},{ 0, 2},{23, 4},{27, 8},{29,32},{30, 3},{ 7, 5},{11,10},{13,12},{14,15},
00044 {39,47},{43, 7},{45,11},{46,13},{16,14},{ 3, 6},{ 5, 9},{10,31},{12,35},{19,37},{21,42},{26,44},
00045 {28,33},{35,34},{37,36},{42,40},{44,39},{ 1,43},{ 2,45},{ 4,46},{ 8,17},{17,18},{18,20},{20,24},
00046 {24,19},{ 6,21},{ 9,26},{22,28},{25,23},{32,27},{33,29},{34,30},{36,22},{40,25},{38,38},{41,41}
00047 }
00048 };
00049
00051 const byte NTAB1[4][8][2] =
00052 {
00053 {{1,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
00054 {{1,1},{1,2},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
00055 {{2,0},{1,3},{1,4},{1,5},{0,0},{0,0},{0,0},{0,0}},
00056 {{3,0},{2,1},{2,2},{1,6},{1,7},{1,8},{1,9},{4,0}},
00057 };
00058 const byte LEVRUN1[16]=
00059 {
00060 4,2,2,1,1,1,1,1,1,1,0,0,0,0,0,0,
00061 };
00062
00063
00064 const byte NTAB2[4][8][2] =
00065 {
00066 {{1,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
00067 {{1,1},{2,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}},
00068 {{1,2},{3,0},{4,0},{5,0},{0,0},{0,0},{0,0},{0,0}},
00069 {{1,3},{1,4},{2,1},{3,1},{6,0},{7,0},{8,0},{9,0}},
00070 };
00071
00073 const byte LEVRUN3[4] =
00074 {
00075 2,1,0,0
00076 };
00077 const byte NTAB3[2][2][2] =
00078 {
00079 {{1,0},{0,0}},
00080 {{2,0},{1,1}},
00081 };
00082
00100 int ue_v (char *tracestring, Bitstream *bitstream)
00101 {
00102 SyntaxElement symbol;
00103
00104
00105 symbol.type = SE_HEADER;
00106 symbol.mapping = linfo_ue;
00107 SYMTRACESTRING(tracestring);
00108 readSyntaxElement_VLC (&symbol, bitstream);
00109 UsedBits+=symbol.len;
00110 return symbol.value1;
00111 }
00112
00113
00131 int se_v (char *tracestring, Bitstream *bitstream)
00132 {
00133 SyntaxElement symbol;
00134
00135
00136 symbol.type = SE_HEADER;
00137 symbol.mapping = linfo_se;
00138 SYMTRACESTRING(tracestring);
00139 readSyntaxElement_VLC (&symbol, bitstream);
00140 UsedBits+=symbol.len;
00141 return symbol.value1;
00142 }
00143
00144
00165 int u_v (int LenInBits, char*tracestring, Bitstream *bitstream)
00166 {
00167 SyntaxElement symbol;
00168 symbol.inf = 0;
00169
00170
00171 symbol.type = SE_HEADER;
00172 symbol.mapping = linfo_ue;
00173 symbol.len = LenInBits;
00174 SYMTRACESTRING(tracestring);
00175 readSyntaxElement_FLC (&symbol, bitstream);
00176 UsedBits+=symbol.len;
00177 return symbol.inf;
00178 }
00179
00200 int i_v (int LenInBits, char*tracestring, Bitstream *bitstream)
00201 {
00202 SyntaxElement symbol;
00203
00204 symbol.inf = 0;
00205
00206
00207 symbol.type = SE_HEADER;
00208 symbol.mapping = linfo_ue;
00209 symbol.len = LenInBits;
00210 SYMTRACESTRING(tracestring);
00211 readSyntaxElement_FLC (&symbol, bitstream);
00212 UsedBits+=symbol.len;
00213
00214
00215 symbol.inf = -( symbol.inf & (1 << (LenInBits - 1)) ) | symbol.inf;
00216
00217 return symbol.inf;
00218 }
00219
00220
00238 Boolean u_1 (char *tracestring, Bitstream *bitstream)
00239 {
00240 return (Boolean) u_v (1, tracestring, bitstream);
00241 }
00242
00243
00244
00255 void linfo_ue(int len, int info, int *value1, int *dummy)
00256 {
00257
00258 *value1 = (1 << (len >> 1)) + info - 1;
00259 }
00260
00271 void linfo_se(int len, int info, int *value1, int *dummy)
00272 {
00273 int n;
00274
00275 n = (1 << (len >> 1)) + info - 1;
00276 *value1 = (n + 1) >> 1;
00277 if((n & 0x01) == 0)
00278 *value1 = -*value1;
00279 }
00280
00281
00290 void linfo_cbp_intra(int len,int info,int *cbp, int *dummy)
00291 {
00292 int cbp_idx;
00293
00294 linfo_ue(len, info, &cbp_idx, dummy);
00295 if((active_sps->chroma_format_idc==0)||(active_sps->chroma_format_idc==3))
00296 *cbp=NCBP[0][cbp_idx][0];
00297 else
00298 *cbp=NCBP[1][cbp_idx][0];
00299
00300 }
00301
00310 void linfo_cbp_inter(int len,int info,int *cbp, int *dummy)
00311 {
00312 int cbp_idx;
00313
00314 linfo_ue(len, info, &cbp_idx, dummy);
00315 if((active_sps->chroma_format_idc==0)||(active_sps->chroma_format_idc==3))
00316 *cbp=NCBP[0][cbp_idx][1];
00317 else
00318 *cbp=NCBP[1][cbp_idx][1];
00319 }
00320
00329 void linfo_levrun_inter(int len, int info, int *level, int *irun)
00330 {
00331
00332
00333 if (len <= 9)
00334 {
00335 int l2 = imax(0,(len >> 1)-1);
00336 int inf = info >> 1;
00337
00338 *level = NTAB1[l2][inf][0];
00339 *irun = NTAB1[l2][inf][1];
00340 if ((info & 0x01) == 1)
00341 *level = -*level;
00342 }
00343 else
00344 {
00345 *irun = (info & 0x1e) >> 1;
00346 *level = LEVRUN1[*irun] + (info >> 5) + ( 1 << ((len >> 1) - 5));
00347 if ((info & 0x01) == 1)
00348 *level = -*level;
00349 }
00350
00351 if (len == 1)
00352 *level = 0;
00353 }
00354
00355
00364 void linfo_levrun_c2x2(int len, int info, int *level, int *irun)
00365 {
00366 if (len<=5)
00367 {
00368 int l2 = imax(0, (len >> 1) - 1);
00369 int inf = info >> 1;
00370 *level = NTAB3[l2][inf][0];
00371 *irun = NTAB3[l2][inf][1];
00372 if ((info & 0x01) == 1)
00373 *level = -*level;
00374 }
00375 else
00376 {
00377 *irun = (info & 0x06) >> 1;
00378 *level = LEVRUN3[*irun] + (info >> 3) + (1 << ((len >> 1) - 3));
00379 if ((info & 0x01) == 1)
00380 *level = -*level;
00381 }
00382
00383 if (len == 1)
00384 *level = 0;
00385 }
00386
00394 int readSyntaxElement_VLC(SyntaxElement *sym, Bitstream *currStream)
00395 {
00396
00397 sym->len = GetVLCSymbol (currStream->streamBuffer, currStream->frame_bitoffset, &(sym->inf), currStream->bitstream_length);
00398 if (sym->len == -1)
00399 return -1;
00400
00401 currStream->frame_bitoffset += sym->len;
00402 sym->mapping(sym->len, sym->inf, &(sym->value1), &(sym->value2));
00403
00404 #if TRACE
00405 tracebits(sym->tracestring, sym->len, sym->inf, sym->value1);
00406 #endif
00407
00408 return 1;
00409 }
00410
00411
00419 int readSyntaxElement_UVLC(SyntaxElement *sym, ImageParameters *img, struct datapartition *dP)
00420 {
00421 return (readSyntaxElement_VLC(sym, dP->bitstream));
00422 }
00423
00431 int readSyntaxElement_Intra4x4PredictionMode(SyntaxElement *sym, ImageParameters *img, Bitstream *currStream)
00432 {
00433 sym->len = GetVLCSymbol_IntraMode (currStream->streamBuffer, currStream->frame_bitoffset, &(sym->inf), currStream->bitstream_length);
00434
00435 if (sym->len == -1)
00436 return -1;
00437
00438 currStream->frame_bitoffset += sym->len;
00439 sym->value1 = (sym->len == 1) ? -1 : sym->inf;
00440
00441 #if TRACE
00442 tracebits2(sym->tracestring, sym->len, sym->value1);
00443 #endif
00444
00445 return 1;
00446 }
00447
00448 int GetVLCSymbol_IntraMode (byte buffer[],int totbitoffset,int *info, int bytecount)
00449 {
00450 int byteoffset = (totbitoffset >> 3);
00451 int bitoffset = (7 - (totbitoffset & 0x07));
00452 byte *cur_byte = &(buffer[byteoffset]);
00453 int ctr_bit = (*cur_byte & (0x01 << bitoffset));
00454
00455
00456 if (ctr_bit)
00457 {
00458 *info = 0;
00459 return 1;
00460 }
00461
00462 if (byteoffset >= bytecount)
00463 {
00464 return -1;
00465 }
00466 else
00467 {
00468 int inf = (*(cur_byte) << 8) + *(cur_byte + 1);
00469 inf <<= (sizeof(byte) * 8) - bitoffset;
00470 inf = inf & 0xFFFF;
00471 inf >>= (sizeof(byte) * 8) * 2 - 3;
00472
00473 *info = inf;
00474 return 4;
00475 }
00476 }
00477
00478
00494 int more_rbsp_data (byte buffer[],int totbitoffset,int bytecount)
00495 {
00496 int bitoffset = (7 - (totbitoffset & 0x07));
00497 long byteoffset = (totbitoffset >> 3);
00498 byte *cur_byte = &(buffer[byteoffset]);
00499 int ctr_bit = 0;
00500 int cnt = 0;
00501
00502
00503
00504
00505 if (byteoffset < (bytecount - 1)) return TRUE;
00506
00507
00508 ctr_bit = ((*cur_byte)>> (bitoffset--)) & 0x01;
00509
00510
00511 if (ctr_bit==0) return TRUE;
00512
00513 while (bitoffset>=0 && !cnt)
00514 {
00515 cnt |= ((*cur_byte)>> (bitoffset--)) & 0x01;
00516 }
00517
00518 return (cnt);
00519 }
00520
00521
00528 int uvlc_startcode_follows(Slice *currSlice, int dummy)
00529 {
00530 byte dp_Nr = assignSE2partition[currSlice->dp_mode][SE_MBTYPE];
00531 DataPartition *dP = &(currSlice->partArr[dp_Nr]);
00532 Bitstream *currStream = dP->bitstream;
00533 byte *buf = currStream->streamBuffer;
00534
00535 return (!(more_rbsp_data(buf, currStream->frame_bitoffset,currStream->bitstream_length)));
00536 }
00537
00538
00539
00557 int GetVLCSymbol (byte buffer[],int totbitoffset,int *info, int bytecount)
00558 {
00559 long byteoffset = (totbitoffset >> 3);
00560 int bitoffset = (7 - (totbitoffset & 0x07));
00561 int bitcounter = 1;
00562 int len = 0;
00563 byte *cur_byte = &(buffer[byteoffset]);
00564 int ctr_bit = ((*cur_byte) >> (bitoffset)) & 0x01;
00565
00566 while (ctr_bit == 0)
00567 {
00568 len++;
00569 bitcounter++;
00570 bitoffset--;
00571 bitoffset &= 0x07;
00572 cur_byte += (bitoffset == 7);
00573 byteoffset+= (bitoffset == 7);
00574 ctr_bit = ((*cur_byte) >> (bitoffset)) & 0x01;
00575 }
00576
00577 if (byteoffset + ((len + 7) >> 3) > bytecount)
00578 return -1;
00579
00580 else
00581 {
00582
00583 int inf = 0;
00584
00585 while (len--)
00586 {
00587 bitoffset --;
00588 bitoffset &= 0x07;
00589 cur_byte += (bitoffset == 7);
00590 bitcounter++;
00591 inf <<= 1;
00592 inf |= ((*cur_byte) >> (bitoffset)) & 0x01;
00593 }
00594
00595 *info = inf;
00596 return bitcounter;
00597 }
00598 }
00599
00600
00614
00615 static inline int ShowBitsThres (int inf, int numbits)
00616 {
00617 return ((inf) >> ((sizeof(byte) * 24) - (numbits)));
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631 }
00632
00633
00641 int code_from_bitstream_2d(SyntaxElement *sym,
00642 Bitstream *currStream,
00643 const byte *lentab,
00644 const byte *codtab,
00645 int tabwidth,
00646 int tabheight,
00647 int *code)
00648 {
00649 int i, j;
00650 const byte *len = &lentab[0], *cod = &codtab[0];
00651
00652 int *frame_bitoffset = &currStream->frame_bitoffset;
00653 byte *buf = &currStream->streamBuffer[*frame_bitoffset >> 3];
00654
00655
00656 unsigned int inf = ((*buf) << 16) + (*(buf + 1) << 8) + *(buf + 2);
00657 inf <<= (*frame_bitoffset & 0x07);
00658 inf &= 0xFFFFFF;
00659
00660
00661 for (j = 0; j < tabheight; j++)
00662 {
00663 for (i = 0; i < tabwidth; i++)
00664 {
00665 if ((*len == 0) || (ShowBitsThres(inf, (int) *len) != *cod))
00666 {
00667 len++;
00668 cod++;
00669 }
00670 else
00671 {
00672 sym->len = *len;
00673 *frame_bitoffset += *len;
00674 *code = *cod;
00675 sym->value1 = i;
00676 sym->value2 = j;
00677 return 0;
00678 }
00679 }
00680 }
00681 return -1;
00682 }
00683
00684
00691 int readSyntaxElement_FLC(SyntaxElement *sym, Bitstream *currStream)
00692 {
00693 int BitstreamLengthInBits = (currStream->bitstream_length << 3) + 7;
00694
00695 if ((GetBits(currStream->streamBuffer, currStream->frame_bitoffset, &(sym->inf), BitstreamLengthInBits, sym->len)) < 0)
00696 return -1;
00697
00698 sym->value1 = sym->inf;
00699 currStream->frame_bitoffset += sym->len;
00700
00701 #if TRACE
00702 tracebits2(sym->tracestring, sym->len, sym->inf);
00703 #endif
00704
00705 return 1;
00706 }
00707
00708
00709
00717 int readSyntaxElement_NumCoeffTrailingOnes(SyntaxElement *sym,
00718 Bitstream *currStream,
00719 char *type)
00720 {
00721 int frame_bitoffset = currStream->frame_bitoffset;
00722 int BitstreamLengthInBytes = currStream->bitstream_length;
00723 int BitstreamLengthInBits = (BitstreamLengthInBytes << 3) + 7;
00724 byte *buf = currStream->streamBuffer;
00725
00726 static const byte lentab[3][4][17] =
00727 {
00728 {
00729 { 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
00730 { 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
00731 { 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
00732 { 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16},
00733 },
00734 {
00735 { 2, 6, 6, 7, 8, 8, 9,11,11,12,12,12,13,13,13,14,14},
00736 { 0, 2, 5, 6, 6, 7, 8, 9,11,11,12,12,13,13,14,14,14},
00737 { 0, 0, 3, 6, 6, 7, 8, 9,11,11,12,12,13,13,13,14,14},
00738 { 0, 0, 0, 4, 4, 5, 6, 6, 7, 9,11,11,12,13,13,13,14},
00739 },
00740 {
00741 { 4, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9,10,10,10,10},
00742 { 0, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8, 9, 9, 9,10,10,10},
00743 { 0, 0, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,10},
00744 { 0, 0, 0, 4, 4, 4, 4, 4, 5, 6, 7, 8, 8, 9,10,10,10},
00745 },
00746 };
00747
00748 static const byte codtab[3][4][17] =
00749 {
00750 {
00751 { 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7,4},
00752 { 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10,6},
00753 { 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9,5},
00754 { 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12,8},
00755 },
00756 {
00757 { 3,11, 7, 7, 7, 4, 7,15,11,15,11, 8,15,11, 7, 9,7},
00758 { 0, 2, 7,10, 6, 6, 6, 6,14,10,14,10,14,10,11, 8,6},
00759 { 0, 0, 3, 9, 5, 5, 5, 5,13, 9,13, 9,13, 9, 6,10,5},
00760 { 0, 0, 0, 5, 4, 6, 8, 4, 4, 4,12, 8,12,12, 8, 1,4},
00761 },
00762 {
00763 {15,15,11, 8,15,11, 9, 8,15,11,15,11, 8,13, 9, 5,1},
00764 { 0,14,15,12,10, 8,14,10,14,14,10,14,10, 7,12, 8,4},
00765 { 0, 0,13,14,11, 9,13, 9,13,10,13, 9,13, 9,11, 7,3},
00766 { 0, 0, 0,12,11,10, 9, 8,13,12,12,12, 8,12,10, 6,2},
00767 },
00768 };
00769
00770 int retval = 0, code;
00771 int vlcnum = sym->value1;
00772
00773
00774
00775 if (vlcnum == 3)
00776 {
00777
00778
00779 code = ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, 6);
00780 currStream->frame_bitoffset += 6;
00781 sym->value2 = (code & 3);
00782 sym->value1 = (code >> 2);
00783
00784 if (!sym->value1 && sym->value2 == 3)
00785 {
00786
00787 sym->value2 = 0;
00788 }
00789 else
00790 sym->value1++;
00791
00792 sym->len = 6;
00793 }
00794 else
00795 {
00796
00797 retval = code_from_bitstream_2d(sym, currStream, lentab[vlcnum][0], codtab[vlcnum][0], 17, 4, &code);
00798 if (retval)
00799 {
00800 printf("ERROR: failed to find NumCoeff/TrailingOnes\n");
00801 exit(-1);
00802 }
00803 }
00804
00805 #if TRACE
00806 snprintf(sym->tracestring, TRACESTRING_SIZE, "%s # c & tr.1s vlc=%d #c=%d #t1=%d",
00807 type, vlcnum, sym->value1, sym->value2);
00808 tracebits2(sym->tracestring, sym->len, code);
00809 #endif
00810
00811 return retval;
00812 }
00813
00814
00821 int readSyntaxElement_NumCoeffTrailingOnesChromaDC(SyntaxElement *sym, Bitstream *currStream)
00822 {
00823 static const byte lentab[3][4][17] =
00824 {
00825
00826 {{ 2, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00827 { 0, 1, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00828 { 0, 0, 3, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00829 { 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
00830
00831 {{ 1, 7, 7, 9, 9,10,11,12,13, 0, 0, 0, 0, 0, 0, 0, 0},
00832 { 0, 2, 7, 7, 9,10,11,12,12, 0, 0, 0, 0, 0, 0, 0, 0},
00833 { 0, 0, 3, 7, 7, 9,10,11,12, 0, 0, 0, 0, 0, 0, 0, 0},
00834 { 0, 0, 0, 5, 6, 7, 7,10,11, 0, 0, 0, 0, 0, 0, 0, 0}},
00835
00836 {{ 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
00837 { 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
00838 { 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
00839 { 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16}}
00840 };
00841
00842 static const byte codtab[3][4][17] =
00843 {
00844
00845 {{ 1, 7, 4, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00846 { 0, 1, 6, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00847 { 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00848 { 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
00849
00850 {{ 1,15,14, 7, 6, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0},
00851 { 0, 1,13,12, 5, 6, 6, 6, 5, 0, 0, 0, 0, 0, 0, 0, 0},
00852 { 0, 0, 1,11,10, 4, 5, 5, 4, 0, 0, 0, 0, 0, 0, 0, 0},
00853 { 0, 0, 0, 1, 1, 9, 8, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0}},
00854
00855 {{ 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7, 4},
00856 { 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10, 6},
00857 { 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9, 5},
00858 { 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12, 8}}
00859
00860 };
00861
00862 int code;
00863 int yuv = active_sps->chroma_format_idc - 1;
00864 int retval = code_from_bitstream_2d(sym, currStream, &lentab[yuv][0][0], &codtab[yuv][0][0], 17, 4, &code);
00865
00866 if (retval)
00867 {
00868 printf("ERROR: failed to find NumCoeff/TrailingOnes ChromaDC\n");
00869 exit(-1);
00870 }
00871
00872 #if TRACE
00873 snprintf(sym->tracestring, TRACESTRING_SIZE, "ChrDC # c & tr.1s #c=%d #t1=%d",
00874 sym->value1, sym->value2);
00875 tracebits2(sym->tracestring, sym->len, code);
00876
00877 #endif
00878
00879 return retval;
00880 }
00881
00888 int readSyntaxElement_Level_VLC0(SyntaxElement *sym, Bitstream *currStream)
00889 {
00890 int frame_bitoffset = currStream->frame_bitoffset;
00891 int BitstreamLengthInBytes = currStream->bitstream_length;
00892 int BitstreamLengthInBits = (BitstreamLengthInBytes << 3) + 7;
00893 byte *buf = currStream->streamBuffer;
00894 int len = 1, sign = 0, level = 0, code = 1;
00895
00896 while (!ShowBits(buf, frame_bitoffset++, BitstreamLengthInBits, 1))
00897 len++;
00898
00899 if (len < 15)
00900 {
00901 sign = (len - 1) & 1;
00902 level = ((len - 1) >> 1) + 1;
00903 }
00904 else if (len == 15)
00905 {
00906
00907 code <<= 4;
00908 code |= ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, 4);
00909 len += 4;
00910 frame_bitoffset += 4;
00911 sign = (code & 0x01);
00912 level = ((code >> 1) & 0x07) + 8;
00913 }
00914 else if (len >= 16)
00915 {
00916
00917 int addbit = (len - 16);
00918 int offset = (2048 << addbit) - 2032;
00919 len -= 4;
00920 code = ShowBits(buf, frame_bitoffset, BitstreamLengthInBits, len);
00921 sign = (code & 0x01);
00922 frame_bitoffset += len;
00923 level = (code >> 1) + offset;
00924
00925 code |= (1 << (len));
00926 len += addbit + 16;
00927 }
00928
00929 sym->inf = (sign) ? -level : level ;
00930 sym->len = len;
00931
00932 #if TRACE
00933 tracebits2(sym->tracestring, sym->len, code);
00934 #endif
00935
00936 currStream->frame_bitoffset = frame_bitoffset;
00937 return 0;
00938 }
00939
00946 int readSyntaxElement_Level_VLCN(SyntaxElement *sym, int vlc, Bitstream *currStream)
00947 {
00948 int frame_bitoffset = currStream->frame_bitoffset;
00949 int BitstreamLengthInBytes = currStream->bitstream_length;
00950 int BitstreamLengthInBits = (BitstreamLengthInBytes << 3) + 7;
00951 byte *buf = currStream->streamBuffer;
00952
00953 int levabs, sign;
00954 int len = 1;
00955 int code = 1, sb;
00956
00957 int shift = vlc - 1;
00958
00959
00960 while (!ShowBits(buf, frame_bitoffset ++, BitstreamLengthInBits, 1))
00961 len++;
00962
00963 frame_bitoffset -= len;
00964
00965 if (len < 16)
00966 {
00967 levabs = ((len - 1) << shift) + 1;
00968
00969
00970 if (shift)
00971 {
00972 sb = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, shift);
00973 code = (code << (shift) )| sb;
00974 levabs += sb;
00975 len += (shift);
00976 }
00977
00978
00979 sign = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, 1);
00980 code = (code << 1)| sign;
00981 len ++;
00982 }
00983 else
00984 {
00985 int addbit = len - 5;
00986 int offset = (1 << addbit) + (15 << shift) - 2047;
00987
00988 sb = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, addbit);
00989 code = (code << addbit ) | sb;
00990 len += addbit;
00991
00992 levabs = sb + offset;
00993
00994
00995 sign = ShowBits(buf, frame_bitoffset + len, BitstreamLengthInBits, 1);
00996
00997 code = (code << 1)| sign;
00998
00999 len++;
01000 }
01001
01002 sym->inf = (sign)? -levabs : levabs;
01003 sym->len = len;
01004
01005 currStream->frame_bitoffset = frame_bitoffset + len;
01006
01007 #if TRACE
01008 tracebits2(sym->tracestring, sym->len, code);
01009 #endif
01010
01011 return 0;
01012 }
01013
01020 int readSyntaxElement_TotalZeros(SyntaxElement *sym, Bitstream *currStream)
01021 {
01022 static const byte lentab[TOTRUN_NUM][16] =
01023 {
01024
01025 { 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
01026 { 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
01027 { 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
01028 { 5,3,4,4,3,3,3,4,3,4,5,5,5},
01029 { 4,4,4,3,3,3,3,3,4,5,4,5},
01030 { 6,5,3,3,3,3,3,3,4,3,6},
01031 { 6,5,3,3,3,2,3,4,3,6},
01032 { 6,4,5,3,2,2,3,3,6},
01033 { 6,6,4,2,2,3,2,5},
01034 { 5,5,3,2,2,2,4},
01035 { 4,4,3,3,1,3},
01036 { 4,4,2,1,3},
01037 { 3,3,1,2},
01038 { 2,2,1},
01039 { 1,1},
01040 };
01041
01042 static const byte codtab[TOTRUN_NUM][16] =
01043 {
01044 {1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
01045 {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
01046 {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
01047 {3,7,5,4,6,5,4,3,3,2,2,1,0},
01048 {5,4,3,7,6,5,4,3,2,1,1,0},
01049 {1,1,7,6,5,4,3,2,1,1,0},
01050 {1,1,5,4,3,3,2,1,1,0},
01051 {1,1,1,3,3,2,2,1,0},
01052 {1,0,1,3,2,1,1,1,},
01053 {1,0,1,3,2,1,1,},
01054 {0,1,1,2,1,3},
01055 {0,1,1,1,1},
01056 {0,1,1,1},
01057 {0,1,1},
01058 {0,1},
01059 };
01060
01061 int code;
01062 int vlcnum = sym->value1;
01063 int retval = code_from_bitstream_2d(sym, currStream, &lentab[vlcnum][0], &codtab[vlcnum][0], 16, 1, &code);
01064
01065 if (retval)
01066 {
01067 printf("ERROR: failed to find Total Zeros !cdc\n");
01068 exit(-1);
01069 }
01070
01071 #if TRACE
01072 tracebits2(sym->tracestring, sym->len, code);
01073 #endif
01074
01075 return retval;
01076 }
01077
01084 int readSyntaxElement_TotalZerosChromaDC(SyntaxElement *sym, Bitstream *currStream)
01085 {
01086 static const byte lentab[3][TOTRUN_NUM][16] =
01087 {
01088
01089 {{ 1,2,3,3},
01090 { 1,2,2},
01091 { 1,1}},
01092
01093 {{ 1,3,3,4,4,4,5,5},
01094 { 3,2,3,3,3,3,3},
01095 { 3,3,2,2,3,3},
01096 { 3,2,2,2,3},
01097 { 2,2,2,2},
01098 { 2,2,1},
01099 { 1,1}},
01100
01101 {{ 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
01102 { 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
01103 { 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
01104 { 5,3,4,4,3,3,3,4,3,4,5,5,5},
01105 { 4,4,4,3,3,3,3,3,4,5,4,5},
01106 { 6,5,3,3,3,3,3,3,4,3,6},
01107 { 6,5,3,3,3,2,3,4,3,6},
01108 { 6,4,5,3,2,2,3,3,6},
01109 { 6,6,4,2,2,3,2,5},
01110 { 5,5,3,2,2,2,4},
01111 { 4,4,3,3,1,3},
01112 { 4,4,2,1,3},
01113 { 3,3,1,2},
01114 { 2,2,1},
01115 { 1,1}}
01116 };
01117
01118 static const byte codtab[3][TOTRUN_NUM][16] =
01119 {
01120
01121 {{ 1,1,1,0},
01122 { 1,1,0},
01123 { 1,0}},
01124
01125 {{ 1,2,3,2,3,1,1,0},
01126 { 0,1,1,4,5,6,7},
01127 { 0,1,1,2,6,7},
01128 { 6,0,1,2,7},
01129 { 0,1,2,3},
01130 { 0,1,1},
01131 { 0,1}},
01132
01133 {{1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
01134 {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
01135 {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
01136 {3,7,5,4,6,5,4,3,3,2,2,1,0},
01137 {5,4,3,7,6,5,4,3,2,1,1,0},
01138 {1,1,7,6,5,4,3,2,1,1,0},
01139 {1,1,5,4,3,3,2,1,1,0},
01140 {1,1,1,3,3,2,2,1,0},
01141 {1,0,1,3,2,1,1,1,},
01142 {1,0,1,3,2,1,1,},
01143 {0,1,1,2,1,3},
01144 {0,1,1,1,1},
01145 {0,1,1,1},
01146 {0,1,1},
01147 {0,1}}
01148 };
01149
01150 int code;
01151 int yuv = active_sps->chroma_format_idc - 1;
01152 int vlcnum = sym->value1;
01153 int retval = code_from_bitstream_2d(sym, currStream, &lentab[yuv][vlcnum][0], &codtab[yuv][vlcnum][0], 16, 1, &code);
01154
01155 if (retval)
01156 {
01157 printf("ERROR: failed to find Total Zeros\n");
01158 exit(-1);
01159 }
01160
01161 #if TRACE
01162 tracebits2(sym->tracestring, sym->len, code);
01163 #endif
01164
01165 return retval;
01166 }
01167
01168
01175 int readSyntaxElement_Run(SyntaxElement *sym, Bitstream *currStream)
01176 {
01177 static const byte lentab[TOTRUN_NUM][16] =
01178 {
01179 {1,1},
01180 {1,2,2},
01181 {2,2,2,2},
01182 {2,2,2,3,3},
01183 {2,2,3,3,3,3},
01184 {2,3,3,3,3,3,3},
01185 {3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
01186 };
01187
01188 static const byte codtab[TOTRUN_NUM][16] =
01189 {
01190 {1,0},
01191 {1,1,0},
01192 {3,2,1,0},
01193 {3,2,1,1,0},
01194 {3,2,3,2,1,0},
01195 {3,0,1,3,2,5,4},
01196 {7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
01197 };
01198 int code;
01199 int vlcnum = sym->value1;
01200 int retval = code_from_bitstream_2d(sym, currStream, &lentab[vlcnum][0], &codtab[vlcnum][0], 16, 1, &code);
01201
01202 if (retval)
01203 {
01204 printf("ERROR: failed to find Run\n");
01205 exit(-1);
01206 }
01207
01208 #if TRACE
01209 tracebits2(sym->tracestring, sym->len, code);
01210 #endif
01211
01212 return retval;
01213 }
01214
01215
01234 int GetBits (byte buffer[],int totbitoffset,int *info, int bitcount,
01235 int numbits)
01236 {
01237 if ((totbitoffset + numbits ) > bitcount)
01238 {
01239 return -1;
01240 }
01241 else
01242 {
01243 int bitoffset = 7 - (totbitoffset & 0x07);
01244 int byteoffset = (totbitoffset >> 3);
01245 int bitcounter = numbits;
01246 byte *curbyte = &(buffer[byteoffset]);
01247 int inf = 0;
01248
01249 while (numbits--)
01250 {
01251 inf <<=1;
01252 inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
01253 if (bitoffset == -1 )
01254 {
01255 curbyte++;
01256 bitoffset = 7;
01257 }
01258
01259
01260
01261 }
01262 *info = inf;
01263
01264 return bitcounter;
01265 }
01266 }
01267
01285 int ShowBits (byte buffer[],int totbitoffset,int bitcount, int numbits)
01286 {
01287 if ((totbitoffset + numbits ) > bitcount)
01288 {
01289 return -1;
01290 }
01291 else
01292 {
01293 int bitoffset = 7 - (totbitoffset & 0x07);
01294 int byteoffset = (totbitoffset >> 3);
01295 byte *curbyte = &(buffer[byteoffset]);
01296 int inf = 0;
01297
01298 while (numbits--)
01299 {
01300 inf <<=1;
01301 inf |= ((*curbyte)>> (bitoffset--)) & 0x01;
01302
01303 if (bitoffset == -1 )
01304 {
01305 curbyte++;
01306 bitoffset = 7;
01307 }
01308 }
01309 return inf;
01310 }
01311 }
01312