1 |
/***************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Vector Length Coding tables - |
* - Macro Block coding functions - |
5 |
* |
* |
6 |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 |
* |
* |
56 |
static VLC DCT3Dinter[4096]; |
static VLC DCT3Dinter[4096]; |
57 |
|
|
58 |
/***************************************************************************** |
/***************************************************************************** |
59 |
* Functions |
* Vector Length Coding Initialization |
60 |
****************************************************************************/ |
****************************************************************************/ |
61 |
|
|
62 |
void |
void |
65 |
|
|
66 |
int32_t k, l, i, intra, last; |
int32_t k, l, i, intra, last; |
67 |
VLC *vlc[2]; |
VLC *vlc[2]; |
68 |
VLC **coeff_ptr; |
VLC const **coeff_ptr; |
69 |
VLC *vlc1, *vlc2; |
VLC *vlc1, *vlc2; |
70 |
|
|
71 |
vlc1 = DCT3Dintra; |
vlc1 = DCT3Dintra; |
86 |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
87 |
|
|
88 |
for (k = -2047; k < 2048; k++) { // level |
for (k = -2047; k < 2048; k++) { // level |
89 |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
int8_t const *max_level_ptr = max_level[last + 2 * intra]; |
90 |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
int8_t const *max_run_ptr = max_run[last + 2 * intra]; |
91 |
|
|
92 |
for (l = 0; l < 64; l++) { // run |
for (l = 0; l < 64; l++) { // run |
93 |
int32_t level = k; |
int32_t level = k; |
180 |
|
|
181 |
} |
} |
182 |
|
|
183 |
|
/***************************************************************************** |
184 |
|
* Local inlined functions for MB coding |
185 |
|
****************************************************************************/ |
186 |
|
|
187 |
static __inline void |
static __inline void |
188 |
CodeVector(Bitstream * bs, |
CodeVector(Bitstream * bs, |
189 |
int32_t value, |
int32_t value, |
239 |
|
|
240 |
} |
} |
241 |
|
|
|
|
|
242 |
static __inline void |
static __inline void |
243 |
CodeCoeff(Bitstream * bs, |
CodeCoeff(Bitstream * bs, |
244 |
const int16_t qcoeff[64], |
const int16_t qcoeff[64], |
277 |
|
|
278 |
} |
} |
279 |
|
|
280 |
|
/***************************************************************************** |
281 |
|
* Local functions |
282 |
|
****************************************************************************/ |
283 |
|
|
284 |
static void |
static void |
285 |
CodeBlockIntra(const FRAMEINFO * frame, |
CodeBlockIntra(const FRAMEINFO * frame, |
406 |
|
|
407 |
} |
} |
408 |
|
|
409 |
|
/***************************************************************************** |
410 |
|
* Macro Block bitstream encoding functions |
411 |
|
****************************************************************************/ |
412 |
|
|
413 |
void |
void |
414 |
MBCoding(const FRAMEINFO * frame, |
MBCoding(const FRAMEINFO * frame, |
437 |
return; |
return; |
438 |
} |
} |
439 |
|
|
440 |
|
/***************************************************************************** |
441 |
/*************************************************************** |
* decoding stuff starts here |
442 |
* bframe encoding start |
****************************************************************************/ |
|
***************************************************************/ |
|
|
|
|
|
/* |
|
|
mbtype |
|
|
0 1b direct(h263) mvdb |
|
|
1 01b interpolate mc+q dbquant, mvdf, mvdb |
|
|
2 001b backward mc+q dbquant, mvdb |
|
|
3 0001b forward mc+q dbquant, mvdf |
|
|
*/ |
|
|
|
|
|
void |
|
|
put_bvop_mbtype(Bitstream * bs, |
|
|
int value) |
|
|
{ |
|
|
switch (value) { |
|
|
case 0: |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
case 1: |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
case 2: |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
case 3: |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
default:; // invalid! |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
443 |
|
|
444 |
/* |
/* |
445 |
dbquant |
* For IVOP addbits == 0 |
446 |
-2 10b |
* For PVOP addbits == fcode - 1 |
447 |
0 0b |
* For BVOP addbits == max(fcode,bcode) - 1 |
448 |
+2 11b |
* returns true or false |
449 |
*/ |
*/ |
450 |
|
|
|
void |
|
|
put_bvop_dbquant(Bitstream * bs, |
|
|
int value) |
|
|
{ |
|
|
switch (value) { |
|
|
case 0: |
|
|
BitstreamPutBit(bs, 0); |
|
|
return; |
|
|
|
|
|
case -2: |
|
|
BitstreamPutBit(bs, 1); |
|
|
BitstreamPutBit(bs, 0); |
|
|
return; |
|
|
|
|
|
case 2: |
|
|
BitstreamPutBit(bs, 1); |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
default:; // invalid |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
MBCodingBVOP(const MACROBLOCK * mb, |
|
|
const int16_t qcoeff[6 * 64], |
|
|
const int32_t fcode, |
|
|
const int32_t bcode, |
|
|
Bitstream * bs, |
|
|
Statistics * pStat) |
|
|
{ |
|
|
int i; |
|
|
|
|
|
/* ------------------------------------------------------------------ |
|
|
when a block is skipped it is decoded DIRECT(0,0) |
|
|
hence is interpolated from forward & backward frames |
|
|
------------------------------------------------------------------ */ |
|
|
|
|
|
if (mb->mode == MODE_DIRECT_NONE_MV) { |
|
|
BitstreamPutBit(bs, 1); // skipped |
|
|
return; |
|
|
} |
|
|
|
|
|
BitstreamPutBit(bs, 0); // not skipped |
|
|
|
|
|
if (mb->cbp == 0) { |
|
|
BitstreamPutBit(bs, 1); // cbp == 0 |
|
|
} else { |
|
|
BitstreamPutBit(bs, 0); // cbp == xxx |
|
|
} |
|
|
|
|
|
put_bvop_mbtype(bs, mb->mode); |
|
|
|
|
|
if (mb->cbp) { |
|
|
BitstreamPutBits(bs, mb->cbp, 6); |
|
|
} |
|
|
|
|
|
if (mb->mode != MODE_DIRECT && mb->cbp != 0) { |
|
|
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
|
|
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
|
|
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
|
|
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
|
|
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_DIRECT) { |
|
|
CodeVector(bs, mb->deltamv.x, 1, pStat); /* fcode is always 1 for delta vector */ |
|
|
CodeVector(bs, mb->deltamv.y, 1, pStat); /* prediction is always (0,0) */ |
|
|
} |
|
|
|
|
|
for (i = 0; i < 6; i++) { |
|
|
if (mb->cbp & (1 << (5 - i))) { |
|
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************** |
|
|
* decoding stuff starts here * |
|
|
***************************************************************/ |
|
|
|
|
|
|
|
|
// for IVOP addbits == 0 |
|
|
// for PVOP addbits == fcode - 1 |
|
|
// for BVOP addbits == max(fcode,bcode) - 1 |
|
|
// returns true or false |
|
451 |
int |
int |
452 |
check_resync_marker(Bitstream * bs, int addbits) |
check_resync_marker(Bitstream * bs, int addbits) |
453 |
{ |
{ |
624 |
|
|
625 |
} |
} |
626 |
|
|
627 |
|
/***************************************************************************** |
628 |
|
* Local inlined function to "decode" written vlc codes |
629 |
|
****************************************************************************/ |
630 |
|
|
631 |
|
static __inline int |
632 |
|
get_coeff(Bitstream * bs, |
633 |
|
int *run, |
634 |
|
int *last, |
635 |
|
int intra, |
636 |
|
int short_video_header) |
637 |
|
{ |
638 |
|
|
639 |
|
uint32_t mode; |
640 |
|
const VLC *tab; |
641 |
|
int32_t level; |
642 |
|
|
643 |
|
if (short_video_header) // inter-VLCs will be used for both intra and inter blocks |
644 |
|
intra = 0; |
645 |
|
|
646 |
|
tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
647 |
|
|
648 |
|
if (tab->code == -1) |
649 |
|
goto error; |
650 |
|
|
651 |
|
BitstreamSkip(bs, tab->len); |
652 |
|
|
653 |
|
if (tab->code != ESCAPE) { |
654 |
|
if (!intra) { |
655 |
|
*run = (tab->code >> 4) & 255; |
656 |
|
level = tab->code & 15; |
657 |
|
*last = (tab->code >> 12) & 1; |
658 |
|
} else { |
659 |
|
*run = (tab->code >> 8) & 255; |
660 |
|
level = tab->code & 255; |
661 |
|
*last = (tab->code >> 16) & 1; |
662 |
|
} |
663 |
|
return BitstreamGetBit(bs) ? -level : level; |
664 |
|
} |
665 |
|
|
666 |
|
if (short_video_header) { |
667 |
|
// escape mode 4 - H.263 type, only used if short_video_header = 1 |
668 |
|
*last = BitstreamGetBit(bs); |
669 |
|
*run = BitstreamGetBits(bs, 6); |
670 |
|
level = BitstreamGetBits(bs, 8); |
671 |
|
|
672 |
|
if (level == 0 || level == 128) |
673 |
|
DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level); |
674 |
|
|
675 |
|
return (level >= 128 ? -(256 - level) : level); |
676 |
|
} |
677 |
|
|
678 |
|
mode = BitstreamShowBits(bs, 2); |
679 |
|
|
680 |
|
if (mode < 3) { |
681 |
|
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
682 |
|
|
683 |
|
tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
684 |
|
if (tab->code == -1) |
685 |
|
goto error; |
686 |
|
|
687 |
|
BitstreamSkip(bs, tab->len); |
688 |
|
|
689 |
|
if (!intra) { |
690 |
|
*run = (tab->code >> 4) & 255; |
691 |
|
level = tab->code & 15; |
692 |
|
*last = (tab->code >> 12) & 1; |
693 |
|
} else { |
694 |
|
*run = (tab->code >> 8) & 255; |
695 |
|
level = tab->code & 255; |
696 |
|
*last = (tab->code >> 16) & 1; |
697 |
|
} |
698 |
|
|
699 |
|
if (mode < 2) // first escape mode, level is offset |
700 |
|
level += max_level[*last + (!intra << 1)][*run]; // need to add back the max level |
701 |
|
else if (mode == 2) // second escape mode, run is offset |
702 |
|
*run += max_run[*last + (!intra << 1)][level] + 1; |
703 |
|
|
704 |
|
return BitstreamGetBit(bs) ? -level : level; |
705 |
|
} |
706 |
|
// third escape mode - fixed length codes |
707 |
|
BitstreamSkip(bs, 2); |
708 |
|
*last = BitstreamGetBits(bs, 1); |
709 |
|
*run = BitstreamGetBits(bs, 6); |
710 |
|
BitstreamSkip(bs, 1); // marker |
711 |
|
level = BitstreamGetBits(bs, 12); |
712 |
|
BitstreamSkip(bs, 1); // marker |
713 |
|
|
714 |
|
return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level; |
715 |
|
|
716 |
|
error: |
717 |
|
*run = VLC_ERROR; |
718 |
|
return 0; |
719 |
|
|
720 |
|
} |
721 |
|
|
722 |
|
/***************************************************************************** |
723 |
|
* MB reading functions |
724 |
|
****************************************************************************/ |
725 |
|
|
726 |
void |
void |
727 |
get_intra_block(Bitstream * bs, |
get_intra_block(Bitstream * bs, |
728 |
int16_t * block, |
int16_t * block, |