88 |
const int code2 = sprite_trajectory_len[len].code; |
const int code2 = sprite_trajectory_len[len].code; |
89 |
const int len2 = sprite_trajectory_len[len].len; |
const int len2 = sprite_trajectory_len[len].len; |
90 |
|
|
91 |
// printf("GMC=%d Code/Len = %d / %d ",val, code,len); |
#if 0 |
92 |
// printf("Code2 / Len2 = %d / %d \n",code2,len2); |
printf("GMC=%d Code/Len = %d / %d ",val, code,len); |
93 |
|
printf("Code2 / Len2 = %d / %d \n",code2,len2); |
94 |
|
#endif |
95 |
|
|
96 |
BitstreamPutBits(bs, code2, len2); |
BitstreamPutBits(bs, code2, len2); |
97 |
if (len) BitstreamPutBits(bs, code, len); |
if (len) BitstreamPutBits(bs, code, len); |
650 |
|
|
651 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
652 |
|
|
653 |
// write mcbpc |
/* write mcbpc */ |
654 |
if (frame->coding_type == I_VOP) { |
if (frame->coding_type == I_VOP) { |
655 |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
656 |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
661 |
mcbpc_inter_tab[mcbpc].len); |
mcbpc_inter_tab[mcbpc].len); |
662 |
} |
} |
663 |
|
|
664 |
// ac prediction flag |
/* ac prediction flag */ |
665 |
if (pMB->acpred_directions[0]) |
if (pMB->acpred_directions[0]) |
666 |
BitstreamPutBits(bs, 1, 1); |
BitstreamPutBits(bs, 1, 1); |
667 |
else |
else |
668 |
BitstreamPutBits(bs, 0, 1); |
BitstreamPutBits(bs, 0, 1); |
669 |
|
|
670 |
// write cbpy |
/* write cbpy */ |
671 |
BitstreamPutBits(bs, xvid_cbpy_tab[cbpy].code, xvid_cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, xvid_cbpy_tab[cbpy].code, xvid_cbpy_tab[cbpy].len); |
672 |
|
|
673 |
// write dquant |
/* write dquant */ |
674 |
if (pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA_Q) |
675 |
BitstreamPutBits(bs, DQ_VALUE2INDEX(pMB->dquant), 2); |
BitstreamPutBits(bs, DQ_VALUE2INDEX(pMB->dquant), 2); |
676 |
|
|
677 |
// write interlacing |
/* write interlacing */ |
678 |
if (frame->vol_flags & XVID_VOL_INTERLACING) { |
if (frame->vol_flags & XVID_VOL_INTERLACING) { |
679 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
680 |
} |
} |
681 |
// code block coeffs |
/* code block coeffs */ |
682 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
683 |
if (i < 4) |
if (i < 4) |
684 |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
722 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
723 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
724 |
|
|
725 |
// write mcbpc |
/* write mcbpc */ |
726 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
727 |
mcbpc_inter_tab[mcbpc].len); |
mcbpc_inter_tab[mcbpc].len); |
728 |
|
|
729 |
if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) ) |
if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) ) |
730 |
BitstreamPutBit(bs, pMB->mcsel); // mcsel: '0'=local motion, '1'=GMC |
BitstreamPutBit(bs, pMB->mcsel); /* mcsel: '0'=local motion, '1'=GMC */ |
731 |
|
|
732 |
// write cbpy |
/* write cbpy */ |
733 |
BitstreamPutBits(bs, xvid_cbpy_tab[cbpy].code, xvid_cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, xvid_cbpy_tab[cbpy].code, xvid_cbpy_tab[cbpy].len); |
734 |
|
|
735 |
// write dquant |
/* write dquant */ |
736 |
if (pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER_Q) |
737 |
BitstreamPutBits(bs, DQ_VALUE2INDEX(pMB->dquant), 2); |
BitstreamPutBits(bs, DQ_VALUE2INDEX(pMB->dquant), 2); |
738 |
|
|
739 |
// interlacing |
/* interlacing */ |
740 |
if (frame->vol_flags & XVID_VOL_INTERLACING) { |
if (frame->vol_flags & XVID_VOL_INTERLACING) { |
741 |
if (pMB->cbp) { |
if (pMB->cbp) { |
742 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
743 |
DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct); |
DPRINTF(XVID_DEBUG_MB,"codep: field_dct: %i\n", pMB->field_dct); |
744 |
} |
} |
745 |
|
|
746 |
// if inter block, write field ME flag |
/* if inter block, write field ME flag */ |
747 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
748 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, pMB->field_pred); |
749 |
DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred); |
DPRINTF(XVID_DEBUG_MB,"codep: field_pred: %i\n", pMB->field_pred); |
750 |
|
|
751 |
// write field prediction references |
/* write field prediction references */ |
752 |
if (pMB->field_pred) { |
if (pMB->field_pred) { |
753 |
BitstreamPutBit(bs, pMB->field_for_top); |
BitstreamPutBit(bs, pMB->field_for_top); |
754 |
BitstreamPutBit(bs, pMB->field_for_bot); |
BitstreamPutBit(bs, pMB->field_for_bot); |
755 |
} |
} |
756 |
} |
} |
757 |
} |
} |
758 |
// code motion vector(s) if motion is local |
/* code motion vector(s) if motion is local */ |
759 |
if (!pMB->mcsel) |
if (!pMB->mcsel) |
760 |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
761 |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
764 |
|
|
765 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
766 |
|
|
767 |
// code block coeffs |
/* code block coeffs */ |
768 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) |
769 |
if (pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) |
770 |
{ |
{ |
792 |
Statistics * pStat) |
Statistics * pStat) |
793 |
{ |
{ |
794 |
if (frame->coding_type != I_VOP) |
if (frame->coding_type != I_VOP) |
795 |
BitstreamPutBit(bs, 0); // not_coded |
BitstreamPutBit(bs, 0); /* not_coded */ |
796 |
|
|
797 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
798 |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
801 |
|
|
802 |
} |
} |
803 |
|
|
804 |
/* |
|
805 |
// moved to mbcoding.h so that in can be 'static __inline' |
/* moved to mbcoding.h so that in can be 'static __inline' */ |
806 |
|
#if 0 |
807 |
void |
void |
808 |
MBSkip(Bitstream * bs) |
MBSkip(Bitstream * bs) |
809 |
{ |
{ |
810 |
BitstreamPutBit(bs, 1); // not coded |
BitstreamPutBit(bs, 1); /* not coded */ |
811 |
} |
} |
812 |
*/ |
#endif |
813 |
|
|
814 |
/*************************************************************** |
/*************************************************************** |
815 |
* bframe encoding start |
* bframe encoding start |
867 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
868 |
return; |
return; |
869 |
|
|
870 |
default:; // invalid |
default:; /* invalid */ |
871 |
} |
} |
872 |
} |
} |
873 |
|
|
891 |
------------------------------------------------------------------ */ |
------------------------------------------------------------------ */ |
892 |
|
|
893 |
if (mb->mode == MODE_DIRECT_NONE_MV) { |
if (mb->mode == MODE_DIRECT_NONE_MV) { |
894 |
BitstreamPutBit(bs, 1); // skipped |
BitstreamPutBit(bs, 1); /* skipped */ |
895 |
return; |
return; |
896 |
} |
} |
897 |
|
|
898 |
BitstreamPutBit(bs, 0); // not skipped |
BitstreamPutBit(bs, 0); /* not skipped */ |
899 |
|
|
900 |
if (mb->cbp == 0) { |
if (mb->cbp == 0) { |
901 |
BitstreamPutBit(bs, 1); // cbp == 0 |
BitstreamPutBit(bs, 1); /* cbp == 0 */ |
902 |
} else { |
} else { |
903 |
BitstreamPutBit(bs, 0); // cbp == xxx |
BitstreamPutBit(bs, 0); /* cbp == xxx */ |
904 |
} |
} |
905 |
|
|
906 |
put_bvop_mbtype(bs, mb->mode); |
put_bvop_mbtype(bs, mb->mode); |
910 |
} |
} |
911 |
|
|
912 |
if (mb->mode != MODE_DIRECT && mb->cbp != 0) { |
if (mb->mode != MODE_DIRECT && mb->cbp != 0) { |
913 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
put_bvop_dbquant(bs, 0); /* todo: mb->dquant = 0 */ |
914 |
} |
} |
915 |
|
|
916 |
switch (mb->mode) { |
switch (mb->mode) { |
917 |
case MODE_INTERPOLATE: |
case MODE_INTERPOLATE: |
918 |
CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode |
CodeVector(bs, mb->pmvs[1].x, vcode, pStat); /* forward vector of interpolate mode */ |
919 |
CodeVector(bs, mb->pmvs[1].y, vcode, pStat); |
CodeVector(bs, mb->pmvs[1].y, vcode, pStat); |
920 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
921 |
vcode = bcode; |
vcode = bcode; |
924 |
CodeVector(bs, mb->pmvs[0].y, vcode, pStat); |
CodeVector(bs, mb->pmvs[0].y, vcode, pStat); |
925 |
break; |
break; |
926 |
case MODE_DIRECT: |
case MODE_DIRECT: |
927 |
CodeVector(bs, mb->pmvs[3].x, 1, pStat); // fcode is always 1 for delta vector |
CodeVector(bs, mb->pmvs[3].x, 1, pStat); /* fcode is always 1 for delta vector */ |
928 |
CodeVector(bs, mb->pmvs[3].y, 1, pStat); // prediction is always (0,0) |
CodeVector(bs, mb->pmvs[3].y, 1, pStat); /* prediction is always (0,0) */ |
929 |
default: break; |
default: break; |
930 |
} |
} |
931 |
|
|
947 |
***************************************************************/ |
***************************************************************/ |
948 |
|
|
949 |
|
|
950 |
// for IVOP addbits == 0 |
/* |
951 |
// for PVOP addbits == fcode - 1 |
* for IVOP addbits == 0 |
952 |
// for BVOP addbits == max(fcode,bcode) - 1 |
* for PVOP addbits == fcode - 1 |
953 |
// returns true or false |
* for BVOP addbits == max(fcode,bcode) - 1 |
954 |
|
* returns true or false |
955 |
|
*/ |
956 |
int |
int |
957 |
check_resync_marker(Bitstream * bs, int addbits) |
check_resync_marker(Bitstream * bs, int addbits) |
958 |
{ |
{ |
1167 |
level = BitstreamGetBits(bs, 8); |
level = BitstreamGetBits(bs, 8); |
1168 |
|
|
1169 |
if (level == 0 || level == 128) |
if (level == 0 || level == 128) |
1170 |
DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level); |
DPRINTF(XVID_DEBUG_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d\n", level); |
1171 |
|
|
1172 |
return (level << 24) >> 24; |
return (level << 24) >> 24; |
1173 |
} |
} |
1223 |
do { |
do { |
1224 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
1225 |
if (run == -1) { |
if (run == -1) { |
1226 |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
DPRINTF(XVID_DEBUG_ERROR,"fatal: invalid run"); |
1227 |
break; |
break; |
1228 |
} |
} |
1229 |
coeff += run; |
coeff += run; |
1230 |
block[scan[coeff]] = level; |
block[scan[coeff]] = level; |
1231 |
|
|
1232 |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i\n", scan[coeff], level); |
1233 |
//DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); |
#if 0 |
1234 |
|
DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i %08x\n", scan[coeff], level, BitstreamShowBits(bs, 32)); |
1235 |
|
#endif |
1236 |
|
|
1237 |
if (level < -2047 || level > 2047) { |
if (level < -2047 || level > 2047) { |
1238 |
DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level); |
DPRINTF(XVID_DEBUG_ERROR,"warning: intra_overflow %i\n", level); |
1239 |
} |
} |
1240 |
coeff++; |
coeff++; |
1241 |
} while (!last); |
} while (!last); |
1258 |
do { |
do { |
1259 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
1260 |
if (run == -1) { |
if (run == -1) { |
1261 |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
DPRINTF(XVID_DEBUG_ERROR,"fatal: invalid run"); |
1262 |
break; |
break; |
1263 |
} |
} |
1264 |
p += run; |
p += run; |
1265 |
|
|
1266 |
block[scan[p]] = level; |
block[scan[p]] = level; |
1267 |
|
|
1268 |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i\n", scan[p], level); |
1269 |
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
/* DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i %08x\n", scan[p], level, BitstreamShowBits(bs, 32)); */ |
1270 |
|
|
1271 |
if (level < -2047 || level > 2047) { |
if (level < -2047 || level > 2047) { |
1272 |
DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level); |
DPRINTF(XVID_DEBUG_ERROR,"warning: inter overflow %i\n", level); |
1273 |
} |
} |
1274 |
p++; |
p++; |
1275 |
} while (!last); |
} while (!last); |
1277 |
} |
} |
1278 |
|
|
1279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************************************ |
|
|
* Trellis based R-D optimal quantization * |
|
|
* not really "bitstream" or "mbcoding" related, but needs VLC tables * |
|
|
* * |
|
|
************************************************************************/ |
|
|
|
|
|
|
|
|
int __inline |
|
|
RunLevel_CalcBits_inter(const int16_t run, int16_t level) |
|
|
{ |
|
|
const int esc_length = 30; |
|
|
|
|
|
if (!((level+32) & -64)) |
|
|
return coeff_VLC[0][0][level+32][run].len; |
|
|
else |
|
|
return esc_length; |
|
|
} |
|
|
|
|
|
int __inline |
|
|
RunLevelLast_CalcBits_inter(const int16_t run, const int16_t level) |
|
|
{ |
|
|
const int esc_length = 30; |
|
|
|
|
|
if (!((level+32) & -64)) |
|
|
return coeff_VLC[0][1][level+32][run].len; |
|
|
else |
|
|
return esc_length; |
|
|
} |
|
|
|
|
|
|
|
|
int __inline |
|
|
RunLevel_CalcBits_intra(const int16_t run, int16_t level) |
|
|
{ |
|
|
const int esc_length = 30; |
|
|
int bits; |
|
|
|
|
|
level = abs(level); |
|
|
if (!(level & -64)) { |
|
|
bits = coeff_VLC[1][0][level][run].len; |
|
|
if (bits!=128) |
|
|
return bits; |
|
|
} |
|
|
return esc_length; |
|
|
} |
|
|
|
|
|
int __inline |
|
|
RunLevelLast_CalcBits_intra(const int16_t run, int16_t level) |
|
|
{ |
|
|
const int esc_length = 30; |
|
|
int bits; |
|
|
|
|
|
level = abs(level); |
|
|
if (!(level & -64)) { |
|
|
bits = coeff_VLC[1][1][level][run].len; |
|
|
if (bits!=128) |
|
|
return bits; |
|
|
} |
|
|
return esc_length; |
|
|
} |
|
|
|
|
|
/* based on ffmpeg's trellis quant, thanks! */ |
|
|
/* (C) 2003 Michael Niedermayer <michaelni@gmx.at> */ |
|
|
|
|
|
int |
|
|
dct_quantize_trellis_inter_h263_c (int16_t *qcoeff, const int16_t *data, int quant) |
|
|
{ |
|
|
|
|
|
/* input: original quantized DCT coefficients (to calc distorion)*/ |
|
|
/* already quantized DCT coefficients */ |
|
|
/* quantizer */ |
|
|
/* output: modified table of quantized DCT coefficients */ |
|
|
|
|
|
/* maybe combining quantize&Trellis would be faster (even that it disables MMX quant) */ |
|
|
|
|
|
int run_tab[65]; |
|
|
int level_tab[65]; |
|
|
int score_tab[65]; |
|
|
int last_run = 0; |
|
|
int last_level = 0; |
|
|
int last_score = 0; |
|
|
int last_i = 0; |
|
|
int coeff[64]; |
|
|
int coeff_count[64]; /* is a table useful for this 0-1 (or 1-2) table? */ |
|
|
int last_non_zero, i; |
|
|
|
|
|
const uint16_t *const zigzag = &scan_tables[0][0]; |
|
|
/* ordinary zigzag order, so it's not INTERLACE compatible, yet */ |
|
|
|
|
|
const int qmul = 2*quant; |
|
|
const int qadd = ((quant-1)|1); |
|
|
|
|
|
/* quant is not needed anymore after this */ |
|
|
|
|
|
int score_limit = 0; |
|
|
int left_limit = 0; |
|
|
|
|
|
const int lambda = (quant * quant * 123 + 64) >> 7; // default lagrangian |
|
|
|
|
|
/* control lambda through a ENVIRONMENT variable (for automatic optmization) */ |
|
|
|
|
|
/* |
|
|
const int lfact=123; // better control of the lagrangian lambda |
|
|
int lambda = (quant * quant * 123 + 64) >> 7; // default lagrangian |
|
|
|
|
|
const char * const trellis_lambda = getenv("TRELLIS_LAMBDA"); |
|
|
if(trellis_lambda) |
|
|
lfact = atoi(trellis_lambda); |
|
|
if (lfact < 1) |
|
|
lfact = 123; // why this value? Who knows? But 123 seems better than 109 = 0.85<<7 |
|
|
|
|
|
lambda = (quant * quant * lfact + 64) >> 7; // lagrangian |
|
|
*/ |
|
|
|
|
|
last_non_zero = -1; |
|
|
for (i = 0; i < 64; i++) |
|
|
{ |
|
|
const int level = qcoeff[zigzag[i]]; |
|
|
|
|
|
if (level) { |
|
|
last_non_zero = i; |
|
|
|
|
|
if (level>0) { |
|
|
if (level==1) { |
|
|
coeff[i] = 1; |
|
|
coeff_count[i] = 0; |
|
|
} else { |
|
|
coeff[i] = level; |
|
|
coeff_count[i] = 1; |
|
|
} |
|
|
} else { |
|
|
if (level==-1) { |
|
|
coeff[i] = -1; |
|
|
coeff_count[i] = 0; |
|
|
} else { |
|
|
coeff[i] = level+1; // because we check coeff[i] and coeff[i]-1 |
|
|
coeff_count[i] = 1; |
|
|
} |
|
|
} |
|
|
} else { |
|
|
coeff[i] = ((data[zigzag[i]]>>31)|1); /* +- 1 because of gap */ |
|
|
coeff_count[i] = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
if (last_non_zero < 0) |
|
|
return last_non_zero; |
|
|
|
|
|
score_tab[0] = 0; |
|
|
|
|
|
for (i = 0; i <= last_non_zero; i++) { |
|
|
int level, run, j; |
|
|
const int dct_coeff = data[zigzag[i]]; |
|
|
const int zero_distortion = dct_coeff * dct_coeff; |
|
|
int best_score = 256 * 256 * 256 * 120; |
|
|
|
|
|
int distortion; |
|
|
int dequant_err; |
|
|
|
|
|
last_score += zero_distortion; |
|
|
|
|
|
|
|
|
/****************** level loop unrolled: first check coeff[i] *********/ |
|
|
level = coeff[i]; |
|
|
|
|
|
if (level > 0) // coeff[i]==0 is not possible here |
|
|
dequant_err = level * qmul + qadd - dct_coeff; |
|
|
else |
|
|
dequant_err = level * qmul - qadd - dct_coeff; |
|
|
|
|
|
distortion = dequant_err*dequant_err; |
|
|
|
|
|
for (run = 0; run <= i - left_limit; run++) { |
|
|
|
|
|
int score = distortion + lambda*RunLevel_CalcBits_inter(run, level) + score_tab[i - run]; |
|
|
|
|
|
if (score < best_score) |
|
|
{ |
|
|
best_score = score_tab[i + 1] = score; |
|
|
run_tab[i + 1] = run; |
|
|
level_tab[i + 1] = level; |
|
|
} |
|
|
} |
|
|
|
|
|
for (run = 0; run <= i - left_limit; run++) { |
|
|
int score = distortion + lambda*RunLevelLast_CalcBits_inter(run, level) + score_tab[i - run]; |
|
|
|
|
|
if (score < last_score) |
|
|
{ |
|
|
last_score = score; |
|
|
last_run = run; |
|
|
last_level = level; |
|
|
last_i = i + 1; |
|
|
} |
|
|
} |
|
|
|
|
|
/****************** level loop unrolled: if possible, check coeff[i]-1 *********/ |
|
|
|
|
|
if (coeff_count[i]) { |
|
|
|
|
|
level--; |
|
|
dequant_err -= qmul; |
|
|
distortion = dequant_err*dequant_err; |
|
|
|
|
|
for (run = 0; run <= i - left_limit; run++) { |
|
|
int score = distortion + lambda*RunLevel_CalcBits_inter(run, level) + score_tab[i-run]; |
|
|
|
|
|
if (score < best_score) |
|
|
{ |
|
|
best_score = score_tab[i + 1] = score; |
|
|
run_tab[i + 1] = run; |
|
|
level_tab[i + 1] = level; |
|
|
} |
|
|
} |
|
|
|
|
|
for (run = 0; run <= i - left_limit; run++) { |
|
|
int score = distortion + lambda*RunLevelLast_CalcBits_inter(run, level) + score_tab[i-run]; |
|
|
|
|
|
if (score < last_score) |
|
|
{ |
|
|
last_score = score; |
|
|
last_run = run; |
|
|
last_level = level; |
|
|
last_i = i + 1; |
|
|
} |
|
|
|
|
|
} |
|
|
} // of check coeff[i]-1 |
|
|
|
|
|
|
|
|
/****************** checking coeff[i]-2 doesn't isn't supported *********/ |
|
|
|
|
|
/****************** add distorsion for higher RUN (-> coeff[i]==0) *******/ |
|
|
for (j = left_limit; j <= i; j++) |
|
|
score_tab[j] += zero_distortion; |
|
|
|
|
|
score_limit += zero_distortion; |
|
|
|
|
|
if (score_tab[i + 1] < score_limit) |
|
|
score_limit = score_tab[i + 1]; |
|
|
|
|
|
// there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level |
|
|
// so we finalize only if we have no chance of getting lower than score_limit + 1*lambda anymore |
|
|
|
|
|
while (score_tab[left_limit] > score_limit + lambda) |
|
|
left_limit++; |
|
|
|
|
|
|
|
|
} // end of (i=0;i<=last_non_zero;i++) |
|
|
|
|
|
|
|
|
last_score = 256 * 256 * 256 * 120; |
|
|
for (i = left_limit; i <= last_non_zero + 1; i++) |
|
|
{ |
|
|
int score = score_tab[i]; |
|
|
if (i) |
|
|
score += 2*lambda; /* coded block means 2 extra bits (roughly) */ |
|
|
|
|
|
if (score < last_score) |
|
|
{ |
|
|
last_score = score; |
|
|
last_i = i; |
|
|
last_level = level_tab[i]; |
|
|
last_run = run_tab[i]; |
|
|
} |
|
|
} |
|
|
|
|
|
last_non_zero = last_i - 1; |
|
|
if (last_non_zero < 0) |
|
|
return last_non_zero; |
|
|
|
|
|
i = last_i; |
|
|
|
|
|
memset(qcoeff,0x00,64*sizeof(int16_t)); |
|
|
|
|
|
qcoeff[zigzag[last_non_zero]] = last_level; |
|
|
i -= last_run + 1; |
|
|
|
|
|
for (; i > 0; i -= run_tab[i] + 1) |
|
|
{ |
|
|
qcoeff[zigzag[i-1]] = level_tab[i]; |
|
|
} |
|
|
|
|
|
return last_non_zero; |
|
|
} |
|
|
|
|
|
int |
|
|
dct_quantize_trellis_inter_mpeg_c (int16_t *qcoeff, const int16_t *data, int quant) |
|
|
{ return 64; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1280 |
/***************************************************************************** |
/***************************************************************************** |
1281 |
* VLC tables and other constant arrays |
* VLC tables and other constant arrays |
1282 |
****************************************************************************/ |
****************************************************************************/ |