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.06.2002 added check_resync_marker() * |
* which is not derived from or based on XviD. |
45 |
* 14.04.2002 bframe encoding * |
* |
46 |
* 08.03.2002 initial version; isibaar * |
* Note that people who make modified versions of XviD are not obligated |
47 |
* * |
* 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: mbcoding.c,v 1.38 2003-01-07 21:57:19 edgomez Exp $ |
54 |
|
* |
55 |
|
****************************************************************************/ |
56 |
|
|
57 |
#include <stdlib.h> |
#include <stdlib.h> |
58 |
#include "../portab.h" |
#include "../portab.h" |
66 |
#define ABS(X) (((X)>0)?(X):-(X)) |
#define ABS(X) (((X)>0)?(X):-(X)) |
67 |
#define CLIP(X,A) (X > A) ? (A) : (X) |
#define CLIP(X,A) (X > A) ? (A) : (X) |
68 |
|
|
69 |
VLC intra_table[524032]; |
/* #define BIGLUT */ |
|
VLC inter_table[524032]; |
|
70 |
|
|
71 |
VLC DCT3Dintra[4096]; |
#ifdef BIGLUT |
72 |
VLC DCT3Dinter[4096]; |
#define LEVELOFFSET 2048 |
73 |
|
#else |
74 |
|
#define LEVELOFFSET 32 |
75 |
|
#endif |
76 |
|
|
77 |
|
/***************************************************************************** |
78 |
|
* Local data |
79 |
|
****************************************************************************/ |
80 |
|
|
81 |
|
static REVERSE_EVENT DCT3D[2][4096]; |
82 |
|
|
83 |
|
#ifdef BIGLUT |
84 |
|
static VLC coeff_VLC[2][2][4096][64]; |
85 |
|
static VLC *intra_table, *inter_table; |
86 |
|
#else |
87 |
|
static VLC coeff_VLC[2][2][64][64]; |
88 |
|
#endif |
89 |
|
|
90 |
|
/***************************************************************************** |
91 |
|
* Vector Length Coding Initialization |
92 |
|
****************************************************************************/ |
93 |
|
|
94 |
void |
void |
95 |
init_vlc_tables(void) |
init_vlc_tables(void) |
96 |
{ |
{ |
97 |
|
uint32_t i, j, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; |
98 |
|
|
99 |
int32_t k, l, i, intra, last; |
#ifdef BIGLUT |
100 |
VLC *vlc[2]; |
intra_table = (VLC*)coeff_VLC[1]; |
101 |
VLC **coeff_ptr; |
inter_table = (VLC*)coeff_VLC[0]; |
102 |
VLC *vlc1, *vlc2; |
#endif |
|
|
|
|
vlc1 = DCT3Dintra; |
|
|
vlc2 = DCT3Dinter; |
|
|
|
|
|
vlc[0] = intra_table; |
|
|
vlc[1] = inter_table; |
|
|
|
|
|
// generate encoding vlc lookup tables |
|
|
// the lookup table idea is taken from the excellent fame project by Vivien Chapellier |
|
|
for (i = 0; i < 4; i++) { |
|
|
intra = i % 2; |
|
|
last = i / 2; |
|
|
|
|
|
coeff_ptr = coeff_vlc[last + 2 * intra]; |
|
|
|
|
|
for (k = -2047; k < 2048; k++) { // level |
|
|
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
|
|
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
|
|
|
|
|
for (l = 0; l < 64; l++) { // run |
|
|
int32_t level = k; |
|
|
ptr_t run = l; |
|
|
|
|
|
if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
|
|
|
|
|
vlc[intra]->code = 0; |
|
|
vlc[intra]->len = 0; |
|
|
goto loop_end; |
|
|
} else { |
|
|
if (level > 0) // correct level |
|
|
level -= max_level_ptr[run]; |
|
|
else |
|
|
level += max_level_ptr[run]; |
|
|
|
|
|
if ((abs(level) <= max_level_ptr[run]) && |
|
|
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
|
103 |
|
|
|
vlc[intra]->code = 0x06; |
|
|
vlc[intra]->len = 8; |
|
|
goto loop_end; |
|
|
} |
|
104 |
|
|
105 |
if (level > 0) // still here? |
for (intra = 0; intra < 2; intra++) |
106 |
level += max_level_ptr[run]; // restore level |
for (i = 0; i < 4096; i++) |
107 |
else |
DCT3D[intra][i].event.level = 0; |
|
level -= max_level_ptr[run]; |
|
108 |
|
|
109 |
run -= max_run_ptr[abs(level)] + 1; // and change run |
for (intra = 0; intra < 2; intra++) |
110 |
|
for (last = 0; last < 2; last++) |
111 |
|
{ |
112 |
|
for (run = 0; run < 63 + last; run++) |
113 |
|
for (level = 0; level < 32 << intra; level++) |
114 |
|
{ |
115 |
|
#ifdef BIGLUT |
116 |
|
offset = LEVELOFFSET; |
117 |
|
#else |
118 |
|
offset = !intra * LEVELOFFSET; |
119 |
|
#endif |
120 |
|
coeff_VLC[intra][last][level + offset][run].len = 128; |
121 |
|
} |
122 |
|
} |
123 |
|
|
124 |
if ((abs(level) <= max_level_ptr[run]) && |
for (intra = 0; intra < 2; intra++) |
125 |
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
for (i = 0; i < 102; i++) |
126 |
|
{ |
127 |
|
#ifdef BIGLUT |
128 |
|
offset = LEVELOFFSET; |
129 |
|
#else |
130 |
|
offset = !intra * LEVELOFFSET; |
131 |
|
#endif |
132 |
|
for (j = 0; j < 1 << (12 - coeff_tab[intra][i].vlc.len); j++) |
133 |
|
{ |
134 |
|
DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len = coeff_tab[intra][i].vlc.len; |
135 |
|
DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event; |
136 |
|
} |
137 |
|
|
138 |
vlc[intra]->code = 0x0e; |
coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].code |
139 |
vlc[intra]->len = 9; |
= coeff_tab[intra][i].vlc.code << 1; |
140 |
goto loop_end; |
coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].len |
141 |
|
= coeff_tab[intra][i].vlc.len + 1; |
142 |
|
#ifndef BIGLUT |
143 |
|
if (!intra) |
144 |
|
#endif |
145 |
|
{ |
146 |
|
coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code |
147 |
|
= (coeff_tab[intra][i].vlc.code << 1) | 1; |
148 |
|
coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len |
149 |
|
= coeff_tab[intra][i].vlc.len + 1; |
150 |
} |
} |
|
run += max_run_ptr[abs(level)] + 1; |
|
151 |
} |
} |
152 |
|
|
153 |
vlc[intra]->code = |
for (intra = 0; intra < 2; intra++) |
154 |
(uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) | |
for (last = 0; last < 2; last++) |
155 |
((k & 0xfff) << 1) | 1; |
for (run = 0; run < 63 + last; run++) |
156 |
|
{ |
157 |
vlc[intra]->len = 30; |
for (level = 1; level < 32 << intra; level++) |
158 |
vlc[intra]++; |
{ |
159 |
|
if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level]) |
160 |
continue; |
continue; |
161 |
|
|
162 |
loop_end: |
#ifdef BIGLUT |
163 |
if (level != 0) { |
offset = LEVELOFFSET; |
164 |
vlc[intra]->code = |
#else |
165 |
(vlc[intra]-> |
offset = !intra * LEVELOFFSET; |
166 |
code << (coeff_ptr[run][abs(level) - 1].len + |
#endif |
167 |
1)) | (coeff_ptr[run][abs(level) - |
level_esc = level - max_level[intra][last][run]; |
168 |
1].code << 1); |
run_esc = run - 1 - max_run[intra][last][level]; |
169 |
vlc[intra]->len = |
/*use this test to use shorter esc2 codes when possible |
170 |
(coeff_ptr[run][abs(level) - 1].len + 1) + |
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc] |
171 |
vlc[intra]->len; |
&& !(coeff_VLC[intra][last][level_esc + offset][run].len + 7 + 1 |
172 |
|
> coeff_VLC[intra][last][level + offset][run_esc].code + 7 + 2))*/ |
173 |
if (level < 0) |
|
174 |
vlc[intra]->code += 1; |
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]) |
175 |
} |
{ |
176 |
|
escape = ESCAPE1; |
177 |
vlc[intra]++; |
escape_len = 7 + 1; |
178 |
|
run_esc = run; |
179 |
} |
} |
180 |
|
else |
181 |
|
{ |
182 |
|
if (level <= max_level[intra][last][run_esc] && run_esc <= max_run[intra][last][level]) |
183 |
|
{ |
184 |
|
escape = ESCAPE2; |
185 |
|
escape_len = 7 + 2; |
186 |
|
level_esc = level; |
187 |
} |
} |
188 |
|
else |
189 |
|
{ |
190 |
|
#ifndef BIGLUT |
191 |
|
if (!intra) |
192 |
|
#endif |
193 |
|
{ |
194 |
|
coeff_VLC[intra][last][level + offset][run].code |
195 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
196 |
|
coeff_VLC[intra][last][level + offset][run].len = 30; |
197 |
|
coeff_VLC[intra][last][offset - level][run].code |
198 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1; |
199 |
|
coeff_VLC[intra][last][offset - level][run].len = 30; |
200 |
|
} |
201 |
|
continue; |
202 |
} |
} |
|
|
|
|
for (i = 0; i < 4096; i++) { |
|
|
if (i >= 512) { |
|
|
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
|
|
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
|
|
} else if (i >= 128) { |
|
|
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
|
|
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
|
|
} else if (i >= 8) { |
|
|
*vlc1 = DCT3Dtab5[i - 8]; |
|
|
*vlc2 = DCT3Dtab2[i - 8]; |
|
|
} else { |
|
|
*vlc1 = ERRtab[i]; |
|
|
*vlc2 = ERRtab[i]; |
|
203 |
} |
} |
204 |
|
|
205 |
vlc1++; |
coeff_VLC[intra][last][level + offset][run].code |
206 |
vlc2++; |
= (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len) |
207 |
|
| coeff_VLC[intra][last][level_esc + offset][run_esc].code; |
208 |
|
coeff_VLC[intra][last][level + offset][run].len |
209 |
|
= coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len; |
210 |
|
#ifndef BIGLUT |
211 |
|
if (!intra) |
212 |
|
#endif |
213 |
|
{ |
214 |
|
coeff_VLC[intra][last][offset - level][run].code |
215 |
|
= (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len) |
216 |
|
| coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1; |
217 |
|
coeff_VLC[intra][last][offset - level][run].len |
218 |
|
= coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len; |
219 |
|
} |
220 |
} |
} |
|
DCT3D[0] = DCT3Dinter; |
|
|
DCT3D[1] = DCT3Dintra; |
|
221 |
|
|
222 |
|
#ifdef BIGLUT |
223 |
|
for (level = 32 << intra; level < 2048; level++) |
224 |
|
{ |
225 |
|
coeff_VLC[intra][last][level + offset][run].code |
226 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
227 |
|
coeff_VLC[intra][last][level + offset][run].len = 30; |
228 |
|
|
229 |
|
coeff_VLC[intra][last][offset - level][run].code |
230 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1; |
231 |
|
coeff_VLC[intra][last][offset - level][run].len = 30; |
232 |
} |
} |
233 |
|
#else |
234 |
|
if (!intra) |
235 |
|
{ |
236 |
|
coeff_VLC[intra][last][0][run].code |
237 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1; |
238 |
|
coeff_VLC[intra][last][0][run].len = 30; |
239 |
|
} |
240 |
|
#endif |
241 |
|
} |
242 |
|
} |
243 |
|
|
244 |
|
/***************************************************************************** |
245 |
|
* Local inlined functions for MB coding |
246 |
|
****************************************************************************/ |
247 |
|
|
248 |
static __inline void |
static __inline void |
249 |
CodeVector(Bitstream * bs, |
CodeVector(Bitstream * bs, |
300 |
|
|
301 |
} |
} |
302 |
|
|
303 |
|
#ifdef BIGLUT |
304 |
|
|
305 |
static __inline void |
static __inline void |
306 |
CodeCoeff(Bitstream * bs, |
CodeCoeff(Bitstream * bs, |
321 |
j++; |
j++; |
322 |
|
|
323 |
do { |
do { |
324 |
vlc = table + 64 * 2047 + (v << 6) + j - last; |
vlc = table + 64 * 2048 + (v << 6) + j - last; |
325 |
last = ++j; |
last = ++j; |
326 |
|
|
327 |
// count zeroes |
/* count zeroes */ |
328 |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
329 |
j++; |
j++; |
330 |
|
|
331 |
// write code |
/* write code */ |
332 |
if (j != 64) { |
if (j != 64) { |
333 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
334 |
} else { |
} else { |
335 |
vlc += 64 * 4095; |
vlc += 64 * 4096; |
336 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
337 |
break; |
break; |
338 |
} |
} |
340 |
|
|
341 |
} |
} |
342 |
|
|
343 |
|
#else |
344 |
|
|
345 |
|
static __inline void |
346 |
|
CodeCoeffInter(Bitstream * bs, |
347 |
|
const int16_t qcoeff[64], |
348 |
|
const uint16_t * zigzag) |
349 |
|
{ |
350 |
|
uint32_t i, run, prev_run, code, len; |
351 |
|
int32_t level, prev_level, level_shifted; |
352 |
|
|
353 |
|
i = 0; |
354 |
|
run = 0; |
355 |
|
|
356 |
|
while (!(level = qcoeff[zigzag[i++]])) |
357 |
|
run++; |
358 |
|
|
359 |
|
prev_level = level; |
360 |
|
prev_run = run; |
361 |
|
run = 0; |
362 |
|
|
363 |
|
while (i < 64) |
364 |
|
{ |
365 |
|
if ((level = qcoeff[zigzag[i++]]) != 0) |
366 |
|
{ |
367 |
|
level_shifted = prev_level + 32; |
368 |
|
if (!(level_shifted & -64)) |
369 |
|
{ |
370 |
|
code = coeff_VLC[0][0][level_shifted][prev_run].code; |
371 |
|
len = coeff_VLC[0][0][level_shifted][prev_run].len; |
372 |
|
} |
373 |
|
else |
374 |
|
{ |
375 |
|
code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
376 |
|
len = 30; |
377 |
|
} |
378 |
|
BitstreamPutBits(bs, code, len); |
379 |
|
prev_level = level; |
380 |
|
prev_run = run; |
381 |
|
run = 0; |
382 |
|
} |
383 |
|
else |
384 |
|
run++; |
385 |
|
} |
386 |
|
|
387 |
|
level_shifted = prev_level + 32; |
388 |
|
if (!(level_shifted & -64)) |
389 |
|
{ |
390 |
|
code = coeff_VLC[0][1][level_shifted][prev_run].code; |
391 |
|
len = coeff_VLC[0][1][level_shifted][prev_run].len; |
392 |
|
} |
393 |
|
else |
394 |
|
{ |
395 |
|
code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
396 |
|
len = 30; |
397 |
|
} |
398 |
|
BitstreamPutBits(bs, code, len); |
399 |
|
} |
400 |
|
|
401 |
|
static __inline void |
402 |
|
CodeCoeffIntra(Bitstream * bs, |
403 |
|
const int16_t qcoeff[64], |
404 |
|
const uint16_t * zigzag) |
405 |
|
{ |
406 |
|
uint32_t i, abs_level, run, prev_run, code, len; |
407 |
|
int32_t level, prev_level; |
408 |
|
|
409 |
|
i = 1; |
410 |
|
run = 0; |
411 |
|
|
412 |
|
while (!(level = qcoeff[zigzag[i++]])) |
413 |
|
run++; |
414 |
|
|
415 |
|
prev_level = level; |
416 |
|
prev_run = run; |
417 |
|
run = 0; |
418 |
|
|
419 |
|
while (i < 64) |
420 |
|
{ |
421 |
|
if ((level = qcoeff[zigzag[i++]]) != 0) |
422 |
|
{ |
423 |
|
abs_level = ABS(prev_level); |
424 |
|
abs_level = abs_level < 64 ? abs_level : 0; |
425 |
|
code = coeff_VLC[1][0][abs_level][prev_run].code; |
426 |
|
len = coeff_VLC[1][0][abs_level][prev_run].len; |
427 |
|
if (len != 128) |
428 |
|
code |= (prev_level < 0); |
429 |
|
else |
430 |
|
{ |
431 |
|
code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
432 |
|
len = 30; |
433 |
|
} |
434 |
|
BitstreamPutBits(bs, code, len); |
435 |
|
prev_level = level; |
436 |
|
prev_run = run; |
437 |
|
run = 0; |
438 |
|
} |
439 |
|
else |
440 |
|
run++; |
441 |
|
} |
442 |
|
|
443 |
|
abs_level = ABS(prev_level); |
444 |
|
abs_level = abs_level < 64 ? abs_level : 0; |
445 |
|
code = coeff_VLC[1][1][abs_level][prev_run].code; |
446 |
|
len = coeff_VLC[1][1][abs_level][prev_run].len; |
447 |
|
if (len != 128) |
448 |
|
code |= (prev_level < 0); |
449 |
|
else |
450 |
|
{ |
451 |
|
code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
452 |
|
len = 30; |
453 |
|
} |
454 |
|
BitstreamPutBits(bs, code, len); |
455 |
|
} |
456 |
|
|
457 |
|
#endif |
458 |
|
|
459 |
|
/***************************************************************************** |
460 |
|
* Local functions |
461 |
|
****************************************************************************/ |
462 |
|
|
463 |
static void |
static void |
464 |
CodeBlockIntra(const FRAMEINFO * frame, |
CodeBlockIntra(const FRAMEINFO * frame, |
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, |
512 |
if (pMB->cbp & (1 << (5 - i))) { |
if (pMB->cbp & (1 << (5 - i))) { |
513 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
514 |
|
|
515 |
|
#ifdef BIGLUT |
516 |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
517 |
scan_tables[pMB->acpred_directions[i]], 1); |
scan_tables[pMB->acpred_directions[i]], 1); |
518 |
|
#else |
519 |
|
CodeCoeffIntra(bs, &qcoeff[i * 64], scan_tables[pMB->acpred_directions[i]]); |
520 |
|
#endif |
521 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
522 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
523 |
} |
} |
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 |
// write cbpy |
/* 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) { |
557 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
558 |
DEBUG1("codep: field_dct: ", 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 |
DEBUG1("codep: field_pred: ", 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) |
/* code motion vector(s) */ |
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))) |
584 |
|
#ifdef BIGLUT |
585 |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
586 |
|
#else |
587 |
|
CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]); |
588 |
|
#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 * frame, |
MBCoding(const FRAMEINFO * frame, |
605 |
{ |
{ |
606 |
|
|
607 |
if (frame->coding_type == P_VOP) { |
if (frame->coding_type == P_VOP) { |
608 |
BitstreamPutBit(bs, 0); // coded |
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) |
619 |
void |
void |
620 |
MBSkip(Bitstream * bs) |
MBSkip(Bitstream * bs) |
621 |
{ |
{ |
622 |
BitstreamPutBit(bs, 1); // not coded |
BitstreamPutBit(bs, 1); /* not coded */ |
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
/*************************************************************** |
|
|
* bframe encoding start |
|
|
***************************************************************/ |
|
|
|
|
|
/* |
|
|
mbtype |
|
|
0 1b direct(h263) mvdb |
|
|
1 01b interpolate mc+q dbquant, mvdf, mvdb |
|
|
2 001b backward mc+q dbquant, mvdb |
|
|
3 0001b forward mc+q dbquant, mvdf |
|
|
*/ |
|
|
|
|
|
void |
|
|
put_bvop_mbtype(Bitstream * bs, |
|
|
int value) |
|
|
{ |
|
|
switch (value) { |
|
|
case 0: |
|
|
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: |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 1); |
|
623 |
return; |
return; |
|
|
|
|
default:; // invalid! |
|
|
|
|
624 |
} |
} |
625 |
|
|
626 |
} |
/***************************************************************************** |
627 |
|
* decoding stuff starts here |
628 |
|
****************************************************************************/ |
629 |
|
|
630 |
/* |
/* |
631 |
dbquant |
* For IVOP addbits == 0 |
632 |
-2 10b |
* For PVOP addbits == fcode - 1 |
633 |
0 0b |
* For BVOP addbits == max(fcode,bcode) - 1 |
634 |
+2 11b |
* returns true or false |
635 |
*/ |
*/ |
636 |
|
|
|
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 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 |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
|
|
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
|
|
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
|
|
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
|
|
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_DIRECT) { |
|
|
CodeVector(bs, mb->deltamv.x, 1, pStat); /* fcode is always 1 for delta vector */ |
|
|
CodeVector(bs, mb->deltamv.y, 1, pStat); /* prediction is always (0,0) */ |
|
|
} |
|
|
|
|
|
for (i = 0; i < 6; i++) { |
|
|
if (mb->cbp & (1 << (5 - i))) { |
|
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************** |
|
|
* 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 |
{ |
{ |
810 |
|
|
811 |
} |
} |
812 |
|
|
813 |
|
/***************************************************************************** |
814 |
|
* Local inlined function to "decode" written vlc codes |
815 |
|
****************************************************************************/ |
816 |
|
|
817 |
|
static __inline int |
818 |
|
get_coeff(Bitstream * bs, |
819 |
|
int *run, |
820 |
|
int *last, |
821 |
|
int intra, |
822 |
|
int short_video_header) |
823 |
|
{ |
824 |
|
|
825 |
|
uint32_t mode; |
826 |
|
int32_t level; |
827 |
|
REVERSE_EVENT *reverse_event; |
828 |
|
|
829 |
|
if (short_video_header) /* inter-VLCs will be used for both intra and inter blocks */ |
830 |
|
intra = 0; |
831 |
|
|
832 |
|
if (BitstreamShowBits(bs, 7) != ESCAPE) { |
833 |
|
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
834 |
|
|
835 |
|
if ((level = reverse_event->event.level) == 0) |
836 |
|
goto error; |
837 |
|
|
838 |
|
*last = reverse_event->event.last; |
839 |
|
*run = reverse_event->event.run; |
840 |
|
|
841 |
|
BitstreamSkip(bs, reverse_event->len); |
842 |
|
|
843 |
|
return BitstreamGetBits(bs, 1) ? -level : level; |
844 |
|
} |
845 |
|
|
846 |
|
BitstreamSkip(bs, 7); |
847 |
|
|
848 |
|
if (short_video_header) { |
849 |
|
/* escape mode 4 - H.263 type, only used if short_video_header = 1 */ |
850 |
|
*last = BitstreamGetBit(bs); |
851 |
|
*run = BitstreamGetBits(bs, 6); |
852 |
|
level = BitstreamGetBits(bs, 8); |
853 |
|
|
854 |
|
if (level == 0 || level == 128) |
855 |
|
DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level); |
856 |
|
|
857 |
|
return (level << 24) >> 24; |
858 |
|
} |
859 |
|
|
860 |
|
mode = BitstreamShowBits(bs, 2); |
861 |
|
|
862 |
|
if (mode < 3) { |
863 |
|
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
864 |
|
|
865 |
|
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
866 |
|
|
867 |
|
if ((level = reverse_event->event.level) == 0) |
868 |
|
goto error; |
869 |
|
|
870 |
|
*last = reverse_event->event.last; |
871 |
|
*run = reverse_event->event.run; |
872 |
|
|
873 |
|
BitstreamSkip(bs, reverse_event->len); |
874 |
|
|
875 |
|
if (mode < 2) /* first escape mode, level is offset */ |
876 |
|
level += max_level[intra][*last][*run]; |
877 |
|
else /* second escape mode, run is offset */ |
878 |
|
*run += max_run[intra][*last][level] + 1; |
879 |
|
|
880 |
|
return BitstreamGetBits(bs, 1) ? -level : level; |
881 |
|
} |
882 |
|
|
883 |
|
/* third escape mode - fixed length codes */ |
884 |
|
BitstreamSkip(bs, 2); |
885 |
|
*last = BitstreamGetBits(bs, 1); |
886 |
|
*run = BitstreamGetBits(bs, 6); |
887 |
|
BitstreamSkip(bs, 1); /* marker */ |
888 |
|
level = BitstreamGetBits(bs, 12); |
889 |
|
BitstreamSkip(bs, 1); /* marker */ |
890 |
|
|
891 |
|
return (level << 20) >> 20; |
892 |
|
|
893 |
|
error: |
894 |
|
*run = VLC_ERROR; |
895 |
|
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, |
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 |
DEBUG("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 < -127 || level > 127) { |
if (level < -2047 || level > 2047) { |
927 |
DEBUG1("warning: intra_overflow", level); |
DPRINTF(DPRINTF_DEBUG, "warning: intra_overflow: %d", level); |
928 |
} |
} |
929 |
coeff++; |
coeff++; |
930 |
} while (!last); |
} while (!last); |
946 |
do { |
do { |
947 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
948 |
if (run == -1) { |
if (run == -1) { |
949 |
DEBUG("fatal: invalid run"); |
DPRINTF(DPRINTF_ERROR, "fatal: invalid run"); |
950 |
break; |
break; |
951 |
} |
} |
952 |
p += run; |
p += 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 < -127 || level > 127) { |
if (level < -2047 || level > 2047) { |
959 |
DEBUG1("warning: inter_overflow", level); |
DPRINTF(DPRINTF_DEBUG, "warning: inter_overflow: %d", level); |
960 |
} |
} |
961 |
p++; |
p++; |
962 |
} while (!last); |
} while (!last); |