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 /* generate variable length code for DC coefficient (7.2.1) */
37 static void putDC(slice_engine_t
*engine
, sVLCtable
*tab
, int val
)
41 absval
= (val
< 0) ? -val
: val
;
43 if(absval
> dctsatlim
)
45 /* should never happen */
46 sprintf(errortext
,"DC value out of range (%d)\n",val
);
50 /* compute dct_dc_size */
59 //printf("putDC %x %d %d\n", tab[size].code, tab[size].len, size);
60 //slice_testbits(engine);
61 /* generate VLC for dct_dc_size (Table B-12 or B-13) */
62 slice_putbits(engine
, tab
[size
].code
, tab
[size
].len
);
63 //slice_testbits(engine);
65 /* append fixed length code (dc_dct_differential) */
71 absval
= val
+ (1 << size
) - 1;
72 slice_putbits(engine
, absval
, size
);
74 //slice_testbits(engine);
77 /* generate variable length code for luminance DC coefficient */
78 void putDClum(slice_engine_t
*engine
, int val
)
80 putDC(engine
, DClumtab
, val
);
83 /* generate variable length code for chrominance DC coefficient */
84 void putDCchrom(slice_engine_t
*engine
, int val
)
86 putDC(engine
, DCchromtab
, val
);
89 /* generate variable length code for other DCT coefficients (7.2.2) */
90 void putAC(slice_engine_t
*engine
, int run
, int signed_level
, int vlcformat
)
95 level
= (signed_level
< 0) ? -signed_level
: signed_level
; /* abs(signed_level) */
97 /* make sure run and level are valid */
98 if(run
< 0 || run
> 63 || level
== 0 || level
> dctsatlim
)
100 sprintf(errortext
,"AC value out of range (run=%d, signed_level=%d)\n",
107 if(run
< 2 && level
< 41)
109 /* vlcformat selects either of Table B-14 / B-15 */
111 ptab
= &dct_code_tab1a
[run
][level
- 1];
113 ptab
= &dct_code_tab1
[run
][level
- 1];
118 if(run
< 32 && level
< 6)
120 /* vlcformat selects either of Table B-14 / B-15 */
122 ptab
= &dct_code_tab2a
[run
- 2][level
- 1];
124 ptab
= &dct_code_tab2
[run
- 2][level
- 1];
129 if(len
!= 0) /* a VLC code exists */
131 slice_putbits(engine
, ptab
->code
, len
);
132 slice_putbits(engine
, signed_level
< 0, 1); /* sign */
136 /* no VLC for this (run, level) combination: use escape coding (7.2.2.3) */
137 slice_putbits(engine
, 1l, 6); /* Escape */
138 slice_putbits(engine
, run
, 6); /* 6 bit code for run */
141 /* ISO/IEC 11172-2 uses a 8 or 16 bit code */
142 if (signed_level
> 127)
143 slice_putbits(engine
, 0, 8);
144 if (signed_level
< -127)
145 slice_putbits(engine
, 128, 8);
146 slice_putbits(engine
, signed_level
, 8);
150 /* ISO/IEC 13818-2 uses a 12 bit code, Table B-16 */
151 slice_putbits(engine
, signed_level
, 12);
156 /* generate variable length code for first coefficient
157 * of a non-intra block (7.2.2.2) */
158 void putACfirst(slice_engine_t
*engine
, int run
, int val
)
160 if(run
== 0 && (val
== 1 || val
== -1)) /* these are treated differently */
161 slice_putbits(engine
, 2 | (val
< 0), 2); /* generate '1s' (s=sign), (Table B-14, line 2) */
163 putAC(engine
, run
, val
, 0); /* no difference for all others */
166 /* generate variable length code for macroblock_address_increment (6.3.16) */
167 void putaddrinc(slice_engine_t
*engine
, int addrinc
)
171 slice_putbits(engine
, 0x08, 11); /* macroblock_escape */
175 //printf("putaddrinc %d %d\n", addrinctab[addrinc - 1].code, addrinctab[addrinc - 1].len);
177 slice_putbits(engine
,
178 addrinctab
[addrinc
- 1].code
,
179 addrinctab
[addrinc
- 1].len
);
182 /* generate variable length code for macroblock_type (6.3.16.1) */
183 void putmbtype(slice_engine_t
*engine
, int pict_type
, int mb_type
)
185 slice_putbits(engine
,
186 mbtypetab
[pict_type
- 1][mb_type
].code
,
187 mbtypetab
[pict_type
- 1][mb_type
].len
);
190 /* generate variable length code for motion_code (6.3.16.3) */
191 void putmotioncode(slice_engine_t
*engine
, int motion_code
)
195 abscode
= (motion_code
>= 0) ? motion_code
: -motion_code
; /* abs(motion_code) */
196 slice_putbits(engine
,
197 motionvectab
[abscode
].code
,
198 motionvectab
[abscode
].len
);
200 slice_putbits(engine
,
202 1); /* sign, 0=positive, 1=negative */
205 /* generate variable length code for dmvector[t] (6.3.16.3), Table B-11 */
206 void putdmv(slice_engine_t
*engine
, int dmv
)
209 slice_putbits(engine
, 0, 1);
211 slice_putbits(engine
, 2, 2);
213 slice_putbits(engine
, 3, 2);
216 /* generate variable length code for coded_block_pattern (6.3.16.4)
218 * 4:2:2, 4:4:4 not implemented
220 void putcbp(slice_engine_t
*engine
, int cbp
)
222 slice_putbits(engine
, cbptable
[cbp
].code
, cbptable
[cbp
].len
);