29 |
|
|
30 |
/****************************************************************************** |
/****************************************************************************** |
31 |
* * |
* * |
32 |
* bitstream.c * |
* mbcoding.c * |
33 |
* * |
* * |
34 |
* Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * |
* Copyright (C) 2002 - Michael Militzer <isibaar@xvid.org> * |
35 |
* * |
* * |
36 |
* For more information visit the XviD homepage: http://www.xvid.org * |
* For more information visit the XviD homepage: http://www.xvid.org * |
37 |
* * |
* * |
41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
* 14.04.2002 bframe encoding |
* 28.06.2002 added check_resync_marker() * |
45 |
|
* 14.04.2002 bframe encoding * |
46 |
* 08.03.2002 initial version; isibaar * |
* 08.03.2002 initial version; isibaar * |
47 |
* * |
* * |
48 |
******************************************************************************/ |
******************************************************************************/ |
61 |
#define ABS(X) (((X)>0)?(X):-(X)) |
#define ABS(X) (((X)>0)?(X):-(X)) |
62 |
#define CLIP(X,A) (X > A) ? (A) : (X) |
#define CLIP(X,A) (X > A) ? (A) : (X) |
63 |
|
|
64 |
VLC intra_table[65536]; |
VLC intra_table[524032]; |
65 |
VLC inter_table[65536]; |
VLC inter_table[524032]; |
66 |
|
|
67 |
VLC DCT3Dintra[4096]; |
VLC DCT3Dintra[4096]; |
68 |
VLC DCT3Dinter[4096]; |
VLC DCT3Dinter[4096]; |
69 |
|
|
70 |
static int16_t clip_table[4096]; |
void |
71 |
|
init_vlc_tables(void) |
|
void init_vlc_tables(void) |
|
72 |
{ |
{ |
73 |
|
|
74 |
int32_t k, l, i, intra, last; |
int32_t k, l, i, intra, last; |
82 |
vlc[0] = intra_table; |
vlc[0] = intra_table; |
83 |
vlc[1] = inter_table; |
vlc[1] = inter_table; |
84 |
|
|
|
// 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; |
|
|
} |
|
|
|
|
85 |
// generate encoding vlc lookup tables |
// generate encoding vlc lookup tables |
86 |
|
// the lookup table idea is taken from the excellent fame project by Vivien Chapellier |
87 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
88 |
intra = i % 2; |
intra = i % 2; |
89 |
last = i / 2; |
last = i / 2; |
90 |
|
|
91 |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
92 |
|
|
93 |
for(k = -255; k < 256; k++) { // level |
for (k = -2047; k < 2048; k++) { // level |
94 |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
95 |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
96 |
|
|
97 |
for(l = 0; l < 64; l++) { // run |
for(l = 0; l < 64; l++) { // run |
98 |
int32_t level = k; |
int32_t level = k; |
99 |
uint32_t run = l; |
ptr_t run = l; |
100 |
|
|
101 |
if((abs(level) <= max_level_ptr[run]) && |
if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
|
(run <= (uint32_t)max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
|
102 |
|
|
103 |
vlc[intra]->code = 0; |
vlc[intra]->code = 0; |
104 |
vlc[intra]->len = 0; |
vlc[intra]->len = 0; |
105 |
goto loop_end; |
goto loop_end; |
106 |
} |
} else { |
|
else { |
|
107 |
if(level > 0) // correct level |
if(level > 0) // correct level |
108 |
level -= max_level_ptr[run]; |
level -= max_level_ptr[run]; |
109 |
else |
else |
134 |
run += max_run_ptr[abs(level)] + 1; |
run += max_run_ptr[abs(level)] + 1; |
135 |
} |
} |
136 |
|
|
137 |
vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) | |
vlc[intra]->code = |
138 |
(1 << 13) | ((k & 0xfff) << 1) | 1; |
(uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) | |
139 |
|
((k & 0xfff) << 1) | 1; |
140 |
|
|
141 |
vlc[intra]->len = 30; |
vlc[intra]->len = 30; |
142 |
vlc[intra]++; |
vlc[intra]++; |
144 |
|
|
145 |
loop_end: |
loop_end: |
146 |
if(level != 0) { |
if(level != 0) { |
147 |
vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) | |
vlc[intra]->code = |
148 |
(coeff_ptr[run][abs(level) - 1].code << 1); |
(vlc[intra]-> |
149 |
vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len; |
code << (coeff_ptr[run][abs(level) - 1].len + |
150 |
|
1)) | (coeff_ptr[run][abs(level) - |
151 |
|
1].code << 1); |
152 |
|
vlc[intra]->len = |
153 |
|
(coeff_ptr[run][abs(level) - 1].len + 1) + |
154 |
|
vlc[intra]->len; |
155 |
|
|
156 |
if(level < 0) |
if(level < 0) |
157 |
vlc[intra]->code += 1; |
vlc[intra]->code += 1; |
166 |
if(i >= 512) { |
if(i >= 512) { |
167 |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
168 |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
169 |
} |
} else if (i >= 128) { |
|
else if(i >= 128) { |
|
170 |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
171 |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
172 |
} |
} else if (i >= 8) { |
|
else if(i >= 8) { |
|
173 |
*vlc1 = DCT3Dtab5[i - 8]; |
*vlc1 = DCT3Dtab5[i - 8]; |
174 |
*vlc2 = DCT3Dtab2[i - 8]; |
*vlc2 = DCT3Dtab2[i - 8]; |
175 |
} |
} else { |
|
else { |
|
176 |
*vlc1 = ERRtab[i]; |
*vlc1 = ERRtab[i]; |
177 |
*vlc2 = ERRtab[i]; |
*vlc2 = ERRtab[i]; |
178 |
} |
} |
185 |
|
|
186 |
} |
} |
187 |
|
|
188 |
static __inline void CodeVector(Bitstream *bs, |
static __inline void |
189 |
int16_t value, |
CodeVector(Bitstream * bs, |
190 |
int16_t f_code, |
int32_t value, |
191 |
|
int32_t f_code, |
192 |
Statistics *pStat) |
Statistics *pStat) |
193 |
{ |
{ |
194 |
|
|
205 |
pStat->iMvCount++; |
pStat->iMvCount++; |
206 |
|
|
207 |
if (value == 0) { |
if (value == 0) { |
208 |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
BitstreamPutBits(bs, mb_motion_table[32].code, |
209 |
|
mb_motion_table[32].len); |
210 |
} else { |
} else { |
211 |
uint16_t length, code, mv_res, sign; |
uint16_t length, code, mv_res, sign; |
212 |
|
|
231 |
code = -code; |
code = -code; |
232 |
|
|
233 |
code += 32; |
code += 32; |
234 |
BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len); |
BitstreamPutBits(bs, mb_motion_table[code].code, |
235 |
|
mb_motion_table[code].len); |
236 |
|
|
237 |
if(f_code) |
if(f_code) |
238 |
BitstreamPutBits(bs, mv_res, f_code); |
BitstreamPutBits(bs, mv_res, f_code); |
241 |
} |
} |
242 |
|
|
243 |
|
|
244 |
static __inline void CodeCoeff(Bitstream *bs, |
static __inline void |
245 |
int16_t qcoeff[64], |
CodeCoeff(Bitstream * bs, |
246 |
|
const int16_t qcoeff[64], |
247 |
VLC *table, |
VLC *table, |
248 |
const uint16_t *zigzag, |
const uint16_t *zigzag, |
249 |
uint16_t intra) |
uint16_t intra) |
256 |
j = intra; |
j = intra; |
257 |
last = intra; |
last = intra; |
258 |
|
|
259 |
while((v = qcoeff[zigzag[j]]) == 0) j++; |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
260 |
|
j++; |
261 |
|
|
262 |
do { |
do { |
263 |
// count zeroes |
vlc = table + 64 * 2047 + (v << 6) + j - last; |
|
vlc = table + 64*255 + (clip_table[2048+v] << 6) + j - last; |
|
264 |
last = ++j; |
last = ++j; |
265 |
while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; |
|
266 |
|
// count zeroes |
267 |
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
268 |
|
j++; |
269 |
|
|
270 |
// write code |
// write code |
271 |
if(j != 64) { |
if(j != 64) { |
272 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
273 |
} else { |
} else { |
274 |
vlc += 64*511; |
vlc += 64 * 4095; |
275 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
276 |
break; |
break; |
277 |
} |
} |
280 |
} |
} |
281 |
|
|
282 |
|
|
283 |
static void CodeBlockIntra(const MBParam * pParam, |
static void |
284 |
|
CodeBlockIntra(const FRAMEINFO * frame, |
285 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
286 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
287 |
Bitstream * bs, |
Bitstream * bs, |
293 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
294 |
|
|
295 |
// write mcbpc |
// write mcbpc |
296 |
if(pParam->coding_type == I_VOP) { |
if (frame->coding_type == I_VOP) { |
297 |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
298 |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
299 |
} |
mcbpc_intra_tab[mcbpc].len); |
300 |
else { |
} else { |
301 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
302 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
303 |
|
mcbpc_inter_tab[mcbpc].len); |
304 |
} |
} |
305 |
|
|
306 |
// ac prediction flag |
// ac prediction flag |
317 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
318 |
|
|
319 |
// write interlacing |
// write interlacing |
320 |
if (pParam->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
|
{ |
|
321 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
322 |
} |
} |
|
|
|
323 |
// code block coeffs |
// code block coeffs |
324 |
for(i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
|
{ |
|
325 |
if(i < 4) |
if(i < 4) |
326 |
BitstreamPutBits(bs, |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
|
dcy_tab[qcoeff[i*64 + 0] + 255].code, |
|
327 |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
328 |
else |
else |
329 |
BitstreamPutBits(bs, |
BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code, |
|
dcc_tab[qcoeff[i*64 + 0] + 255].code, |
|
330 |
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
331 |
|
|
332 |
if(pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) { |
|
{ |
|
333 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
334 |
|
|
335 |
CodeCoeff(bs, |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
336 |
&qcoeff[i*64], |
scan_tables[pMB->acpred_directions[i]], 1); |
|
intra_table, |
|
|
scan_tables[pMB->acpred_directions[i]], |
|
|
1); |
|
337 |
|
|
338 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
339 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
343 |
} |
} |
344 |
|
|
345 |
|
|
346 |
static void CodeBlockInter(const MBParam * pParam, |
static void |
347 |
|
CodeBlockInter(const FRAMEINFO * frame, |
348 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
349 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
350 |
Bitstream * bs, |
Bitstream * bs, |
358 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
359 |
|
|
360 |
// write mcbpc |
// write mcbpc |
361 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
362 |
|
mcbpc_inter_tab[mcbpc].len); |
363 |
|
|
364 |
// write cbpy |
// write cbpy |
365 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
369 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
370 |
|
|
371 |
// interlacing |
// interlacing |
372 |
if (pParam->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
|
{ |
|
373 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
374 |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
375 |
|
|
376 |
// if inter block, write field ME flag |
// if inter block, write field ME flag |
377 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
378 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, pMB->field_pred); |
379 |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
380 |
|
|
381 |
// write field prediction references |
// write field prediction references |
382 |
if (pMB->field_pred) |
if (pMB->field_pred) { |
|
{ |
|
383 |
BitstreamPutBit(bs, pMB->field_for_top); |
BitstreamPutBit(bs, pMB->field_for_top); |
384 |
BitstreamPutBit(bs, pMB->field_for_bot); |
BitstreamPutBit(bs, pMB->field_for_bot); |
385 |
} |
} |
386 |
} |
} |
387 |
} |
} |
|
|
|
388 |
// code motion vector(s) |
// code motion vector(s) |
389 |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
390 |
{ |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
391 |
CodeVector(bs, pMB->pmvs[i].x, pParam->fixed_code, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
|
CodeVector(bs, pMB->pmvs[i].y, pParam->fixed_code, pStat); |
|
392 |
} |
} |
393 |
|
|
394 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
404 |
} |
} |
405 |
|
|
406 |
|
|
407 |
void MBCoding(const MBParam * pParam, |
void |
408 |
|
MBCoding(const FRAMEINFO * frame, |
409 |
MACROBLOCK *pMB, |
MACROBLOCK *pMB, |
410 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
411 |
Bitstream * bs, |
Bitstream * bs, |
414 |
|
|
415 |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
416 |
|
|
417 |
if(pParam->coding_type == P_VOP) { |
if (frame->coding_type == P_VOP) { |
418 |
if(pMB->cbp == 0 && pMB->mode == MODE_INTER && |
if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
419 |
pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
pMB->mvs[0].y == 0) { |
|
{ |
|
420 |
BitstreamPutBit(bs, 1); // not_coded |
BitstreamPutBit(bs, 1); // not_coded |
421 |
return; |
return; |
422 |
} |
} else |
|
else |
|
423 |
BitstreamPutBit(bs, 0); // coded |
BitstreamPutBit(bs, 0); // coded |
424 |
} |
} |
425 |
|
|
426 |
if(intra) |
if(intra) |
427 |
CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
428 |
else |
else |
429 |
CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
430 |
|
|
431 |
} |
} |
432 |
|
|
442 |
3 0001b forward mc+q dbquant, mvdf |
3 0001b forward mc+q dbquant, mvdf |
443 |
*/ |
*/ |
444 |
|
|
445 |
void put_bvop_mbtype(Bitstream * bs, int value) |
void |
446 |
{ |
put_bvop_mbtype(Bitstream * bs, |
447 |
switch(value) |
int value) |
448 |
{ |
{ |
449 |
case 0 : BitstreamPutBit(bs, 1); |
switch (value) { |
450 |
|
case 0: |
451 |
|
BitstreamPutBit(bs, 1); |
452 |
return; |
return; |
453 |
|
|
454 |
case 1 : BitstreamPutBit(bs, 0); |
case 1: |
455 |
|
BitstreamPutBit(bs, 0); |
456 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
457 |
return; |
return; |
458 |
|
|
459 |
case 2 : BitstreamPutBit(bs, 0); |
case 2: |
460 |
|
BitstreamPutBit(bs, 0); |
461 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
462 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
463 |
return; |
return; |
464 |
|
|
465 |
case 3 : BitstreamPutBit(bs, 0); |
case 3: |
466 |
|
BitstreamPutBit(bs, 0); |
467 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
468 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
469 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
482 |
+2 11b |
+2 11b |
483 |
*/ |
*/ |
484 |
|
|
485 |
void put_bvop_dbquant(Bitstream *bs, int value) |
void |
486 |
{ |
put_bvop_dbquant(Bitstream * bs, |
487 |
switch (value) |
int value) |
488 |
{ |
{ |
489 |
case 0 : BitstreamPutBit(bs, 0); |
switch (value) { |
490 |
|
case 0: |
491 |
|
BitstreamPutBit(bs, 0); |
492 |
return; |
return; |
493 |
|
|
494 |
case -2 : BitstreamPutBit(bs, 1); |
case -2: |
495 |
|
BitstreamPutBit(bs, 1); |
496 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
497 |
return; |
return; |
498 |
|
|
499 |
case 2 : BitstreamPutBit(bs, 1); |
case 2: |
500 |
|
BitstreamPutBit(bs, 1); |
501 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
502 |
return; |
return; |
503 |
|
|
507 |
|
|
508 |
|
|
509 |
|
|
510 |
void MBCodingBVOP(const MACROBLOCK * mb, |
void |
511 |
|
MBCodingBVOP(const MACROBLOCK * mb, |
512 |
const int16_t qcoeff[6*64], |
const int16_t qcoeff[6*64], |
513 |
const int16_t fcode, |
const int32_t fcode, |
514 |
const int16_t bcode, |
const int32_t bcode, |
515 |
Bitstream * bs, |
Bitstream * bs, |
516 |
Statistics * pStat) |
Statistics * pStat) |
517 |
{ |
{ |
522 |
hence are interpolated from forward & backward frames |
hence are interpolated from forward & backward frames |
523 |
------------------------------------------------------------------ */ |
------------------------------------------------------------------ */ |
524 |
|
|
525 |
if (mb->mode == 5) |
if (mb->mode == 5) { |
|
{ |
|
526 |
BitstreamPutBit(bs, 1); // skipped |
BitstreamPutBit(bs, 1); // skipped |
527 |
return; |
return; |
528 |
} |
} |
529 |
|
|
530 |
BitstreamPutBit(bs, 0); // not skipped |
BitstreamPutBit(bs, 0); // not skipped |
531 |
|
|
532 |
if (mb->cbp == 0) |
if (mb->cbp == 0) { |
|
{ |
|
533 |
BitstreamPutBit(bs, 1); // cbp == 0 |
BitstreamPutBit(bs, 1); // cbp == 0 |
534 |
} |
} else { |
|
else |
|
|
{ |
|
535 |
BitstreamPutBit(bs, 0); // cbp == xxx |
BitstreamPutBit(bs, 0); // cbp == xxx |
536 |
} |
} |
537 |
|
|
538 |
put_bvop_mbtype(bs, mb->mode); |
put_bvop_mbtype(bs, mb->mode); |
539 |
|
|
540 |
if (mb->cbp) |
if (mb->cbp) { |
|
{ |
|
541 |
BitstreamPutBits(bs, mb->cbp, 6); |
BitstreamPutBits(bs, mb->cbp, 6); |
542 |
} |
} |
543 |
|
|
544 |
if (mb->mode != MODE_DIRECT && mb->cbp != 0) |
if (mb->mode != MODE_DIRECT && mb->cbp != 0) { |
|
{ |
|
545 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
546 |
} |
} |
547 |
|
|
548 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
|
{ |
|
549 |
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
550 |
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
551 |
} |
} |
552 |
|
|
553 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
|
{ |
|
554 |
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
555 |
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
556 |
} |
} |
557 |
|
|
558 |
if (mb->mode == MODE_DIRECT) |
if (mb->mode == MODE_DIRECT) { |
|
{ |
|
559 |
// TODO: direct |
// TODO: direct |
560 |
} |
} |
561 |
|
|
562 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
563 |
{ |
if (mb->cbp & (1 << (5 - i))) { |
|
if (mb->cbp & (1 << (5 - i))) |
|
|
{ |
|
564 |
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
565 |
} |
} |
566 |
} |
} |
572 |
* decoding stuff starts here * |
* decoding stuff starts here * |
573 |
***************************************************************/ |
***************************************************************/ |
574 |
|
|
575 |
int get_mcbpc_intra(Bitstream * bs) |
|
576 |
|
void |
577 |
|
skip_stuffing(Bitstream *bs) |
578 |
{ |
{ |
579 |
|
while (BitstreamShowBits(bs, 9) == 1) |
580 |
|
BitstreamSkip(bs, 9); |
581 |
|
} |
582 |
|
|
583 |
|
|
584 |
|
|
585 |
|
// for IVOP addbits == 0 |
586 |
|
// for PVOP addbits == fcode - 1 |
587 |
|
// for BVOP addbits == max(fcode,bcode) - 1 |
588 |
|
// returns true or false |
589 |
|
int |
590 |
|
check_resync_marker(Bitstream * bs, int addbits) |
591 |
|
{ |
592 |
|
uint32_t nbits; |
593 |
|
uint32_t code; |
594 |
|
uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits; |
595 |
|
|
596 |
|
nbits = BitstreamNumBitsToByteAlign(bs); |
597 |
|
code = BitstreamShowBits(bs, nbits); |
598 |
|
|
599 |
|
if (code == (((uint32_t)1 << (nbits - 1)) - 1)) |
600 |
|
{ |
601 |
|
return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER; |
602 |
|
} |
603 |
|
|
604 |
|
return 0; |
605 |
|
} |
606 |
|
|
|
uint32_t index; |
|
607 |
|
|
|
while((index = BitstreamShowBits(bs, 9)) == 1) |
|
|
BitstreamSkip(bs, 9); |
|
608 |
|
|
609 |
|
int |
610 |
|
get_mcbpc_intra(Bitstream * bs) |
611 |
|
{ |
612 |
|
|
613 |
|
uint32_t index; |
614 |
|
|
615 |
|
index = BitstreamShowBits(bs, 9); |
616 |
index >>= 3; |
index >>= 3; |
617 |
|
|
618 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
621 |
|
|
622 |
} |
} |
623 |
|
|
624 |
int get_mcbpc_inter(Bitstream * bs) |
int |
625 |
|
get_mcbpc_inter(Bitstream * bs) |
626 |
{ |
{ |
627 |
|
|
628 |
uint32_t index; |
uint32_t index; |
629 |
|
|
630 |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
|
BitstreamSkip(bs, 9); |
|
631 |
|
|
632 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
633 |
|
|
635 |
|
|
636 |
} |
} |
637 |
|
|
638 |
int get_cbpy(Bitstream * bs, int intra) |
int |
639 |
|
get_cbpy(Bitstream * bs, |
640 |
|
int intra) |
641 |
{ |
{ |
642 |
|
|
643 |
int cbpy; |
int cbpy; |
653 |
|
|
654 |
} |
} |
655 |
|
|
656 |
int get_mv_data(Bitstream * bs) |
int |
657 |
|
get_mv_data(Bitstream * bs) |
658 |
{ |
{ |
659 |
|
|
660 |
uint32_t index; |
uint32_t index; |
664 |
|
|
665 |
index = BitstreamShowBits(bs, 12); |
index = BitstreamShowBits(bs, 12); |
666 |
|
|
667 |
if(index >= 512) |
if (index >= 512) { |
|
{ |
|
668 |
index = (index >> 8) - 2; |
index = (index >> 8) - 2; |
669 |
BitstreamSkip(bs, TMNMVtab0[index].len); |
BitstreamSkip(bs, TMNMVtab0[index].len); |
670 |
return TMNMVtab0[index].code; |
return TMNMVtab0[index].code; |
671 |
} |
} |
672 |
|
|
673 |
if(index >= 128) |
if (index >= 128) { |
|
{ |
|
674 |
index = (index >> 2) - 32; |
index = (index >> 2) - 32; |
675 |
BitstreamSkip(bs, TMNMVtab1[index].len); |
BitstreamSkip(bs, TMNMVtab1[index].len); |
676 |
return TMNMVtab1[index].code; |
return TMNMVtab1[index].code; |
683 |
|
|
684 |
} |
} |
685 |
|
|
686 |
int get_mv(Bitstream * bs, int fcode) |
int |
687 |
|
get_mv(Bitstream * bs, |
688 |
|
int fcode) |
689 |
{ |
{ |
690 |
|
|
691 |
int data; |
int data; |
705 |
|
|
706 |
} |
} |
707 |
|
|
708 |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
int |
709 |
|
get_dc_dif(Bitstream * bs, |
710 |
|
uint32_t dc_size) |
711 |
{ |
{ |
712 |
|
|
713 |
int code = BitstreamGetBits(bs, dc_size); |
int code = BitstreamGetBits(bs, dc_size); |
720 |
|
|
721 |
} |
} |
722 |
|
|
723 |
int get_dc_size_lum(Bitstream * bs) |
int |
724 |
|
get_dc_size_lum(Bitstream * bs) |
725 |
{ |
{ |
726 |
|
|
727 |
int code, i; |
int code, i; |
728 |
|
|
729 |
code = BitstreamShowBits(bs, 11); |
code = BitstreamShowBits(bs, 11); |
730 |
|
|
731 |
for(i = 11; i > 3; i--) { |
for(i = 11; i > 3; i--) { |
742 |
} |
} |
743 |
|
|
744 |
|
|
745 |
int get_dc_size_chrom(Bitstream * bs) |
int |
746 |
|
get_dc_size_chrom(Bitstream * bs) |
747 |
{ |
{ |
748 |
|
|
749 |
uint32_t code, i; |
uint32_t code, i; |
750 |
|
|
751 |
code = BitstreamShowBits(bs, 12); |
code = BitstreamShowBits(bs, 12); |
752 |
|
|
753 |
for(i = 12; i > 2; i--) { |
for(i = 12; i > 2; i--) { |
762 |
|
|
763 |
} |
} |
764 |
|
|
765 |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
void |
766 |
|
get_intra_block(Bitstream * bs, |
767 |
|
int16_t * block, |
768 |
|
int direction, |
769 |
|
int coeff) |
770 |
{ |
{ |
771 |
|
|
772 |
const uint16_t * scan = scan_tables[ direction ]; |
const uint16_t * scan = scan_tables[ direction ]; |
774 |
int run; |
int run; |
775 |
int last; |
int last; |
776 |
|
|
777 |
do |
do { |
|
{ |
|
778 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
779 |
if (run == -1) |
if (run == -1) { |
|
{ |
|
780 |
DEBUG("fatal: invalid run"); |
DEBUG("fatal: invalid run"); |
781 |
break; |
break; |
782 |
} |
} |
783 |
coeff += run; |
coeff += run; |
784 |
block[ scan[coeff] ] = level; |
block[ scan[coeff] ] = level; |
785 |
if (level < -127 || level > 127) |
if (level < -127 || level > 127) { |
|
{ |
|
786 |
DEBUG1("warning: intra_overflow", level); |
DEBUG1("warning: intra_overflow", level); |
787 |
} |
} |
788 |
coeff++; |
coeff++; |
790 |
|
|
791 |
} |
} |
792 |
|
|
793 |
void get_inter_block(Bitstream * bs, int16_t * block) |
void |
794 |
|
get_inter_block(Bitstream * bs, |
795 |
|
int16_t * block) |
796 |
{ |
{ |
797 |
|
|
798 |
const uint16_t * scan = scan_tables[0]; |
const uint16_t * scan = scan_tables[0]; |
802 |
int last; |
int last; |
803 |
|
|
804 |
p = 0; |
p = 0; |
805 |
do |
do { |
|
{ |
|
806 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
807 |
if (run == -1) |
if (run == -1) { |
|
{ |
|
808 |
DEBUG("fatal: invalid run"); |
DEBUG("fatal: invalid run"); |
809 |
break; |
break; |
810 |
} |
} |
811 |
p += run; |
p += run; |
812 |
|
|
813 |
block[ scan[p] ] = level; |
block[ scan[p] ] = level; |
814 |
if (level < -127 || level > 127) |
if (level < -127 || level > 127) { |
|
{ |
|
815 |
DEBUG1("warning: inter_overflow", level); |
DEBUG1("warning: inter_overflow", level); |
816 |
} |
} |
817 |
p++; |
p++; |