[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.25.2.2 - (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 : chl 1.25.2.1 static __inline void
284 : edgomez 1.17 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 : h 1.25.2.2 const uint16_t *scan_table =
334 :     frame->global_flags & XVID_ALTERNATESCAN ?
335 :     scan_tables[2] : scan_tables[pMB->acpred_directions[i]];
336 :    
337 : Isibaar 1.1 bits = BitstreamPos(bs);
338 :    
339 : h 1.25.2.2 CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1);
340 : Isibaar 1.1
341 :     bits = BitstreamPos(bs) - bits;
342 :     pStat->iTextBits += bits;
343 :     }
344 :     }
345 : edgomez 1.7
346 : Isibaar 1.1 }
347 :    
348 :    
349 : edgomez 1.17 static void
350 :     CodeBlockInter(const FRAMEINFO * frame,
351 :     const MACROBLOCK * pMB,
352 :     int16_t qcoeff[6 * 64],
353 : edgomez 1.7 Bitstream * bs,
354 :     Statistics * pStat)
355 : Isibaar 1.1 {
356 : edgomez 1.7
357 : Isibaar 1.1 int32_t i;
358 :     uint32_t bits, mcbpc, cbpy;
359 :    
360 : edgomez 1.7 mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3);
361 : Isibaar 1.1 cbpy = 15 - (pMB->cbp >> 2);
362 :    
363 :     // write mcbpc
364 : edgomez 1.17 BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code,
365 :     mcbpc_inter_tab[mcbpc].len);
366 : Isibaar 1.1
367 :     // write cbpy
368 :     BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len);
369 :    
370 :     // write dquant
371 : edgomez 1.17 if (pMB->mode == MODE_INTER_Q)
372 : Isibaar 1.1 BitstreamPutBits(bs, pMB->dquant, 2);
373 : edgomez 1.17
374 : h 1.6 // interlacing
375 : edgomez 1.17 if (frame->global_flags & XVID_INTERLACING) {
376 : h 1.25 if (pMB->cbp) {
377 :     BitstreamPutBit(bs, pMB->field_dct);
378 :     DEBUG1("codep: field_dct: ", pMB->field_dct);
379 :     }
380 : h 1.6
381 :     // if inter block, write field ME flag
382 : edgomez 1.17 if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
383 : h 1.6 BitstreamPutBit(bs, pMB->field_pred);
384 :     DEBUG1("codep: field_pred: ", pMB->field_pred);
385 :    
386 :     // write field prediction references
387 : edgomez 1.17 if (pMB->field_pred) {
388 : h 1.6 BitstreamPutBit(bs, pMB->field_for_top);
389 :     BitstreamPutBit(bs, pMB->field_for_bot);
390 :     }
391 :     }
392 :     }
393 : Isibaar 1.1 // code motion vector(s)
394 : edgomez 1.17 for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) {
395 : suxen_drol 1.13 CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat);
396 :     CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat);
397 : Isibaar 1.1 }
398 :    
399 :     bits = BitstreamPos(bs);
400 : edgomez 1.17
401 : Isibaar 1.1 // code block coeffs
402 : edgomez 1.17 for (i = 0; i < 6; i++)
403 :     if (pMB->cbp & (1 << (5 - i)))
404 : h 1.25.2.2 {
405 :     const uint16_t *scan_table =
406 :     frame->global_flags & XVID_ALTERNATESCAN ?
407 :     scan_tables[2] : scan_tables[0];
408 :    
409 :     CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0);
410 :     }
411 : Isibaar 1.1
412 :     bits = BitstreamPos(bs) - bits;
413 :     pStat->iTextBits += bits;
414 : edgomez 1.7
415 : Isibaar 1.1 }
416 :    
417 :    
418 : edgomez 1.17 void
419 :     MBCoding(const FRAMEINFO * frame,
420 :     MACROBLOCK * pMB,
421 :     int16_t qcoeff[6 * 64],
422 :     Bitstream * bs,
423 :     Statistics * pStat)
424 : Isibaar 1.1 {
425 : edgomez 1.7
426 : edgomez 1.17 if (frame->coding_type == P_VOP) {
427 :     BitstreamPutBit(bs, 0); // coded
428 : Isibaar 1.1 }
429 :    
430 : chl 1.22 if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)
431 : suxen_drol 1.13 CodeBlockIntra(frame, pMB, qcoeff, bs, pStat);
432 : Isibaar 1.1 else
433 : suxen_drol 1.13 CodeBlockInter(frame, pMB, qcoeff, bs, pStat);
434 : edgomez 1.7
435 : Isibaar 1.1 }
436 : chl 1.24
437 : chl 1.25.2.1 /*
438 :     // moved to mbcoding.h so that in can be 'static __inline'
439 : chl 1.24 void
440 :     MBSkip(Bitstream * bs)
441 :     {
442 :     BitstreamPutBit(bs, 1); // not coded
443 :     }
444 : chl 1.25.2.1 */
445 : suxen_drol 1.11
446 :     /***************************************************************
447 :     * bframe encoding start
448 :     ***************************************************************/
449 :    
450 :     /*
451 :     mbtype
452 :     0 1b direct(h263) mvdb
453 :     1 01b interpolate mc+q dbquant, mvdf, mvdb
454 :     2 001b backward mc+q dbquant, mvdb
455 :     3 0001b forward mc+q dbquant, mvdf
456 :     */
457 :    
458 : chl 1.25.2.1 static __inline void
459 : edgomez 1.17 put_bvop_mbtype(Bitstream * bs,
460 :     int value)
461 :     {
462 :     switch (value) {
463 : chl 1.25.2.1 case MODE_FORWARD:
464 :     BitstreamPutBit(bs, 0);
465 :     case MODE_BACKWARD:
466 :     BitstreamPutBit(bs, 0);
467 :     case MODE_INTERPOLATE:
468 :     BitstreamPutBit(bs, 0);
469 :     case MODE_DIRECT:
470 :     BitstreamPutBit(bs, 1);
471 :     default:
472 :     break;
473 : suxen_drol 1.11 }
474 :     }
475 :    
476 :     /*
477 :     dbquant
478 :     -2 10b
479 :     0 0b
480 :     +2 11b
481 :     */
482 :    
483 : chl 1.25.2.1 static __inline void
484 : edgomez 1.17 put_bvop_dbquant(Bitstream * bs,
485 :     int value)
486 :     {
487 :     switch (value) {
488 :     case 0:
489 :     BitstreamPutBit(bs, 0);
490 :     return;
491 :    
492 :     case -2:
493 :     BitstreamPutBit(bs, 1);
494 :     BitstreamPutBit(bs, 0);
495 :     return;
496 : suxen_drol 1.11
497 : edgomez 1.17 case 2:
498 :     BitstreamPutBit(bs, 1);
499 :     BitstreamPutBit(bs, 1);
500 :     return;
501 : suxen_drol 1.11
502 : edgomez 1.17 default:; // invalid
503 : suxen_drol 1.11 }
504 :     }
505 :    
506 :    
507 :    
508 : edgomez 1.17 void
509 :     MBCodingBVOP(const MACROBLOCK * mb,
510 :     const int16_t qcoeff[6 * 64],
511 :     const int32_t fcode,
512 :     const int32_t bcode,
513 :     Bitstream * bs,
514 : h 1.25.2.2 Statistics * pStat,
515 :     int direction)
516 : suxen_drol 1.11 {
517 : chl 1.25.2.1 int vcode = fcode;
518 :     unsigned int i;
519 : suxen_drol 1.11
520 :     /* ------------------------------------------------------------------
521 : chl 1.21 when a block is skipped it is decoded DIRECT(0,0)
522 :     hence is interpolated from forward & backward frames
523 : suxen_drol 1.11 ------------------------------------------------------------------ */
524 :    
525 : chl 1.21 if (mb->mode == MODE_DIRECT_NONE_MV) {
526 : edgomez 1.17 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 : chl 1.25.2.1 switch (mb->mode) {
549 :     case MODE_INTERPOLATE:
550 :     CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode
551 :     CodeVector(bs, mb->pmvs[1].y, vcode, pStat);
552 :     case MODE_BACKWARD:
553 :     vcode = bcode;
554 :     case MODE_FORWARD:
555 :     CodeVector(bs, mb->pmvs[0].x, vcode, pStat);
556 :     CodeVector(bs, mb->pmvs[0].y, vcode, pStat);
557 :     break;
558 :     case MODE_DIRECT:
559 :     CodeVector(bs, mb->pmvs[3].x, 1, pStat); // fcode is always 1 for delta vector
560 :     CodeVector(bs, mb->pmvs[3].y, 1, pStat); // prediction is always (0,0)
561 :     default: break;
562 : suxen_drol 1.11 }
563 : edgomez 1.17
564 :     for (i = 0; i < 6; i++) {
565 :     if (mb->cbp & (1 << (5 - i))) {
566 : h 1.25.2.2 CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0);
567 : suxen_drol 1.11 }
568 : edgomez 1.17 }
569 : suxen_drol 1.11 }
570 :    
571 : Isibaar 1.1
572 :    
573 :     /***************************************************************
574 : edgomez 1.2 * decoding stuff starts here *
575 :     ***************************************************************/
576 : Isibaar 1.1
577 : suxen_drol 1.19
578 :     // for IVOP addbits == 0
579 :     // for PVOP addbits == fcode - 1
580 :     // for BVOP addbits == max(fcode,bcode) - 1
581 :     // returns true or false
582 :     int
583 :     check_resync_marker(Bitstream * bs, int addbits)
584 :     {
585 :     uint32_t nbits;
586 :     uint32_t code;
587 :     uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits;
588 :    
589 :     nbits = BitstreamNumBitsToByteAlign(bs);
590 :     code = BitstreamShowBits(bs, nbits);
591 :    
592 :     if (code == (((uint32_t)1 << (nbits - 1)) - 1))
593 :     {
594 :     return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER;
595 :     }
596 :    
597 :     return 0;
598 :     }
599 :    
600 :    
601 :    
602 : edgomez 1.17 int
603 :     get_mcbpc_intra(Bitstream * bs)
604 : Isibaar 1.1 {
605 : edgomez 1.7
606 : Isibaar 1.1 uint32_t index;
607 : edgomez 1.17
608 : suxen_drol 1.19 index = BitstreamShowBits(bs, 9);
609 : Isibaar 1.1 index >>= 3;
610 :    
611 :     BitstreamSkip(bs, mcbpc_intra_table[index].len);
612 : edgomez 1.7
613 : Isibaar 1.1 return mcbpc_intra_table[index].code;
614 : edgomez 1.7
615 : Isibaar 1.1 }
616 :    
617 : edgomez 1.17 int
618 :     get_mcbpc_inter(Bitstream * bs)
619 : Isibaar 1.1 {
620 : edgomez 1.7
621 : Isibaar 1.1 uint32_t index;
622 : suxen_drol 1.19
623 :     index = CLIP(BitstreamShowBits(bs, 9), 256);
624 : Isibaar 1.1
625 : edgomez 1.17 BitstreamSkip(bs, mcbpc_inter_table[index].len);
626 : edgomez 1.7
627 : Isibaar 1.1 return mcbpc_inter_table[index].code;
628 : edgomez 1.7
629 : Isibaar 1.1 }
630 :    
631 : edgomez 1.17 int
632 :     get_cbpy(Bitstream * bs,
633 :     int intra)
634 : Isibaar 1.1 {
635 : edgomez 1.7
636 : Isibaar 1.1 int cbpy;
637 :     uint32_t index = BitstreamShowBits(bs, 6);
638 :    
639 :     BitstreamSkip(bs, cbpy_table[index].len);
640 :     cbpy = cbpy_table[index].code;
641 :    
642 : edgomez 1.17 if (!intra)
643 : Isibaar 1.1 cbpy = 15 - cbpy;
644 :    
645 :     return cbpy;
646 : edgomez 1.7
647 : Isibaar 1.1 }
648 :    
649 : chl 1.25.2.1 static __inline int
650 : edgomez 1.17 get_mv_data(Bitstream * bs)
651 : Isibaar 1.1 {
652 : edgomez 1.7
653 : Isibaar 1.1 uint32_t index;
654 :    
655 : edgomez 1.17 if (BitstreamGetBit(bs))
656 : Isibaar 1.1 return 0;
657 : edgomez 1.17
658 : Isibaar 1.1 index = BitstreamShowBits(bs, 12);
659 :    
660 : edgomez 1.17 if (index >= 512) {
661 : Isibaar 1.1 index = (index >> 8) - 2;
662 :     BitstreamSkip(bs, TMNMVtab0[index].len);
663 :     return TMNMVtab0[index].code;
664 :     }
665 : edgomez 1.17
666 :     if (index >= 128) {
667 : Isibaar 1.1 index = (index >> 2) - 32;
668 :     BitstreamSkip(bs, TMNMVtab1[index].len);
669 :     return TMNMVtab1[index].code;
670 :     }
671 :    
672 : edgomez 1.17 index -= 4;
673 : Isibaar 1.1
674 :     BitstreamSkip(bs, TMNMVtab2[index].len);
675 :     return TMNMVtab2[index].code;
676 : edgomez 1.7
677 : Isibaar 1.1 }
678 :    
679 : edgomez 1.17 int
680 :     get_mv(Bitstream * bs,
681 :     int fcode)
682 : Isibaar 1.1 {
683 : edgomez 1.7
684 : Isibaar 1.1 int data;
685 :     int res;
686 :     int mv;
687 :     int scale_fac = 1 << (fcode - 1);
688 :    
689 :     data = get_mv_data(bs);
690 : edgomez 1.17
691 :     if (scale_fac == 1 || data == 0)
692 : Isibaar 1.1 return data;
693 :    
694 :     res = BitstreamGetBits(bs, fcode - 1);
695 :     mv = ((ABS(data) - 1) * scale_fac) + res + 1;
696 : edgomez 1.17
697 : Isibaar 1.1 return data < 0 ? -mv : mv;
698 : edgomez 1.7
699 : Isibaar 1.1 }
700 :    
701 : edgomez 1.17 int
702 :     get_dc_dif(Bitstream * bs,
703 :     uint32_t dc_size)
704 : Isibaar 1.1 {
705 : edgomez 1.7
706 : Isibaar 1.1 int code = BitstreamGetBits(bs, dc_size);
707 :     int msb = code >> (dc_size - 1);
708 :    
709 : edgomez 1.17 if (msb == 0)
710 :     return (-1 * (code ^ ((1 << dc_size) - 1)));
711 : Isibaar 1.1
712 :     return code;
713 : edgomez 1.7
714 : Isibaar 1.1 }
715 :    
716 : edgomez 1.17 int
717 :     get_dc_size_lum(Bitstream * bs)
718 : Isibaar 1.1 {
719 : edgomez 1.7
720 : Isibaar 1.1 int code, i;
721 : edgomez 1.17
722 : Isibaar 1.1 code = BitstreamShowBits(bs, 11);
723 :    
724 : edgomez 1.17 for (i = 11; i > 3; i--) {
725 :     if (code == 1) {
726 : Isibaar 1.1 BitstreamSkip(bs, i);
727 :     return i + 1;
728 :     }
729 :     code >>= 1;
730 :     }
731 :    
732 :     BitstreamSkip(bs, dc_lum_tab[code].len);
733 :     return dc_lum_tab[code].code;
734 : edgomez 1.7
735 : Isibaar 1.1 }
736 :    
737 :    
738 : edgomez 1.17 int
739 :     get_dc_size_chrom(Bitstream * bs)
740 : Isibaar 1.1 {
741 : edgomez 1.7
742 : Isibaar 1.1 uint32_t code, i;
743 : edgomez 1.17
744 : Isibaar 1.1 code = BitstreamShowBits(bs, 12);
745 :    
746 : edgomez 1.17 for (i = 12; i > 2; i--) {
747 :     if (code == 1) {
748 : Isibaar 1.1 BitstreamSkip(bs, i);
749 :     return i;
750 :     }
751 :     code >>= 1;
752 :     }
753 :    
754 :     return 3 - BitstreamGetBits(bs, 2);
755 : edgomez 1.7
756 : Isibaar 1.1 }
757 :    
758 : edgomez 1.17 void
759 :     get_intra_block(Bitstream * bs,
760 :     int16_t * block,
761 :     int direction,
762 :     int coeff)
763 : Isibaar 1.1 {
764 : edgomez 1.7
765 : edgomez 1.17 const uint16_t *scan = scan_tables[direction];
766 : chl 1.25.2.1 int level, run, last;
767 : Isibaar 1.1
768 : edgomez 1.17 do {
769 : Isibaar 1.1 level = get_coeff(bs, &run, &last, 1, 0);
770 : edgomez 1.17 if (run == -1) {
771 : Isibaar 1.1 DEBUG("fatal: invalid run");
772 :     break;
773 :     }
774 :     coeff += run;
775 : edgomez 1.17 block[scan[coeff]] = level;
776 : suxen_drol 1.20
777 :     DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level);
778 :     //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32));
779 :    
780 : edgomez 1.17 if (level < -127 || level > 127) {
781 : Isibaar 1.1 DEBUG1("warning: intra_overflow", level);
782 :     }
783 :     coeff++;
784 :     } while (!last);
785 : edgomez 1.7
786 : Isibaar 1.1 }
787 :    
788 : edgomez 1.17 void
789 :     get_inter_block(Bitstream * bs,
790 : h 1.25.2.2 int16_t * block,
791 :     int direction)
792 : Isibaar 1.1 {
793 : edgomez 1.7
794 : h 1.25.2.2 const uint16_t *scan = scan_tables[direction];
795 : Isibaar 1.1 int p;
796 :     int level;
797 :     int run;
798 :     int last;
799 :    
800 :     p = 0;
801 : edgomez 1.17 do {
802 : Isibaar 1.1 level = get_coeff(bs, &run, &last, 0, 0);
803 : edgomez 1.17 if (run == -1) {
804 : Isibaar 1.1 DEBUG("fatal: invalid run");
805 :     break;
806 :     }
807 :     p += run;
808 : chenm001 1.16
809 : edgomez 1.17 block[scan[p]] = level;
810 : suxen_drol 1.20
811 :     DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level);
812 :     // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32));
813 :    
814 : edgomez 1.17 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