1 |
|
/***************************************************************************** |
2 |
|
* |
3 |
|
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - Macro Block coding functions - |
5 |
|
* |
6 |
|
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 |
|
* |
8 |
|
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
9 |
|
* |
10 |
|
* XviD is free software; you can redistribute it and/or modify it |
11 |
|
* under the terms of the GNU General Public License as published by |
12 |
|
* the Free Software Foundation; either version 2 of the License, or |
13 |
|
* (at your option) any later version. |
14 |
|
* |
15 |
|
* This program is distributed in the hope that it will be useful, |
16 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
* GNU General Public License for more details. |
19 |
|
* |
20 |
|
* You should have received a copy of the GNU General Public License |
21 |
|
* along with this program; if not, write to the Free Software |
22 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
|
* |
24 |
|
* Under section 8 of the GNU General Public License, the copyright |
25 |
|
* holders of XVID explicitly forbid distribution in the following |
26 |
|
* countries: |
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 |
33 |
|
* GNU General Public License cover the whole combination. |
34 |
|
* |
35 |
|
* As a special exception, the copyright holders of XviD give you |
36 |
|
* 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 |
|
* combined work), being distributed under the terms of the GNU General |
43 |
|
* Public License plus this exception. An independent module is a module |
44 |
|
* which is not derived from or based on XviD. |
45 |
|
* |
46 |
|
* 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$ |
54 |
|
* |
55 |
|
****************************************************************************/ |
56 |
|
|
57 |
|
#include <stdlib.h> |
58 |
#include "../portab.h" |
#include "../portab.h" |
59 |
#include "bitstream.h" |
#include "bitstream.h" |
60 |
#include "zigzag.h" |
#include "zigzag.h" |
61 |
#include "vlc_codes.h" |
#include "vlc_codes.h" |
62 |
|
#include "mbcoding.h" |
63 |
|
|
64 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
65 |
|
|
|
#include <stdlib.h> /* malloc, free */ |
|
|
|
|
|
#define ESCAPE 7167 |
|
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 |
static VLC *DCT3D[2]; |
/* #define BIGLUT */ |
|
|
|
|
VLC *intra_table, *inter_table; |
|
|
static short clip_table[4096]; |
|
|
|
|
|
void create_vlc_tables(void) |
|
|
{ |
|
|
int32_t k, l, i, intra, last; |
|
|
VLC *vlc[2]; |
|
|
VLC **coeff_ptr; |
|
|
VLC *vlc1, *vlc2; |
|
|
|
|
|
VLC *DCT3Dintra; |
|
|
VLC *DCT3Dinter; |
|
70 |
|
|
71 |
DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096); |
#ifdef BIGLUT |
72 |
DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096); |
#define LEVELOFFSET 2048 |
73 |
|
#else |
74 |
|
#define LEVELOFFSET 32 |
75 |
|
#endif |
76 |
|
|
77 |
vlc1 = DCT3Dintra; |
/***************************************************************************** |
78 |
vlc2 = DCT3Dinter; |
* Local data |
79 |
|
****************************************************************************/ |
80 |
|
|
81 |
vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC)); |
static REVERSE_EVENT DCT3D[2][4096]; |
|
vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC)); |
|
82 |
|
|
83 |
// initialize the clipping table |
#ifdef BIGLUT |
84 |
for(i = -2048; i < 2048; i++) { |
static VLC coeff_VLC[2][2][4096][64]; |
85 |
clip_table[i + 2048] = i; |
static VLC *intra_table, *inter_table; |
86 |
if(i < -255) |
#else |
87 |
clip_table[i + 2048] = -255; |
static VLC coeff_VLC[2][2][64][64]; |
88 |
if(i > 255) |
#endif |
|
clip_table[i + 2048] = 255; |
|
|
} |
|
89 |
|
|
90 |
// generate intra/inter vlc lookup table |
/***************************************************************************** |
91 |
for(i = 0; i < 4; i++) { |
* Vector Length Coding Initialization |
92 |
intra = i % 2; |
****************************************************************************/ |
|
last = i >> 1; |
|
93 |
|
|
94 |
coeff_ptr = coeff_vlc[last + (intra << 1)]; |
void |
95 |
|
init_vlc_tables(void) |
96 |
|
{ |
97 |
|
ptr_t i, j, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; |
98 |
|
|
99 |
for(k = -255; k < 256; k++) { // level |
#ifdef BIGLUT |
100 |
char *max_level_ptr = max_level[last + (intra << 1)]; |
intra_table = (VLC*)coeff_VLC[1]; |
101 |
char *max_run_ptr = max_run[last + (intra << 1)]; |
inter_table = (VLC*)coeff_VLC[0]; |
102 |
|
#endif |
103 |
|
|
|
for(l = 0; l < 64; l++) { // run |
|
|
int32_t level = k, run = l; |
|
104 |
|
|
105 |
if(abs(level) <= max_level_ptr[run] && run <= max_run_ptr[abs(level)]) { |
for (intra = 0; intra < 2; intra++) |
106 |
|
for (i = 0; i < 4096; i++) |
107 |
|
DCT3D[intra][i].event.level = 0; |
108 |
|
|
109 |
if(level > 0) { |
for (intra = 0; intra < 2; intra++) |
110 |
vlc[intra]->code = (coeff_ptr[run][level - 1].code) << 1; |
for (last = 0; last < 2; last++) |
111 |
vlc[intra]->len = coeff_ptr[run][level - 1].len + 1; |
{ |
112 |
} |
for (run = 0; run < 63 + last; run++) |
113 |
else if(level < 0) { |
for (level = 0; level < 32 << intra; level++) |
114 |
vlc[intra]->code = ((coeff_ptr[run][-level - 1].code) << 1) + 1; |
{ |
115 |
vlc[intra]->len = coeff_ptr[run][-level - 1].len + 1; |
#ifdef BIGLUT |
116 |
|
offset = LEVELOFFSET; |
117 |
|
#else |
118 |
|
offset = !intra * LEVELOFFSET; |
119 |
|
#endif |
120 |
|
coeff_VLC[intra][last][level + offset][run].len = 128; |
121 |
} |
} |
|
else { |
|
|
vlc[intra]->code = 0; |
|
|
vlc[intra]->len = 0; |
|
122 |
} |
} |
|
} else { |
|
|
if(level > 0) |
|
|
level -= max_level_ptr[run]; |
|
|
else |
|
|
level += max_level_ptr[run]; |
|
123 |
|
|
124 |
if(abs(level) <= max_level_ptr[run] && |
for (intra = 0; intra < 2; intra++) |
125 |
run <= 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 |
if(level > 0) { |
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]->code = (0x06 << (coeff_ptr[run][level - 1].len + 1)) | |
= coeff_tab[intra][i].vlc.code << 1; |
140 |
(coeff_ptr[run][level - 1].code << 1); |
coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].len |
141 |
vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 8; |
= coeff_tab[intra][i].vlc.len + 1; |
142 |
} |
#ifndef BIGLUT |
143 |
else if(level < 0) { |
if (!intra) |
144 |
vlc[intra]->code = (0x06 << (coeff_ptr[run][-level - 1].len + 1)) | |
#endif |
145 |
((coeff_ptr[run][-level - 1].code << 1) + 1); |
{ |
146 |
vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 8; |
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 |
else { |
coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len |
149 |
vlc[intra]->code = 0x06; |
= coeff_tab[intra][i].vlc.len + 1; |
|
vlc[intra]->len = 8; |
|
150 |
} |
} |
|
} else { |
|
|
if(level > 0) |
|
|
level += max_level_ptr[run]; |
|
|
else |
|
|
level -= max_level_ptr[run]; |
|
|
DEBUG1("1) run:", run); |
|
|
run -= max_run_ptr[abs(level)] + 1; |
|
|
DEBUG1("2) run:", run); |
|
|
|
|
|
if(abs(level) <= max_level_ptr[run] && |
|
|
run <= max_run_ptr[abs(level)]) { |
|
|
|
|
|
if(level > 0) { |
|
|
vlc[intra]->code = (0x0e << (coeff_ptr[run][level - 1].len + 1)) | |
|
|
(coeff_ptr[run][level - 1].code << 1); |
|
|
vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 9; |
|
|
} |
|
|
else if(level < 0) { |
|
|
vlc[intra]->code = (0x0e << (coeff_ptr[run][-level - 1].len + 1)) | |
|
|
((coeff_ptr[run][-level - 1].code << 1) + 1); |
|
|
vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 9; |
|
|
} |
|
|
else { |
|
|
vlc[intra]->code = 0x0e; |
|
|
vlc[intra]->len = 9; |
|
151 |
} |
} |
|
} else { |
|
|
if(level != 0) |
|
|
run += max_run_ptr[abs(level)] + 1; |
|
|
else |
|
|
run++; |
|
152 |
|
|
153 |
DEBUG1("3) run:", run); |
for (intra = 0; intra < 2; intra++) |
154 |
|
for (last = 0; last < 2; last++) |
155 |
|
for (run = 0; run < 63 + last; run++) |
156 |
|
{ |
157 |
|
for (level = 1; level < 32 << intra; level++) |
158 |
|
{ |
159 |
|
if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level]) |
160 |
|
continue; |
161 |
|
|
162 |
vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) | |
#ifdef BIGLUT |
163 |
(l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1; |
offset = LEVELOFFSET; |
164 |
|
#else |
165 |
|
offset = !intra * LEVELOFFSET; |
166 |
|
#endif |
167 |
|
level_esc = level - max_level[intra][last][run]; |
168 |
|
run_esc = run - 1 - max_run[intra][last][level]; |
169 |
|
/*use this test to use shorter esc2 codes when possible |
170 |
|
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc] |
171 |
|
&& !(coeff_VLC[intra][last][level_esc + offset][run].len + 7 + 1 |
172 |
|
> coeff_VLC[intra][last][level + offset][run_esc].code + 7 + 2))*/ |
173 |
|
|
174 |
vlc[intra]->len = 30; |
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]) |
175 |
} |
{ |
176 |
|
escape = ESCAPE1; |
177 |
|
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 |
vlc[intra]++; |
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 |
} |
} |
203 |
} |
} |
|
intra_table += 64*255; // center vlc tables |
|
|
inter_table += 64*255; // center vlc tables |
|
204 |
|
|
205 |
for(i = 0; i < 4096; i++) { |
coeff_VLC[intra][last][level + offset][run].code |
206 |
if(i >= 512) { |
= (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len) |
207 |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
| coeff_VLC[intra][last][level_esc + offset][run_esc].code; |
208 |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
coeff_VLC[intra][last][level + offset][run].len |
209 |
} |
= coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len; |
210 |
else if(i >= 128) { |
#ifndef BIGLUT |
211 |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
if (!intra) |
212 |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
#endif |
213 |
} |
{ |
214 |
else if(i >= 8) { |
coeff_VLC[intra][last][offset - level][run].code |
215 |
*vlc1 = DCT3Dtab5[i - 8]; |
= (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len) |
216 |
*vlc2 = DCT3Dtab2[i - 8]; |
| 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 |
} |
} |
|
else { |
|
|
*vlc1 = ERRtab[i]; |
|
|
*vlc2 = ERRtab[i]; |
|
220 |
} |
} |
221 |
|
|
222 |
vlc1++; |
#ifdef BIGLUT |
223 |
vlc2++; |
for (level = 32 << intra; level < 2048; level++) |
224 |
} |
{ |
225 |
DCT3D[0] = DCT3Dinter; |
coeff_VLC[intra][last][level + offset][run].code |
226 |
DCT3D[1] = DCT3Dintra; |
= (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 |
void destroy_vlc_tables(void) { |
if (!intra) |
235 |
|
{ |
236 |
if(intra_table != NULL && inter_table != NULL) { |
coeff_VLC[intra][last][0][run].code |
237 |
intra_table -= 64*255; // uncenter vlc tables |
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1; |
238 |
inter_table -= 64*255; // uncenter vlc tables |
coeff_VLC[intra][last][0][run].len = 30; |
|
|
|
|
free(intra_table); |
|
|
free(inter_table); |
|
239 |
} |
} |
240 |
|
#endif |
|
if(DCT3D[0] != NULL && DCT3D[1] != NULL) { |
|
|
free(DCT3D[0]); |
|
|
free(DCT3D[1]); |
|
241 |
} |
} |
|
|
|
242 |
} |
} |
243 |
|
|
244 |
static __inline void CodeVector(Bitstream *bs, int16_t value, int16_t f_code, Statistics *pStat) |
/***************************************************************************** |
245 |
|
* Local inlined functions for MB coding |
246 |
|
****************************************************************************/ |
247 |
|
|
248 |
|
static __inline void |
249 |
|
CodeVector(Bitstream * bs, |
250 |
|
int32_t value, |
251 |
|
int32_t f_code, |
252 |
|
Statistics * pStat) |
253 |
{ |
{ |
254 |
|
|
255 |
const int scale_factor = 1 << (f_code - 1); |
const int scale_factor = 1 << (f_code - 1); |
256 |
const int cmp = scale_factor << 5; |
const int cmp = scale_factor << 5; |
257 |
|
|
264 |
pStat->iMvSum += value * value; |
pStat->iMvSum += value * value; |
265 |
pStat->iMvCount++; |
pStat->iMvCount++; |
266 |
|
|
267 |
if (value == 0) |
if (value == 0) { |
268 |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
BitstreamPutBits(bs, mb_motion_table[32].code, |
269 |
else { |
mb_motion_table[32].len); |
270 |
|
} else { |
271 |
uint16_t length, code, mv_res, sign; |
uint16_t length, code, mv_res, sign; |
272 |
|
|
273 |
length = 16 << f_code; |
length = 16 << f_code; |
291 |
code = -code; |
code = -code; |
292 |
|
|
293 |
code += 32; |
code += 32; |
294 |
BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len); |
BitstreamPutBits(bs, mb_motion_table[code].code, |
295 |
|
mb_motion_table[code].len); |
296 |
|
|
297 |
if(f_code) |
if(f_code) |
298 |
BitstreamPutBits(bs, mv_res, f_code); |
BitstreamPutBits(bs, mv_res, f_code); |
299 |
} |
} |
300 |
|
|
301 |
} |
} |
302 |
|
|
303 |
|
#ifdef BIGLUT |
304 |
|
|
305 |
|
static __inline void |
306 |
|
CodeCoeff(Bitstream * bs, |
307 |
|
const int16_t qcoeff[64], |
308 |
|
VLC * table, |
309 |
|
const uint16_t * zigzag, |
310 |
|
uint16_t intra) |
311 |
|
{ |
312 |
|
|
|
static __inline void CodeCoeff(Bitstream *bs, int16_t qcoeff[64], VLC *table, |
|
|
const uint16_t *zigzag, uint16_t intra) { |
|
313 |
uint32_t j, last; |
uint32_t j, last; |
314 |
short v; |
short v; |
315 |
VLC *vlc; |
VLC *vlc; |
316 |
|
|
317 |
j = intra; |
j = intra; |
318 |
last = 1 + intra; |
last = intra; |
319 |
|
|
320 |
while((v = qcoeff[zigzag[j++]]) == 0); |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
321 |
|
j++; |
322 |
|
|
323 |
do { |
do { |
324 |
// count zeroes |
vlc = table + 64 * 2048 + (v << 6) + j - last; |
325 |
vlc = table + (clip_table[2048+v] << 6) + j - last; |
last = ++j; |
326 |
last = j + 1; |
|
327 |
while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0); |
/* count zeroes */ |
328 |
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
329 |
|
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*511; |
vlc += 64 * 4096; |
336 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
337 |
break; |
break; |
338 |
} |
} |
339 |
} while(1); |
} while(1); |
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 |
static void CodeBlockIntra(const MBParam * pParam, const MACROBLOCK *pMB, |
while (i < 64) |
364 |
int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) |
{ |
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 |
464 |
|
CodeBlockIntra(const FRAMEINFO * frame, |
465 |
|
const MACROBLOCK * pMB, |
466 |
|
int16_t qcoeff[6 * 64], |
467 |
|
Bitstream * bs, |
468 |
|
Statistics * pStat) |
469 |
|
{ |
470 |
|
|
471 |
uint32_t i, mcbpc, cbpy, bits; |
uint32_t i, mcbpc, cbpy, bits; |
472 |
|
|
473 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
474 |
|
|
475 |
// write mcbpc |
/* write mcbpc */ |
476 |
if(pParam->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, mcbpc_intra_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
479 |
} |
mcbpc_intra_tab[mcbpc].len); |
480 |
else { |
} else { |
481 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
482 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
483 |
|
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 |
// code block coeffs |
/* write interlacing */ |
500 |
for(i = 0; i < 6; i++) |
if (frame->global_flags & XVID_INTERLACING) { |
501 |
{ |
BitstreamPutBit(bs, pMB->field_dct); |
502 |
|
} |
503 |
|
/* code block coeffs */ |
504 |
|
for (i = 0; i < 6; i++) { |
505 |
if(i < 4) |
if(i < 4) |
506 |
BitstreamPutBits(bs, dcy_tab[qcoeff[i][0] + 255].code, |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
507 |
dcy_tab[qcoeff[i][0] + 255].len); |
dcy_tab[qcoeff[i * 64 + 0] + 255].len); |
508 |
else |
else |
509 |
BitstreamPutBits(bs, dcc_tab[qcoeff[i][0] + 255].code, |
BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code, |
510 |
dcc_tab[qcoeff[i][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))) { |
|
{ |
|
513 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
514 |
|
|
515 |
CodeCoeff(bs, qcoeff[i], intra_table, scan_tables[pMB->acpred_directions[i]], 1); |
#ifdef BIGLUT |
516 |
|
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
517 |
|
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 |
} |
} |
524 |
} |
} |
525 |
|
|
526 |
} |
} |
527 |
|
|
528 |
|
|
529 |
static void CodeBlockInter(const MBParam * pParam, const MACROBLOCK *pMB, |
static void |
530 |
int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) |
CodeBlockInter(const FRAMEINFO * frame, |
531 |
|
const MACROBLOCK * pMB, |
532 |
|
int16_t qcoeff[6 * 64], |
533 |
|
Bitstream * bs, |
534 |
|
Statistics * pStat) |
535 |
{ |
{ |
536 |
|
|
537 |
int32_t i; |
int32_t i; |
538 |
uint32_t bits, mcbpc, cbpy; |
uint32_t bits, mcbpc, cbpy; |
539 |
|
|
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, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
545 |
|
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 |
// code motion vector(s) |
/* interlacing */ |
555 |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
if (frame->global_flags & XVID_INTERLACING) { |
556 |
{ |
if (pMB->cbp) { |
557 |
CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat); |
BitstreamPutBit(bs, pMB->field_dct); |
558 |
CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat); |
DPRINTF(DPRINTF_DEBUG, "codep: field_dct: %d", pMB->field_dct); |
559 |
|
} |
560 |
|
|
561 |
|
/* if inter block, write field ME flag */ |
562 |
|
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
563 |
|
BitstreamPutBit(bs, pMB->field_pred); |
564 |
|
DPRINTF(DPRINTF_DEBUG, "codep: field_pred: %d", pMB->field_pred); |
565 |
|
|
566 |
|
/* write field prediction references */ |
567 |
|
if (pMB->field_pred) { |
568 |
|
BitstreamPutBit(bs, pMB->field_for_top); |
569 |
|
BitstreamPutBit(bs, pMB->field_for_bot); |
570 |
|
} |
571 |
|
} |
572 |
|
} |
573 |
|
/* code motion vector(s) */ |
574 |
|
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
575 |
|
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
576 |
|
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
577 |
} |
} |
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 |
CodeCoeff(bs, qcoeff[i], inter_table, scan_tables[0], 0); |
#ifdef BIGLUT |
585 |
|
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 MBCoding(const MBParam * pParam, MACROBLOCK *pMB, |
void |
600 |
int16_t qcoeff[][64], |
MBCoding(const FRAMEINFO * frame, |
601 |
Bitstream * bs, Statistics * pStat) |
MACROBLOCK * pMB, |
602 |
|
int16_t qcoeff[6 * 64], |
603 |
|
Bitstream * bs, |
604 |
|
Statistics * pStat) |
605 |
{ |
{ |
|
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
|
606 |
|
|
607 |
if(pParam->coding_type == P_VOP) { |
if (frame->coding_type == P_VOP) { |
608 |
if(pMB->cbp == 0 && pMB->mode == MODE_INTER && |
BitstreamPutBit(bs, 0); /* coded */ |
609 |
pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
} |
610 |
|
|
611 |
|
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
612 |
|
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
613 |
|
else |
614 |
|
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
615 |
|
|
616 |
|
} |
617 |
|
|
618 |
|
|
619 |
|
void |
620 |
|
MBSkip(Bitstream * bs) |
621 |
{ |
{ |
622 |
BitstreamPutBit(bs, 1); // not_coded |
BitstreamPutBit(bs, 1); /* not coded */ |
623 |
return; |
return; |
624 |
} |
} |
625 |
else |
|
626 |
BitstreamPutBit(bs, 0); // coded |
/***************************************************************************** |
627 |
|
* decoding stuff starts here |
628 |
|
****************************************************************************/ |
629 |
|
|
630 |
|
/* |
631 |
|
* For IVOP addbits == 0 |
632 |
|
* For PVOP addbits == fcode - 1 |
633 |
|
* For BVOP addbits == max(fcode,bcode) - 1 |
634 |
|
* returns true or false |
635 |
|
*/ |
636 |
|
|
637 |
|
int |
638 |
|
check_resync_marker(Bitstream * bs, int addbits) |
639 |
|
{ |
640 |
|
uint32_t nbits; |
641 |
|
uint32_t code; |
642 |
|
uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits; |
643 |
|
|
644 |
|
nbits = BitstreamNumBitsToByteAlign(bs); |
645 |
|
code = BitstreamShowBits(bs, nbits); |
646 |
|
|
647 |
|
if (code == (((uint32_t)1 << (nbits - 1)) - 1)) |
648 |
|
{ |
649 |
|
return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER; |
650 |
} |
} |
651 |
|
|
652 |
if(intra) |
return 0; |
|
CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); |
|
|
else |
|
|
CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); |
|
653 |
} |
} |
654 |
|
|
655 |
|
|
|
/*************************************************************** |
|
|
* decoding stuff starts here * |
|
|
***************************************************************/ |
|
656 |
|
|
657 |
int get_mcbpc_intra(Bitstream * bs) |
int |
658 |
|
get_mcbpc_intra(Bitstream * bs) |
659 |
{ |
{ |
|
uint32_t index; |
|
660 |
|
|
661 |
while((index = BitstreamShowBits(bs, 9)) == 1) |
uint32_t index; |
|
BitstreamSkip(bs, 9); |
|
662 |
|
|
663 |
|
index = BitstreamShowBits(bs, 9); |
664 |
index >>= 3; |
index >>= 3; |
665 |
|
|
666 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
667 |
|
|
668 |
return mcbpc_intra_table[index].code; |
return mcbpc_intra_table[index].code; |
669 |
|
|
670 |
} |
} |
671 |
|
|
672 |
int get_mcbpc_inter(Bitstream * bs) |
int |
673 |
|
get_mcbpc_inter(Bitstream * bs) |
674 |
{ |
{ |
675 |
|
|
676 |
uint32_t index; |
uint32_t index; |
677 |
|
|
678 |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
|
BitstreamSkip(bs, 9); |
|
679 |
|
|
680 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
681 |
|
|
682 |
return mcbpc_inter_table[index].code; |
return mcbpc_inter_table[index].code; |
683 |
|
|
684 |
} |
} |
685 |
|
|
686 |
int get_cbpy(Bitstream * bs, int intra) |
int |
687 |
|
get_cbpy(Bitstream * bs, |
688 |
|
int intra) |
689 |
{ |
{ |
690 |
|
|
691 |
int cbpy; |
int cbpy; |
692 |
uint32_t index = BitstreamShowBits(bs, 6); |
uint32_t index = BitstreamShowBits(bs, 6); |
693 |
|
|
698 |
cbpy = 15 - cbpy; |
cbpy = 15 - cbpy; |
699 |
|
|
700 |
return cbpy; |
return cbpy; |
701 |
|
|
702 |
} |
} |
703 |
|
|
704 |
int get_mv_data(Bitstream * bs) |
int |
705 |
|
get_mv_data(Bitstream * bs) |
706 |
{ |
{ |
707 |
|
|
708 |
uint32_t index; |
uint32_t index; |
709 |
|
|
710 |
if(BitstreamGetBit(bs)) |
if(BitstreamGetBit(bs)) |
712 |
|
|
713 |
index = BitstreamShowBits(bs, 12); |
index = BitstreamShowBits(bs, 12); |
714 |
|
|
715 |
if(index >= 512) |
if (index >= 512) { |
|
{ |
|
716 |
index = (index >> 8) - 2; |
index = (index >> 8) - 2; |
717 |
BitstreamSkip(bs, TMNMVtab0[index].len); |
BitstreamSkip(bs, TMNMVtab0[index].len); |
718 |
return TMNMVtab0[index].code; |
return TMNMVtab0[index].code; |
719 |
} |
} |
720 |
|
|
721 |
if(index >= 128) |
if (index >= 128) { |
|
{ |
|
722 |
index = (index >> 2) - 32; |
index = (index >> 2) - 32; |
723 |
BitstreamSkip(bs, TMNMVtab1[index].len); |
BitstreamSkip(bs, TMNMVtab1[index].len); |
724 |
return TMNMVtab1[index].code; |
return TMNMVtab1[index].code; |
728 |
|
|
729 |
BitstreamSkip(bs, TMNMVtab2[index].len); |
BitstreamSkip(bs, TMNMVtab2[index].len); |
730 |
return TMNMVtab2[index].code; |
return TMNMVtab2[index].code; |
731 |
|
|
732 |
} |
} |
733 |
|
|
734 |
int get_mv(Bitstream * bs, int fcode) |
int |
735 |
|
get_mv(Bitstream * bs, |
736 |
|
int fcode) |
737 |
{ |
{ |
738 |
|
|
739 |
int data; |
int data; |
740 |
int res; |
int res; |
741 |
int mv; |
int mv; |
750 |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
751 |
|
|
752 |
return data < 0 ? -mv : mv; |
return data < 0 ? -mv : mv; |
753 |
|
|
754 |
} |
} |
755 |
|
|
756 |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
int |
757 |
|
get_dc_dif(Bitstream * bs, |
758 |
|
uint32_t dc_size) |
759 |
{ |
{ |
760 |
|
|
761 |
int code = BitstreamGetBits(bs, dc_size); |
int code = BitstreamGetBits(bs, dc_size); |
762 |
int msb = code >> (dc_size - 1); |
int msb = code >> (dc_size - 1); |
763 |
|
|
765 |
return (-1 * (code^((1 << dc_size) - 1))); |
return (-1 * (code^((1 << dc_size) - 1))); |
766 |
|
|
767 |
return code; |
return code; |
768 |
|
|
769 |
} |
} |
770 |
|
|
771 |
int get_dc_size_lum(Bitstream * bs) |
int |
772 |
|
get_dc_size_lum(Bitstream * bs) |
773 |
{ |
{ |
774 |
|
|
775 |
int code, i; |
int code, i; |
776 |
|
|
777 |
code = BitstreamShowBits(bs, 11); |
code = BitstreamShowBits(bs, 11); |
778 |
|
|
779 |
for(i = 11; i > 3; i--) { |
for(i = 11; i > 3; i--) { |
786 |
|
|
787 |
BitstreamSkip(bs, dc_lum_tab[code].len); |
BitstreamSkip(bs, dc_lum_tab[code].len); |
788 |
return dc_lum_tab[code].code; |
return dc_lum_tab[code].code; |
789 |
|
|
790 |
} |
} |
791 |
|
|
792 |
|
|
793 |
int get_dc_size_chrom(Bitstream * bs) |
int |
794 |
|
get_dc_size_chrom(Bitstream * bs) |
795 |
{ |
{ |
796 |
|
|
797 |
uint32_t code, i; |
uint32_t code, i; |
798 |
|
|
799 |
code = BitstreamShowBits(bs, 12); |
code = BitstreamShowBits(bs, 12); |
800 |
|
|
801 |
for(i = 12; i > 2; i--) { |
for(i = 12; i > 2; i--) { |
807 |
} |
} |
808 |
|
|
809 |
return 3 - BitstreamGetBits(bs, 2); |
return 3 - BitstreamGetBits(bs, 2); |
810 |
|
|
811 |
} |
} |
812 |
|
|
813 |
int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header) |
/***************************************************************************** |
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; |
uint32_t mode; |
|
const VLC *tab; |
|
826 |
int32_t level; |
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 |
if (short_video_header) /* inter-VLCs will be used for both intra and inter blocks */ |
830 |
intra = 0; |
intra = 0; |
831 |
|
|
832 |
tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
if (BitstreamShowBits(bs, 7) != ESCAPE) { |
833 |
|
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
834 |
|
|
835 |
if(tab->code == -1) |
if ((level = reverse_event->event.level) == 0) |
836 |
goto error; |
goto error; |
837 |
|
|
838 |
BitstreamSkip(bs, tab->len); |
*last = reverse_event->event.last; |
839 |
|
*run = reverse_event->event.run; |
840 |
|
|
841 |
if(tab->code != ESCAPE) { |
BitstreamSkip(bs, reverse_event->len); |
842 |
if(!intra) |
|
843 |
{ |
return BitstreamGetBits(bs, 1) ? -level : level; |
|
*run = (tab->code >> 4) & 255; |
|
|
level = tab->code & 15; |
|
|
*last = (tab->code >> 12) & 1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
*run = (tab->code >> 8) & 255; |
|
|
level = tab->code & 255; |
|
|
*last = (tab->code >> 16) & 1; |
|
|
} |
|
|
return BitstreamGetBit(bs) ? -level : level; |
|
844 |
} |
} |
845 |
|
|
846 |
if(short_video_header) |
BitstreamSkip(bs, 7); |
847 |
{ |
|
848 |
// escape mode 4 - H.263 type, only used if short_video_header = 1 |
if (short_video_header) { |
849 |
|
/* escape mode 4 - H.263 type, only used if short_video_header = 1 */ |
850 |
*last = BitstreamGetBit(bs); |
*last = BitstreamGetBit(bs); |
851 |
*run = BitstreamGetBits(bs, 6); |
*run = BitstreamGetBits(bs, 6); |
852 |
level = BitstreamGetBits(bs, 8); |
level = BitstreamGetBits(bs, 8); |
853 |
|
|
854 |
if (level == 0 || level == 128) |
if (level == 0 || level == 128) |
855 |
DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level); |
DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level); |
856 |
|
|
857 |
return (level >= 128 ? -(256 - level) : level); |
return (level << 24) >> 24; |
858 |
} |
} |
859 |
|
|
860 |
mode = BitstreamShowBits(bs, 2); |
mode = BitstreamShowBits(bs, 2); |
862 |
if(mode < 3) { |
if(mode < 3) { |
863 |
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
864 |
|
|
865 |
tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
866 |
if (tab->code == -1) |
|
867 |
|
if ((level = reverse_event->event.level) == 0) |
868 |
goto error; |
goto error; |
869 |
|
|
870 |
BitstreamSkip(bs, tab->len); |
*last = reverse_event->event.last; |
871 |
|
*run = reverse_event->event.run; |
872 |
|
|
873 |
if (!intra) { |
BitstreamSkip(bs, reverse_event->len); |
|
*run = (tab->code >> 4) & 255; |
|
|
level = tab->code & 15; |
|
|
*last = (tab->code >> 12) & 1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
*run = (tab->code >> 8) & 255; |
|
|
level = tab->code & 255; |
|
|
*last = (tab->code >> 16) & 1; |
|
|
} |
|
874 |
|
|
875 |
if(mode < 2) // first escape mode, level is offset |
if (mode < 2) /* first escape mode, level is offset */ |
876 |
level += max_level[*last + (!intra<<1)][*run]; // need to add back the max level |
level += max_level[intra][*last][*run]; |
877 |
else if(mode == 2) // second escape mode, run is offset |
else /* second escape mode, run is offset */ |
878 |
*run += max_run[*last + (!intra<<1)][level] + 1; |
*run += max_run[intra][*last][level] + 1; |
879 |
|
|
880 |
return BitstreamGetBit(bs) ? -level : level; |
return BitstreamGetBits(bs, 1) ? -level : level; |
881 |
} |
} |
882 |
|
|
883 |
// third escape mode - fixed length codes |
/* third escape mode - fixed length codes */ |
884 |
BitstreamSkip(bs, 2); |
BitstreamSkip(bs, 2); |
885 |
*last = BitstreamGetBits(bs, 1); |
*last = BitstreamGetBits(bs, 1); |
886 |
*run = BitstreamGetBits(bs, 6); |
*run = BitstreamGetBits(bs, 6); |
887 |
BitstreamSkip(bs, 1); // marker |
BitstreamSkip(bs, 1); /* marker */ |
888 |
level = BitstreamGetBits(bs, 12); |
level = BitstreamGetBits(bs, 12); |
889 |
BitstreamSkip(bs, 1); // marker |
BitstreamSkip(bs, 1); /* marker */ |
890 |
|
|
891 |
return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level; |
return (level << 20) >> 20; |
892 |
|
|
893 |
error: |
error: |
894 |
*run = VLC_ERROR; |
*run = VLC_ERROR; |
895 |
return 0; |
return 0; |
896 |
} |
} |
897 |
|
|
898 |
|
/***************************************************************************** |
899 |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
* MB reading functions |
900 |
|
****************************************************************************/ |
901 |
|
|
902 |
|
void |
903 |
|
get_intra_block(Bitstream * bs, |
904 |
|
int16_t * block, |
905 |
|
int direction, |
906 |
|
int coeff) |
907 |
{ |
{ |
908 |
|
|
909 |
const uint16_t * scan = scan_tables[ direction ]; |
const uint16_t * scan = scan_tables[ direction ]; |
910 |
int level; |
int level; |
911 |
int run; |
int run; |
912 |
int last; |
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_DEBUG, "fatal: invalid run"); |
|
DEBUG("fatal: invalid run"); |
|
918 |
break; |
break; |
919 |
} |
} |
920 |
coeff += run; |
coeff += run; |
921 |
block[ scan[coeff] ] = level; |
block[ scan[coeff] ] = level; |
922 |
if (level < -127 || level > 127) |
|
923 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
924 |
DEBUG1("warning: intra_overflow", level); |
/*DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); */ |
925 |
|
|
926 |
|
if (level < -2047 || level > 2047) { |
927 |
|
DPRINTF(DPRINTF_DEBUG, "warning: intra_overflow: %d", level); |
928 |
} |
} |
929 |
coeff++; |
coeff++; |
930 |
} while (!last); |
} while (!last); |
931 |
|
|
932 |
} |
} |
933 |
|
|
934 |
void get_inter_block(Bitstream * bs, int16_t * block) |
void |
935 |
|
get_inter_block(Bitstream * bs, |
936 |
|
int16_t * block) |
937 |
{ |
{ |
938 |
|
|
939 |
const uint16_t * scan = scan_tables[0]; |
const uint16_t * scan = scan_tables[0]; |
940 |
int p; |
int p; |
941 |
int level; |
int level; |
943 |
int last; |
int last; |
944 |
|
|
945 |
p = 0; |
p = 0; |
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 |
{ |
DPRINTF(DPRINTF_ERROR, "fatal: invalid run"); |
|
DEBUG("fatal: invalid run"); |
|
950 |
break; |
break; |
951 |
} |
} |
952 |
p += run; |
p += run; |
953 |
|
|
954 |
block[ scan[p] ] = level; |
block[ scan[p] ] = level; |
955 |
if (level < -127 || level > 127) |
|
956 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
957 |
DEBUG1("warning: inter_overflow", level); |
|
958 |
|
if (level < -2047 || level > 2047) { |
959 |
|
DPRINTF(DPRINTF_DEBUG, "warning: inter_overflow: %d", level); |
960 |
} |
} |
961 |
p++; |
p++; |
962 |
} while (!last); |
} while (!last); |
963 |
|
|
964 |
} |
} |