fbui | Framebuffer-based graphical environment |
| download: https://git.y1.nz/archives/fbui.tar.gz | |
| README | Files | Log | Refs |
libfbui/MPEG/getvlc.c
1 /* getvlc.c, variable length decoding */
2
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
4
5 /*
6 * Disclaimer of Warranty
7 *
8 * These software programs are available to the user without any license fee or
9 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
10 * any and all warranties, whether express, implied, or statuary, including any
11 * implied warranties or merchantability or of fitness for a particular
12 * purpose. In no event shall the copyright-holder be liable for any
13 * incidental, punitive, or consequential damages of any kind whatsoever
14 * arising from the use of these programs.
15 *
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
18 *
19 * The MPEG Software Simulation Group does not represent or warrant that the
20 * programs furnished hereunder are free of infringement of any third-party
21 * patents.
22 *
23 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24 * are subject to royalty fees to patent holders. Many of these patents are
25 * general enough such that they are unavoidable regardless of implementation
26 * design.
27 *
28 */
29
30 #include <stdio.h>
31
32 #include "config.h"
33 #include "global.h"
34 #include "getvlc.h"
35
36 /* private prototypes */
37 /* generic picture macroblock type processing functions */
38 static int Get_I_macroblock_type _ANSI_ARGS_((void));
39 static int Get_P_macroblock_type _ANSI_ARGS_((void));
40 static int Get_B_macroblock_type _ANSI_ARGS_((void));
41 static int Get_D_macroblock_type _ANSI_ARGS_((void));
42
43 /* spatial picture macroblock type processing functions */
44 static int Get_I_Spatial_macroblock_type _ANSI_ARGS_((void));
45 static int Get_P_Spatial_macroblock_type _ANSI_ARGS_((void));
46 static int Get_B_Spatial_macroblock_type _ANSI_ARGS_((void));
47 static int Get_SNR_macroblock_type _ANSI_ARGS_((void));
48
49 int Get_macroblock_type()
50 {
51 int macroblock_type = 0;
52
53 if (ld->scalable_mode==SC_SNR)
54 macroblock_type = Get_SNR_macroblock_type();
55 else
56 {
57 switch (picture_coding_type)
58 {
59 case I_TYPE:
60 macroblock_type = ld->pict_scal ? Get_I_Spatial_macroblock_type() : Get_I_macroblock_type();
61 break;
62 case P_TYPE:
63 macroblock_type = ld->pict_scal ? Get_P_Spatial_macroblock_type() : Get_P_macroblock_type();
64 break;
65 case B_TYPE:
66 macroblock_type = ld->pict_scal ? Get_B_Spatial_macroblock_type() : Get_B_macroblock_type();
67 break;
68 case D_TYPE:
69 macroblock_type = Get_D_macroblock_type();
70 break;
71 default:
72 printf("Get_macroblock_type(): unrecognized picture coding type\n");
73 break;
74 }
75 }
76
77 return macroblock_type;
78 }
79
80 static int Get_I_macroblock_type()
81 {
82 #ifdef TRACE
83 if (Trace_Flag)
84 printf("macroblock_type(I) ");
85 #endif /* TRACE */
86
87 if (Get_Bits1())
88 {
89 #ifdef TRACE
90 if (Trace_Flag)
91 printf("(1): Intra (1)\n");
92 #endif /* TRACE */
93 return 1;
94 }
95
96 if (!Get_Bits1())
97 {
98 if (!Quiet_Flag)
99 printf("Invalid macroblock_type code\n");
100 Fault_Flag = 1;
101 }
102
103 #ifdef TRACE
104 if (Trace_Flag)
105 printf("(01): Intra, Quant (17)\n");
106 #endif /* TRACE */
107
108 return 17;
109 }
110
111 static char *MBdescr[]={
112 "", "Intra", "No MC, Coded", "",
113 "Bwd, Not Coded", "", "Bwd, Coded", "",
114 "Fwd, Not Coded", "", "Fwd, Coded", "",
115 "Interp, Not Coded", "", "Interp, Coded", "",
116 "", "Intra, Quant", "No MC, Coded, Quant", "",
117 "", "", "Bwd, Coded, Quant", "",
118 "", "", "Fwd, Coded, Quant", "",
119 "", "", "Interp, Coded, Quant", ""
120 };
121
122 static int Get_P_macroblock_type()
123 {
124 int code;
125
126 #ifdef TRACE
127 if (Trace_Flag)
128 printf("macroblock_type(P) (");
129 #endif /* TRACE */
130
131 if ((code = Show_Bits(6))>=8)
132 {
133 code >>= 3;
134 Flush_Buffer(PMBtab0[code].len);
135 #ifdef TRACE
136 if (Trace_Flag)
137 {
138 Print_Bits(code,3,PMBtab0[code].len);
139 printf("): %s (%d)\n",MBdescr[(int)PMBtab0[code].val],PMBtab0[code].val);
140 }
141 #endif /* TRACE */
142 return PMBtab0[code].val;
143 }
144
145 if (code==0)
146 {
147 if (!Quiet_Flag)
148 printf("Invalid macroblock_type code\n");
149 Fault_Flag = 1;
150 return 0;
151 }
152
153 Flush_Buffer(PMBtab1[code].len);
154
155 #ifdef TRACE
156 if (Trace_Flag)
157 {
158 Print_Bits(code,6,PMBtab1[code].len);
159 printf("): %s (%d)\n",MBdescr[(int)PMBtab1[code].val],PMBtab1[code].val);
160 }
161 #endif /* TRACE */
162
163 return PMBtab1[code].val;
164 }
165
166 static int Get_B_macroblock_type()
167 {
168 int code;
169
170 #ifdef TRACE
171 if (Trace_Flag)
172 printf("macroblock_type(B) (");
173 #endif /* TRACE */
174
175 if ((code = Show_Bits(6))>=8)
176 {
177 code >>= 2;
178 Flush_Buffer(BMBtab0[code].len);
179
180 #ifdef TRACE
181 if (Trace_Flag)
182 {
183 Print_Bits(code,4,BMBtab0[code].len);
184 printf("): %s (%d)\n",MBdescr[(int)BMBtab0[code].val],BMBtab0[code].val);
185 }
186 #endif /* TRACE */
187
188 return BMBtab0[code].val;
189 }
190
191 if (code==0)
192 {
193 if (!Quiet_Flag)
194 printf("Invalid macroblock_type code\n");
195 Fault_Flag = 1;
196 return 0;
197 }
198
199 Flush_Buffer(BMBtab1[code].len);
200
201 #ifdef TRACE
202 if (Trace_Flag)
203 {
204 Print_Bits(code,6,BMBtab1[code].len);
205 printf("): %s (%d)\n",MBdescr[(int)BMBtab1[code].val],BMBtab1[code].val);
206 }
207 #endif /* TRACE */
208
209 return BMBtab1[code].val;
210 }
211
212 static int Get_D_macroblock_type()
213 {
214 if (!Get_Bits1())
215 {
216 if (!Quiet_Flag)
217 printf("Invalid macroblock_type code\n");
218 Fault_Flag=1;
219 }
220
221 return 1;
222 }
223
224 /* macroblock_type for pictures with spatial scalability */
225 static int Get_I_Spatial_macroblock_type()
226 {
227 int code;
228
229 #ifdef TRACE
230 if (Trace_Flag)
231 printf("macroblock_type(I,spat) (");
232 #endif /* TRACE */
233
234 code = Show_Bits(4);
235
236 if (code==0)
237 {
238 if (!Quiet_Flag)
239 printf("Invalid macroblock_type code\n");
240 Fault_Flag = 1;
241 return 0;
242 }
243
244 #ifdef TRACE
245 if (Trace_Flag)
246 {
247 Print_Bits(code,4,spIMBtab[code].len);
248 printf("): %02x\n",spIMBtab[code].val);
249 }
250 #endif /* TRACE */
251
252 Flush_Buffer(spIMBtab[code].len);
253 return spIMBtab[code].val;
254 }
255
256 static int Get_P_Spatial_macroblock_type()
257 {
258 int code;
259
260 #ifdef TRACE
261 if (Trace_Flag)
262 printf("macroblock_type(P,spat) (");
263 #endif /* TRACE */
264
265 code = Show_Bits(7);
266
267 if (code<2)
268 {
269 if (!Quiet_Flag)
270 printf("Invalid macroblock_type code\n");
271 Fault_Flag = 1;
272 return 0;
273 }
274
275 if (code>=16)
276 {
277 code >>= 3;
278 Flush_Buffer(spPMBtab0[code].len);
279
280 #ifdef TRACE
281 if (Trace_Flag)
282 {
283 Print_Bits(code,4,spPMBtab0[code].len);
284 printf("): %02x\n",spPMBtab0[code].val);
285 }
286 #endif /* TRACE */
287
288 return spPMBtab0[code].val;
289 }
290
291 Flush_Buffer(spPMBtab1[code].len);
292
293 #ifdef TRACE
294 if (Trace_Flag)
295 {
296 Print_Bits(code,7,spPMBtab1[code].len);
297 printf("): %02x\n",spPMBtab1[code].val);
298 }
299 #endif /* TRACE */
300
301 return spPMBtab1[code].val;
302 }
303
304 static int Get_B_Spatial_macroblock_type()
305 {
306 int code;
307 VLCtab *p;
308
309 #ifdef TRACE
310 if (Trace_Flag)
311 printf("macroblock_type(B,spat) (");
312 #endif /* TRACE */
313
314 code = Show_Bits(9);
315
316 if (code>=64)
317 p = &spBMBtab0[(code>>5)-2];
318 else if (code>=16)
319 p = &spBMBtab1[(code>>2)-4];
320 else if (code>=8)
321 p = &spBMBtab2[code-8];
322 else
323 {
324 if (!Quiet_Flag)
325 printf("Invalid macroblock_type code\n");
326 Fault_Flag = 1;
327 return 0;
328 }
329
330 Flush_Buffer(p->len);
331
332 #ifdef TRACE
333 if (Trace_Flag)
334 {
335 Print_Bits(code,9,p->len);
336 printf("): %02x\n",p->val);
337 }
338 #endif /* TRACE */
339
340 return p->val;
341 }
342
343 static int Get_SNR_macroblock_type()
344 {
345 int code;
346
347 #ifdef TRACE /* *CH* */
348 if (Trace_Flag)
349 printf("macroblock_type(SNR) (");
350 #endif TRACE
351
352 code = Show_Bits(3);
353
354 if (code==0)
355 {
356 if (!Quiet_Flag)
357 printf("Invalid macroblock_type code\n");
358 Fault_Flag = 1;
359 return 0;
360 }
361
362 Flush_Buffer(SNRMBtab[code].len);
363
364 #ifdef TRACE /* *CH* */
365 if (Trace_Flag)
366 {
367 Print_Bits(code,3,SNRMBtab[code].len);
368 printf("): %s (%d)\n",MBdescr[(int)SNRMBtab[code].val],SNRMBtab[code].val);
369 }
370 #endif TRACE
371
372
373 return SNRMBtab[code].val;
374 }
375
376 int Get_motion_code()
377 {
378 int code;
379
380 #ifdef TRACE
381 if (Trace_Flag)
382 printf("motion_code (");
383 #endif /* TRACE */
384
385 if (Get_Bits1())
386 {
387 #ifdef TRACE
388 if (Trace_Flag)
389 printf("0): 0\n");
390 #endif /* TRACE */
391 return 0;
392 }
393
394 if ((code = Show_Bits(9))>=64)
395 {
396 code >>= 6;
397 Flush_Buffer(MVtab0[code].len);
398
399 #ifdef TRACE
400 if (Trace_Flag)
401 {
402 Print_Bits(code,3,MVtab0[code].len);
403 printf("%d): %d\n",
404 Show_Bits(1),Show_Bits(1)?-MVtab0[code].val:MVtab0[code].val);
405 }
406 #endif /* TRACE */
407
408 return Get_Bits1()?-MVtab0[code].val:MVtab0[code].val;
409 }
410
411 if (code>=24)
412 {
413 code >>= 3;
414 Flush_Buffer(MVtab1[code].len);
415
416 #ifdef TRACE
417 if (Trace_Flag)
418 {
419 Print_Bits(code,6,MVtab1[code].len);
420 printf("%d): %d\n",
421 Show_Bits(1),Show_Bits(1)?-MVtab1[code].val:MVtab1[code].val);
422 }
423 #endif /* TRACE */
424
425 return Get_Bits1()?-MVtab1[code].val:MVtab1[code].val;
426 }
427
428 if ((code-=12)<0)
429 {
430 if (!Quiet_Flag)
431 /* HACK */
432 printf("Invalid motion_vector code (MBA %d, pic %d)\n", global_MBA, global_pic);
433 Fault_Flag=1;
434 return 0;
435 }
436
437 Flush_Buffer(MVtab2[code].len);
438
439 #ifdef TRACE
440 if (Trace_Flag)
441 {
442 Print_Bits(code+12,9,MVtab2[code].len);
443 printf("%d): %d\n",
444 Show_Bits(1),Show_Bits(1)?-MVtab2[code].val:MVtab2[code].val);
445 }
446 #endif /* TRACE */
447
448 return Get_Bits1() ? -MVtab2[code].val : MVtab2[code].val;
449 }
450
451 /* get differential motion vector (for dual prime prediction) */
452 int Get_dmvector()
453 {
454 #ifdef TRACE
455 if (Trace_Flag)
456 printf("dmvector (");
457 #endif /* TRACE */
458
459 if (Get_Bits(1))
460 {
461 #ifdef TRACE
462 if (Trace_Flag)
463 printf(Show_Bits(1) ? "11): -1\n" : "10): 1\n");
464 #endif /* TRACE */
465 return Get_Bits(1) ? -1 : 1;
466 }
467 else
468 {
469 #ifdef TRACE
470 if (Trace_Flag)
471 printf("0): 0\n");
472 #endif /* TRACE */
473 return 0;
474 }
475 }
476
477 int Get_coded_block_pattern()
478 {
479 int code;
480
481 #ifdef TRACE
482 if (Trace_Flag)
483 printf("coded_block_pattern_420 (");
484 #endif /* TRACE */
485
486 if ((code = Show_Bits(9))>=128)
487 {
488 code >>= 4;
489 Flush_Buffer(CBPtab0[code].len);
490
491 #ifdef TRACE
492 if (Trace_Flag)
493 {
494 Print_Bits(code,5,CBPtab0[code].len);
495 printf("): ");
496 Print_Bits(CBPtab0[code].val,6,6);
497 printf(" (%d)\n",CBPtab0[code].val);
498 }
499 #endif /* TRACE */
500
501 return CBPtab0[code].val;
502 }
503
504 if (code>=8)
505 {
506 code >>= 1;
507 Flush_Buffer(CBPtab1[code].len);
508
509 #ifdef TRACE
510 if (Trace_Flag)
511 {
512 Print_Bits(code,8,CBPtab1[code].len);
513 printf("): ");
514 Print_Bits(CBPtab1[code].val,6,6);
515 printf(" (%d)\n",CBPtab1[code].val);
516 }
517 #endif /* TRACE */
518
519 return CBPtab1[code].val;
520 }
521
522 if (code<1)
523 {
524 if (!Quiet_Flag)
525 printf("Invalid coded_block_pattern code\n");
526 Fault_Flag = 1;
527 return 0;
528 }
529
530 Flush_Buffer(CBPtab2[code].len);
531
532 #ifdef TRACE
533 if (Trace_Flag)
534 {
535 Print_Bits(code,9,CBPtab2[code].len);
536 printf("): ");
537 Print_Bits(CBPtab2[code].val,6,6);
538 printf(" (%d)\n",CBPtab2[code].val);
539 }
540 #endif /* TRACE */
541
542 return CBPtab2[code].val;
543 }
544
545 int Get_macroblock_address_increment()
546 {
547 int code, val;
548
549 #ifdef TRACE
550 if (Trace_Flag)
551 printf("macroblock_address_increment (");
552 #endif /* TRACE */
553
554 val = 0;
555
556 while ((code = Show_Bits(11))<24)
557 {
558 if (code!=15) /* if not macroblock_stuffing */
559 {
560 if (code==8) /* if macroblock_escape */
561 {
562 #ifdef TRACE
563 if (Trace_Flag)
564 printf("00000001000 ");
565 #endif /* TRACE */
566
567 val+= 33;
568 }
569 else
570 {
571 if (!Quiet_Flag)
572 printf("Invalid macroblock_address_increment code\n");
573
574 Fault_Flag = 1;
575 return 1;
576 }
577 }
578 else /* macroblock suffing */
579 {
580 #ifdef TRACE
581 if (Trace_Flag)
582 printf("00000001111 ");
583 #endif /* TRACE */
584 }
585
586 Flush_Buffer(11);
587 }
588
589 /* macroblock_address_increment == 1 */
590 /* ('1' is in the MSB position of the lookahead) */
591 if (code>=1024)
592 {
593 Flush_Buffer(1);
594 #ifdef TRACE
595 if (Trace_Flag)
596 printf("1): %d\n",val+1);
597 #endif /* TRACE */
598 return val + 1;
599 }
600
601 /* codes 00010 ... 011xx */
602 if (code>=128)
603 {
604 /* remove leading zeros */
605 code >>= 6;
606 Flush_Buffer(MBAtab1[code].len);
607
608 #ifdef TRACE
609 if (Trace_Flag)
610 {
611 Print_Bits(code,5,MBAtab1[code].len);
612 printf("): %d\n",val+MBAtab1[code].val);
613 }
614 #endif /* TRACE */
615
616
617 return val + MBAtab1[code].val;
618 }
619
620 /* codes 00000011000 ... 0000111xxxx */
621 code-= 24; /* remove common base */
622 Flush_Buffer(MBAtab2[code].len);
623
624 #ifdef TRACE
625 if (Trace_Flag)
626 {
627 Print_Bits(code+24,11,MBAtab2[code].len);
628 printf("): %d\n",val+MBAtab2[code].val);
629 }
630 #endif /* TRACE */
631
632 return val + MBAtab2[code].val;
633 }
634
635 /* combined MPEG-1 and MPEG-2 stage. parse VLC and
636 perform dct_diff arithmetic.
637
638 MPEG-1: ISO/IEC 11172-2 section
639 MPEG-2: ISO/IEC 13818-2 section 7.2.1
640
641 Note: the arithmetic here is presented more elegantly than
642 the spec, yet the results, dct_diff, are the same.
643 */
644
645 int Get_Luma_DC_dct_diff()
646 {
647 int code, size, dct_diff;
648
649 #ifdef TRACE
650 /*
651 if (Trace_Flag)
652 printf("dct_dc_size_luminance: (");
653 */
654 #endif /* TRACE */
655
656 /* decode length */
657 code = Show_Bits(5);
658
659 if (code<31)
660 {
661 size = DClumtab0[code].val;
662 Flush_Buffer(DClumtab0[code].len);
663 #ifdef TRACE
664 /*
665 if (Trace_Flag)
666 {
667 Print_Bits(code,5,DClumtab0[code].len);
668 printf("): %d",size);
669 }
670 */
671 #endif /* TRACE */
672 }
673 else
674 {
675 code = Show_Bits(9) - 0x1f0;
676 size = DClumtab1[code].val;
677 Flush_Buffer(DClumtab1[code].len);
678
679 #ifdef TRACE
680 /*
681 if (Trace_Flag)
682 {
683 Print_Bits(code+0x1f0,9,DClumtab1[code].len);
684 printf("): %d",size);
685 }
686 */
687 #endif /* TRACE */
688 }
689
690 #ifdef TRACE
691 /*
692 if (Trace_Flag)
693 printf(", dct_dc_differential (");
694 */
695 #endif /* TRACE */
696
697 if (size==0)
698 dct_diff = 0;
699 else
700 {
701 dct_diff = Get_Bits(size);
702 #ifdef TRACE
703 /*
704 if (Trace_Flag)
705 Print_Bits(dct_diff,size,size);
706 */
707 #endif /* TRACE */
708 if ((dct_diff & (1<<(size-1)))==0)
709 dct_diff-= (1<<size) - 1;
710 }
711
712 #ifdef TRACE
713 /*
714 if (Trace_Flag)
715 printf("): %d\n",dct_diff);
716 */
717 #endif /* TRACE */
718
719 return dct_diff;
720 }
721
722
723 int Get_Chroma_DC_dct_diff()
724 {
725 int code, size, dct_diff;
726
727 #ifdef TRACE
728 /*
729 if (Trace_Flag)
730 printf("dct_dc_size_chrominance: (");
731 */
732 #endif /* TRACE */
733
734 /* decode length */
735 code = Show_Bits(5);
736
737 if (code<31)
738 {
739 size = DCchromtab0[code].val;
740 Flush_Buffer(DCchromtab0[code].len);
741
742 #ifdef TRACE
743 /*
744 if (Trace_Flag)
745 {
746 Print_Bits(code,5,DCchromtab0[code].len);
747 printf("): %d",size);
748 }
749 */
750 #endif /* TRACE */
751 }
752 else
753 {
754 code = Show_Bits(10) - 0x3e0;
755 size = DCchromtab1[code].val;
756 Flush_Buffer(DCchromtab1[code].len);
757
758 #ifdef TRACE
759 /*
760 if (Trace_Flag)
761 {
762 Print_Bits(code+0x3e0,10,DCchromtab1[code].len);
763 printf("): %d",size);
764 }
765 */
766 #endif /* TRACE */
767 }
768
769 #ifdef TRACE
770 /*
771 if (Trace_Flag)
772 printf(", dct_dc_differential (");
773 */
774 #endif /* TRACE */
775
776 if (size==0)
777 dct_diff = 0;
778 else
779 {
780 dct_diff = Get_Bits(size);
781 #ifdef TRACE
782 /*
783 if (Trace_Flag)
784 Print_Bits(dct_diff,size,size);
785 */
786 #endif /* TRACE */
787 if ((dct_diff & (1<<(size-1)))==0)
788 dct_diff-= (1<<size) - 1;
789 }
790
791 #ifdef TRACE
792 /*
793 if (Trace_Flag)
794 printf("): %d\n",dct_diff);
795 */
796 #endif /* TRACE */
797
798 return dct_diff;
799 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.