36 |
|
|
37 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
38 |
|
|
|
/* #define BIGLUT */ |
|
|
|
|
|
#ifdef BIGLUT |
|
|
#define LEVELOFFSET 2048 |
|
|
#else |
|
39 |
#define LEVELOFFSET 32 |
#define LEVELOFFSET 32 |
|
#endif |
|
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]; |
|
|
|
|
#ifdef BIGLUT |
|
|
static VLC coeff_VLC[2][2][4096][64]; |
|
|
VLC *intra_table; |
|
|
static VLC *inter_table; |
|
|
#else |
|
44 |
static VLC coeff_VLC[2][2][64][64]; |
static VLC coeff_VLC[2][2][64][64]; |
|
#endif |
|
45 |
|
|
46 |
/* not really MB related, but VLCs are only available here */ |
/* not really MB related, but VLCs are only available here */ |
47 |
void bs_put_spritetrajectory(Bitstream * bs, const int val) |
void bs_put_spritetrajectory(Bitstream * bs, const int val) |
80 |
uint32_t i, j, k, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; |
uint32_t i, j, k, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; |
81 |
int32_t l; |
int32_t l; |
82 |
|
|
|
#ifdef BIGLUT |
|
|
intra_table = coeff_VLC[1]; |
|
|
inter_table = coeff_VLC[0]; |
|
|
#endif |
|
|
|
|
|
|
|
83 |
for (intra = 0; intra < 2; intra++) |
for (intra = 0; intra < 2; intra++) |
84 |
for (i = 0; i < 4096; i++) |
for (i = 0; i < 4096; i++) |
85 |
DCT3D[intra][i].event.level = 0; |
DCT3D[intra][i].event.level = 0; |
86 |
|
|
87 |
for (intra = 0; intra < 2; intra++) |
for (intra = 0; intra < 2; intra++) { |
88 |
for (last = 0; last < 2; last++) |
for (last = 0; last < 2; last++) { |
89 |
{ |
for (run = 0; run < 63 + last; run++) { |
90 |
for (run = 0; run < 63 + last; run++) |
for (level = 0; level < (uint32_t)(32 << intra); level++) { |
|
for (level = 0; level < (uint32_t)(32 << intra); level++) |
|
|
{ |
|
|
#ifdef BIGLUT |
|
|
offset = LEVELOFFSET; |
|
|
#else |
|
91 |
offset = !intra * LEVELOFFSET; |
offset = !intra * LEVELOFFSET; |
|
#endif |
|
92 |
coeff_VLC[intra][last][level + offset][run].len = 128; |
coeff_VLC[intra][last][level + offset][run].len = 128; |
93 |
} |
} |
94 |
} |
} |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
for (intra = 0; intra < 2; intra++) |
for (intra = 0; intra < 2; intra++) { |
99 |
for (i = 0; i < 102; i++) |
for (i = 0; i < 102; i++) { |
|
{ |
|
|
#ifdef BIGLUT |
|
|
offset = LEVELOFFSET; |
|
|
#else |
|
100 |
offset = !intra * LEVELOFFSET; |
offset = !intra * LEVELOFFSET; |
101 |
#endif |
|
102 |
for (j = 0; j < (uint32_t)(1 << (12 - coeff_tab[intra][i].vlc.len)); j++) |
for (j = 0; j < (uint32_t)(1 << (12 - coeff_tab[intra][i].vlc.len)); j++) { |
|
{ |
|
103 |
DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len = coeff_tab[intra][i].vlc.len; |
DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len = coeff_tab[intra][i].vlc.len; |
104 |
DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event; |
DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event; |
105 |
} |
} |
108 |
= coeff_tab[intra][i].vlc.code << 1; |
= coeff_tab[intra][i].vlc.code << 1; |
109 |
coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].len |
coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].len |
110 |
= coeff_tab[intra][i].vlc.len + 1; |
= coeff_tab[intra][i].vlc.len + 1; |
111 |
#ifndef BIGLUT |
|
112 |
if (!intra) |
if (!intra) { |
|
#endif |
|
|
{ |
|
113 |
coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code |
coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code |
114 |
= (coeff_tab[intra][i].vlc.code << 1) | 1; |
= (coeff_tab[intra][i].vlc.code << 1) | 1; |
115 |
coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len |
coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len |
116 |
= coeff_tab[intra][i].vlc.len + 1; |
= coeff_tab[intra][i].vlc.len + 1; |
117 |
} |
} |
118 |
} |
} |
119 |
|
} |
120 |
|
|
121 |
|
for (intra = 0; intra < 2; intra++) { |
122 |
|
for (last = 0; last < 2; last++) { |
123 |
|
for (run = 0; run < 63 + last; run++) { |
124 |
|
for (level = 1; level < (uint32_t)(32 << intra); level++) { |
125 |
|
|
|
for (intra = 0; intra < 2; intra++) |
|
|
for (last = 0; last < 2; last++) |
|
|
for (run = 0; run < 63 + last; run++) |
|
|
{ |
|
|
for (level = 1; level < (uint32_t)(32 << intra); level++) |
|
|
{ |
|
126 |
if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level]) |
if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level]) |
127 |
continue; |
continue; |
128 |
|
|
|
#ifdef BIGLUT |
|
|
offset = LEVELOFFSET; |
|
|
#else |
|
129 |
offset = !intra * LEVELOFFSET; |
offset = !intra * LEVELOFFSET; |
|
#endif |
|
130 |
level_esc = level - max_level[intra][last][run]; |
level_esc = level - max_level[intra][last][run]; |
131 |
run_esc = run - 1 - max_run[intra][last][level]; |
run_esc = run - 1 - max_run[intra][last][level]; |
|
/*use this test to use shorter esc2 codes when possible |
|
|
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc] |
|
|
&& !(coeff_VLC[intra][last][level_esc + offset][run].len + 7 + 1 |
|
|
> coeff_VLC[intra][last][level + offset][run_esc].code + 7 + 2))*/ |
|
132 |
|
|
133 |
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]) |
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]) { |
|
{ |
|
134 |
escape = ESCAPE1; |
escape = ESCAPE1; |
135 |
escape_len = 7 + 1; |
escape_len = 7 + 1; |
136 |
run_esc = run; |
run_esc = run; |
137 |
} |
} else { |
138 |
else |
if (run_esc <= max_run[intra][last][level] && level <= max_level[intra][last][run_esc]) { |
|
{ |
|
|
if (run_esc <= max_run[intra][last][level] && level <= max_level[intra][last][run_esc]) |
|
|
{ |
|
139 |
escape = ESCAPE2; |
escape = ESCAPE2; |
140 |
escape_len = 7 + 2; |
escape_len = 7 + 2; |
141 |
level_esc = level; |
level_esc = level; |
142 |
} |
} else { |
143 |
else |
if (!intra) { |
|
{ |
|
|
#ifndef BIGLUT |
|
|
if (!intra) |
|
|
#endif |
|
|
{ |
|
144 |
coeff_VLC[intra][last][level + offset][run].code |
coeff_VLC[intra][last][level + offset][run].code |
145 |
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
146 |
coeff_VLC[intra][last][level + offset][run].len = 30; |
coeff_VLC[intra][last][level + offset][run].len = 30; |
157 |
| coeff_VLC[intra][last][level_esc + offset][run_esc].code; |
| coeff_VLC[intra][last][level_esc + offset][run_esc].code; |
158 |
coeff_VLC[intra][last][level + offset][run].len |
coeff_VLC[intra][last][level + offset][run].len |
159 |
= coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len; |
= coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len; |
160 |
#ifndef BIGLUT |
|
161 |
if (!intra) |
if (!intra) { |
|
#endif |
|
|
{ |
|
162 |
coeff_VLC[intra][last][offset - level][run].code |
coeff_VLC[intra][last][offset - level][run].code |
163 |
= (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len) |
= (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len) |
164 |
| coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1; |
| coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1; |
167 |
} |
} |
168 |
} |
} |
169 |
|
|
170 |
#ifdef BIGLUT |
if (!intra) { |
|
for (level = 32 << intra; level < 2048; level++) |
|
|
{ |
|
|
coeff_VLC[intra][last][level + offset][run].code |
|
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
|
|
coeff_VLC[intra][last][level + offset][run].len = 30; |
|
|
|
|
|
coeff_VLC[intra][last][offset - level][run].code |
|
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1; |
|
|
coeff_VLC[intra][last][offset - level][run].len = 30; |
|
|
} |
|
|
#else |
|
|
if (!intra) |
|
|
{ |
|
171 |
coeff_VLC[intra][last][0][run].code |
coeff_VLC[intra][last][0][run].code |
172 |
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1; |
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1; |
173 |
coeff_VLC[intra][last][0][run].len = 30; |
coeff_VLC[intra][last][0][run].len = 30; |
174 |
} |
} |
|
#endif |
|
175 |
} |
} |
176 |
/* init sprite_trajectory tables */ |
} |
177 |
/* even if GMC is not specified (it might be used later...) */ |
} |
178 |
|
|
179 |
|
/* init sprite_trajectory tables |
180 |
|
* even if GMC is not specified (it might be used later...) */ |
181 |
|
|
182 |
sprite_trajectory_code[0+16384].code = 0; |
sprite_trajectory_code[0+16384].code = 0; |
183 |
sprite_trajectory_code[0+16384].len = 0; |
sprite_trajectory_code[0+16384].len = 0; |
184 |
for (k=0;k<14;k++) |
for (k=0;k<14;k++) { |
|
{ |
|
185 |
int limit = (1<<k); |
int limit = (1<<k); |
186 |
|
|
187 |
for (l=-(2*limit-1); l <= -limit; l++) |
for (l=-(2*limit-1); l <= -limit; l++) { |
|
{ |
|
188 |
sprite_trajectory_code[l+16384].code = (2*limit-1)+l; |
sprite_trajectory_code[l+16384].code = (2*limit-1)+l; |
189 |
sprite_trajectory_code[l+16384].len = k+1; |
sprite_trajectory_code[l+16384].len = k+1; |
190 |
} |
} |
191 |
|
|
192 |
for (l=limit; l<= 2*limit-1; l++) |
for (l=limit; l<= 2*limit-1; l++) { |
|
{ |
|
193 |
sprite_trajectory_code[l+16384].code = l; |
sprite_trajectory_code[l+16384].code = l; |
194 |
sprite_trajectory_code[l+16384].len = k+1; |
sprite_trajectory_code[l+16384].len = k+1; |
195 |
} |
} |
251 |
|
|
252 |
} |
} |
253 |
|
|
|
#ifdef BIGLUT |
|
|
|
|
|
static __inline void |
|
|
CodeCoeff(Bitstream * bs, |
|
|
const int16_t qcoeff[64], |
|
|
VLC * table, |
|
|
const uint16_t * zigzag, |
|
|
uint16_t intra) |
|
|
{ |
|
|
|
|
|
uint32_t j, last; |
|
|
short v; |
|
|
VLC *vlc; |
|
|
|
|
|
j = intra; |
|
|
last = intra; |
|
|
|
|
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
|
|
j++; |
|
|
|
|
|
do { |
|
|
vlc = table + 64 * 2048 + (v << 6) + j - last; |
|
|
last = ++j; |
|
|
|
|
|
/* count zeroes */ |
|
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
|
|
j++; |
|
|
|
|
|
/* write code */ |
|
|
if (j != 64) { |
|
|
BitstreamPutBits(bs, vlc->code, vlc->len); |
|
|
} else { |
|
|
vlc += 64 * 4096; |
|
|
BitstreamPutBits(bs, vlc->code, vlc->len); |
|
|
break; |
|
|
} |
|
|
} while (1); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* returns the number of bits required to encode qcoeff */ |
|
|
int |
|
|
CodeCoeff_CalcBits(const int16_t qcoeff[64], |
|
|
VLC * table, |
|
|
const uint16_t * zigzag, |
|
|
uint16_t intra) |
|
|
{ |
|
|
int bits = 0; |
|
|
uint32_t j, last; |
|
|
short v; |
|
|
VLC *vlc; |
|
|
|
|
|
j = intra; |
|
|
last = intra; |
|
|
|
|
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
|
|
j++; |
|
|
|
|
|
if (j >= 64) return 0; /* empty block */ |
|
|
|
|
|
do { |
|
|
vlc = table + 64 * 2048 + (v << 6) + j - last; |
|
|
last = ++j; |
|
|
|
|
|
/* count zeroes */ |
|
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
|
|
j++; |
|
|
|
|
|
/* write code */ |
|
|
if (j != 64) { |
|
|
bits += vlc->len; |
|
|
} else { |
|
|
vlc += 64 * 4096; |
|
|
bits += vlc->len; |
|
|
break; |
|
|
} |
|
|
} while (1); |
|
|
|
|
|
return bits; |
|
|
} |
|
|
|
|
|
|
|
|
#else |
|
|
|
|
254 |
static __inline void |
static __inline void |
255 |
CodeCoeffInter(Bitstream * bs, |
CodeCoeffInter(Bitstream * bs, |
256 |
const int16_t qcoeff[64], |
const int16_t qcoeff[64], |
455 |
return bits; |
return bits; |
456 |
} |
} |
457 |
|
|
458 |
|
static const int iDQtab[5] = { |
|
#endif |
|
|
|
|
|
|
|
|
static int iDQtab[5] = { |
|
459 |
1, 0, -1 /* no change */, 2, 3 |
1, 0, -1 /* no change */, 2, 3 |
460 |
}; |
}; |
461 |
#define DQ_VALUE2INDEX(value) iDQtab[(value)+2] |
#define DQ_VALUE2INDEX(value) iDQtab[(value)+2] |
517 |
|
|
518 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
519 |
|
|
|
#ifdef BIGLUT |
|
|
CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1); |
|
|
#else |
|
520 |
CodeCoeffIntra(bs, &qcoeff[i * 64], scan_table); |
CodeCoeffIntra(bs, &qcoeff[i * 64], scan_table); |
|
#endif |
|
521 |
|
|
522 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
523 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
563 |
} |
} |
564 |
|
|
565 |
/* if inter block, write field ME flag */ |
/* if inter block, write field ME flag */ |
566 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) && (pMB->mcsel == 0)) { |
567 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, 0 /*pMB->field_pred*/); /* not implemented yet */ |
568 |
DPRINTF(XVID_DEBUG_MB,"codep: field_pred: %i\n", pMB->field_pred); |
DPRINTF(XVID_DEBUG_MB,"codep: field_pred: %i\n", pMB->field_pred); |
569 |
|
|
570 |
/* write field prediction references */ |
/* write field prediction references */ |
571 |
|
#if 0 /* Remove the #if once field_pred is supported */ |
572 |
if (pMB->field_pred) { |
if (pMB->field_pred) { |
573 |
BitstreamPutBit(bs, pMB->field_for_top); |
BitstreamPutBit(bs, pMB->field_for_top); |
574 |
BitstreamPutBit(bs, pMB->field_for_bot); |
BitstreamPutBit(bs, pMB->field_for_bot); |
575 |
} |
} |
576 |
|
#endif |
577 |
} |
} |
578 |
} |
} |
579 |
/* code motion vector(s) if motion is local */ |
/* code motion vector(s) if motion is local */ |
587 |
|
|
588 |
/* code block coeffs */ |
/* code block coeffs */ |
589 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) |
590 |
if (pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) { |
|
{ |
|
591 |
const uint16_t *scan_table = |
const uint16_t *scan_table = |
592 |
frame->vop_flags & XVID_VOP_ALTERNATESCAN ? |
frame->vop_flags & XVID_VOP_ALTERNATESCAN ? |
593 |
scan_tables[2] : scan_tables[0]; |
scan_tables[2] : scan_tables[0]; |
594 |
|
|
|
#ifdef BIGLUT |
|
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0); |
|
|
#else |
|
595 |
CodeCoeffInter(bs, &qcoeff[i * 64], scan_table); |
CodeCoeffInter(bs, &qcoeff[i * 64], scan_table); |
|
#endif |
|
596 |
} |
} |
597 |
|
|
598 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
694 |
const uint16_t *scan_table = |
const uint16_t *scan_table = |
695 |
frame->vop_flags & XVID_VOP_ALTERNATESCAN ? |
frame->vop_flags & XVID_VOP_ALTERNATESCAN ? |
696 |
scan_tables[2] : scan_tables[0]; |
scan_tables[2] : scan_tables[0]; |
697 |
|
int bits; |
698 |
|
|
699 |
|
|
700 |
/* ------------------------------------------------------------------ |
/* ------------------------------------------------------------------ |
736 |
BitstreamPutBit(bs, 0 /*mb->field_pred*/); /* field ME not implemented */ |
BitstreamPutBit(bs, 0 /*mb->field_pred*/); /* field ME not implemented */ |
737 |
|
|
738 |
/* write field prediction references */ |
/* write field prediction references */ |
739 |
/* if (mb->field_pred) { |
#if 0 /* Remove the #if once field_pred is supported */ |
740 |
|
if (mb->field_pred) { |
741 |
BitstreamPutBit(bs, mb->field_for_top); |
BitstreamPutBit(bs, mb->field_for_top); |
742 |
BitstreamPutBit(bs, mb->field_for_bot); |
BitstreamPutBit(bs, mb->field_for_bot); |
743 |
}*/ |
} |
744 |
|
#endif |
745 |
} |
} |
746 |
} |
} |
747 |
|
|
762 |
default: break; |
default: break; |
763 |
} |
} |
764 |
|
|
765 |
|
bits = BitstreamPos(bs); |
766 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
767 |
if (mb->cbp & (1 << (5 - i))) { |
if (mb->cbp & (1 << (5 - i))) { |
768 |
#ifdef BIGLUT |
CodeCoeffInter(bs, &qcoeff[i * 64], scan_table); |
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
|
|
#else |
|
|
CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]); |
|
|
#endif |
|
769 |
} |
} |
770 |
} |
} |
771 |
|
pStat->iTextBits += BitstreamPos(bs) - bits; |
772 |
} |
} |
773 |
|
|
774 |
|
|
960 |
|
|
961 |
} |
} |
962 |
|
|
963 |
|
#define GET_BITS(cache, n) ((cache)>>(32-(n))) |
964 |
|
|
965 |
static __inline int |
static __inline int |
966 |
get_coeff(Bitstream * bs, |
get_coeff(Bitstream * bs, |
967 |
int *run, |
int *run, |
974 |
int32_t level; |
int32_t level; |
975 |
REVERSE_EVENT *reverse_event; |
REVERSE_EVENT *reverse_event; |
976 |
|
|
977 |
|
uint32_t cache = BitstreamShowBits(bs, 32); |
978 |
|
|
979 |
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 */ |
980 |
intra = 0; |
intra = 0; |
981 |
|
|
982 |
if (BitstreamShowBits(bs, 7) != ESCAPE) { |
if (GET_BITS(cache, 7) != ESCAPE) { |
983 |
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
reverse_event = &DCT3D[intra][GET_BITS(cache, 12)]; |
984 |
|
|
985 |
if ((level = reverse_event->event.level) == 0) |
if ((level = reverse_event->event.level) == 0) |
986 |
goto error; |
goto error; |
988 |
*last = reverse_event->event.last; |
*last = reverse_event->event.last; |
989 |
*run = reverse_event->event.run; |
*run = reverse_event->event.run; |
990 |
|
|
991 |
BitstreamSkip(bs, reverse_event->len); |
/* Don't forget to update the bitstream position */ |
992 |
|
BitstreamSkip(bs, reverse_event->len+1); |
993 |
|
|
994 |
return BitstreamGetBits(bs, 1) ? -level : level; |
return (GET_BITS(cache, reverse_event->len+1)&0x01) ? -level : level; |
995 |
} |
} |
996 |
|
|
997 |
BitstreamSkip(bs, 7); |
/* flush 7bits of cache */ |
998 |
|
cache <<= 7; |
999 |
|
|
1000 |
if (short_video_header) { |
if (short_video_header) { |
1001 |
/* 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 */ |
1002 |
*last = BitstreamGetBit(bs); |
*last = GET_BITS(cache, 1); |
1003 |
*run = BitstreamGetBits(bs, 6); |
*run = (GET_BITS(cache, 7) &0x3f); |
1004 |
level = BitstreamGetBits(bs, 8); |
level = (GET_BITS(cache, 15)&0xff); |
1005 |
|
|
1006 |
if (level == 0 || level == 128) |
if (level == 0 || level == 128) |
1007 |
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); |
1008 |
|
|
1009 |
|
/* We've "eaten" 22 bits */ |
1010 |
|
BitstreamSkip(bs, 22); |
1011 |
|
|
1012 |
return (level << 24) >> 24; |
return (level << 24) >> 24; |
1013 |
} |
} |
1014 |
|
|
1015 |
mode = BitstreamShowBits(bs, 2); |
if ((mode = GET_BITS(cache, 2)) < 3) { |
1016 |
|
const int skip[3] = {1, 1, 2}; |
1017 |
if (mode < 3) { |
cache <<= skip[mode]; |
|
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
|
1018 |
|
|
1019 |
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
reverse_event = &DCT3D[intra][GET_BITS(cache, 12)]; |
1020 |
|
|
1021 |
if ((level = reverse_event->event.level) == 0) |
if ((level = reverse_event->event.level) == 0) |
1022 |
goto error; |
goto error; |
1024 |
*last = reverse_event->event.last; |
*last = reverse_event->event.last; |
1025 |
*run = reverse_event->event.run; |
*run = reverse_event->event.run; |
1026 |
|
|
1027 |
BitstreamSkip(bs, reverse_event->len); |
if (mode < 2) { |
1028 |
|
/* first escape mode, level is offset */ |
|
if (mode < 2) /* first escape mode, level is offset */ |
|
1029 |
level += max_level[intra][*last][*run]; |
level += max_level[intra][*last][*run]; |
1030 |
else /* second escape mode, run is offset */ |
} else { |
1031 |
|
/* second escape mode, run is offset */ |
1032 |
*run += max_run[intra][*last][level] + 1; |
*run += max_run[intra][*last][level] + 1; |
1033 |
|
} |
1034 |
|
|
1035 |
return BitstreamGetBits(bs, 1) ? -level : level; |
/* Update bitstream position */ |
1036 |
|
BitstreamSkip(bs, 7 + skip[mode] + reverse_event->len + 1); |
1037 |
|
|
1038 |
|
return (GET_BITS(cache, reverse_event->len+1)&0x01) ? -level : level; |
1039 |
} |
} |
1040 |
|
|
1041 |
/* third escape mode - fixed length codes */ |
/* third escape mode - fixed length codes */ |
1042 |
BitstreamSkip(bs, 2); |
cache <<= 2; |
1043 |
*last = BitstreamGetBits(bs, 1); |
*last = GET_BITS(cache, 1); |
1044 |
*run = BitstreamGetBits(bs, 6); |
*run = (GET_BITS(cache, 7)&0x3f); |
1045 |
BitstreamSkip(bs, 1); /* marker */ |
level = (GET_BITS(cache, 20)&0xfff); |
1046 |
level = BitstreamGetBits(bs, 12); |
|
1047 |
BitstreamSkip(bs, 1); /* marker */ |
/* Update bitstream position */ |
1048 |
|
BitstreamSkip(bs, 30); |
1049 |
|
|
1050 |
return (level << 20) >> 20; |
return (level << 20) >> 20; |
1051 |
|
|
1087 |
} |
} |
1088 |
|
|
1089 |
void |
void |
1090 |
get_inter_block(Bitstream * bs, |
get_inter_block_h263( |
1091 |
|
Bitstream * bs, |
1092 |
int16_t * block, |
int16_t * block, |
1093 |
int direction) |
int direction, |
1094 |
|
const int quant, |
1095 |
|
const uint16_t *matrix) |
1096 |
{ |
{ |
1097 |
|
|
1098 |
const uint16_t *scan = scan_tables[direction]; |
const uint16_t *scan = scan_tables[direction]; |
1099 |
|
const uint16_t quant_m_2 = quant << 1; |
1100 |
|
const uint16_t quant_add = (quant & 1 ? quant : quant - 1); |
1101 |
int p; |
int p; |
1102 |
int level; |
int level; |
1103 |
int run; |
int run; |
1112 |
} |
} |
1113 |
p += run; |
p += run; |
1114 |
|
|
1115 |
block[scan[p]] = level; |
if (level < 0) { |
1116 |
|
level = level*quant_m_2 - quant_add; |
1117 |
|
block[scan[p]] = (level >= -2048 ? level : -2048); |
1118 |
|
} else { |
1119 |
|
level = level * quant_m_2 + quant_add; |
1120 |
|
block[scan[p]] = (level <= 2047 ? level : 2047); |
1121 |
|
} |
1122 |
|
p++; |
1123 |
|
} while (!last); |
1124 |
|
} |
1125 |
|
|
1126 |
|
void |
1127 |
|
get_inter_block_mpeg( |
1128 |
|
Bitstream * bs, |
1129 |
|
int16_t * block, |
1130 |
|
int direction, |
1131 |
|
const int quant, |
1132 |
|
const uint16_t *matrix) |
1133 |
|
{ |
1134 |
|
const uint16_t *scan = scan_tables[direction]; |
1135 |
|
uint32_t sum = 0; |
1136 |
|
int p; |
1137 |
|
int level; |
1138 |
|
int run; |
1139 |
|
int last; |
1140 |
|
|
1141 |
DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i\n", scan[p], level); |
p = 0; |
1142 |
/* DPRINTF(XVID_DEBUG_COEFF,"block[%i] %i %08x\n", scan[p], level, BitstreamShowBits(bs, 32)); */ |
do { |
1143 |
|
level = get_coeff(bs, &run, &last, 0, 0); |
1144 |
|
if (run == -1) { |
1145 |
|
DPRINTF(XVID_DEBUG_ERROR,"fatal: invalid run"); |
1146 |
|
break; |
1147 |
|
} |
1148 |
|
p += run; |
1149 |
|
|
1150 |
if (level < -2047 || level > 2047) { |
if (level < 0) { |
1151 |
DPRINTF(XVID_DEBUG_ERROR,"warning: inter overflow %i\n", level); |
level = ((2 * -level + 1) * matrix[scan[p]] * quant) >> 4; |
1152 |
|
block[scan[p]] = (level <= 2048 ? -level : -2048); |
1153 |
|
} else { |
1154 |
|
level = ((2 * level + 1) * matrix[scan[p]] * quant) >> 4; |
1155 |
|
block[scan[p]] = (level <= 2047 ? level : 2047); |
1156 |
} |
} |
1157 |
|
|
1158 |
|
sum ^= block[scan[p]]; |
1159 |
|
|
1160 |
p++; |
p++; |
1161 |
} while (!last); |
} while (!last); |
1162 |
|
|
1163 |
|
/* mismatch control */ |
1164 |
|
if ((sum & 1) == 0) { |
1165 |
|
block[63] ^= 1; |
1166 |
|
} |
1167 |
} |
} |
1168 |
|
|
1169 |
|
|