29 |
|
|
30 |
/****************************************************************************** |
/****************************************************************************** |
31 |
* * |
* * |
32 |
* bitstream.c * |
* mbcoding.c * |
33 |
* * |
* * |
34 |
* Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * |
* Copyright (C) 2002 - Michael Militzer <isibaar@xvid.org> * |
35 |
* * |
* * |
36 |
* For more information visit the XviD homepage: http://www.xvid.org * |
* For more information visit the XviD homepage: http://www.xvid.org * |
37 |
* * |
* * |
41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
* 14.04.2002 bframe encoding |
* 14.04.2002 bframe encoding * |
45 |
* 08.03.2002 initial version; isibaar * |
* 08.03.2002 initial version; isibaar * |
46 |
* * |
* * |
47 |
******************************************************************************/ |
******************************************************************************/ |
60 |
#define ABS(X) (((X)>0)?(X):-(X)) |
#define ABS(X) (((X)>0)?(X):-(X)) |
61 |
#define CLIP(X,A) (X > A) ? (A) : (X) |
#define CLIP(X,A) (X > A) ? (A) : (X) |
62 |
|
|
63 |
VLC intra_table[65536]; |
VLC intra_table[524032]; |
64 |
VLC inter_table[65536]; |
VLC inter_table[524032]; |
65 |
|
|
66 |
VLC DCT3Dintra[4096]; |
VLC DCT3Dintra[4096]; |
67 |
VLC DCT3Dinter[4096]; |
VLC DCT3Dinter[4096]; |
68 |
|
|
|
static int16_t clip_table[4096]; |
|
|
|
|
69 |
void init_vlc_tables(void) |
void init_vlc_tables(void) |
70 |
{ |
{ |
71 |
|
|
80 |
vlc[0] = intra_table; |
vlc[0] = intra_table; |
81 |
vlc[1] = inter_table; |
vlc[1] = inter_table; |
82 |
|
|
|
// initialize the clipping table |
|
|
for(i = -2048; i < 2048; i++) { |
|
|
clip_table[i + 2048] = i; |
|
|
if(i < -255) |
|
|
clip_table[i + 2048] = -255; |
|
|
if(i > 255) |
|
|
clip_table[i + 2048] = 255; |
|
|
} |
|
|
|
|
83 |
// generate encoding vlc lookup tables |
// generate encoding vlc lookup tables |
84 |
|
// the lookup table idea is taken from the excellent fame project by Vivien Chapellier |
85 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
86 |
intra = i % 2; |
intra = i % 2; |
87 |
last = i / 2; |
last = i / 2; |
88 |
|
|
89 |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
90 |
|
|
91 |
for(k = -255; k < 256; k++) { // level |
for(k = -2047; k < 2048; k++) { // level |
92 |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
93 |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
94 |
|
|
97 |
uint32_t run = l; |
uint32_t run = l; |
98 |
|
|
99 |
if((abs(level) <= max_level_ptr[run]) && |
if((abs(level) <= max_level_ptr[run]) && |
100 |
(run <= max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
(run <= (uint32_t)max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
101 |
|
|
102 |
vlc[intra]->code = 0; |
vlc[intra]->code = 0; |
103 |
vlc[intra]->len = 0; |
vlc[intra]->len = 0; |
110 |
level += max_level_ptr[run]; |
level += max_level_ptr[run]; |
111 |
|
|
112 |
if((abs(level) <= max_level_ptr[run]) && |
if((abs(level) <= max_level_ptr[run]) && |
113 |
(run <= max_run_ptr[abs(level)])) { |
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
114 |
|
|
115 |
vlc[intra]->code = 0x06; |
vlc[intra]->code = 0x06; |
116 |
vlc[intra]->len = 8; |
vlc[intra]->len = 8; |
125 |
run -= max_run_ptr[abs(level)] + 1; // and change run |
run -= max_run_ptr[abs(level)] + 1; // and change run |
126 |
|
|
127 |
if((abs(level) <= max_level_ptr[run]) && |
if((abs(level) <= max_level_ptr[run]) && |
128 |
(run <= max_run_ptr[abs(level)])) { |
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
129 |
|
|
130 |
vlc[intra]->code = 0x0e; |
vlc[intra]->code = 0x0e; |
131 |
vlc[intra]->len = 9; |
vlc[intra]->len = 9; |
183 |
} |
} |
184 |
|
|
185 |
static __inline void CodeVector(Bitstream *bs, |
static __inline void CodeVector(Bitstream *bs, |
186 |
int16_t value, |
int32_t value, |
187 |
int16_t f_code, |
int32_t f_code, |
188 |
Statistics *pStat) |
Statistics *pStat) |
189 |
{ |
{ |
190 |
|
|
236 |
|
|
237 |
|
|
238 |
static __inline void CodeCoeff(Bitstream *bs, |
static __inline void CodeCoeff(Bitstream *bs, |
239 |
int16_t qcoeff[64], |
const int16_t qcoeff[64], |
240 |
VLC *table, |
VLC *table, |
241 |
const uint16_t *zigzag, |
const uint16_t *zigzag, |
242 |
uint16_t intra) |
uint16_t intra) |
249 |
j = intra; |
j = intra; |
250 |
last = intra; |
last = intra; |
251 |
|
|
252 |
while((v = qcoeff[zigzag[j]]) == 0) j++; |
while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; |
253 |
|
|
254 |
do { |
do { |
255 |
// count zeroes |
vlc = table + 64 * 2047 + (v << 6) + j - last; |
|
vlc = table + 64*255 + (clip_table[2048+v] << 6) + j - last; |
|
256 |
last = ++j; |
last = ++j; |
257 |
|
|
258 |
|
// count zeroes |
259 |
while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; |
while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; |
260 |
|
|
261 |
// write code |
// write code |
262 |
if(j != 64) { |
if(j != 64) { |
263 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
264 |
} else { |
} else { |
265 |
vlc += 64*511; |
vlc += 64 * 4095; |
266 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
267 |
break; |
break; |
268 |
} |
} |
271 |
} |
} |
272 |
|
|
273 |
|
|
274 |
static void CodeBlockIntra(const MBParam * pParam, |
static void CodeBlockIntra(const FRAMEINFO * frame, |
275 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
276 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
277 |
Bitstream * bs, |
Bitstream * bs, |
283 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
284 |
|
|
285 |
// write mcbpc |
// write mcbpc |
286 |
if(pParam->coding_type == I_VOP) { |
if(frame->coding_type == I_VOP) { |
287 |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
288 |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len); |
289 |
} |
} |
306 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
307 |
|
|
308 |
// write interlacing |
// write interlacing |
309 |
if (pParam->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) |
310 |
{ |
{ |
311 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
312 |
} |
} |
341 |
} |
} |
342 |
|
|
343 |
|
|
344 |
static void CodeBlockInter(const MBParam * pParam, |
static void CodeBlockInter(const FRAMEINFO * frame, |
345 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
346 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
347 |
Bitstream * bs, |
Bitstream * bs, |
365 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
366 |
|
|
367 |
// interlacing |
// interlacing |
368 |
if (pParam->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) |
369 |
{ |
{ |
370 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
371 |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
388 |
// code motion vector(s) |
// code motion vector(s) |
389 |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
390 |
{ |
{ |
391 |
CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
392 |
CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
393 |
} |
} |
394 |
|
|
395 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
405 |
} |
} |
406 |
|
|
407 |
|
|
408 |
void MBCoding(const MBParam * pParam, |
void MBCoding(const FRAMEINFO * frame, |
409 |
MACROBLOCK *pMB, |
MACROBLOCK *pMB, |
410 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
411 |
Bitstream * bs, |
Bitstream * bs, |
414 |
|
|
415 |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
416 |
|
|
417 |
if(pParam->coding_type == P_VOP) { |
if(frame->coding_type == P_VOP) { |
418 |
if(pMB->cbp == 0 && pMB->mode == MODE_INTER && |
if(pMB->cbp == 0 && pMB->mode == MODE_INTER && |
419 |
pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
420 |
{ |
{ |
426 |
} |
} |
427 |
|
|
428 |
if(intra) |
if(intra) |
429 |
CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
430 |
else |
else |
431 |
CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
432 |
|
|
433 |
} |
} |
434 |
|
|
502 |
|
|
503 |
void MBCodingBVOP(const MACROBLOCK * mb, |
void MBCodingBVOP(const MACROBLOCK * mb, |
504 |
const int16_t qcoeff[6*64], |
const int16_t qcoeff[6*64], |
505 |
const int16_t fcode, |
const int32_t fcode, |
506 |
const int16_t bcode, |
const int32_t bcode, |
507 |
Bitstream * bs, |
Bitstream * bs, |
508 |
Statistics * pStat) |
Statistics * pStat) |
509 |
{ |
{ |