41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
|
* 28.06.2002 added check_resync_marker() * |
45 |
* 14.04.2002 bframe encoding * |
* 14.04.2002 bframe encoding * |
46 |
* 08.03.2002 initial version; isibaar * |
* 08.03.2002 initial version; isibaar * |
47 |
* * |
* * |
280 |
} |
} |
281 |
|
|
282 |
|
|
283 |
static void |
static __inline void |
284 |
CodeBlockIntra(const FRAMEINFO * frame, |
CodeBlockIntra(const FRAMEINFO * frame, |
285 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
286 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
370 |
|
|
371 |
// interlacing |
// interlacing |
372 |
if (frame->global_flags & XVID_INTERLACING) { |
if (frame->global_flags & XVID_INTERLACING) { |
373 |
|
if (pMB->cbp) { |
374 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
375 |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
376 |
|
} |
377 |
|
|
378 |
// if inter block, write field ME flag |
// if inter block, write field ME flag |
379 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
414 |
Statistics * pStat) |
Statistics * pStat) |
415 |
{ |
{ |
416 |
|
|
|
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
|
|
|
|
417 |
if (frame->coding_type == P_VOP) { |
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 |
|
418 |
BitstreamPutBit(bs, 0); // coded |
BitstreamPutBit(bs, 0); // coded |
419 |
} |
} |
420 |
|
|
421 |
if (intra) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
422 |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
423 |
else |
else |
424 |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
425 |
|
|
426 |
} |
} |
427 |
|
|
428 |
|
/* |
429 |
|
// moved to mbcoding.h so that in can be 'static __inline' |
430 |
|
void |
431 |
|
MBSkip(Bitstream * bs) |
432 |
|
{ |
433 |
|
BitstreamPutBit(bs, 1); // not coded |
434 |
|
} |
435 |
|
*/ |
436 |
|
|
437 |
/*************************************************************** |
/*************************************************************** |
438 |
* bframe encoding start |
* bframe encoding start |
439 |
***************************************************************/ |
***************************************************************/ |
446 |
3 0001b forward mc+q dbquant, mvdf |
3 0001b forward mc+q dbquant, mvdf |
447 |
*/ |
*/ |
448 |
|
|
449 |
void |
static __inline void |
450 |
put_bvop_mbtype(Bitstream * bs, |
put_bvop_mbtype(Bitstream * bs, |
451 |
int value) |
int value) |
452 |
{ |
{ |
453 |
switch (value) { |
switch (value) { |
454 |
case 0: |
case MODE_FORWARD: |
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
case 1: |
|
455 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
456 |
BitstreamPutBit(bs, 1); |
case MODE_BACKWARD: |
|
return; |
|
|
|
|
|
case 2: |
|
457 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
458 |
|
case MODE_INTERPOLATE: |
459 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
460 |
|
case MODE_DIRECT: |
461 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
462 |
return; |
default: |
463 |
|
break; |
|
case 3: |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
default:; // invalid! |
|
|
|
|
464 |
} |
} |
|
|
|
465 |
} |
} |
466 |
|
|
467 |
/* |
/* |
471 |
+2 11b |
+2 11b |
472 |
*/ |
*/ |
473 |
|
|
474 |
void |
static __inline void |
475 |
put_bvop_dbquant(Bitstream * bs, |
put_bvop_dbquant(Bitstream * bs, |
476 |
int value) |
int value) |
477 |
{ |
{ |
504 |
Bitstream * bs, |
Bitstream * bs, |
505 |
Statistics * pStat) |
Statistics * pStat) |
506 |
{ |
{ |
507 |
int i; |
int vcode = fcode; |
508 |
|
unsigned int i; |
509 |
|
|
510 |
/* ------------------------------------------------------------------ |
/* ------------------------------------------------------------------ |
511 |
when a block is skipped it is decoded DIRECT(0,) |
when a block is skipped it is decoded DIRECT(0,0) |
512 |
hence are interpolated from forward & backward frames |
hence is interpolated from forward & backward frames |
513 |
------------------------------------------------------------------ */ |
------------------------------------------------------------------ */ |
514 |
|
|
515 |
if (mb->mode == 5) { |
if (mb->mode == MODE_DIRECT_NONE_MV) { |
516 |
BitstreamPutBit(bs, 1); // skipped |
BitstreamPutBit(bs, 1); // skipped |
517 |
return; |
return; |
518 |
} |
} |
535 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
536 |
} |
} |
537 |
|
|
538 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
switch (mb->mode) { |
539 |
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
case MODE_INTERPOLATE: |
540 |
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode |
541 |
} |
CodeVector(bs, mb->pmvs[1].y, vcode, pStat); |
542 |
|
case MODE_BACKWARD: |
543 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
vcode = bcode; |
544 |
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
case MODE_FORWARD: |
545 |
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
CodeVector(bs, mb->pmvs[0].x, vcode, pStat); |
546 |
} |
CodeVector(bs, mb->pmvs[0].y, vcode, pStat); |
547 |
|
break; |
548 |
if (mb->mode == MODE_DIRECT) { |
case MODE_DIRECT: |
549 |
// TODO: direct |
CodeVector(bs, mb->pmvs[3].x, 1, pStat); // fcode is always 1 for delta vector |
550 |
|
CodeVector(bs, mb->pmvs[3].y, 1, pStat); // prediction is always (0,0) |
551 |
|
default: break; |
552 |
} |
} |
553 |
|
|
554 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
564 |
* decoding stuff starts here * |
* decoding stuff starts here * |
565 |
***************************************************************/ |
***************************************************************/ |
566 |
|
|
567 |
|
|
568 |
|
// for IVOP addbits == 0 |
569 |
|
// for PVOP addbits == fcode - 1 |
570 |
|
// for BVOP addbits == max(fcode,bcode) - 1 |
571 |
|
// returns true or false |
572 |
|
int |
573 |
|
check_resync_marker(Bitstream * bs, int addbits) |
574 |
|
{ |
575 |
|
uint32_t nbits; |
576 |
|
uint32_t code; |
577 |
|
uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits; |
578 |
|
|
579 |
|
nbits = BitstreamNumBitsToByteAlign(bs); |
580 |
|
code = BitstreamShowBits(bs, nbits); |
581 |
|
|
582 |
|
if (code == (((uint32_t)1 << (nbits - 1)) - 1)) |
583 |
|
{ |
584 |
|
return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER; |
585 |
|
} |
586 |
|
|
587 |
|
return 0; |
588 |
|
} |
589 |
|
|
590 |
|
|
591 |
|
|
592 |
int |
int |
593 |
get_mcbpc_intra(Bitstream * bs) |
get_mcbpc_intra(Bitstream * bs) |
594 |
{ |
{ |
595 |
|
|
596 |
uint32_t index; |
uint32_t index; |
597 |
|
|
598 |
while ((index = BitstreamShowBits(bs, 9)) == 1) |
index = BitstreamShowBits(bs, 9); |
|
BitstreamSkip(bs, 9); |
|
|
|
|
599 |
index >>= 3; |
index >>= 3; |
600 |
|
|
601 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
610 |
|
|
611 |
uint32_t index; |
uint32_t index; |
612 |
|
|
613 |
while ((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
|
BitstreamSkip(bs, 9); |
|
614 |
|
|
615 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
616 |
|
|
636 |
|
|
637 |
} |
} |
638 |
|
|
639 |
int |
static __inline int |
640 |
get_mv_data(Bitstream * bs) |
get_mv_data(Bitstream * bs) |
641 |
{ |
{ |
642 |
|
|
753 |
{ |
{ |
754 |
|
|
755 |
const uint16_t *scan = scan_tables[direction]; |
const uint16_t *scan = scan_tables[direction]; |
756 |
int level; |
int level, run, last; |
|
int run; |
|
|
int last; |
|
757 |
|
|
758 |
do { |
do { |
759 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
763 |
} |
} |
764 |
coeff += run; |
coeff += run; |
765 |
block[scan[coeff]] = level; |
block[scan[coeff]] = level; |
766 |
|
|
767 |
|
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
768 |
|
//DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); |
769 |
|
|
770 |
if (level < -127 || level > 127) { |
if (level < -127 || level > 127) { |
771 |
DEBUG1("warning: intra_overflow", level); |
DEBUG1("warning: intra_overflow", level); |
772 |
} |
} |
796 |
p += run; |
p += run; |
797 |
|
|
798 |
block[scan[p]] = level; |
block[scan[p]] = level; |
799 |
|
|
800 |
|
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
801 |
|
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
802 |
|
|
803 |
if (level < -127 || level > 127) { |
if (level < -127 || level > 127) { |
804 |
DEBUG1("warning: inter_overflow", level); |
DEBUG1("warning: inter_overflow", level); |
805 |
} |
} |