51 |
|
|
52 |
#include <stdio.h> |
#include <stdio.h> |
53 |
#include <stdlib.h> |
#include <stdlib.h> |
54 |
|
#include <string.h> |
55 |
|
|
56 |
#include "../portab.h" |
#include "../portab.h" |
57 |
#include "../global.h" |
#include "../global.h" |
58 |
#include "bitstream.h" |
#include "bitstream.h" |
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); |
502 |
{ |
{ |
503 |
if ((level = qcoeff[zigzag[i++]]) != 0) |
if ((level = qcoeff[zigzag[i++]]) != 0) |
504 |
{ |
{ |
505 |
abs_level = ABS(prev_level); |
abs_level = abs(prev_level); |
506 |
abs_level = abs_level < 64 ? abs_level : 0; |
abs_level = abs_level < 64 ? abs_level : 0; |
507 |
code = coeff_VLC[1][0][abs_level][prev_run].code; |
code = coeff_VLC[1][0][abs_level][prev_run].code; |
508 |
len = coeff_VLC[1][0][abs_level][prev_run].len; |
len = coeff_VLC[1][0][abs_level][prev_run].len; |
522 |
run++; |
run++; |
523 |
} |
} |
524 |
|
|
525 |
abs_level = ABS(prev_level); |
abs_level = abs(prev_level); |
526 |
abs_level = abs_level < 64 ? abs_level : 0; |
abs_level = abs_level < 64 ? abs_level : 0; |
527 |
code = coeff_VLC[1][1][abs_level][prev_run].code; |
code = coeff_VLC[1][1][abs_level][prev_run].code; |
528 |
len = coeff_VLC[1][1][abs_level][prev_run].len; |
len = coeff_VLC[1][1][abs_level][prev_run].len; |
563 |
{ |
{ |
564 |
if ((level = qcoeff[zigzag[i++]]) != 0) |
if ((level = qcoeff[zigzag[i++]]) != 0) |
565 |
{ |
{ |
566 |
abs_level = ABS(prev_level); |
abs_level = abs(prev_level); |
567 |
abs_level = abs_level < 64 ? abs_level : 0; |
abs_level = abs_level < 64 ? abs_level : 0; |
568 |
len = coeff_VLC[1][0][abs_level][prev_run].len; |
len = coeff_VLC[1][0][abs_level][prev_run].len; |
569 |
bits += len!=128 ? len : 30; |
bits += len!=128 ? len : 30; |
576 |
run++; |
run++; |
577 |
} |
} |
578 |
|
|
579 |
abs_level = ABS(prev_level); |
abs_level = abs(prev_level); |
580 |
abs_level = abs_level < 64 ? abs_level : 0; |
abs_level = abs_level < 64 ? abs_level : 0; |
581 |
len = coeff_VLC[1][1][abs_level][prev_run].len; |
len = coeff_VLC[1][1][abs_level][prev_run].len; |
582 |
bits += len!=128 ? len : 30; |
bits += len!=128 ? len : 30; |
631 |
|
|
632 |
#endif |
#endif |
633 |
|
|
634 |
|
|
635 |
|
static int iDQtab[5] = { |
636 |
|
1, 0, -1 /* no change */, 2, 3 |
637 |
|
}; |
638 |
|
#define DQ_VALUE2INDEX(value) iDQtab[(value)+2] |
639 |
|
|
640 |
|
|
641 |
static __inline void |
static __inline void |
642 |
CodeBlockIntra(const FRAMEINFO * const frame, |
CodeBlockIntra(const FRAMEINFO * const frame, |
643 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
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, cbpy_tab[cbpy].code, 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, pMB->dquant, 2); |
BitstreamPutBits(bs, DQ_VALUE2INDEX(pMB->dquant), 2); |
676 |
|
|
677 |
// write interlacing |
/* write interlacing */ |
678 |
if (frame->vol_flags & XVID_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, |
689 |
|
|
690 |
if (pMB->cbp & (1 << (5 - i))) { |
if (pMB->cbp & (1 << (5 - i))) { |
691 |
const uint16_t *scan_table = |
const uint16_t *scan_table = |
692 |
frame->vop_flags & XVID_ALTERNATESCAN ? |
frame->vop_flags & XVID_VOP_ALTERNATESCAN ? |
693 |
scan_tables[2] : scan_tables[pMB->acpred_directions[i]]; |
scan_tables[2] : scan_tables[pMB->acpred_directions[i]]; |
694 |
|
|
695 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
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, cbpy_tab[cbpy].code, 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, pMB->dquant, 2); |
BitstreamPutBits(bs, DQ_VALUE2INDEX(pMB->dquant), 2); |
738 |
|
|
739 |
// interlacing |
/* interlacing */ |
740 |
if (frame->vol_flags & XVID_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 |
{ |
{ |
771 |
const uint16_t *scan_table = |
const uint16_t *scan_table = |
772 |
frame->vop_flags & XVID_ALTERNATESCAN ? |
frame->vop_flags & XVID_VOP_ALTERNATESCAN ? |
773 |
scan_tables[2] : scan_tables[0]; |
scan_tables[2] : scan_tables[0]; |
774 |
|
|
775 |
#ifdef BIGLUT |
#ifdef BIGLUT |
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 |
{ |
{ |
1066 |
return data; |
return data; |
1067 |
|
|
1068 |
res = BitstreamGetBits(bs, fcode - 1); |
res = BitstreamGetBits(bs, fcode - 1); |
1069 |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
mv = ((abs(data) - 1) * scale_fac) + res + 1; |
1070 |
|
|
1071 |
return data < 0 ? -mv : mv; |
return data < 0 ? -mv : mv; |
1072 |
|
|
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); |
1276 |
|
|
1277 |
} |
} |
1278 |
|
|
1279 |
|
|
1280 |
/***************************************************************************** |
/***************************************************************************** |
1281 |
* VLC tables and other constant arrays |
* VLC tables and other constant arrays |
1282 |
****************************************************************************/ |
****************************************************************************/ |
1635 |
{5, 6}, {5, 9}, {5, 8}, {3, 7}, {2, 9} |
{5, 6}, {5, 9}, {5, 8}, {3, 7}, {2, 9} |
1636 |
}; |
}; |
1637 |
|
|
1638 |
const VLC cbpy_tab[16] = { |
const VLC xvid_cbpy_tab[16] = { |
1639 |
{3, 4}, {5, 5}, {4, 5}, {9, 4}, {3, 5}, {7, 4}, {2, 6}, {11, 4}, |
{3, 4}, {5, 5}, {4, 5}, {9, 4}, {3, 5}, {7, 4}, {2, 6}, {11, 4}, |
1640 |
{2, 5}, {3, 6}, {5, 4}, {10, 4}, {4, 4}, {8, 4}, {6, 4}, {3, 2} |
{2, 5}, {3, 6}, {5, 4}, {10, 4}, {4, 4}, {8, 4}, {6, 4}, {3, 2} |
1641 |
}; |
}; |