41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
|
* 28.10.2002 GMC support - gruel * |
45 |
* 28.06.2002 added check_resync_marker() * |
* 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 * |
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 inline bs_put_spritetrajectory(Bitstream * bs, |
73 |
|
const int val) |
74 |
|
{ |
75 |
|
const int code = sprite_trajectory_code[val+16384].code; |
76 |
|
const int len = sprite_trajectory_code[val+16384].len; |
77 |
|
const int code2 = sprite_trajectory_len[len].code; |
78 |
|
const int len2 = sprite_trajectory_len[len].len; |
79 |
|
|
80 |
|
// printf("GMC=%d Code/Len = %d / %d ",val, code,len); |
81 |
|
// printf("Code2 / Len2 = %d / %d \n",code2,len2); |
82 |
|
|
83 |
|
BitstreamPutBits(bs, code2, len2); |
84 |
|
if (len) BitstreamPutBits(bs, code, len); |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
void |
void |
89 |
init_vlc_tables(void) |
init_vlc_tables(void) |
90 |
{ |
{ |
201 |
DCT3D[0] = DCT3Dinter; |
DCT3D[0] = DCT3Dinter; |
202 |
DCT3D[1] = DCT3Dintra; |
DCT3D[1] = DCT3Dintra; |
203 |
|
|
204 |
|
|
205 |
|
/* init sprite_trajectory tables */ |
206 |
|
/* even if GMC is not specified (it might be used later...) */ |
207 |
|
|
208 |
|
sprite_trajectory_code[0+16384].code = 0; |
209 |
|
sprite_trajectory_code[0+16384].len = 0; |
210 |
|
for (k=0;k<14;k++) |
211 |
|
{ |
212 |
|
int limit = (1<<k); |
213 |
|
|
214 |
|
for (i=-(2*limit-1); i<= -limit; i++) |
215 |
|
{ |
216 |
|
sprite_trajectory_code[i+16384].code = (2*limit-1)+i; |
217 |
|
sprite_trajectory_code[i+16384].len = k+1; |
218 |
|
} |
219 |
|
|
220 |
|
for (i=limit; i<= 2*limit-1; i++) |
221 |
|
{ |
222 |
|
sprite_trajectory_code[i+16384].code = i; |
223 |
|
sprite_trajectory_code[i+16384].len = k+1; |
224 |
|
} |
225 |
|
} |
226 |
} |
} |
227 |
|
|
228 |
static __inline void |
static __inline void |
320 |
} |
} |
321 |
|
|
322 |
|
|
323 |
static void |
static __inline void |
324 |
CodeBlockIntra(const FRAMEINFO * frame, |
CodeBlockIntra(const FRAMEINFO * const frame, |
325 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
326 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
327 |
Bitstream * bs, |
Bitstream * bs, |
370 |
dcc_tab[qcoeff[i * 64 + 0] + 255].len); |
dcc_tab[qcoeff[i * 64 + 0] + 255].len); |
371 |
|
|
372 |
if (pMB->cbp & (1 << (5 - i))) { |
if (pMB->cbp & (1 << (5 - i))) { |
373 |
|
const uint16_t *scan_table = |
374 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
375 |
|
scan_tables[2] : scan_tables[pMB->acpred_directions[i]]; |
376 |
|
|
377 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
378 |
|
|
379 |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1); |
|
scan_tables[pMB->acpred_directions[i]], 1); |
|
380 |
|
|
381 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
382 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
387 |
|
|
388 |
|
|
389 |
static void |
static void |
390 |
CodeBlockInter(const FRAMEINFO * frame, |
CodeBlockInter(const FRAMEINFO * const frame, |
391 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
392 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
393 |
Bitstream * bs, |
Bitstream * bs, |
396 |
|
|
397 |
int32_t i; |
int32_t i; |
398 |
uint32_t bits, mcbpc, cbpy; |
uint32_t bits, mcbpc, cbpy; |
399 |
|
int mcsel=0; |
400 |
|
|
401 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
402 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
405 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
406 |
mcbpc_inter_tab[mcbpc].len); |
mcbpc_inter_tab[mcbpc].len); |
407 |
|
|
408 |
|
if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) ) |
409 |
|
{ |
410 |
|
if (frame->quarterpel) { |
411 |
|
if ( (pMB->qmvs[0].x == frame->GMC_MV.x) && (pMB->qmvs[0].y == frame->GMC_MV.y) ) |
412 |
|
mcsel=1; |
413 |
|
} else { |
414 |
|
if ( (pMB->mvs[0].x == frame->GMC_MV.x) && (pMB->mvs[0].y == frame->GMC_MV.y) ) |
415 |
|
mcsel=1; |
416 |
|
} |
417 |
|
BitstreamPutBit(bs, mcsel); // mcsel: '0'=local motion, '1'=GMC |
418 |
|
} |
419 |
|
|
420 |
// write cbpy |
// write cbpy |
421 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
422 |
|
|
426 |
|
|
427 |
// interlacing |
// interlacing |
428 |
if (frame->global_flags & XVID_INTERLACING) { |
if (frame->global_flags & XVID_INTERLACING) { |
429 |
|
if (pMB->cbp) { |
430 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
431 |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
432 |
|
} |
433 |
|
|
434 |
// if inter block, write field ME flag |
// if inter block, write field ME flag |
435 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
443 |
} |
} |
444 |
} |
} |
445 |
} |
} |
446 |
// code motion vector(s) |
// code motion vector(s) if motion is local |
447 |
|
if (mcsel==0) |
448 |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
449 |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
450 |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
455 |
// code block coeffs |
// code block coeffs |
456 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) |
457 |
if (pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) |
458 |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
{ |
459 |
|
const uint16_t *scan_table = |
460 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
461 |
|
scan_tables[2] : scan_tables[0]; |
462 |
|
|
463 |
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0); |
464 |
|
} |
465 |
|
|
466 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
467 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
|
|
|
468 |
} |
} |
469 |
|
|
470 |
|
|
471 |
void |
void |
472 |
MBCoding(const FRAMEINFO * frame, |
MBCoding(const FRAMEINFO * const frame, |
473 |
MACROBLOCK * pMB, |
MACROBLOCK * pMB, |
474 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
475 |
Bitstream * bs, |
Bitstream * bs, |
476 |
Statistics * pStat) |
Statistics * pStat) |
477 |
{ |
{ |
478 |
|
if (frame->coding_type != I_VOP) |
479 |
|
BitstreamPutBit(bs, 0); // not_coded |
480 |
|
|
481 |
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) |
|
482 |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
483 |
else |
else |
484 |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
485 |
|
|
486 |
} |
} |
487 |
|
|
488 |
|
/* |
489 |
|
// moved to mbcoding.h so that in can be 'static __inline' |
490 |
|
void |
491 |
|
MBSkip(Bitstream * bs) |
492 |
|
{ |
493 |
|
BitstreamPutBit(bs, 1); // not coded |
494 |
|
} |
495 |
|
*/ |
496 |
|
|
497 |
/*************************************************************** |
/*************************************************************** |
498 |
* bframe encoding start |
* bframe encoding start |
499 |
***************************************************************/ |
***************************************************************/ |
506 |
3 0001b forward mc+q dbquant, mvdf |
3 0001b forward mc+q dbquant, mvdf |
507 |
*/ |
*/ |
508 |
|
|
509 |
void |
static __inline void |
510 |
put_bvop_mbtype(Bitstream * bs, |
put_bvop_mbtype(Bitstream * bs, |
511 |
int value) |
int value) |
512 |
{ |
{ |
513 |
switch (value) { |
switch (value) { |
514 |
case 0: |
case MODE_FORWARD: |
|
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: |
|
515 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
516 |
|
case MODE_BACKWARD: |
517 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
518 |
|
case MODE_INTERPOLATE: |
519 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
520 |
|
case MODE_DIRECT: |
521 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
522 |
return; |
default: |
523 |
|
break; |
|
default:; // invalid! |
|
|
|
|
524 |
} |
} |
|
|
|
525 |
} |
} |
526 |
|
|
527 |
/* |
/* |
531 |
+2 11b |
+2 11b |
532 |
*/ |
*/ |
533 |
|
|
534 |
void |
static __inline void |
535 |
put_bvop_dbquant(Bitstream * bs, |
put_bvop_dbquant(Bitstream * bs, |
536 |
int value) |
int value) |
537 |
{ |
{ |
562 |
const int32_t fcode, |
const int32_t fcode, |
563 |
const int32_t bcode, |
const int32_t bcode, |
564 |
Bitstream * bs, |
Bitstream * bs, |
565 |
Statistics * pStat) |
Statistics * pStat, |
566 |
|
int direction) |
567 |
{ |
{ |
568 |
int i; |
int vcode = fcode; |
569 |
|
unsigned int i; |
570 |
|
|
571 |
/* ------------------------------------------------------------------ |
/* ------------------------------------------------------------------ |
572 |
when a block is skipped it is decoded DIRECT(0,0) |
when a block is skipped it is decoded DIRECT(0,0) |
596 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
597 |
} |
} |
598 |
|
|
599 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
switch (mb->mode) { |
600 |
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
case MODE_INTERPOLATE: |
601 |
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode |
602 |
} |
CodeVector(bs, mb->pmvs[1].y, vcode, pStat); |
603 |
|
case MODE_BACKWARD: |
604 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
vcode = bcode; |
605 |
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
case MODE_FORWARD: |
606 |
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
CodeVector(bs, mb->pmvs[0].x, vcode, pStat); |
607 |
} |
CodeVector(bs, mb->pmvs[0].y, vcode, pStat); |
608 |
|
break; |
609 |
if (mb->mode == MODE_DIRECT) { |
case MODE_DIRECT: |
610 |
CodeVector(bs, mb->mvs[0].x, 1, pStat); /* fcode is always 1 for delta vector */ |
CodeVector(bs, mb->pmvs[3].x, 1, pStat); // fcode is always 1 for delta vector |
611 |
CodeVector(bs, mb->mvs[0].y, 1, pStat); /* prediction is always (0,0) */ |
CodeVector(bs, mb->pmvs[3].y, 1, pStat); // prediction is always (0,0) |
612 |
|
default: break; |
613 |
} |
} |
614 |
|
|
615 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
616 |
if (mb->cbp & (1 << (5 - i))) { |
if (mb->cbp & (1 << (5 - i))) { |
617 |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0); |
618 |
} |
} |
619 |
} |
} |
620 |
} |
} |
697 |
|
|
698 |
} |
} |
699 |
|
|
700 |
int |
static __inline int |
701 |
get_mv_data(Bitstream * bs) |
get_mv_data(Bitstream * bs) |
702 |
{ |
{ |
703 |
|
|
814 |
{ |
{ |
815 |
|
|
816 |
const uint16_t *scan = scan_tables[direction]; |
const uint16_t *scan = scan_tables[direction]; |
817 |
int level; |
int level, run, last; |
|
int run; |
|
|
int last; |
|
818 |
|
|
819 |
do { |
do { |
820 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
838 |
|
|
839 |
void |
void |
840 |
get_inter_block(Bitstream * bs, |
get_inter_block(Bitstream * bs, |
841 |
int16_t * block) |
int16_t * block, |
842 |
|
int direction) |
843 |
{ |
{ |
844 |
|
|
845 |
const uint16_t *scan = scan_tables[0]; |
const uint16_t *scan = scan_tables[direction]; |
846 |
int p; |
int p; |
847 |
int level; |
int level; |
848 |
int run; |
int run; |