49 |
******************************************************************************/ |
******************************************************************************/ |
50 |
|
|
51 |
|
|
52 |
|
#include <stdio.h> |
53 |
#include <stdlib.h> |
#include <stdlib.h> |
54 |
#include "../portab.h" |
#include "../portab.h" |
55 |
|
#include "../global.h" |
56 |
#include "bitstream.h" |
#include "bitstream.h" |
57 |
#include "zigzag.h" |
#include "zigzag.h" |
58 |
#include "vlc_codes.h" |
#include "vlc_codes.h" |
60 |
|
|
61 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
62 |
|
|
63 |
#define ABS(X) (((X)>0)?(X):-(X)) |
/* #define BIGLUT */ |
|
#define CLIP(X,A) (X > A) ? (A) : (X) |
|
|
|
|
|
VLC intra_table[524032]; |
|
|
VLC inter_table[524032]; |
|
64 |
|
|
65 |
VLC DCT3Dintra[4096]; |
#ifdef BIGLUT |
66 |
VLC DCT3Dinter[4096]; |
#define LEVELOFFSET 2048 |
67 |
|
#else |
68 |
|
#define LEVELOFFSET 32 |
69 |
|
#endif |
70 |
|
|
71 |
|
static REVERSE_EVENT DCT3D[2][4096]; |
72 |
|
|
73 |
|
#ifdef BIGLUT |
74 |
|
static VLC coeff_VLC[2][2][4096][64]; |
75 |
|
VLC *intra_table; |
76 |
|
static VLC *inter_table; |
77 |
|
#else |
78 |
|
static VLC coeff_VLC[2][2][64][64]; |
79 |
|
#endif |
80 |
|
|
81 |
/* not really MB related, but VLCs are only available here */ |
/* not really MB related, but VLCs are only available here */ |
82 |
void bs_put_spritetrajectory(Bitstream * bs, const int val) |
void bs_put_spritetrajectory(Bitstream * bs, const int val) |
93 |
if (len) BitstreamPutBits(bs, code, len); |
if (len) BitstreamPutBits(bs, code, len); |
94 |
} |
} |
95 |
|
|
96 |
|
int bs_get_spritetrajectory(Bitstream * bs) |
97 |
|
{ |
98 |
|
int i; |
99 |
|
for (i = 0; i < 12; i++) |
100 |
|
{ |
101 |
|
if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code) |
102 |
|
{ |
103 |
|
BitstreamSkip(bs, sprite_trajectory_len[i].len); |
104 |
|
return i; |
105 |
|
} |
106 |
|
} |
107 |
|
return -1; |
108 |
|
} |
109 |
|
|
110 |
void |
void |
111 |
init_vlc_tables(void) |
init_vlc_tables(void) |
112 |
{ |
{ |
113 |
|
ptr_t i, j, k, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; |
114 |
|
int32_t l; |
115 |
|
|
116 |
int32_t k, l, i, intra, last; |
#ifdef BIGLUT |
117 |
VLC *vlc[2]; |
intra_table = coeff_VLC[1]; |
118 |
VLC **coeff_ptr; |
inter_table = coeff_VLC[0]; |
119 |
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]; |
|
120 |
|
|
|
if ((abs(level) <= max_level_ptr[run]) && |
|
|
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
|
121 |
|
|
122 |
vlc[intra]->code = 0x06; |
for (intra = 0; intra < 2; intra++) |
123 |
vlc[intra]->len = 8; |
for (i = 0; i < 4096; i++) |
124 |
goto loop_end; |
DCT3D[intra][i].event.level = 0; |
|
} |
|
125 |
|
|
126 |
if (level > 0) // still here? |
for (intra = 0; intra < 2; intra++) |
127 |
level += max_level_ptr[run]; // restore level |
for (last = 0; last < 2; last++) |
128 |
else |
{ |
129 |
level -= max_level_ptr[run]; |
for (run = 0; run < 63 + last; run++) |
130 |
|
for (level = 0; level < 32 << intra; level++) |
131 |
run -= max_run_ptr[abs(level)] + 1; // and change run |
{ |
132 |
|
#ifdef BIGLUT |
133 |
|
offset = LEVELOFFSET; |
134 |
|
#else |
135 |
|
offset = !intra * LEVELOFFSET; |
136 |
|
#endif |
137 |
|
coeff_VLC[intra][last][level + offset][run].len = 128; |
138 |
|
} |
139 |
|
} |
140 |
|
|
141 |
if ((abs(level) <= max_level_ptr[run]) && |
for (intra = 0; intra < 2; intra++) |
142 |
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
for (i = 0; i < 102; i++) |
143 |
|
{ |
144 |
|
#ifdef BIGLUT |
145 |
|
offset = LEVELOFFSET; |
146 |
|
#else |
147 |
|
offset = !intra * LEVELOFFSET; |
148 |
|
#endif |
149 |
|
for (j = 0; j < 1 << (12 - coeff_tab[intra][i].vlc.len); j++) |
150 |
|
{ |
151 |
|
DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len = coeff_tab[intra][i].vlc.len; |
152 |
|
DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event; |
153 |
|
} |
154 |
|
|
155 |
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 |
156 |
vlc[intra]->len = 9; |
= coeff_tab[intra][i].vlc.code << 1; |
157 |
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 |
158 |
|
= coeff_tab[intra][i].vlc.len + 1; |
159 |
|
#ifndef BIGLUT |
160 |
|
if (!intra) |
161 |
|
#endif |
162 |
|
{ |
163 |
|
coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code |
164 |
|
= (coeff_tab[intra][i].vlc.code << 1) | 1; |
165 |
|
coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len |
166 |
|
= coeff_tab[intra][i].vlc.len + 1; |
167 |
} |
} |
|
run += max_run_ptr[abs(level)] + 1; |
|
168 |
} |
} |
169 |
|
|
170 |
vlc[intra]->code = |
for (intra = 0; intra < 2; intra++) |
171 |
(uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) | |
for (last = 0; last < 2; last++) |
172 |
((k & 0xfff) << 1) | 1; |
for (run = 0; run < 63 + last; run++) |
173 |
|
{ |
174 |
vlc[intra]->len = 30; |
for (level = 1; level < 32 << intra; level++) |
175 |
vlc[intra]++; |
{ |
176 |
|
if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level]) |
177 |
continue; |
continue; |
178 |
|
|
179 |
loop_end: |
#ifdef BIGLUT |
180 |
if (level != 0) { |
offset = LEVELOFFSET; |
181 |
vlc[intra]->code = |
#else |
182 |
(vlc[intra]-> |
offset = !intra * LEVELOFFSET; |
183 |
code << (coeff_ptr[run][abs(level) - 1].len + |
#endif |
184 |
1)) | (coeff_ptr[run][abs(level) - |
level_esc = level - max_level[intra][last][run]; |
185 |
1].code << 1); |
run_esc = run - 1 - max_run[intra][last][level]; |
186 |
vlc[intra]->len = |
/*use this test to use shorter esc2 codes when possible |
187 |
(coeff_ptr[run][abs(level) - 1].len + 1) + |
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc] |
188 |
vlc[intra]->len; |
&& !(coeff_VLC[intra][last][level_esc + offset][run].len + 7 + 1 |
189 |
|
> coeff_VLC[intra][last][level + offset][run_esc].code + 7 + 2))*/ |
190 |
if (level < 0) |
|
191 |
vlc[intra]->code += 1; |
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]) |
192 |
} |
{ |
193 |
|
escape = ESCAPE1; |
194 |
vlc[intra]++; |
escape_len = 7 + 1; |
195 |
|
run_esc = run; |
196 |
} |
} |
197 |
|
else |
198 |
|
{ |
199 |
|
if (level <= max_level[intra][last][run_esc] && run_esc <= max_run[intra][last][level]) |
200 |
|
{ |
201 |
|
escape = ESCAPE2; |
202 |
|
escape_len = 7 + 2; |
203 |
|
level_esc = level; |
204 |
} |
} |
205 |
|
else |
206 |
|
{ |
207 |
|
#ifndef BIGLUT |
208 |
|
if (!intra) |
209 |
|
#endif |
210 |
|
{ |
211 |
|
coeff_VLC[intra][last][level + offset][run].code |
212 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
213 |
|
coeff_VLC[intra][last][level + offset][run].len = 30; |
214 |
|
coeff_VLC[intra][last][offset - level][run].code |
215 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1; |
216 |
|
coeff_VLC[intra][last][offset - level][run].len = 30; |
217 |
|
} |
218 |
|
continue; |
219 |
} |
} |
|
|
|
|
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]; |
|
220 |
} |
} |
221 |
|
|
222 |
vlc1++; |
coeff_VLC[intra][last][level + offset][run].code |
223 |
vlc2++; |
= (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len) |
224 |
|
| coeff_VLC[intra][last][level_esc + offset][run_esc].code; |
225 |
|
coeff_VLC[intra][last][level + offset][run].len |
226 |
|
= coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len; |
227 |
|
#ifndef BIGLUT |
228 |
|
if (!intra) |
229 |
|
#endif |
230 |
|
{ |
231 |
|
coeff_VLC[intra][last][offset - level][run].code |
232 |
|
= (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len) |
233 |
|
| coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1; |
234 |
|
coeff_VLC[intra][last][offset - level][run].len |
235 |
|
= coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len; |
236 |
|
} |
237 |
} |
} |
|
DCT3D[0] = DCT3Dinter; |
|
|
DCT3D[1] = DCT3Dintra; |
|
238 |
|
|
239 |
|
#ifdef BIGLUT |
240 |
|
for (level = 32 << intra; level < 2048; level++) |
241 |
|
{ |
242 |
|
coeff_VLC[intra][last][level + offset][run].code |
243 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
244 |
|
coeff_VLC[intra][last][level + offset][run].len = 30; |
245 |
|
|
246 |
|
coeff_VLC[intra][last][offset - level][run].code |
247 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1; |
248 |
|
coeff_VLC[intra][last][offset - level][run].len = 30; |
249 |
|
} |
250 |
|
#else |
251 |
|
if (!intra) |
252 |
|
{ |
253 |
|
coeff_VLC[intra][last][0][run].code |
254 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1; |
255 |
|
coeff_VLC[intra][last][0][run].len = 30; |
256 |
|
} |
257 |
|
#endif |
258 |
|
} |
259 |
/* init sprite_trajectory tables */ |
/* init sprite_trajectory tables */ |
260 |
/* even if GMC is not specified (it might be used later...) */ |
/* even if GMC is not specified (it might be used later...) */ |
261 |
|
|
265 |
{ |
{ |
266 |
int limit = (1<<k); |
int limit = (1<<k); |
267 |
|
|
268 |
for (i=-(2*limit-1); i<= -limit; i++) |
for (l=-(2*limit-1); l <= -limit; l++) |
269 |
{ |
{ |
270 |
sprite_trajectory_code[i+16384].code = (2*limit-1)+i; |
sprite_trajectory_code[l+16384].code = (2*limit-1)+l; |
271 |
sprite_trajectory_code[i+16384].len = k+1; |
sprite_trajectory_code[l+16384].len = k+1; |
272 |
} |
} |
273 |
|
|
274 |
for (i=limit; i<= 2*limit-1; i++) |
for (l=limit; l<= 2*limit-1; l++) |
275 |
{ |
{ |
276 |
sprite_trajectory_code[i+16384].code = i; |
sprite_trajectory_code[l+16384].code = l; |
277 |
sprite_trajectory_code[i+16384].len = k+1; |
sprite_trajectory_code[l+16384].len = k+1; |
278 |
} |
} |
279 |
} |
} |
280 |
} |
} |
334 |
|
|
335 |
} |
} |
336 |
|
|
337 |
|
#ifdef BIGLUT |
338 |
|
|
339 |
static __inline void |
static __inline void |
340 |
CodeCoeff(Bitstream * bs, |
CodeCoeff(Bitstream * bs, |
355 |
j++; |
j++; |
356 |
|
|
357 |
do { |
do { |
358 |
vlc = table + 64 * 2047 + (v << 6) + j - last; |
vlc = table + 64 * 2048 + (v << 6) + j - last; |
359 |
last = ++j; |
last = ++j; |
360 |
|
|
361 |
// count zeroes |
/* count zeroes */ |
362 |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
363 |
j++; |
j++; |
364 |
|
|
365 |
// write code |
/* write code */ |
366 |
if (j != 64) { |
if (j != 64) { |
367 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
368 |
} else { |
} else { |
369 |
vlc += 64 * 4095; |
vlc += 64 * 4096; |
370 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
371 |
break; |
break; |
372 |
} |
} |
375 |
} |
} |
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
/* returns the number of bits required to encode qcoeff */ |
380 |
|
int |
381 |
|
CodeCoeff_CalcBits(const int16_t qcoeff[64], |
382 |
|
VLC * table, |
383 |
|
const uint16_t * zigzag, |
384 |
|
uint16_t intra) |
385 |
|
{ |
386 |
|
int bits = 0; |
387 |
|
uint32_t j, last; |
388 |
|
short v; |
389 |
|
VLC *vlc; |
390 |
|
|
391 |
|
j = intra; |
392 |
|
last = intra; |
393 |
|
|
394 |
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
395 |
|
j++; |
396 |
|
|
397 |
|
if (j >= 64) return 0; /* empty block */ |
398 |
|
|
399 |
|
do { |
400 |
|
vlc = table + 64 * 2048 + (v << 6) + j - last; |
401 |
|
last = ++j; |
402 |
|
|
403 |
|
/* count zeroes */ |
404 |
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
405 |
|
j++; |
406 |
|
|
407 |
|
/* write code */ |
408 |
|
if (j != 64) { |
409 |
|
bits += vlc->len; |
410 |
|
} else { |
411 |
|
vlc += 64 * 4096; |
412 |
|
bits += vlc->len; |
413 |
|
break; |
414 |
|
} |
415 |
|
} while (1); |
416 |
|
|
417 |
|
return bits; |
418 |
|
} |
419 |
|
|
420 |
|
|
421 |
|
#else |
422 |
|
|
423 |
|
static __inline void |
424 |
|
CodeCoeffInter(Bitstream * bs, |
425 |
|
const int16_t qcoeff[64], |
426 |
|
const uint16_t * zigzag) |
427 |
|
{ |
428 |
|
uint32_t i, run, prev_run, code, len; |
429 |
|
int32_t level, prev_level, level_shifted; |
430 |
|
|
431 |
|
i = 0; |
432 |
|
run = 0; |
433 |
|
|
434 |
|
while (!(level = qcoeff[zigzag[i++]])) |
435 |
|
run++; |
436 |
|
|
437 |
|
prev_level = level; |
438 |
|
prev_run = run; |
439 |
|
run = 0; |
440 |
|
|
441 |
|
while (i < 64) |
442 |
|
{ |
443 |
|
if ((level = qcoeff[zigzag[i++]]) != 0) |
444 |
|
{ |
445 |
|
level_shifted = prev_level + 32; |
446 |
|
if (!(level_shifted & -64)) |
447 |
|
{ |
448 |
|
code = coeff_VLC[0][0][level_shifted][prev_run].code; |
449 |
|
len = coeff_VLC[0][0][level_shifted][prev_run].len; |
450 |
|
} |
451 |
|
else |
452 |
|
{ |
453 |
|
code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
454 |
|
len = 30; |
455 |
|
} |
456 |
|
BitstreamPutBits(bs, code, len); |
457 |
|
prev_level = level; |
458 |
|
prev_run = run; |
459 |
|
run = 0; |
460 |
|
} |
461 |
|
else |
462 |
|
run++; |
463 |
|
} |
464 |
|
|
465 |
|
level_shifted = prev_level + 32; |
466 |
|
if (!(level_shifted & -64)) |
467 |
|
{ |
468 |
|
code = coeff_VLC[0][1][level_shifted][prev_run].code; |
469 |
|
len = coeff_VLC[0][1][level_shifted][prev_run].len; |
470 |
|
} |
471 |
|
else |
472 |
|
{ |
473 |
|
code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
474 |
|
len = 30; |
475 |
|
} |
476 |
|
BitstreamPutBits(bs, code, len); |
477 |
|
} |
478 |
|
|
479 |
|
static __inline void |
480 |
|
CodeCoeffIntra(Bitstream * bs, |
481 |
|
const int16_t qcoeff[64], |
482 |
|
const uint16_t * zigzag) |
483 |
|
{ |
484 |
|
uint32_t i, abs_level, run, prev_run, code, len; |
485 |
|
int32_t level, prev_level; |
486 |
|
|
487 |
|
i = 1; |
488 |
|
run = 0; |
489 |
|
|
490 |
|
while (i<64 && !(level = qcoeff[zigzag[i++]])) |
491 |
|
run++; |
492 |
|
|
493 |
|
prev_level = level; |
494 |
|
prev_run = run; |
495 |
|
run = 0; |
496 |
|
|
497 |
|
while (i < 64) |
498 |
|
{ |
499 |
|
if ((level = qcoeff[zigzag[i++]]) != 0) |
500 |
|
{ |
501 |
|
abs_level = ABS(prev_level); |
502 |
|
abs_level = abs_level < 64 ? abs_level : 0; |
503 |
|
code = coeff_VLC[1][0][abs_level][prev_run].code; |
504 |
|
len = coeff_VLC[1][0][abs_level][prev_run].len; |
505 |
|
if (len != 128) |
506 |
|
code |= (prev_level < 0); |
507 |
|
else |
508 |
|
{ |
509 |
|
code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
510 |
|
len = 30; |
511 |
|
} |
512 |
|
BitstreamPutBits(bs, code, len); |
513 |
|
prev_level = level; |
514 |
|
prev_run = run; |
515 |
|
run = 0; |
516 |
|
} |
517 |
|
else |
518 |
|
run++; |
519 |
|
} |
520 |
|
|
521 |
|
abs_level = ABS(prev_level); |
522 |
|
abs_level = abs_level < 64 ? abs_level : 0; |
523 |
|
code = coeff_VLC[1][1][abs_level][prev_run].code; |
524 |
|
len = coeff_VLC[1][1][abs_level][prev_run].len; |
525 |
|
if (len != 128) |
526 |
|
code |= (prev_level < 0); |
527 |
|
else |
528 |
|
{ |
529 |
|
code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
530 |
|
len = 30; |
531 |
|
} |
532 |
|
BitstreamPutBits(bs, code, len); |
533 |
|
} |
534 |
|
|
535 |
|
|
536 |
|
|
537 |
|
/* returns the number of bits required to encode qcoeff */ |
538 |
|
|
539 |
|
int |
540 |
|
CodeCoeffIntra_CalcBits(const int16_t qcoeff[64], const uint16_t * zigzag) |
541 |
|
{ |
542 |
|
int bits = 0; |
543 |
|
uint32_t i, abs_level, run, prev_run, len; |
544 |
|
int32_t level, prev_level; |
545 |
|
|
546 |
|
i = 1; |
547 |
|
run = 0; |
548 |
|
|
549 |
|
while (i<64 && !(level = qcoeff[zigzag[i++]])) |
550 |
|
run++; |
551 |
|
|
552 |
|
if (i >= 64) return 0; /* empty block */ |
553 |
|
|
554 |
|
prev_level = level; |
555 |
|
prev_run = run; |
556 |
|
run = 0; |
557 |
|
|
558 |
|
while (i < 64) |
559 |
|
{ |
560 |
|
if ((level = qcoeff[zigzag[i++]]) != 0) |
561 |
|
{ |
562 |
|
abs_level = ABS(prev_level); |
563 |
|
abs_level = abs_level < 64 ? abs_level : 0; |
564 |
|
len = coeff_VLC[1][0][abs_level][prev_run].len; |
565 |
|
bits += len!=128 ? len : 30; |
566 |
|
|
567 |
|
prev_level = level; |
568 |
|
prev_run = run; |
569 |
|
run = 0; |
570 |
|
} |
571 |
|
else |
572 |
|
run++; |
573 |
|
} |
574 |
|
|
575 |
|
abs_level = ABS(prev_level); |
576 |
|
abs_level = abs_level < 64 ? abs_level : 0; |
577 |
|
len = coeff_VLC[1][1][abs_level][prev_run].len; |
578 |
|
bits += len!=128 ? len : 30; |
579 |
|
|
580 |
|
return bits; |
581 |
|
} |
582 |
|
|
583 |
|
|
584 |
|
#endif |
585 |
|
|
586 |
static __inline void |
static __inline void |
587 |
CodeBlockIntra(const FRAMEINFO * const frame, |
CodeBlockIntra(const FRAMEINFO * const frame, |
588 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
639 |
|
|
640 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
641 |
|
|
642 |
|
#ifdef BIGLUT |
643 |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1); |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1); |
644 |
|
#else |
645 |
|
CodeCoeffIntra(bs, &qcoeff[i * 64], scan_table); |
646 |
|
#endif |
647 |
|
|
648 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
649 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
663 |
|
|
664 |
int32_t i; |
int32_t i; |
665 |
uint32_t bits, mcbpc, cbpy; |
uint32_t bits, mcbpc, cbpy; |
|
int mcsel=0; |
|
666 |
|
|
667 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
668 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
672 |
mcbpc_inter_tab[mcbpc].len); |
mcbpc_inter_tab[mcbpc].len); |
673 |
|
|
674 |
if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) ) |
if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) ) |
675 |
{ |
BitstreamPutBit(bs, pMB->mcsel); // mcsel: '0'=local motion, '1'=GMC |
|
if (frame->quarterpel) { |
|
|
if ( (pMB->qmvs[0].x == frame->GMC_MV.x) && (pMB->qmvs[0].y == frame->GMC_MV.y) ) |
|
|
mcsel=1; |
|
|
} else { |
|
|
if ( (pMB->mvs[0].x == frame->GMC_MV.x) && (pMB->mvs[0].y == frame->GMC_MV.y) ) |
|
|
mcsel=1; |
|
|
} |
|
|
BitstreamPutBit(bs, mcsel); // mcsel: '0'=local motion, '1'=GMC |
|
|
} |
|
676 |
|
|
677 |
// write cbpy |
// write cbpy |
678 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
685 |
if (frame->global_flags & XVID_INTERLACING) { |
if (frame->global_flags & XVID_INTERLACING) { |
686 |
if (pMB->cbp) { |
if (pMB->cbp) { |
687 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
688 |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct); |
689 |
} |
} |
690 |
|
|
691 |
// if inter block, write field ME flag |
// if inter block, write field ME flag |
692 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
693 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, pMB->field_pred); |
694 |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred); |
695 |
|
|
696 |
// write field prediction references |
// write field prediction references |
697 |
if (pMB->field_pred) { |
if (pMB->field_pred) { |
701 |
} |
} |
702 |
} |
} |
703 |
// code motion vector(s) if motion is local |
// code motion vector(s) if motion is local |
704 |
if (mcsel==0) |
if (!pMB->mcsel) |
705 |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
706 |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
707 |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
717 |
frame->global_flags & XVID_ALTERNATESCAN ? |
frame->global_flags & XVID_ALTERNATESCAN ? |
718 |
scan_tables[2] : scan_tables[0]; |
scan_tables[2] : scan_tables[0]; |
719 |
|
|
720 |
|
#ifdef BIGLUT |
721 |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0); |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0); |
722 |
|
#else |
723 |
|
CodeCoeffInter(bs, &qcoeff[i * 64], scan_table); |
724 |
|
#endif |
725 |
} |
} |
726 |
|
|
727 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
875 |
|
|
876 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
877 |
if (mb->cbp & (1 << (5 - i))) { |
if (mb->cbp & (1 << (5 - i))) { |
878 |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0); |
#ifdef BIGLUT |
879 |
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); |
880 |
|
#else |
881 |
|
CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]); |
882 |
|
#endif |
883 |
} |
} |
884 |
} |
} |
885 |
} |
} |
936 |
|
|
937 |
uint32_t index; |
uint32_t index; |
938 |
|
|
939 |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
index = MIN(BitstreamShowBits(bs, 9), 256); |
940 |
|
|
941 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
942 |
|
|
1071 |
|
|
1072 |
} |
} |
1073 |
|
|
1074 |
|
static __inline int |
1075 |
|
get_coeff(Bitstream * bs, |
1076 |
|
int *run, |
1077 |
|
int *last, |
1078 |
|
int intra, |
1079 |
|
int short_video_header) |
1080 |
|
{ |
1081 |
|
|
1082 |
|
uint32_t mode; |
1083 |
|
int32_t level; |
1084 |
|
REVERSE_EVENT *reverse_event; |
1085 |
|
|
1086 |
|
if (short_video_header) /* inter-VLCs will be used for both intra and inter blocks */ |
1087 |
|
intra = 0; |
1088 |
|
|
1089 |
|
if (BitstreamShowBits(bs, 7) != ESCAPE) { |
1090 |
|
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
1091 |
|
|
1092 |
|
if ((level = reverse_event->event.level) == 0) |
1093 |
|
goto error; |
1094 |
|
|
1095 |
|
*last = reverse_event->event.last; |
1096 |
|
*run = reverse_event->event.run; |
1097 |
|
|
1098 |
|
BitstreamSkip(bs, reverse_event->len); |
1099 |
|
|
1100 |
|
return BitstreamGetBits(bs, 1) ? -level : level; |
1101 |
|
} |
1102 |
|
|
1103 |
|
BitstreamSkip(bs, 7); |
1104 |
|
|
1105 |
|
if (short_video_header) { |
1106 |
|
/* escape mode 4 - H.263 type, only used if short_video_header = 1 */ |
1107 |
|
*last = BitstreamGetBit(bs); |
1108 |
|
*run = BitstreamGetBits(bs, 6); |
1109 |
|
level = BitstreamGetBits(bs, 8); |
1110 |
|
|
1111 |
|
if (level == 0 || level == 128) |
1112 |
|
DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level); |
1113 |
|
|
1114 |
|
return (level << 24) >> 24; |
1115 |
|
} |
1116 |
|
|
1117 |
|
mode = BitstreamShowBits(bs, 2); |
1118 |
|
|
1119 |
|
if (mode < 3) { |
1120 |
|
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
1121 |
|
|
1122 |
|
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
1123 |
|
|
1124 |
|
if ((level = reverse_event->event.level) == 0) |
1125 |
|
goto error; |
1126 |
|
|
1127 |
|
*last = reverse_event->event.last; |
1128 |
|
*run = reverse_event->event.run; |
1129 |
|
|
1130 |
|
BitstreamSkip(bs, reverse_event->len); |
1131 |
|
|
1132 |
|
if (mode < 2) /* first escape mode, level is offset */ |
1133 |
|
level += max_level[intra][*last][*run]; |
1134 |
|
else /* second escape mode, run is offset */ |
1135 |
|
*run += max_run[intra][*last][level] + 1; |
1136 |
|
|
1137 |
|
return BitstreamGetBits(bs, 1) ? -level : level; |
1138 |
|
} |
1139 |
|
|
1140 |
|
/* third escape mode - fixed length codes */ |
1141 |
|
BitstreamSkip(bs, 2); |
1142 |
|
*last = BitstreamGetBits(bs, 1); |
1143 |
|
*run = BitstreamGetBits(bs, 6); |
1144 |
|
BitstreamSkip(bs, 1); /* marker */ |
1145 |
|
level = BitstreamGetBits(bs, 12); |
1146 |
|
BitstreamSkip(bs, 1); /* marker */ |
1147 |
|
|
1148 |
|
return (level << 20) >> 20; |
1149 |
|
|
1150 |
|
error: |
1151 |
|
*run = VLC_ERROR; |
1152 |
|
return 0; |
1153 |
|
} |
1154 |
|
|
1155 |
void |
void |
1156 |
get_intra_block(Bitstream * bs, |
get_intra_block(Bitstream * bs, |
1157 |
int16_t * block, |
int16_t * block, |
1165 |
do { |
do { |
1166 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
1167 |
if (run == -1) { |
if (run == -1) { |
1168 |
DEBUG("fatal: invalid run"); |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
1169 |
break; |
break; |
1170 |
} |
} |
1171 |
coeff += run; |
coeff += run; |
1174 |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
1175 |
//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)); |
1176 |
|
|
1177 |
if (level < -127 || level > 127) { |
if (level < -2047 || level > 2047) { |
1178 |
DEBUG1("warning: intra_overflow", level); |
DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level); |
1179 |
} |
} |
1180 |
coeff++; |
coeff++; |
1181 |
} while (!last); |
} while (!last); |
1198 |
do { |
do { |
1199 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
1200 |
if (run == -1) { |
if (run == -1) { |
1201 |
DEBUG("fatal: invalid run"); |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
1202 |
break; |
break; |
1203 |
} |
} |
1204 |
p += run; |
p += run; |
1208 |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
1209 |
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
1210 |
|
|
1211 |
if (level < -127 || level > 127) { |
if (level < -2047 || level > 2047) { |
1212 |
DEBUG1("warning: inter_overflow", level); |
DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level); |
1213 |
} |
} |
1214 |
p++; |
p++; |
1215 |
} while (!last); |
} while (!last); |