1 /************************************************************************
2 *
3 * getvlc.c, variable length decoding for tmndecode (H.263 decoder)
4 * Copyright (C) 1995, 1996 Telenor R&D, Norway
5 *
6 * Contacts:
7 * Robert Danielsen <Robert.Danielsen@nta.no>
8 *
9 * Telenor Research and Development http://www.nta.no/brukere/DVC/
10 * P.O.Box 83 tel.: +47 63 84 84 00
11 * N-2007 Kjeller, Norway fax.: +47 63 81 00 76
12 *
13 * Copyright (C) 1997 University of BC, Canada
14 * Modified by: Michael Gallant <mikeg@ee.ubc.ca>
15 * Guy Cote <guyc@ee.ubc.ca>
16 * Berna Erol <bernae@ee.ubc.ca>
17 *
18 * Contacts:
19 * Michael Gallant <mikeg@ee.ubc.ca>
20 *
21 * UBC Image Processing Laboratory http://www.ee.ubc.ca/image
22 * 2356 Main Mall tel.: +1 604 822 4051
23 * Vancouver BC Canada V6T1Z4 fax.: +1 604 822 5949
24 *
25 ************************************************************************/
26
27 /* Disclaimer of Warranty
28 *
29 * These software programs are available to the user without any license fee
30 * or royalty on an "as is" basis. The University of British Columbia
31 * disclaims any and all warranties, whether express, implied, or
32 * statuary, including any implied warranties or merchantability or of
33 * fitness for a particular purpose. In no event shall the
34 * copyright-holder be liable for any incidental, punitive, or
35 * consequential damages of any kind whatsoever arising from the use of
36 * these programs.
37 *
38 * This disclaimer of warranty extends to the user of these programs and
39 * user's customers, employees, agents, transferees, successors, and
40 * assigns.
41 *
42 * The University of British Columbia does not represent or warrant that the
43 * programs furnished hereunder are free of infringement of any
44 * third-party patents.
45 *
46 * Commercial implementations of H.263, including shareware, are subject to
47 * royalty fees to patent holders. Many of these patents are general
48 * enough such that they are unavoidable regardless of implementation
49 * design.
50 *
51 */
52
53
54
55 /* based on mpeg2decode, (C) 1994, MPEG Software Simulation Group and
56 * mpeg2play, (C) 1994 Stefan Eckart <stefan@lis.e-technik.tu-muenchen.de>
57 *
58 */
59
60
61 #include <stdio.h>
62
63 #include "config.h"
64 #include "tmndec.h"
65 #include "global.h"
66 #include "getvlc.h"
67
68 int getTMNMV ()
69 {
70 int code;
71
72 if (trace)
73 fprintf (trace_file, "motion_code (");
74
75 if (getbits1 ())
76 {
77 if (trace)
78 fprintf (trace_file, "1): 0\n");
79 return 0;
80 }
81 if ((code = showbits (12)) >= 512)
82 {
83 code = (code >> 8) - 2;
84 flushbits (TMNMVtab0[code].len);
85
86 if (trace)
87 {
88 fprintf (trace_file, "");
89 printbits (code + 2, 4, TMNMVtab0[code].len);
90 fprintf (trace_file, "): %d\n", TMNMVtab0[code].val);
91 }
92 return TMNMVtab0[code].val;
93 }
94 if (code >= 128)
95 {
96 code = (code >> 2) - 32;
97 flushbits (TMNMVtab1[code].len);
98
99 if (trace)
100 {
101 fprintf (trace_file, "");
102 printbits (code + 32, 10, TMNMVtab1[code].len);
103 fprintf (trace_file, "): %d\n", TMNMVtab1[code].val);
104 }
105 return TMNMVtab1[code].val;
106 }
107 if ((code -= 5) < 0)
108 {
109 if (!quiet)
110 fprintf (stderr, "Invalid motion_vector code\n");
111 fault = 1;
112 return 0;
113 }
114 flushbits (TMNMVtab2[code].len);
115
116 if (trace)
117 {
118 fprintf (trace_file, "");
119 printbits (code + 5, 12, TMNMVtab2[code].len);
120 fprintf (trace_file, "): %d\n", TMNMVtab2[code].val);
121 }
122 return TMNMVtab2[code].val;
123 }
124
125 /**********************************************************************
126 *
127 * Name: getRVLC
128 * Description: extracts RVLC for motion vectors in Annex D
129 *
130 * Input:
131 *
132 * Returns: motion vector component
133 * Side effects:
134 *
135 * Date: 971026 Author: Guy Cote -- guyc@ee.ubc.ca
136 *
137 ***********************************************************************/
138
139 int getRVLC ()
140 {
141
142 int code = 0, sign;
143
144 if (trace)
145 fprintf (trace_file, "motion_code (");
146
147 if (getbits1 ())
148 {
149 if (trace)
150 fprintf (trace_file, "1): 0\n");
151 return 0;
152 }
153 code = 2 + getbits(1);
154 while (getbits(1))
155 {
156 code <<= 1;
157 code += getbits(1);
158 }
159 sign = code & 1;
160 code >>= 1;
161 if (trace)
162 {
163 fprintf (trace_file, "RVLC");
164 fprintf (trace_file, "): %d\n", (sign) ? -code : code);
165 }
166 return (sign) ? -code : code;
167 }
168
169 int getMCBPC ()
170 {
171 int code;
172
173 if (trace)
174 fprintf (trace_file, "MCBPC (");
175
176 code = showbits (13);
177
178 if (code >> 4 == 1)
179 {
180 /* macroblock stuffing */
181 if (trace)
182 fprintf (trace_file, "000000001): stuffing\n");
183 flushbits (9);
184 return 255;
185 }
186 if (code == 0)
187 {
188 if (!quiet)
189 fprintf (stderr, "Invalid MCBPC code\n");
190 fault = 1;
191 return 0;
192 }
193 if (code >= 4096)
194 {
195 flushbits (1);
196 if (trace)
197 fprintf (trace_file, "1): %d\n", 0);
198 return 0;
199 }
200 if (code >= 16)
201 {
202 flushbits (MCBPCtab0[code >> 4].len);
203 if (trace)
204 {
205 printbits (code >> 4, 9, MCBPCtab0[code >> 4].len);
206 fprintf (trace_file, "): %d\n", MCBPCtab0[code >> 4].val);
207 }
208 return MCBPCtab0[code >> 4].val;
209 } else
210 {
211 flushbits (MCBPCtab1[code - 8].len);
212 if (trace)
213 {
214 printbits (code, 13, MCBPCtab1[code - 8].len);
215 fprintf (trace_file, "): %d\n", MCBPCtab1[code - 8].val);
216 }
217 return MCBPCtab1[code - 8].val;
218 }
219 }
220
221 /**********************************************************************
222 *
223 * Name: getMBTYPE
224 * Description: extracts annex O MBTYPE information from bitstream
225 * and whether or not CBP or Quant fields
226 * follow
227 *
228 * Input: pointers for cbp and dquant (to be filled in from
229 * MBTYPE table in annex O).
230 *
231 * Returns:
232 * Side effects:
233 *
234 * Date: 971102 Author: Michael Gallant --- mikeg@ee.ubc.ca
235 *
236 ***********************************************************************/
237 int getMBTYPE (int *cbp_present, int *quant_present)
238 {
239 int code;
240
241 if (trace)
242 fprintf (trace_file, "MBTYPE (");
243
244 code = showbits (9);
245
246 if (code == 1)
247 {
248 /* macroblock stuffing */
249 if (trace)
250 fprintf (trace_file, "000000001): stuffing\n");
251 flushbits (9);
252 *cbp_present = *quant_present = NO;
253 return B_EI_EP_STUFFING;
254
255 }
256
257 switch (pict_type)
258 {
259 case PCT_B:
260
261 if (code < 4)
262 {
263 if (!quiet)
264 fprintf (stderr, "Invalid MBTYPE code\n");
265 fault = 1;
266 *cbp_present = *quant_present = -1;
267 return INVALID_MBTYPE;
268 }
269
270 code >>= 2;
271
272 if (code >= 96)
273 {
274 flushbits (2);
275 if (trace)
276 fprintf (trace_file, "11): %d\n", 0);
277 *cbp_present = YES;
278 *quant_present = NO;
279 return B_DIRECT_PREDICTION;
280 }
281 else
282 {
283 flushbits (MBTYPEtabB[code].len);
284 if (trace)
285 {
286 printbits (code, 7, MBTYPEtabB[code].len);
287 fprintf (trace_file, "): %d\n", MBTYPEtabB[code].val);
288 }
289 *cbp_present = CBP_present_B[MBTYPEtabB[code].val];
290 *quant_present = QUANT_present_B[MBTYPEtabB[code].val];
291 return PRED_type_B[MBTYPEtabB[code].val];
292 }
293
294 break;
295
296 case PCT_EP:
297
298 if (code < 2)
299 {
300 if (!quiet)
301 fprintf (stderr, "Invalid MBTYPE code\n");
302 fault = 1;
303 *cbp_present = *quant_present = -1;
304 return INVALID_MBTYPE;
305 }
306
307 code >>= 1;
308
309 if (code >= 128)
310 {
311 flushbits (1);
312 if (trace)
313 fprintf (trace_file, "1): %d\n", 0);
314 *cbp_present = YES;
315 *quant_present = NO;
316 return EP_FORWARD_PREDICTION;
317 }
318 else
319 {
320 flushbits (MBTYPEtabEP[code].len);
321 if (trace)
322 {
323 printbits (code, 8, MBTYPEtabEP[code].len);
324 fprintf (trace_file, "): %d\n", MBTYPEtabEP[code].val);
325 }
326 *cbp_present = CBP_present_EP[MBTYPEtabEP[code].val];
327 *quant_present = QUANT_present_EP[MBTYPEtabEP[code].val];
328 return PRED_type_EP[MBTYPEtabEP[code].val];
329 }
330
331 break;
332
333 case PCT_EI:
334
335 /* In the case of EI pictures, we use cbp_present to return the chrominance
336 * coded block pattern */
337 if (code < 2)
338 {
339 if (!quiet)
340 fprintf (stderr, "Invalid MBTYPE code\n");
341 fault = 1;
342 *cbp_present = *quant_present = -1;
343 return INVALID_MBTYPE;
344 }
345
346 code >>= 1;
347
348 if (8 == code)
349 {
350 if (!quiet)
351 fprintf (stderr, "Invalid MBTYPE code\n");
352 fault = 1;
353 *cbp_present = *quant_present = -1;
354 return INVALID_MBTYPE;
355 }
356
357 if (code >= 128)
358 {
359 flushbits (1);
360 if (trace)
361 fprintf (trace_file, "1): %d\n", 0);
362 *cbp_present = 0;
363 *quant_present = NO;
364 return EI_EP_UPWARD_PREDICTION;
365 }
366 else
367 {
368 flushbits (MBTYPEtabEI[code].len);
369 if (trace)
370 {
371 printbits (code, 8, MBTYPEtabEI[code].len);
372 fprintf (trace_file, "): %d\n", MBTYPEtabEI[code].val);
373 }
374 *cbp_present = CBPC_pattern_EI[MBTYPEtabEI[code].val];
375 *quant_present = QUANT_present_EI[MBTYPEtabEI[code].val];
376 return PRED_type_EI[MBTYPEtabEI[code].val];
377 }
378
379 break;
380
381 default:
382
383 break;
384
385 }
386 }
387
388 int getMODB ()
389 {
390 int code;
391 int MODB;
392
393 if (trace)
394 fprintf (trace_file, "MODB (");
395
396 if (pict_type == PCT_IPB)
397 {
398 code = getbits (1);
399 if (code == 0)
400 {
401 MODB = PBMODE_BIDIR_PRED;
402 if (trace)
403 fprintf (trace_file, "0): 0\n");
404 }
405 else
406 {
407 code=getbits (1);
408 if (code==0)
409 {
410 MODB = PBMODE_CBPB_BIDIR_PRED;
411 if (trace)
412 fprintf (trace_file, "10): 2\n");
413 }
414 else
415 {
416 code=getbits (1);
417 if (code==0)
418 {
419 MODB = PBMODE_FRW_PRED;
420 if (trace)
421 fprintf (trace_file, "110): 6\n");
422 }
423 else
424 {
425 code=getbits (1);
426 if (code==0)
427 {
428 MODB = PBMODE_CBPB_FRW_PRED;
429 if (trace)
430 fprintf (trace_file, "1110): 14\n");
431 }
432 else
433 {
434 code=getbits (1);
435 if (code==0)
436 {
437 MODB = PBMODE_BCKW_PRED;
438 if (trace)
439 fprintf (trace_file, "11110): 30\n");
440 }
441 else
442 {
443 MODB = PBMODE_CBPB_BCKW_PRED;
444 if (trace)
445 fprintf (trace_file, "11111): 31\n");
446 }
447 }
448 }
449 }
450 }
451 } else
452 {
453 code = showbits (2);
454
455 if (code < 2)
456 {
457 if (trace)
458 fprintf (trace_file, "0): 0\n");
459 MODB = 0;
460 flushbits (1);
461 } else if (code == 2)
462 {
463 if (trace)
464 fprintf (trace_file, "10): 1\n");
465 MODB = 1;
466 flushbits (2);
467 } else
468 {
469 /* code == 3 */
470 if (trace)
471 fprintf (trace_file, "11): 2\n");
472 MODB = 2;
473 flushbits (2);
474 }
475 }
476
477 return MODB;
478
479 }
480
481
482 int getMCBPCintra ()
483 {
484 int code;
485
486 if (trace)
487 fprintf (trace_file, "MCBPCintra (");
488
489 code = showbits (9);
490
491 if (code == 1)
492 {
493 /* macroblock stuffing */
494 if (trace)
495 fprintf (trace_file, "000000001): stuffing\n");
496 flushbits (9);
497 return 255;
498
499 }
500 if (code < 8)
501 {
502 if (!quiet)
503 fprintf (stderr, "Invalid MCBPCintra code\n");
504 fault = 1;
505 return 0;
506 }
507 code >>= 3;
508
509 if (code >= 32)
510 {
511 flushbits (1);
512 if (trace)
513 fprintf (trace_file, "1): %d\n", 3);
514 return 3;
515 }
516 flushbits (MCBPCtabintra[code].len);
517
518 if (trace)
519 {
520 printbits (code, 6, MCBPCtabintra[code].len);
521 fprintf (trace_file, "): %d\n", MCBPCtabintra[code].val);
522 }
523 return MCBPCtabintra[code].val;
524 }
525
526 /* extract vlc representing true B CBPC --- mikeg@ee.ubc.ca */
527 int getscalabilityCBPC ()
528 {
529 int code;
530
531 if (trace)
532 fprintf (trace_file, "CBPC: ");
533
534 code = showbits (1);
535
536 if (0 == code)
537 {
538 if (trace)
539 fprintf (trace_file, "0 - Blocks 56: 00\n");
540 flushbits (1);
541 return 0;
542 } else
543 {
544 code = showbits (2);
545
546 if (2 == code)
547 {
548 if (trace)
549 fprintf (trace_file, "10 - Blocks 56: 01\n");
550 flushbits (2);
551 return 1;
552 } else
553 {
554 code = showbits (3);
555
556 if (6 == code)
557 {
558 if (trace)
559 fprintf (trace_file, "110 - Blocks 56: 11\n");
560 flushbits (3);
561 return 3;
562 } else if (7 == code)
563 {
564 if (trace)
565 fprintf (trace_file, "111 - Blocks 56: 10\n");
566 flushbits (3);
567 return 2;
568 } else
569 {
570 if (!quiet)
571 printf ("Invalid chromiance CBP in getscalabilityCBPC (getvlc.c).\n");
572 fault = 1;
573 return 0;
574 }
575 }
576 }
577 }
578
579 int getCBPY ()
580 {
581 int code;
582
583 if (trace)
584 fprintf (trace_file, "CBPY (");
585
586 code = showbits (6);
587 if (code < 2)
588 {
589 if (!quiet)
590 fprintf (stderr, "Invalid CBPY code\n");
591 fault = 1;
592 return -1;
593 }
594 if (code >= 48)
595 {
596 flushbits (2);
597 if (trace)
598 fprintf (trace_file, "11): %d\n", 0);
599 return 0;
600 }
601 flushbits (CBPYtab[code].len);
602
603 if (trace)
604 {
605 printbits (code, 6, CBPYtab[code].len);
606 fprintf (trace_file, "): %d\n", CBPYtab[code].val);
607 }
608 return CBPYtab[code].val;
609 }
610
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.