[svn] / branches / dev-api-4 / xvidcore / src / bitstream / mbcoding.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/src/bitstream/mbcoding.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 152 - (view) (download)
Original Path: trunk/xvidcore/src/bitstream/mbcoding.c

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

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