1 /* putvlc.c, generation of variable length codes */
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 static void putDC _ANSI_ARGS_((sVLCtable *tab, int val));
39 /* generate variable length code for luminance DC coefficient */
46 /* generate variable length code for chrominance DC coefficient */
50 putDC(DCchromtab,val);
53 /* generate variable length code for DC coefficient (7.2.1) */
54 static void putDC(tab,val)
60 absval = (val<0) ? -val : val; /* abs(val) */
62 if (absval>2047 || (mpeg1 && absval>255))
64 /* should never happen */
65 sprintf(errortext,"DC value out of range (%d)\n",val);
69 /* compute dct_dc_size */
78 /* generate VLC for dct_dc_size (Table B-12 or B-13) */
79 putbits(tab[size].code,tab[size].len);
81 /* append fixed length code (dc_dct_differential) */
87 absval = val + (1<<size) - 1; /* val + (2 ^ size) - 1 */
92 /* generate variable length code for first coefficient
93 * of a non-intra block (7.2.2.2) */
94 void putACfirst(run,val)
97 if (run==0 && (val==1 || val==-1)) /* these are treated differently */
98 putbits(2|(val<0),2); /* generate '1s' (s=sign), (Table B-14, line 2) */
100 putAC(run,val,0); /* no difference for all others */
103 /* generate variable length code for other DCT coefficients (7.2.2) */
104 void putAC(run,signed_level,vlcformat)
105 int run,signed_level,vlcformat;
110 level = (signed_level<0) ? -signed_level : signed_level; /* abs(signed_level) */
112 /* make sure run and level are valid */
113 if (run<0 || run>63 || level==0 || level>2047 || (mpeg1 && level>255))
115 sprintf(errortext,"AC value out of range (run=%d, signed_level=%d)\n",
122 if (run<2 && level<41)
124 /* vlcformat selects either of Table B-14 / B-15 */
126 ptab = &dct_code_tab1a[run][level-1];
128 ptab = &dct_code_tab1[run][level-1];
132 else if (run<32 && level<6)
134 /* vlcformat selects either of Table B-14 / B-15 */
136 ptab = &dct_code_tab2a[run-2][level-1];
138 ptab = &dct_code_tab2[run-2][level-1];
143 if (len!=0) /* a VLC code exists */
145 putbits(ptab->code,len);
146 putbits(signed_level<0,1); /* sign */
150 /* no VLC for this (run, level) combination: use escape coding (7.2.2.3) */
151 putbits(1l,6); /* Escape */
152 putbits(run,6); /* 6 bit code for run */
155 /* ISO/IEC 11172-2 uses a 8 or 16 bit code */
156 if (signed_level>127)
158 if (signed_level<-127)
160 putbits(signed_level,8);
164 /* ISO/IEC 13818-2 uses a 12 bit code, Table B-16 */
165 putbits(signed_level,12);
170 /* generate variable length code for macroblock_address_increment (6.3.16) */
171 void putaddrinc(addrinc)
176 putbits(0x08,11); /* macroblock_escape */
180 putbits(addrinctab[addrinc-1].code,addrinctab[addrinc-1].len);
183 /* generate variable length code for macroblock_type (6.3.16.1) */
184 void putmbtype(pict_type,mb_type)
185 int pict_type,mb_type;
187 putbits(mbtypetab[pict_type-1][mb_type].code,
188 mbtypetab[pict_type-1][mb_type].len);
191 /* generate variable length code for motion_code (6.3.16.3) */
192 void putmotioncode(motion_code)
197 abscode = (motion_code>=0) ? motion_code : -motion_code; /* abs(motion_code) */
198 putbits(motionvectab[abscode].code,motionvectab[abscode].len);
200 putbits(motion_code<0,1); /* sign, 0=positive, 1=negative */
203 /* generate variable length code for dmvector[t] (6.3.16.3), Table B-11 */
215 /* generate variable length code for coded_block_pattern (6.3.16.4)
217 * 4:2:2, 4:4:4 not implemented
222 putbits(cbptable[cbp].code,cbptable[cbp].len);