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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (view) (download)

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

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