[svn] / trunk / xvidcore / src / bitstream / mbcoding.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/src/bitstream/mbcoding.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 153 - (view) (download)

1 : suxen_drol 118 /******************************************************************************
2 :     * *
3 :     * This file is part of XviD, a free MPEG-4 video encoder/decoder *
4 :     * *
5 :     * XviD is an implementation of a part of one or more MPEG-4 Video tools *
6 :     * as specified in ISO/IEC 14496-2 standard. Those intending to use this *
7 :     * software module in hardware or software products are advised that its *
8 :     * use may infringe existing patents or copyrights, and any such use *
9 :     * would be at such party's own risk. The original developer of this *
10 :     * software module and his/her company, and subsequent editors and their *
11 :     * companies, will have no liability for use of this software or *
12 :     * modifications or derivatives thereof. *
13 :     * *
14 :     * XviD is free software; you can redistribute it and/or modify it *
15 :     * under the terms of the GNU General Public License as published by *
16 :     * the Free Software Foundation; either version 2 of the License, or *
17 :     * (at your option) any later version. *
18 :     * *
19 :     * XviD is distributed in the hope that it will be useful, but *
20 :     * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 :     * GNU General Public License for more details. *
23 :     * *
24 :     * You should have received a copy of the GNU General Public License *
25 :     * along with this program; if not, write to the Free Software *
26 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
27 :     * *
28 :     ******************************************************************************/
29 :    
30 :     /******************************************************************************
31 :     * *
32 : Isibaar 153 * mbcoding.c *
33 : suxen_drol 118 * *
34 : Isibaar 153 * Copyright (C) 2002 - Michael Militzer <isibaar@xvid.org> *
35 : suxen_drol 118 * *
36 :     * For more information visit the XviD homepage: http://www.xvid.org *
37 :     * *
38 :     ******************************************************************************/
39 :    
40 :     /******************************************************************************
41 :     * *
42 :     * Revision history: *
43 :     * *
44 : Isibaar 153 * 14.04.2002 bframe encoding *
45 : suxen_drol 118 * 08.03.2002 initial version; isibaar *
46 :     * *
47 :     ******************************************************************************/
48 :    
49 :    
50 :    
51 : Isibaar 100 #include <stdlib.h>
52 : Isibaar 3 #include "../portab.h"
53 :     #include "bitstream.h"
54 :     #include "zigzag.h"
55 :     #include "vlc_codes.h"
56 : Isibaar 100 #include "mbcoding.h"
57 : Isibaar 3
58 :     #include "../utils/mbfunctions.h"
59 :    
60 :     #define ABS(X) (((X)>0)?(X):-(X))
61 :     #define CLIP(X,A) (X > A) ? (A) : (X)
62 :    
63 : Isibaar 153 VLC intra_table[524032];
64 :     VLC inter_table[524032];
65 : Isibaar 100
66 : Isibaar 114 VLC DCT3Dintra[4096];
67 :     VLC DCT3Dinter[4096];
68 : Isibaar 3
69 : Isibaar 100 void init_vlc_tables(void)
70 : Isibaar 3 {
71 : edgomez 78
72 : Isibaar 3 int32_t k, l, i, intra, last;
73 :     VLC *vlc[2];
74 :     VLC **coeff_ptr;
75 :     VLC *vlc1, *vlc2;
76 :    
77 :     vlc1 = DCT3Dintra;
78 :     vlc2 = DCT3Dinter;
79 :    
80 : Isibaar 100 vlc[0] = intra_table;
81 :     vlc[1] = inter_table;
82 : Isibaar 3
83 : Isibaar 100 // generate encoding vlc lookup tables
84 : Isibaar 153 // the lookup table idea is taken from the excellent fame project by Vivien Chapellier
85 : Isibaar 3 for(i = 0; i < 4; i++) {
86 :     intra = i % 2;
87 : Isibaar 100 last = i / 2;
88 : Isibaar 3
89 : Isibaar 100 coeff_ptr = coeff_vlc[last + 2 * intra];
90 : Isibaar 153
91 :     for(k = -2047; k < 2048; k++) { // level
92 : Isibaar 100 int8_t *max_level_ptr = max_level[last + 2 * intra];
93 :     int8_t *max_run_ptr = max_run[last + 2 * intra];
94 : Isibaar 3
95 :     for(l = 0; l < 64; l++) { // run
96 : Isibaar 35 int32_t level = k;
97 :     uint32_t run = l;
98 : Isibaar 100
99 :     if((abs(level) <= max_level_ptr[run]) &&
100 : chenm001 133 (run <= (uint32_t)max_run_ptr[abs(level)])) { // level < max_level and run < max_run
101 : Isibaar 100
102 : Isibaar 3 vlc[intra]->code = 0;
103 :     vlc[intra]->len = 0;
104 : Isibaar 100 goto loop_end;
105 :     }
106 :     else {
107 :     if(level > 0) // correct level
108 : Isibaar 3 level -= max_level_ptr[run];
109 :     else
110 :     level += max_level_ptr[run];
111 : Isibaar 35
112 : Isibaar 100 if((abs(level) <= max_level_ptr[run]) &&
113 : chenm001 133 (run <= (uint32_t) max_run_ptr[abs(level)])) {
114 : Isibaar 28
115 : Isibaar 100 vlc[intra]->code = 0x06;
116 :     vlc[intra]->len = 8;
117 :     goto loop_end;
118 :     }
119 : Isibaar 3
120 : Isibaar 100 if(level > 0) // still here?
121 :     level += max_level_ptr[run]; // restore level
122 :     else
123 :     level -= max_level_ptr[run];
124 :    
125 :     run -= max_run_ptr[abs(level)] + 1; // and change run
126 :    
127 :     if((abs(level) <= max_level_ptr[run]) &&
128 : chenm001 133 (run <= (uint32_t) max_run_ptr[abs(level)])) {
129 : Isibaar 100
130 :     vlc[intra]->code = 0x0e;
131 :     vlc[intra]->len = 9;
132 :     goto loop_end;
133 : Isibaar 3 }
134 : Isibaar 100 run += max_run_ptr[abs(level)] + 1;
135 : Isibaar 3 }
136 : Isibaar 100
137 :     vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) |
138 :     (1 << 13) | ((k & 0xfff) << 1) | 1;
139 :    
140 :     vlc[intra]->len = 30;
141 : Isibaar 3 vlc[intra]++;
142 : Isibaar 100 continue;
143 :    
144 :     loop_end:
145 :     if(level != 0) {
146 :     vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) |
147 :     (coeff_ptr[run][abs(level) - 1].code << 1);
148 :     vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len;
149 :    
150 :     if(level < 0)
151 :     vlc[intra]->code += 1;
152 :     }
153 :    
154 :     vlc[intra]++;
155 : Isibaar 3 }
156 :     }
157 :     }
158 : edgomez 78
159 : Isibaar 3 for(i = 0; i < 4096; i++) {
160 :     if(i >= 512) {
161 :     *vlc1 = DCT3Dtab3[(i >> 5) - 16];
162 :     *vlc2 = DCT3Dtab0[(i >> 5) - 16];
163 :     }
164 :     else if(i >= 128) {
165 :     *vlc1 = DCT3Dtab4[(i >> 2) - 32];
166 :     *vlc2 = DCT3Dtab1[(i >> 2) - 32];
167 :     }
168 :     else if(i >= 8) {
169 :     *vlc1 = DCT3Dtab5[i - 8];
170 :     *vlc2 = DCT3Dtab2[i - 8];
171 :     }
172 :     else {
173 :     *vlc1 = ERRtab[i];
174 :     *vlc2 = ERRtab[i];
175 :     }
176 :    
177 :     vlc1++;
178 :     vlc2++;
179 :     }
180 :     DCT3D[0] = DCT3Dinter;
181 :     DCT3D[1] = DCT3Dintra;
182 :    
183 :     }
184 :    
185 : edgomez 78 static __inline void CodeVector(Bitstream *bs,
186 : suxen_drol 136 int32_t value,
187 :     int32_t f_code,
188 : edgomez 78 Statistics *pStat)
189 : Isibaar 3 {
190 : edgomez 78
191 : Isibaar 3 const int scale_factor = 1 << (f_code - 1);
192 :     const int cmp = scale_factor << 5;
193 :    
194 :     if(value < (-1 * cmp))
195 :     value += 64 * scale_factor;
196 :    
197 :     if(value > (cmp - 1))
198 :     value -= 64 * scale_factor;
199 :    
200 : edgomez 78 pStat->iMvSum += value * value;
201 :     pStat->iMvCount++;
202 : Isibaar 3
203 : edgomez 78 if (value == 0) {
204 : Isibaar 3 BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len);
205 : edgomez 78 } else {
206 : Isibaar 3 uint16_t length, code, mv_res, sign;
207 :    
208 :     length = 16 << f_code;
209 :     f_code--;
210 :    
211 :     sign = (value < 0);
212 :    
213 :     if(value >= length)
214 :     value -= 2 * length;
215 :     else if(value < -length)
216 :     value += 2 * length;
217 :    
218 :     if(sign)
219 :     value = -value;
220 :    
221 :     value--;
222 :     mv_res = value & ((1 << f_code) - 1);
223 :     code = ((value - mv_res) >> f_code) + 1;
224 :    
225 :     if(sign)
226 :     code = -code;
227 :    
228 :     code += 32;
229 :     BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len);
230 :    
231 :     if(f_code)
232 :     BitstreamPutBits(bs, mv_res, f_code);
233 : edgomez 78 }
234 :    
235 : Isibaar 3 }
236 :    
237 :    
238 : edgomez 78 static __inline void CodeCoeff(Bitstream *bs,
239 : suxen_drol 136 const int16_t qcoeff[64],
240 : edgomez 78 VLC *table,
241 :     const uint16_t *zigzag,
242 :     uint16_t intra)
243 :     {
244 :    
245 : Isibaar 3 uint32_t j, last;
246 :     short v;
247 :     VLC *vlc;
248 :    
249 :     j = intra;
250 : Isibaar 116 last = intra;
251 : Isibaar 3
252 : Isibaar 153 while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++;
253 : Isibaar 3
254 :     do {
255 : Isibaar 153 vlc = table + 64 * 2047 + (v << 6) + j - last;
256 : Isibaar 116 last = ++j;
257 : Isibaar 153
258 :     // count zeroes
259 : Isibaar 116 while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++;
260 : Isibaar 3
261 :     // write code
262 :     if(j != 64) {
263 :     BitstreamPutBits(bs, vlc->code, vlc->len);
264 :     } else {
265 : Isibaar 153 vlc += 64 * 4095;
266 : Isibaar 3 BitstreamPutBits(bs, vlc->code, vlc->len);
267 :     break;
268 :     }
269 :     } while(1);
270 : edgomez 78
271 : Isibaar 3 }
272 :    
273 :    
274 : suxen_drol 136 static void CodeBlockIntra(const FRAMEINFO * frame,
275 : edgomez 78 const MACROBLOCK *pMB,
276 :     int16_t qcoeff[6*64],
277 :     Bitstream * bs,
278 :     Statistics * pStat)
279 : Isibaar 3 {
280 : edgomez 78
281 : Isibaar 3 uint32_t i, mcbpc, cbpy, bits;
282 :    
283 :     cbpy = pMB->cbp >> 2;
284 :    
285 : edgomez 78 // write mcbpc
286 : suxen_drol 136 if(frame->coding_type == I_VOP) {
287 : edgomez 78 mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2);
288 : Isibaar 28 BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len);
289 :     }
290 :     else {
291 : edgomez 78 mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
292 : Isibaar 28 BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);
293 :     }
294 : Isibaar 3
295 :     // ac prediction flag
296 :     if(pMB->acpred_directions[0])
297 : edgomez 78 BitstreamPutBits(bs, 1, 1);
298 : Isibaar 3 else
299 : edgomez 78 BitstreamPutBits(bs, 0, 1);
300 : Isibaar 3
301 : edgomez 78 // write cbpy
302 : Isibaar 3 BitstreamPutBits (bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
303 :    
304 :     // write dquant
305 : edgomez 78 if(pMB->mode == MODE_INTRA_Q)
306 : Isibaar 3 BitstreamPutBits(bs, pMB->dquant, 2);
307 :    
308 : h 69 // write interlacing
309 : suxen_drol 136 if (frame->global_flags & XVID_INTERLACING)
310 : h 69 {
311 :     BitstreamPutBit(bs, pMB->field_dct);
312 :     }
313 :    
314 : Isibaar 3 // code block coeffs
315 :     for(i = 0; i < 6; i++)
316 :     {
317 :     if(i < 4)
318 : edgomez 78 BitstreamPutBits(bs,
319 :     dcy_tab[qcoeff[i*64 + 0] + 255].code,
320 :     dcy_tab[qcoeff[i*64 + 0] + 255].len);
321 : Isibaar 3 else
322 : edgomez 78 BitstreamPutBits(bs,
323 :     dcc_tab[qcoeff[i*64 + 0] + 255].code,
324 :     dcc_tab[qcoeff[i*64 + 0] + 255].len);
325 : Isibaar 3
326 :     if(pMB->cbp & (1 << (5 - i)))
327 :     {
328 :     bits = BitstreamPos(bs);
329 :    
330 : edgomez 78 CodeCoeff(bs,
331 :     &qcoeff[i*64],
332 :     intra_table,
333 :     scan_tables[pMB->acpred_directions[i]],
334 :     1);
335 : Isibaar 3
336 :     bits = BitstreamPos(bs) - bits;
337 :     pStat->iTextBits += bits;
338 :     }
339 :     }
340 : edgomez 78
341 : Isibaar 3 }
342 :    
343 :    
344 : suxen_drol 136 static void CodeBlockInter(const FRAMEINFO * frame,
345 : edgomez 78 const MACROBLOCK *pMB,
346 :     int16_t qcoeff[6*64],
347 :     Bitstream * bs,
348 :     Statistics * pStat)
349 : Isibaar 3 {
350 : edgomez 78
351 : Isibaar 3 int32_t i;
352 :     uint32_t bits, mcbpc, cbpy;
353 :    
354 : edgomez 78 mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
355 : Isibaar 3 cbpy = 15 - (pMB->cbp >> 2);
356 :    
357 :     // write mcbpc
358 : edgomez 78 BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len);
359 : Isibaar 3
360 :     // write cbpy
361 :     BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
362 :    
363 :     // write dquant
364 : edgomez 78 if(pMB->mode == MODE_INTER_Q)
365 : Isibaar 3 BitstreamPutBits(bs, pMB->dquant, 2);
366 :    
367 : h 69 // interlacing
368 : suxen_drol 136 if (frame->global_flags & XVID_INTERLACING)
369 : h 69 {
370 :     BitstreamPutBit(bs, pMB->field_dct);
371 :     DEBUG1("codep: field_dct: ", pMB->field_dct);
372 :    
373 :     // if inter block, write field ME flag
374 :     if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)
375 :     {
376 :     BitstreamPutBit(bs, pMB->field_pred);
377 :     DEBUG1("codep: field_pred: ", pMB->field_pred);
378 :    
379 :     // write field prediction references
380 :     if (pMB->field_pred)
381 :     {
382 :     BitstreamPutBit(bs, pMB->field_for_top);
383 :     BitstreamPutBit(bs, pMB->field_for_bot);
384 :     }
385 :     }
386 :     }
387 :    
388 : Isibaar 3 // code motion vector(s)
389 :     for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++)
390 :     {
391 : suxen_drol 136 CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
392 :     CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
393 : Isibaar 3 }
394 :    
395 :     bits = BitstreamPos(bs);
396 :    
397 :     // code block coeffs
398 :     for(i = 0; i < 6; i++)
399 :     if(pMB->cbp & (1 << (5 - i)))
400 : edgomez 78 CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0);
401 : Isibaar 3
402 :     bits = BitstreamPos(bs) - bits;
403 :     pStat->iTextBits += bits;
404 : edgomez 78
405 : Isibaar 3 }
406 :    
407 :    
408 : suxen_drol 136 void MBCoding(const FRAMEINFO * frame,
409 : edgomez 78 MACROBLOCK *pMB,
410 :     int16_t qcoeff[6*64],
411 :     Bitstream * bs,
412 :     Statistics * pStat)
413 : Isibaar 3 {
414 : edgomez 78
415 : Isibaar 3 int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q);
416 :    
417 : suxen_drol 136 if(frame->coding_type == P_VOP) {
418 : Isibaar 3 if(pMB->cbp == 0 && pMB->mode == MODE_INTER &&
419 : edgomez 78 pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)
420 : Isibaar 3 {
421 :     BitstreamPutBit(bs, 1); // not_coded
422 :     return;
423 :     }
424 :     else
425 :     BitstreamPutBit(bs, 0); // coded
426 :     }
427 :    
428 :     if(intra)
429 : suxen_drol 136 CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
430 : Isibaar 3 else
431 : suxen_drol 136 CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
432 : edgomez 78
433 : Isibaar 3 }
434 :    
435 : suxen_drol 118 /***************************************************************
436 :     * bframe encoding start
437 :     ***************************************************************/
438 : Isibaar 3
439 : suxen_drol 118 /*
440 :     mbtype
441 :     0 1b direct(h263) mvdb
442 :     1 01b interpolate mc+q dbquant, mvdf, mvdb
443 :     2 001b backward mc+q dbquant, mvdb
444 :     3 0001b forward mc+q dbquant, mvdf
445 :     */
446 :    
447 :     void put_bvop_mbtype(Bitstream * bs, int value)
448 :     {
449 :     switch(value)
450 :     {
451 :     case 0 : BitstreamPutBit(bs, 1);
452 :     return;
453 :    
454 :     case 1 : BitstreamPutBit(bs, 0);
455 :     BitstreamPutBit(bs, 1);
456 :     return;
457 :    
458 :     case 2 : BitstreamPutBit(bs, 0);
459 :     BitstreamPutBit(bs, 0);
460 :     BitstreamPutBit(bs, 1);
461 :     return;
462 :    
463 :     case 3 : BitstreamPutBit(bs, 0);
464 :     BitstreamPutBit(bs, 0);
465 :     BitstreamPutBit(bs, 0);
466 :     BitstreamPutBit(bs, 1);
467 :     return;
468 :    
469 :     default : ; // invalid!
470 :    
471 :     }
472 :    
473 :     }
474 :    
475 :     /*
476 :     dbquant
477 :     -2 10b
478 :     0 0b
479 :     +2 11b
480 :     */
481 :    
482 :     void put_bvop_dbquant(Bitstream *bs, int value)
483 :     {
484 :     switch (value)
485 :     {
486 :     case 0 : BitstreamPutBit(bs, 0);
487 :     return;
488 :    
489 :     case -2 : BitstreamPutBit(bs, 1);
490 :     BitstreamPutBit(bs, 0);
491 :     return;
492 :    
493 :     case 2 : BitstreamPutBit(bs, 1);
494 :     BitstreamPutBit(bs, 1);
495 :     return;
496 :    
497 :     default : ; // invalid
498 :     }
499 :     }
500 :    
501 :    
502 :    
503 :     void MBCodingBVOP(const MACROBLOCK * mb,
504 :     const int16_t qcoeff[6*64],
505 : suxen_drol 152 const int32_t fcode,
506 :     const int32_t bcode,
507 : suxen_drol 118 Bitstream * bs,
508 :     Statistics * pStat)
509 :     {
510 :     int i;
511 :    
512 :     /* ------------------------------------------------------------------
513 :     when a block is skipped it is decoded DIRECT(0,)
514 :     hence are interpolated from forward & backward frames
515 :     ------------------------------------------------------------------ */
516 :    
517 :     if (mb->mode == 5)
518 :     {
519 :     BitstreamPutBit(bs, 1); // skipped
520 :     return;
521 :     }
522 :    
523 :     BitstreamPutBit(bs, 0); // not skipped
524 :    
525 :     if (mb->cbp == 0)
526 :     {
527 :     BitstreamPutBit(bs, 1); // cbp == 0
528 :     }
529 :     else
530 :     {
531 :     BitstreamPutBit(bs, 0); // cbp == xxx
532 :     }
533 :    
534 :     put_bvop_mbtype(bs, mb->mode);
535 :    
536 :     if (mb->cbp)
537 :     {
538 :     BitstreamPutBits(bs, mb->cbp, 6);
539 :     }
540 :    
541 :     if (mb->mode != MODE_DIRECT && mb->cbp != 0)
542 :     {
543 :     put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0
544 :     }
545 :    
546 :     if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)
547 :     {
548 :     CodeVector(bs, mb->pmvs[0].x, fcode, pStat);
549 :     CodeVector(bs, mb->pmvs[0].y, fcode, pStat);
550 :     }
551 :    
552 :     if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD)
553 :     {
554 :     CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat);
555 :     CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat);
556 :     }
557 :    
558 :     if (mb->mode == MODE_DIRECT)
559 :     {
560 :     // TODO: direct
561 :     }
562 :    
563 :     for (i = 0; i < 6; i++)
564 :     {
565 :     if (mb->cbp & (1 << (5 - i)))
566 :     {
567 :     CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0);
568 :     }
569 :     }
570 :     }
571 :    
572 :    
573 :    
574 : Isibaar 3 /***************************************************************
575 : edgomez 15 * decoding stuff starts here *
576 :     ***************************************************************/
577 : Isibaar 3
578 :     int get_mcbpc_intra(Bitstream * bs)
579 :     {
580 : edgomez 78
581 : Isibaar 3 uint32_t index;
582 :    
583 :     while((index = BitstreamShowBits(bs, 9)) == 1)
584 :     BitstreamSkip(bs, 9);
585 :    
586 :     index >>= 3;
587 :    
588 :     BitstreamSkip(bs, mcbpc_intra_table[index].len);
589 : edgomez 78
590 : Isibaar 3 return mcbpc_intra_table[index].code;
591 : edgomez 78
592 : Isibaar 3 }
593 :    
594 :     int get_mcbpc_inter(Bitstream * bs)
595 :     {
596 : edgomez 78
597 : Isibaar 3 uint32_t index;
598 :    
599 :     while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1)
600 :     BitstreamSkip(bs, 9);
601 :    
602 : edgomez 78 BitstreamSkip(bs, mcbpc_inter_table[index].len);
603 :    
604 : Isibaar 3 return mcbpc_inter_table[index].code;
605 : edgomez 78
606 : Isibaar 3 }
607 :    
608 :     int get_cbpy(Bitstream * bs, int intra)
609 :     {
610 : edgomez 78
611 : Isibaar 3 int cbpy;
612 :     uint32_t index = BitstreamShowBits(bs, 6);
613 :    
614 :     BitstreamSkip(bs, cbpy_table[index].len);
615 :     cbpy = cbpy_table[index].code;
616 :    
617 :     if(!intra)
618 :     cbpy = 15 - cbpy;
619 :    
620 :     return cbpy;
621 : edgomez 78
622 : Isibaar 3 }
623 :    
624 :     int get_mv_data(Bitstream * bs)
625 :     {
626 : edgomez 78
627 : Isibaar 3 uint32_t index;
628 :    
629 :     if(BitstreamGetBit(bs))
630 :     return 0;
631 :    
632 :     index = BitstreamShowBits(bs, 12);
633 :    
634 :     if(index >= 512)
635 :     {
636 :     index = (index >> 8) - 2;
637 :     BitstreamSkip(bs, TMNMVtab0[index].len);
638 :     return TMNMVtab0[index].code;
639 :     }
640 :    
641 :     if(index >= 128)
642 :     {
643 :     index = (index >> 2) - 32;
644 :     BitstreamSkip(bs, TMNMVtab1[index].len);
645 :     return TMNMVtab1[index].code;
646 :     }
647 :    
648 :     index -= 4;
649 :    
650 :     BitstreamSkip(bs, TMNMVtab2[index].len);
651 :     return TMNMVtab2[index].code;
652 : edgomez 78
653 : Isibaar 3 }
654 :    
655 :     int get_mv(Bitstream * bs, int fcode)
656 :     {
657 : edgomez 78
658 : Isibaar 3 int data;
659 :     int res;
660 :     int mv;
661 :     int scale_fac = 1 << (fcode - 1);
662 :    
663 :     data = get_mv_data(bs);
664 :    
665 :     if(scale_fac == 1 || data == 0)
666 :     return data;
667 :    
668 :     res = BitstreamGetBits(bs, fcode - 1);
669 :     mv = ((ABS(data) - 1) * scale_fac) + res + 1;
670 :    
671 :     return data < 0 ? -mv : mv;
672 : edgomez 78
673 : Isibaar 3 }
674 :    
675 :     int get_dc_dif(Bitstream * bs, uint32_t dc_size)
676 :     {
677 : edgomez 78
678 : Isibaar 3 int code = BitstreamGetBits(bs, dc_size);
679 :     int msb = code >> (dc_size - 1);
680 :    
681 :     if(msb == 0)
682 :     return (-1 * (code^((1 << dc_size) - 1)));
683 :    
684 :     return code;
685 : edgomez 78
686 : Isibaar 3 }
687 :    
688 :     int get_dc_size_lum(Bitstream * bs)
689 :     {
690 : edgomez 78
691 : Isibaar 3 int code, i;
692 :     code = BitstreamShowBits(bs, 11);
693 :    
694 :     for(i = 11; i > 3; i--) {
695 :     if(code == 1) {
696 :     BitstreamSkip(bs, i);
697 :     return i + 1;
698 :     }
699 :     code >>= 1;
700 :     }
701 :    
702 :     BitstreamSkip(bs, dc_lum_tab[code].len);
703 :     return dc_lum_tab[code].code;
704 : edgomez 78
705 : Isibaar 3 }
706 :    
707 :    
708 :     int get_dc_size_chrom(Bitstream * bs)
709 :     {
710 : edgomez 78
711 : Isibaar 3 uint32_t code, i;
712 :     code = BitstreamShowBits(bs, 12);
713 :    
714 :     for(i = 12; i > 2; i--) {
715 :     if(code == 1) {
716 :     BitstreamSkip(bs, i);
717 :     return i;
718 :     }
719 :     code >>= 1;
720 :     }
721 :    
722 :     return 3 - BitstreamGetBits(bs, 2);
723 : edgomez 78
724 : Isibaar 3 }
725 :    
726 :     void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff)
727 :     {
728 : edgomez 78
729 : Isibaar 3 const uint16_t * scan = scan_tables[ direction ];
730 :     int level;
731 :     int run;
732 :     int last;
733 :    
734 :     do
735 :     {
736 :     level = get_coeff(bs, &run, &last, 1, 0);
737 :     if (run == -1)
738 :     {
739 :     DEBUG("fatal: invalid run");
740 :     break;
741 :     }
742 :     coeff += run;
743 :     block[ scan[coeff] ] = level;
744 :     if (level < -127 || level > 127)
745 :     {
746 :     DEBUG1("warning: intra_overflow", level);
747 :     }
748 :     coeff++;
749 :     } while (!last);
750 : edgomez 78
751 : Isibaar 3 }
752 :    
753 :     void get_inter_block(Bitstream * bs, int16_t * block)
754 :     {
755 : edgomez 78
756 : Isibaar 3 const uint16_t * scan = scan_tables[0];
757 :     int p;
758 :     int level;
759 :     int run;
760 :     int last;
761 :    
762 :     p = 0;
763 :     do
764 :     {
765 :     level = get_coeff(bs, &run, &last, 0, 0);
766 :     if (run == -1)
767 :     {
768 :     DEBUG("fatal: invalid run");
769 :     break;
770 :     }
771 :     p += run;
772 :     block[ scan[p] ] = level;
773 :     if (level < -127 || level > 127)
774 :     {
775 :     DEBUG1("warning: inter_overflow", level);
776 :     }
777 :     p++;
778 :     } while (!last);
779 : edgomez 78
780 : Isibaar 3 }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4