1 |
|
#include <stdlib.h> |
2 |
#include "../portab.h" |
#include "../portab.h" |
3 |
#include "bitstream.h" |
#include "bitstream.h" |
4 |
#include "zigzag.h" |
#include "zigzag.h" |
5 |
#include "vlc_codes.h" |
#include "vlc_codes.h" |
6 |
|
#include "mbcoding.h" |
7 |
|
|
8 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
9 |
|
|
|
#include <stdlib.h> /* malloc, free */ |
|
|
|
|
|
#define ESCAPE 7167 |
|
10 |
#define ABS(X) (((X)>0)?(X):-(X)) |
#define ABS(X) (((X)>0)?(X):-(X)) |
11 |
#define CLIP(X,A) (X > A) ? (A) : (X) |
#define CLIP(X,A) (X > A) ? (A) : (X) |
12 |
|
|
13 |
static VLC *DCT3D[2]; |
VLC intra_table[65536]; |
14 |
|
VLC inter_table[65536]; |
15 |
|
|
16 |
|
VLC DCT3Dintra[4096]; |
17 |
|
VLC DCT3Dinter[4096]; |
18 |
|
|
19 |
VLC *intra_table, *inter_table; |
static int16_t clip_table[4096]; |
|
static short clip_table[4096]; |
|
20 |
|
|
21 |
void create_vlc_tables(void) |
void init_vlc_tables(void) |
22 |
{ |
{ |
23 |
|
|
24 |
int32_t k, l, i, intra, last; |
int32_t k, l, i, intra, last; |
25 |
VLC *vlc[2]; |
VLC *vlc[2]; |
26 |
VLC **coeff_ptr; |
VLC **coeff_ptr; |
27 |
VLC *vlc1, *vlc2; |
VLC *vlc1, *vlc2; |
28 |
|
|
|
VLC *DCT3Dintra; |
|
|
VLC *DCT3Dinter; |
|
|
|
|
|
DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096); |
|
|
DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096); |
|
|
|
|
29 |
vlc1 = DCT3Dintra; |
vlc1 = DCT3Dintra; |
30 |
vlc2 = DCT3Dinter; |
vlc2 = DCT3Dinter; |
31 |
|
|
32 |
vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC)); |
vlc[0] = intra_table; |
33 |
vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC)); |
vlc[1] = inter_table; |
34 |
|
|
35 |
// initialize the clipping table |
// initialize the clipping table |
36 |
for(i = -2048; i < 2048; i++) { |
for(i = -2048; i < 2048; i++) { |
41 |
clip_table[i + 2048] = 255; |
clip_table[i + 2048] = 255; |
42 |
} |
} |
43 |
|
|
44 |
// generate intra/inter vlc lookup table |
// generate encoding vlc lookup tables |
45 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
46 |
intra = i % 2; |
intra = i % 2; |
47 |
last = i >> 1; |
last = i / 2; |
48 |
|
|
49 |
coeff_ptr = coeff_vlc[last + (intra << 1)]; |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
50 |
|
|
51 |
for(k = -255; k < 256; k++) { // level |
for(k = -255; k < 256; k++) { // level |
52 |
char *max_level_ptr = max_level[last + (intra << 1)]; |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
53 |
char *max_run_ptr = max_run[last + (intra << 1)]; |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
54 |
|
|
55 |
for(l = 0; l < 64; l++) { // run |
for(l = 0; l < 64; l++) { // run |
56 |
int32_t level = k; |
int32_t level = k; |
57 |
uint32_t run = l; |
uint32_t run = l; |
58 |
|
|
59 |
if(abs(level) <= max_level_ptr[run] && run <= max_run_ptr[abs(level)]) { |
if((abs(level) <= max_level_ptr[run]) && |
60 |
|
(run <= max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
61 |
|
|
|
if(level > 0) { |
|
|
vlc[intra]->code = (coeff_ptr[run][level - 1].code) << 1; |
|
|
vlc[intra]->len = coeff_ptr[run][level - 1].len + 1; |
|
|
} |
|
|
else if(level < 0) { |
|
|
vlc[intra]->code = ((coeff_ptr[run][-level - 1].code) << 1) + 1; |
|
|
vlc[intra]->len = coeff_ptr[run][-level - 1].len + 1; |
|
|
} |
|
|
else { |
|
62 |
vlc[intra]->code = 0; |
vlc[intra]->code = 0; |
63 |
vlc[intra]->len = 0; |
vlc[intra]->len = 0; |
64 |
|
goto loop_end; |
65 |
} |
} |
66 |
} else { |
else { |
67 |
if(level > 0) |
if(level > 0) // correct level |
68 |
level -= max_level_ptr[run]; |
level -= max_level_ptr[run]; |
69 |
else |
else |
70 |
level += max_level_ptr[run]; |
level += max_level_ptr[run]; |
71 |
|
|
72 |
if(abs(level) <= max_level_ptr[run] && |
if((abs(level) <= max_level_ptr[run]) && |
73 |
run <= max_run_ptr[abs(level)]) { |
(run <= max_run_ptr[abs(level)])) { |
74 |
|
|
|
if(level > 0) { |
|
|
vlc[intra]->code = (0x06 << (coeff_ptr[run][level - 1].len + 1)) | |
|
|
(coeff_ptr[run][level - 1].code << 1); |
|
|
vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 8; |
|
|
} |
|
|
else if(level < 0) { |
|
|
vlc[intra]->code = (0x06 << (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) + 8; |
|
|
} |
|
|
else { |
|
75 |
vlc[intra]->code = 0x06; |
vlc[intra]->code = 0x06; |
76 |
vlc[intra]->len = 8; |
vlc[intra]->len = 8; |
77 |
|
goto loop_end; |
78 |
} |
} |
79 |
} else { |
|
80 |
if(level > 0) |
if(level > 0) // still here? |
81 |
level += max_level_ptr[run]; |
level += max_level_ptr[run]; // restore level |
82 |
else |
else |
83 |
level -= max_level_ptr[run]; |
level -= max_level_ptr[run]; |
84 |
|
|
85 |
run -= max_run_ptr[abs(level)] + 1; |
run -= max_run_ptr[abs(level)] + 1; // and change run |
86 |
|
|
87 |
if(abs(level) <= max_level_ptr[run] && |
if((abs(level) <= max_level_ptr[run]) && |
88 |
run <= max_run_ptr[abs(level)]) { |
(run <= max_run_ptr[abs(level)])) { |
89 |
|
|
|
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 { |
|
90 |
vlc[intra]->code = 0x0e; |
vlc[intra]->code = 0x0e; |
91 |
vlc[intra]->len = 9; |
vlc[intra]->len = 9; |
92 |
|
goto loop_end; |
93 |
} |
} |
|
} else { |
|
|
if(level != 0) |
|
94 |
run += max_run_ptr[abs(level)] + 1; |
run += max_run_ptr[abs(level)] + 1; |
95 |
else |
} |
|
run++; |
|
96 |
|
|
97 |
vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) | |
vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) | |
98 |
(l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1; |
(1 << 13) | ((k & 0xfff) << 1) | 1; |
99 |
|
|
100 |
vlc[intra]->len = 30; |
vlc[intra]->len = 30; |
101 |
|
vlc[intra]++; |
102 |
|
continue; |
103 |
|
|
104 |
|
loop_end: |
105 |
|
if(level != 0) { |
106 |
|
vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) | |
107 |
|
(coeff_ptr[run][abs(level) - 1].code << 1); |
108 |
|
vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len; |
109 |
|
|
110 |
|
if(level < 0) |
111 |
|
vlc[intra]->code += 1; |
112 |
} |
} |
113 |
} |
|
|
} |
|
114 |
vlc[intra]++; |
vlc[intra]++; |
115 |
} |
} |
116 |
} |
} |
117 |
} |
} |
|
intra_table += 64*255; // center vlc tables |
|
|
inter_table += 64*255; // center vlc tables |
|
118 |
|
|
119 |
for(i = 0; i < 4096; i++) { |
for(i = 0; i < 4096; i++) { |
120 |
if(i >= 512) { |
if(i >= 512) { |
142 |
|
|
143 |
} |
} |
144 |
|
|
145 |
void destroy_vlc_tables(void) { |
static __inline void CodeVector(Bitstream *bs, |
146 |
|
int16_t value, |
147 |
if(intra_table != NULL && inter_table != NULL) { |
int16_t f_code, |
148 |
intra_table -= 64*255; // uncenter vlc tables |
Statistics *pStat) |
|
inter_table -= 64*255; // uncenter vlc tables |
|
|
|
|
|
free(intra_table); |
|
|
free(inter_table); |
|
|
} |
|
|
|
|
|
if(DCT3D[0] != NULL && DCT3D[1] != NULL) { |
|
|
free(DCT3D[0]); |
|
|
free(DCT3D[1]); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
static __inline void CodeVector(Bitstream *bs, int16_t value, int16_t f_code, Statistics *pStat) |
|
149 |
{ |
{ |
150 |
|
|
151 |
const int scale_factor = 1 << (f_code - 1); |
const int scale_factor = 1 << (f_code - 1); |
152 |
const int cmp = scale_factor << 5; |
const int cmp = scale_factor << 5; |
153 |
|
|
160 |
pStat->iMvSum += value * value; |
pStat->iMvSum += value * value; |
161 |
pStat->iMvCount++; |
pStat->iMvCount++; |
162 |
|
|
163 |
if (value == 0) |
if (value == 0) { |
164 |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
165 |
else { |
} else { |
166 |
uint16_t length, code, mv_res, sign; |
uint16_t length, code, mv_res, sign; |
167 |
|
|
168 |
length = 16 << f_code; |
length = 16 << f_code; |
191 |
if(f_code) |
if(f_code) |
192 |
BitstreamPutBits(bs, mv_res, f_code); |
BitstreamPutBits(bs, mv_res, f_code); |
193 |
} |
} |
194 |
|
|
195 |
} |
} |
196 |
|
|
197 |
|
|
198 |
static __inline void CodeCoeff(Bitstream *bs, int16_t qcoeff[64], VLC *table, |
static __inline void CodeCoeff(Bitstream *bs, |
199 |
const uint16_t *zigzag, uint16_t intra) { |
int16_t qcoeff[64], |
200 |
|
VLC *table, |
201 |
|
const uint16_t *zigzag, |
202 |
|
uint16_t intra) |
203 |
|
{ |
204 |
|
|
205 |
uint32_t j, last; |
uint32_t j, last; |
206 |
short v; |
short v; |
207 |
VLC *vlc; |
VLC *vlc; |
213 |
|
|
214 |
do { |
do { |
215 |
// count zeroes |
// count zeroes |
216 |
vlc = table + (clip_table[2048+v] << 6) + j - last; |
vlc = table + 64*255 + (clip_table[2048+v] << 6) + j - last; |
217 |
last = j + 1; |
last = j + 1; |
218 |
while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0); |
while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0); |
219 |
|
|
226 |
break; |
break; |
227 |
} |
} |
228 |
} while(1); |
} while(1); |
229 |
|
|
230 |
} |
} |
231 |
|
|
232 |
|
|
233 |
static void CodeBlockIntra(const MBParam * pParam, const MACROBLOCK *pMB, |
static void CodeBlockIntra(const MBParam * pParam, |
234 |
int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) |
const MACROBLOCK *pMB, |
235 |
|
int16_t qcoeff[6*64], |
236 |
|
Bitstream * bs, |
237 |
|
Statistics * pStat) |
238 |
{ |
{ |
239 |
|
|
240 |
uint32_t i, mcbpc, cbpy, bits; |
uint32_t i, mcbpc, cbpy, bits; |
241 |
|
|
242 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
274 |
for(i = 0; i < 6; i++) |
for(i = 0; i < 6; i++) |
275 |
{ |
{ |
276 |
if(i < 4) |
if(i < 4) |
277 |
BitstreamPutBits(bs, dcy_tab[qcoeff[i][0] + 255].code, |
BitstreamPutBits(bs, |
278 |
dcy_tab[qcoeff[i][0] + 255].len); |
dcy_tab[qcoeff[i*64 + 0] + 255].code, |
279 |
|
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
280 |
else |
else |
281 |
BitstreamPutBits(bs, dcc_tab[qcoeff[i][0] + 255].code, |
BitstreamPutBits(bs, |
282 |
dcc_tab[qcoeff[i][0] + 255].len); |
dcc_tab[qcoeff[i*64 + 0] + 255].code, |
283 |
|
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
284 |
|
|
285 |
if(pMB->cbp & (1 << (5 - i))) |
if(pMB->cbp & (1 << (5 - i))) |
286 |
{ |
{ |
287 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
288 |
|
|
289 |
CodeCoeff(bs, qcoeff[i], intra_table, scan_tables[pMB->acpred_directions[i]], 1); |
CodeCoeff(bs, |
290 |
|
&qcoeff[i*64], |
291 |
|
intra_table, |
292 |
|
scan_tables[pMB->acpred_directions[i]], |
293 |
|
1); |
294 |
|
|
295 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
296 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
297 |
} |
} |
298 |
} |
} |
299 |
|
|
300 |
} |
} |
301 |
|
|
302 |
|
|
303 |
static void CodeBlockInter(const MBParam * pParam, const MACROBLOCK *pMB, |
static void CodeBlockInter(const MBParam * pParam, |
304 |
int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) |
const MACROBLOCK *pMB, |
305 |
|
int16_t qcoeff[6*64], |
306 |
|
Bitstream * bs, |
307 |
|
Statistics * pStat) |
308 |
{ |
{ |
309 |
|
|
310 |
int32_t i; |
int32_t i; |
311 |
uint32_t bits, mcbpc, cbpy; |
uint32_t bits, mcbpc, cbpy; |
312 |
|
|
356 |
// code block coeffs |
// code block coeffs |
357 |
for(i = 0; i < 6; i++) |
for(i = 0; i < 6; i++) |
358 |
if(pMB->cbp & (1 << (5 - i))) |
if(pMB->cbp & (1 << (5 - i))) |
359 |
CodeCoeff(bs, qcoeff[i], inter_table, scan_tables[0], 0); |
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
360 |
|
|
361 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
362 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
363 |
|
|
364 |
} |
} |
365 |
|
|
366 |
|
|
367 |
void MBCoding(const MBParam * pParam, MACROBLOCK *pMB, |
void MBCoding(const MBParam * pParam, |
368 |
int16_t qcoeff[][64], |
MACROBLOCK *pMB, |
369 |
Bitstream * bs, Statistics * pStat) |
int16_t qcoeff[6*64], |
370 |
|
Bitstream * bs, |
371 |
|
Statistics * pStat) |
372 |
{ |
{ |
373 |
|
|
374 |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
375 |
|
|
376 |
if(pParam->coding_type == P_VOP) { |
if(pParam->coding_type == P_VOP) { |
388 |
CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); |
389 |
else |
else |
390 |
CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); |
391 |
|
|
392 |
} |
} |
393 |
|
|
394 |
|
|
398 |
|
|
399 |
int get_mcbpc_intra(Bitstream * bs) |
int get_mcbpc_intra(Bitstream * bs) |
400 |
{ |
{ |
401 |
|
|
402 |
uint32_t index; |
uint32_t index; |
403 |
|
|
404 |
while((index = BitstreamShowBits(bs, 9)) == 1) |
while((index = BitstreamShowBits(bs, 9)) == 1) |
407 |
index >>= 3; |
index >>= 3; |
408 |
|
|
409 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
410 |
|
|
411 |
return mcbpc_intra_table[index].code; |
return mcbpc_intra_table[index].code; |
412 |
|
|
413 |
} |
} |
414 |
|
|
415 |
int get_mcbpc_inter(Bitstream * bs) |
int get_mcbpc_inter(Bitstream * bs) |
416 |
{ |
{ |
417 |
|
|
418 |
uint32_t index; |
uint32_t index; |
419 |
|
|
420 |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
421 |
BitstreamSkip(bs, 9); |
BitstreamSkip(bs, 9); |
422 |
|
|
423 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
424 |
|
|
425 |
return mcbpc_inter_table[index].code; |
return mcbpc_inter_table[index].code; |
426 |
|
|
427 |
} |
} |
428 |
|
|
429 |
int get_cbpy(Bitstream * bs, int intra) |
int get_cbpy(Bitstream * bs, int intra) |
430 |
{ |
{ |
431 |
|
|
432 |
int cbpy; |
int cbpy; |
433 |
uint32_t index = BitstreamShowBits(bs, 6); |
uint32_t index = BitstreamShowBits(bs, 6); |
434 |
|
|
439 |
cbpy = 15 - cbpy; |
cbpy = 15 - cbpy; |
440 |
|
|
441 |
return cbpy; |
return cbpy; |
442 |
|
|
443 |
} |
} |
444 |
|
|
445 |
int get_mv_data(Bitstream * bs) |
int get_mv_data(Bitstream * bs) |
446 |
{ |
{ |
447 |
|
|
448 |
uint32_t index; |
uint32_t index; |
449 |
|
|
450 |
if(BitstreamGetBit(bs)) |
if(BitstreamGetBit(bs)) |
470 |
|
|
471 |
BitstreamSkip(bs, TMNMVtab2[index].len); |
BitstreamSkip(bs, TMNMVtab2[index].len); |
472 |
return TMNMVtab2[index].code; |
return TMNMVtab2[index].code; |
473 |
|
|
474 |
} |
} |
475 |
|
|
476 |
int get_mv(Bitstream * bs, int fcode) |
int get_mv(Bitstream * bs, int fcode) |
477 |
{ |
{ |
478 |
|
|
479 |
int data; |
int data; |
480 |
int res; |
int res; |
481 |
int mv; |
int mv; |
490 |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
491 |
|
|
492 |
return data < 0 ? -mv : mv; |
return data < 0 ? -mv : mv; |
493 |
|
|
494 |
} |
} |
495 |
|
|
496 |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
497 |
{ |
{ |
498 |
|
|
499 |
int code = BitstreamGetBits(bs, dc_size); |
int code = BitstreamGetBits(bs, dc_size); |
500 |
int msb = code >> (dc_size - 1); |
int msb = code >> (dc_size - 1); |
501 |
|
|
503 |
return (-1 * (code^((1 << dc_size) - 1))); |
return (-1 * (code^((1 << dc_size) - 1))); |
504 |
|
|
505 |
return code; |
return code; |
506 |
|
|
507 |
} |
} |
508 |
|
|
509 |
int get_dc_size_lum(Bitstream * bs) |
int get_dc_size_lum(Bitstream * bs) |
510 |
{ |
{ |
511 |
|
|
512 |
int code, i; |
int code, i; |
513 |
code = BitstreamShowBits(bs, 11); |
code = BitstreamShowBits(bs, 11); |
514 |
|
|
522 |
|
|
523 |
BitstreamSkip(bs, dc_lum_tab[code].len); |
BitstreamSkip(bs, dc_lum_tab[code].len); |
524 |
return dc_lum_tab[code].code; |
return dc_lum_tab[code].code; |
525 |
|
|
526 |
} |
} |
527 |
|
|
528 |
|
|
529 |
int get_dc_size_chrom(Bitstream * bs) |
int get_dc_size_chrom(Bitstream * bs) |
530 |
{ |
{ |
531 |
|
|
532 |
uint32_t code, i; |
uint32_t code, i; |
533 |
code = BitstreamShowBits(bs, 12); |
code = BitstreamShowBits(bs, 12); |
534 |
|
|
541 |
} |
} |
542 |
|
|
543 |
return 3 - BitstreamGetBits(bs, 2); |
return 3 - BitstreamGetBits(bs, 2); |
|
} |
|
|
|
|
|
int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header) |
|
|
{ |
|
|
uint32_t mode; |
|
|
const VLC *tab; |
|
|
int32_t level; |
|
|
|
|
|
if(short_video_header) // inter-VLCs will be used for both intra and inter blocks |
|
|
intra = 0; |
|
|
|
|
|
tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
|
|
|
|
|
if(tab->code == -1) |
|
|
goto error; |
|
544 |
|
|
|
BitstreamSkip(bs, tab->len); |
|
|
|
|
|
if(tab->code != ESCAPE) { |
|
|
if(!intra) |
|
|
{ |
|
|
*run = (tab->code >> 4) & 255; |
|
|
level = tab->code & 15; |
|
|
*last = (tab->code >> 12) & 1; |
|
545 |
} |
} |
|
else |
|
|
{ |
|
|
*run = (tab->code >> 8) & 255; |
|
|
level = tab->code & 255; |
|
|
*last = (tab->code >> 16) & 1; |
|
|
} |
|
|
return BitstreamGetBit(bs) ? -level : level; |
|
|
} |
|
|
|
|
|
if(short_video_header) |
|
|
{ |
|
|
// escape mode 4 - H.263 type, only used if short_video_header = 1 |
|
|
*last = BitstreamGetBit(bs); |
|
|
*run = BitstreamGetBits(bs, 6); |
|
|
level = BitstreamGetBits(bs, 8); |
|
|
|
|
|
if (level == 0 || level == 128) |
|
|
DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level); |
|
|
|
|
|
return (level >= 128 ? -(256 - level) : level); |
|
|
} |
|
|
|
|
|
mode = BitstreamShowBits(bs, 2); |
|
|
|
|
|
if(mode < 3) { |
|
|
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
|
|
|
|
|
tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
|
|
if (tab->code == -1) |
|
|
goto error; |
|
|
|
|
|
BitstreamSkip(bs, tab->len); |
|
|
|
|
|
if (!intra) { |
|
|
*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; |
|
|
} |
|
|
|
|
|
if(mode < 2) // first escape mode, level is offset |
|
|
level += max_level[*last + (!intra<<1)][*run]; // need to add back the max level |
|
|
else if(mode == 2) // second escape mode, run is offset |
|
|
*run += max_run[*last + (!intra<<1)][level] + 1; |
|
|
|
|
|
return BitstreamGetBit(bs) ? -level : level; |
|
|
} |
|
|
|
|
|
// third escape mode - fixed length codes |
|
|
BitstreamSkip(bs, 2); |
|
|
*last = BitstreamGetBits(bs, 1); |
|
|
*run = BitstreamGetBits(bs, 6); |
|
|
BitstreamSkip(bs, 1); // marker |
|
|
level = BitstreamGetBits(bs, 12); |
|
|
BitstreamSkip(bs, 1); // marker |
|
|
|
|
|
return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level; |
|
|
|
|
|
error: |
|
|
*run = VLC_ERROR; |
|
|
return 0; |
|
|
} |
|
|
|
|
546 |
|
|
547 |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
548 |
{ |
{ |
549 |
|
|
550 |
const uint16_t * scan = scan_tables[ direction ]; |
const uint16_t * scan = scan_tables[ direction ]; |
551 |
int level; |
int level; |
552 |
int run; |
int run; |
568 |
} |
} |
569 |
coeff++; |
coeff++; |
570 |
} while (!last); |
} while (!last); |
571 |
|
|
572 |
} |
} |
573 |
|
|
574 |
void get_inter_block(Bitstream * bs, int16_t * block) |
void get_inter_block(Bitstream * bs, int16_t * block) |
575 |
{ |
{ |
576 |
|
|
577 |
const uint16_t * scan = scan_tables[0]; |
const uint16_t * scan = scan_tables[0]; |
578 |
int p; |
int p; |
579 |
int level; |
int level; |
597 |
} |
} |
598 |
p++; |
p++; |
599 |
} while (!last); |
} while (!last); |
600 |
|
|
601 |
} |
} |