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 * |
52 |
|
|
53 |
#include <stdlib.h> |
#include <stdlib.h> |
54 |
#include "../portab.h" |
#include "../portab.h" |
55 |
|
#include "../global.h" |
56 |
#include "bitstream.h" |
#include "bitstream.h" |
57 |
#include "zigzag.h" |
#include "zigzag.h" |
58 |
#include "vlc_codes.h" |
#include "vlc_codes.h" |
60 |
|
|
61 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
62 |
|
|
63 |
#define ABS(X) (((X)>0)?(X):-(X)) |
VLC intra_table[4*2048*64]; |
64 |
#define CLIP(X,A) (X > A) ? (A) : (X) |
VLC inter_table[4*2048*64]; |
|
|
|
|
VLC intra_table[524032]; |
|
|
VLC inter_table[524032]; |
|
65 |
|
|
66 |
VLC DCT3Dintra[4096]; |
VLC DCT3Dintra[4096]; |
67 |
VLC DCT3Dinter[4096]; |
VLC DCT3Dinter[4096]; |
68 |
|
|
69 |
|
/* not really MB related, but VLCs are only available here */ |
70 |
|
void bs_put_spritetrajectory(Bitstream * bs, const int val) |
71 |
|
{ |
72 |
|
const int code = sprite_trajectory_code[val+16384].code; |
73 |
|
const int len = sprite_trajectory_code[val+16384].len; |
74 |
|
const int code2 = sprite_trajectory_len[len].code; |
75 |
|
const int len2 = sprite_trajectory_len[len].len; |
76 |
|
|
77 |
|
// printf("GMC=%d Code/Len = %d / %d ",val, code,len); |
78 |
|
// printf("Code2 / Len2 = %d / %d \n",code2,len2); |
79 |
|
|
80 |
|
BitstreamPutBits(bs, code2, len2); |
81 |
|
if (len) BitstreamPutBits(bs, code, len); |
82 |
|
} |
83 |
|
|
84 |
|
int bs_get_spritetrajectory(Bitstream * bs) |
85 |
|
{ |
86 |
|
int i; |
87 |
|
for (i = 0; i < 12; i++) |
88 |
|
{ |
89 |
|
if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code) |
90 |
|
{ |
91 |
|
BitstreamSkip(bs, sprite_trajectory_len[i].len); |
92 |
|
return i; |
93 |
|
} |
94 |
|
} |
95 |
|
return -1; |
96 |
|
} |
97 |
|
|
98 |
void |
void |
99 |
init_vlc_tables(void) |
init_vlc_tables(void) |
100 |
{ |
{ |
211 |
DCT3D[0] = DCT3Dinter; |
DCT3D[0] = DCT3Dinter; |
212 |
DCT3D[1] = DCT3Dintra; |
DCT3D[1] = DCT3Dintra; |
213 |
|
|
214 |
|
|
215 |
|
/* init sprite_trajectory tables */ |
216 |
|
/* even if GMC is not specified (it might be used later...) */ |
217 |
|
|
218 |
|
sprite_trajectory_code[0+16384].code = 0; |
219 |
|
sprite_trajectory_code[0+16384].len = 0; |
220 |
|
for (k=0;k<14;k++) |
221 |
|
{ |
222 |
|
int limit = (1<<k); |
223 |
|
|
224 |
|
for (i=-(2*limit-1); i<= -limit; i++) |
225 |
|
{ |
226 |
|
sprite_trajectory_code[i+16384].code = (2*limit-1)+i; |
227 |
|
sprite_trajectory_code[i+16384].len = k+1; |
228 |
|
} |
229 |
|
|
230 |
|
for (i=limit; i<= 2*limit-1; i++) |
231 |
|
{ |
232 |
|
sprite_trajectory_code[i+16384].code = i; |
233 |
|
sprite_trajectory_code[i+16384].len = k+1; |
234 |
|
} |
235 |
|
} |
236 |
} |
} |
237 |
|
|
238 |
static __inline void |
static __inline void |
331 |
|
|
332 |
|
|
333 |
static __inline void |
static __inline void |
334 |
CodeBlockIntra(const FRAMEINFO * frame, |
CodeBlockIntra(const FRAMEINFO * const frame, |
335 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
336 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
337 |
Bitstream * bs, |
Bitstream * bs, |
380 |
dcc_tab[qcoeff[i * 64 + 0] + 255].len); |
dcc_tab[qcoeff[i * 64 + 0] + 255].len); |
381 |
|
|
382 |
if (pMB->cbp & (1 << (5 - i))) { |
if (pMB->cbp & (1 << (5 - i))) { |
383 |
|
const uint16_t *scan_table = |
384 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
385 |
|
scan_tables[2] : scan_tables[pMB->acpred_directions[i]]; |
386 |
|
|
387 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
388 |
|
|
389 |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1); |
|
scan_tables[pMB->acpred_directions[i]], 1); |
|
390 |
|
|
391 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
392 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
397 |
|
|
398 |
|
|
399 |
static void |
static void |
400 |
CodeBlockInter(const FRAMEINFO * frame, |
CodeBlockInter(const FRAMEINFO * const frame, |
401 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
402 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
403 |
Bitstream * bs, |
Bitstream * bs, |
406 |
|
|
407 |
int32_t i; |
int32_t i; |
408 |
uint32_t bits, mcbpc, cbpy; |
uint32_t bits, mcbpc, cbpy; |
409 |
|
int mcsel=0; |
410 |
|
|
411 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
412 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
415 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
416 |
mcbpc_inter_tab[mcbpc].len); |
mcbpc_inter_tab[mcbpc].len); |
417 |
|
|
418 |
|
if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) ) |
419 |
|
{ |
420 |
|
if (frame->quarterpel) { |
421 |
|
if ( (pMB->qmvs[0].x == frame->GMC_MV.x) && (pMB->qmvs[0].y == frame->GMC_MV.y) ) |
422 |
|
mcsel=1; |
423 |
|
} else { |
424 |
|
if ( (pMB->mvs[0].x == frame->GMC_MV.x) && (pMB->mvs[0].y == frame->GMC_MV.y) ) |
425 |
|
mcsel=1; |
426 |
|
} |
427 |
|
BitstreamPutBit(bs, mcsel); // mcsel: '0'=local motion, '1'=GMC |
428 |
|
} |
429 |
|
|
430 |
// write cbpy |
// write cbpy |
431 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
432 |
|
|
438 |
if (frame->global_flags & XVID_INTERLACING) { |
if (frame->global_flags & XVID_INTERLACING) { |
439 |
if (pMB->cbp) { |
if (pMB->cbp) { |
440 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
441 |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct); |
442 |
} |
} |
443 |
|
|
444 |
// if inter block, write field ME flag |
// if inter block, write field ME flag |
445 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
446 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, pMB->field_pred); |
447 |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred); |
448 |
|
|
449 |
// write field prediction references |
// write field prediction references |
450 |
if (pMB->field_pred) { |
if (pMB->field_pred) { |
453 |
} |
} |
454 |
} |
} |
455 |
} |
} |
456 |
// code motion vector(s) |
// code motion vector(s) if motion is local |
457 |
|
if (mcsel==0) |
458 |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
459 |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
460 |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
465 |
// code block coeffs |
// code block coeffs |
466 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) |
467 |
if (pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) |
468 |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
{ |
469 |
|
const uint16_t *scan_table = |
470 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
471 |
|
scan_tables[2] : scan_tables[0]; |
472 |
|
|
473 |
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0); |
474 |
|
} |
475 |
|
|
476 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
477 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
|
|
|
478 |
} |
} |
479 |
|
|
480 |
|
|
481 |
void |
void |
482 |
MBCoding(const FRAMEINFO * frame, |
MBCoding(const FRAMEINFO * const frame, |
483 |
MACROBLOCK * pMB, |
MACROBLOCK * pMB, |
484 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
485 |
Bitstream * bs, |
Bitstream * bs, |
486 |
Statistics * pStat) |
Statistics * pStat) |
487 |
{ |
{ |
488 |
|
if (frame->coding_type != I_VOP) |
489 |
if (frame->coding_type == P_VOP) { |
BitstreamPutBit(bs, 0); // not_coded |
|
BitstreamPutBit(bs, 0); // coded |
|
|
} |
|
490 |
|
|
491 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
492 |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
572 |
const int32_t fcode, |
const int32_t fcode, |
573 |
const int32_t bcode, |
const int32_t bcode, |
574 |
Bitstream * bs, |
Bitstream * bs, |
575 |
Statistics * pStat) |
Statistics * pStat, |
576 |
|
int direction) |
577 |
{ |
{ |
578 |
int vcode = fcode; |
int vcode = fcode; |
579 |
unsigned int i; |
unsigned int i; |
624 |
|
|
625 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
626 |
if (mb->cbp & (1 << (5 - i))) { |
if (mb->cbp & (1 << (5 - i))) { |
627 |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0); |
628 |
} |
} |
629 |
} |
} |
630 |
} |
} |
681 |
|
|
682 |
uint32_t index; |
uint32_t index; |
683 |
|
|
684 |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
index = MIN(BitstreamShowBits(bs, 9), 256); |
685 |
|
|
686 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
687 |
|
|
829 |
do { |
do { |
830 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
831 |
if (run == -1) { |
if (run == -1) { |
832 |
DEBUG("fatal: invalid run"); |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
833 |
break; |
break; |
834 |
} |
} |
835 |
coeff += run; |
coeff += run; |
838 |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
839 |
//DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); |
//DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); |
840 |
|
|
841 |
if (level < -127 || level > 127) { |
if (level < -2047 || level > 2047) { |
842 |
DEBUG1("warning: intra_overflow", level); |
DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level); |
843 |
} |
} |
844 |
coeff++; |
coeff++; |
845 |
} while (!last); |
} while (!last); |
848 |
|
|
849 |
void |
void |
850 |
get_inter_block(Bitstream * bs, |
get_inter_block(Bitstream * bs, |
851 |
int16_t * block) |
int16_t * block, |
852 |
|
int direction) |
853 |
{ |
{ |
854 |
|
|
855 |
const uint16_t *scan = scan_tables[0]; |
const uint16_t *scan = scan_tables[direction]; |
856 |
int p; |
int p; |
857 |
int level; |
int level; |
858 |
int run; |
int run; |
862 |
do { |
do { |
863 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
864 |
if (run == -1) { |
if (run == -1) { |
865 |
DEBUG("fatal: invalid run"); |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
866 |
break; |
break; |
867 |
} |
} |
868 |
p += run; |
p += run; |
872 |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
873 |
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
874 |
|
|
875 |
if (level < -127 || level > 127) { |
if (level < -2047 || level > 2047) { |
876 |
DEBUG1("warning: inter_overflow", level); |
DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level); |
877 |
} |
} |
878 |
p++; |
p++; |
879 |
} while (!last); |
} while (!last); |