1 |
|
/****************************************************************************** |
2 |
|
* * |
3 |
|
* This file is part of XviD, a free MPEG-4 video encoder/decoder * |
4 |
|
* * |
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 * |
7 |
|
* software module in hardware or software products are advised that its * |
8 |
|
* use may infringe existing patents or copyrights, and any such use * |
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 * |
11 |
|
* companies, will have no liability for use of this software or * |
12 |
|
* modifications or derivatives thereof. * |
13 |
|
* * |
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 * |
16 |
|
* the Free Software Foundation; either version 2 of the License, or * |
17 |
|
* (at your option) any later version. * |
18 |
|
* * |
19 |
|
* XviD is distributed in the hope that it will be useful, but * |
20 |
|
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
21 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
22 |
|
* GNU General Public License for more details. * |
23 |
|
* * |
24 |
|
* You should have received a copy of the GNU General Public License * |
25 |
|
* along with this program; if not, write to the Free Software * |
26 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
27 |
|
* * |
28 |
|
******************************************************************************/ |
29 |
|
|
30 |
|
/****************************************************************************** |
31 |
|
* * |
32 |
|
* mbcoding.c * |
33 |
|
* * |
34 |
|
* Copyright (C) 2002 - Michael Militzer <isibaar@xvid.org> * |
35 |
|
* * |
36 |
|
* For more information visit the XviD homepage: http://www.xvid.org * |
37 |
|
* * |
38 |
|
******************************************************************************/ |
39 |
|
|
40 |
|
/****************************************************************************** |
41 |
|
* * |
42 |
|
* Revision history: * |
43 |
|
* * |
44 |
|
* 28.10.2002 GMC support - gruel * |
45 |
|
* 28.06.2002 added check_resync_marker() * |
46 |
|
* 14.04.2002 bframe encoding * |
47 |
|
* 08.03.2002 initial version; isibaar * |
48 |
|
* * |
49 |
|
******************************************************************************/ |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
#include <stdlib.h> |
54 |
#include "../portab.h" |
#include "../portab.h" |
55 |
#include "bitstream.h" |
#include "bitstream.h" |
56 |
#include "zigzag.h" |
#include "zigzag.h" |
57 |
#include "vlc_codes.h" |
#include "vlc_codes.h" |
58 |
|
#include "mbcoding.h" |
59 |
|
|
60 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
61 |
|
|
|
#define ESCAPE 7167 |
|
62 |
#define ABS(X) (((X)>0)?(X):-(X)) |
#define ABS(X) (((X)>0)?(X):-(X)) |
63 |
#define CLIP(X,A) (X > A) ? (A) : (X) |
#define CLIP(X,A) (X > A) ? (A) : (X) |
64 |
|
|
65 |
static VLC *DCT3D[2]; |
VLC intra_table[524032]; |
66 |
|
VLC inter_table[524032]; |
67 |
|
|
68 |
|
VLC DCT3Dintra[4096]; |
69 |
|
VLC DCT3Dinter[4096]; |
70 |
|
|
71 |
|
/* not really MB related, but VLCs are only available here */ |
72 |
|
void bs_put_spritetrajectory(Bitstream * bs, const int val) |
73 |
|
{ |
74 |
|
const int code = sprite_trajectory_code[val+16384].code; |
75 |
|
const int len = sprite_trajectory_code[val+16384].len; |
76 |
|
const int code2 = sprite_trajectory_len[len].code; |
77 |
|
const int len2 = sprite_trajectory_len[len].len; |
78 |
|
|
79 |
|
// printf("GMC=%d Code/Len = %d / %d ",val, code,len); |
80 |
|
// printf("Code2 / Len2 = %d / %d \n",code2,len2); |
81 |
|
|
82 |
VLC *intra_table, *inter_table; |
BitstreamPutBits(bs, code2, len2); |
83 |
static short clip_table[4096]; |
if (len) BitstreamPutBits(bs, code, len); |
84 |
|
} |
85 |
|
|
86 |
void create_vlc_tables(void) |
int bs_get_spritetrajectory(Bitstream * bs) |
87 |
|
{ |
88 |
|
int i; |
89 |
|
for (i = 0; i < 12; i++) |
90 |
{ |
{ |
91 |
|
if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code) |
92 |
|
{ |
93 |
|
BitstreamSkip(bs, sprite_trajectory_len[i].len); |
94 |
|
return i; |
95 |
|
} |
96 |
|
} |
97 |
|
return -1; |
98 |
|
} |
99 |
|
|
100 |
|
void |
101 |
|
init_vlc_tables(void) |
102 |
|
{ |
103 |
|
|
104 |
int32_t k, l, i, intra, last; |
int32_t k, l, i, intra, last; |
105 |
VLC *vlc[2]; |
VLC *vlc[2]; |
106 |
VLC **coeff_ptr; |
VLC **coeff_ptr; |
107 |
VLC *vlc1, *vlc2; |
VLC *vlc1, *vlc2; |
108 |
|
|
|
VLC *DCT3Dintra; |
|
|
VLC *DCT3Dinter; |
|
|
|
|
|
DCT3Dintra = (VLC *) malloc(sizeof(VLC) * 4096); |
|
|
DCT3Dinter = (VLC *) malloc(sizeof(VLC) * 4096); |
|
|
|
|
109 |
vlc1 = DCT3Dintra; |
vlc1 = DCT3Dintra; |
110 |
vlc2 = DCT3Dinter; |
vlc2 = DCT3Dinter; |
111 |
|
|
112 |
vlc[0] = intra_table = (VLC *) malloc(128 * 511 * sizeof(VLC)); |
vlc[0] = intra_table; |
113 |
vlc[1] = inter_table = (VLC *) malloc(128 * 511 * sizeof(VLC)); |
vlc[1] = inter_table; |
|
|
|
|
// 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; |
|
|
} |
|
114 |
|
|
115 |
// generate intra/inter vlc lookup table |
// generate encoding vlc lookup tables |
116 |
|
// the lookup table idea is taken from the excellent fame project by Vivien Chapellier |
117 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
118 |
intra = i % 2; |
intra = i % 2; |
119 |
last = i >> 1; |
last = i / 2; |
120 |
|
|
121 |
coeff_ptr = coeff_vlc[last + (intra << 1)]; |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
122 |
|
|
123 |
for(k = -255; k < 256; k++) { // level |
for (k = -2047; k < 2048; k++) { // level |
124 |
char *max_level_ptr = max_level[last + (intra << 1)]; |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
125 |
char *max_run_ptr = max_run[last + (intra << 1)]; |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
126 |
|
|
127 |
for(l = 0; l < 64; l++) { // run |
for(l = 0; l < 64; l++) { // run |
128 |
int32_t level = k, run = l; |
int32_t level = k; |
129 |
|
ptr_t run = l; |
130 |
|
|
131 |
if(abs(level) <= max_level_ptr[run] && run <= max_run_ptr[abs(level)]) { |
if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
132 |
|
|
|
if(level > 0) { |
|
|
vlc[intra]->code = (coeff_ptr[run][level - 1].code) << 1; |
|
|
vlc[intra]->len = coeff_ptr[run][level - 1].len + 1; |
|
|
} |
|
|
else if(level < 0) { |
|
|
vlc[intra]->code = ((coeff_ptr[run][-level - 1].code) << 1) + 1; |
|
|
vlc[intra]->len = coeff_ptr[run][-level - 1].len + 1; |
|
|
} |
|
|
else { |
|
133 |
vlc[intra]->code = 0; |
vlc[intra]->code = 0; |
134 |
vlc[intra]->len = 0; |
vlc[intra]->len = 0; |
135 |
} |
goto loop_end; |
136 |
} else { |
} else { |
137 |
if(level > 0) |
if (level > 0) // correct level |
138 |
level -= max_level_ptr[run]; |
level -= max_level_ptr[run]; |
139 |
else |
else |
140 |
level += max_level_ptr[run]; |
level += max_level_ptr[run]; |
141 |
|
|
142 |
if(abs(level) <= max_level_ptr[run] && |
if ((abs(level) <= max_level_ptr[run]) && |
143 |
run <= max_run_ptr[abs(level)]) { |
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
144 |
|
|
|
if(level > 0) { |
|
|
vlc[intra]->code = (0x06 << (coeff_ptr[run][level - 1].len + 1)) | |
|
|
(coeff_ptr[run][level - 1].code << 1); |
|
|
vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 8; |
|
|
} |
|
|
else if(level < 0) { |
|
|
vlc[intra]->code = (0x06 << (coeff_ptr[run][-level - 1].len + 1)) | |
|
|
((coeff_ptr[run][-level - 1].code << 1) + 1); |
|
|
vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 8; |
|
|
} |
|
|
else { |
|
145 |
vlc[intra]->code = 0x06; |
vlc[intra]->code = 0x06; |
146 |
vlc[intra]->len = 8; |
vlc[intra]->len = 8; |
147 |
|
goto loop_end; |
148 |
} |
} |
149 |
} else { |
|
150 |
if(level > 0) |
if (level > 0) // still here? |
151 |
level += max_level_ptr[run]; |
level += max_level_ptr[run]; // restore level |
152 |
else |
else |
153 |
level -= max_level_ptr[run]; |
level -= max_level_ptr[run]; |
154 |
|
|
155 |
run -= max_run_ptr[abs(level)] + 1; |
run -= max_run_ptr[abs(level)] + 1; // and change run |
156 |
|
|
157 |
if(abs(level) <= max_level_ptr[run] && |
if ((abs(level) <= max_level_ptr[run]) && |
158 |
run <= max_run_ptr[abs(level)]) { |
(run <= (uint32_t) max_run_ptr[abs(level)])) { |
159 |
|
|
|
if(level > 0) { |
|
|
vlc[intra]->code = (0x0e << (coeff_ptr[run][level - 1].len + 1)) | |
|
|
(coeff_ptr[run][level - 1].code << 1); |
|
|
vlc[intra]->len = (coeff_ptr[run][level - 1].len + 1) + 9; |
|
|
} |
|
|
else if(level < 0) { |
|
|
vlc[intra]->code = (0x0e << (coeff_ptr[run][-level - 1].len + 1)) | |
|
|
((coeff_ptr[run][-level - 1].code << 1) + 1); |
|
|
vlc[intra]->len = (coeff_ptr[run][-level - 1].len + 1) + 9; |
|
|
} |
|
|
else { |
|
160 |
vlc[intra]->code = 0x0e; |
vlc[intra]->code = 0x0e; |
161 |
vlc[intra]->len = 9; |
vlc[intra]->len = 9; |
162 |
|
goto loop_end; |
163 |
} |
} |
|
} else { |
|
|
if(level != 0) |
|
164 |
run += max_run_ptr[abs(level)] + 1; |
run += max_run_ptr[abs(level)] + 1; |
165 |
else |
} |
|
run++; |
|
166 |
|
|
167 |
vlc[intra]->code = (uint32_t) ((0x1e + last) << 20) | |
vlc[intra]->code = |
168 |
(l << 14) | (1 << 13) | ((k & 0xfff) << 1) | 1; |
(uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) | |
169 |
|
((k & 0xfff) << 1) | 1; |
170 |
|
|
171 |
vlc[intra]->len = 30; |
vlc[intra]->len = 30; |
172 |
|
vlc[intra]++; |
173 |
|
continue; |
174 |
|
|
175 |
|
loop_end: |
176 |
|
if (level != 0) { |
177 |
|
vlc[intra]->code = |
178 |
|
(vlc[intra]-> |
179 |
|
code << (coeff_ptr[run][abs(level) - 1].len + |
180 |
|
1)) | (coeff_ptr[run][abs(level) - |
181 |
|
1].code << 1); |
182 |
|
vlc[intra]->len = |
183 |
|
(coeff_ptr[run][abs(level) - 1].len + 1) + |
184 |
|
vlc[intra]->len; |
185 |
|
|
186 |
|
if (level < 0) |
187 |
|
vlc[intra]->code += 1; |
188 |
} |
} |
189 |
} |
|
|
} |
|
190 |
vlc[intra]++; |
vlc[intra]++; |
191 |
} |
} |
192 |
} |
} |
193 |
} |
} |
|
intra_table += 64*255; // center vlc tables |
|
|
inter_table += 64*255; // center vlc tables |
|
194 |
|
|
195 |
for(i = 0; i < 4096; i++) { |
for(i = 0; i < 4096; i++) { |
196 |
if(i >= 512) { |
if(i >= 512) { |
197 |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
198 |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
199 |
} |
} else if (i >= 128) { |
|
else if(i >= 128) { |
|
200 |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
201 |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
202 |
} |
} else if (i >= 8) { |
|
else if(i >= 8) { |
|
203 |
*vlc1 = DCT3Dtab5[i - 8]; |
*vlc1 = DCT3Dtab5[i - 8]; |
204 |
*vlc2 = DCT3Dtab2[i - 8]; |
*vlc2 = DCT3Dtab2[i - 8]; |
205 |
} |
} else { |
|
else { |
|
206 |
*vlc1 = ERRtab[i]; |
*vlc1 = ERRtab[i]; |
207 |
*vlc2 = ERRtab[i]; |
*vlc2 = ERRtab[i]; |
208 |
} |
} |
213 |
DCT3D[0] = DCT3Dinter; |
DCT3D[0] = DCT3Dinter; |
214 |
DCT3D[1] = DCT3Dintra; |
DCT3D[1] = DCT3Dintra; |
215 |
|
|
|
} |
|
216 |
|
|
217 |
void destroy_vlc_tables(void) { |
/* init sprite_trajectory tables */ |
218 |
|
/* even if GMC is not specified (it might be used later...) */ |
219 |
|
|
220 |
if(intra_table != NULL && inter_table != NULL) { |
sprite_trajectory_code[0+16384].code = 0; |
221 |
intra_table -= 64*255; // uncenter vlc tables |
sprite_trajectory_code[0+16384].len = 0; |
222 |
inter_table -= 64*255; // uncenter vlc tables |
for (k=0;k<14;k++) |
223 |
|
{ |
224 |
|
int limit = (1<<k); |
225 |
|
|
226 |
free(intra_table); |
for (i=-(2*limit-1); i<= -limit; i++) |
227 |
free(inter_table); |
{ |
228 |
|
sprite_trajectory_code[i+16384].code = (2*limit-1)+i; |
229 |
|
sprite_trajectory_code[i+16384].len = k+1; |
230 |
} |
} |
231 |
|
|
232 |
if(DCT3D[0] != NULL && DCT3D[1] != NULL) { |
for (i=limit; i<= 2*limit-1; i++) |
233 |
free(DCT3D[0]); |
{ |
234 |
free(DCT3D[1]); |
sprite_trajectory_code[i+16384].code = i; |
235 |
|
sprite_trajectory_code[i+16384].len = k+1; |
236 |
|
} |
237 |
} |
} |
|
|
|
238 |
} |
} |
239 |
|
|
240 |
static __inline void CodeVector(Bitstream *bs, int16_t value, int16_t f_code, Statistics *pStat) |
static __inline void |
241 |
|
CodeVector(Bitstream * bs, |
242 |
|
int32_t value, |
243 |
|
int32_t f_code, |
244 |
|
Statistics * pStat) |
245 |
{ |
{ |
246 |
|
|
247 |
const int scale_factor = 1 << (f_code - 1); |
const int scale_factor = 1 << (f_code - 1); |
248 |
const int cmp = scale_factor << 5; |
const int cmp = scale_factor << 5; |
249 |
|
|
256 |
pStat->iMvSum += value * value; |
pStat->iMvSum += value * value; |
257 |
pStat->iMvCount++; |
pStat->iMvCount++; |
258 |
|
|
259 |
if (value == 0) |
if (value == 0) { |
260 |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
BitstreamPutBits(bs, mb_motion_table[32].code, |
261 |
else { |
mb_motion_table[32].len); |
262 |
|
} else { |
263 |
uint16_t length, code, mv_res, sign; |
uint16_t length, code, mv_res, sign; |
264 |
|
|
265 |
length = 16 << f_code; |
length = 16 << f_code; |
283 |
code = -code; |
code = -code; |
284 |
|
|
285 |
code += 32; |
code += 32; |
286 |
BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len); |
BitstreamPutBits(bs, mb_motion_table[code].code, |
287 |
|
mb_motion_table[code].len); |
288 |
|
|
289 |
if(f_code) |
if(f_code) |
290 |
BitstreamPutBits(bs, mv_res, f_code); |
BitstreamPutBits(bs, mv_res, f_code); |
291 |
} |
} |
292 |
|
|
293 |
} |
} |
294 |
|
|
295 |
|
|
296 |
static __inline void CodeCoeff(Bitstream *bs, int16_t qcoeff[64], VLC *table, |
static __inline void |
297 |
const uint16_t *zigzag, uint16_t intra) { |
CodeCoeff(Bitstream * bs, |
298 |
|
const int16_t qcoeff[64], |
299 |
|
VLC * table, |
300 |
|
const uint16_t * zigzag, |
301 |
|
uint16_t intra) |
302 |
|
{ |
303 |
|
|
304 |
uint32_t j, last; |
uint32_t j, last; |
305 |
short v; |
short v; |
306 |
VLC *vlc; |
VLC *vlc; |
307 |
|
|
308 |
j = intra; |
j = intra; |
309 |
last = 1 + intra; |
last = intra; |
310 |
|
|
311 |
while((v = qcoeff[zigzag[j++]]) == 0); |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
312 |
|
j++; |
313 |
|
|
314 |
do { |
do { |
315 |
|
vlc = table + 64 * 2047 + (v << 6) + j - last; |
316 |
|
last = ++j; |
317 |
|
|
318 |
// count zeroes |
// count zeroes |
319 |
vlc = table + (clip_table[2048+v] << 6) + j - last; |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
320 |
last = j + 1; |
j++; |
|
while(j < 64 && (v = qcoeff[zigzag[j++]]) == 0); |
|
321 |
|
|
322 |
// write code |
// write code |
323 |
if(j != 64) { |
if(j != 64) { |
324 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
325 |
} else { |
} else { |
326 |
vlc += 64*511; |
vlc += 64 * 4095; |
327 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
328 |
break; |
break; |
329 |
} |
} |
330 |
} while(1); |
} while(1); |
331 |
|
|
332 |
} |
} |
333 |
|
|
334 |
|
|
335 |
static void CodeBlockIntra(const MBParam * pParam, const MACROBLOCK *pMB, |
static __inline void |
336 |
int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) |
CodeBlockIntra(const FRAMEINFO * const frame, |
337 |
|
const MACROBLOCK * pMB, |
338 |
|
int16_t qcoeff[6 * 64], |
339 |
|
Bitstream * bs, |
340 |
|
Statistics * pStat) |
341 |
{ |
{ |
342 |
|
|
343 |
uint32_t i, mcbpc, cbpy, bits; |
uint32_t i, mcbpc, cbpy, bits; |
344 |
|
|
|
mcbpc = pMB->cbp & 3; |
|
345 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
346 |
|
|
347 |
// write mcbpc |
// write mcbpc |
348 |
if(pParam->coding_type == I_VOP) |
if (frame->coding_type == I_VOP) { |
349 |
BitstreamPutBits(bs, mcbpc_I[mcbpc].code, mcbpc_I[mcbpc].len); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
350 |
else |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
351 |
BitstreamPutBits(bs, mcbpc_P_intra[mcbpc].code, mcbpc_P_intra[mcbpc].len); |
mcbpc_intra_tab[mcbpc].len); |
352 |
|
} else { |
353 |
|
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
354 |
|
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
355 |
|
mcbpc_inter_tab[mcbpc].len); |
356 |
|
} |
357 |
|
|
358 |
// ac prediction flag |
// ac prediction flag |
359 |
if(pMB->acpred_directions[0]) |
if(pMB->acpred_directions[0]) |
368 |
if(pMB->mode == MODE_INTRA_Q) |
if(pMB->mode == MODE_INTRA_Q) |
369 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
370 |
|
|
371 |
|
// write interlacing |
372 |
|
if (frame->global_flags & XVID_INTERLACING) { |
373 |
|
BitstreamPutBit(bs, pMB->field_dct); |
374 |
|
} |
375 |
// code block coeffs |
// code block coeffs |
376 |
for(i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
|
{ |
|
377 |
if(i < 4) |
if(i < 4) |
378 |
BitstreamPutBits(bs, dcy_tab[qcoeff[i][0] + 255].code, |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
379 |
dcy_tab[qcoeff[i][0] + 255].len); |
dcy_tab[qcoeff[i * 64 + 0] + 255].len); |
380 |
else |
else |
381 |
BitstreamPutBits(bs, dcc_tab[qcoeff[i][0] + 255].code, |
BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code, |
382 |
dcc_tab[qcoeff[i][0] + 255].len); |
dcc_tab[qcoeff[i * 64 + 0] + 255].len); |
383 |
|
|
384 |
|
if (pMB->cbp & (1 << (5 - i))) { |
385 |
|
const uint16_t *scan_table = |
386 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
387 |
|
scan_tables[2] : scan_tables[pMB->acpred_directions[i]]; |
388 |
|
|
|
if(pMB->cbp & (1 << (5 - i))) |
|
|
{ |
|
389 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
390 |
|
|
391 |
CodeCoeff(bs, qcoeff[i], intra_table, scan_tables[pMB->acpred_directions[i]], 1); |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1); |
392 |
|
|
393 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
394 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
395 |
} |
} |
396 |
} |
} |
397 |
|
|
398 |
} |
} |
399 |
|
|
400 |
|
|
401 |
static void CodeBlockInter(const MBParam * pParam, const MACROBLOCK *pMB, |
static void |
402 |
int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) |
CodeBlockInter(const FRAMEINFO * const frame, |
403 |
|
const MACROBLOCK * pMB, |
404 |
|
int16_t qcoeff[6 * 64], |
405 |
|
Bitstream * bs, |
406 |
|
Statistics * pStat) |
407 |
{ |
{ |
408 |
|
|
409 |
int32_t i; |
int32_t i; |
410 |
uint32_t bits, mcbpc, cbpy; |
uint32_t bits, mcbpc, cbpy; |
411 |
|
int mcsel=0; |
412 |
|
|
413 |
mcbpc = pMB->cbp & 3; |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
414 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
415 |
|
|
416 |
// write mcbpc |
// write mcbpc |
417 |
if(pMB->mode == MODE_INTER4V) |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
418 |
BitstreamPutBits(bs, mcbpc_P_inter4v[mcbpc].code, mcbpc_P_inter4v[mcbpc].len); |
mcbpc_inter_tab[mcbpc].len); |
419 |
else |
|
420 |
BitstreamPutBits(bs, mcbpc_P_inter[mcbpc].code, mcbpc_P_inter[mcbpc].len); |
if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) ) |
421 |
|
{ |
422 |
|
if (frame->quarterpel) { |
423 |
|
if ( (pMB->qmvs[0].x == frame->GMC_MV.x) && (pMB->qmvs[0].y == frame->GMC_MV.y) ) |
424 |
|
mcsel=1; |
425 |
|
} else { |
426 |
|
if ( (pMB->mvs[0].x == frame->GMC_MV.x) && (pMB->mvs[0].y == frame->GMC_MV.y) ) |
427 |
|
mcsel=1; |
428 |
|
} |
429 |
|
BitstreamPutBit(bs, mcsel); // mcsel: '0'=local motion, '1'=GMC |
430 |
|
} |
431 |
|
|
432 |
// write cbpy |
// write cbpy |
433 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
436 |
if(pMB->mode == MODE_INTER_Q) |
if(pMB->mode == MODE_INTER_Q) |
437 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
438 |
|
|
439 |
// code motion vector(s) |
// interlacing |
440 |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
if (frame->global_flags & XVID_INTERLACING) { |
441 |
{ |
if (pMB->cbp) { |
442 |
CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat); |
BitstreamPutBit(bs, pMB->field_dct); |
443 |
CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat); |
DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct); |
444 |
|
} |
445 |
|
|
446 |
|
// if inter block, write field ME flag |
447 |
|
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
448 |
|
BitstreamPutBit(bs, pMB->field_pred); |
449 |
|
DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred); |
450 |
|
|
451 |
|
// write field prediction references |
452 |
|
if (pMB->field_pred) { |
453 |
|
BitstreamPutBit(bs, pMB->field_for_top); |
454 |
|
BitstreamPutBit(bs, pMB->field_for_bot); |
455 |
|
} |
456 |
|
} |
457 |
|
} |
458 |
|
// code motion vector(s) if motion is local |
459 |
|
if (mcsel==0) |
460 |
|
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
461 |
|
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
462 |
|
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
463 |
} |
} |
464 |
|
|
465 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
467 |
// code block coeffs |
// code block coeffs |
468 |
for(i = 0; i < 6; i++) |
for(i = 0; i < 6; i++) |
469 |
if(pMB->cbp & (1 << (5 - i))) |
if(pMB->cbp & (1 << (5 - i))) |
470 |
CodeCoeff(bs, qcoeff[i], inter_table, scan_tables[0], 0); |
{ |
471 |
|
const uint16_t *scan_table = |
472 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
473 |
|
scan_tables[2] : scan_tables[0]; |
474 |
|
|
475 |
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0); |
476 |
|
} |
477 |
|
|
478 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
479 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
480 |
} |
} |
481 |
|
|
482 |
|
|
483 |
void MBCoding(const MBParam * pParam, MACROBLOCK *pMB, |
void |
484 |
int16_t qcoeff[][64], |
MBCoding(const FRAMEINFO * const frame, |
485 |
Bitstream * bs, Statistics * pStat) |
MACROBLOCK * pMB, |
486 |
|
int16_t qcoeff[6 * 64], |
487 |
|
Bitstream * bs, |
488 |
|
Statistics * pStat) |
489 |
{ |
{ |
490 |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
if (frame->coding_type != I_VOP) |
491 |
|
BitstreamPutBit(bs, 0); // not_coded |
492 |
|
|
493 |
if(pParam->coding_type == P_VOP) { |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
494 |
if(pMB->cbp == 0 && pMB->mode == MODE_INTER && |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
495 |
pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
else |
496 |
|
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
497 |
|
|
498 |
|
} |
499 |
|
|
500 |
|
/* |
501 |
|
// moved to mbcoding.h so that in can be 'static __inline' |
502 |
|
void |
503 |
|
MBSkip(Bitstream * bs) |
504 |
{ |
{ |
505 |
BitstreamPutBit(bs, 1); // not_coded |
BitstreamPutBit(bs, 1); // not coded |
506 |
|
} |
507 |
|
*/ |
508 |
|
|
509 |
|
/*************************************************************** |
510 |
|
* bframe encoding start |
511 |
|
***************************************************************/ |
512 |
|
|
513 |
|
/* |
514 |
|
mbtype |
515 |
|
0 1b direct(h263) mvdb |
516 |
|
1 01b interpolate mc+q dbquant, mvdf, mvdb |
517 |
|
2 001b backward mc+q dbquant, mvdb |
518 |
|
3 0001b forward mc+q dbquant, mvdf |
519 |
|
*/ |
520 |
|
|
521 |
|
static __inline void |
522 |
|
put_bvop_mbtype(Bitstream * bs, |
523 |
|
int value) |
524 |
|
{ |
525 |
|
switch (value) { |
526 |
|
case MODE_FORWARD: |
527 |
|
BitstreamPutBit(bs, 0); |
528 |
|
case MODE_BACKWARD: |
529 |
|
BitstreamPutBit(bs, 0); |
530 |
|
case MODE_INTERPOLATE: |
531 |
|
BitstreamPutBit(bs, 0); |
532 |
|
case MODE_DIRECT: |
533 |
|
BitstreamPutBit(bs, 1); |
534 |
|
default: |
535 |
|
break; |
536 |
|
} |
537 |
|
} |
538 |
|
|
539 |
|
/* |
540 |
|
dbquant |
541 |
|
-2 10b |
542 |
|
0 0b |
543 |
|
+2 11b |
544 |
|
*/ |
545 |
|
|
546 |
|
static __inline void |
547 |
|
put_bvop_dbquant(Bitstream * bs, |
548 |
|
int value) |
549 |
|
{ |
550 |
|
switch (value) { |
551 |
|
case 0: |
552 |
|
BitstreamPutBit(bs, 0); |
553 |
return; |
return; |
554 |
|
|
555 |
|
case -2: |
556 |
|
BitstreamPutBit(bs, 1); |
557 |
|
BitstreamPutBit(bs, 0); |
558 |
|
return; |
559 |
|
|
560 |
|
case 2: |
561 |
|
BitstreamPutBit(bs, 1); |
562 |
|
BitstreamPutBit(bs, 1); |
563 |
|
return; |
564 |
|
|
565 |
|
default:; // invalid |
566 |
} |
} |
|
else |
|
|
BitstreamPutBit(bs, 0); // coded |
|
567 |
} |
} |
568 |
|
|
569 |
if(intra) |
|
570 |
CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); |
|
571 |
else |
void |
572 |
CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); |
MBCodingBVOP(const MACROBLOCK * mb, |
573 |
|
const int16_t qcoeff[6 * 64], |
574 |
|
const int32_t fcode, |
575 |
|
const int32_t bcode, |
576 |
|
Bitstream * bs, |
577 |
|
Statistics * pStat, |
578 |
|
int direction) |
579 |
|
{ |
580 |
|
int vcode = fcode; |
581 |
|
unsigned int i; |
582 |
|
|
583 |
|
/* ------------------------------------------------------------------ |
584 |
|
when a block is skipped it is decoded DIRECT(0,0) |
585 |
|
hence is interpolated from forward & backward frames |
586 |
|
------------------------------------------------------------------ */ |
587 |
|
|
588 |
|
if (mb->mode == MODE_DIRECT_NONE_MV) { |
589 |
|
BitstreamPutBit(bs, 1); // skipped |
590 |
|
return; |
591 |
} |
} |
592 |
|
|
593 |
|
BitstreamPutBit(bs, 0); // not skipped |
594 |
|
|
595 |
|
if (mb->cbp == 0) { |
596 |
|
BitstreamPutBit(bs, 1); // cbp == 0 |
597 |
|
} else { |
598 |
|
BitstreamPutBit(bs, 0); // cbp == xxx |
599 |
|
} |
600 |
|
|
601 |
|
put_bvop_mbtype(bs, mb->mode); |
602 |
|
|
603 |
|
if (mb->cbp) { |
604 |
|
BitstreamPutBits(bs, mb->cbp, 6); |
605 |
|
} |
606 |
|
|
607 |
|
if (mb->mode != MODE_DIRECT && mb->cbp != 0) { |
608 |
|
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
609 |
|
} |
610 |
|
|
611 |
|
switch (mb->mode) { |
612 |
|
case MODE_INTERPOLATE: |
613 |
|
CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode |
614 |
|
CodeVector(bs, mb->pmvs[1].y, vcode, pStat); |
615 |
|
case MODE_BACKWARD: |
616 |
|
vcode = bcode; |
617 |
|
case MODE_FORWARD: |
618 |
|
CodeVector(bs, mb->pmvs[0].x, vcode, pStat); |
619 |
|
CodeVector(bs, mb->pmvs[0].y, vcode, pStat); |
620 |
|
break; |
621 |
|
case MODE_DIRECT: |
622 |
|
CodeVector(bs, mb->pmvs[3].x, 1, pStat); // fcode is always 1 for delta vector |
623 |
|
CodeVector(bs, mb->pmvs[3].y, 1, pStat); // prediction is always (0,0) |
624 |
|
default: break; |
625 |
|
} |
626 |
|
|
627 |
|
for (i = 0; i < 6; i++) { |
628 |
|
if (mb->cbp & (1 << (5 - i))) { |
629 |
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0); |
630 |
|
} |
631 |
|
} |
632 |
|
} |
633 |
|
|
634 |
|
|
635 |
|
|
636 |
/*************************************************************** |
/*************************************************************** |
637 |
* decoding stuff starts here * |
* decoding stuff starts here * |
638 |
***************************************************************/ |
***************************************************************/ |
639 |
|
|
640 |
int get_mcbpc_intra(Bitstream * bs) |
|
641 |
|
// for IVOP addbits == 0 |
642 |
|
// for PVOP addbits == fcode - 1 |
643 |
|
// for BVOP addbits == max(fcode,bcode) - 1 |
644 |
|
// returns true or false |
645 |
|
int |
646 |
|
check_resync_marker(Bitstream * bs, int addbits) |
647 |
|
{ |
648 |
|
uint32_t nbits; |
649 |
|
uint32_t code; |
650 |
|
uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits; |
651 |
|
|
652 |
|
nbits = BitstreamNumBitsToByteAlign(bs); |
653 |
|
code = BitstreamShowBits(bs, nbits); |
654 |
|
|
655 |
|
if (code == (((uint32_t)1 << (nbits - 1)) - 1)) |
656 |
{ |
{ |
657 |
uint32_t index; |
return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER; |
658 |
|
} |
659 |
|
|
660 |
|
return 0; |
661 |
|
} |
662 |
|
|
663 |
|
|
|
while((index = BitstreamShowBits(bs, 9)) == 1) |
|
|
BitstreamSkip(bs, 9); |
|
664 |
|
|
665 |
|
int |
666 |
|
get_mcbpc_intra(Bitstream * bs) |
667 |
|
{ |
668 |
|
|
669 |
|
uint32_t index; |
670 |
|
|
671 |
|
index = BitstreamShowBits(bs, 9); |
672 |
index >>= 3; |
index >>= 3; |
673 |
|
|
674 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
675 |
|
|
676 |
return mcbpc_intra_table[index].code; |
return mcbpc_intra_table[index].code; |
677 |
|
|
678 |
} |
} |
679 |
|
|
680 |
int get_mcbpc_inter(Bitstream * bs) |
int |
681 |
|
get_mcbpc_inter(Bitstream * bs) |
682 |
{ |
{ |
683 |
|
|
684 |
uint32_t index; |
uint32_t index; |
685 |
|
|
686 |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
|
BitstreamSkip(bs, 9); |
|
687 |
|
|
688 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
689 |
|
|
690 |
return mcbpc_inter_table[index].code; |
return mcbpc_inter_table[index].code; |
691 |
|
|
692 |
} |
} |
693 |
|
|
694 |
int get_cbpy(Bitstream * bs, int intra) |
int |
695 |
|
get_cbpy(Bitstream * bs, |
696 |
|
int intra) |
697 |
{ |
{ |
698 |
|
|
699 |
int cbpy; |
int cbpy; |
700 |
uint32_t index = BitstreamShowBits(bs, 6); |
uint32_t index = BitstreamShowBits(bs, 6); |
701 |
|
|
706 |
cbpy = 15 - cbpy; |
cbpy = 15 - cbpy; |
707 |
|
|
708 |
return cbpy; |
return cbpy; |
709 |
|
|
710 |
} |
} |
711 |
|
|
712 |
int get_mv_data(Bitstream * bs) |
static __inline int |
713 |
|
get_mv_data(Bitstream * bs) |
714 |
{ |
{ |
715 |
|
|
716 |
uint32_t index; |
uint32_t index; |
717 |
|
|
718 |
if(BitstreamGetBit(bs)) |
if(BitstreamGetBit(bs)) |
720 |
|
|
721 |
index = BitstreamShowBits(bs, 12); |
index = BitstreamShowBits(bs, 12); |
722 |
|
|
723 |
if(index >= 512) |
if (index >= 512) { |
|
{ |
|
724 |
index = (index >> 8) - 2; |
index = (index >> 8) - 2; |
725 |
BitstreamSkip(bs, TMNMVtab0[index].len); |
BitstreamSkip(bs, TMNMVtab0[index].len); |
726 |
return TMNMVtab0[index].code; |
return TMNMVtab0[index].code; |
727 |
} |
} |
728 |
|
|
729 |
if(index >= 128) |
if (index >= 128) { |
|
{ |
|
730 |
index = (index >> 2) - 32; |
index = (index >> 2) - 32; |
731 |
BitstreamSkip(bs, TMNMVtab1[index].len); |
BitstreamSkip(bs, TMNMVtab1[index].len); |
732 |
return TMNMVtab1[index].code; |
return TMNMVtab1[index].code; |
736 |
|
|
737 |
BitstreamSkip(bs, TMNMVtab2[index].len); |
BitstreamSkip(bs, TMNMVtab2[index].len); |
738 |
return TMNMVtab2[index].code; |
return TMNMVtab2[index].code; |
739 |
|
|
740 |
} |
} |
741 |
|
|
742 |
int get_mv(Bitstream * bs, int fcode) |
int |
743 |
|
get_mv(Bitstream * bs, |
744 |
|
int fcode) |
745 |
{ |
{ |
746 |
|
|
747 |
int data; |
int data; |
748 |
int res; |
int res; |
749 |
int mv; |
int mv; |
758 |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
759 |
|
|
760 |
return data < 0 ? -mv : mv; |
return data < 0 ? -mv : mv; |
761 |
|
|
762 |
} |
} |
763 |
|
|
764 |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
int |
765 |
|
get_dc_dif(Bitstream * bs, |
766 |
|
uint32_t dc_size) |
767 |
{ |
{ |
768 |
|
|
769 |
int code = BitstreamGetBits(bs, dc_size); |
int code = BitstreamGetBits(bs, dc_size); |
770 |
int msb = code >> (dc_size - 1); |
int msb = code >> (dc_size - 1); |
771 |
|
|
773 |
return (-1 * (code^((1 << dc_size) - 1))); |
return (-1 * (code^((1 << dc_size) - 1))); |
774 |
|
|
775 |
return code; |
return code; |
776 |
|
|
777 |
} |
} |
778 |
|
|
779 |
int get_dc_size_lum(Bitstream * bs) |
int |
780 |
|
get_dc_size_lum(Bitstream * bs) |
781 |
{ |
{ |
782 |
|
|
783 |
int code, i; |
int code, i; |
784 |
|
|
785 |
code = BitstreamShowBits(bs, 11); |
code = BitstreamShowBits(bs, 11); |
786 |
|
|
787 |
for(i = 11; i > 3; i--) { |
for(i = 11; i > 3; i--) { |
794 |
|
|
795 |
BitstreamSkip(bs, dc_lum_tab[code].len); |
BitstreamSkip(bs, dc_lum_tab[code].len); |
796 |
return dc_lum_tab[code].code; |
return dc_lum_tab[code].code; |
797 |
|
|
798 |
} |
} |
799 |
|
|
800 |
|
|
801 |
int get_dc_size_chrom(Bitstream * bs) |
int |
802 |
|
get_dc_size_chrom(Bitstream * bs) |
803 |
{ |
{ |
804 |
|
|
805 |
uint32_t code, i; |
uint32_t code, i; |
806 |
|
|
807 |
code = BitstreamShowBits(bs, 12); |
code = BitstreamShowBits(bs, 12); |
808 |
|
|
809 |
for(i = 12; i > 2; i--) { |
for(i = 12; i > 2; i--) { |
815 |
} |
} |
816 |
|
|
817 |
return 3 - BitstreamGetBits(bs, 2); |
return 3 - BitstreamGetBits(bs, 2); |
|
} |
|
|
|
|
|
int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header) |
|
|
{ |
|
|
uint32_t mode; |
|
|
const VLC *tab; |
|
|
int32_t level; |
|
818 |
|
|
|
if(short_video_header) // inter-VLCs will be used for both intra and inter blocks |
|
|
intra = 0; |
|
|
|
|
|
tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
|
|
|
|
|
if(tab->code == -1) |
|
|
goto error; |
|
|
|
|
|
BitstreamSkip(bs, tab->len); |
|
|
|
|
|
if(tab->code != ESCAPE) { |
|
|
if(!intra) |
|
|
{ |
|
|
*run = (tab->code >> 4) & 255; |
|
|
level = tab->code & 15; |
|
|
*last = (tab->code >> 12) & 1; |
|
|
} |
|
|
else |
|
|
{ |
|
|
*run = (tab->code >> 8) & 255; |
|
|
level = tab->code & 255; |
|
|
*last = (tab->code >> 16) & 1; |
|
|
} |
|
|
return BitstreamGetBit(bs) ? -level : level; |
|
|
} |
|
|
|
|
|
if(short_video_header) |
|
|
{ |
|
|
// escape mode 4 - H.263 type, only used if short_video_header = 1 |
|
|
*last = BitstreamGetBit(bs); |
|
|
*run = BitstreamGetBits(bs, 6); |
|
|
level = BitstreamGetBits(bs, 8); |
|
|
|
|
|
if (level == 0 || level == 128) |
|
|
DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level); |
|
|
|
|
|
return (level >= 128 ? -(256 - level) : level); |
|
819 |
} |
} |
820 |
|
|
821 |
mode = BitstreamShowBits(bs, 2); |
void |
822 |
|
get_intra_block(Bitstream * bs, |
823 |
if(mode < 3) { |
int16_t * block, |
824 |
BitstreamSkip(bs, (mode == 2) ? 2 : 1); |
int direction, |
825 |
|
int coeff) |
|
tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; |
|
|
if (tab->code == -1) |
|
|
goto error; |
|
|
|
|
|
BitstreamSkip(bs, tab->len); |
|
|
|
|
|
if (!intra) { |
|
|
*run = (tab->code >> 4) & 255; |
|
|
level = tab->code & 15; |
|
|
*last = (tab->code >> 12) & 1; |
|
|
} |
|
|
else |
|
826 |
{ |
{ |
|
*run = (tab->code >> 8) & 255; |
|
|
level = tab->code & 255; |
|
|
*last = (tab->code >> 16) & 1; |
|
|
} |
|
|
|
|
|
if(mode < 2) // first escape mode, level is offset |
|
|
level += max_level[*last + (!intra<<1)][*run]; // need to add back the max level |
|
|
else if(mode == 2) // second escape mode, run is offset |
|
|
*run += max_run[*last + (!intra<<1)][level] + 1; |
|
|
|
|
|
return BitstreamGetBit(bs) ? -level : level; |
|
|
} |
|
|
|
|
|
// third escape mode - fixed length codes |
|
|
BitstreamSkip(bs, 2); |
|
|
*last = BitstreamGetBits(bs, 1); |
|
|
*run = BitstreamGetBits(bs, 6); |
|
|
BitstreamSkip(bs, 1); // marker |
|
|
level = BitstreamGetBits(bs, 12); |
|
|
BitstreamSkip(bs, 1); // marker |
|
827 |
|
|
|
return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level; |
|
|
|
|
|
error: |
|
|
*run = VLC_ERROR; |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
|
|
{ |
|
828 |
const uint16_t * scan = scan_tables[ direction ]; |
const uint16_t * scan = scan_tables[ direction ]; |
829 |
int level; |
int level, run, last; |
|
int run; |
|
|
int last; |
|
830 |
|
|
831 |
do |
do { |
|
{ |
|
832 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
833 |
if (run == -1) |
if (run == -1) { |
834 |
{ |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
|
DEBUG("fatal: invalid run"); |
|
835 |
break; |
break; |
836 |
} |
} |
837 |
coeff += run; |
coeff += run; |
838 |
block[ scan[coeff] ] = level; |
block[ scan[coeff] ] = level; |
839 |
if (level < -127 || level > 127) |
|
840 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
841 |
DEBUG1("warning: intra_overflow", level); |
//DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); |
842 |
|
|
843 |
|
if (level < -127 || level > 127) { |
844 |
|
DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level); |
845 |
} |
} |
846 |
coeff++; |
coeff++; |
847 |
} while (!last); |
} while (!last); |
848 |
|
|
849 |
} |
} |
850 |
|
|
851 |
void get_inter_block(Bitstream * bs, int16_t * block) |
void |
852 |
|
get_inter_block(Bitstream * bs, |
853 |
|
int16_t * block, |
854 |
|
int direction) |
855 |
{ |
{ |
856 |
const uint16_t * scan = scan_tables[0]; |
|
857 |
|
const uint16_t *scan = scan_tables[direction]; |
858 |
int p; |
int p; |
859 |
int level; |
int level; |
860 |
int run; |
int run; |
861 |
int last; |
int last; |
862 |
|
|
863 |
p = 0; |
p = 0; |
864 |
do |
do { |
|
{ |
|
865 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
866 |
if (run == -1) |
if (run == -1) { |
867 |
{ |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
|
DEBUG("fatal: invalid run"); |
|
868 |
break; |
break; |
869 |
} |
} |
870 |
p += run; |
p += run; |
871 |
|
|
872 |
block[ scan[p] ] = level; |
block[ scan[p] ] = level; |
873 |
if (level < -127 || level > 127) |
|
874 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
875 |
DEBUG1("warning: inter_overflow", level); |
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
876 |
|
|
877 |
|
if (level < -127 || level > 127) { |
878 |
|
DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level); |
879 |
} |
} |
880 |
p++; |
p++; |
881 |
} while (!last); |
} while (!last); |
882 |
|
|
883 |
} |
} |