Parent Directory | Revision Log
Revision 15 - (view) (download)
1 : | Isibaar | 3 | #include "../portab.h" |
2 : | #include "bitstream.h" | ||
3 : | #include "zigzag.h" | ||
4 : | #include "vlc_codes.h" | ||
5 : | |||
6 : | #include "../utils/mbfunctions.h" | ||
7 : | |||
8 : | #define ESCAPE 7167 | ||
9 : | #define ABS(X) (((X)>0)?(X):-(X)) | ||
10 : | #define CLIP(X,A) (X > A) ? (A) : (X) | ||
11 : | |||
12 : | static VLC *DCT3D[2]; | ||
13 : | |||
14 : | VLC *intra_table, *inter_table; | ||
15 : | static short clip_table[4096]; | ||
16 : | |||
17 : | void create_vlc_tables(void) | ||
18 : | { | ||
19 : | int32_t k, l, i, intra, last; | ||
20 : | VLC *vlc[2]; | ||
21 : | VLC **coeff_ptr; | ||
22 : | VLC *vlc1, *vlc2; | ||
23 : | |||
24 : | VLC *DCT3Dintra; | ||
25 : | VLC *DCT3Dinter; | ||
26 : | |||
27 : | DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096); | ||
28 : | DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096); | ||
29 : | |||
30 : | vlc1 = DCT3Dintra; | ||
31 : | vlc2 = DCT3Dinter; | ||
32 : | |||
33 : | vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC)); | ||
34 : | vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC)); | ||
35 : | |||
36 : | // initialize the clipping table | ||
37 : | for(i = -2048; i < 2048; i++) { | ||
38 : | clip_table[i + 2048] = i; | ||
39 : | if(i < -255) | ||
40 : | clip_table[i + 2048] = -255; | ||
41 : | if(i > 255) | ||
42 : | clip_table[i + 2048] = 255; | ||
43 : | } | ||
44 : | |||
45 : | // generate intra/inter vlc lookup table | ||
46 : | for(i = 0; i < 4; i++) { | ||
47 : | intra = i % 2; | ||
48 : | last = i >> 1; | ||
49 : | |||
50 : | coeff_ptr = coeff_vlc[last + (intra << 1)]; | ||
51 : | |||
52 : | for(k = -255; k < 256; k++) { // level | ||
53 : | char *max_level_ptr = max_level[last + (intra << 1)]; | ||
54 : | char *max_run_ptr = max_run[last + (intra << 1)]; | ||
55 : | |||
56 : | for(l = 0; l < 64; l++) { // run | ||
57 : | int32_t level = k, run = l; | ||
58 : | |||
59 : | if(abs(level) <= max_level_ptr[run] && run <= max_run_ptr[abs(level)]) { | ||
60 : | |||
61 : | if(level > 0) { | ||
62 : | vlc[intra]->code = (coeff_ptr[run][level - 1].code) << 1; | ||
63 : | vlc[intra]->len = coeff_ptr[run][level - 1].len + 1; | ||
64 : | } | ||
65 : | else if(level < 0) { | ||
66 : | vlc[intra]->code = ((coeff_ptr[run][-level - 1].code) << 1) + 1; | ||
67 : | vlc[intra]->len = coeff_ptr[run][-level - 1].len + 1; | ||
68 : | } | ||
69 : | else { | ||
70 : | vlc[intra]->code = 0; | ||
71 : | vlc[intra]->len = 0; | ||
72 : | } | ||
73 : | } else { | ||
74 : | if(level > 0) | ||
75 : | level -= max_level_ptr[run]; | ||
76 : | else | ||
77 : | level += max_level_ptr[run]; | ||
78 : | |||
79 : | if(abs(level) <= max_level_ptr[run] && | ||
80 : | run <= max_run_ptr[abs(level)]) { | ||
81 : | |||
82 : | if(level > 0) { | ||
83 : | vlc[intra]->code = (0x06 << (coeff_ptr[run][level - 1].len + 1)) | | ||
84 : | (coeff_ptr[run][level - 1].code << 1); | ||
85 : | vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 8; | ||
86 : | } | ||
87 : | else if(level < 0) { | ||
88 : | vlc[intra]->code = (0x06 << (coeff_ptr[run][-level - 1].len + 1)) | | ||
89 : | ((coeff_ptr[run][-level - 1].code << 1) + 1); | ||
90 : | vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 8; | ||
91 : | } | ||
92 : | else { | ||
93 : | vlc[intra]->code = 0x06; | ||
94 : | vlc[intra]->len = 8; | ||
95 : | } | ||
96 : | } else { | ||
97 : | if(level > 0) | ||
98 : | level += max_level_ptr[run]; | ||
99 : | else | ||
100 : | level -= max_level_ptr[run]; | ||
101 : | |||
102 : | run -= max_run_ptr[abs(level)] + 1; | ||
103 : | |||
104 : | if(abs(level) <= max_level_ptr[run] && | ||
105 : | run <= max_run_ptr[abs(level)]) { | ||
106 : | |||
107 : | if(level > 0) { | ||
108 : | vlc[intra]->code = (0x0e << (coeff_ptr[run][level - 1].len + 1)) | | ||
109 : | (coeff_ptr[run][level - 1].code << 1); | ||
110 : | vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 9; | ||
111 : | } | ||
112 : | else if(level < 0) { | ||
113 : | vlc[intra]->code = (0x0e << (coeff_ptr[run][-level - 1].len + 1)) | | ||
114 : | ((coeff_ptr[run][-level - 1].code << 1) + 1); | ||
115 : | vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 9; | ||
116 : | } | ||
117 : | else { | ||
118 : | vlc[intra]->code = 0x0e; | ||
119 : | vlc[intra]->len = 9; | ||
120 : | } | ||
121 : | } else { | ||
122 : | if(level != 0) | ||
123 : | run += max_run_ptr[abs(level)] + 1; | ||
124 : | else | ||
125 : | run++; | ||
126 : | |||
127 : | vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) | | ||
128 : | (l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1; | ||
129 : | |||
130 : | vlc[intra]->len = 30; | ||
131 : | } | ||
132 : | } | ||
133 : | } | ||
134 : | vlc[intra]++; | ||
135 : | } | ||
136 : | } | ||
137 : | } | ||
138 : | intra_table += 64*255; // center vlc tables | ||
139 : | inter_table += 64*255; // center vlc tables | ||
140 : | |||
141 : | for(i = 0; i < 4096; i++) { | ||
142 : | if(i >= 512) { | ||
143 : | *vlc1 = DCT3Dtab3[(i >> 5) - 16]; | ||
144 : | *vlc2 = DCT3Dtab0[(i >> 5) - 16]; | ||
145 : | } | ||
146 : | else if(i >= 128) { | ||
147 : | *vlc1 = DCT3Dtab4[(i >> 2) - 32]; | ||
148 : | *vlc2 = DCT3Dtab1[(i >> 2) - 32]; | ||
149 : | } | ||
150 : | else if(i >= 8) { | ||
151 : | *vlc1 = DCT3Dtab5[i - 8]; | ||
152 : | *vlc2 = DCT3Dtab2[i - 8]; | ||
153 : | } | ||
154 : | else { | ||
155 : | *vlc1 = ERRtab[i]; | ||
156 : | *vlc2 = ERRtab[i]; | ||
157 : | } | ||
158 : | |||
159 : | vlc1++; | ||
160 : | vlc2++; | ||
161 : | } | ||
162 : | DCT3D[0] = DCT3Dinter; | ||
163 : | DCT3D[1] = DCT3Dintra; | ||
164 : | |||
165 : | } | ||
166 : | |||
167 : | void destroy_vlc_tables(void) { | ||
168 : | |||
169 : | if(intra_table != NULL && inter_table != NULL) { | ||
170 : | intra_table -= 64*255; // uncenter vlc tables | ||
171 : | inter_table -= 64*255; // uncenter vlc tables | ||
172 : | |||
173 : | free(intra_table); | ||
174 : | free(inter_table); | ||
175 : | } | ||
176 : | |||
177 : | if(DCT3D[0] != NULL && DCT3D[1] != NULL) { | ||
178 : | free(DCT3D[0]); | ||
179 : | free(DCT3D[1]); | ||
180 : | } | ||
181 : | |||
182 : | } | ||
183 : | |||
184 : | static __inline void CodeVector(Bitstream *bs, int16_t value, int16_t f_code, Statistics *pStat) | ||
185 : | { | ||
186 : | const int scale_factor = 1 << (f_code - 1); | ||
187 : | const int cmp = scale_factor << 5; | ||
188 : | |||
189 : | if(value < (-1 * cmp)) | ||
190 : | value += 64 * scale_factor; | ||
191 : | |||
192 : | if(value > (cmp - 1)) | ||
193 : | value -= 64 * scale_factor; | ||
194 : | |||
195 : | pStat->iMvSum += value * value; | ||
196 : | pStat->iMvCount++; | ||
197 : | |||
198 : | if (value == 0) | ||
199 : | BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); | ||
200 : | else { | ||
201 : | uint16_t length, code, mv_res, sign; | ||
202 : | |||
203 : | length = 16 << f_code; | ||
204 : | f_code--; | ||
205 : | |||
206 : | sign = (value < 0); | ||
207 : | |||
208 : | if(value >= length) | ||
209 : | value -= 2 * length; | ||
210 : | else if(value < -length) | ||
211 : | value += 2 * length; | ||
212 : | |||
213 : | if(sign) | ||
214 : | value = -value; | ||
215 : | |||
216 : | value--; | ||
217 : | mv_res = value & ((1 << f_code) - 1); | ||
218 : | code = ((value - mv_res) >> f_code) + 1; | ||
219 : | |||
220 : | if(sign) | ||
221 : | code = -code; | ||
222 : | |||
223 : | code += 32; | ||
224 : | BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len); | ||
225 : | |||
226 : | if(f_code) | ||
227 : | BitstreamPutBits(bs, mv_res, f_code); | ||
228 : | } | ||
229 : | } | ||
230 : | |||
231 : | |||
232 : | static __inline void CodeCoeff(Bitstream *bs, int16_t qcoeff[64], VLC *table, | ||
233 : | const uint16_t *zigzag, uint16_t intra) { | ||
234 : | uint32_t j, last; | ||
235 : | short v; | ||
236 : | VLC *vlc; | ||
237 : | |||
238 : | j = intra; | ||
239 : | last = 1 + intra; | ||
240 : | |||
241 : | while((v = qcoeff[zigzag[j++]]) == 0); | ||
242 : | |||
243 : | do { | ||
244 : | // count zeroes | ||
245 : | vlc = table + (clip_table[2048+v] << 6) + j - last; | ||
246 : | last = j + 1; | ||
247 : | while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0); | ||
248 : | |||
249 : | // write code | ||
250 : | if(j != 64) { | ||
251 : | BitstreamPutBits(bs, vlc->code, vlc->len); | ||
252 : | } else { | ||
253 : | vlc += 64*511; | ||
254 : | BitstreamPutBits(bs, vlc->code, vlc->len); | ||
255 : | break; | ||
256 : | } | ||
257 : | } while(1); | ||
258 : | } | ||
259 : | |||
260 : | |||
261 : | static void CodeBlockIntra(const MBParam * pParam, const MACROBLOCK *pMB, | ||
262 : | int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) | ||
263 : | { | ||
264 : | uint32_t i, mcbpc, cbpy, bits; | ||
265 : | |||
266 : | mcbpc = pMB->cbp & 3; | ||
267 : | cbpy = pMB->cbp >> 2; | ||
268 : | |||
269 : | // write mcbpc | ||
270 : | if(pParam->coding_type == I_VOP) | ||
271 : | BitstreamPutBits(bs, mcbpc_I[mcbpc].code, mcbpc_I[mcbpc].len); | ||
272 : | else | ||
273 : | BitstreamPutBits(bs, mcbpc_P_intra[mcbpc].code, mcbpc_P_intra[mcbpc].len); | ||
274 : | |||
275 : | // ac prediction flag | ||
276 : | if(pMB->acpred_directions[0]) | ||
277 : | BitstreamPutBits(bs, 1, 1); | ||
278 : | else | ||
279 : | BitstreamPutBits(bs, 0, 1); | ||
280 : | |||
281 : | // write cbpy | ||
282 : | BitstreamPutBits (bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); | ||
283 : | |||
284 : | // write dquant | ||
285 : | if(pMB->mode == MODE_INTRA_Q) | ||
286 : | BitstreamPutBits(bs, pMB->dquant, 2); | ||
287 : | |||
288 : | // code block coeffs | ||
289 : | for(i = 0; i < 6; i++) | ||
290 : | { | ||
291 : | if(i < 4) | ||
292 : | BitstreamPutBits(bs, dcy_tab[qcoeff[i][0] + 255].code, | ||
293 : | dcy_tab[qcoeff[i][0] + 255].len); | ||
294 : | else | ||
295 : | BitstreamPutBits(bs, dcc_tab[qcoeff[i][0] + 255].code, | ||
296 : | dcc_tab[qcoeff[i][0] + 255].len); | ||
297 : | |||
298 : | if(pMB->cbp & (1 << (5 - i))) | ||
299 : | { | ||
300 : | bits = BitstreamPos(bs); | ||
301 : | |||
302 : | CodeCoeff(bs, qcoeff[i], intra_table, scan_tables[pMB->acpred_directions[i]], 1); | ||
303 : | |||
304 : | bits = BitstreamPos(bs) - bits; | ||
305 : | pStat->iTextBits += bits; | ||
306 : | } | ||
307 : | } | ||
308 : | } | ||
309 : | |||
310 : | |||
311 : | static void CodeBlockInter(const MBParam * pParam, const MACROBLOCK *pMB, | ||
312 : | int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) | ||
313 : | { | ||
314 : | int32_t i; | ||
315 : | uint32_t bits, mcbpc, cbpy; | ||
316 : | |||
317 : | mcbpc = pMB->cbp & 3; | ||
318 : | cbpy = 15 - (pMB->cbp >> 2); | ||
319 : | |||
320 : | // write mcbpc | ||
321 : | if(pMB->mode == MODE_INTER4V) | ||
322 : | BitstreamPutBits(bs, mcbpc_P_inter4v[mcbpc].code, mcbpc_P_inter4v[mcbpc].len); | ||
323 : | else | ||
324 : | BitstreamPutBits(bs, mcbpc_P_inter[mcbpc].code, mcbpc_P_inter[mcbpc].len); | ||
325 : | |||
326 : | // write cbpy | ||
327 : | BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); | ||
328 : | |||
329 : | // write dquant | ||
330 : | if(pMB->mode == MODE_INTER_Q) | ||
331 : | BitstreamPutBits(bs, pMB->dquant, 2); | ||
332 : | |||
333 : | // code motion vector(s) | ||
334 : | for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) | ||
335 : | { | ||
336 : | CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat); | ||
337 : | CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat); | ||
338 : | } | ||
339 : | |||
340 : | bits = BitstreamPos(bs); | ||
341 : | |||
342 : | // code block coeffs | ||
343 : | for(i = 0; i < 6; i++) | ||
344 : | if(pMB->cbp & (1 << (5 - i))) | ||
345 : | CodeCoeff(bs, qcoeff[i], inter_table, scan_tables[0], 0); | ||
346 : | |||
347 : | bits = BitstreamPos(bs) - bits; | ||
348 : | pStat->iTextBits += bits; | ||
349 : | } | ||
350 : | |||
351 : | |||
352 : | void MBCoding(const MBParam * pParam, MACROBLOCK *pMB, | ||
353 : | int16_t qcoeff[][64], | ||
354 : | Bitstream * bs, Statistics * pStat) | ||
355 : | { | ||
356 : | int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); | ||
357 : | |||
358 : | if(pParam->coding_type == P_VOP) { | ||
359 : | if(pMB->cbp == 0 && pMB->mode == MODE_INTER && | ||
360 : | pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) | ||
361 : | { | ||
362 : | BitstreamPutBit(bs, 1); // not_coded | ||
363 : | return; | ||
364 : | } | ||
365 : | else | ||
366 : | BitstreamPutBit(bs, 0); // coded | ||
367 : | } | ||
368 : | |||
369 : | if(intra) | ||
370 : | CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); | ||
371 : | else | ||
372 : | CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); | ||
373 : | } | ||
374 : | |||
375 : | |||
376 : | /*************************************************************** | ||
377 : | edgomez | 15 | * decoding stuff starts here * |
378 : | ***************************************************************/ | ||
379 : | Isibaar | 3 | |
380 : | int get_mcbpc_intra(Bitstream * bs) | ||
381 : | { | ||
382 : | uint32_t index; | ||
383 : | |||
384 : | while((index = BitstreamShowBits(bs, 9)) == 1) | ||
385 : | BitstreamSkip(bs, 9); | ||
386 : | |||
387 : | index >>= 3; | ||
388 : | |||
389 : | BitstreamSkip(bs, mcbpc_intra_table[index].len); | ||
390 : | return mcbpc_intra_table[index].code; | ||
391 : | } | ||
392 : | |||
393 : | int get_mcbpc_inter(Bitstream * bs) | ||
394 : | { | ||
395 : | uint32_t index; | ||
396 : | |||
397 : | while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) | ||
398 : | BitstreamSkip(bs, 9); | ||
399 : | |||
400 : | BitstreamSkip(bs, mcbpc_inter_table[index].len); | ||
401 : | return mcbpc_inter_table[index].code; | ||
402 : | } | ||
403 : | |||
404 : | int get_cbpy(Bitstream * bs, int intra) | ||
405 : | { | ||
406 : | int cbpy; | ||
407 : | uint32_t index = BitstreamShowBits(bs, 6); | ||
408 : | |||
409 : | BitstreamSkip(bs, cbpy_table[index].len); | ||
410 : | cbpy = cbpy_table[index].code; | ||
411 : | |||
412 : | if(!intra) | ||
413 : | cbpy = 15 - cbpy; | ||
414 : | |||
415 : | return cbpy; | ||
416 : | } | ||
417 : | |||
418 : | int get_mv_data(Bitstream * bs) | ||
419 : | { | ||
420 : | uint32_t index; | ||
421 : | |||
422 : | if(BitstreamGetBit(bs)) | ||
423 : | return 0; | ||
424 : | |||
425 : | index = BitstreamShowBits(bs, 12); | ||
426 : | |||
427 : | if(index >= 512) | ||
428 : | { | ||
429 : | index = (index >> 8) - 2; | ||
430 : | BitstreamSkip(bs, TMNMVtab0[index].len); | ||
431 : | return TMNMVtab0[index].code; | ||
432 : | } | ||
433 : | |||
434 : | if(index >= 128) | ||
435 : | { | ||
436 : | index = (index >> 2) - 32; | ||
437 : | BitstreamSkip(bs, TMNMVtab1[index].len); | ||
438 : | return TMNMVtab1[index].code; | ||
439 : | } | ||
440 : | |||
441 : | index -= 4; | ||
442 : | |||
443 : | BitstreamSkip(bs, TMNMVtab2[index].len); | ||
444 : | return TMNMVtab2[index].code; | ||
445 : | } | ||
446 : | |||
447 : | int get_mv(Bitstream * bs, int fcode) | ||
448 : | { | ||
449 : | int data; | ||
450 : | int res; | ||
451 : | int mv; | ||
452 : | int scale_fac = 1 << (fcode - 1); | ||
453 : | |||
454 : | data = get_mv_data(bs); | ||
455 : | |||
456 : | if(scale_fac == 1 || data == 0) | ||
457 : | return data; | ||
458 : | |||
459 : | res = BitstreamGetBits(bs, fcode - 1); | ||
460 : | mv = ((ABS(data) - 1) * scale_fac) + res + 1; | ||
461 : | |||
462 : | return data < 0 ? -mv : mv; | ||
463 : | } | ||
464 : | |||
465 : | int get_dc_dif(Bitstream * bs, uint32_t dc_size) | ||
466 : | { | ||
467 : | int code = BitstreamGetBits(bs, dc_size); | ||
468 : | int msb = code >> (dc_size - 1); | ||
469 : | |||
470 : | if(msb == 0) | ||
471 : | return (-1 * (code^((1 << dc_size) - 1))); | ||
472 : | |||
473 : | return code; | ||
474 : | } | ||
475 : | |||
476 : | int get_dc_size_lum(Bitstream * bs) | ||
477 : | { | ||
478 : | int code, i; | ||
479 : | code = BitstreamShowBits(bs, 11); | ||
480 : | |||
481 : | for(i = 11; i > 3; i--) { | ||
482 : | if(code == 1) { | ||
483 : | BitstreamSkip(bs, i); | ||
484 : | return i + 1; | ||
485 : | } | ||
486 : | code >>= 1; | ||
487 : | } | ||
488 : | |||
489 : | BitstreamSkip(bs, dc_lum_tab[code].len); | ||
490 : | return dc_lum_tab[code].code; | ||
491 : | } | ||
492 : | |||
493 : | |||
494 : | int get_dc_size_chrom(Bitstream * bs) | ||
495 : | { | ||
496 : | uint32_t code, i; | ||
497 : | code = BitstreamShowBits(bs, 12); | ||
498 : | |||
499 : | for(i = 12; i > 2; i--) { | ||
500 : | if(code == 1) { | ||
501 : | BitstreamSkip(bs, i); | ||
502 : | return i; | ||
503 : | } | ||
504 : | code >>= 1; | ||
505 : | } | ||
506 : | |||
507 : | return 3 - BitstreamGetBits(bs, 2); | ||
508 : | } | ||
509 : | |||
510 : | int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header) | ||
511 : | { | ||
512 : | uint32_t mode; | ||
513 : | const VLC *tab; | ||
514 : | int32_t level; | ||
515 : | |||
516 : | if(short_video_header) // inter-VLCs will be used for both intra and inter blocks | ||
517 : | intra = 0; | ||
518 : | |||
519 : | tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; | ||
520 : | |||
521 : | if(tab->code == -1) | ||
522 : | goto error; | ||
523 : | |||
524 : | BitstreamSkip(bs, tab->len); | ||
525 : | |||
526 : | if(tab->code != ESCAPE) { | ||
527 : | if(!intra) | ||
528 : | { | ||
529 : | *run = (tab->code >> 4) & 255; | ||
530 : | level = tab->code & 15; | ||
531 : | *last = (tab->code >> 12) & 1; | ||
532 : | } | ||
533 : | else | ||
534 : | { | ||
535 : | *run = (tab->code >> 8) & 255; | ||
536 : | level = tab->code & 255; | ||
537 : | *last = (tab->code >> 16) & 1; | ||
538 : | } | ||
539 : | return BitstreamGetBit(bs) ? -level : level; | ||
540 : | } | ||
541 : | |||
542 : | if(short_video_header) | ||
543 : | { | ||
544 : | // escape mode 4 - H.263 type, only used if short_video_header = 1 | ||
545 : | *last = BitstreamGetBit(bs); | ||
546 : | *run = BitstreamGetBits(bs, 6); | ||
547 : | level = BitstreamGetBits(bs, 8); | ||
548 : | |||
549 : | if (level == 0 || level == 128) | ||
550 : | DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level); | ||
551 : | |||
552 : | return (level >= 128 ? -(256 - level) : level); | ||
553 : | } | ||
554 : | |||
555 : | mode = BitstreamShowBits(bs, 2); | ||
556 : | |||
557 : | if(mode < 3) { | ||
558 : | BitstreamSkip(bs, (mode == 2) ? 2 : 1); | ||
559 : | |||
560 : | tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; | ||
561 : | if (tab->code == -1) | ||
562 : | goto error; | ||
563 : | |||
564 : | BitstreamSkip(bs, tab->len); | ||
565 : | |||
566 : | if (!intra) { | ||
567 : | *run = (tab->code >> 4) & 255; | ||
568 : | level = tab->code & 15; | ||
569 : | *last = (tab->code >> 12) & 1; | ||
570 : | } | ||
571 : | else | ||
572 : | { | ||
573 : | *run = (tab->code >> 8) & 255; | ||
574 : | level = tab->code & 255; | ||
575 : | *last = (tab->code >> 16) & 1; | ||
576 : | } | ||
577 : | |||
578 : | if(mode < 2) // first escape mode, level is offset | ||
579 : | level += max_level[*last + (!intra<<1)][*run]; // need to add back the max level | ||
580 : | else if(mode == 2) // second escape mode, run is offset | ||
581 : | *run += max_run[*last + (!intra<<1)][level] + 1; | ||
582 : | |||
583 : | return BitstreamGetBit(bs) ? -level : level; | ||
584 : | } | ||
585 : | |||
586 : | // third escape mode - fixed length codes | ||
587 : | BitstreamSkip(bs, 2); | ||
588 : | *last = BitstreamGetBits(bs, 1); | ||
589 : | *run = BitstreamGetBits(bs, 6); | ||
590 : | BitstreamSkip(bs, 1); // marker | ||
591 : | level = BitstreamGetBits(bs, 12); | ||
592 : | BitstreamSkip(bs, 1); // marker | ||
593 : | |||
594 : | return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level; | ||
595 : | |||
596 : | error: | ||
597 : | *run = VLC_ERROR; | ||
598 : | return 0; | ||
599 : | } | ||
600 : | |||
601 : | |||
602 : | void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) | ||
603 : | { | ||
604 : | const uint16_t * scan = scan_tables[ direction ]; | ||
605 : | int level; | ||
606 : | int run; | ||
607 : | int last; | ||
608 : | |||
609 : | do | ||
610 : | { | ||
611 : | level = get_coeff(bs, &run, &last, 1, 0); | ||
612 : | if (run == -1) | ||
613 : | { | ||
614 : | DEBUG("fatal: invalid run"); | ||
615 : | break; | ||
616 : | } | ||
617 : | coeff += run; | ||
618 : | block[ scan[coeff] ] = level; | ||
619 : | if (level < -127 || level > 127) | ||
620 : | { | ||
621 : | DEBUG1("warning: intra_overflow", level); | ||
622 : | } | ||
623 : | coeff++; | ||
624 : | } while (!last); | ||
625 : | } | ||
626 : | |||
627 : | void get_inter_block(Bitstream * bs, int16_t * block) | ||
628 : | { | ||
629 : | const uint16_t * scan = scan_tables[0]; | ||
630 : | int p; | ||
631 : | int level; | ||
632 : | int run; | ||
633 : | int last; | ||
634 : | |||
635 : | p = 0; | ||
636 : | do | ||
637 : | { | ||
638 : | level = get_coeff(bs, &run, &last, 0, 0); | ||
639 : | if (run == -1) | ||
640 : | { | ||
641 : | DEBUG("fatal: invalid run"); | ||
642 : | break; | ||
643 : | } | ||
644 : | p += run; | ||
645 : | block[ scan[p] ] = level; | ||
646 : | if (level < -127 || level > 127) | ||
647 : | { | ||
648 : | DEBUG1("warning: inter_overflow", level); | ||
649 : | } | ||
650 : | p++; | ||
651 : | } while (!last); | ||
652 : | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |