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

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