00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "contributors.h"
00018
00019 #include <math.h>
00020
00021 #include "global.h"
00022 #include "enc_statistics.h"
00023 #include "vlc.h"
00024
00025 #if TRACE
00026 #define SYMTRACESTRING(s) strncpy(sym.tracestring,s,TRACESTRING_SIZE)
00027 #else
00028 #define SYMTRACESTRING(s) // do nothing
00029 #endif
00030
00031
00032 static const byte NCBP[2][48][2]=
00033 {
00034 {
00035 { 1, 0},{10, 1},{11, 2},{ 6, 5},{12, 3},{ 7, 6},{14,14},{ 2,10},{13, 4},{15,15},{ 8, 7},{ 3,11},
00036 { 9, 8},{ 4,12},{ 5,13},{ 0, 9},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},
00037 { 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},
00038 { 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0},{ 0, 0}
00039 },
00040 {
00041 { 3, 0},{29, 2},{30, 3},{17, 7},{31, 4},{18, 8},{37,17},{ 8,13},{32, 5},{38,18},{19, 9},{ 9,14},
00042 {20,10},{10,15},{11,16},{ 2,11},{16, 1},{33,32},{34,33},{21,36},{35,34},{22,37},{39,44},{ 4,40},
00043 {36,35},{40,45},{23,38},{ 5,41},{24,39},{ 6,42},{ 7,43},{ 1,19},{41, 6},{42,24},{43,25},{25,20},
00044 {44,26},{26,21},{46,46},{12,28},{45,27},{47,47},{27,22},{13,29},{28,23},{14,30},{15,31},{ 0,12}
00045 }
00046 };
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 int ue_v (char *tracestring, int value, Bitstream *bitstream)
00073 {
00074 SyntaxElement symbol, *sym=&symbol;
00075 sym->value1 = value;
00076 sym->value2 = 0;
00077
00078
00079
00080 ue_linfo(sym->value1,sym->value2,&(sym->len),&(sym->inf));
00081 symbol2uvlc(sym);
00082
00083 writeUVLC2buffer (sym, bitstream);
00084
00085 #if TRACE
00086 strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
00087 trace2out (sym);
00088 #endif
00089
00090 return (sym->len);
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 int se_v (char *tracestring, int value, Bitstream *bitstream)
00118 {
00119 SyntaxElement symbol, *sym=&symbol;
00120 sym->value1 = value;
00121 sym->value2 = 0;
00122
00123
00124
00125 se_linfo(sym->value1,sym->value2,&(sym->len),&(sym->inf));
00126 symbol2uvlc(sym);
00127
00128 writeUVLC2buffer (sym, bitstream);
00129
00130 #if TRACE
00131 strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
00132 trace2out (sym);
00133 #endif
00134
00135 return (sym->len);
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 Boolean u_1 (char *tracestring, int value, Bitstream *bitstream)
00164 {
00165 SyntaxElement symbol, *sym=&symbol;
00166
00167 sym->bitpattern = value;
00168 sym->value1 = value;
00169 sym->len = 1;
00170
00171
00172
00173 writeUVLC2buffer(sym, bitstream);
00174
00175 #if TRACE
00176 strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
00177 trace2out (sym);
00178 #endif
00179
00180 return ((Boolean) sym->len);
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 int u_v (int n, char *tracestring, int value, Bitstream *bitstream)
00211 {
00212 SyntaxElement symbol, *sym=&symbol;
00213
00214 sym->bitpattern = value;
00215 sym->value1 = value;
00216 sym->len = n;
00217
00218
00219
00220 writeUVLC2buffer(sym, bitstream);
00221
00222 #if TRACE
00223 strncpy(sym->tracestring,tracestring,TRACESTRING_SIZE);
00224 trace2out (sym);
00225 #endif
00226
00227 return (sym->len);
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 void ue_linfo(int ue, int dummy, int *len,int *info)
00246 {
00247 int i, nn =(ue + 1) >> 1;
00248
00249 for (i=0; i < 33 && nn != 0; i++)
00250 {
00251 nn >>= 1;
00252 }
00253 *len = (i << 1) + 1;
00254 *info = ue + 1 - (1 << i);
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 void se_linfo(int se, int dummy, int *len,int *info)
00273 {
00274 int sign = (se <= 0) ? 1 : 0;
00275 int n = iabs(se) << 1;
00276 int nn = (n >> 1);
00277 int i;
00278 for (i = 0; i < 33 && nn != 0; i++)
00279 {
00280 nn >>= 1;
00281 }
00282 *len = (i << 1) + 1;
00283 *info = n - (1 << i) + sign;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 void cbp_linfo_intra_other(int cbp, int dummy, int *len,int *info)
00296 {
00297 ue_linfo(NCBP[0][cbp][0], dummy, len, info);
00298 }
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309 void cbp_linfo_intra_normal(int cbp, int dummy, int *len,int *info)
00310 {
00311 ue_linfo(NCBP[1][cbp][0], dummy, len, info);
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 void cbp_linfo_inter_other(int cbp, int dummy, int *len,int *info)
00323 {
00324 ue_linfo(NCBP[0][cbp][1], dummy, len, info);
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 void cbp_linfo_inter_normal(int cbp, int dummy, int *len,int *info)
00336 {
00337 ue_linfo(NCBP[1][cbp][1], dummy, len, info);
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 void levrun_linfo_c2x2(int level,int run,int *len,int *info)
00353 {
00354 static const byte NTAB[2][2]=
00355 {
00356 {1,5},
00357 {3,0}
00358 };
00359 static const byte LEVRUN[4]=
00360 {
00361 2,1,0,0
00362 };
00363
00364 if (level == 0)
00365 {
00366 *len=1;
00367 return;
00368 }
00369 else
00370 {
00371 int levabs = iabs(level);
00372 int sign = level <= 0 ? 1 : 0;
00373 int n = (levabs <= LEVRUN[run]) ? NTAB[levabs - 1][run] + 1 : (levabs - LEVRUN[run]) * 8 + run * 2;
00374 int nn = n >> 1;
00375 int i;
00376
00377 for (i=0; i < 16 && nn != 0; i++)
00378 {
00379 nn >>= 1;
00380 }
00381 *len = (i << 1) + 1;
00382 *info = n - (1 << i) + sign;
00383 }
00384 }
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 void levrun_linfo_inter(int level,int run,int *len,int *info)
00400 {
00401 static const byte LEVRUN[16]=
00402 {
00403 4,2,2,1,1,1,1,1,1,1,0,0,0,0,0,0
00404 };
00405 static const byte NTAB[4][10]=
00406 {
00407 { 1, 3, 5, 9,11,13,21,23,25,27},
00408 { 7,17,19, 0, 0, 0, 0, 0, 0, 0},
00409 {15, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00410 {29, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00411 };
00412
00413 if (level == 0)
00414 {
00415 *len=1;
00416 return;
00417 }
00418 else
00419 {
00420
00421 int sign = (level <= 0) ? 1 : 0;
00422 int levabs = iabs(level);
00423
00424 int n = (levabs <= LEVRUN[run]) ? NTAB[levabs - 1][run] + 1 : (levabs - LEVRUN[run]) * 32 + run * 2;
00425
00426 int nn = n >> 1;
00427 int i;
00428
00429 for (i=0; i < 16 && nn != 0; i++)
00430 {
00431 nn >>= 1;
00432 }
00433 *len = (i << 1) + 1;
00434 *info = n - (1 << i) + sign;
00435 }
00436 }
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451 int symbol2uvlc(SyntaxElement *sym)
00452 {
00453 int suffix_len = sym->len >> 1;
00454
00455 suffix_len = (1 << suffix_len);
00456 sym->bitpattern = suffix_len | (sym->inf & (suffix_len - 1));
00457 return 0;
00458 }
00459
00460
00461
00462
00463
00464
00465
00466 void writeSE_UVLC(SyntaxElement *se, DataPartition *dp)
00467 {
00468 ue_linfo (se->value1,se->value2,&(se->len),&(se->inf));
00469 symbol2uvlc(se);
00470
00471 writeUVLC2buffer(se, dp->bitstream);
00472
00473 if(se->type != SE_HEADER)
00474 dp->bitstream->write_flag = 1;
00475
00476 #if TRACE
00477 if(dp->bitstream->trace_enabled)
00478 trace2out (se);
00479 #endif
00480 }
00481
00482
00483
00484
00485
00486
00487
00488 void writeSE_SVLC(SyntaxElement *se, DataPartition *dp)
00489 {
00490 se_linfo (se->value1,se->value2,&(se->len),&(se->inf));
00491 symbol2uvlc(se);
00492
00493 writeUVLC2buffer(se, dp->bitstream);
00494
00495 if(se->type != SE_HEADER)
00496 dp->bitstream->write_flag = 1;
00497
00498 #if TRACE
00499 if(dp->bitstream->trace_enabled)
00500 trace2out (se);
00501 #endif
00502 }
00503
00504
00505
00506
00507
00508
00509
00510 void writeCBP_VLC (Macroblock* currMB, SyntaxElement *se, DataPartition *dp)
00511 {
00512 if (currMB->mb_type == I4MB || currMB->mb_type == SI4MB || currMB->mb_type == I8MB)
00513 {
00514 currMB->cbp_linfo_intra (se->value1, se->value2, &(se->len), &(se->inf));
00515 }
00516 else
00517 {
00518 currMB->cbp_linfo_inter (se->value1,se->value2,&(se->len),&(se->inf));
00519 }
00520 symbol2uvlc(se);
00521
00522 writeUVLC2buffer(se, dp->bitstream);
00523
00524 if(se->type != SE_HEADER)
00525 dp->bitstream->write_flag = 1;
00526
00527 #if TRACE
00528 if(dp->bitstream->trace_enabled)
00529 trace2out (se);
00530 #endif
00531 }
00532
00533
00534
00535
00536
00537
00538
00539
00540 void writeIntraPredMode_CAVLC(SyntaxElement *se, DataPartition *dp)
00541 {
00542
00543 if (se->value1 == -1)
00544 {
00545 se->len = 1;
00546 se->inf = 1;
00547 }
00548 else
00549 {
00550 se->len = 4;
00551 se->inf = se->value1;
00552 }
00553
00554 se->bitpattern = se->inf;
00555 writeUVLC2buffer(se, dp->bitstream);
00556
00557 if(se->type != SE_HEADER)
00558 dp->bitstream->write_flag = 1;
00559
00560 #if TRACE
00561 if(dp->bitstream->trace_enabled)
00562 trace2out (se);
00563 #endif
00564
00565 return;
00566 }
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577 int writeSyntaxElement2Buf_UVLC(SyntaxElement *se, Bitstream* this_streamBuffer )
00578 {
00579
00580 se->mapping(se->value1,se->value2,&(se->len),&(se->inf));
00581
00582 symbol2uvlc(se);
00583
00584 writeUVLC2buffer(se, this_streamBuffer );
00585
00586 #if TRACE
00587 if(se->type <= 1)
00588 trace2out (se);
00589 #endif
00590
00591 return (se->len);
00592 }
00593
00594
00595
00596
00597
00598
00599
00600
00601 void writeUVLC2buffer(SyntaxElement *se, Bitstream *currStream)
00602 {
00603 unsigned int mask = 1 << (se->len - 1);
00604 byte *byte_buf = &currStream->byte_buf;
00605 int *bits_to_go = &currStream->bits_to_go;
00606 int i;
00607
00608
00609
00610 if ( se->len < 33 )
00611 {
00612 for (i = 0; i < se->len; i++)
00613 {
00614 *byte_buf <<= 1;
00615
00616 if (se->bitpattern & mask)
00617 *byte_buf |= 1;
00618
00619 mask >>= 1;
00620
00621 if ((--(*bits_to_go)) == 0)
00622 {
00623 *bits_to_go = 8;
00624 currStream->streamBuffer[currStream->byte_pos++] = *byte_buf;
00625 *byte_buf = 0;
00626 }
00627 }
00628 }
00629 else
00630 {
00631
00632 for (i = 0; i < (se->len - 32); i++)
00633 {
00634 *byte_buf <<= 1;
00635
00636 if ((--(*bits_to_go)) == 0)
00637 {
00638 *bits_to_go = 8;
00639 currStream->streamBuffer[currStream->byte_pos++] = *byte_buf;
00640 *byte_buf = 0;
00641 }
00642 }
00643
00644 mask = (unsigned int) 1 << 31;
00645 for (i = 0; i < 32; i++)
00646 {
00647 *byte_buf <<= 1;
00648
00649 if (se->bitpattern & mask)
00650 *byte_buf |= 1;
00651
00652 mask >>= 1;
00653
00654 if ((--(*bits_to_go)) == 0)
00655 {
00656 *bits_to_go = 8;
00657 currStream->streamBuffer[currStream->byte_pos++] = *byte_buf;
00658 *byte_buf = 0;
00659 }
00660 }
00661 }
00662 }
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673 int writeSyntaxElement2Buf_Fixed(SyntaxElement *se, Bitstream* this_streamBuffer )
00674 {
00675 writeUVLC2buffer(se, this_streamBuffer );
00676
00677 #if TRACE
00678 if(se->type <= 1)
00679 trace2out (se);
00680 #endif
00681 return (se->len);
00682 }
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692 void writeSE_Flag(SyntaxElement *se, DataPartition *dp )
00693 {
00694 se->len = 1;
00695 se->bitpattern = (se->value1 & 1);
00696
00697 writeUVLC2buffer(se, dp->bitstream );
00698
00699 #if TRACE
00700 if(dp->bitstream->trace_enabled)
00701 trace2out (se);
00702 #endif
00703 }
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713 void writeSE_invFlag(SyntaxElement *se, DataPartition *dp )
00714 {
00715 se->len = 1;
00716 se->bitpattern = 1 - (se->value1 & 1);
00717
00718 writeUVLC2buffer(se, dp->bitstream );
00719
00720 #if TRACE
00721 if(dp->bitstream->trace_enabled)
00722 trace2out (se);
00723 #endif
00724 }
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734 void writeSE_Dummy(SyntaxElement *se, DataPartition *dp )
00735 {
00736 se->len = 0;
00737 }
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748 void writeSE_Fix(SyntaxElement *se, Bitstream *bitstream )
00749 {
00750 writeUVLC2buffer(se, bitstream);
00751
00752 #if TRACE
00753 if(bitstream->trace_enabled)
00754 trace2out (se);
00755 #endif
00756 }
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770 int symbol2vlc(SyntaxElement *sym)
00771 {
00772 int info_len = sym->len;
00773
00774
00775 sym->bitpattern = 0;
00776
00777
00778 while(--info_len >= 0)
00779 {
00780 sym->bitpattern <<= 1;
00781 sym->bitpattern |= (0x01 & (sym->inf >> info_len));
00782 }
00783 return 0;
00784 }
00785
00786
00787
00788
00789
00790
00791
00792
00793 int writeSyntaxElement_VLC(SyntaxElement *se, DataPartition *dp)
00794 {
00795 se->inf = se->value1;
00796 se->len = se->value2;
00797 symbol2vlc(se);
00798
00799 writeUVLC2buffer(se, dp->bitstream);
00800
00801 if(se->type != SE_HEADER)
00802 dp->bitstream->write_flag = 1;
00803
00804 #if TRACE
00805 if(dp->bitstream->trace_enabled)
00806 trace2out (se);
00807 #endif
00808
00809 return (se->len);
00810 }
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820 int writeSyntaxElement_NumCoeffTrailingOnes(SyntaxElement *se, DataPartition *dp)
00821 {
00822 static const byte lentab[3][4][17] =
00823 {
00824 {
00825 { 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
00826 { 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
00827 { 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
00828 { 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16},
00829 },
00830 {
00831 { 2, 6, 6, 7, 8, 8, 9,11,11,12,12,12,13,13,13,14,14},
00832 { 0, 2, 5, 6, 6, 7, 8, 9,11,11,12,12,13,13,14,14,14},
00833 { 0, 0, 3, 6, 6, 7, 8, 9,11,11,12,12,13,13,13,14,14},
00834 { 0, 0, 0, 4, 4, 5, 6, 6, 7, 9,11,11,12,13,13,13,14},
00835 },
00836 {
00837 { 4, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9,10,10,10,10},
00838 { 0, 4, 5, 5, 5, 5, 6, 6, 7, 8, 8, 9, 9, 9,10,10,10},
00839 { 0, 0, 4, 5, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,10},
00840 { 0, 0, 0, 4, 4, 4, 4, 4, 5, 6, 7, 8, 8, 9,10,10,10},
00841 },
00842
00843 };
00844
00845 static const byte codtab[3][4][17] =
00846 {
00847 {
00848 { 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7,4},
00849 { 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10,6},
00850 { 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9,5},
00851 { 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12,8},
00852 },
00853 {
00854 { 3,11, 7, 7, 7, 4, 7,15,11,15,11, 8,15,11, 7, 9,7},
00855 { 0, 2, 7,10, 6, 6, 6, 6,14,10,14,10,14,10,11, 8,6},
00856 { 0, 0, 3, 9, 5, 5, 5, 5,13, 9,13, 9,13, 9, 6,10,5},
00857 { 0, 0, 0, 5, 4, 6, 8, 4, 4, 4,12, 8,12,12, 8, 1,4},
00858 },
00859 {
00860 {15,15,11, 8,15,11, 9, 8,15,11,15,11, 8,13, 9, 5,1},
00861 { 0,14,15,12,10, 8,14,10,14,14,10,14,10, 7,12, 8,4},
00862 { 0, 0,13,14,11, 9,13, 9,13,10,13, 9,13, 9,11, 7,3},
00863 { 0, 0, 0,12,11,10, 9, 8,13,12,12,12, 8,12,10, 6,2},
00864 },
00865 };
00866 int vlcnum = se->len;
00867
00868
00869
00870
00871 if (vlcnum == 3)
00872 {
00873 se->len = 6;
00874 if (se->value1 > 0)
00875 {
00876 se->inf = ((se->value1-1) << 2) | se->value2;
00877 }
00878 else
00879 {
00880 se->inf = 3;
00881 }
00882 }
00883 else
00884 {
00885 se->len = lentab[vlcnum][se->value2][se->value1];
00886 se->inf = codtab[vlcnum][se->value2][se->value1];
00887 }
00888
00889 if (se->len == 0)
00890 {
00891 printf("ERROR: (numcoeff,trailingones) not valid: vlc=%d (%d, %d)\n",
00892 vlcnum, se->value1, se->value2);
00893 exit(-1);
00894 }
00895
00896 symbol2vlc(se);
00897
00898 writeUVLC2buffer(se, dp->bitstream);
00899
00900 if(se->type != SE_HEADER)
00901 dp->bitstream->write_flag = 1;
00902
00903 #if TRACE
00904 if(dp->bitstream->trace_enabled)
00905 trace2out (se);
00906 #endif
00907
00908 return (se->len);
00909 }
00910
00911
00912
00913
00914
00915
00916
00917
00918 int writeSyntaxElement_NumCoeffTrailingOnesChromaDC(ImageParameters *p_Img, SyntaxElement *se, DataPartition *dp)
00919 {
00920 static const byte lentab[3][4][17] =
00921 {
00922
00923 {{ 2, 6, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00924 { 0, 1, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00925 { 0, 0, 3, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00926 { 0, 0, 0, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
00927
00928 {{ 1, 7, 7, 9, 9,10,11,12,13, 0, 0, 0, 0, 0, 0, 0, 0},
00929 { 0, 2, 7, 7, 9,10,11,12,12, 0, 0, 0, 0, 0, 0, 0, 0},
00930 { 0, 0, 3, 7, 7, 9,10,11,12, 0, 0, 0, 0, 0, 0, 0, 0},
00931 { 0, 0, 0, 5, 6, 7, 7,10,11, 0, 0, 0, 0, 0, 0, 0, 0}},
00932
00933 {{ 1, 6, 8, 9,10,11,13,13,13,14,14,15,15,16,16,16,16},
00934 { 0, 2, 6, 8, 9,10,11,13,13,14,14,15,15,15,16,16,16},
00935 { 0, 0, 3, 7, 8, 9,10,11,13,13,14,14,15,15,16,16,16},
00936 { 0, 0, 0, 5, 6, 7, 8, 9,10,11,13,14,14,15,15,16,16}}
00937 };
00938
00939 static const byte codtab[3][4][17] =
00940 {
00941
00942 {{ 1, 7, 4, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00943 { 0, 1, 6, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00944 { 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00945 { 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
00946
00947 {{ 1,15,14, 7, 6, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 0},
00948 { 0, 1,13,12, 5, 6, 6, 6, 5, 0, 0, 0, 0, 0, 0, 0, 0},
00949 { 0, 0, 1,11,10, 4, 5, 5, 4, 0, 0, 0, 0, 0, 0, 0, 0},
00950 { 0, 0, 0, 1, 1, 9, 8, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0}},
00951
00952 {{ 1, 5, 7, 7, 7, 7,15,11, 8,15,11,15,11,15,11, 7, 4},
00953 { 0, 1, 4, 6, 6, 6, 6,14,10,14,10,14,10, 1,14,10, 6},
00954 { 0, 0, 1, 5, 5, 5, 5, 5,13, 9,13, 9,13, 9,13, 9, 5},
00955 { 0, 0, 0, 3, 3, 4, 4, 4, 4, 4,12,12, 8,12, 8,12, 8}}
00956
00957 };
00958 int yuv = p_Img->yuv_format - 1;
00959
00960
00961
00962 se->len = lentab[yuv][se->value2][se->value1];
00963 se->inf = codtab[yuv][se->value2][se->value1];
00964
00965 if (se->len == 0)
00966 {
00967 printf("ERROR: (numcoeff,trailingones) not valid: (%d, %d)\n",
00968 se->value1, se->value2);
00969 exit(-1);
00970 }
00971
00972 symbol2vlc(se);
00973
00974 writeUVLC2buffer(se, dp->bitstream);
00975
00976 if(se->type != SE_HEADER)
00977 dp->bitstream->write_flag = 1;
00978
00979 #if TRACE
00980 if(dp->bitstream->trace_enabled)
00981 trace2out (se);
00982 #endif
00983
00984 return (se->len);
00985 }
00986
00987
00988
00989
00990
00991
00992
00993
00994 int writeSyntaxElement_TotalZeros(SyntaxElement *se, DataPartition *dp)
00995 {
00996 static const byte lentab[TOTRUN_NUM][16] =
00997 {
00998 { 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
00999 { 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
01000 { 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
01001 { 5,3,4,4,3,3,3,4,3,4,5,5,5},
01002 { 4,4,4,3,3,3,3,3,4,5,4,5},
01003 { 6,5,3,3,3,3,3,3,4,3,6},
01004 { 6,5,3,3,3,2,3,4,3,6},
01005 { 6,4,5,3,2,2,3,3,6},
01006 { 6,6,4,2,2,3,2,5},
01007 { 5,5,3,2,2,2,4},
01008 { 4,4,3,3,1,3},
01009 { 4,4,2,1,3},
01010 { 3,3,1,2},
01011 { 2,2,1},
01012 { 1,1},
01013 };
01014
01015 static const byte codtab[TOTRUN_NUM][16] =
01016 {
01017 {1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
01018 {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
01019 {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
01020 {3,7,5,4,6,5,4,3,3,2,2,1,0},
01021 {5,4,3,7,6,5,4,3,2,1,1,0},
01022 {1,1,7,6,5,4,3,2,1,1,0},
01023 {1,1,5,4,3,3,2,1,1,0},
01024 {1,1,1,3,3,2,2,1,0},
01025 {1,0,1,3,2,1,1,1,},
01026 {1,0,1,3,2,1,1,},
01027 {0,1,1,2,1,3},
01028 {0,1,1,1,1},
01029 {0,1,1,1},
01030 {0,1,1},
01031 {0,1},
01032 };
01033 int vlcnum = se->len;
01034
01035
01036 se->len = lentab[vlcnum][se->value1];
01037 se->inf = codtab[vlcnum][se->value1];
01038
01039 if (se->len == 0)
01040 {
01041 printf("ERROR: (TotalZeros) not valid: (%d)\n",se->value1);
01042 exit(-1);
01043 }
01044
01045 symbol2vlc(se);
01046
01047 writeUVLC2buffer(se, dp->bitstream);
01048
01049 if(se->type != SE_HEADER)
01050 dp->bitstream->write_flag = 1;
01051
01052 #if TRACE
01053 if(dp->bitstream->trace_enabled)
01054 trace2out (se);
01055 #endif
01056
01057 return (se->len);
01058 }
01059
01060
01061
01062
01063
01064
01065
01066
01067 int writeSyntaxElement_TotalZerosChromaDC(ImageParameters *p_Img, SyntaxElement *se, DataPartition *dp)
01068 {
01069 static const byte lentab[3][TOTRUN_NUM][16] =
01070 {
01071
01072 {{ 1,2,3,3},
01073 { 1,2,2},
01074 { 1,1}},
01075
01076 {{ 1,3,3,4,4,4,5,5},
01077 { 3,2,3,3,3,3,3},
01078 { 3,3,2,2,3,3},
01079 { 3,2,2,2,3},
01080 { 2,2,2,2},
01081 { 2,2,1},
01082 { 1,1}},
01083
01084 {{ 1,3,3,4,4,5,5,6,6,7,7,8,8,9,9,9},
01085 { 3,3,3,3,3,4,4,4,4,5,5,6,6,6,6},
01086 { 4,3,3,3,4,4,3,3,4,5,5,6,5,6},
01087 { 5,3,4,4,3,3,3,4,3,4,5,5,5},
01088 { 4,4,4,3,3,3,3,3,4,5,4,5},
01089 { 6,5,3,3,3,3,3,3,4,3,6},
01090 { 6,5,3,3,3,2,3,4,3,6},
01091 { 6,4,5,3,2,2,3,3,6},
01092 { 6,6,4,2,2,3,2,5},
01093 { 5,5,3,2,2,2,4},
01094 { 4,4,3,3,1,3},
01095 { 4,4,2,1,3},
01096 { 3,3,1,2},
01097 { 2,2,1},
01098 { 1,1}}
01099 };
01100
01101 static const byte codtab[3][TOTRUN_NUM][16] =
01102 {
01103
01104 {{ 1,1,1,0},
01105 { 1,1,0},
01106 { 1,0}},
01107
01108 {{ 1,2,3,2,3,1,1,0},
01109 { 0,1,1,4,5,6,7},
01110 { 0,1,1,2,6,7},
01111 { 6,0,1,2,7},
01112 { 0,1,2,3},
01113 { 0,1,1},
01114 { 0,1}},
01115
01116 {{1,3,2,3,2,3,2,3,2,3,2,3,2,3,2,1},
01117 {7,6,5,4,3,5,4,3,2,3,2,3,2,1,0},
01118 {5,7,6,5,4,3,4,3,2,3,2,1,1,0},
01119 {3,7,5,4,6,5,4,3,3,2,2,1,0},
01120 {5,4,3,7,6,5,4,3,2,1,1,0},
01121 {1,1,7,6,5,4,3,2,1,1,0},
01122 {1,1,5,4,3,3,2,1,1,0},
01123 {1,1,1,3,3,2,2,1,0},
01124 {1,0,1,3,2,1,1,1,},
01125 {1,0,1,3,2,1,1,},
01126 {0,1,1,2,1,3},
01127 {0,1,1,1,1},
01128 {0,1,1,1},
01129 {0,1,1},
01130 {0,1}}
01131 };
01132 int vlcnum = se->len;
01133 int yuv = p_Img->yuv_format - 1;
01134
01135
01136 se->len = lentab[yuv][vlcnum][se->value1];
01137 se->inf = codtab[yuv][vlcnum][se->value1];
01138
01139 if (se->len == 0)
01140 {
01141 printf("ERROR: (TotalZeros) not valid: (%d)\n",se->value1);
01142 exit(-1);
01143 }
01144
01145 symbol2vlc(se);
01146
01147 writeUVLC2buffer(se, dp->bitstream);
01148
01149 if(se->type != SE_HEADER)
01150 dp->bitstream->write_flag = 1;
01151
01152 #if TRACE
01153 if(dp->bitstream->trace_enabled)
01154 trace2out (se);
01155 #endif
01156
01157 return (se->len);
01158 }
01159
01160
01161
01162
01163
01164
01165
01166
01167 int writeSyntaxElement_Run(SyntaxElement *se, DataPartition *dp)
01168 {
01169 static const byte lentab[TOTRUN_NUM][16] =
01170 {
01171 {1,1},
01172 {1,2,2},
01173 {2,2,2,2},
01174 {2,2,2,3,3},
01175 {2,2,3,3,3,3},
01176 {2,3,3,3,3,3,3},
01177 {3,3,3,3,3,3,3,4,5,6,7,8,9,10,11},
01178 };
01179
01180 static const byte codtab[TOTRUN_NUM][16] =
01181 {
01182 {1,0},
01183 {1,1,0},
01184 {3,2,1,0},
01185 {3,2,1,1,0},
01186 {3,2,3,2,1,0},
01187 {3,0,1,3,2,5,4},
01188 {7,6,5,4,3,2,1,1,1,1,1,1,1,1,1},
01189 };
01190 int vlcnum = se->len;
01191
01192
01193 se->len = lentab[vlcnum][se->value1];
01194 se->inf = codtab[vlcnum][se->value1];
01195
01196 if (se->len == 0)
01197 {
01198 printf("ERROR: (run) not valid: (%d)\n",se->value1);
01199 exit(-1);
01200 }
01201
01202 symbol2vlc(se);
01203
01204 writeUVLC2buffer(se, dp->bitstream);
01205
01206 if(se->type != SE_HEADER)
01207 dp->bitstream->write_flag = 1;
01208
01209 #if TRACE
01210 if(dp->bitstream->trace_enabled)
01211 trace2out (se);
01212 #endif
01213
01214 return (se->len);
01215 }
01216
01217
01218
01219
01220
01221
01222
01223
01224 int writeSyntaxElement_Level_VLC1(SyntaxElement *se, DataPartition *dp, int profile_idc)
01225 {
01226 int level = se->value1;
01227 int sign = (level < 0 ? 1 : 0);
01228 int levabs = iabs(level);
01229
01230 if (levabs < 8)
01231 {
01232 se->len = levabs * 2 + sign - 1;
01233 se->inf = 1;
01234 }
01235 else if (levabs < 16)
01236 {
01237
01238 se->len = 19;
01239 se->inf = 16 | ((levabs << 1) - 16) | sign;
01240 }
01241 else
01242 {
01243 int iMask = 4096, numPrefix = 0;
01244 int levabsm16 = levabs + 2032;
01245
01246
01247 if ((levabsm16) >= 4096)
01248 {
01249 numPrefix++;
01250 while ((levabsm16) >= (4096 << numPrefix))
01251 {
01252 numPrefix++;
01253 }
01254 }
01255
01256 iMask <<= numPrefix;
01257 se->inf = iMask | ((levabsm16 << 1) - iMask) | sign;
01258
01259
01260
01261 if (numPrefix > 0 && !IS_FREXT_PROFILE( profile_idc ))
01262 {
01263
01264 se->len = 0x0000FFFF;
01265 return (se->len);
01266 }
01267
01268 se->len = 28 + (numPrefix << 1);
01269 }
01270
01271 symbol2vlc(se);
01272
01273 writeUVLC2buffer(se, dp->bitstream);
01274
01275 if(se->type != SE_HEADER)
01276 dp->bitstream->write_flag = 1;
01277
01278 #if TRACE
01279 if(dp->bitstream->trace_enabled)
01280 trace2out (se);
01281 #endif
01282
01283 return (se->len);
01284 }
01285
01286
01287
01288
01289
01290
01291
01292
01293 int writeSyntaxElement_Level_VLCN(SyntaxElement *se, int vlc, DataPartition *dp, int profile_idc)
01294 {
01295 int level = se->value1;
01296 int sign = (level < 0 ? 1 : 0);
01297 int levabs = iabs(level) - 1;
01298
01299 int shift = vlc - 1;
01300 int escape = (15 << shift);
01301
01302 if (levabs < escape)
01303 {
01304 int sufmask = ~((0xffffffff) << shift);
01305 int suffix = (levabs) & sufmask;
01306
01307 se->len = ((levabs) >> shift) + 1 + vlc;
01308 se->inf = (2 << shift) | (suffix << 1) | sign;
01309 }
01310 else
01311 {
01312 int iMask = 4096;
01313 int levabsesc = levabs - escape + 2048;
01314 int numPrefix = 0;
01315
01316 if ((levabsesc) >= 4096)
01317 {
01318 numPrefix++;
01319 while ((levabsesc) >= (4096 << numPrefix))
01320 {
01321 numPrefix++;
01322 }
01323 }
01324
01325 iMask <<= numPrefix;
01326 se->inf = iMask | ((levabsesc << 1) - iMask) | sign;
01327
01328
01329
01330 if (numPrefix > 0 && !IS_FREXT_PROFILE( profile_idc ))
01331 {
01332
01333 se->len = 0x0000FFFF;
01334 return (se->len);
01335 }
01336 se->len = 28 + (numPrefix << 1);
01337 }
01338
01339 symbol2vlc(se);
01340
01341 writeUVLC2buffer(se, dp->bitstream);
01342
01343 if(se->type != SE_HEADER)
01344 dp->bitstream->write_flag = 1;
01345
01346 #if TRACE
01347 if(dp->bitstream->trace_enabled)
01348 trace2out (se);
01349 #endif
01350
01351 return (se->len);
01352 }
01353
01354
01355 #if TRACE
01356 int bitcounter = 0;
01357
01358
01359
01360
01361
01362
01363
01364 void trace2out(SyntaxElement *sym)
01365 {
01366 int i, chars;
01367
01368 if (p_Enc->p_trace != NULL)
01369 {
01370 putc('@', p_Enc->p_trace);
01371 chars = fprintf(p_Enc->p_trace, "%i", bitcounter);
01372 while(chars++ < 6)
01373 putc(' ',p_Enc->p_trace);
01374
01375 chars += fprintf(p_Enc->p_trace, "%s", sym->tracestring);
01376 while(chars++ < 55)
01377 putc(' ',p_Enc->p_trace);
01378
01379
01380 if(sym->len<15)
01381 {
01382 for(i=0 ; i<15-sym->len ; i++)
01383 fputc(' ', p_Enc->p_trace);
01384 }
01385
01386
01387 bitcounter += sym->len;
01388 for(i=1 ; i<=sym->len ; i++)
01389 {
01390 if((sym->bitpattern >> (sym->len-i)) & 0x1)
01391 fputc('1', p_Enc->p_trace);
01392 else
01393 fputc('0', p_Enc->p_trace);
01394 }
01395 fprintf(p_Enc->p_trace, " (%3d) \n",sym->value1);
01396 }
01397 fflush (p_Enc->p_trace);
01398 }
01399
01400 void trace2out_cabac(SyntaxElement *sym)
01401 {
01402 int chars;
01403
01404 if (p_Enc->p_trace != NULL)
01405 {
01406 putc('@', p_Enc->p_trace);
01407 chars = fprintf(p_Enc->p_trace, "%i", bitcounter);
01408 while(chars++ < 6)
01409 putc(' ',p_Enc->p_trace);
01410
01411 chars += fprintf(p_Enc->p_trace, "%s", sym->tracestring);
01412 while(chars++ < 70)
01413 putc(' ',p_Enc->p_trace);
01414
01415 fprintf(p_Enc->p_trace, " (%3d) \n",sym->value1);
01416 }
01417 fflush (p_Enc->p_trace);
01418 bitcounter += sym->len;
01419 }
01420 #endif
01421
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438 void writeVlcByteAlign(ImageParameters *p_Img, Bitstream* currStream, StatParameters *cur_stats)
01439 {
01440 if (currStream->bits_to_go < 8)
01441 {
01442 currStream->byte_buf = (byte) ((currStream->byte_buf << currStream->bits_to_go) | (0xff >> (8 - currStream->bits_to_go)));
01443 currStream->streamBuffer[currStream->byte_pos++] = currStream->byte_buf;
01444 cur_stats->bit_use_stuffingBits[p_Img->type] += currStream->bits_to_go;
01445 currStream->bits_to_go = 8;
01446 }
01447 }
01448
01449
01450
01451
01452
01453
01454 void reset_mb_nz_coeff(ImageParameters *p_Img, int mb_number)
01455 {
01456 memset(&p_Img->nz_coeff [mb_number][0][0], 0, BLOCK_SIZE * (BLOCK_SIZE + p_Img->num_blk8x8_uv) * sizeof(int));
01457 }
01458
01459
01460 void writeUVLC_CAVLC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
01461 {
01462 writeSE_UVLC(se, dp);
01463 }
01464
01465 void writeSVLC_CAVLC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
01466 {
01467 writeSE_SVLC(se, dp);
01468 }
01469
01470 void writeFlag_CAVLC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
01471 {
01472 writeSE_Flag(se, dp);
01473 }