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