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