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
34 /* private prototypes */
35 /* generic picture macroblock type processing functions */
36 static int Get_I_macroblock_type _ANSI_ARGS_((void));
37 static int Get_P_macroblock_type _ANSI_ARGS_((void));
38 static int Get_B_macroblock_type _ANSI_ARGS_((void));
39 static int Get_D_macroblock_type _ANSI_ARGS_((void));
41 /* spatial picture macroblock type processing functions */
42 static int Get_I_Spatial_macroblock_type _ANSI_ARGS_((void));
43 static int Get_P_Spatial_macroblock_type _ANSI_ARGS_((void));
44 static int Get_B_Spatial_macroblock_type _ANSI_ARGS_((void));
45 static int Get_SNR_macroblock_type _ANSI_ARGS_((void));
47 int Get_macroblock_type()
49 int macroblock_type = 0;
51 if (ld->scalable_mode==SC_SNR)
52 macroblock_type = Get_SNR_macroblock_type();
55 switch (picture_coding_type)
58 macroblock_type = ld->pict_scal ? Get_I_Spatial_macroblock_type() : Get_I_macroblock_type();
61 macroblock_type = ld->pict_scal ? Get_P_Spatial_macroblock_type() : Get_P_macroblock_type();
64 macroblock_type = ld->pict_scal ? Get_B_Spatial_macroblock_type() : Get_B_macroblock_type();
67 macroblock_type = Get_D_macroblock_type();
70 printf("Get_macroblock_type(): unrecognized picture coding type\n");
75 return macroblock_type;
78 static int Get_I_macroblock_type()
82 printf("macroblock_type(I) ");
89 printf("(1): Intra (1)\n");
97 printf("Invalid macroblock_type code\n");
103 printf("(01): Intra, Quant (17)\n");
110 static char *MBdescr[]={
111 "", "Intra", "No MC, Coded", "",
112 "Bwd, Not Coded", "", "Bwd, Coded", "",
113 "Fwd, Not Coded", "", "Fwd, Coded", "",
114 "Interp, Not Coded", "", "Interp, Coded", "",
115 "", "Intra, Quant", "No MC, Coded, Quant", "",
116 "", "", "Bwd, Coded, Quant", "",
117 "", "", "Fwd, Coded, Quant", "",
118 "", "", "Interp, Coded, Quant", ""
122 static int Get_P_macroblock_type()
128 printf("macroblock_type(P) (");
131 if ((code = Show_Bits(6))>=8)
134 Flush_Buffer(PMBtab0[code].len);
138 Print_Bits(code,3,PMBtab0[code].len);
139 printf("): %s (%d)\n",MBdescr[(int)PMBtab0[code].val],PMBtab0[code].val);
142 return PMBtab0[code].val;
148 printf("Invalid macroblock_type code\n");
153 Flush_Buffer(PMBtab1[code].len);
158 Print_Bits(code,6,PMBtab1[code].len);
159 printf("): %s (%d)\n",MBdescr[(int)PMBtab1[code].val],PMBtab1[code].val);
163 return PMBtab1[code].val;
166 static int Get_B_macroblock_type()
172 printf("macroblock_type(B) (");
175 if ((code = Show_Bits(6))>=8)
178 Flush_Buffer(BMBtab0[code].len);
183 Print_Bits(code,4,BMBtab0[code].len);
184 printf("): %s (%d)\n",MBdescr[(int)BMBtab0[code].val],BMBtab0[code].val);
188 return BMBtab0[code].val;
194 printf("Invalid macroblock_type code\n");
199 Flush_Buffer(BMBtab1[code].len);
204 Print_Bits(code,6,BMBtab1[code].len);
205 printf("): %s (%d)\n",MBdescr[(int)BMBtab1[code].val],BMBtab1[code].val);
209 return BMBtab1[code].val;
212 static int Get_D_macroblock_type()
217 printf("Invalid macroblock_type code\n");
224 /* macroblock_type for pictures with spatial scalability */
225 static int Get_I_Spatial_macroblock_type()
231 printf("macroblock_type(I,spat) (");
239 printf("Invalid macroblock_type code\n");
247 Print_Bits(code,4,spIMBtab[code].len);
248 printf("): %02x\n",spIMBtab[code].val);
252 Flush_Buffer(spIMBtab[code].len);
253 return spIMBtab[code].val;
256 static int Get_P_Spatial_macroblock_type()
262 printf("macroblock_type(P,spat) (");
270 printf("Invalid macroblock_type code\n");
278 Flush_Buffer(spPMBtab0[code].len);
283 Print_Bits(code,4,spPMBtab0[code].len);
284 printf("): %02x\n",spPMBtab0[code].val);
288 return spPMBtab0[code].val;
291 Flush_Buffer(spPMBtab1[code].len);
296 Print_Bits(code,7,spPMBtab1[code].len);
297 printf("): %02x\n",spPMBtab1[code].val);
301 return spPMBtab1[code].val;
304 static int Get_B_Spatial_macroblock_type()
311 printf("macroblock_type(B,spat) (");
317 p = &spBMBtab0[(code>>5)-2];
319 p = &spBMBtab1[(code>>2)-4];
321 p = &spBMBtab2[code-8];
325 printf("Invalid macroblock_type code\n");
330 Flush_Buffer(p->len);
335 Print_Bits(code,9,p->len);
336 printf("): %02x\n",p->val);
343 static int Get_SNR_macroblock_type()
347 #ifdef TRACE /* *CH* */
349 printf("macroblock_type(SNR) (");
357 printf("Invalid macroblock_type code\n");
362 Flush_Buffer(SNRMBtab[code].len);
364 #ifdef TRACE /* *CH* */
367 Print_Bits(code,3,SNRMBtab[code].len);
368 printf("): %s (%d)\n",MBdescr[(int)SNRMBtab[code].val],SNRMBtab[code].val);
373 return SNRMBtab[code].val;
376 int Get_motion_code()
382 printf("motion_code (");
394 if ((code = Show_Bits(9))>=64)
397 Flush_Buffer(MVtab0[code].len);
402 Print_Bits(code,3,MVtab0[code].len);
404 Show_Bits(1),Show_Bits(1)?-MVtab0[code].val:MVtab0[code].val);
408 return Get_Bits1()?-MVtab0[code].val:MVtab0[code].val;
414 Flush_Buffer(MVtab1[code].len);
419 Print_Bits(code,6,MVtab1[code].len);
421 Show_Bits(1),Show_Bits(1)?-MVtab1[code].val:MVtab1[code].val);
425 return Get_Bits1()?-MVtab1[code].val:MVtab1[code].val;
432 printf("Invalid motion_vector code (MBA %d, pic %d)\n", global_MBA, global_pic);
437 Flush_Buffer(MVtab2[code].len);
442 Print_Bits(code+12,9,MVtab2[code].len);
444 Show_Bits(1),Show_Bits(1)?-MVtab2[code].val:MVtab2[code].val);
448 return Get_Bits1() ? -MVtab2[code].val : MVtab2[code].val;
451 /* get differential motion vector (for dual prime prediction) */
456 printf("dmvector (");
463 printf(Show_Bits(1) ? "11): -1\n" : "10): 1\n");
465 return Get_Bits(1) ? -1 : 1;
477 int Get_coded_block_pattern()
483 printf("coded_block_pattern_420 (");
486 if ((code = Show_Bits(9))>=128)
489 Flush_Buffer(CBPtab0[code].len);
494 Print_Bits(code,5,CBPtab0[code].len);
496 Print_Bits(CBPtab0[code].val,6,6);
497 printf(" (%d)\n",CBPtab0[code].val);
501 return CBPtab0[code].val;
507 Flush_Buffer(CBPtab1[code].len);
512 Print_Bits(code,8,CBPtab1[code].len);
514 Print_Bits(CBPtab1[code].val,6,6);
515 printf(" (%d)\n",CBPtab1[code].val);
519 return CBPtab1[code].val;
525 printf("Invalid coded_block_pattern code\n");
530 Flush_Buffer(CBPtab2[code].len);
535 Print_Bits(code,9,CBPtab2[code].len);
537 Print_Bits(CBPtab2[code].val,6,6);
538 printf(" (%d)\n",CBPtab2[code].val);
542 return CBPtab2[code].val;
545 int Get_macroblock_address_increment()
551 printf("macroblock_address_increment (");
556 while ((code = Show_Bits(11))<24)
558 if (code!=15) /* if not macroblock_stuffing */
560 if (code==8) /* if macroblock_escape */
564 printf("00000001000 ");
572 printf("Invalid macroblock_address_increment code\n");
578 else /* macroblock suffing */
582 printf("00000001111 ");
589 /* macroblock_address_increment == 1 */
590 /* ('1' is in the MSB position of the lookahead) */
596 printf("1): %d\n",val+1);
601 /* codes 00010 ... 011xx */
604 /* remove leading zeros */
606 Flush_Buffer(MBAtab1[code].len);
611 Print_Bits(code,5,MBAtab1[code].len);
612 printf("): %d\n",val+MBAtab1[code].val);
617 return val + MBAtab1[code].val;
620 /* codes 00000011000 ... 0000111xxxx */
621 code-= 24; /* remove common base */
622 Flush_Buffer(MBAtab2[code].len);
627 Print_Bits(code+24,11,MBAtab2[code].len);
628 printf("): %d\n",val+MBAtab2[code].val);
632 return val + MBAtab2[code].val;
635 /* combined MPEG-1 and MPEG-2 stage. parse VLC and
636 perform dct_diff arithmetic.
638 MPEG-1: ISO/IEC 11172-2 section
639 MPEG-2: ISO/IEC 13818-2 section 7.2.1
641 Note: the arithmetic here is presented more elegantly than
642 the spec, yet the results, dct_diff, are the same.
645 int Get_Luma_DC_dct_diff()
647 int code, size, dct_diff;
652 printf("dct_dc_size_luminance: (");
661 size = DClumtab0[code].val;
662 Flush_Buffer(DClumtab0[code].len);
667 Print_Bits(code,5,DClumtab0[code].len);
668 printf("): %d",size);
675 code = Show_Bits(9) - 0x1f0;
676 size = DClumtab1[code].val;
677 Flush_Buffer(DClumtab1[code].len);
683 Print_Bits(code+0x1f0,9,DClumtab1[code].len);
684 printf("): %d",size);
693 printf(", dct_dc_differential (");
701 dct_diff = Get_Bits(size);
705 Print_Bits(dct_diff,size,size);
708 if ((dct_diff & (1<<(size-1)))==0)
709 dct_diff-= (1<<size) - 1;
715 printf("): %d\n",dct_diff);
723 int Get_Chroma_DC_dct_diff()
725 int code, size, dct_diff;
730 printf("dct_dc_size_chrominance: (");
739 size = DCchromtab0[code].val;
740 Flush_Buffer(DCchromtab0[code].len);
746 Print_Bits(code,5,DCchromtab0[code].len);
747 printf("): %d",size);
754 code = Show_Bits(10) - 0x3e0;
755 size = DCchromtab1[code].val;
756 Flush_Buffer(DCchromtab1[code].len);
762 Print_Bits(code+0x3e0,10,DCchromtab1[code].len);
763 printf("): %d",size);
772 printf(", dct_dc_differential (");
780 dct_diff = Get_Bits(size);
784 Print_Bits(dct_diff,size,size);
787 if ((dct_diff & (1<<(size-1)))==0)
788 dct_diff-= (1<<size) - 1;
794 printf("): %d\n",dct_diff);