00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include "momusys.h"
00042 #include "text_defs.h"
00043 #include "bitstream.h"
00044 #include "text_bits.h"
00045 #include "putvlc.h"
00046 #include "zigzag.h"
00047 #include "max_level.h"
00048
00049 Int IntraDC_dpcm (Int val, Int lum, Image *bitstream);
00050 Int CodeCoeff (Int j_start, Int Mode, Int qcoeff[],
00051 Int block, Int ncoeffs, Image *bitstream);
00052 Int CodeCoeff_RVLC (Int j_start, Int Mode, Int qcoeff[],
00053 Int block, Int ncoeffs, Image *bitstream);
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 Void MB_CodeCoeff(Bits* bits, Int *qcoeff,
00081 Int Mode, Int CBP, Int ncoeffs,
00082 Int intra_dcpred_disable,
00083 Image *DCbitstream,
00084 Image *bitstream,
00085 Int transp_pattern[], Int direction[],
00086 Int error_res_disable,
00087 Int reverse_vlc,
00088 Int switched,
00089 Int alternate_scan)
00090 {
00091 Int i, m, coeff[64];
00092 Int *zz = alternate_scan ? zigzag_v : zigzag;
00093
00094 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00095 {
00096 if (intra_dcpred_disable == 0)
00097 {
00098 for (i = 0; i < 6; i++)
00099 {
00100
00101 {
00102 if (!alternate_scan)
00103 {
00104 switch (direction[i])
00105 {
00106 case 1: zz = zigzag_v; break;
00107 case 2: zz = zigzag_h; break;
00108 case 0: break;
00109 default: fprintf(stderr, "MB_CodeCoeff(): Error in zigzag direction\n");
00110 exit(-1);
00111 }
00112 }
00113
00114 for (m = 0; m < 64; m++)
00115 {
00116 *(coeff + zz[m]) = qcoeff[i*ncoeffs+m];
00117 }
00118
00119 if (switched==0)
00120 {
00121 if (error_res_disable)
00122 {
00123 if (i < 4)
00124 bits->Y += IntraDC_dpcm(coeff[0],1,bitstream);
00125 else
00126 bits->C += IntraDC_dpcm(coeff[0],0,bitstream);
00127 }
00128 else
00129 {
00130 if (i < 4)
00131 bits->Y += IntraDC_dpcm(coeff[0],1,DCbitstream);
00132 else
00133 bits->C += IntraDC_dpcm(coeff[0],0,DCbitstream);
00134 }
00135 }
00136
00137
00138 if ((i==0 && CBP&32) ||
00139 (i==1 && CBP&16) ||
00140 (i==2 && CBP&8) ||
00141 (i==3 && CBP&4) ||
00142 (i==4 && CBP&2) ||
00143 (i==5 && CBP&1))
00144 {
00145 if (error_res_disable || ((!error_res_disable) && (!reverse_vlc)))
00146 {
00147 if (i < 4)
00148 bits->Y += CodeCoeff(1-switched,Mode, coeff,i,ncoeffs,bitstream);
00149 else
00150 bits->C += CodeCoeff(1-switched,Mode, coeff,i,ncoeffs,
00151 bitstream);
00152 }
00153 else
00154 {
00155 if (i < 4)
00156 bits->Y += CodeCoeff_RVLC(1-switched,Mode, coeff, i,
00157 ncoeffs, bitstream);
00158 else
00159 bits->C += CodeCoeff_RVLC(1-switched,Mode, coeff, i,
00160 ncoeffs, bitstream);
00161 }
00162 }
00163 }
00164 }
00165 }
00166 else
00167 {
00168 for (i = 0; i < 6; i++)
00169 {
00170
00171 {
00172
00173 for (m = 0; m < 64; m++)
00174 *(coeff + zz[m]) = qcoeff[i*ncoeffs+m];
00175
00176 if (switched==0)
00177 {
00178 if (error_res_disable)
00179 {
00180 if (coeff[0] != 128)
00181 BitstreamPutBits(bitstream,(long)(coeff[0]),8L);
00182 else
00183 BitstreamPutBits(bitstream, 255L, 8L);
00184 }
00185 else
00186 {
00187 if (coeff[0] != 128)
00188 BitstreamPutBits(DCbitstream,(long)(coeff[0]),8L);
00189 else
00190 BitstreamPutBits(DCbitstream,255L, 8L);
00191 }
00192
00193 if (i < 4)
00194 bits->Y += 8;
00195 else
00196 bits->C += 8;
00197 }
00198
00199 if ((i==0 && CBP&32) || (i==1 && CBP&16) ||
00200 (i==2 && CBP&8) || (i==3 && CBP&4) ||
00201 (i==4 && CBP&2) || (i==5 && CBP&1))
00202 {
00203
00204
00205 if (error_res_disable || ((!error_res_disable) && (!reverse_vlc)))
00206 {
00207 if (i < 4)
00208 bits->Y += CodeCoeff(1-switched,Mode, coeff,i,ncoeffs,
00209 bitstream);
00210 else
00211 bits->C += CodeCoeff(1-switched,Mode, coeff,i,ncoeffs,
00212 bitstream);
00213 }
00214 else
00215 {
00216 if (i < 4)
00217 bits->Y += CodeCoeff_RVLC(1-switched,Mode, coeff, i,
00218 ncoeffs, bitstream);
00219 else
00220 bits->C += CodeCoeff_RVLC(1-switched,Mode, coeff, i,
00221 ncoeffs, bitstream);
00222 }
00223
00224 }
00225 }
00226 }
00227 }
00228 }
00229 else
00230 {
00231 for (i = 0; i < 6; i++)
00232 {
00233
00234 for (m = 0; m < 64; m++)
00235 *(coeff + zz[m]) = qcoeff[i*ncoeffs+m];
00236 if ((i==0 && CBP&32) ||
00237 (i==1 && CBP&16) ||
00238 (i==2 && CBP&8) ||
00239 (i==3 && CBP&4) ||
00240 (i==4 && CBP&2) ||
00241 (i==5 && CBP&1))
00242 {
00243 if (error_res_disable || ((!error_res_disable) && (!reverse_vlc)))
00244 {
00245 if (i < 4)
00246 bits->Y += CodeCoeff(0,Mode, coeff, i, ncoeffs, bitstream);
00247 else
00248 bits->C += CodeCoeff(0,Mode, coeff, i, ncoeffs, bitstream);
00249 }
00250 else
00251 {
00252 if (i < 4)
00253 bits->Y += CodeCoeff_RVLC(0,Mode, coeff, i, ncoeffs,
00254 bitstream);
00255 else
00256 bits->C += CodeCoeff_RVLC(0,Mode, coeff, i, ncoeffs,
00257 bitstream);
00258 }
00259
00260 }
00261 }
00262 }
00263 }
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 Int
00285 IntraDC_dpcm(Int val, Int lum, Image *bitstream)
00286 {
00287 Int n_bits;
00288 Int absval, size = 0;
00289
00290 absval = ( val <0)?-val:val;
00291
00292
00293
00294 size = 0;
00295 while(absval)
00296 {
00297 absval>>=1;
00298 size++;
00299 }
00300
00301 if (lum)
00302 {
00303 n_bits = PutDCsize_lum (size, bitstream);
00304 }
00305 else
00306 {
00307 n_bits = PutDCsize_chrom (size, bitstream);
00308 }
00309
00310 if ( size != 0 )
00311 {
00312 if (val>=0)
00313 {
00314 ;
00315 }
00316 else
00317 {
00318 absval = -val;
00319 val = (absval ^( (int)pow(2.0,(double)size)-1) );
00320 }
00321 BitstreamPutBits(bitstream, (long)(val), (long)(size));
00322 n_bits += size;
00323
00324 if (size > 8)
00325 BitstreamPutBits(bitstream, (long)1, (long)1);
00326 }
00327
00328 return n_bits;
00329
00330 }
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 Int CodeCoeff(Int j_start, Int Mode, Int qcoeff[], Int block, Int ncoeffs, Image *bitstream)
00357 {
00358 Int j, bits;
00359 Int prev_run, run, prev_level, level, first;
00360 Int prev_ind, ind, prev_s, s, length;
00361
00362 run = bits = 0;
00363 first = 1;
00364 prev_run = prev_level = prev_ind = level = s = prev_s = ind = 0;
00365
00366 for (j = j_start; j< ncoeffs; j++)
00367 {
00368
00369
00370
00371 {
00372
00373
00374
00375
00376
00377 s = 0;
00378
00379
00380
00381 if ((level = qcoeff[j]) == 0)
00382 {
00383 run++;
00384 }
00385 else
00386 {
00387
00388
00389 if (level < 0)
00390 {
00391 s = 1;
00392 level = -level;
00393 }
00394
00395 ind = level | run<<4;
00396 ind = ind | 0<<12;
00397
00398 if (!first)
00399 {
00400
00401
00402 if ((prev_run < 64) &&
00403 (((prev_level < 13) && (Mode != MODE_INTRA &&
00404 Mode != MODE_INTRA_Q))
00405 || ((prev_level < 28) && (Mode == MODE_INTRA ||
00406 Mode == MODE_INTRA_Q))))
00407 {
00408
00409 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00410 {
00411 length = PutCoeff_Intra(prev_run, prev_level,
00412 0, bitstream);
00413 }
00414 else
00415 {
00416 length = PutCoeff_Inter(prev_run, prev_level,
00417 0, bitstream);
00418 }
00419 }
00420 else
00421 length = 0;
00422
00423
00424 if (length == 0)
00425 {
00426
00427 if ( prev_run < 64 )
00428 {
00429
00430
00431 int level_minus_max;
00432
00433 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00434 level_minus_max = prev_level -
00435 intra_max_level[0][prev_run];
00436 else
00437 level_minus_max = prev_level -
00438 inter_max_level[0][prev_run];
00439
00440 if ( ( (level_minus_max < 13) && (Mode != MODE_INTRA && Mode != MODE_INTRA_Q) ) ||
00441 ( (level_minus_max < 28) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) ) )
00442 {
00443
00444 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00445 {
00446 length = PutLevelCoeff_Intra(prev_run, level_minus_max, 0, bitstream);
00447 }
00448 else
00449 {
00450 length = PutLevelCoeff_Inter(prev_run, level_minus_max, 0, bitstream);
00451 }
00452 } else
00453 length = 0;
00454 }
00455 else length = 0;
00456 }
00457
00458
00459 if (length == 0)
00460 {
00461 if ( ((prev_level < 13) && (Mode != MODE_INTRA && Mode != MODE_INTRA_Q)) ||
00462 ((prev_level < 28) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)) )
00463 {
00464
00465
00466 int run_minus_max;
00467
00468 if (prev_level == 0)
00469 {
00470 fprintf (stdout, "ERROR(CodeCoeff-second esc): level is %d\n", prev_level);
00471 exit(-1);
00472 }
00473
00474 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00475 run_minus_max = prev_run - (intra_max_run0[prev_level]+1);
00476 else
00477 run_minus_max = prev_run - (inter_max_run0[prev_level]+1);
00478
00479
00480 if (run_minus_max < 64)
00481 {
00482
00483 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00484 {
00485 length = PutRunCoeff_Intra(run_minus_max, prev_level, 0, bitstream);
00486 }
00487 else
00488 {
00489 length = PutRunCoeff_Inter(run_minus_max, prev_level, 0, bitstream);
00490 }
00491 } else
00492 length = 0;
00493 }
00494 else length = 0;
00495 }
00496
00497
00498 if (length == 0)
00499 {
00500
00501 if (prev_s == 1)
00502 {
00503
00504
00505 prev_level = (prev_level^0xfff)+1;
00506 }
00507 BitstreamPutBits(bitstream, 3L, 7L);
00508
00509 BitstreamPutBits(bitstream, 3L, 2L);
00510
00511
00512 BitstreamPutBits(bitstream, 0L, 1L);
00513
00514 BitstreamPutBits(bitstream, (long)(prev_run), 6L);
00515
00516
00517 BitstreamPutBits(bitstream, MARKER_BIT, 1);
00518
00519
00520
00521
00522
00523 BitstreamPutBits(bitstream, (long)(prev_level), 12L);
00524
00525
00526 BitstreamPutBits(bitstream, MARKER_BIT, 1);
00527
00528
00529 bits += 30;
00530 }
00531 else
00532 {
00533 BitstreamPutBits(bitstream, (long)(prev_s), 1L);
00534 bits += length + 1;
00535 }
00536 }
00537
00538 prev_run = run; prev_s = s;
00539 prev_level = level; prev_ind = ind;
00540
00541 run = first = 0;
00542 }
00543 }
00544 }
00545
00546
00547
00548 if (!first)
00549 {
00550 prev_ind = prev_ind | 1<<12;
00551
00552 if ((prev_run < 64) &&
00553 (((prev_level < 4) && (Mode != MODE_INTRA &&
00554 Mode != MODE_INTRA_Q))
00555 || ((prev_level < 9) && ((Mode == MODE_INTRA) ||
00556 (Mode == MODE_INTRA_Q)))))
00557 {
00558
00559 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00560 {
00561 length = PutCoeff_Intra(prev_run, prev_level, 1,
00562 bitstream);
00563 }
00564 else
00565 {
00566 length = PutCoeff_Inter(prev_run, prev_level, 1,
00567 bitstream);
00568 }
00569 }
00570 else
00571 length = 0;
00572
00573
00574 if (length == 0)
00575 {
00576 if ( prev_run < 64 )
00577 {
00578
00579
00580 int level_minus_max;
00581
00582 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00583 level_minus_max = prev_level - intra_max_level[1][prev_run];
00584 else
00585 level_minus_max = prev_level - inter_max_level[1][prev_run];
00586
00587 if ( ( (level_minus_max < 4) && (Mode != MODE_INTRA && Mode != MODE_INTRA_Q) ) ||
00588 ( (level_minus_max < 9) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q) ) )
00589 {
00590
00591 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00592 {
00593 length = PutLevelCoeff_Intra(prev_run, level_minus_max, 1, bitstream);
00594 }
00595 else
00596 {
00597 length = PutLevelCoeff_Inter(prev_run, level_minus_max, 1, bitstream);
00598 }
00599 } else
00600 length = 0;
00601 }
00602 else length = 0;
00603 }
00604
00605
00606 if (length == 0)
00607 {
00608 if (((prev_level < 4) && (Mode != MODE_INTRA && Mode != MODE_INTRA_Q))||
00609 ((prev_level < 9) && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)) )
00610 {
00611
00612
00613 int run_minus_max;
00614
00615 if (prev_level == 0)
00616 {
00617 fprintf (stdout, "ERROR(CodeCoeff-second esc): level is %d\n", prev_level);
00618 exit(-1);
00619 }
00620
00621 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00622 run_minus_max = prev_run - (intra_max_run1[prev_level]+1);
00623 else
00624 run_minus_max = prev_run - (inter_max_run1[prev_level]+1);
00625
00626 if (run_minus_max < 64)
00627 {
00628
00629 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00630 {
00631 length = PutRunCoeff_Intra(run_minus_max, prev_level, 1, bitstream);
00632 }
00633 else
00634 {
00635 length = PutRunCoeff_Inter(run_minus_max, prev_level, 1, bitstream);
00636 }
00637 } else
00638 length = 0;
00639 }
00640 else length = 0;
00641 }
00642
00643
00644 if (length == 0)
00645 {
00646
00647 if (prev_s == 1)
00648 {
00649
00650
00651 prev_level = (prev_level^0xfff)+1;
00652 }
00653 BitstreamPutBits(bitstream, 3L, 7L);
00654 BitstreamPutBits(bitstream, 3L, 2L);
00655
00656 BitstreamPutBits(bitstream, 1L, 1L);
00657 BitstreamPutBits(bitstream, (long)(prev_run), 6L);
00658
00659
00660 BitstreamPutBits(bitstream, MARKER_BIT, 1);
00661
00662
00663
00664
00665
00666 BitstreamPutBits(bitstream, (long)(prev_level), 12L);
00667
00668
00669 BitstreamPutBits(bitstream, MARKER_BIT, 1);
00670
00671
00672 bits += 30;
00673
00674 }
00675 else
00676 {
00677 BitstreamPutBits(bitstream, (long)(prev_s), 1L);
00678 bits += length + 1;
00679 }
00680 }
00681
00682 return bits;
00683 }
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708 Int CodeCoeff_RVLC(Int j_start, Int Mode, Int qcoeff[], Int block, Int ncoeffs, Image *bitstream)
00709 {
00710 Int j, bits;
00711 Int prev_run, run, prev_level, level, first;
00712 Int prev_ind, ind, prev_s, s, length;
00713
00714 run = bits = 0;
00715 first = 1;
00716 prev_run = prev_level = prev_ind = level = s = prev_s = ind = 0;
00717
00718 for (j = j_start; j< ncoeffs; j++)
00719 {
00720
00721
00722
00723 {
00724
00725
00726
00727
00728
00729 s = 0;
00730
00731
00732
00733 if ((level = qcoeff[j]) == 0)
00734 {
00735 run++;
00736 }
00737 else
00738 {
00739
00740
00741 if (level < 0)
00742 {
00743 s = 1;
00744 level = -level;
00745 }
00746
00747 ind = level | run<<4;
00748 ind = ind | 0<<12;
00749 if (!first)
00750 {
00751
00752
00753 if (prev_level < 28 && prev_run < 39)
00754
00755 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00756 {
00757 length = PutCoeff_Intra_RVLC(prev_run, prev_level, 0,
00758 bitstream);
00759 }
00760 else
00761 {
00762 length = PutCoeff_Inter_RVLC(prev_run, prev_level, 0,
00763 bitstream);
00764 }
00765 else
00766 length = 0;
00767
00768 if (length == 0)
00769 {
00770
00771
00772 BitstreamPutBits(bitstream, 1L, 5L);
00773
00774
00775 BitstreamPutBits(bitstream, 0L, 1L);
00776
00777 BitstreamPutBits(bitstream,
00778 (long)(prev_run), 6L);
00779
00780
00781
00782
00783
00784
00785 BitstreamPutBits( bitstream, MARKER_BIT, 1 );
00786
00787 BitstreamPutBits( bitstream, (long)(prev_level), 11L);
00788 BitstreamPutBits( bitstream, MARKER_BIT, 1 );
00789
00790
00791 BitstreamPutBits(bitstream, 0L, 4L);
00792
00793
00794 BitstreamPutBits(bitstream,
00795 (long)(prev_s),1L);
00796
00797 bits += 5 + 1 + 6 + 1 + 11 + 1 + 4 + 1;
00798
00799 }
00800 else
00801 {
00802 BitstreamPutBits(bitstream,
00803 (long)(prev_s), 1L);
00804 bits += length + 1;
00805 }
00806 }
00807
00808 prev_run = run; prev_s = s;
00809 prev_level = level; prev_ind = ind;
00810
00811 run = first = 0;
00812 }
00813 }
00814 }
00815
00816
00817
00818 if (!first)
00819 {
00820 prev_ind = prev_ind | 1<<12;
00821
00822 if (prev_level < 5 && prev_run < 45)
00823
00824 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
00825 {
00826 length = PutCoeff_Intra_RVLC(prev_run, prev_level, 1,
00827 bitstream);
00828 }
00829 else
00830 {
00831 length = PutCoeff_Inter_RVLC(prev_run, prev_level, 1,
00832 bitstream);
00833 }
00834 else
00835 length = 0;
00836
00837 if (length == 0)
00838 {
00839
00840 BitstreamPutBits(bitstream, 1L, 5L);
00841
00842 BitstreamPutBits(bitstream, 1L, 1L);
00843
00844
00845 BitstreamPutBits(bitstream, (long)(prev_run), 6L);
00846
00847
00848
00849
00850 BitstreamPutBits( bitstream, MARKER_BIT, 1 );
00851
00852 BitstreamPutBits( bitstream, (long)(prev_level), 11L);
00853 BitstreamPutBits( bitstream, MARKER_BIT, 1 );
00854
00855 BitstreamPutBits(bitstream, 0L, 4L);
00856
00857
00858 BitstreamPutBits(bitstream, (long)(prev_s), 1L);
00859
00860 bits += 24;
00861
00862 }
00863 else
00864 {
00865 BitstreamPutBits(bitstream, (long)(prev_s), 1L);
00866 bits += length + 1;
00867 }
00868 }
00869
00870 return bits;
00871 }
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887 void
00888 Bits_Reset (Bits *bits)
00889 {
00890 memset(bits, 0, sizeof(Bits));
00891 }