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: mbcoding.c,v 1.40 2003-02-06 00:48:08 edgomez Exp $ |
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 |
|
|
|
#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]; |
|
70 |
|
|
71 |
void create_vlc_tables(void) |
#ifdef BIGLUT |
72 |
{ |
#define LEVELOFFSET 2048 |
73 |
int32_t k, l, i, intra, last; |
#else |
74 |
VLC *vlc[2]; |
#define LEVELOFFSET 32 |
75 |
VLC **coeff_ptr; |
#endif |
|
VLC *vlc1, *vlc2; |
|
76 |
|
|
77 |
VLC *DCT3Dintra; |
/***************************************************************************** |
78 |
VLC *DCT3Dinter; |
* Local data |
79 |
|
****************************************************************************/ |
80 |
|
|
81 |
DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096); |
static REVERSE_EVENT DCT3D[2][4096]; |
|
DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096); |
|
82 |
|
|
83 |
vlc1 = DCT3Dintra; |
#ifdef BIGLUT |
84 |
vlc2 = DCT3Dinter; |
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 |
vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC)); |
/***************************************************************************** |
91 |
vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC)); |
* Vector Length Coding Initialization |
92 |
|
****************************************************************************/ |
93 |
|
|
94 |
// initialize the clipping table |
void |
95 |
for(i = -2048; i < 2048; i++) { |
init_vlc_tables(void) |
96 |
clip_table[i + 2048] = i; |
{ |
97 |
if(i < -255) |
uint32_t i, j, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; |
|
clip_table[i + 2048] = -255; |
|
|
if(i > 255) |
|
|
clip_table[i + 2048] = 255; |
|
|
} |
|
|
|
|
|
// generate intra/inter vlc lookup table |
|
|
for(i = 0; i < 4; i++) { |
|
|
intra = i % 2; |
|
|
last = i >> 1; |
|
|
|
|
|
coeff_ptr = coeff_vlc[last + (intra << 1)]; |
|
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]; |
|
|
|
|
|
if(abs(level) <= max_level_ptr[run] && |
|
|
run <= max_run_ptr[abs(level)]) { |
|
123 |
|
|
124 |
if(level > 0) { |
for (intra = 0; intra < 2; intra++) |
125 |
vlc[intra]->code = (0x06 << (coeff_ptr[run][level - 1].len + 1)) | |
for (i = 0; i < 102; i++) |
126 |
(coeff_ptr[run][level - 1].code << 1); |
{ |
127 |
vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 8; |
#ifdef BIGLUT |
128 |
} |
offset = LEVELOFFSET; |
129 |
else if(level < 0) { |
#else |
130 |
vlc[intra]->code = (0x06 << (coeff_ptr[run][-level - 1].len + 1)) | |
offset = !intra * LEVELOFFSET; |
131 |
((coeff_ptr[run][-level - 1].code << 1) + 1); |
#endif |
132 |
vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 8; |
for (j = 0; j < (uint32_t)(1 << (12 - coeff_tab[intra][i].vlc.len)); j++) |
133 |
} |
{ |
134 |
else { |
DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len = coeff_tab[intra][i].vlc.len; |
135 |
vlc[intra]->code = 0x06; |
DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event; |
|
vlc[intra]->len = 8; |
|
136 |
} |
} |
|
} else { |
|
|
if(level > 0) |
|
|
level += max_level_ptr[run]; |
|
|
else |
|
|
level -= max_level_ptr[run]; |
|
|
|
|
|
run -= max_run_ptr[abs(level)] + 1; |
|
|
|
|
|
if(abs(level) <= max_level_ptr[run] && |
|
|
run <= max_run_ptr[abs(level)]) { |
|
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 = (0x0e << (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) + 9; |
= coeff_tab[intra][i].vlc.len + 1; |
142 |
} |
#ifndef BIGLUT |
143 |
else if(level < 0) { |
if (!intra) |
144 |
vlc[intra]->code = (0x0e << (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) + 9; |
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 |
} |
} |
|
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 |
vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) | |
for (intra = 0; intra < 2; intra++) |
154 |
(l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1; |
for (last = 0; last < 2; last++) |
155 |
|
for (run = 0; run < 63 + last; run++) |
156 |
|
{ |
157 |
|
for (level = 1; level < (uint32_t)(32 << intra); level++) |
158 |
|
{ |
159 |
|
if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level]) |
160 |
|
continue; |
161 |
|
|
162 |
vlc[intra]->len = 30; |
#ifdef BIGLUT |
163 |
} |
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 |
|
|
170 |
|
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]) |
171 |
|
{ |
172 |
|
escape = ESCAPE1; |
173 |
|
escape_len = 7 + 1; |
174 |
|
run_esc = run; |
175 |
} |
} |
176 |
|
else |
177 |
|
{ |
178 |
|
if (run_esc <= max_run[intra][last][level] && level <= max_level[intra][last][run_esc]) |
179 |
|
{ |
180 |
|
escape = ESCAPE2; |
181 |
|
escape_len = 7 + 2; |
182 |
|
level_esc = level; |
183 |
} |
} |
184 |
vlc[intra]++; |
else |
185 |
|
{ |
186 |
|
#ifndef BIGLUT |
187 |
|
if (!intra) |
188 |
|
#endif |
189 |
|
{ |
190 |
|
coeff_VLC[intra][last][level + offset][run].code |
191 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
192 |
|
coeff_VLC[intra][last][level + offset][run].len = 30; |
193 |
|
coeff_VLC[intra][last][offset - level][run].code |
194 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1; |
195 |
|
coeff_VLC[intra][last][offset - level][run].len = 30; |
196 |
} |
} |
197 |
|
continue; |
198 |
} |
} |
199 |
} |
} |
|
intra_table += 64*255; // center vlc tables |
|
|
inter_table += 64*255; // center vlc tables |
|
200 |
|
|
201 |
for(i = 0; i < 4096; i++) { |
coeff_VLC[intra][last][level + offset][run].code |
202 |
if(i >= 512) { |
= (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len) |
203 |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
| coeff_VLC[intra][last][level_esc + offset][run_esc].code; |
204 |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
coeff_VLC[intra][last][level + offset][run].len |
205 |
} |
= coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len; |
206 |
else if(i >= 128) { |
#ifndef BIGLUT |
207 |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
if (!intra) |
208 |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
#endif |
209 |
} |
{ |
210 |
else if(i >= 8) { |
coeff_VLC[intra][last][offset - level][run].code |
211 |
*vlc1 = DCT3Dtab5[i - 8]; |
= (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len) |
212 |
*vlc2 = DCT3Dtab2[i - 8]; |
| coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1; |
213 |
|
coeff_VLC[intra][last][offset - level][run].len |
214 |
|
= coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len; |
215 |
} |
} |
|
else { |
|
|
*vlc1 = ERRtab[i]; |
|
|
*vlc2 = ERRtab[i]; |
|
216 |
} |
} |
217 |
|
|
218 |
vlc1++; |
#ifdef BIGLUT |
219 |
vlc2++; |
for (level = (uint32_t)(32 << intra); level < 2048; level++) |
220 |
} |
{ |
221 |
DCT3D[0] = DCT3Dinter; |
coeff_VLC[intra][last][level + offset][run].code |
222 |
DCT3D[1] = DCT3Dintra; |
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
223 |
|
coeff_VLC[intra][last][level + offset][run].len = 30; |
224 |
|
|
225 |
|
coeff_VLC[intra][last][offset - level][run].code |
226 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1; |
227 |
|
coeff_VLC[intra][last][offset - level][run].len = 30; |
228 |
} |
} |
229 |
|
#else |
230 |
void destroy_vlc_tables(void) { |
if (!intra) |
231 |
|
{ |
232 |
if(intra_table != NULL && inter_table != NULL) { |
coeff_VLC[intra][last][0][run].code |
233 |
intra_table -= 64*255; // uncenter vlc tables |
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1; |
234 |
inter_table -= 64*255; // uncenter vlc tables |
coeff_VLC[intra][last][0][run].len = 30; |
|
|
|
|
free(intra_table); |
|
|
free(inter_table); |
|
235 |
} |
} |
236 |
|
#endif |
|
if(DCT3D[0] != NULL && DCT3D[1] != NULL) { |
|
|
free(DCT3D[0]); |
|
|
free(DCT3D[1]); |
|
237 |
} |
} |
|
|
|
238 |
} |
} |
239 |
|
|
240 |
static __inline void CodeVector(Bitstream *bs, int16_t value, int16_t f_code, Statistics *pStat) |
/***************************************************************************** |
241 |
|
* Local inlined functions for MB coding |
242 |
|
****************************************************************************/ |
243 |
|
|
244 |
|
static __inline void |
245 |
|
CodeVector(Bitstream * bs, |
246 |
|
int32_t value, |
247 |
|
int32_t f_code, |
248 |
|
Statistics * pStat) |
249 |
{ |
{ |
250 |
|
|
251 |
const int scale_factor = 1 << (f_code - 1); |
const int scale_factor = 1 << (f_code - 1); |
252 |
const int cmp = scale_factor << 5; |
const int cmp = scale_factor << 5; |
253 |
|
|
260 |
pStat->iMvSum += value * value; |
pStat->iMvSum += value * value; |
261 |
pStat->iMvCount++; |
pStat->iMvCount++; |
262 |
|
|
263 |
if (value == 0) |
if (value == 0) { |
264 |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
BitstreamPutBits(bs, mb_motion_table[32].code, |
265 |
else { |
mb_motion_table[32].len); |
266 |
|
} else { |
267 |
uint16_t length, code, mv_res, sign; |
uint16_t length, code, mv_res, sign; |
268 |
|
|
269 |
length = 16 << f_code; |
length = 16 << f_code; |
287 |
code = -code; |
code = -code; |
288 |
|
|
289 |
code += 32; |
code += 32; |
290 |
BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len); |
BitstreamPutBits(bs, mb_motion_table[code].code, |
291 |
|
mb_motion_table[code].len); |
292 |
|
|
293 |
if(f_code) |
if(f_code) |
294 |
BitstreamPutBits(bs, mv_res, f_code); |
BitstreamPutBits(bs, mv_res, f_code); |
295 |
} |
} |
296 |
|
|
297 |
} |
} |
298 |
|
|
299 |
|
#ifdef BIGLUT |
300 |
|
|
301 |
|
static __inline void |
302 |
|
CodeCoeff(Bitstream * bs, |
303 |
|
const int16_t qcoeff[64], |
304 |
|
VLC * table, |
305 |
|
const uint16_t * zigzag, |
306 |
|
uint16_t intra) |
307 |
|
{ |
308 |
|
|
|
static __inline void CodeCoeff(Bitstream *bs, int16_t qcoeff[64], VLC *table, |
|
|
const uint16_t *zigzag, uint16_t intra) { |
|
309 |
uint32_t j, last; |
uint32_t j, last; |
310 |
short v; |
short v; |
311 |
VLC *vlc; |
VLC *vlc; |
312 |
|
|
313 |
j = intra; |
j = intra; |
314 |
last = 1 + intra; |
last = intra; |
315 |
|
|
316 |
while((v = qcoeff[zigzag[j++]]) == 0); |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
317 |
|
j++; |
318 |
|
|
319 |
do { |
do { |
320 |
// count zeroes |
vlc = table + 64 * 2048 + (v << 6) + j - last; |
321 |
vlc = table + (clip_table[2048+v] << 6) + j - last; |
last = ++j; |
322 |
last = j + 1; |
|
323 |
while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0); |
/* count zeroes */ |
324 |
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
325 |
|
j++; |
326 |
|
|
327 |
// write code |
/* write code */ |
328 |
if(j != 64) { |
if(j != 64) { |
329 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
330 |
} else { |
} else { |
331 |
vlc += 64*511; |
vlc += 64 * 4096; |
332 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
333 |
break; |
break; |
334 |
} |
} |
335 |
} while(1); |
} while(1); |
336 |
|
|
337 |
} |
} |
338 |
|
|
339 |
|
#else |
340 |
|
|
341 |
static void CodeBlockIntra(const MBParam * pParam, const MACROBLOCK *pMB, |
static __inline void |
342 |
int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) |
CodeCoeffInter(Bitstream * bs, |
343 |
|
const int16_t qcoeff[64], |
344 |
|
const uint16_t * zigzag) |
345 |
{ |
{ |
346 |
|
uint32_t i, run, prev_run, code, len; |
347 |
|
int32_t level, prev_level, level_shifted; |
348 |
|
|
349 |
|
i = 0; |
350 |
|
run = 0; |
351 |
|
|
352 |
|
while (!(level = qcoeff[zigzag[i++]])) |
353 |
|
run++; |
354 |
|
|
355 |
|
prev_level = level; |
356 |
|
prev_run = run; |
357 |
|
run = 0; |
358 |
|
|
359 |
|
while (i < 64) |
360 |
|
{ |
361 |
|
if ((level = qcoeff[zigzag[i++]]) != 0) |
362 |
|
{ |
363 |
|
level_shifted = prev_level + 32; |
364 |
|
if (!(level_shifted & -64)) |
365 |
|
{ |
366 |
|
code = coeff_VLC[0][0][level_shifted][prev_run].code; |
367 |
|
len = coeff_VLC[0][0][level_shifted][prev_run].len; |
368 |
|
} |
369 |
|
else |
370 |
|
{ |
371 |
|
code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
372 |
|
len = 30; |
373 |
|
} |
374 |
|
BitstreamPutBits(bs, code, len); |
375 |
|
prev_level = level; |
376 |
|
prev_run = run; |
377 |
|
run = 0; |
378 |
|
} |
379 |
|
else |
380 |
|
run++; |
381 |
|
} |
382 |
|
|
383 |
|
level_shifted = prev_level + 32; |
384 |
|
if (!(level_shifted & -64)) |
385 |
|
{ |
386 |
|
code = coeff_VLC[0][1][level_shifted][prev_run].code; |
387 |
|
len = coeff_VLC[0][1][level_shifted][prev_run].len; |
388 |
|
} |
389 |
|
else |
390 |
|
{ |
391 |
|
code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
392 |
|
len = 30; |
393 |
|
} |
394 |
|
BitstreamPutBits(bs, code, len); |
395 |
|
} |
396 |
|
|
397 |
|
static __inline void |
398 |
|
CodeCoeffIntra(Bitstream * bs, |
399 |
|
const int16_t qcoeff[64], |
400 |
|
const uint16_t * zigzag) |
401 |
|
{ |
402 |
|
uint32_t i, abs_level, run, prev_run, code, len; |
403 |
|
int32_t level, prev_level; |
404 |
|
|
405 |
|
i = 1; |
406 |
|
run = 0; |
407 |
|
|
408 |
|
while (!(level = qcoeff[zigzag[i++]])) |
409 |
|
run++; |
410 |
|
|
411 |
|
prev_level = level; |
412 |
|
prev_run = run; |
413 |
|
run = 0; |
414 |
|
|
415 |
|
while (i < 64) |
416 |
|
{ |
417 |
|
if ((level = qcoeff[zigzag[i++]]) != 0) |
418 |
|
{ |
419 |
|
abs_level = ABS(prev_level); |
420 |
|
abs_level = abs_level < 64 ? abs_level : 0; |
421 |
|
code = coeff_VLC[1][0][abs_level][prev_run].code; |
422 |
|
len = coeff_VLC[1][0][abs_level][prev_run].len; |
423 |
|
if (len != 128) |
424 |
|
code |= (prev_level < 0); |
425 |
|
else |
426 |
|
{ |
427 |
|
code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
428 |
|
len = 30; |
429 |
|
} |
430 |
|
BitstreamPutBits(bs, code, len); |
431 |
|
prev_level = level; |
432 |
|
prev_run = run; |
433 |
|
run = 0; |
434 |
|
} |
435 |
|
else |
436 |
|
run++; |
437 |
|
} |
438 |
|
|
439 |
|
abs_level = ABS(prev_level); |
440 |
|
abs_level = abs_level < 64 ? abs_level : 0; |
441 |
|
code = coeff_VLC[1][1][abs_level][prev_run].code; |
442 |
|
len = coeff_VLC[1][1][abs_level][prev_run].len; |
443 |
|
if (len != 128) |
444 |
|
code |= (prev_level < 0); |
445 |
|
else |
446 |
|
{ |
447 |
|
code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
448 |
|
len = 30; |
449 |
|
} |
450 |
|
BitstreamPutBits(bs, code, len); |
451 |
|
} |
452 |
|
|
453 |
|
#endif |
454 |
|
|
455 |
|
/***************************************************************************** |
456 |
|
* Local functions |
457 |
|
****************************************************************************/ |
458 |
|
|
459 |
|
static void |
460 |
|
CodeBlockIntra(const FRAMEINFO * frame, |
461 |
|
const MACROBLOCK * pMB, |
462 |
|
int16_t qcoeff[6 * 64], |
463 |
|
Bitstream * bs, |
464 |
|
Statistics * pStat) |
465 |
|
{ |
466 |
|
|
467 |
uint32_t i, mcbpc, cbpy, bits; |
uint32_t i, mcbpc, cbpy, bits; |
468 |
|
|
|
mcbpc = pMB->cbp & 3; |
|
469 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
470 |
|
|
471 |
// write mcbpc |
/* write mcbpc */ |
472 |
if(pParam->coding_type == I_VOP) |
if (frame->coding_type == I_VOP) { |
473 |
BitstreamPutBits(bs, mcbpc_I[mcbpc].code, mcbpc_I[mcbpc].len); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
474 |
else |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
475 |
BitstreamPutBits(bs, mcbpc_P_intra[mcbpc].code, mcbpc_P_intra[mcbpc].len); |
mcbpc_intra_tab[mcbpc].len); |
476 |
|
} else { |
477 |
|
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
478 |
|
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
479 |
|
mcbpc_inter_tab[mcbpc].len); |
480 |
|
} |
481 |
|
|
482 |
// ac prediction flag |
/* ac prediction flag */ |
483 |
if(pMB->acpred_directions[0]) |
if(pMB->acpred_directions[0]) |
484 |
BitstreamPutBits(bs, 1, 1); |
BitstreamPutBits(bs, 1, 1); |
485 |
else |
else |
486 |
BitstreamPutBits(bs, 0, 1); |
BitstreamPutBits(bs, 0, 1); |
487 |
|
|
488 |
// write cbpy |
/* write cbpy */ |
489 |
BitstreamPutBits (bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits (bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
490 |
|
|
491 |
// write dquant |
/* write dquant */ |
492 |
if(pMB->mode == MODE_INTRA_Q) |
if(pMB->mode == MODE_INTRA_Q) |
493 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
494 |
|
|
495 |
// code block coeffs |
/* write interlacing */ |
496 |
for(i = 0; i < 6; i++) |
if (frame->global_flags & XVID_INTERLACING) { |
497 |
{ |
BitstreamPutBit(bs, pMB->field_dct); |
498 |
|
} |
499 |
|
/* code block coeffs */ |
500 |
|
for (i = 0; i < 6; i++) { |
501 |
if(i < 4) |
if(i < 4) |
502 |
BitstreamPutBits(bs, dcy_tab[qcoeff[i][0] + 255].code, |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
503 |
dcy_tab[qcoeff[i][0] + 255].len); |
dcy_tab[qcoeff[i * 64 + 0] + 255].len); |
504 |
else |
else |
505 |
BitstreamPutBits(bs, dcc_tab[qcoeff[i][0] + 255].code, |
BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code, |
506 |
dcc_tab[qcoeff[i][0] + 255].len); |
dcc_tab[qcoeff[i * 64 + 0] + 255].len); |
507 |
|
|
508 |
if(pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) { |
|
{ |
|
509 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
510 |
|
|
511 |
CodeCoeff(bs, qcoeff[i], intra_table, scan_tables[pMB->acpred_directions[i]], 1); |
#ifdef BIGLUT |
512 |
|
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
513 |
|
scan_tables[pMB->acpred_directions[i]], 1); |
514 |
|
#else |
515 |
|
CodeCoeffIntra(bs, &qcoeff[i * 64], scan_tables[pMB->acpred_directions[i]]); |
516 |
|
#endif |
517 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
518 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
519 |
} |
} |
520 |
} |
} |
521 |
|
|
522 |
} |
} |
523 |
|
|
524 |
|
|
525 |
static void CodeBlockInter(const MBParam * pParam, const MACROBLOCK *pMB, |
static void |
526 |
int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) |
CodeBlockInter(const FRAMEINFO * frame, |
527 |
|
const MACROBLOCK * pMB, |
528 |
|
int16_t qcoeff[6 * 64], |
529 |
|
Bitstream * bs, |
530 |
|
Statistics * pStat) |
531 |
{ |
{ |
532 |
|
|
533 |
int32_t i; |
int32_t i; |
534 |
uint32_t bits, mcbpc, cbpy; |
uint32_t bits, mcbpc, cbpy; |
535 |
|
|
536 |
mcbpc = pMB->cbp & 3; |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
537 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
538 |
|
|
539 |
// write mcbpc |
/* write mcbpc */ |
540 |
if(pMB->mode == MODE_INTER4V) |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
541 |
BitstreamPutBits(bs, mcbpc_P_inter4v[mcbpc].code, mcbpc_P_inter4v[mcbpc].len); |
mcbpc_inter_tab[mcbpc].len); |
|
else |
|
|
BitstreamPutBits(bs, mcbpc_P_inter[mcbpc].code, mcbpc_P_inter[mcbpc].len); |
|
542 |
|
|
543 |
// write cbpy |
/* write cbpy */ |
544 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
545 |
|
|
546 |
// write dquant |
/* write dquant */ |
547 |
if(pMB->mode == MODE_INTER_Q) |
if(pMB->mode == MODE_INTER_Q) |
548 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
549 |
|
|
550 |
// code motion vector(s) |
/* interlacing */ |
551 |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
if (frame->global_flags & XVID_INTERLACING) { |
552 |
{ |
if (pMB->cbp) { |
553 |
CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat); |
BitstreamPutBit(bs, pMB->field_dct); |
554 |
CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat); |
DPRINTF(DPRINTF_DEBUG, "codep: field_dct: %d", pMB->field_dct); |
555 |
|
} |
556 |
|
|
557 |
|
/* if inter block, write field ME flag */ |
558 |
|
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
559 |
|
BitstreamPutBit(bs, pMB->field_pred); |
560 |
|
DPRINTF(DPRINTF_DEBUG, "codep: field_pred: %d", pMB->field_pred); |
561 |
|
|
562 |
|
/* write field prediction references */ |
563 |
|
if (pMB->field_pred) { |
564 |
|
BitstreamPutBit(bs, pMB->field_for_top); |
565 |
|
BitstreamPutBit(bs, pMB->field_for_bot); |
566 |
|
} |
567 |
|
} |
568 |
|
} |
569 |
|
/* code motion vector(s) */ |
570 |
|
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
571 |
|
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
572 |
|
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
573 |
} |
} |
574 |
|
|
575 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
576 |
|
|
577 |
// code block coeffs |
/* code block coeffs */ |
578 |
for(i = 0; i < 6; i++) |
for(i = 0; i < 6; i++) |
579 |
if(pMB->cbp & (1 << (5 - i))) |
if(pMB->cbp & (1 << (5 - i))) |
580 |
CodeCoeff(bs, qcoeff[i], inter_table, scan_tables[0], 0); |
#ifdef BIGLUT |
581 |
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
582 |
|
#else |
583 |
|
CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]); |
584 |
|
#endif |
585 |
|
|
586 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
587 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
588 |
|
|
589 |
} |
} |
590 |
|
|
591 |
|
/***************************************************************************** |
592 |
|
* Macro Block bitstream encoding functions |
593 |
|
****************************************************************************/ |
594 |
|
|
595 |
void MBCoding(const MBParam * pParam, MACROBLOCK *pMB, |
void |
596 |
int16_t qcoeff[][64], |
MBCoding(const FRAMEINFO * frame, |
597 |
Bitstream * bs, Statistics * pStat) |
MACROBLOCK * pMB, |
598 |
|
int16_t qcoeff[6 * 64], |
599 |
|
Bitstream * bs, |
600 |
|
Statistics * pStat) |
601 |
{ |
{ |
|
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
|
602 |
|
|
603 |
if(pParam->coding_type == P_VOP) { |
if (frame->coding_type == P_VOP) { |
604 |
if(pMB->cbp == 0 && pMB->mode == MODE_INTER && |
BitstreamPutBit(bs, 0); /* coded */ |
605 |
pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
} |
606 |
|
|
607 |
|
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
608 |
|
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
609 |
|
else |
610 |
|
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
611 |
|
|
612 |
|
} |
613 |
|
|
614 |
|
|
615 |
|
void |
616 |
|
MBSkip(Bitstream * bs) |
617 |
{ |
{ |
618 |
BitstreamPutBit(bs, 1); // not_coded |
BitstreamPutBit(bs, 1); /* not coded */ |
619 |
return; |
return; |
620 |
} |
} |
621 |
else |
|
622 |
BitstreamPutBit(bs, 0); // coded |
/***************************************************************************** |
623 |
|
* decoding stuff starts here |
624 |
|
****************************************************************************/ |
625 |
|
|
626 |
|
/* |
627 |
|
* For IVOP addbits == 0 |
628 |
|
* For PVOP addbits == fcode - 1 |
629 |
|
* For BVOP addbits == max(fcode,bcode) - 1 |
630 |
|
* returns true or false |
631 |
|
*/ |
632 |
|
|
633 |
|
int |
634 |
|
check_resync_marker(Bitstream * bs, int addbits) |
635 |
|
{ |
636 |
|
uint32_t nbits; |
637 |
|
uint32_t code; |
638 |
|
uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits; |
639 |
|
|
640 |
|
nbits = BitstreamNumBitsToByteAlign(bs); |
641 |
|
code = BitstreamShowBits(bs, nbits); |
642 |
|
|
643 |
|
if (code == (((uint32_t)1 << (nbits - 1)) - 1)) |
644 |
|
{ |
645 |
|
return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER; |
646 |
} |
} |
647 |
|
|
648 |
if(intra) |
return 0; |
|
CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); |
|
|
else |
|
|
CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); |
|
649 |
} |
} |
650 |
|
|
651 |
|
|
|
/*************************************************************** |
|
|
* decoding stuff starts here * |
|
|
***************************************************************/ |
|
652 |
|
|
653 |
int get_mcbpc_intra(Bitstream * bs) |
int |
654 |
|
get_mcbpc_intra(Bitstream * bs) |
655 |
{ |
{ |
|
uint32_t index; |
|
656 |
|
|
657 |
while((index = BitstreamShowBits(bs, 9)) == 1) |
uint32_t index; |
|
BitstreamSkip(bs, 9); |
|
658 |
|
|
659 |
|
index = BitstreamShowBits(bs, 9); |
660 |
index >>= 3; |
index >>= 3; |
661 |
|
|
662 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
663 |
|
|
664 |
return mcbpc_intra_table[index].code; |
return mcbpc_intra_table[index].code; |
665 |
|
|
666 |
} |
} |
667 |
|
|
668 |
int get_mcbpc_inter(Bitstream * bs) |
int |
669 |
|
get_mcbpc_inter(Bitstream * bs) |
670 |
{ |
{ |
671 |
|
|
672 |
uint32_t index; |
uint32_t index; |
673 |
|
|
674 |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
|
BitstreamSkip(bs, 9); |
|
675 |
|
|
676 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
677 |
|
|
678 |
return mcbpc_inter_table[index].code; |
return mcbpc_inter_table[index].code; |
679 |
|
|
680 |
} |
} |
681 |
|
|
682 |
int get_cbpy(Bitstream * bs, int intra) |
int |
683 |
|
get_cbpy(Bitstream * bs, |
684 |
|
int intra) |
685 |
{ |
{ |
686 |
|
|
687 |
int cbpy; |
int cbpy; |
688 |
uint32_t index = BitstreamShowBits(bs, 6); |
uint32_t index = BitstreamShowBits(bs, 6); |
689 |
|
|
694 |
cbpy = 15 - cbpy; |
cbpy = 15 - cbpy; |
695 |
|
|
696 |
return cbpy; |
return cbpy; |
697 |
|
|
698 |
} |
} |
699 |
|
|
700 |
int get_mv_data(Bitstream * bs) |
int |
701 |
|
get_mv_data(Bitstream * bs) |
702 |
{ |
{ |
703 |
|
|
704 |
uint32_t index; |
uint32_t index; |
705 |
|
|
706 |
if(BitstreamGetBit(bs)) |
if(BitstreamGetBit(bs)) |
708 |
|
|
709 |
index = BitstreamShowBits(bs, 12); |
index = BitstreamShowBits(bs, 12); |
710 |
|
|
711 |
if(index >= 512) |
if (index >= 512) { |
|
{ |
|
712 |
index = (index >> 8) - 2; |
index = (index >> 8) - 2; |
713 |
BitstreamSkip(bs, TMNMVtab0[index].len); |
BitstreamSkip(bs, TMNMVtab0[index].len); |
714 |
return TMNMVtab0[index].code; |
return TMNMVtab0[index].code; |
715 |
} |
} |
716 |
|
|
717 |
if(index >= 128) |
if (index >= 128) { |
|
{ |
|
718 |
index = (index >> 2) - 32; |
index = (index >> 2) - 32; |
719 |
BitstreamSkip(bs, TMNMVtab1[index].len); |
BitstreamSkip(bs, TMNMVtab1[index].len); |
720 |
return TMNMVtab1[index].code; |
return TMNMVtab1[index].code; |
724 |
|
|
725 |
BitstreamSkip(bs, TMNMVtab2[index].len); |
BitstreamSkip(bs, TMNMVtab2[index].len); |
726 |
return TMNMVtab2[index].code; |
return TMNMVtab2[index].code; |
727 |
|
|
728 |
} |
} |
729 |
|
|
730 |
int get_mv(Bitstream * bs, int fcode) |
int |
731 |
|
get_mv(Bitstream * bs, |
732 |
|
int fcode) |
733 |
{ |
{ |
734 |
|
|
735 |
int data; |
int data; |
736 |
int res; |
int res; |
737 |
int mv; |
int mv; |
746 |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
747 |
|
|
748 |
return data < 0 ? -mv : mv; |
return data < 0 ? -mv : mv; |
749 |
|
|
750 |
} |
} |
751 |
|
|
752 |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
int |
753 |
|
get_dc_dif(Bitstream * bs, |
754 |
|
uint32_t dc_size) |
755 |
{ |
{ |
756 |
|
|
757 |
int code = BitstreamGetBits(bs, dc_size); |
int code = BitstreamGetBits(bs, dc_size); |
758 |
int msb = code >> (dc_size - 1); |
int msb = code >> (dc_size - 1); |
759 |
|
|
761 |
return (-1 * (code^((1 << dc_size) - 1))); |
return (-1 * (code^((1 << dc_size) - 1))); |
762 |
|
|
763 |
return code; |
return code; |
764 |
|
|
765 |
} |
} |
766 |
|
|
767 |
int get_dc_size_lum(Bitstream * bs) |
int |
768 |
|
get_dc_size_lum(Bitstream * bs) |
769 |
{ |
{ |
770 |
|
|
771 |
int code, i; |
int code, i; |
772 |
|
|
773 |
code = BitstreamShowBits(bs, 11); |
code = BitstreamShowBits(bs, 11); |
774 |
|
|
775 |
for(i = 11; i > 3; i--) { |
for(i = 11; i > 3; i--) { |
782 |
|
|
783 |
BitstreamSkip(bs, dc_lum_tab[code].len); |
BitstreamSkip(bs, dc_lum_tab[code].len); |
784 |
return dc_lum_tab[code].code; |
return dc_lum_tab[code].code; |
785 |
|
|
786 |
} |
} |
787 |
|
|
788 |
|
|
789 |
int get_dc_size_chrom(Bitstream * bs) |
int |
790 |
|
get_dc_size_chrom(Bitstream * bs) |
791 |
{ |
{ |
792 |
|
|
793 |
uint32_t code, i; |
uint32_t code, i; |
794 |
|
|
795 |
code = BitstreamShowBits(bs, 12); |
code = BitstreamShowBits(bs, 12); |
796 |
|
|
797 |
for(i = 12; i > 2; i--) { |
for(i = 12; i > 2; i--) { |
803 |
} |
} |
804 |
|
|
805 |
return 3 - BitstreamGetBits(bs, 2); |
return 3 - BitstreamGetBits(bs, 2); |
806 |
|
|
807 |
} |
} |
808 |
|
|
809 |
int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header) |
/***************************************************************************** |
810 |
|
* Local inlined function to "decode" written vlc codes |
811 |
|
****************************************************************************/ |
812 |
|
|
813 |
|
static __inline int |
814 |
|
get_coeff(Bitstream * bs, |
815 |
|
int *run, |
816 |
|
int *last, |
817 |
|
int intra, |
818 |
|
int short_video_header) |
819 |
{ |
{ |
820 |
|
|
821 |
uint32_t mode; |
uint32_t mode; |
|
const VLC *tab; |
|
822 |
int32_t level; |
int32_t level; |
823 |
|
REVERSE_EVENT *reverse_event; |
824 |
|
|
825 |
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 */ |
826 |
intra = 0; |
intra = 0; |
827 |
|
|
828 |
tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
if (BitstreamShowBits(bs, 7) != ESCAPE) { |
829 |
|
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
830 |
|
|
831 |
if(tab->code == -1) |
if ((level = reverse_event->event.level) == 0) |
832 |
goto error; |
goto error; |
833 |
|
|
834 |
BitstreamSkip(bs, tab->len); |
*last = reverse_event->event.last; |
835 |
|
*run = reverse_event->event.run; |
836 |
|
|
837 |
if(tab->code != ESCAPE) { |
BitstreamSkip(bs, reverse_event->len); |
838 |
if(!intra) |
|
839 |
{ |
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; |
|
840 |
} |
} |
841 |
|
|
842 |
if(short_video_header) |
BitstreamSkip(bs, 7); |
843 |
{ |
|
844 |
// escape mode 4 - H.263 type, only used if short_video_header = 1 |
if (short_video_header) { |
845 |
|
/* escape mode 4 - H.263 type, only used if short_video_header = 1 */ |
846 |
*last = BitstreamGetBit(bs); |
*last = BitstreamGetBit(bs); |
847 |
*run = BitstreamGetBits(bs, 6); |
*run = BitstreamGetBits(bs, 6); |
848 |
level = BitstreamGetBits(bs, 8); |
level = BitstreamGetBits(bs, 8); |
849 |
|
|
850 |
if (level == 0 || level == 128) |
if (level == 0 || level == 128) |
851 |
DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level); |
DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level); |
852 |
|
|
853 |
return (level >= 128 ? -(256 - level) : level); |
return (level << 24) >> 24; |
854 |
} |
} |
855 |
|
|
856 |
mode = BitstreamShowBits(bs, 2); |
mode = BitstreamShowBits(bs, 2); |
858 |
if(mode < 3) { |
if(mode < 3) { |
859 |
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
860 |
|
|
861 |
tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
862 |
if (tab->code == -1) |
|
863 |
|
if ((level = reverse_event->event.level) == 0) |
864 |
goto error; |
goto error; |
865 |
|
|
866 |
BitstreamSkip(bs, tab->len); |
*last = reverse_event->event.last; |
867 |
|
*run = reverse_event->event.run; |
868 |
|
|
869 |
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; |
|
|
} |
|
870 |
|
|
871 |
if(mode < 2) // first escape mode, level is offset |
if (mode < 2) /* first escape mode, level is offset */ |
872 |
level += max_level[*last + (!intra<<1)][*run]; // need to add back the max level |
level += max_level[intra][*last][*run]; |
873 |
else if(mode == 2) // second escape mode, run is offset |
else /* second escape mode, run is offset */ |
874 |
*run += max_run[*last + (!intra<<1)][level] + 1; |
*run += max_run[intra][*last][level] + 1; |
875 |
|
|
876 |
return BitstreamGetBit(bs) ? -level : level; |
return BitstreamGetBits(bs, 1) ? -level : level; |
877 |
} |
} |
878 |
|
|
879 |
// third escape mode - fixed length codes |
/* third escape mode - fixed length codes */ |
880 |
BitstreamSkip(bs, 2); |
BitstreamSkip(bs, 2); |
881 |
*last = BitstreamGetBits(bs, 1); |
*last = BitstreamGetBits(bs, 1); |
882 |
*run = BitstreamGetBits(bs, 6); |
*run = BitstreamGetBits(bs, 6); |
883 |
BitstreamSkip(bs, 1); // marker |
BitstreamSkip(bs, 1); /* marker */ |
884 |
level = BitstreamGetBits(bs, 12); |
level = BitstreamGetBits(bs, 12); |
885 |
BitstreamSkip(bs, 1); // marker |
BitstreamSkip(bs, 1); /* marker */ |
886 |
|
|
887 |
return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level; |
return (level << 20) >> 20; |
888 |
|
|
889 |
error: |
error: |
890 |
*run = VLC_ERROR; |
*run = VLC_ERROR; |
891 |
return 0; |
return 0; |
892 |
} |
} |
893 |
|
|
894 |
|
/***************************************************************************** |
895 |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
* MB reading functions |
896 |
|
****************************************************************************/ |
897 |
|
|
898 |
|
void |
899 |
|
get_intra_block(Bitstream * bs, |
900 |
|
int16_t * block, |
901 |
|
int direction, |
902 |
|
int coeff) |
903 |
{ |
{ |
904 |
|
|
905 |
const uint16_t * scan = scan_tables[ direction ]; |
const uint16_t * scan = scan_tables[ direction ]; |
906 |
int level; |
int level; |
907 |
int run; |
int run; |
908 |
int last; |
int last; |
909 |
|
|
910 |
do |
do { |
|
{ |
|
911 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
912 |
if (run == -1) |
if (run == -1) { |
913 |
{ |
DPRINTF(DPRINTF_DEBUG, "fatal: invalid run"); |
|
DEBUG("fatal: invalid run"); |
|
914 |
break; |
break; |
915 |
} |
} |
916 |
coeff += run; |
coeff += run; |
917 |
block[ scan[coeff] ] = level; |
block[ scan[coeff] ] = level; |
918 |
if (level < -127 || level > 127) |
|
919 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
920 |
DEBUG1("warning: intra_overflow", level); |
/*DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); */ |
921 |
|
|
922 |
|
if (level < -2047 || level > 2047) { |
923 |
|
DPRINTF(DPRINTF_DEBUG, "warning: intra_overflow: %d", level); |
924 |
} |
} |
925 |
coeff++; |
coeff++; |
926 |
} while (!last); |
} while (!last); |
927 |
|
|
928 |
} |
} |
929 |
|
|
930 |
void get_inter_block(Bitstream * bs, int16_t * block) |
void |
931 |
|
get_inter_block(Bitstream * bs, |
932 |
|
int16_t * block) |
933 |
{ |
{ |
934 |
|
|
935 |
const uint16_t * scan = scan_tables[0]; |
const uint16_t * scan = scan_tables[0]; |
936 |
int p; |
int p; |
937 |
int level; |
int level; |
939 |
int last; |
int last; |
940 |
|
|
941 |
p = 0; |
p = 0; |
942 |
do |
do { |
|
{ |
|
943 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
944 |
if (run == -1) |
if (run == -1) { |
945 |
{ |
DPRINTF(DPRINTF_ERROR, "fatal: invalid run"); |
|
DEBUG("fatal: invalid run"); |
|
946 |
break; |
break; |
947 |
} |
} |
948 |
p += run; |
p += run; |
949 |
|
|
950 |
block[ scan[p] ] = level; |
block[ scan[p] ] = level; |
951 |
if (level < -127 || level > 127) |
|
952 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
953 |
DEBUG1("warning: inter_overflow", level); |
|
954 |
|
if (level < -2047 || level > 2047) { |
955 |
|
DPRINTF(DPRINTF_DEBUG, "warning: inter_overflow: %d", level); |
956 |
} |
} |
957 |
p++; |
p++; |
958 |
} while (!last); |
} while (!last); |
959 |
|
|
960 |
} |
} |