1 |
/****************************************************************************** |
/***************************************************************************** |
2 |
* * |
* |
3 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder * |
* XVID MPEG-4 VIDEO CODEC |
4 |
* * |
* - Macro Block coding functions - |
5 |
* XviD is an implementation of a part of one or more MPEG-4 Video tools * |
* |
6 |
* as specified in ISO/IEC 14496-2 standard. Those intending to use this * |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 |
* software module in hardware or software products are advised that its * |
* |
8 |
* use may infringe existing patents or copyrights, and any such use * |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
9 |
* would be at such party's own risk. The original developer of this * |
* |
10 |
* software module and his/her company, and subsequent editors and their * |
* XviD is free software; you can redistribute it and/or modify it |
11 |
* companies, will have no liability for use of this software or * |
* under the terms of the GNU General Public License as published by |
12 |
* modifications or derivatives thereof. * |
* the Free Software Foundation; either version 2 of the License, or |
13 |
* * |
* (at your option) any later version. |
14 |
* XviD is free software; you can redistribute it and/or modify it * |
* |
15 |
* under the terms of the GNU General Public License as published by * |
* This program is distributed in the hope that it will be useful, |
16 |
* the Free Software Foundation; either version 2 of the License, or * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* (at your option) any later version. * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
* * |
* GNU General Public License for more details. |
19 |
* XviD is distributed in the hope that it will be useful, but * |
* |
20 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
* You should have received a copy of the GNU General Public License |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* along with this program; if not, write to the Free Software |
22 |
* GNU General Public License for more details. * |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* * |
* |
24 |
* You should have received a copy of the GNU General Public License * |
* Under section 8 of the GNU General Public License, the copyright |
25 |
* along with this program; if not, write to the Free Software * |
* holders of XVID explicitly forbid distribution in the following |
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
* countries: |
27 |
* * |
* |
28 |
******************************************************************************/ |
* - Japan |
29 |
|
* - United States of America |
30 |
/****************************************************************************** |
* |
31 |
* * |
* Linking XviD statically or dynamically with other modules is making a |
32 |
* bitstream.c * |
* combined work based on XviD. Thus, the terms and conditions of the |
33 |
* * |
* GNU General Public License cover the whole combination. |
34 |
* Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * |
* |
35 |
* * |
* As a special exception, the copyright holders of XviD give you |
36 |
* For more information visit the XviD homepage: http://www.xvid.org * |
* permission to link XviD with independent modules that communicate with |
37 |
* * |
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
38 |
******************************************************************************/ |
* license terms of these independent modules, and to copy and distribute |
39 |
|
* the resulting combined work under terms of your choice, provided that |
40 |
/****************************************************************************** |
* every copy of the combined work is accompanied by a complete copy of |
41 |
* * |
* the source code of XviD (the version of XviD used to produce the |
42 |
* Revision history: * |
* combined work), being distributed under the terms of the GNU General |
43 |
* * |
* Public License plus this exception. An independent module is a module |
44 |
* 14.04.2002 bframe encoding |
* which is not derived from or based on XviD. |
45 |
* 08.03.2002 initial version; isibaar * |
* |
46 |
* * |
* Note that people who make modified versions of XviD are not obligated |
47 |
******************************************************************************/ |
* to grant this special exception for their modified versions; it is |
48 |
|
* their choice whether to do so. The GNU General Public License gives |
49 |
|
* permission to release a modified version without this exception; this |
50 |
|
* exception also makes it possible to release a modified version which |
51 |
|
* carries forward this exception. |
52 |
|
* |
53 |
|
* $Id$ |
54 |
|
* |
55 |
|
****************************************************************************/ |
56 |
|
|
57 |
#include <stdlib.h> |
#include <stdlib.h> |
58 |
#include "../portab.h" |
#include "../portab.h" |
66 |
#define ABS(X) (((X)>0)?(X):-(X)) |
#define ABS(X) (((X)>0)?(X):-(X)) |
67 |
#define CLIP(X,A) (X > A) ? (A) : (X) |
#define CLIP(X,A) (X > A) ? (A) : (X) |
68 |
|
|
69 |
VLC intra_table[65536]; |
/* #define BIGLUT */ |
|
VLC inter_table[65536]; |
|
70 |
|
|
71 |
VLC DCT3Dintra[4096]; |
#ifdef BIGLUT |
72 |
VLC DCT3Dinter[4096]; |
#define LEVELOFFSET 2048 |
73 |
|
#else |
74 |
|
#define LEVELOFFSET 32 |
75 |
|
#endif |
76 |
|
|
77 |
static int16_t clip_table[4096]; |
/***************************************************************************** |
78 |
|
* Local data |
79 |
void init_vlc_tables(void) |
****************************************************************************/ |
|
{ |
|
80 |
|
|
81 |
int32_t k, l, i, intra, last; |
static REVERSE_EVENT DCT3D[2][4096]; |
|
VLC *vlc[2]; |
|
|
VLC **coeff_ptr; |
|
|
VLC *vlc1, *vlc2; |
|
82 |
|
|
83 |
vlc1 = DCT3Dintra; |
#ifdef BIGLUT |
84 |
vlc2 = DCT3Dinter; |
static VLC coeff_VLC[2][2][4096][64]; |
85 |
|
static VLC *intra_table, *inter_table; |
86 |
vlc[0] = intra_table; |
#else |
87 |
vlc[1] = inter_table; |
static VLC coeff_VLC[2][2][64][64]; |
88 |
|
#endif |
|
// initialize the clipping table |
|
|
for(i = -2048; i < 2048; i++) { |
|
|
clip_table[i + 2048] = i; |
|
|
if(i < -255) |
|
|
clip_table[i + 2048] = -255; |
|
|
if(i > 255) |
|
|
clip_table[i + 2048] = 255; |
|
|
} |
|
89 |
|
|
90 |
// generate encoding vlc lookup tables |
/***************************************************************************** |
91 |
for(i = 0; i < 4; i++) { |
* Vector Length Coding Initialization |
92 |
intra = i % 2; |
****************************************************************************/ |
|
last = i / 2; |
|
93 |
|
|
94 |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
void |
95 |
|
init_vlc_tables(void) |
96 |
|
{ |
97 |
|
uint32_t i, j, intra, last, run, run_esc, level, level_esc, escape, escape_len, offset; |
98 |
|
|
99 |
for(k = -255; k < 256; k++) { // level |
#ifdef BIGLUT |
100 |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
intra_table = (VLC*)coeff_VLC[1]; |
101 |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
inter_table = (VLC*)coeff_VLC[0]; |
102 |
|
#endif |
103 |
|
|
|
for(l = 0; l < 64; l++) { // run |
|
|
int32_t level = k; |
|
|
uint32_t run = l; |
|
104 |
|
|
105 |
if((abs(level) <= max_level_ptr[run]) && |
for (intra = 0; intra < 2; intra++) |
106 |
(run <= (uint32_t)max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
for (i = 0; i < 4096; i++) |
107 |
|
DCT3D[intra][i].event.level = 0; |
108 |
|
|
109 |
vlc[intra]->code = 0; |
for (intra = 0; intra < 2; intra++) |
110 |
vlc[intra]->len = 0; |
for (last = 0; last < 2; last++) |
111 |
goto loop_end; |
{ |
112 |
|
for (run = 0; run < 63 + last; run++) |
113 |
|
for (level = 0; level < (uint32_t)(32 << intra); level++) |
114 |
|
{ |
115 |
|
#ifdef BIGLUT |
116 |
|
offset = LEVELOFFSET; |
117 |
|
#else |
118 |
|
offset = !intra * LEVELOFFSET; |
119 |
|
#endif |
120 |
|
coeff_VLC[intra][last][level + offset][run].len = 128; |
121 |
} |
} |
|
else { |
|
|
if(level > 0) // correct level |
|
|
level -= max_level_ptr[run]; |
|
|
else |
|
|
level += max_level_ptr[run]; |
|
|
|
|
|
if((abs(level) <= max_level_ptr[run]) && |
|
|
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
|
|
|
|
|
vlc[intra]->code = 0x06; |
|
|
vlc[intra]->len = 8; |
|
|
goto loop_end; |
|
122 |
} |
} |
123 |
|
|
124 |
if(level > 0) // still here? |
for (intra = 0; intra < 2; intra++) |
125 |
level += max_level_ptr[run]; // restore level |
for (i = 0; i < 102; i++) |
126 |
else |
{ |
127 |
level -= max_level_ptr[run]; |
#ifdef BIGLUT |
128 |
|
offset = LEVELOFFSET; |
129 |
run -= max_run_ptr[abs(level)] + 1; // and change run |
#else |
130 |
|
offset = !intra * LEVELOFFSET; |
131 |
if((abs(level) <= max_level_ptr[run]) && |
#endif |
132 |
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
for (j = 0; j < (uint32_t)(1 << (12 - coeff_tab[intra][i].vlc.len)); j++) |
133 |
|
{ |
134 |
|
DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].len = coeff_tab[intra][i].vlc.len; |
135 |
|
DCT3D[intra][(coeff_tab[intra][i].vlc.code << (12 - coeff_tab[intra][i].vlc.len)) | j].event = coeff_tab[intra][i].event; |
136 |
|
} |
137 |
|
|
138 |
vlc[intra]->code = 0x0e; |
coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].code |
139 |
vlc[intra]->len = 9; |
= coeff_tab[intra][i].vlc.code << 1; |
140 |
goto loop_end; |
coeff_VLC[intra][coeff_tab[intra][i].event.last][coeff_tab[intra][i].event.level + offset][coeff_tab[intra][i].event.run].len |
141 |
|
= coeff_tab[intra][i].vlc.len + 1; |
142 |
|
#ifndef BIGLUT |
143 |
|
if (!intra) |
144 |
|
#endif |
145 |
|
{ |
146 |
|
coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].code |
147 |
|
= (coeff_tab[intra][i].vlc.code << 1) | 1; |
148 |
|
coeff_VLC[intra][coeff_tab[intra][i].event.last][offset - coeff_tab[intra][i].event.level][coeff_tab[intra][i].event.run].len |
149 |
|
= coeff_tab[intra][i].vlc.len + 1; |
150 |
} |
} |
|
run += max_run_ptr[abs(level)] + 1; |
|
151 |
} |
} |
152 |
|
|
153 |
vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) | |
for (intra = 0; intra < 2; intra++) |
154 |
(1 << 13) | ((k & 0xfff) << 1) | 1; |
for (last = 0; last < 2; last++) |
155 |
|
for (run = 0; run < 63 + last; run++) |
156 |
vlc[intra]->len = 30; |
{ |
157 |
vlc[intra]++; |
for (level = 1; level < (uint32_t)(32 << intra); level++) |
158 |
|
{ |
159 |
|
if (level <= max_level[intra][last][run] && run <= max_run[intra][last][level]) |
160 |
continue; |
continue; |
161 |
|
|
162 |
loop_end: |
#ifdef BIGLUT |
163 |
if(level != 0) { |
offset = LEVELOFFSET; |
164 |
vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) | |
#else |
165 |
(coeff_ptr[run][abs(level) - 1].code << 1); |
offset = !intra * LEVELOFFSET; |
166 |
vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len; |
#endif |
167 |
|
level_esc = level - max_level[intra][last][run]; |
168 |
if(level < 0) |
run_esc = run - 1 - max_run[intra][last][level]; |
169 |
vlc[intra]->code += 1; |
|
170 |
|
if (level_esc <= max_level[intra][last][run] && run <= max_run[intra][last][level_esc]) |
171 |
|
{ |
172 |
|
escape = ESCAPE1; |
173 |
|
escape_len = 7 + 1; |
174 |
|
run_esc = run; |
175 |
} |
} |
176 |
|
else |
177 |
vlc[intra]++; |
{ |
178 |
|
if (run_esc <= max_run[intra][last][level] && level <= max_level[intra][last][run_esc]) |
179 |
|
{ |
180 |
|
escape = ESCAPE2; |
181 |
|
escape_len = 7 + 2; |
182 |
|
level_esc = level; |
183 |
} |
} |
184 |
|
else |
185 |
|
{ |
186 |
|
#ifndef BIGLUT |
187 |
|
if (!intra) |
188 |
|
#endif |
189 |
|
{ |
190 |
|
coeff_VLC[intra][last][level + offset][run].code |
191 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
192 |
|
coeff_VLC[intra][last][level + offset][run].len = 30; |
193 |
|
coeff_VLC[intra][last][offset - level][run].code |
194 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1; |
195 |
|
coeff_VLC[intra][last][offset - level][run].len = 30; |
196 |
|
} |
197 |
|
continue; |
198 |
} |
} |
199 |
} |
} |
200 |
|
|
201 |
for(i = 0; i < 4096; i++) { |
coeff_VLC[intra][last][level + offset][run].code |
202 |
if(i >= 512) { |
= (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len) |
203 |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
| coeff_VLC[intra][last][level_esc + offset][run_esc].code; |
204 |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
coeff_VLC[intra][last][level + offset][run].len |
205 |
|
= coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len; |
206 |
|
#ifndef BIGLUT |
207 |
|
if (!intra) |
208 |
|
#endif |
209 |
|
{ |
210 |
|
coeff_VLC[intra][last][offset - level][run].code |
211 |
|
= (escape << coeff_VLC[intra][last][level_esc + offset][run_esc].len) |
212 |
|
| coeff_VLC[intra][last][level_esc + offset][run_esc].code | 1; |
213 |
|
coeff_VLC[intra][last][offset - level][run].len |
214 |
|
= coeff_VLC[intra][last][level_esc + offset][run_esc].len + escape_len; |
215 |
} |
} |
|
else if(i >= 128) { |
|
|
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
|
|
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
|
216 |
} |
} |
217 |
else if(i >= 8) { |
|
218 |
*vlc1 = DCT3Dtab5[i - 8]; |
#ifdef BIGLUT |
219 |
*vlc2 = DCT3Dtab2[i - 8]; |
for (level = (uint32_t)(32 << intra); level < 2048; level++) |
220 |
|
{ |
221 |
|
coeff_VLC[intra][last][level + offset][run].code |
222 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((level & 0xfff) << 1) | 1; |
223 |
|
coeff_VLC[intra][last][level + offset][run].len = 30; |
224 |
|
|
225 |
|
coeff_VLC[intra][last][offset - level][run].code |
226 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-level & 0xfff) << 1) | 1; |
227 |
|
coeff_VLC[intra][last][offset - level][run].len = 30; |
228 |
} |
} |
229 |
else { |
#else |
230 |
*vlc1 = ERRtab[i]; |
if (!intra) |
231 |
*vlc2 = ERRtab[i]; |
{ |
232 |
|
coeff_VLC[intra][last][0][run].code |
233 |
|
= (ESCAPE3 << 21) | (last << 20) | (run << 14) | (1 << 13) | ((-32 & 0xfff) << 1) | 1; |
234 |
|
coeff_VLC[intra][last][0][run].len = 30; |
235 |
} |
} |
236 |
|
#endif |
|
vlc1++; |
|
|
vlc2++; |
|
237 |
} |
} |
238 |
DCT3D[0] = DCT3Dinter; |
|
239 |
DCT3D[1] = DCT3Dintra; |
/* Shut up the compiler -- gcc 3.3 pre release */ |
240 |
|
i = dc_threshold[0]; |
241 |
|
|
242 |
} |
} |
243 |
|
|
244 |
static __inline void CodeVector(Bitstream *bs, |
/***************************************************************************** |
245 |
int16_t value, |
* Local inlined functions for MB coding |
246 |
int16_t f_code, |
****************************************************************************/ |
247 |
|
|
248 |
|
static __inline void |
249 |
|
CodeVector(Bitstream * bs, |
250 |
|
int32_t value, |
251 |
|
int32_t f_code, |
252 |
Statistics *pStat) |
Statistics *pStat) |
253 |
{ |
{ |
254 |
|
|
265 |
pStat->iMvCount++; |
pStat->iMvCount++; |
266 |
|
|
267 |
if (value == 0) { |
if (value == 0) { |
268 |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
BitstreamPutBits(bs, mb_motion_table[32].code, |
269 |
|
mb_motion_table[32].len); |
270 |
} else { |
} else { |
271 |
uint16_t length, code, mv_res, sign; |
uint16_t length, code, mv_res, sign; |
272 |
|
|
291 |
code = -code; |
code = -code; |
292 |
|
|
293 |
code += 32; |
code += 32; |
294 |
BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len); |
BitstreamPutBits(bs, mb_motion_table[code].code, |
295 |
|
mb_motion_table[code].len); |
296 |
|
|
297 |
if(f_code) |
if(f_code) |
298 |
BitstreamPutBits(bs, mv_res, f_code); |
BitstreamPutBits(bs, mv_res, f_code); |
300 |
|
|
301 |
} |
} |
302 |
|
|
303 |
|
#ifdef BIGLUT |
304 |
|
|
305 |
static __inline void CodeCoeff(Bitstream *bs, |
static __inline void |
306 |
int16_t qcoeff[64], |
CodeCoeff(Bitstream * bs, |
307 |
|
const int16_t qcoeff[64], |
308 |
VLC *table, |
VLC *table, |
309 |
const uint16_t *zigzag, |
const uint16_t *zigzag, |
310 |
uint16_t intra) |
uint16_t intra) |
317 |
j = intra; |
j = intra; |
318 |
last = intra; |
last = intra; |
319 |
|
|
320 |
while((v = qcoeff[zigzag[j]]) == 0) j++; |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
321 |
|
j++; |
322 |
|
|
323 |
do { |
do { |
324 |
// count zeroes |
vlc = table + 64 * 2048 + (v << 6) + j - last; |
|
vlc = table + 64*255 + (clip_table[2048+v] << 6) + j - last; |
|
325 |
last = ++j; |
last = ++j; |
|
while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; |
|
326 |
|
|
327 |
// write code |
/* count zeroes */ |
328 |
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
329 |
|
j++; |
330 |
|
|
331 |
|
/* write code */ |
332 |
if(j != 64) { |
if(j != 64) { |
333 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
334 |
} else { |
} else { |
335 |
vlc += 64*511; |
vlc += 64 * 4096; |
336 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
337 |
break; |
break; |
338 |
} |
} |
340 |
|
|
341 |
} |
} |
342 |
|
|
343 |
|
#else |
344 |
|
|
345 |
|
static __inline void |
346 |
|
CodeCoeffInter(Bitstream * bs, |
347 |
|
const int16_t qcoeff[64], |
348 |
|
const uint16_t * zigzag) |
349 |
|
{ |
350 |
|
uint32_t i, run, prev_run, code, len; |
351 |
|
int32_t level, prev_level, level_shifted; |
352 |
|
|
353 |
|
i = 0; |
354 |
|
run = 0; |
355 |
|
|
356 |
static void CodeBlockIntra(const MBParam * pParam, |
while (!(level = qcoeff[zigzag[i++]])) |
357 |
|
run++; |
358 |
|
|
359 |
|
prev_level = level; |
360 |
|
prev_run = run; |
361 |
|
run = 0; |
362 |
|
|
363 |
|
while (i < 64) |
364 |
|
{ |
365 |
|
if ((level = qcoeff[zigzag[i++]]) != 0) |
366 |
|
{ |
367 |
|
level_shifted = prev_level + 32; |
368 |
|
if (!(level_shifted & -64)) |
369 |
|
{ |
370 |
|
code = coeff_VLC[0][0][level_shifted][prev_run].code; |
371 |
|
len = coeff_VLC[0][0][level_shifted][prev_run].len; |
372 |
|
} |
373 |
|
else |
374 |
|
{ |
375 |
|
code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
376 |
|
len = 30; |
377 |
|
} |
378 |
|
BitstreamPutBits(bs, code, len); |
379 |
|
prev_level = level; |
380 |
|
prev_run = run; |
381 |
|
run = 0; |
382 |
|
} |
383 |
|
else |
384 |
|
run++; |
385 |
|
} |
386 |
|
|
387 |
|
level_shifted = prev_level + 32; |
388 |
|
if (!(level_shifted & -64)) |
389 |
|
{ |
390 |
|
code = coeff_VLC[0][1][level_shifted][prev_run].code; |
391 |
|
len = coeff_VLC[0][1][level_shifted][prev_run].len; |
392 |
|
} |
393 |
|
else |
394 |
|
{ |
395 |
|
code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
396 |
|
len = 30; |
397 |
|
} |
398 |
|
BitstreamPutBits(bs, code, len); |
399 |
|
} |
400 |
|
|
401 |
|
static __inline void |
402 |
|
CodeCoeffIntra(Bitstream * bs, |
403 |
|
const int16_t qcoeff[64], |
404 |
|
const uint16_t * zigzag) |
405 |
|
{ |
406 |
|
uint32_t i, abs_level, run, prev_run, code, len; |
407 |
|
int32_t level, prev_level; |
408 |
|
|
409 |
|
i = 1; |
410 |
|
run = 0; |
411 |
|
|
412 |
|
while (!(level = qcoeff[zigzag[i++]])) |
413 |
|
run++; |
414 |
|
|
415 |
|
prev_level = level; |
416 |
|
prev_run = run; |
417 |
|
run = 0; |
418 |
|
|
419 |
|
while (i < 64) |
420 |
|
{ |
421 |
|
if ((level = qcoeff[zigzag[i++]]) != 0) |
422 |
|
{ |
423 |
|
abs_level = ABS(prev_level); |
424 |
|
abs_level = abs_level < 64 ? abs_level : 0; |
425 |
|
code = coeff_VLC[1][0][abs_level][prev_run].code; |
426 |
|
len = coeff_VLC[1][0][abs_level][prev_run].len; |
427 |
|
if (len != 128) |
428 |
|
code |= (prev_level < 0); |
429 |
|
else |
430 |
|
{ |
431 |
|
code = (ESCAPE3 << 21) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
432 |
|
len = 30; |
433 |
|
} |
434 |
|
BitstreamPutBits(bs, code, len); |
435 |
|
prev_level = level; |
436 |
|
prev_run = run; |
437 |
|
run = 0; |
438 |
|
} |
439 |
|
else |
440 |
|
run++; |
441 |
|
} |
442 |
|
|
443 |
|
abs_level = ABS(prev_level); |
444 |
|
abs_level = abs_level < 64 ? abs_level : 0; |
445 |
|
code = coeff_VLC[1][1][abs_level][prev_run].code; |
446 |
|
len = coeff_VLC[1][1][abs_level][prev_run].len; |
447 |
|
if (len != 128) |
448 |
|
code |= (prev_level < 0); |
449 |
|
else |
450 |
|
{ |
451 |
|
code = (ESCAPE3 << 21) | (1 << 20) | (prev_run << 14) | (1 << 13) | ((prev_level & 0xfff) << 1) | 1; |
452 |
|
len = 30; |
453 |
|
} |
454 |
|
BitstreamPutBits(bs, code, len); |
455 |
|
} |
456 |
|
|
457 |
|
#endif |
458 |
|
|
459 |
|
/***************************************************************************** |
460 |
|
* Local functions |
461 |
|
****************************************************************************/ |
462 |
|
|
463 |
|
static void |
464 |
|
CodeBlockIntra(const FRAMEINFO * frame, |
465 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
466 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
467 |
Bitstream * bs, |
Bitstream * bs, |
472 |
|
|
473 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
474 |
|
|
475 |
// write mcbpc |
/* write mcbpc */ |
476 |
if(pParam->coding_type == I_VOP) { |
if (frame->coding_type == I_VOP) { |
477 |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
478 |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
479 |
} |
mcbpc_intra_tab[mcbpc].len); |
480 |
else { |
} else { |
481 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
482 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
483 |
|
mcbpc_inter_tab[mcbpc].len); |
484 |
} |
} |
485 |
|
|
486 |
// ac prediction flag |
/* ac prediction flag */ |
487 |
if(pMB->acpred_directions[0]) |
if(pMB->acpred_directions[0]) |
488 |
BitstreamPutBits(bs, 1, 1); |
BitstreamPutBits(bs, 1, 1); |
489 |
else |
else |
490 |
BitstreamPutBits(bs, 0, 1); |
BitstreamPutBits(bs, 0, 1); |
491 |
|
|
492 |
// write cbpy |
/* write cbpy */ |
493 |
BitstreamPutBits (bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits (bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
494 |
|
|
495 |
// write dquant |
/* write dquant */ |
496 |
if(pMB->mode == MODE_INTRA_Q) |
if(pMB->mode == MODE_INTRA_Q) |
497 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
498 |
|
|
499 |
// write interlacing |
/* write interlacing */ |
500 |
if (pParam->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
|
{ |
|
501 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
502 |
} |
} |
503 |
|
/* code block coeffs */ |
504 |
// code block coeffs |
for (i = 0; i < 6; i++) { |
|
for(i = 0; i < 6; i++) |
|
|
{ |
|
505 |
if(i < 4) |
if(i < 4) |
506 |
BitstreamPutBits(bs, |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
|
dcy_tab[qcoeff[i*64 + 0] + 255].code, |
|
507 |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
508 |
else |
else |
509 |
BitstreamPutBits(bs, |
BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code, |
|
dcc_tab[qcoeff[i*64 + 0] + 255].code, |
|
510 |
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
511 |
|
|
512 |
if(pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) { |
|
{ |
|
513 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
514 |
|
|
515 |
CodeCoeff(bs, |
#ifdef BIGLUT |
516 |
&qcoeff[i*64], |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
517 |
intra_table, |
scan_tables[pMB->acpred_directions[i]], 1); |
518 |
scan_tables[pMB->acpred_directions[i]], |
#else |
519 |
1); |
CodeCoeffIntra(bs, &qcoeff[i * 64], scan_tables[pMB->acpred_directions[i]]); |
520 |
|
#endif |
521 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
522 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
523 |
} |
} |
526 |
} |
} |
527 |
|
|
528 |
|
|
529 |
static void CodeBlockInter(const MBParam * pParam, |
static void |
530 |
|
CodeBlockInter(const FRAMEINFO * frame, |
531 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
532 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
533 |
Bitstream * bs, |
Bitstream * bs, |
540 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
541 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
542 |
|
|
543 |
// write mcbpc |
/* write mcbpc */ |
544 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
545 |
|
mcbpc_inter_tab[mcbpc].len); |
546 |
|
|
547 |
// write cbpy |
/* write cbpy */ |
548 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
549 |
|
|
550 |
// write dquant |
/* write dquant */ |
551 |
if(pMB->mode == MODE_INTER_Q) |
if(pMB->mode == MODE_INTER_Q) |
552 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
553 |
|
|
554 |
// interlacing |
/* interlacing */ |
555 |
if (pParam->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
556 |
{ |
if (pMB->cbp) { |
557 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
558 |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
DPRINTF(DPRINTF_DEBUG, "codep: field_dct: %d", pMB->field_dct); |
559 |
|
} |
560 |
|
|
561 |
// if inter block, write field ME flag |
/* if inter block, write field ME flag */ |
562 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
563 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, pMB->field_pred); |
564 |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
DPRINTF(DPRINTF_DEBUG, "codep: field_pred: %d", pMB->field_pred); |
565 |
|
|
566 |
// write field prediction references |
/* write field prediction references */ |
567 |
if (pMB->field_pred) |
if (pMB->field_pred) { |
|
{ |
|
568 |
BitstreamPutBit(bs, pMB->field_for_top); |
BitstreamPutBit(bs, pMB->field_for_top); |
569 |
BitstreamPutBit(bs, pMB->field_for_bot); |
BitstreamPutBit(bs, pMB->field_for_bot); |
570 |
} |
} |
571 |
} |
} |
572 |
} |
} |
573 |
|
/* code motion vector(s) */ |
574 |
// code motion vector(s) |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
575 |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
576 |
{ |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
|
CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat); |
|
|
CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat); |
|
577 |
} |
} |
578 |
|
|
579 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
580 |
|
|
581 |
// code block coeffs |
/* code block coeffs */ |
582 |
for(i = 0; i < 6; i++) |
for(i = 0; i < 6; i++) |
583 |
if(pMB->cbp & (1 << (5 - i))) |
if(pMB->cbp & (1 << (5 - i))) |
584 |
|
#ifdef BIGLUT |
585 |
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
586 |
|
#else |
587 |
|
CodeCoeffInter(bs, &qcoeff[i * 64], scan_tables[0]); |
588 |
|
#endif |
589 |
|
|
590 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
591 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
592 |
|
|
593 |
} |
} |
594 |
|
|
595 |
|
/***************************************************************************** |
596 |
|
* Macro Block bitstream encoding functions |
597 |
|
****************************************************************************/ |
598 |
|
|
599 |
void MBCoding(const MBParam * pParam, |
void |
600 |
|
MBCoding(const FRAMEINFO * frame, |
601 |
MACROBLOCK *pMB, |
MACROBLOCK *pMB, |
602 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
603 |
Bitstream * bs, |
Bitstream * bs, |
604 |
Statistics * pStat) |
Statistics * pStat) |
605 |
{ |
{ |
606 |
|
|
607 |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
if (frame->coding_type == P_VOP) { |
608 |
|
BitstreamPutBit(bs, 0); /* coded */ |
|
if(pParam->coding_type == P_VOP) { |
|
|
if(pMB->cbp == 0 && pMB->mode == MODE_INTER && |
|
|
pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
|
|
{ |
|
|
BitstreamPutBit(bs, 1); // not_coded |
|
|
return; |
|
|
} |
|
|
else |
|
|
BitstreamPutBit(bs, 0); // coded |
|
609 |
} |
} |
610 |
|
|
611 |
if(intra) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
612 |
CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
613 |
else |
else |
614 |
CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
615 |
|
|
616 |
} |
} |
617 |
|
|
|
/*************************************************************** |
|
|
* bframe encoding start |
|
|
***************************************************************/ |
|
|
|
|
|
/* |
|
|
mbtype |
|
|
0 1b direct(h263) mvdb |
|
|
1 01b interpolate mc+q dbquant, mvdf, mvdb |
|
|
2 001b backward mc+q dbquant, mvdb |
|
|
3 0001b forward mc+q dbquant, mvdf |
|
|
*/ |
|
618 |
|
|
619 |
void put_bvop_mbtype(Bitstream * bs, int value) |
void |
620 |
|
MBSkip(Bitstream * bs) |
621 |
{ |
{ |
622 |
switch(value) |
BitstreamPutBit(bs, 1); /* not coded */ |
|
{ |
|
|
case 0 : BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
case 1 : BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 1); |
|
623 |
return; |
return; |
|
|
|
|
case 2 : BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
case 3 : BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
default : ; // invalid! |
|
|
|
|
624 |
} |
} |
625 |
|
|
626 |
} |
/***************************************************************************** |
627 |
|
* decoding stuff starts here |
628 |
|
****************************************************************************/ |
629 |
|
|
630 |
/* |
/* |
631 |
dbquant |
* For IVOP addbits == 0 |
632 |
-2 10b |
* For PVOP addbits == fcode - 1 |
633 |
0 0b |
* For BVOP addbits == max(fcode,bcode) - 1 |
634 |
+2 11b |
* returns true or false |
635 |
*/ |
*/ |
636 |
|
|
637 |
void put_bvop_dbquant(Bitstream *bs, int value) |
int |
638 |
|
check_resync_marker(Bitstream * bs, int addbits) |
639 |
{ |
{ |
640 |
switch (value) |
uint32_t nbits; |
641 |
{ |
uint32_t code; |
642 |
case 0 : BitstreamPutBit(bs, 0); |
uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits; |
|
return; |
|
|
|
|
|
case -2 : BitstreamPutBit(bs, 1); |
|
|
BitstreamPutBit(bs, 0); |
|
|
return; |
|
|
|
|
|
case 2 : BitstreamPutBit(bs, 1); |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
default : ; // invalid |
|
|
} |
|
|
} |
|
643 |
|
|
644 |
|
nbits = BitstreamNumBitsToByteAlign(bs); |
645 |
|
code = BitstreamShowBits(bs, nbits); |
646 |
|
|
647 |
|
if (code == (((uint32_t)1 << (nbits - 1)) - 1)) |
|
void MBCodingBVOP(const MACROBLOCK * mb, |
|
|
const int16_t qcoeff[6*64], |
|
|
const int16_t fcode, |
|
|
const int16_t bcode, |
|
|
Bitstream * bs, |
|
|
Statistics * pStat) |
|
648 |
{ |
{ |
649 |
int i; |
return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER; |
|
|
|
|
/* ------------------------------------------------------------------ |
|
|
when a block is skipped it is decoded DIRECT(0,) |
|
|
hence are interpolated from forward & backward frames |
|
|
------------------------------------------------------------------ */ |
|
|
|
|
|
if (mb->mode == 5) |
|
|
{ |
|
|
BitstreamPutBit(bs, 1); // skipped |
|
|
return; |
|
|
} |
|
|
|
|
|
BitstreamPutBit(bs, 0); // not skipped |
|
|
|
|
|
if (mb->cbp == 0) |
|
|
{ |
|
|
BitstreamPutBit(bs, 1); // cbp == 0 |
|
|
} |
|
|
else |
|
|
{ |
|
|
BitstreamPutBit(bs, 0); // cbp == xxx |
|
650 |
} |
} |
651 |
|
|
652 |
put_bvop_mbtype(bs, mb->mode); |
return 0; |
|
|
|
|
if (mb->cbp) |
|
|
{ |
|
|
BitstreamPutBits(bs, mb->cbp, 6); |
|
|
} |
|
|
|
|
|
if (mb->mode != MODE_DIRECT && mb->cbp != 0) |
|
|
{ |
|
|
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
|
|
{ |
|
|
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
|
|
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
|
|
{ |
|
|
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
|
|
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
|
|
} |
|
|
|
|
|
if (mb->mode == MODE_DIRECT) |
|
|
{ |
|
|
// TODO: direct |
|
|
} |
|
|
|
|
|
for (i = 0; i < 6; i++) |
|
|
{ |
|
|
if (mb->cbp & (1 << (5 - i))) |
|
|
{ |
|
|
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
|
|
} |
|
|
} |
|
653 |
} |
} |
654 |
|
|
655 |
|
|
656 |
|
|
657 |
/*************************************************************** |
int |
658 |
* decoding stuff starts here * |
get_mcbpc_intra(Bitstream * bs) |
|
***************************************************************/ |
|
|
|
|
|
int get_mcbpc_intra(Bitstream * bs) |
|
659 |
{ |
{ |
660 |
|
|
661 |
uint32_t index; |
uint32_t index; |
662 |
|
|
663 |
while((index = BitstreamShowBits(bs, 9)) == 1) |
index = BitstreamShowBits(bs, 9); |
|
BitstreamSkip(bs, 9); |
|
|
|
|
664 |
index >>= 3; |
index >>= 3; |
665 |
|
|
666 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
669 |
|
|
670 |
} |
} |
671 |
|
|
672 |
int get_mcbpc_inter(Bitstream * bs) |
int |
673 |
|
get_mcbpc_inter(Bitstream * bs) |
674 |
{ |
{ |
675 |
|
|
676 |
uint32_t index; |
uint32_t index; |
677 |
|
|
678 |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
|
BitstreamSkip(bs, 9); |
|
679 |
|
|
680 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
681 |
|
|
683 |
|
|
684 |
} |
} |
685 |
|
|
686 |
int get_cbpy(Bitstream * bs, int intra) |
int |
687 |
|
get_cbpy(Bitstream * bs, |
688 |
|
int intra) |
689 |
{ |
{ |
690 |
|
|
691 |
int cbpy; |
int cbpy; |
701 |
|
|
702 |
} |
} |
703 |
|
|
704 |
int get_mv_data(Bitstream * bs) |
int |
705 |
|
get_mv_data(Bitstream * bs) |
706 |
{ |
{ |
707 |
|
|
708 |
uint32_t index; |
uint32_t index; |
712 |
|
|
713 |
index = BitstreamShowBits(bs, 12); |
index = BitstreamShowBits(bs, 12); |
714 |
|
|
715 |
if(index >= 512) |
if (index >= 512) { |
|
{ |
|
716 |
index = (index >> 8) - 2; |
index = (index >> 8) - 2; |
717 |
BitstreamSkip(bs, TMNMVtab0[index].len); |
BitstreamSkip(bs, TMNMVtab0[index].len); |
718 |
return TMNMVtab0[index].code; |
return TMNMVtab0[index].code; |
719 |
} |
} |
720 |
|
|
721 |
if(index >= 128) |
if (index >= 128) { |
|
{ |
|
722 |
index = (index >> 2) - 32; |
index = (index >> 2) - 32; |
723 |
BitstreamSkip(bs, TMNMVtab1[index].len); |
BitstreamSkip(bs, TMNMVtab1[index].len); |
724 |
return TMNMVtab1[index].code; |
return TMNMVtab1[index].code; |
731 |
|
|
732 |
} |
} |
733 |
|
|
734 |
int get_mv(Bitstream * bs, int fcode) |
int |
735 |
|
get_mv(Bitstream * bs, |
736 |
|
int fcode) |
737 |
{ |
{ |
738 |
|
|
739 |
int data; |
int data; |
753 |
|
|
754 |
} |
} |
755 |
|
|
756 |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
int |
757 |
|
get_dc_dif(Bitstream * bs, |
758 |
|
uint32_t dc_size) |
759 |
{ |
{ |
760 |
|
|
761 |
int code = BitstreamGetBits(bs, dc_size); |
int code = BitstreamGetBits(bs, dc_size); |
768 |
|
|
769 |
} |
} |
770 |
|
|
771 |
int get_dc_size_lum(Bitstream * bs) |
int |
772 |
|
get_dc_size_lum(Bitstream * bs) |
773 |
{ |
{ |
774 |
|
|
775 |
int code, i; |
int code, i; |
776 |
|
|
777 |
code = BitstreamShowBits(bs, 11); |
code = BitstreamShowBits(bs, 11); |
778 |
|
|
779 |
for(i = 11; i > 3; i--) { |
for(i = 11; i > 3; i--) { |
790 |
} |
} |
791 |
|
|
792 |
|
|
793 |
int get_dc_size_chrom(Bitstream * bs) |
int |
794 |
|
get_dc_size_chrom(Bitstream * bs) |
795 |
{ |
{ |
796 |
|
|
797 |
uint32_t code, i; |
uint32_t code, i; |
798 |
|
|
799 |
code = BitstreamShowBits(bs, 12); |
code = BitstreamShowBits(bs, 12); |
800 |
|
|
801 |
for(i = 12; i > 2; i--) { |
for(i = 12; i > 2; i--) { |
810 |
|
|
811 |
} |
} |
812 |
|
|
813 |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
/***************************************************************************** |
814 |
|
* Local inlined function to "decode" written vlc codes |
815 |
|
****************************************************************************/ |
816 |
|
|
817 |
|
static __inline int |
818 |
|
get_coeff(Bitstream * bs, |
819 |
|
int *run, |
820 |
|
int *last, |
821 |
|
int intra, |
822 |
|
int short_video_header) |
823 |
|
{ |
824 |
|
|
825 |
|
uint32_t mode; |
826 |
|
int32_t level; |
827 |
|
REVERSE_EVENT *reverse_event; |
828 |
|
|
829 |
|
if (short_video_header) /* inter-VLCs will be used for both intra and inter blocks */ |
830 |
|
intra = 0; |
831 |
|
|
832 |
|
if (BitstreamShowBits(bs, 7) != ESCAPE) { |
833 |
|
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
834 |
|
|
835 |
|
if ((level = reverse_event->event.level) == 0) |
836 |
|
goto error; |
837 |
|
|
838 |
|
*last = reverse_event->event.last; |
839 |
|
*run = reverse_event->event.run; |
840 |
|
|
841 |
|
BitstreamSkip(bs, reverse_event->len); |
842 |
|
|
843 |
|
return BitstreamGetBits(bs, 1) ? -level : level; |
844 |
|
} |
845 |
|
|
846 |
|
BitstreamSkip(bs, 7); |
847 |
|
|
848 |
|
if (short_video_header) { |
849 |
|
/* escape mode 4 - H.263 type, only used if short_video_header = 1 */ |
850 |
|
*last = BitstreamGetBit(bs); |
851 |
|
*run = BitstreamGetBits(bs, 6); |
852 |
|
level = BitstreamGetBits(bs, 8); |
853 |
|
|
854 |
|
if (level == 0 || level == 128) |
855 |
|
DPRINTF(DPRINTF_ERROR, "Illegal LEVEL for ESCAPE mode 4: %d", level); |
856 |
|
|
857 |
|
return (level << 24) >> 24; |
858 |
|
} |
859 |
|
|
860 |
|
mode = BitstreamShowBits(bs, 2); |
861 |
|
|
862 |
|
if (mode < 3) { |
863 |
|
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
864 |
|
|
865 |
|
reverse_event = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
866 |
|
|
867 |
|
if ((level = reverse_event->event.level) == 0) |
868 |
|
goto error; |
869 |
|
|
870 |
|
*last = reverse_event->event.last; |
871 |
|
*run = reverse_event->event.run; |
872 |
|
|
873 |
|
BitstreamSkip(bs, reverse_event->len); |
874 |
|
|
875 |
|
if (mode < 2) /* first escape mode, level is offset */ |
876 |
|
level += max_level[intra][*last][*run]; |
877 |
|
else /* second escape mode, run is offset */ |
878 |
|
*run += max_run[intra][*last][level] + 1; |
879 |
|
|
880 |
|
return BitstreamGetBits(bs, 1) ? -level : level; |
881 |
|
} |
882 |
|
|
883 |
|
/* third escape mode - fixed length codes */ |
884 |
|
BitstreamSkip(bs, 2); |
885 |
|
*last = BitstreamGetBits(bs, 1); |
886 |
|
*run = BitstreamGetBits(bs, 6); |
887 |
|
BitstreamSkip(bs, 1); /* marker */ |
888 |
|
level = BitstreamGetBits(bs, 12); |
889 |
|
BitstreamSkip(bs, 1); /* marker */ |
890 |
|
|
891 |
|
return (level << 20) >> 20; |
892 |
|
|
893 |
|
error: |
894 |
|
*run = VLC_ERROR; |
895 |
|
return 0; |
896 |
|
} |
897 |
|
|
898 |
|
/***************************************************************************** |
899 |
|
* MB reading functions |
900 |
|
****************************************************************************/ |
901 |
|
|
902 |
|
void |
903 |
|
get_intra_block(Bitstream * bs, |
904 |
|
int16_t * block, |
905 |
|
int direction, |
906 |
|
int coeff) |
907 |
{ |
{ |
908 |
|
|
909 |
const uint16_t * scan = scan_tables[ direction ]; |
const uint16_t * scan = scan_tables[ direction ]; |
911 |
int run; |
int run; |
912 |
int last; |
int last; |
913 |
|
|
914 |
do |
do { |
|
{ |
|
915 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
916 |
if (run == -1) |
if (run == -1) { |
917 |
{ |
DPRINTF(DPRINTF_DEBUG, "fatal: invalid run"); |
|
DEBUG("fatal: invalid run"); |
|
918 |
break; |
break; |
919 |
} |
} |
920 |
coeff += run; |
coeff += run; |
921 |
block[ scan[coeff] ] = level; |
block[ scan[coeff] ] = level; |
922 |
if (level < -127 || level > 127) |
|
923 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
924 |
DEBUG1("warning: intra_overflow", level); |
/*DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); */ |
925 |
|
|
926 |
|
if (level < -2047 || level > 2047) { |
927 |
|
DPRINTF(DPRINTF_DEBUG, "warning: intra_overflow: %d", level); |
928 |
} |
} |
929 |
coeff++; |
coeff++; |
930 |
} while (!last); |
} while (!last); |
931 |
|
|
932 |
} |
} |
933 |
|
|
934 |
void get_inter_block(Bitstream * bs, int16_t * block) |
void |
935 |
|
get_inter_block(Bitstream * bs, |
936 |
|
int16_t * block) |
937 |
{ |
{ |
938 |
|
|
939 |
const uint16_t * scan = scan_tables[0]; |
const uint16_t * scan = scan_tables[0]; |
943 |
int last; |
int last; |
944 |
|
|
945 |
p = 0; |
p = 0; |
946 |
do |
do { |
|
{ |
|
947 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
948 |
if (run == -1) |
if (run == -1) { |
949 |
{ |
DPRINTF(DPRINTF_ERROR, "fatal: invalid run"); |
|
DEBUG("fatal: invalid run"); |
|
950 |
break; |
break; |
951 |
} |
} |
952 |
p += run; |
p += run; |
953 |
|
|
954 |
block[ scan[p] ] = level; |
block[ scan[p] ] = level; |
955 |
if (level < -127 || level > 127) |
|
956 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
957 |
DEBUG1("warning: inter_overflow", level); |
|
958 |
|
if (level < -2047 || level > 2047) { |
959 |
|
DPRINTF(DPRINTF_DEBUG, "warning: inter_overflow: %d", level); |
960 |
} |
} |
961 |
p++; |
p++; |
962 |
} while (!last); |
} while (!last); |