1 |
/***************************************************************************** |
/****************************************************************************** |
2 |
* |
* * |
3 |
* XVID MPEG-4 VIDEO CODEC |
* This file is part of XviD, a free MPEG-4 video encoder/decoder * |
4 |
* - Macro Block coding functions - |
* * |
5 |
* |
* XviD is an implementation of a part of one or more MPEG-4 Video tools * |
6 |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
* as specified in ISO/IEC 14496-2 standard. Those intending to use this * |
7 |
* |
* software module in hardware or software products are advised that its * |
8 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
* use may infringe existing patents or copyrights, and any such use * |
9 |
* |
* would be at such party's own risk. The original developer of this * |
10 |
* XviD is free software; you can redistribute it and/or modify it |
* software module and his/her company, and subsequent editors and their * |
11 |
* under the terms of the GNU General Public License as published by |
* companies, will have no liability for use of this software or * |
12 |
* the Free Software Foundation; either version 2 of the License, or |
* modifications or derivatives thereof. * |
13 |
* (at your option) any later version. |
* * |
14 |
* |
* XviD is free software; you can redistribute it and/or modify it * |
15 |
* This program is distributed in the hope that it will be useful, |
* under the terms of the GNU General Public License as published by * |
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* the Free Software Foundation; either version 2 of the License, or * |
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* (at your option) any later version. * |
18 |
* GNU General Public License for more details. |
* * |
19 |
* |
* XviD is distributed in the hope that it will be useful, but * |
20 |
* You should have received a copy of the GNU General Public License |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
21 |
* along with this program; if not, write to the Free Software |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* GNU General Public License for more details. * |
23 |
* |
* * |
24 |
* Under section 8 of the GNU General Public License, the copyright |
* You should have received a copy of the GNU General Public License * |
25 |
* holders of XVID explicitly forbid distribution in the following |
* along with this program; if not, write to the Free Software * |
26 |
* countries: |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
27 |
* |
* * |
28 |
* - Japan |
******************************************************************************/ |
29 |
* - United States of America |
|
30 |
* |
/****************************************************************************** |
31 |
* Linking XviD statically or dynamically with other modules is making a |
* * |
32 |
* combined work based on XviD. Thus, the terms and conditions of the |
* mbcoding.c * |
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 |
* permission to link XviD with independent modules that communicate with |
* For more information visit the XviD homepage: http://www.xvid.org * |
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 |
* combined work), being distributed under the terms of the GNU General |
* Revision history: * |
43 |
* Public License plus this exception. An independent module is a module |
* * |
44 |
* which is not derived from or based on XviD. |
* 28.10.2002 GMC support - gruel * |
45 |
* |
* 28.06.2002 added check_resync_marker() * |
46 |
* Note that people who make modified versions of XviD are not obligated |
* 14.04.2002 bframe encoding * |
47 |
* to grant this special exception for their modified versions; it is |
* 08.03.2002 initial version; isibaar * |
48 |
* their choice whether to do so. The GNU General Public License gives |
* * |
49 |
* permission to release a modified version without this exception; this |
******************************************************************************/ |
|
* exception also makes it possible to release a modified version which |
|
|
* carries forward this exception. |
|
|
* |
|
|
* $Id$ |
|
|
* |
|
|
****************************************************************************/ |
|
50 |
|
|
51 |
|
|
52 |
|
#include <stdio.h> |
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 |
|
|
|
#define ABS(X) (((X)>0)?(X):-(X)) |
|
|
#define CLIP(X,A) (X > A) ? (A) : (X) |
|
|
|
|
63 |
/* #define BIGLUT */ |
/* #define BIGLUT */ |
64 |
|
|
65 |
#ifdef BIGLUT |
#ifdef BIGLUT |
68 |
#define LEVELOFFSET 32 |
#define LEVELOFFSET 32 |
69 |
#endif |
#endif |
70 |
|
|
|
/***************************************************************************** |
|
|
* Local data |
|
|
****************************************************************************/ |
|
|
|
|
71 |
static REVERSE_EVENT DCT3D[2][4096]; |
static REVERSE_EVENT DCT3D[2][4096]; |
72 |
|
|
73 |
#ifdef BIGLUT |
#ifdef BIGLUT |
74 |
static VLC coeff_VLC[2][2][4096][64]; |
static VLC coeff_VLC[2][2][4096][64]; |
75 |
static VLC *intra_table, *inter_table; |
VLC *intra_table; |
76 |
|
static VLC *inter_table; |
77 |
#else |
#else |
78 |
static VLC coeff_VLC[2][2][64][64]; |
static VLC coeff_VLC[2][2][64][64]; |
79 |
#endif |
#endif |
80 |
|
|
81 |
/***************************************************************************** |
/* not really MB related, but VLCs are only available here */ |
82 |
* Vector Length Coding Initialization |
void bs_put_spritetrajectory(Bitstream * bs, const int val) |
83 |
****************************************************************************/ |
{ |
84 |
|
const int code = sprite_trajectory_code[val+16384].code; |
85 |
|
const int len = sprite_trajectory_code[val+16384].len; |
86 |
|
const int code2 = sprite_trajectory_len[len].code; |
87 |
|
const int len2 = sprite_trajectory_len[len].len; |
88 |
|
|
89 |
|
// printf("GMC=%d Code/Len = %d / %d ",val, code,len); |
90 |
|
// printf("Code2 / Len2 = %d / %d \n",code2,len2); |
91 |
|
|
92 |
|
BitstreamPutBits(bs, code2, len2); |
93 |
|
if (len) BitstreamPutBits(bs, code, len); |
94 |
|
} |
95 |
|
|
96 |
|
int bs_get_spritetrajectory(Bitstream * bs) |
97 |
|
{ |
98 |
|
int i; |
99 |
|
for (i = 0; i < 12; i++) |
100 |
|
{ |
101 |
|
if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code) |
102 |
|
{ |
103 |
|
BitstreamSkip(bs, sprite_trajectory_len[i].len); |
104 |
|
return i; |
105 |
|
} |
106 |
|
} |
107 |
|
return -1; |
108 |
|
} |
109 |
|
|
110 |
void |
void |
111 |
init_vlc_tables(void) |
init_vlc_tables(void) |
112 |
{ |
{ |
113 |
uint32_t i, j, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; |
uint32_t i, j, k, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; |
114 |
|
int32_t l; |
115 |
|
|
116 |
#ifdef BIGLUT |
#ifdef BIGLUT |
117 |
intra_table = (VLC*)coeff_VLC[1]; |
intra_table = coeff_VLC[1]; |
118 |
inter_table = (VLC*)coeff_VLC[0]; |
inter_table = coeff_VLC[0]; |
119 |
#endif |
#endif |
120 |
|
|
121 |
|
|
127 |
for (last = 0; last < 2; last++) |
for (last = 0; last < 2; last++) |
128 |
{ |
{ |
129 |
for (run = 0; run < 63 + last; run++) |
for (run = 0; run < 63 + last; run++) |
130 |
for (level = 0; level < 32 << intra; level++) |
for (level = 0; level < (uint32_t)(32 << intra); level++) |
131 |
{ |
{ |
132 |
#ifdef BIGLUT |
#ifdef BIGLUT |
133 |
offset = LEVELOFFSET; |
offset = LEVELOFFSET; |
183 |
#endif |
#endif |
184 |
level_esc = level - max_level[intra][last][run]; |
level_esc = level - max_level[intra][last][run]; |
185 |
run_esc = run - 1 - max_run[intra][last][level]; |
run_esc = run - 1 - max_run[intra][last][level]; |
186 |
|
/*use this test to use shorter esc2 codes when possible |
187 |
|
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc] |
188 |
|
&& !(coeff_VLC[intra][last][level_esc + offset][run].len + 7 + 1 |
189 |
|
> coeff_VLC[intra][last][level + offset][run_esc].code + 7 + 2))*/ |
190 |
|
|
191 |
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]) |
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]) |
192 |
{ |
{ |
237 |
} |
} |
238 |
|
|
239 |
#ifdef BIGLUT |
#ifdef BIGLUT |
240 |
for (level = (uint32_t)(32 << intra); level < 2048; level++) |
for (level = 32 << intra; level < 2048; level++) |
241 |
{ |
{ |
242 |
coeff_VLC[intra][last][level + offset][run].code |
coeff_VLC[intra][last][level + offset][run].code |
243 |
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
256 |
} |
} |
257 |
#endif |
#endif |
258 |
} |
} |
259 |
|
/* init sprite_trajectory tables */ |
260 |
|
/* even if GMC is not specified (it might be used later...) */ |
261 |
|
|
262 |
|
sprite_trajectory_code[0+16384].code = 0; |
263 |
|
sprite_trajectory_code[0+16384].len = 0; |
264 |
|
for (k=0;k<14;k++) |
265 |
|
{ |
266 |
|
int limit = (1<<k); |
267 |
|
|
268 |
|
for (l=-(2*limit-1); l <= -limit; l++) |
269 |
|
{ |
270 |
|
sprite_trajectory_code[l+16384].code = (2*limit-1)+l; |
271 |
|
sprite_trajectory_code[l+16384].len = k+1; |
272 |
} |
} |
273 |
|
|
274 |
/***************************************************************************** |
for (l=limit; l<= 2*limit-1; l++) |
275 |
* Local inlined functions for MB coding |
{ |
276 |
****************************************************************************/ |
sprite_trajectory_code[l+16384].code = l; |
277 |
|
sprite_trajectory_code[l+16384].len = k+1; |
278 |
|
} |
279 |
|
} |
280 |
|
} |
281 |
|
|
282 |
static __inline void |
static __inline void |
283 |
CodeVector(Bitstream * bs, |
CodeVector(Bitstream * bs, |
374 |
|
|
375 |
} |
} |
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
/* returns the number of bits required to encode qcoeff */ |
380 |
|
int |
381 |
|
CodeCoeff_CalcBits(const int16_t qcoeff[64], |
382 |
|
VLC * table, |
383 |
|
const uint16_t * zigzag, |
384 |
|
uint16_t intra) |
385 |
|
{ |
386 |
|
int bits = 0; |
387 |
|
uint32_t j, last; |
388 |
|
short v; |
389 |
|
VLC *vlc; |
390 |
|
|
391 |
|
j = intra; |
392 |
|
last = intra; |
393 |
|
|
394 |
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
395 |
|
j++; |
396 |
|
|
397 |
|
if (j >= 64) return 0; /* empty block */ |
398 |
|
|
399 |
|
do { |
400 |
|
vlc = table + 64 * 2048 + (v << 6) + j - last; |
401 |
|
last = ++j; |
402 |
|
|
403 |
|
/* count zeroes */ |
404 |
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
405 |
|
j++; |
406 |
|
|
407 |
|
/* write code */ |
408 |
|
if (j != 64) { |
409 |
|
bits += vlc->len; |
410 |
|
} else { |
411 |
|
vlc += 64 * 4096; |
412 |
|
bits += vlc->len; |
413 |
|
break; |
414 |
|
} |
415 |
|
} while (1); |
416 |
|
|
417 |
|
return bits; |
418 |
|
} |
419 |
|
|
420 |
|
|
421 |
#else |
#else |
422 |
|
|
423 |
static __inline void |
static __inline void |
487 |
i = 1; |
i = 1; |
488 |
run = 0; |
run = 0; |
489 |
|
|
490 |
while (!(level = qcoeff[zigzag[i++]])) |
while (i<64 && !(level = qcoeff[zigzag[i++]])) |
491 |
run++; |
run++; |
492 |
|
|
493 |
prev_level = level; |
prev_level = level; |
532 |
BitstreamPutBits(bs, code, len); |
BitstreamPutBits(bs, code, len); |
533 |
} |
} |
534 |
|
|
|
#endif |
|
535 |
|
|
|
/***************************************************************************** |
|
|
* Local functions |
|
|
****************************************************************************/ |
|
536 |
|
|
537 |
static void |
/* returns the number of bits required to encode qcoeff */ |
538 |
CodeBlockIntra(const FRAMEINFO * frame, |
|
539 |
|
int |
540 |
|
CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag) |
541 |
|
{ |
542 |
|
int bits = 0; |
543 |
|
uint32_t i, abs_level, run, prev_run, len; |
544 |
|
int32_t level, prev_level; |
545 |
|
|
546 |
|
i = 1; |
547 |
|
run = 0; |
548 |
|
|
549 |
|
while (i<64 && !(level = qcoeff[zigzag[i++]])) |
550 |
|
run++; |
551 |
|
|
552 |
|
if (i >= 64) return 0; /* empty block */ |
553 |
|
|
554 |
|
prev_level = level; |
555 |
|
prev_run = run; |
556 |
|
run = 0; |
557 |
|
|
558 |
|
while (i < 64) |
559 |
|
{ |
560 |
|
if ((level = qcoeff[zigzag[i++]]) != 0) |
561 |
|
{ |
562 |
|
abs_level = ABS(prev_level); |
563 |
|
abs_level = abs_level < 64 ? abs_level : 0; |
564 |
|
len = coeff_VLC[1][0][abs_level][prev_run].len; |
565 |
|
bits += len!=128 ? len : 30; |
566 |
|
|
567 |
|
prev_level = level; |
568 |
|
prev_run = run; |
569 |
|
run = 0; |
570 |
|
} |
571 |
|
else |
572 |
|
run++; |
573 |
|
} |
574 |
|
|
575 |
|
abs_level = ABS(prev_level); |
576 |
|
abs_level = abs_level < 64 ? abs_level : 0; |
577 |
|
len = coeff_VLC[1][1][abs_level][prev_run].len; |
578 |
|
bits += len!=128 ? len : 30; |
579 |
|
|
580 |
|
return bits; |
581 |
|
} |
582 |
|
|
583 |
|
int |
584 |
|
CodeCoeffInter_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag) |
585 |
|
{ |
586 |
|
uint32_t i, run, prev_run, len; |
587 |
|
int32_t level, prev_level, level_shifted; |
588 |
|
int bits = 0; |
589 |
|
|
590 |
|
i = 0; |
591 |
|
run = 0; |
592 |
|
|
593 |
|
while (!(level = qcoeff[zigzag[i++]])) |
594 |
|
run++; |
595 |
|
|
596 |
|
prev_level = level; |
597 |
|
prev_run = run; |
598 |
|
run = 0; |
599 |
|
|
600 |
|
while (i < 64) { |
601 |
|
if ((level = qcoeff[zigzag[i++]]) != 0) { |
602 |
|
level_shifted = prev_level + 32; |
603 |
|
if (!(level_shifted & -64)) |
604 |
|
len = coeff_VLC[0][0][level_shifted][prev_run].len; |
605 |
|
else |
606 |
|
len = 30; |
607 |
|
|
608 |
|
bits += len; |
609 |
|
prev_level = level; |
610 |
|
prev_run = run; |
611 |
|
run = 0; |
612 |
|
} |
613 |
|
else |
614 |
|
run++; |
615 |
|
} |
616 |
|
|
617 |
|
level_shifted = prev_level + 32; |
618 |
|
if (!(level_shifted & -64)) |
619 |
|
len = coeff_VLC[0][1][level_shifted][prev_run].len; |
620 |
|
else |
621 |
|
len = 30; |
622 |
|
bits += len; |
623 |
|
|
624 |
|
return bits; |
625 |
|
} |
626 |
|
|
627 |
|
|
628 |
|
#endif |
629 |
|
|
630 |
|
static __inline void |
631 |
|
CodeBlockIntra(const FRAMEINFO * const frame, |
632 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
633 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
634 |
Bitstream * bs, |
Bitstream * bs, |
639 |
|
|
640 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
641 |
|
|
642 |
/* write mcbpc */ |
// write mcbpc |
643 |
if (frame->coding_type == I_VOP) { |
if (frame->coding_type == I_VOP) { |
644 |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
645 |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
650 |
mcbpc_inter_tab[mcbpc].len); |
mcbpc_inter_tab[mcbpc].len); |
651 |
} |
} |
652 |
|
|
653 |
/* ac prediction flag */ |
// ac prediction flag |
654 |
if (pMB->acpred_directions[0]) |
if (pMB->acpred_directions[0]) |
655 |
BitstreamPutBits(bs, 1, 1); |
BitstreamPutBits(bs, 1, 1); |
656 |
else |
else |
657 |
BitstreamPutBits(bs, 0, 1); |
BitstreamPutBits(bs, 0, 1); |
658 |
|
|
659 |
/* write cbpy */ |
// write cbpy |
660 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
661 |
|
|
662 |
/* write dquant */ |
// write dquant |
663 |
if (pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA_Q) |
664 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
665 |
|
|
666 |
/* write interlacing */ |
// write interlacing |
667 |
if (frame->global_flags & XVID_INTERLACING) { |
if (frame->global_flags & XVID_INTERLACING) { |
668 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
669 |
} |
} |
670 |
/* code block coeffs */ |
// code block coeffs |
671 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
672 |
if (i < 4) |
if (i < 4) |
673 |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
677 |
dcc_tab[qcoeff[i * 64 + 0] + 255].len); |
dcc_tab[qcoeff[i * 64 + 0] + 255].len); |
678 |
|
|
679 |
if (pMB->cbp & (1 << (5 - i))) { |
if (pMB->cbp & (1 << (5 - i))) { |
680 |
|
const uint16_t *scan_table = |
681 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
682 |
|
scan_tables[2] : scan_tables[pMB->acpred_directions[i]]; |
683 |
|
|
684 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
685 |
|
|
686 |
#ifdef BIGLUT |
#ifdef BIGLUT |
687 |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1); |
|
scan_tables[pMB->acpred_directions[i]], 1); |
|
688 |
#else |
#else |
689 |
CodeCoeffIntra(bs, &qcoeff[i * 64], scan_tables[pMB->acpred_directions[i]]); |
CodeCoeffIntra(bs, &qcoeff[i * 64], scan_table); |
690 |
#endif |
#endif |
691 |
|
|
692 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
693 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
694 |
} |
} |
698 |
|
|
699 |
|
|
700 |
static void |
static void |
701 |
CodeBlockInter(const FRAMEINFO * frame, |
CodeBlockInter(const FRAMEINFO * const frame, |
702 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
703 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
704 |
Bitstream * bs, |
Bitstream * bs, |
711 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
712 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
713 |
|
|
714 |
/* write mcbpc */ |
// write mcbpc |
715 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
716 |
mcbpc_inter_tab[mcbpc].len); |
mcbpc_inter_tab[mcbpc].len); |
717 |
|
|
718 |
/* write cbpy */ |
if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) ) |
719 |
|
BitstreamPutBit(bs, pMB->mcsel); // mcsel: '0'=local motion, '1'=GMC |
720 |
|
|
721 |
|
// write cbpy |
722 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
723 |
|
|
724 |
/* write dquant */ |
// write dquant |
725 |
if (pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER_Q) |
726 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
727 |
|
|
728 |
/* interlacing */ |
// interlacing |
729 |
if (frame->global_flags & XVID_INTERLACING) { |
if (frame->global_flags & XVID_INTERLACING) { |
730 |
if (pMB->cbp) { |
if (pMB->cbp) { |
731 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
732 |
DPRINTF(DPRINTF_DEBUG, "codep: field_dct: %d", pMB->field_dct); |
DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct); |
733 |
} |
} |
734 |
|
|
735 |
/* if inter block, write field ME flag */ |
// if inter block, write field ME flag |
736 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
737 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, pMB->field_pred); |
738 |
DPRINTF(DPRINTF_DEBUG, "codep: field_pred: %d", pMB->field_pred); |
DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred); |
739 |
|
|
740 |
/* write field prediction references */ |
// write field prediction references |
741 |
if (pMB->field_pred) { |
if (pMB->field_pred) { |
742 |
BitstreamPutBit(bs, pMB->field_for_top); |
BitstreamPutBit(bs, pMB->field_for_top); |
743 |
BitstreamPutBit(bs, pMB->field_for_bot); |
BitstreamPutBit(bs, pMB->field_for_bot); |
744 |
} |
} |
745 |
} |
} |
746 |
} |
} |
747 |
/* code motion vector(s) */ |
// code motion vector(s) if motion is local |
748 |
|
if (!pMB->mcsel) |
749 |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
750 |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
751 |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
753 |
|
|
754 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
755 |
|
|
756 |
/* code block coeffs */ |
// code block coeffs |
757 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) |
758 |
if (pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) |
759 |
|
{ |
760 |
|
const uint16_t *scan_table = |
761 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
762 |
|
scan_tables[2] : scan_tables[0]; |
763 |
|
|
764 |
#ifdef BIGLUT |
#ifdef BIGLUT |
765 |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0); |
766 |
#else |
#else |
767 |
CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]); |
CodeCoeffInter(bs, &qcoeff[i * 64], scan_table); |
768 |
#endif |
#endif |
769 |
|
} |
770 |
|
|
771 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
772 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
|
|
|
773 |
} |
} |
774 |
|
|
|
/***************************************************************************** |
|
|
* Macro Block bitstream encoding functions |
|
|
****************************************************************************/ |
|
775 |
|
|
776 |
void |
void |
777 |
MBCoding(const FRAMEINFO * frame, |
MBCoding(const FRAMEINFO * const frame, |
778 |
MACROBLOCK * pMB, |
MACROBLOCK * pMB, |
779 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
780 |
Bitstream * bs, |
Bitstream * bs, |
781 |
Statistics * pStat) |
Statistics * pStat) |
782 |
{ |
{ |
783 |
|
if (frame->coding_type != I_VOP) |
784 |
if (frame->coding_type == P_VOP) { |
BitstreamPutBit(bs, 0); // not_coded |
|
BitstreamPutBit(bs, 0); /* coded */ |
|
|
} |
|
785 |
|
|
786 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
787 |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
790 |
|
|
791 |
} |
} |
792 |
|
|
793 |
|
/* |
794 |
|
// moved to mbcoding.h so that in can be 'static __inline' |
795 |
void |
void |
796 |
MBSkip(Bitstream * bs) |
MBSkip(Bitstream * bs) |
797 |
{ |
{ |
798 |
BitstreamPutBit(bs, 1); /* not coded */ |
BitstreamPutBit(bs, 1); // not coded |
|
return; |
|
799 |
} |
} |
800 |
|
*/ |
801 |
|
|
802 |
/***************************************************************************** |
/*************************************************************** |
803 |
* decoding stuff starts here |
* bframe encoding start |
804 |
****************************************************************************/ |
***************************************************************/ |
805 |
|
|
806 |
/* |
/* |
807 |
* For IVOP addbits == 0 |
mbtype |
808 |
* For PVOP addbits == fcode - 1 |
0 1b direct(h263) mvdb |
809 |
* For BVOP addbits == max(fcode,bcode) - 1 |
1 01b interpolate mc+q dbquant, mvdf, mvdb |
810 |
* returns true or false |
2 001b backward mc+q dbquant, mvdb |
811 |
|
3 0001b forward mc+q dbquant, mvdf |
812 |
*/ |
*/ |
813 |
|
|
814 |
|
static __inline void |
815 |
|
put_bvop_mbtype(Bitstream * bs, |
816 |
|
int value) |
817 |
|
{ |
818 |
|
switch (value) { |
819 |
|
case MODE_FORWARD: |
820 |
|
BitstreamPutBit(bs, 0); |
821 |
|
case MODE_BACKWARD: |
822 |
|
BitstreamPutBit(bs, 0); |
823 |
|
case MODE_INTERPOLATE: |
824 |
|
BitstreamPutBit(bs, 0); |
825 |
|
case MODE_DIRECT: |
826 |
|
BitstreamPutBit(bs, 1); |
827 |
|
default: |
828 |
|
break; |
829 |
|
} |
830 |
|
} |
831 |
|
|
832 |
|
/* |
833 |
|
dbquant |
834 |
|
-2 10b |
835 |
|
0 0b |
836 |
|
+2 11b |
837 |
|
*/ |
838 |
|
|
839 |
|
static __inline void |
840 |
|
put_bvop_dbquant(Bitstream * bs, |
841 |
|
int value) |
842 |
|
{ |
843 |
|
switch (value) { |
844 |
|
case 0: |
845 |
|
BitstreamPutBit(bs, 0); |
846 |
|
return; |
847 |
|
|
848 |
|
case -2: |
849 |
|
BitstreamPutBit(bs, 1); |
850 |
|
BitstreamPutBit(bs, 0); |
851 |
|
return; |
852 |
|
|
853 |
|
case 2: |
854 |
|
BitstreamPutBit(bs, 1); |
855 |
|
BitstreamPutBit(bs, 1); |
856 |
|
return; |
857 |
|
|
858 |
|
default:; // invalid |
859 |
|
} |
860 |
|
} |
861 |
|
|
862 |
|
|
863 |
|
|
864 |
|
void |
865 |
|
MBCodingBVOP(const MACROBLOCK * mb, |
866 |
|
const int16_t qcoeff[6 * 64], |
867 |
|
const int32_t fcode, |
868 |
|
const int32_t bcode, |
869 |
|
Bitstream * bs, |
870 |
|
Statistics * pStat, |
871 |
|
int direction) |
872 |
|
{ |
873 |
|
int vcode = fcode; |
874 |
|
unsigned int i; |
875 |
|
|
876 |
|
/* ------------------------------------------------------------------ |
877 |
|
when a block is skipped it is decoded DIRECT(0,0) |
878 |
|
hence is interpolated from forward & backward frames |
879 |
|
------------------------------------------------------------------ */ |
880 |
|
|
881 |
|
if (mb->mode == MODE_DIRECT_NONE_MV) { |
882 |
|
BitstreamPutBit(bs, 1); // skipped |
883 |
|
return; |
884 |
|
} |
885 |
|
|
886 |
|
BitstreamPutBit(bs, 0); // not skipped |
887 |
|
|
888 |
|
if (mb->cbp == 0) { |
889 |
|
BitstreamPutBit(bs, 1); // cbp == 0 |
890 |
|
} else { |
891 |
|
BitstreamPutBit(bs, 0); // cbp == xxx |
892 |
|
} |
893 |
|
|
894 |
|
put_bvop_mbtype(bs, mb->mode); |
895 |
|
|
896 |
|
if (mb->cbp) { |
897 |
|
BitstreamPutBits(bs, mb->cbp, 6); |
898 |
|
} |
899 |
|
|
900 |
|
if (mb->mode != MODE_DIRECT && mb->cbp != 0) { |
901 |
|
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
902 |
|
} |
903 |
|
|
904 |
|
switch (mb->mode) { |
905 |
|
case MODE_INTERPOLATE: |
906 |
|
CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode |
907 |
|
CodeVector(bs, mb->pmvs[1].y, vcode, pStat); |
908 |
|
case MODE_BACKWARD: |
909 |
|
vcode = bcode; |
910 |
|
case MODE_FORWARD: |
911 |
|
CodeVector(bs, mb->pmvs[0].x, vcode, pStat); |
912 |
|
CodeVector(bs, mb->pmvs[0].y, vcode, pStat); |
913 |
|
break; |
914 |
|
case MODE_DIRECT: |
915 |
|
CodeVector(bs, mb->pmvs[3].x, 1, pStat); // fcode is always 1 for delta vector |
916 |
|
CodeVector(bs, mb->pmvs[3].y, 1, pStat); // prediction is always (0,0) |
917 |
|
default: break; |
918 |
|
} |
919 |
|
|
920 |
|
for (i = 0; i < 6; i++) { |
921 |
|
if (mb->cbp & (1 << (5 - i))) { |
922 |
|
#ifdef BIGLUT |
923 |
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
924 |
|
#else |
925 |
|
CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]); |
926 |
|
#endif |
927 |
|
} |
928 |
|
} |
929 |
|
} |
930 |
|
|
931 |
|
|
932 |
|
|
933 |
|
/*************************************************************** |
934 |
|
* decoding stuff starts here * |
935 |
|
***************************************************************/ |
936 |
|
|
937 |
|
|
938 |
|
// for IVOP addbits == 0 |
939 |
|
// for PVOP addbits == fcode - 1 |
940 |
|
// for BVOP addbits == max(fcode,bcode) - 1 |
941 |
|
// returns true or false |
942 |
int |
int |
943 |
check_resync_marker(Bitstream * bs, int addbits) |
check_resync_marker(Bitstream * bs, int addbits) |
944 |
{ |
{ |
980 |
|
|
981 |
uint32_t index; |
uint32_t index; |
982 |
|
|
983 |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
index = MIN(BitstreamShowBits(bs, 9), 256); |
984 |
|
|
985 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
986 |
|
|
1006 |
|
|
1007 |
} |
} |
1008 |
|
|
1009 |
int |
static __inline int |
1010 |
get_mv_data(Bitstream * bs) |
get_mv_data(Bitstream * bs) |
1011 |
{ |
{ |
1012 |
|
|
1115 |
|
|
1116 |
} |
} |
1117 |
|
|
|
/***************************************************************************** |
|
|
* Local inlined function to "decode" written vlc codes |
|
|
****************************************************************************/ |
|
|
|
|
1118 |
static __inline int |
static __inline int |
1119 |
get_coeff(Bitstream * bs, |
get_coeff(Bitstream * bs, |
1120 |
int *run, |
int *run, |
1196 |
return 0; |
return 0; |
1197 |
} |
} |
1198 |
|
|
|
/***************************************************************************** |
|
|
* MB reading functions |
|
|
****************************************************************************/ |
|
|
|
|
1199 |
void |
void |
1200 |
get_intra_block(Bitstream * bs, |
get_intra_block(Bitstream * bs, |
1201 |
int16_t * block, |
int16_t * block, |
1204 |
{ |
{ |
1205 |
|
|
1206 |
const uint16_t *scan = scan_tables[direction]; |
const uint16_t *scan = scan_tables[direction]; |
1207 |
int level; |
int level, run, last; |
|
int run; |
|
|
int last; |
|
1208 |
|
|
1209 |
do { |
do { |
1210 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
1211 |
if (run == -1) { |
if (run == -1) { |
1212 |
DPRINTF(DPRINTF_DEBUG, "fatal: invalid run"); |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
1213 |
break; |
break; |
1214 |
} |
} |
1215 |
coeff += run; |
coeff += run; |
1216 |
block[scan[coeff]] = level; |
block[scan[coeff]] = level; |
1217 |
|
|
1218 |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
1219 |
/*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)); |
1220 |
|
|
1221 |
if (level < -2047 || level > 2047) { |
if (level < -2047 || level > 2047) { |
1222 |
DPRINTF(DPRINTF_DEBUG, "warning: intra_overflow: %d", level); |
DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level); |
1223 |
} |
} |
1224 |
coeff++; |
coeff++; |
1225 |
} while (!last); |
} while (!last); |
1228 |
|
|
1229 |
void |
void |
1230 |
get_inter_block(Bitstream * bs, |
get_inter_block(Bitstream * bs, |
1231 |
int16_t * block) |
int16_t * block, |
1232 |
|
int direction) |
1233 |
{ |
{ |
1234 |
|
|
1235 |
const uint16_t *scan = scan_tables[0]; |
const uint16_t *scan = scan_tables[direction]; |
1236 |
int p; |
int p; |
1237 |
int level; |
int level; |
1238 |
int run; |
int run; |
1250 |
block[scan[p]] = level; |
block[scan[p]] = level; |
1251 |
|
|
1252 |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
1253 |
|
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
1254 |
|
|
1255 |
if (level < -2047 || level > 2047) { |
if (level < -2047 || level > 2047) { |
1256 |
DPRINTF(DPRINTF_DEBUG, "warning: inter_overflow: %d", level); |
DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level); |
1257 |
} |
} |
1258 |
p++; |
p++; |
1259 |
} while (!last); |
} while (!last); |