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