19 |
* along with this program ; if not, write to the Free Software |
* along with this program ; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
22 |
* $Id: mbcoding.c,v 1.46 2004-03-22 22:36:23 edgomez Exp $ |
* $Id: mbcoding.c,v 1.51 2005-04-04 23:49:37 edgomez Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
38 |
|
|
39 |
#define LEVELOFFSET 32 |
#define LEVELOFFSET 32 |
40 |
|
|
41 |
|
/* Initialized once during xvid_global call |
42 |
|
* RO access is thread safe */ |
43 |
static REVERSE_EVENT DCT3D[2][4096]; |
static REVERSE_EVENT DCT3D[2][4096]; |
44 |
static VLC coeff_VLC[2][2][64][64]; |
static VLC coeff_VLC[2][2][64][64]; |
45 |
|
|
199 |
static __inline void |
static __inline void |
200 |
CodeVector(Bitstream * bs, |
CodeVector(Bitstream * bs, |
201 |
int32_t value, |
int32_t value, |
202 |
int32_t f_code, |
int32_t f_code) |
|
Statistics * pStat) |
|
203 |
{ |
{ |
204 |
|
|
205 |
const int scale_factor = 1 << (f_code - 1); |
const int scale_factor = 1 << (f_code - 1); |
211 |
if (value > (cmp - 1)) |
if (value > (cmp - 1)) |
212 |
value -= 64 * scale_factor; |
value -= 64 * scale_factor; |
213 |
|
|
|
pStat->iMvSum += value * value; |
|
|
pStat->iMvCount++; |
|
|
|
|
214 |
if (value == 0) { |
if (value == 0) { |
215 |
BitstreamPutBits(bs, mb_motion_table[32].code, |
BitstreamPutBits(bs, mb_motion_table[32].code, |
216 |
mb_motion_table[32].len); |
mb_motion_table[32].len); |
451 |
return bits; |
return bits; |
452 |
} |
} |
453 |
|
|
454 |
static int iDQtab[5] = { |
static const int iDQtab[5] = { |
455 |
1, 0, -1 /* no change */, 2, 3 |
1, 0, -1 /* no change */, 2, 3 |
456 |
}; |
}; |
457 |
#define DQ_VALUE2INDEX(value) iDQtab[(value)+2] |
#define DQ_VALUE2INDEX(value) iDQtab[(value)+2] |
575 |
/* code motion vector(s) if motion is local */ |
/* code motion vector(s) if motion is local */ |
576 |
if (!pMB->mcsel) |
if (!pMB->mcsel) |
577 |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
578 |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode); |
579 |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode); |
580 |
} |
} |
581 |
|
|
582 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
606 |
if (frame->coding_type != I_VOP) |
if (frame->coding_type != I_VOP) |
607 |
BitstreamPutBit(bs, 0); /* not_coded */ |
BitstreamPutBit(bs, 0); /* not_coded */ |
608 |
|
|
609 |
|
if (frame->vop_flags & XVID_VOP_GREYSCALE) { |
610 |
|
pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
611 |
|
qcoeff[4*64+0]=0; /* for INTRA DC value is saved */ |
612 |
|
qcoeff[5*64+0]=0; |
613 |
|
} |
614 |
|
|
615 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
616 |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
617 |
else |
else |
698 |
scan_tables[2] : scan_tables[0]; |
scan_tables[2] : scan_tables[0]; |
699 |
int bits; |
int bits; |
700 |
|
|
|
|
|
701 |
/* ------------------------------------------------------------------ |
/* ------------------------------------------------------------------ |
702 |
when a block is skipped it is decoded DIRECT(0,0) |
when a block is skipped it is decoded DIRECT(0,0) |
703 |
hence is interpolated from forward & backward frames |
hence is interpolated from forward & backward frames |
749 |
|
|
750 |
switch (mb->mode) { |
switch (mb->mode) { |
751 |
case MODE_INTERPOLATE: |
case MODE_INTERPOLATE: |
752 |
CodeVector(bs, mb->pmvs[1].x, vcode, pStat); /* forward vector of interpolate mode */ |
CodeVector(bs, mb->pmvs[1].x, vcode); /* forward vector of interpolate mode */ |
753 |
CodeVector(bs, mb->pmvs[1].y, vcode, pStat); |
CodeVector(bs, mb->pmvs[1].y, vcode); |
754 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
755 |
vcode = bcode; |
vcode = bcode; |
756 |
case MODE_FORWARD: |
case MODE_FORWARD: |
757 |
CodeVector(bs, mb->pmvs[0].x, vcode, pStat); |
CodeVector(bs, mb->pmvs[0].x, vcode); |
758 |
CodeVector(bs, mb->pmvs[0].y, vcode, pStat); |
CodeVector(bs, mb->pmvs[0].y, vcode); |
759 |
break; |
break; |
760 |
case MODE_DIRECT: |
case MODE_DIRECT: |
761 |
CodeVector(bs, mb->pmvs[3].x, 1, pStat); /* fcode is always 1 for delta vector */ |
CodeVector(bs, mb->pmvs[3].x, 1); /* fcode is always 1 for delta vector */ |
762 |
CodeVector(bs, mb->pmvs[3].y, 1, pStat); /* prediction is always (0,0) */ |
CodeVector(bs, mb->pmvs[3].y, 1); /* prediction is always (0,0) */ |
763 |
default: break; |
default: break; |
764 |
} |
} |
765 |
|
|
961 |
|
|
962 |
} |
} |
963 |
|
|
964 |
|
#define GET_BITS(cache, n) ((cache)>>(32-(n))) |
965 |
|
|
966 |
static __inline int |
static __inline int |
967 |
get_coeff(Bitstream * bs, |
get_coeff(Bitstream * bs, |
968 |
int *run, |
int *run, |
975 |
int32_t level; |
int32_t level; |
976 |
REVERSE_EVENT *reverse_event; |
REVERSE_EVENT *reverse_event; |
977 |
|
|
978 |
|
uint32_t cache = BitstreamShowBits(bs, 32); |
979 |
|
|
980 |
if (short_video_header) /* inter-VLCs will be used for both intra and inter blocks */ |
if (short_video_header) /* inter-VLCs will be used for both intra and inter blocks */ |
981 |
intra = 0; |
intra = 0; |
982 |
|
|
983 |
if (BitstreamShowBits(bs, 7) != ESCAPE) { |
if (GET_BITS(cache, 7) != ESCAPE) { |
984 |
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
reverse_event = &DCT3D[intra][GET_BITS(cache, 12)]; |
985 |
|
|
986 |
if ((level = reverse_event->event.level) == 0) |
if ((level = reverse_event->event.level) == 0) |
987 |
goto error; |
goto error; |
989 |
*last = reverse_event->event.last; |
*last = reverse_event->event.last; |
990 |
*run = reverse_event->event.run; |
*run = reverse_event->event.run; |
991 |
|
|
992 |
BitstreamSkip(bs, reverse_event->len); |
/* Don't forget to update the bitstream position */ |
993 |
|
BitstreamSkip(bs, reverse_event->len+1); |
994 |
|
|
995 |
return BitstreamGetBits(bs, 1) ? -level : level; |
return (GET_BITS(cache, reverse_event->len+1)&0x01) ? -level : level; |
996 |
} |
} |
997 |
|
|
998 |
BitstreamSkip(bs, 7); |
/* flush 7bits of cache */ |
999 |
|
cache <<= 7; |
1000 |
|
|
1001 |
if (short_video_header) { |
if (short_video_header) { |
1002 |
/* escape mode 4 - H.263 type, only used if short_video_header = 1 */ |
/* escape mode 4 - H.263 type, only used if short_video_header = 1 */ |
1003 |
*last = BitstreamGetBit(bs); |
*last = GET_BITS(cache, 1); |
1004 |
*run = BitstreamGetBits(bs, 6); |
*run = (GET_BITS(cache, 7) &0x3f); |
1005 |
level = BitstreamGetBits(bs, 8); |
level = (GET_BITS(cache, 15)&0xff); |
1006 |
|
|
1007 |
if (level == 0 || level == 128) |
if (level == 0 || level == 128) |
1008 |
DPRINTF(XVID_DEBUG_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d\n", level); |
DPRINTF(XVID_DEBUG_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d\n", level); |
1009 |
|
|
1010 |
|
/* We've "eaten" 22 bits */ |
1011 |
|
BitstreamSkip(bs, 22); |
1012 |
|
|
1013 |
return (level << 24) >> 24; |
return (level << 24) >> 24; |
1014 |
} |
} |
1015 |
|
|
1016 |
mode = BitstreamShowBits(bs, 2); |
if ((mode = GET_BITS(cache, 2)) < 3) { |
1017 |
|
const int skip[3] = {1, 1, 2}; |
1018 |
if (mode < 3) { |
cache <<= skip[mode]; |
|
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
|
1019 |
|
|
1020 |
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
reverse_event = &DCT3D[intra][GET_BITS(cache, 12)]; |
1021 |
|
|
1022 |
if ((level = reverse_event->event.level) == 0) |
if ((level = reverse_event->event.level) == 0) |
1023 |
goto error; |
goto error; |
1025 |
*last = reverse_event->event.last; |
*last = reverse_event->event.last; |
1026 |
*run = reverse_event->event.run; |
*run = reverse_event->event.run; |
1027 |
|
|
1028 |
BitstreamSkip(bs, reverse_event->len); |
if (mode < 2) { |
1029 |
|
/* first escape mode, level is offset */ |
|
if (mode < 2) /* first escape mode, level is offset */ |
|
1030 |
level += max_level[intra][*last][*run]; |
level += max_level[intra][*last][*run]; |
1031 |
else /* second escape mode, run is offset */ |
} else { |
1032 |
|
/* second escape mode, run is offset */ |
1033 |
*run += max_run[intra][*last][level] + 1; |
*run += max_run[intra][*last][level] + 1; |
1034 |
|
} |
1035 |
|
|
1036 |
return BitstreamGetBits(bs, 1) ? -level : level; |
/* Update bitstream position */ |
1037 |
|
BitstreamSkip(bs, 7 + skip[mode] + reverse_event->len + 1); |
1038 |
|
|
1039 |
|
return (GET_BITS(cache, reverse_event->len+1)&0x01) ? -level : level; |
1040 |
} |
} |
1041 |
|
|
1042 |
/* third escape mode - fixed length codes */ |
/* third escape mode - fixed length codes */ |
1043 |
BitstreamSkip(bs, 2); |
cache <<= 2; |
1044 |
*last = BitstreamGetBits(bs, 1); |
*last = GET_BITS(cache, 1); |
1045 |
*run = BitstreamGetBits(bs, 6); |
*run = (GET_BITS(cache, 7)&0x3f); |
1046 |
BitstreamSkip(bs, 1); /* marker */ |
level = (GET_BITS(cache, 20)&0xfff); |
1047 |
level = BitstreamGetBits(bs, 12); |
|
1048 |
BitstreamSkip(bs, 1); /* marker */ |
/* Update bitstream position */ |
1049 |
|
BitstreamSkip(bs, 30); |
1050 |
|
|
1051 |
return (level << 20) >> 20; |
return (level << 20) >> 20; |
1052 |
|
|
1088 |
} |
} |
1089 |
|
|
1090 |
void |
void |
1091 |
get_inter_block(Bitstream * bs, |
get_inter_block_h263( |
1092 |
|
Bitstream * bs, |
1093 |
int16_t * block, |
int16_t * block, |
1094 |
int direction) |
int direction, |
1095 |
|
const int quant, |
1096 |
|
const uint16_t *matrix) |
1097 |
{ |
{ |
1098 |
|
|
1099 |
const uint16_t *scan = scan_tables[direction]; |
const uint16_t *scan = scan_tables[direction]; |
1100 |
|
const uint16_t quant_m_2 = quant << 1; |
1101 |
|
const uint16_t quant_add = (quant & 1 ? quant : quant - 1); |
1102 |
int p; |
int p; |
1103 |
int level; |
int level; |
1104 |
int run; |
int run; |
1113 |
} |
} |
1114 |
p += run; |
p += run; |
1115 |
|
|
1116 |
block[scan[p]] = level; |
if (level < 0) { |
1117 |
|
level = level*quant_m_2 - quant_add; |
1118 |
|
block[scan[p]] = (level >= -2048 ? level : -2048); |
1119 |
|
} else { |
1120 |
|
level = level * quant_m_2 + quant_add; |
1121 |
|
block[scan[p]] = (level <= 2047 ? level : 2047); |
1122 |
|
} |
1123 |
|
p++; |
1124 |
|
} while (!last); |
1125 |
|
} |
1126 |
|
|
1127 |
DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i\n", scan[p], level); |
void |
1128 |
/* DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i %08x\n", scan[p], level, BitstreamShowBits(bs, 32)); */ |
get_inter_block_mpeg( |
1129 |
|
Bitstream * bs, |
1130 |
|
int16_t * block, |
1131 |
|
int direction, |
1132 |
|
const int quant, |
1133 |
|
const uint16_t *matrix) |
1134 |
|
{ |
1135 |
|
const uint16_t *scan = scan_tables[direction]; |
1136 |
|
uint32_t sum = 0; |
1137 |
|
int p; |
1138 |
|
int level; |
1139 |
|
int run; |
1140 |
|
int last; |
1141 |
|
|
1142 |
if (level < -2047 || level > 2047) { |
p = 0; |
1143 |
DPRINTF(XVID_DEBUG_ERROR,"warning: inter overflow %i\n", level); |
do { |
1144 |
|
level = get_coeff(bs, &run, &last, 0, 0); |
1145 |
|
if (run == -1) { |
1146 |
|
DPRINTF(XVID_DEBUG_ERROR,"fatal: invalid run"); |
1147 |
|
break; |
1148 |
} |
} |
1149 |
|
p += run; |
1150 |
|
|
1151 |
|
if (level < 0) { |
1152 |
|
level = ((2 * -level + 1) * matrix[scan[p]] * quant) >> 4; |
1153 |
|
block[scan[p]] = (level <= 2048 ? -level : -2048); |
1154 |
|
} else { |
1155 |
|
level = ((2 * level + 1) * matrix[scan[p]] * quant) >> 4; |
1156 |
|
block[scan[p]] = (level <= 2047 ? level : 2047); |
1157 |
|
} |
1158 |
|
|
1159 |
|
sum ^= block[scan[p]]; |
1160 |
|
|
1161 |
p++; |
p++; |
1162 |
} while (!last); |
} while (!last); |
1163 |
|
|
1164 |
|
/* mismatch control */ |
1165 |
|
if ((sum & 1) == 0) { |
1166 |
|
block[63] ^= 1; |
1167 |
|
} |
1168 |
} |
} |
1169 |
|
|
1170 |
|
|
1944 |
}; |
}; |
1945 |
|
|
1946 |
short const dc_threshold[] = { |
short const dc_threshold[] = { |
1947 |
21514, 26984, 8307, 28531, 29798, 24951, 25970, 26912, |
26708, 29545, 29472, 26223, 30580, 29281, 8293, 29545, |
1948 |
8307, 25956, 26994, 25974, 8292, 29286, 28015, 29728, |
25632, 29285, 30313, 25701, 26144, 28530, 8301, 26740, |
1949 |
25960, 18208, 21838, 18208, 19536, 22560, 26998, 8260, |
8293, 20039, 8277, 20551, 8268, 30296, 17513, 25376, |
1950 |
28515, 25956, 8291, 25640, 30309, 27749, 11817, 22794, |
25711, 25445, 10272, 11825, 11825, 10544, 2606, 28505, |
1951 |
30063, 8306, 28531, 29798, 24951, 25970, 25632, 29545, |
29301, 29472, 26223, 30580, 29281, 8293, 26980, 29811, |
1952 |
29300, 25193, 29813, 29295, 26656, 29537, 29728, 8303, |
26994, 30050, 28532, 8306, 24936, 8307, 28532, 26400, |
1953 |
26983, 25974, 24864, 25443, 29541, 8307, 28532, 26912, |
30313, 8293, 25441, 25955, 29555, 29728, 8303, 29801, |
1954 |
29556, 29472, 30063, 25458, 8293, 28515, 25956, 2606 |
8307, 28531, 29301, 25955, 25376, 25711, 11877, 10 |
1955 |
}; |
}; |
1956 |
|
|
1957 |
VLC const dc_lum_tab[] = { |
VLC const dc_lum_tab[] = { |