41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
|
* 28.10.2002 GMC support - gruel * |
45 |
|
* 28.06.2002 added check_resync_marker() * |
46 |
* 14.04.2002 bframe encoding * |
* 14.04.2002 bframe encoding * |
47 |
* 08.03.2002 initial version; isibaar * |
* 08.03.2002 initial version; isibaar * |
48 |
* * |
* * |
68 |
VLC DCT3Dintra[4096]; |
VLC DCT3Dintra[4096]; |
69 |
VLC DCT3Dinter[4096]; |
VLC DCT3Dinter[4096]; |
70 |
|
|
71 |
|
/* not really MB related, but VLCs are only available here */ |
72 |
|
void bs_put_spritetrajectory(Bitstream * bs, const int val) |
73 |
|
{ |
74 |
|
const int code = sprite_trajectory_code[val+16384].code; |
75 |
|
const int len = sprite_trajectory_code[val+16384].len; |
76 |
|
const int code2 = sprite_trajectory_len[len].code; |
77 |
|
const int len2 = sprite_trajectory_len[len].len; |
78 |
|
|
79 |
|
// printf("GMC=%d Code/Len = %d / %d ",val, code,len); |
80 |
|
// printf("Code2 / Len2 = %d / %d \n",code2,len2); |
81 |
|
|
82 |
|
BitstreamPutBits(bs, code2, len2); |
83 |
|
if (len) BitstreamPutBits(bs, code, len); |
84 |
|
} |
85 |
|
|
86 |
|
int bs_get_spritetrajectory(Bitstream * bs) |
87 |
|
{ |
88 |
|
int i; |
89 |
|
for (i = 0; i < 12; i++) |
90 |
|
{ |
91 |
|
if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code) |
92 |
|
{ |
93 |
|
BitstreamSkip(bs, sprite_trajectory_len[i].len); |
94 |
|
return i; |
95 |
|
} |
96 |
|
} |
97 |
|
return -1; |
98 |
|
} |
99 |
|
|
100 |
void |
void |
101 |
init_vlc_tables(void) |
init_vlc_tables(void) |
102 |
{ |
{ |
213 |
DCT3D[0] = DCT3Dinter; |
DCT3D[0] = DCT3Dinter; |
214 |
DCT3D[1] = DCT3Dintra; |
DCT3D[1] = DCT3Dintra; |
215 |
|
|
216 |
|
|
217 |
|
/* init sprite_trajectory tables */ |
218 |
|
/* even if GMC is not specified (it might be used later...) */ |
219 |
|
|
220 |
|
sprite_trajectory_code[0+16384].code = 0; |
221 |
|
sprite_trajectory_code[0+16384].len = 0; |
222 |
|
for (k=0;k<14;k++) |
223 |
|
{ |
224 |
|
int limit = (1<<k); |
225 |
|
|
226 |
|
for (i=-(2*limit-1); i<= -limit; i++) |
227 |
|
{ |
228 |
|
sprite_trajectory_code[i+16384].code = (2*limit-1)+i; |
229 |
|
sprite_trajectory_code[i+16384].len = k+1; |
230 |
|
} |
231 |
|
|
232 |
|
for (i=limit; i<= 2*limit-1; i++) |
233 |
|
{ |
234 |
|
sprite_trajectory_code[i+16384].code = i; |
235 |
|
sprite_trajectory_code[i+16384].len = k+1; |
236 |
|
} |
237 |
|
} |
238 |
} |
} |
239 |
|
|
240 |
static __inline void |
static __inline void |
332 |
} |
} |
333 |
|
|
334 |
|
|
335 |
static void |
static __inline void |
336 |
CodeBlockIntra(const FRAMEINFO * frame, |
CodeBlockIntra(const FRAMEINFO * const frame, |
337 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
338 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
339 |
Bitstream * bs, |
Bitstream * bs, |
382 |
dcc_tab[qcoeff[i * 64 + 0] + 255].len); |
dcc_tab[qcoeff[i * 64 + 0] + 255].len); |
383 |
|
|
384 |
if (pMB->cbp & (1 << (5 - i))) { |
if (pMB->cbp & (1 << (5 - i))) { |
385 |
|
const uint16_t *scan_table = |
386 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
387 |
|
scan_tables[2] : scan_tables[pMB->acpred_directions[i]]; |
388 |
|
|
389 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
390 |
|
|
391 |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1); |
|
scan_tables[pMB->acpred_directions[i]], 1); |
|
392 |
|
|
393 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
394 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
399 |
|
|
400 |
|
|
401 |
static void |
static void |
402 |
CodeBlockInter(const FRAMEINFO * frame, |
CodeBlockInter(const FRAMEINFO * const frame, |
403 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
404 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
405 |
Bitstream * bs, |
Bitstream * bs, |
408 |
|
|
409 |
int32_t i; |
int32_t i; |
410 |
uint32_t bits, mcbpc, cbpy; |
uint32_t bits, mcbpc, cbpy; |
411 |
|
int mcsel=0; |
412 |
|
|
413 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
414 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
417 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
418 |
mcbpc_inter_tab[mcbpc].len); |
mcbpc_inter_tab[mcbpc].len); |
419 |
|
|
420 |
|
if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) ) |
421 |
|
{ |
422 |
|
if (frame->quarterpel) { |
423 |
|
if ( (pMB->qmvs[0].x == frame->GMC_MV.x) && (pMB->qmvs[0].y == frame->GMC_MV.y) ) |
424 |
|
mcsel=1; |
425 |
|
} else { |
426 |
|
if ( (pMB->mvs[0].x == frame->GMC_MV.x) && (pMB->mvs[0].y == frame->GMC_MV.y) ) |
427 |
|
mcsel=1; |
428 |
|
} |
429 |
|
BitstreamPutBit(bs, mcsel); // mcsel: '0'=local motion, '1'=GMC |
430 |
|
} |
431 |
|
|
432 |
// write cbpy |
// write cbpy |
433 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
434 |
|
|
438 |
|
|
439 |
// interlacing |
// interlacing |
440 |
if (frame->global_flags & XVID_INTERLACING) { |
if (frame->global_flags & XVID_INTERLACING) { |
441 |
|
if (pMB->cbp) { |
442 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
443 |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct); |
444 |
|
} |
445 |
|
|
446 |
// if inter block, write field ME flag |
// if inter block, write field ME flag |
447 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
448 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, pMB->field_pred); |
449 |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred); |
450 |
|
|
451 |
// write field prediction references |
// write field prediction references |
452 |
if (pMB->field_pred) { |
if (pMB->field_pred) { |
455 |
} |
} |
456 |
} |
} |
457 |
} |
} |
458 |
// code motion vector(s) |
// code motion vector(s) if motion is local |
459 |
|
if (mcsel==0) |
460 |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
461 |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
462 |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
467 |
// code block coeffs |
// code block coeffs |
468 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) |
469 |
if (pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) |
470 |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
{ |
471 |
|
const uint16_t *scan_table = |
472 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
473 |
|
scan_tables[2] : scan_tables[0]; |
474 |
|
|
475 |
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0); |
476 |
|
} |
477 |
|
|
478 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
479 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
|
|
|
480 |
} |
} |
481 |
|
|
482 |
|
|
483 |
void |
void |
484 |
MBCoding(const FRAMEINFO * frame, |
MBCoding(const FRAMEINFO * const frame, |
485 |
MACROBLOCK * pMB, |
MACROBLOCK * pMB, |
486 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
487 |
Bitstream * bs, |
Bitstream * bs, |
488 |
Statistics * pStat) |
Statistics * pStat) |
489 |
{ |
{ |
490 |
|
if (frame->coding_type != I_VOP) |
491 |
|
BitstreamPutBit(bs, 0); // not_coded |
492 |
|
|
493 |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
|
|
|
|
if (frame->coding_type == P_VOP) { |
|
|
if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
|
|
pMB->mvs[0].y == 0) { |
|
|
BitstreamPutBit(bs, 1); // not_coded |
|
|
return; |
|
|
} else |
|
|
BitstreamPutBit(bs, 0); // coded |
|
|
} |
|
|
|
|
|
if (intra) |
|
494 |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
495 |
else |
else |
496 |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
497 |
|
|
498 |
} |
} |
499 |
|
|
500 |
|
/* |
501 |
|
// moved to mbcoding.h so that in can be 'static __inline' |
502 |
|
void |
503 |
|
MBSkip(Bitstream * bs) |
504 |
|
{ |
505 |
|
BitstreamPutBit(bs, 1); // not coded |
506 |
|
} |
507 |
|
*/ |
508 |
|
|
509 |
/*************************************************************** |
/*************************************************************** |
510 |
* bframe encoding start |
* bframe encoding start |
511 |
***************************************************************/ |
***************************************************************/ |
518 |
3 0001b forward mc+q dbquant, mvdf |
3 0001b forward mc+q dbquant, mvdf |
519 |
*/ |
*/ |
520 |
|
|
521 |
void |
static __inline void |
522 |
put_bvop_mbtype(Bitstream * bs, |
put_bvop_mbtype(Bitstream * bs, |
523 |
int value) |
int value) |
524 |
{ |
{ |
525 |
switch (value) { |
switch (value) { |
526 |
case 0: |
case MODE_FORWARD: |
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
case 1: |
|
527 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
528 |
BitstreamPutBit(bs, 1); |
case MODE_BACKWARD: |
|
return; |
|
|
|
|
|
case 2: |
|
529 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
530 |
|
case MODE_INTERPOLATE: |
531 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
532 |
|
case MODE_DIRECT: |
533 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
534 |
return; |
default: |
535 |
|
break; |
|
case 3: |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
default:; // invalid! |
|
|
|
|
536 |
} |
} |
|
|
|
537 |
} |
} |
538 |
|
|
539 |
/* |
/* |
543 |
+2 11b |
+2 11b |
544 |
*/ |
*/ |
545 |
|
|
546 |
void |
static __inline void |
547 |
put_bvop_dbquant(Bitstream * bs, |
put_bvop_dbquant(Bitstream * bs, |
548 |
int value) |
int value) |
549 |
{ |
{ |
574 |
const int32_t fcode, |
const int32_t fcode, |
575 |
const int32_t bcode, |
const int32_t bcode, |
576 |
Bitstream * bs, |
Bitstream * bs, |
577 |
Statistics * pStat) |
Statistics * pStat, |
578 |
|
int direction) |
579 |
{ |
{ |
580 |
int i; |
int vcode = fcode; |
581 |
|
unsigned int i; |
582 |
|
|
583 |
/* ------------------------------------------------------------------ |
/* ------------------------------------------------------------------ |
584 |
when a block is skipped it is decoded DIRECT(0,) |
when a block is skipped it is decoded DIRECT(0,0) |
585 |
hence are interpolated from forward & backward frames |
hence is interpolated from forward & backward frames |
586 |
------------------------------------------------------------------ */ |
------------------------------------------------------------------ */ |
587 |
|
|
588 |
if (mb->mode == 5) { |
if (mb->mode == MODE_DIRECT_NONE_MV) { |
589 |
BitstreamPutBit(bs, 1); // skipped |
BitstreamPutBit(bs, 1); // skipped |
590 |
return; |
return; |
591 |
} |
} |
608 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
609 |
} |
} |
610 |
|
|
611 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
switch (mb->mode) { |
612 |
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
case MODE_INTERPOLATE: |
613 |
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode |
614 |
} |
CodeVector(bs, mb->pmvs[1].y, vcode, pStat); |
615 |
|
case MODE_BACKWARD: |
616 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
vcode = bcode; |
617 |
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
case MODE_FORWARD: |
618 |
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
CodeVector(bs, mb->pmvs[0].x, vcode, pStat); |
619 |
} |
CodeVector(bs, mb->pmvs[0].y, vcode, pStat); |
620 |
|
break; |
621 |
if (mb->mode == MODE_DIRECT) { |
case MODE_DIRECT: |
622 |
// TODO: direct |
CodeVector(bs, mb->pmvs[3].x, 1, pStat); // fcode is always 1 for delta vector |
623 |
|
CodeVector(bs, mb->pmvs[3].y, 1, pStat); // prediction is always (0,0) |
624 |
|
default: break; |
625 |
} |
} |
626 |
|
|
627 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
628 |
if (mb->cbp & (1 << (5 - i))) { |
if (mb->cbp & (1 << (5 - i))) { |
629 |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0); |
630 |
} |
} |
631 |
} |
} |
632 |
} |
} |
637 |
* decoding stuff starts here * |
* decoding stuff starts here * |
638 |
***************************************************************/ |
***************************************************************/ |
639 |
|
|
640 |
|
|
641 |
|
// for IVOP addbits == 0 |
642 |
|
// for PVOP addbits == fcode - 1 |
643 |
|
// for BVOP addbits == max(fcode,bcode) - 1 |
644 |
|
// returns true or false |
645 |
|
int |
646 |
|
check_resync_marker(Bitstream * bs, int addbits) |
647 |
|
{ |
648 |
|
uint32_t nbits; |
649 |
|
uint32_t code; |
650 |
|
uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits; |
651 |
|
|
652 |
|
nbits = BitstreamNumBitsToByteAlign(bs); |
653 |
|
code = BitstreamShowBits(bs, nbits); |
654 |
|
|
655 |
|
if (code == (((uint32_t)1 << (nbits - 1)) - 1)) |
656 |
|
{ |
657 |
|
return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER; |
658 |
|
} |
659 |
|
|
660 |
|
return 0; |
661 |
|
} |
662 |
|
|
663 |
|
|
664 |
|
|
665 |
int |
int |
666 |
get_mcbpc_intra(Bitstream * bs) |
get_mcbpc_intra(Bitstream * bs) |
667 |
{ |
{ |
668 |
|
|
669 |
uint32_t index; |
uint32_t index; |
670 |
|
|
671 |
while ((index = BitstreamShowBits(bs, 9)) == 1) |
index = BitstreamShowBits(bs, 9); |
|
BitstreamSkip(bs, 9); |
|
|
|
|
672 |
index >>= 3; |
index >>= 3; |
673 |
|
|
674 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
683 |
|
|
684 |
uint32_t index; |
uint32_t index; |
685 |
|
|
686 |
while ((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
|
BitstreamSkip(bs, 9); |
|
687 |
|
|
688 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
689 |
|
|
709 |
|
|
710 |
} |
} |
711 |
|
|
712 |
int |
static __inline int |
713 |
get_mv_data(Bitstream * bs) |
get_mv_data(Bitstream * bs) |
714 |
{ |
{ |
715 |
|
|
826 |
{ |
{ |
827 |
|
|
828 |
const uint16_t *scan = scan_tables[direction]; |
const uint16_t *scan = scan_tables[direction]; |
829 |
int level; |
int level, run, last; |
|
int run; |
|
|
int last; |
|
830 |
|
|
831 |
do { |
do { |
832 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
833 |
if (run == -1) { |
if (run == -1) { |
834 |
DEBUG("fatal: invalid run"); |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
835 |
break; |
break; |
836 |
} |
} |
837 |
coeff += run; |
coeff += run; |
838 |
block[scan[coeff]] = level; |
block[scan[coeff]] = level; |
839 |
|
|
840 |
|
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
841 |
|
//DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); |
842 |
|
|
843 |
if (level < -127 || level > 127) { |
if (level < -127 || level > 127) { |
844 |
DEBUG1("warning: intra_overflow", level); |
DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level); |
845 |
} |
} |
846 |
coeff++; |
coeff++; |
847 |
} while (!last); |
} while (!last); |
850 |
|
|
851 |
void |
void |
852 |
get_inter_block(Bitstream * bs, |
get_inter_block(Bitstream * bs, |
853 |
int16_t * block) |
int16_t * block, |
854 |
|
int direction) |
855 |
{ |
{ |
856 |
|
|
857 |
const uint16_t *scan = scan_tables[0]; |
const uint16_t *scan = scan_tables[direction]; |
858 |
int p; |
int p; |
859 |
int level; |
int level; |
860 |
int run; |
int run; |
864 |
do { |
do { |
865 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
866 |
if (run == -1) { |
if (run == -1) { |
867 |
DEBUG("fatal: invalid run"); |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
868 |
break; |
break; |
869 |
} |
} |
870 |
p += run; |
p += run; |
871 |
|
|
872 |
block[scan[p]] = level; |
block[scan[p]] = level; |
873 |
|
|
874 |
|
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
875 |
|
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
876 |
|
|
877 |
if (level < -127 || level > 127) { |
if (level < -127 || level > 127) { |
878 |
DEBUG1("warning: inter_overflow", level); |
DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level); |
879 |
} |
} |
880 |
p++; |
p++; |
881 |
} while (!last); |
} while (!last); |