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

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