1 |
/****************************************************************************** |
/***************************************************************************** |
2 |
* * |
* |
3 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder * |
* XVID MPEG-4 VIDEO CODEC |
4 |
* * |
* - Macro Block coding functions - |
5 |
* XviD is an implementation of a part of one or more MPEG-4 Video tools * |
* |
6 |
* as specified in ISO/IEC 14496-2 standard. Those intending to use this * |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 |
* software module in hardware or software products are advised that its * |
* |
8 |
* use may infringe existing patents or copyrights, and any such use * |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
9 |
* would be at such party's own risk. The original developer of this * |
* |
10 |
* software module and his/her company, and subsequent editors and their * |
* XviD is free software; you can redistribute it and/or modify it |
11 |
* companies, will have no liability for use of this software or * |
* under the terms of the GNU General Public License as published by |
12 |
* modifications or derivatives thereof. * |
* the Free Software Foundation; either version 2 of the License, or |
13 |
* * |
* (at your option) any later version. |
14 |
* XviD is free software; you can redistribute it and/or modify it * |
* |
15 |
* under the terms of the GNU General Public License as published by * |
* This program is distributed in the hope that it will be useful, |
16 |
* the Free Software Foundation; either version 2 of the License, or * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* (at your option) any later version. * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
* * |
* GNU General Public License for more details. |
19 |
* XviD is distributed in the hope that it will be useful, but * |
* |
20 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
* You should have received a copy of the GNU General Public License |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* along with this program; if not, write to the Free Software |
22 |
* GNU General Public License for more details. * |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* * |
* |
24 |
* You should have received a copy of the GNU General Public License * |
* Under section 8 of the GNU General Public License, the copyright |
25 |
* along with this program; if not, write to the Free Software * |
* holders of XVID explicitly forbid distribution in the following |
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
* countries: |
27 |
* * |
* |
28 |
******************************************************************************/ |
* - Japan |
29 |
|
* - United States of America |
30 |
/****************************************************************************** |
* |
31 |
* * |
* Linking XviD statically or dynamically with other modules is making a |
32 |
* mbcoding.c * |
* combined work based on XviD. Thus, the terms and conditions of the |
33 |
* * |
* GNU General Public License cover the whole combination. |
34 |
* Copyright (C) 2002 - Michael Militzer <isibaar@xvid.org> * |
* |
35 |
* * |
* As a special exception, the copyright holders of XviD give you |
36 |
* For more information visit the XviD homepage: http://www.xvid.org * |
* permission to link XviD with independent modules that communicate with |
37 |
* * |
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
38 |
******************************************************************************/ |
* license terms of these independent modules, and to copy and distribute |
39 |
|
* the resulting combined work under terms of your choice, provided that |
40 |
/****************************************************************************** |
* every copy of the combined work is accompanied by a complete copy of |
41 |
* * |
* the source code of XviD (the version of XviD used to produce the |
42 |
* Revision history: * |
* combined work), being distributed under the terms of the GNU General |
43 |
* * |
* Public License plus this exception. An independent module is a module |
44 |
* 28.10.2002 GMC support - gruel * |
* which is not derived from or based on XviD. |
45 |
* 28.06.2002 added check_resync_marker() * |
* |
46 |
* 14.04.2002 bframe encoding * |
* Note that people who make modified versions of XviD are not obligated |
47 |
* 08.03.2002 initial version; isibaar * |
* to grant this special exception for their modified versions; it is |
48 |
* * |
* their choice whether to do so. The GNU General Public License gives |
49 |
******************************************************************************/ |
* permission to release a modified version without this exception; this |
50 |
|
* exception also makes it possible to release a modified version which |
51 |
|
* carries forward this exception. |
52 |
|
* |
53 |
|
* $Id$ |
54 |
|
* |
55 |
|
****************************************************************************/ |
56 |
|
|
|
|
|
|
#include <stdio.h> |
|
57 |
#include <stdlib.h> |
#include <stdlib.h> |
58 |
#include "../portab.h" |
#include "../portab.h" |
|
#include "../global.h" |
|
59 |
#include "bitstream.h" |
#include "bitstream.h" |
60 |
#include "zigzag.h" |
#include "zigzag.h" |
61 |
#include "vlc_codes.h" |
#include "vlc_codes.h" |
63 |
|
|
64 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
65 |
|
|
66 |
|
#define ABS(X) (((X)>0)?(X):-(X)) |
67 |
|
#define CLIP(X,A) (X > A) ? (A) : (X) |
68 |
|
|
69 |
/* #define BIGLUT */ |
/* #define BIGLUT */ |
70 |
|
|
71 |
#ifdef BIGLUT |
#ifdef BIGLUT |
74 |
#define LEVELOFFSET 32 |
#define LEVELOFFSET 32 |
75 |
#endif |
#endif |
76 |
|
|
77 |
|
/***************************************************************************** |
78 |
|
* Local data |
79 |
|
****************************************************************************/ |
80 |
|
|
81 |
static REVERSE_EVENT DCT3D[2][4096]; |
static REVERSE_EVENT DCT3D[2][4096]; |
82 |
|
|
83 |
#ifdef BIGLUT |
#ifdef BIGLUT |
87 |
static VLC coeff_VLC[2][2][64][64]; |
static VLC coeff_VLC[2][2][64][64]; |
88 |
#endif |
#endif |
89 |
|
|
90 |
/* not really MB related, but VLCs are only available here */ |
/***************************************************************************** |
91 |
void bs_put_spritetrajectory(Bitstream * bs, const int val) |
* Vector Length Coding Initialization |
92 |
{ |
****************************************************************************/ |
|
const int code = sprite_trajectory_code[val+16384].code; |
|
|
const int len = sprite_trajectory_code[val+16384].len; |
|
|
const int code2 = sprite_trajectory_len[len].code; |
|
|
const int len2 = sprite_trajectory_len[len].len; |
|
|
|
|
|
// printf("GMC=%d Code/Len = %d / %d ",val, code,len); |
|
|
// printf("Code2 / Len2 = %d / %d \n",code2,len2); |
|
|
|
|
|
BitstreamPutBits(bs, code2, len2); |
|
|
if (len) BitstreamPutBits(bs, code, len); |
|
|
} |
|
|
|
|
|
int bs_get_spritetrajectory(Bitstream * bs) |
|
|
{ |
|
|
int i; |
|
|
for (i = 0; i < 12; i++) |
|
|
{ |
|
|
if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code) |
|
|
{ |
|
|
BitstreamSkip(bs, sprite_trajectory_len[i].len); |
|
|
return i; |
|
|
} |
|
|
} |
|
|
return -1; |
|
|
} |
|
93 |
|
|
94 |
void |
void |
95 |
init_vlc_tables(void) |
init_vlc_tables(void) |
96 |
{ |
{ |
97 |
ptr_t i, j, k, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; |
uint32_t i, j, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; |
|
int32_t l; |
|
98 |
|
|
99 |
#ifdef BIGLUT |
#ifdef BIGLUT |
100 |
intra_table = coeff_VLC[1]; |
intra_table = (VLC*)coeff_VLC[1]; |
101 |
inter_table = coeff_VLC[0]; |
inter_table = (VLC*)coeff_VLC[0]; |
102 |
#endif |
#endif |
103 |
|
|
104 |
|
|
239 |
} |
} |
240 |
#endif |
#endif |
241 |
} |
} |
|
/* init sprite_trajectory tables */ |
|
|
/* even if GMC is not specified (it might be used later...) */ |
|
|
|
|
|
sprite_trajectory_code[0+16384].code = 0; |
|
|
sprite_trajectory_code[0+16384].len = 0; |
|
|
for (k=0;k<14;k++) |
|
|
{ |
|
|
int limit = (1<<k); |
|
|
|
|
|
for (l=-(2*limit-1); l <= -limit; l++) |
|
|
{ |
|
|
sprite_trajectory_code[l+16384].code = (2*limit-1)+l; |
|
|
sprite_trajectory_code[l+16384].len = k+1; |
|
242 |
} |
} |
243 |
|
|
244 |
for (l=limit; l<= 2*limit-1; l++) |
/***************************************************************************** |
245 |
{ |
* Local inlined functions for MB coding |
246 |
sprite_trajectory_code[l+16384].code = l; |
****************************************************************************/ |
|
sprite_trajectory_code[l+16384].len = k+1; |
|
|
} |
|
|
} |
|
|
} |
|
247 |
|
|
248 |
static __inline void |
static __inline void |
249 |
CodeVector(Bitstream * bs, |
CodeVector(Bitstream * bs, |
456 |
|
|
457 |
#endif |
#endif |
458 |
|
|
459 |
static __inline void |
/***************************************************************************** |
460 |
CodeBlockIntra(const FRAMEINFO * const frame, |
* Local functions |
461 |
|
****************************************************************************/ |
462 |
|
|
463 |
|
static void |
464 |
|
CodeBlockIntra(const FRAMEINFO * frame, |
465 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
466 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
467 |
Bitstream * bs, |
Bitstream * bs, |
472 |
|
|
473 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
474 |
|
|
475 |
// write mcbpc |
/* write mcbpc */ |
476 |
if (frame->coding_type == I_VOP) { |
if (frame->coding_type == I_VOP) { |
477 |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
478 |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
483 |
mcbpc_inter_tab[mcbpc].len); |
mcbpc_inter_tab[mcbpc].len); |
484 |
} |
} |
485 |
|
|
486 |
// ac prediction flag |
/* ac prediction flag */ |
487 |
if (pMB->acpred_directions[0]) |
if (pMB->acpred_directions[0]) |
488 |
BitstreamPutBits(bs, 1, 1); |
BitstreamPutBits(bs, 1, 1); |
489 |
else |
else |
490 |
BitstreamPutBits(bs, 0, 1); |
BitstreamPutBits(bs, 0, 1); |
491 |
|
|
492 |
// write cbpy |
/* write cbpy */ |
493 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
494 |
|
|
495 |
// write dquant |
/* write dquant */ |
496 |
if (pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA_Q) |
497 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
498 |
|
|
499 |
// write interlacing |
/* write interlacing */ |
500 |
if (frame->global_flags & XVID_INTERLACING) { |
if (frame->global_flags & XVID_INTERLACING) { |
501 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
502 |
} |
} |
503 |
// code block coeffs |
/* code block coeffs */ |
504 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
505 |
if (i < 4) |
if (i < 4) |
506 |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
510 |
dcc_tab[qcoeff[i * 64 + 0] + 255].len); |
dcc_tab[qcoeff[i * 64 + 0] + 255].len); |
511 |
|
|
512 |
if (pMB->cbp & (1 << (5 - i))) { |
if (pMB->cbp & (1 << (5 - i))) { |
|
const uint16_t *scan_table = |
|
|
frame->global_flags & XVID_ALTERNATESCAN ? |
|
|
scan_tables[2] : scan_tables[pMB->acpred_directions[i]]; |
|
|
|
|
513 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
514 |
|
|
515 |
#ifdef BIGLUT |
#ifdef BIGLUT |
516 |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1); |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
517 |
|
scan_tables[pMB->acpred_directions[i]], 1); |
518 |
#else |
#else |
519 |
CodeCoeffIntra(bs, &qcoeff[i * 64], scan_table); |
CodeCoeffIntra(bs, &qcoeff[i * 64], scan_tables[pMB->acpred_directions[i]]); |
520 |
#endif |
#endif |
|
|
|
521 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
522 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
523 |
} |
} |
527 |
|
|
528 |
|
|
529 |
static void |
static void |
530 |
CodeBlockInter(const FRAMEINFO * const frame, |
CodeBlockInter(const FRAMEINFO * frame, |
531 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
532 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
533 |
Bitstream * bs, |
Bitstream * bs, |
540 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
541 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
542 |
|
|
543 |
// write mcbpc |
/* write mcbpc */ |
544 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
545 |
mcbpc_inter_tab[mcbpc].len); |
mcbpc_inter_tab[mcbpc].len); |
546 |
|
|
547 |
if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) ) |
/* write cbpy */ |
|
BitstreamPutBit(bs, pMB->mcsel); // mcsel: '0'=local motion, '1'=GMC |
|
|
|
|
|
// write cbpy |
|
548 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
549 |
|
|
550 |
// write dquant |
/* write dquant */ |
551 |
if (pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER_Q) |
552 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
553 |
|
|
554 |
// interlacing |
/* interlacing */ |
555 |
if (frame->global_flags & XVID_INTERLACING) { |
if (frame->global_flags & XVID_INTERLACING) { |
556 |
if (pMB->cbp) { |
if (pMB->cbp) { |
557 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
558 |
DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct); |
DPRINTF(DPRINTF_DEBUG, "codep: field_dct: %d", pMB->field_dct); |
559 |
} |
} |
560 |
|
|
561 |
// if inter block, write field ME flag |
/* if inter block, write field ME flag */ |
562 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
563 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, pMB->field_pred); |
564 |
DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred); |
DPRINTF(DPRINTF_DEBUG, "codep: field_pred: %d", pMB->field_pred); |
565 |
|
|
566 |
// write field prediction references |
/* write field prediction references */ |
567 |
if (pMB->field_pred) { |
if (pMB->field_pred) { |
568 |
BitstreamPutBit(bs, pMB->field_for_top); |
BitstreamPutBit(bs, pMB->field_for_top); |
569 |
BitstreamPutBit(bs, pMB->field_for_bot); |
BitstreamPutBit(bs, pMB->field_for_bot); |
570 |
} |
} |
571 |
} |
} |
572 |
} |
} |
573 |
// code motion vector(s) if motion is local |
/* code motion vector(s) */ |
|
if (!pMB->mcsel) |
|
574 |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
575 |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
576 |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
578 |
|
|
579 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
580 |
|
|
581 |
// code block coeffs |
/* code block coeffs */ |
582 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) |
583 |
if (pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) |
|
{ |
|
|
const uint16_t *scan_table = |
|
|
frame->global_flags & XVID_ALTERNATESCAN ? |
|
|
scan_tables[2] : scan_tables[0]; |
|
|
|
|
584 |
#ifdef BIGLUT |
#ifdef BIGLUT |
585 |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0); |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
586 |
#else |
#else |
587 |
CodeCoeffInter(bs, &qcoeff[i * 64], scan_table); |
CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]); |
588 |
#endif |
#endif |
|
} |
|
589 |
|
|
590 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
591 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
592 |
|
|
593 |
} |
} |
594 |
|
|
595 |
|
/***************************************************************************** |
596 |
|
* Macro Block bitstream encoding functions |
597 |
|
****************************************************************************/ |
598 |
|
|
599 |
void |
void |
600 |
MBCoding(const FRAMEINFO * const frame, |
MBCoding(const FRAMEINFO * frame, |
601 |
MACROBLOCK * pMB, |
MACROBLOCK * pMB, |
602 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
603 |
Bitstream * bs, |
Bitstream * bs, |
604 |
Statistics * pStat) |
Statistics * pStat) |
605 |
{ |
{ |
606 |
if (frame->coding_type != I_VOP) |
|
607 |
BitstreamPutBit(bs, 0); // not_coded |
if (frame->coding_type == P_VOP) { |
608 |
|
BitstreamPutBit(bs, 0); /* coded */ |
609 |
|
} |
610 |
|
|
611 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
612 |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
615 |
|
|
616 |
} |
} |
617 |
|
|
618 |
/* |
|
|
// moved to mbcoding.h so that in can be 'static __inline' |
|
619 |
void |
void |
620 |
MBSkip(Bitstream * bs) |
MBSkip(Bitstream * bs) |
621 |
{ |
{ |
622 |
BitstreamPutBit(bs, 1); // not coded |
BitstreamPutBit(bs, 1); /* not coded */ |
623 |
|
return; |
624 |
} |
} |
|
*/ |
|
625 |
|
|
626 |
/*************************************************************** |
/***************************************************************************** |
627 |
* bframe encoding start |
* decoding stuff starts here |
628 |
***************************************************************/ |
****************************************************************************/ |
629 |
|
|
630 |
/* |
/* |
631 |
mbtype |
* For IVOP addbits == 0 |
632 |
0 1b direct(h263) mvdb |
* For PVOP addbits == fcode - 1 |
633 |
1 01b interpolate mc+q dbquant, mvdf, mvdb |
* For BVOP addbits == max(fcode,bcode) - 1 |
634 |
2 001b backward mc+q dbquant, mvdb |
* returns true or false |
|
3 0001b forward mc+q dbquant, mvdf |
|
635 |
*/ |
*/ |
636 |
|
|
|
static __inline void |
|
|
put_bvop_mbtype(Bitstream * bs, |
|
|
int value) |
|
|
{ |
|
|
switch (value) { |
|
|
case MODE_FORWARD: |
|
|
BitstreamPutBit(bs, 0); |
|
|
case MODE_BACKWARD: |
|
|
BitstreamPutBit(bs, 0); |
|
|
case MODE_INTERPOLATE: |
|
|
BitstreamPutBit(bs, 0); |
|
|
case MODE_DIRECT: |
|
|
BitstreamPutBit(bs, 1); |
|
|
default: |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
/* |
|
|
dbquant |
|
|
-2 10b |
|
|
0 0b |
|
|
+2 11b |
|
|
*/ |
|
|
|
|
|
static __inline void |
|
|
put_bvop_dbquant(Bitstream * bs, |
|
|
int value) |
|
|
{ |
|
|
switch (value) { |
|
|
case 0: |
|
|
BitstreamPutBit(bs, 0); |
|
|
return; |
|
|
|
|
|
case -2: |
|
|
BitstreamPutBit(bs, 1); |
|
|
BitstreamPutBit(bs, 0); |
|
|
return; |
|
|
|
|
|
case 2: |
|
|
BitstreamPutBit(bs, 1); |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
default:; // invalid |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
MBCodingBVOP(const MACROBLOCK * mb, |
|
|
const int16_t qcoeff[6 * 64], |
|
|
const int32_t fcode, |
|
|
const int32_t bcode, |
|
|
Bitstream * bs, |
|
|
Statistics * pStat, |
|
|
int direction) |
|
|
{ |
|
|
int vcode = fcode; |
|
|
unsigned int i; |
|
|
|
|
|
/* ------------------------------------------------------------------ |
|
|
when a block is skipped it is decoded DIRECT(0,0) |
|
|
hence is interpolated from forward & backward frames |
|
|
------------------------------------------------------------------ */ |
|
|
|
|
|
if (mb->mode == MODE_DIRECT_NONE_MV) { |
|
|
BitstreamPutBit(bs, 1); // skipped |
|
|
return; |
|
|
} |
|
|
|
|
|
BitstreamPutBit(bs, 0); // not skipped |
|
|
|
|
|
if (mb->cbp == 0) { |
|
|
BitstreamPutBit(bs, 1); // cbp == 0 |
|
|
} else { |
|
|
BitstreamPutBit(bs, 0); // cbp == xxx |
|
|
} |
|
|
|
|
|
put_bvop_mbtype(bs, mb->mode); |
|
|
|
|
|
if (mb->cbp) { |
|
|
BitstreamPutBits(bs, mb->cbp, 6); |
|
|
} |
|
|
|
|
|
if (mb->mode != MODE_DIRECT && mb->cbp != 0) { |
|
|
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
|
|
} |
|
|
|
|
|
switch (mb->mode) { |
|
|
case MODE_INTERPOLATE: |
|
|
CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode |
|
|
CodeVector(bs, mb->pmvs[1].y, vcode, pStat); |
|
|
case MODE_BACKWARD: |
|
|
vcode = bcode; |
|
|
case MODE_FORWARD: |
|
|
CodeVector(bs, mb->pmvs[0].x, vcode, pStat); |
|
|
CodeVector(bs, mb->pmvs[0].y, vcode, pStat); |
|
|
break; |
|
|
case MODE_DIRECT: |
|
|
CodeVector(bs, mb->pmvs[3].x, 1, pStat); // fcode is always 1 for delta vector |
|
|
CodeVector(bs, mb->pmvs[3].y, 1, pStat); // prediction is always (0,0) |
|
|
default: break; |
|
|
} |
|
|
|
|
|
for (i = 0; i < 6; i++) { |
|
|
if (mb->cbp & (1 << (5 - i))) { |
|
|
#ifdef BIGLUT |
|
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
|
|
#else |
|
|
CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]); |
|
|
#endif |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************** |
|
|
* decoding stuff starts here * |
|
|
***************************************************************/ |
|
|
|
|
|
|
|
|
// for IVOP addbits == 0 |
|
|
// for PVOP addbits == fcode - 1 |
|
|
// for BVOP addbits == max(fcode,bcode) - 1 |
|
|
// returns true or false |
|
637 |
int |
int |
638 |
check_resync_marker(Bitstream * bs, int addbits) |
check_resync_marker(Bitstream * bs, int addbits) |
639 |
{ |
{ |
675 |
|
|
676 |
uint32_t index; |
uint32_t index; |
677 |
|
|
678 |
index = MIN(BitstreamShowBits(bs, 9), 256); |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
679 |
|
|
680 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
681 |
|
|
701 |
|
|
702 |
} |
} |
703 |
|
|
704 |
static __inline int |
int |
705 |
get_mv_data(Bitstream * bs) |
get_mv_data(Bitstream * bs) |
706 |
{ |
{ |
707 |
|
|
810 |
|
|
811 |
} |
} |
812 |
|
|
813 |
|
/***************************************************************************** |
814 |
|
* Local inlined function to "decode" written vlc codes |
815 |
|
****************************************************************************/ |
816 |
|
|
817 |
static __inline int |
static __inline int |
818 |
get_coeff(Bitstream * bs, |
get_coeff(Bitstream * bs, |
819 |
int *run, |
int *run, |
895 |
return 0; |
return 0; |
896 |
} |
} |
897 |
|
|
898 |
|
/***************************************************************************** |
899 |
|
* MB reading functions |
900 |
|
****************************************************************************/ |
901 |
|
|
902 |
void |
void |
903 |
get_intra_block(Bitstream * bs, |
get_intra_block(Bitstream * bs, |
904 |
int16_t * block, |
int16_t * block, |
907 |
{ |
{ |
908 |
|
|
909 |
const uint16_t *scan = scan_tables[direction]; |
const uint16_t *scan = scan_tables[direction]; |
910 |
int level, run, last; |
int level; |
911 |
|
int run; |
912 |
|
int last; |
913 |
|
|
914 |
do { |
do { |
915 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
916 |
if (run == -1) { |
if (run == -1) { |
917 |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
DPRINTF(DPRINTF_DEBUG, "fatal: invalid run"); |
918 |
break; |
break; |
919 |
} |
} |
920 |
coeff += run; |
coeff += run; |
921 |
block[scan[coeff]] = level; |
block[scan[coeff]] = level; |
922 |
|
|
923 |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
924 |
//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)); */ |
925 |
|
|
926 |
if (level < -2047 || level > 2047) { |
if (level < -2047 || level > 2047) { |
927 |
DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level); |
DPRINTF(DPRINTF_DEBUG, "warning: intra_overflow: %d", level); |
928 |
} |
} |
929 |
coeff++; |
coeff++; |
930 |
} while (!last); |
} while (!last); |
933 |
|
|
934 |
void |
void |
935 |
get_inter_block(Bitstream * bs, |
get_inter_block(Bitstream * bs, |
936 |
int16_t * block, |
int16_t * block) |
|
int direction) |
|
937 |
{ |
{ |
938 |
|
|
939 |
const uint16_t *scan = scan_tables[direction]; |
const uint16_t *scan = scan_tables[0]; |
940 |
int p; |
int p; |
941 |
int level; |
int level; |
942 |
int run; |
int run; |
954 |
block[scan[p]] = level; |
block[scan[p]] = level; |
955 |
|
|
956 |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
|
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
|
957 |
|
|
958 |
if (level < -2047 || level > 2047) { |
if (level < -2047 || level > 2047) { |
959 |
DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level); |
DPRINTF(DPRINTF_DEBUG, "warning: inter_overflow: %d", level); |
960 |
} |
} |
961 |
p++; |
p++; |
962 |
} while (!last); |
} while (!last); |