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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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