r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / mpeg2enc / putvlc.c
blob5ed62b8625d165216f0426ba2e5c67a14a84728c
1 /* putvlc.c, generation of variable length codes */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
5 /*
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
21 * patents.
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.
30 #include <stdio.h>
32 #include "config.h"
33 #include "global.h"
34 #include "vlc.h"
36 /* generate variable length code for DC coefficient (7.2.1) */
37 static void putDC(slice_engine_t *engine, sVLCtable *tab, int val)
39 int absval, size;
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);
47 error(errortext);
50 /* compute dct_dc_size */
51 size = 0;
53 while(absval)
55 absval >>= 1;
56 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) */
66 if(size != 0)
68 if(val >= 0)
69 absval = val;
70 else
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)
92 int level, len;
93 VLCtable *ptab;
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",
101 run,signed_level);
102 error(errortext);
105 len = 0;
107 if(run < 2 && level < 41)
109 /* vlcformat selects either of Table B-14 / B-15 */
110 if(vlcformat)
111 ptab = &dct_code_tab1a[run][level - 1];
112 else
113 ptab = &dct_code_tab1[run][level - 1];
115 len = ptab->len;
117 else
118 if(run < 32 && level < 6)
120 /* vlcformat selects either of Table B-14 / B-15 */
121 if(vlcformat)
122 ptab = &dct_code_tab2a[run - 2][level - 1];
123 else
124 ptab = &dct_code_tab2[run - 2][level - 1];
126 len = ptab->len;
129 if(len != 0) /* a VLC code exists */
131 slice_putbits(engine, ptab->code, len);
132 slice_putbits(engine, signed_level < 0, 1); /* sign */
134 else
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 */
139 if(mpeg1)
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);
148 else
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) */
162 else
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)
169 while(addrinc > 33)
171 slice_putbits(engine, 0x08, 11); /* macroblock_escape */
172 addrinc -= 33;
175 //printf("putaddrinc %d %d\n", addrinctab[addrinc - 1].code, addrinctab[addrinc - 1].len);
176 //sleep(1);
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)
193 int abscode;
195 abscode = (motion_code >= 0) ? motion_code : -motion_code; /* abs(motion_code) */
196 slice_putbits(engine,
197 motionvectab[abscode].code,
198 motionvectab[abscode].len);
199 if(motion_code!=0)
200 slice_putbits(engine,
201 motion_code < 0,
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)
208 if(dmv == 0)
209 slice_putbits(engine, 0, 1);
210 else if (dmv>0)
211 slice_putbits(engine, 2, 2);
212 else
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);