1 /* getvlc.c, variable length decoding */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6 * Disclaimer of Warranty
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.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
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
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
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));
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));
49 int Get_macroblock_type()
51 int macroblock_type = 0;
53 if (ld->scalable_mode==SC_SNR)
54 macroblock_type = Get_SNR_macroblock_type();
57 switch (picture_coding_type)
60 macroblock_type = ld->pict_scal ? Get_I_Spatial_macroblock_type() : Get_I_macroblock_type();
63 macroblock_type = ld->pict_scal ? Get_P_Spatial_macroblock_type() : Get_P_macroblock_type();
66 macroblock_type = ld->pict_scal ? Get_B_Spatial_macroblock_type() : Get_B_macroblock_type();
69 macroblock_type = Get_D_macroblock_type();
72 printf("Get_macroblock_type(): unrecognized picture coding type\n");
77 return macroblock_type;
80 static int Get_I_macroblock_type()
84 printf("macroblock_type(I) ");
91 printf("(1): Intra (1)\n");
99 printf("Invalid macroblock_type code\n");
105 printf("(01): Intra, Quant (17)\n");
112 static char *MBdescr[]={
113 "", "Intra", "No MC, Coded", "",
114 "Bwd, Not Coded", "", "Bwd, Coded", "",
115 "Fwd, Not Coded", "", "Fwd, Coded", "",
116 "Interp, Not Coded", "", "Interp, Coded", "",
117 "", "Intra, Quant", "No MC, Coded, Quant", "",
118 "", "", "Bwd, Coded, Quant", "",
119 "", "", "Fwd, Coded, Quant", "",
120 "", "", "Interp, Coded, Quant", ""
124 static int Get_P_macroblock_type()
130 printf("macroblock_type(P) (");
133 if ((code = Show_Bits(6))>=8)
136 Flush_Buffer(PMBtab0[code].len);
140 Print_Bits(code,3,PMBtab0[code].len);
141 printf("): %s (%d)\n",MBdescr[(int)PMBtab0[code].val],PMBtab0[code].val);
144 return PMBtab0[code].val;
150 printf("Invalid macroblock_type code\n");
155 Flush_Buffer(PMBtab1[code].len);
160 Print_Bits(code,6,PMBtab1[code].len);
161 printf("): %s (%d)\n",MBdescr[(int)PMBtab1[code].val],PMBtab1[code].val);
165 return PMBtab1[code].val;
168 static int Get_B_macroblock_type()
174 printf("macroblock_type(B) (");
177 if ((code = Show_Bits(6))>=8)
180 Flush_Buffer(BMBtab0[code].len);
185 Print_Bits(code,4,BMBtab0[code].len);
186 printf("): %s (%d)\n",MBdescr[(int)BMBtab0[code].val],BMBtab0[code].val);
190 return BMBtab0[code].val;
196 printf("Invalid macroblock_type code\n");
201 Flush_Buffer(BMBtab1[code].len);
206 Print_Bits(code,6,BMBtab1[code].len);
207 printf("): %s (%d)\n",MBdescr[(int)BMBtab1[code].val],BMBtab1[code].val);
211 return BMBtab1[code].val;
214 static int Get_D_macroblock_type()
219 printf("Invalid macroblock_type code\n");
226 /* macroblock_type for pictures with spatial scalability */
227 static int Get_I_Spatial_macroblock_type()
233 printf("macroblock_type(I,spat) (");
241 printf("Invalid macroblock_type code\n");
249 Print_Bits(code,4,spIMBtab[code].len);
250 printf("): %02x\n",spIMBtab[code].val);
254 Flush_Buffer(spIMBtab[code].len);
255 return spIMBtab[code].val;
258 static int Get_P_Spatial_macroblock_type()
264 printf("macroblock_type(P,spat) (");
272 printf("Invalid macroblock_type code\n");
280 Flush_Buffer(spPMBtab0[code].len);
285 Print_Bits(code,4,spPMBtab0[code].len);
286 printf("): %02x\n",spPMBtab0[code].val);
290 return spPMBtab0[code].val;
293 Flush_Buffer(spPMBtab1[code].len);
298 Print_Bits(code,7,spPMBtab1[code].len);
299 printf("): %02x\n",spPMBtab1[code].val);
303 return spPMBtab1[code].val;
306 static int Get_B_Spatial_macroblock_type()
313 printf("macroblock_type(B,spat) (");
319 p = &spBMBtab0[(code>>5)-2];
321 p = &spBMBtab1[(code>>2)-4];
323 p = &spBMBtab2[code-8];
327 printf("Invalid macroblock_type code\n");
332 Flush_Buffer(p->len);
337 Print_Bits(code,9,p->len);
338 printf("): %02x\n",p->val);
345 static int Get_SNR_macroblock_type()
349 #ifdef TRACE /* *CH* */
351 printf("macroblock_type(SNR) (");
359 printf("Invalid macroblock_type code\n");
364 Flush_Buffer(SNRMBtab[code].len);
366 #ifdef TRACE /* *CH* */
369 Print_Bits(code,3,SNRMBtab[code].len);
370 printf("): %s (%d)\n",MBdescr[(int)SNRMBtab[code].val],SNRMBtab[code].val);
375 return SNRMBtab[code].val;
378 int Get_motion_code()
384 printf("motion_code (");
396 if ((code = Show_Bits(9))>=64)
399 Flush_Buffer(MVtab0[code].len);
404 Print_Bits(code,3,MVtab0[code].len);
406 Show_Bits(1),Show_Bits(1)?-MVtab0[code].val:MVtab0[code].val);
410 return Get_Bits1()?-MVtab0[code].val:MVtab0[code].val;
416 Flush_Buffer(MVtab1[code].len);
421 Print_Bits(code,6,MVtab1[code].len);
423 Show_Bits(1),Show_Bits(1)?-MVtab1[code].val:MVtab1[code].val);
427 return Get_Bits1()?-MVtab1[code].val:MVtab1[code].val;
434 printf("Invalid motion_vector code (MBA %d, pic %d)\n", global_MBA, global_pic);
439 Flush_Buffer(MVtab2[code].len);
444 Print_Bits(code+12,9,MVtab2[code].len);
446 Show_Bits(1),Show_Bits(1)?-MVtab2[code].val:MVtab2[code].val);
450 return Get_Bits1() ? -MVtab2[code].val : MVtab2[code].val;
453 /* get differential motion vector (for dual prime prediction) */
458 printf("dmvector (");
465 printf(Show_Bits(1) ? "11): -1\n" : "10): 1\n");
467 return Get_Bits(1) ? -1 : 1;
479 int Get_coded_block_pattern()
485 printf("coded_block_pattern_420 (");
488 if ((code = Show_Bits(9))>=128)
491 Flush_Buffer(CBPtab0[code].len);
496 Print_Bits(code,5,CBPtab0[code].len);
498 Print_Bits(CBPtab0[code].val,6,6);
499 printf(" (%d)\n",CBPtab0[code].val);
503 return CBPtab0[code].val;
509 Flush_Buffer(CBPtab1[code].len);
514 Print_Bits(code,8,CBPtab1[code].len);
516 Print_Bits(CBPtab1[code].val,6,6);
517 printf(" (%d)\n",CBPtab1[code].val);
521 return CBPtab1[code].val;
527 printf("Invalid coded_block_pattern code\n");
532 Flush_Buffer(CBPtab2[code].len);
537 Print_Bits(code,9,CBPtab2[code].len);
539 Print_Bits(CBPtab2[code].val,6,6);
540 printf(" (%d)\n",CBPtab2[code].val);
544 return CBPtab2[code].val;
547 int Get_macroblock_address_increment()
553 printf("macroblock_address_increment (");
558 while ((code = Show_Bits(11))<24)
560 if (code!=15) /* if not macroblock_stuffing */
562 if (code==8) /* if macroblock_escape */
566 printf("00000001000 ");
574 printf("Invalid macroblock_address_increment code\n");
580 else /* macroblock suffing */
584 printf("00000001111 ");
591 /* macroblock_address_increment == 1 */
592 /* ('1' is in the MSB position of the lookahead) */
598 printf("1): %d\n",val+1);
603 /* codes 00010 ... 011xx */
606 /* remove leading zeros */
608 Flush_Buffer(MBAtab1[code].len);
613 Print_Bits(code,5,MBAtab1[code].len);
614 printf("): %d\n",val+MBAtab1[code].val);
619 return val + MBAtab1[code].val;
622 /* codes 00000011000 ... 0000111xxxx */
623 code-= 24; /* remove common base */
624 Flush_Buffer(MBAtab2[code].len);
629 Print_Bits(code+24,11,MBAtab2[code].len);
630 printf("): %d\n",val+MBAtab2[code].val);
634 return val + MBAtab2[code].val;
637 /* combined MPEG-1 and MPEG-2 stage. parse VLC and
638 perform dct_diff arithmetic.
640 MPEG-1: ISO/IEC 11172-2 section
641 MPEG-2: ISO/IEC 13818-2 section 7.2.1
643 Note: the arithmetic here is presented more elegantly than
644 the spec, yet the results, dct_diff, are the same.
647 int Get_Luma_DC_dct_diff()
649 int code, size, dct_diff;
654 printf("dct_dc_size_luminance: (");
663 size = DClumtab0[code].val;
664 Flush_Buffer(DClumtab0[code].len);
669 Print_Bits(code,5,DClumtab0[code].len);
670 printf("): %d",size);
677 code = Show_Bits(9) - 0x1f0;
678 size = DClumtab1[code].val;
679 Flush_Buffer(DClumtab1[code].len);
685 Print_Bits(code+0x1f0,9,DClumtab1[code].len);
686 printf("): %d",size);
695 printf(", dct_dc_differential (");
703 dct_diff = Get_Bits(size);
707 Print_Bits(dct_diff,size,size);
710 if ((dct_diff & (1<<(size-1)))==0)
711 dct_diff-= (1<<size) - 1;
717 printf("): %d\n",dct_diff);
725 int Get_Chroma_DC_dct_diff()
727 int code, size, dct_diff;
732 printf("dct_dc_size_chrominance: (");
741 size = DCchromtab0[code].val;
742 Flush_Buffer(DCchromtab0[code].len);
748 Print_Bits(code,5,DCchromtab0[code].len);
749 printf("): %d",size);
756 code = Show_Bits(10) - 0x3e0;
757 size = DCchromtab1[code].val;
758 Flush_Buffer(DCchromtab1[code].len);
764 Print_Bits(code+0x3e0,10,DCchromtab1[code].len);
765 printf("): %d",size);
774 printf(", dct_dc_differential (");
782 dct_diff = Get_Bits(size);
786 Print_Bits(dct_diff,size,size);
789 if ((dct_diff & (1<<(size-1)))==0)
790 dct_diff-= (1<<size) - 1;
796 printf("): %d\n",dct_diff);