41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
|
* 28.10.2002 GMC support - gruel * |
45 |
|
* 28.06.2002 added check_resync_marker() * |
46 |
* 14.04.2002 bframe encoding * |
* 14.04.2002 bframe encoding * |
47 |
* 08.03.2002 initial version; isibaar * |
* 08.03.2002 initial version; isibaar * |
48 |
* * |
* * |
68 |
VLC DCT3Dintra[4096]; |
VLC DCT3Dintra[4096]; |
69 |
VLC DCT3Dinter[4096]; |
VLC DCT3Dinter[4096]; |
70 |
|
|
71 |
void init_vlc_tables(void) |
/* 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 |
|
BitstreamPutBits(bs, code2, len2); |
83 |
|
if (len) BitstreamPutBits(bs, code, len); |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
|
void |
88 |
|
init_vlc_tables(void) |
89 |
{ |
{ |
90 |
|
|
91 |
int32_t k, l, i, intra, last; |
int32_t k, l, i, intra, last; |
113 |
|
|
114 |
for(l = 0; l < 64; l++) { // run |
for(l = 0; l < 64; l++) { // run |
115 |
int32_t level = k; |
int32_t level = k; |
116 |
uint32_t run = l; |
ptr_t run = l; |
117 |
|
|
118 |
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 |
|
119 |
|
|
120 |
vlc[intra]->code = 0; |
vlc[intra]->code = 0; |
121 |
vlc[intra]->len = 0; |
vlc[intra]->len = 0; |
122 |
goto loop_end; |
goto loop_end; |
123 |
} |
} else { |
|
else { |
|
124 |
if(level > 0) // correct level |
if(level > 0) // correct level |
125 |
level -= max_level_ptr[run]; |
level -= max_level_ptr[run]; |
126 |
else |
else |
151 |
run += max_run_ptr[abs(level)] + 1; |
run += max_run_ptr[abs(level)] + 1; |
152 |
} |
} |
153 |
|
|
154 |
vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) | |
vlc[intra]->code = |
155 |
(1 << 13) | ((k & 0xfff) << 1) | 1; |
(uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) | |
156 |
|
((k & 0xfff) << 1) | 1; |
157 |
|
|
158 |
vlc[intra]->len = 30; |
vlc[intra]->len = 30; |
159 |
vlc[intra]++; |
vlc[intra]++; |
161 |
|
|
162 |
loop_end: |
loop_end: |
163 |
if(level != 0) { |
if(level != 0) { |
164 |
vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) | |
vlc[intra]->code = |
165 |
(coeff_ptr[run][abs(level) - 1].code << 1); |
(vlc[intra]-> |
166 |
vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len; |
code << (coeff_ptr[run][abs(level) - 1].len + |
167 |
|
1)) | (coeff_ptr[run][abs(level) - |
168 |
|
1].code << 1); |
169 |
|
vlc[intra]->len = |
170 |
|
(coeff_ptr[run][abs(level) - 1].len + 1) + |
171 |
|
vlc[intra]->len; |
172 |
|
|
173 |
if(level < 0) |
if(level < 0) |
174 |
vlc[intra]->code += 1; |
vlc[intra]->code += 1; |
183 |
if(i >= 512) { |
if(i >= 512) { |
184 |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
185 |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
186 |
} |
} else if (i >= 128) { |
|
else if(i >= 128) { |
|
187 |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
188 |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
189 |
} |
} else if (i >= 8) { |
|
else if(i >= 8) { |
|
190 |
*vlc1 = DCT3Dtab5[i - 8]; |
*vlc1 = DCT3Dtab5[i - 8]; |
191 |
*vlc2 = DCT3Dtab2[i - 8]; |
*vlc2 = DCT3Dtab2[i - 8]; |
192 |
} |
} else { |
|
else { |
|
193 |
*vlc1 = ERRtab[i]; |
*vlc1 = ERRtab[i]; |
194 |
*vlc2 = ERRtab[i]; |
*vlc2 = ERRtab[i]; |
195 |
} |
} |
200 |
DCT3D[0] = DCT3Dinter; |
DCT3D[0] = DCT3Dinter; |
201 |
DCT3D[1] = DCT3Dintra; |
DCT3D[1] = DCT3Dintra; |
202 |
|
|
203 |
|
|
204 |
|
/* init sprite_trajectory tables */ |
205 |
|
/* even if GMC is not specified (it might be used later...) */ |
206 |
|
|
207 |
|
sprite_trajectory_code[0+16384].code = 0; |
208 |
|
sprite_trajectory_code[0+16384].len = 0; |
209 |
|
for (k=0;k<14;k++) |
210 |
|
{ |
211 |
|
int limit = (1<<k); |
212 |
|
|
213 |
|
for (i=-(2*limit-1); i<= -limit; i++) |
214 |
|
{ |
215 |
|
sprite_trajectory_code[i+16384].code = (2*limit-1)+i; |
216 |
|
sprite_trajectory_code[i+16384].len = k+1; |
217 |
|
} |
218 |
|
|
219 |
|
for (i=limit; i<= 2*limit-1; i++) |
220 |
|
{ |
221 |
|
sprite_trajectory_code[i+16384].code = i; |
222 |
|
sprite_trajectory_code[i+16384].len = k+1; |
223 |
|
} |
224 |
|
} |
225 |
} |
} |
226 |
|
|
227 |
static __inline void CodeVector(Bitstream *bs, |
static __inline void |
228 |
|
CodeVector(Bitstream * bs, |
229 |
int32_t value, |
int32_t value, |
230 |
int32_t f_code, |
int32_t f_code, |
231 |
Statistics *pStat) |
Statistics *pStat) |
244 |
pStat->iMvCount++; |
pStat->iMvCount++; |
245 |
|
|
246 |
if (value == 0) { |
if (value == 0) { |
247 |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
BitstreamPutBits(bs, mb_motion_table[32].code, |
248 |
|
mb_motion_table[32].len); |
249 |
} else { |
} else { |
250 |
uint16_t length, code, mv_res, sign; |
uint16_t length, code, mv_res, sign; |
251 |
|
|
270 |
code = -code; |
code = -code; |
271 |
|
|
272 |
code += 32; |
code += 32; |
273 |
BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len); |
BitstreamPutBits(bs, mb_motion_table[code].code, |
274 |
|
mb_motion_table[code].len); |
275 |
|
|
276 |
if(f_code) |
if(f_code) |
277 |
BitstreamPutBits(bs, mv_res, f_code); |
BitstreamPutBits(bs, mv_res, f_code); |
280 |
} |
} |
281 |
|
|
282 |
|
|
283 |
static __inline void CodeCoeff(Bitstream *bs, |
static __inline void |
284 |
|
CodeCoeff(Bitstream * bs, |
285 |
const int16_t qcoeff[64], |
const int16_t qcoeff[64], |
286 |
VLC *table, |
VLC *table, |
287 |
const uint16_t *zigzag, |
const uint16_t *zigzag, |
295 |
j = intra; |
j = intra; |
296 |
last = intra; |
last = intra; |
297 |
|
|
298 |
while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
299 |
|
j++; |
300 |
|
|
301 |
do { |
do { |
302 |
vlc = table + 64 * 2047 + (v << 6) + j - last; |
vlc = table + 64 * 2047 + (v << 6) + j - last; |
303 |
last = ++j; |
last = ++j; |
304 |
|
|
305 |
// count zeroes |
// count zeroes |
306 |
while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
307 |
|
j++; |
308 |
|
|
309 |
// write code |
// write code |
310 |
if(j != 64) { |
if(j != 64) { |
319 |
} |
} |
320 |
|
|
321 |
|
|
322 |
static void CodeBlockIntra(const FRAMEINFO * frame, |
static __inline void |
323 |
|
CodeBlockIntra(const FRAMEINFO * const frame, |
324 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
325 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
326 |
Bitstream * bs, |
Bitstream * bs, |
334 |
// write mcbpc |
// write mcbpc |
335 |
if(frame->coding_type == I_VOP) { |
if(frame->coding_type == I_VOP) { |
336 |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
337 |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
338 |
} |
mcbpc_intra_tab[mcbpc].len); |
339 |
else { |
} else { |
340 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
341 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
342 |
|
mcbpc_inter_tab[mcbpc].len); |
343 |
} |
} |
344 |
|
|
345 |
// ac prediction flag |
// ac prediction flag |
356 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
357 |
|
|
358 |
// write interlacing |
// write interlacing |
359 |
if (frame->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
|
{ |
|
360 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
361 |
} |
} |
|
|
|
362 |
// code block coeffs |
// code block coeffs |
363 |
for(i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
|
{ |
|
364 |
if(i < 4) |
if(i < 4) |
365 |
BitstreamPutBits(bs, |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
|
dcy_tab[qcoeff[i*64 + 0] + 255].code, |
|
366 |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
367 |
else |
else |
368 |
BitstreamPutBits(bs, |
BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code, |
|
dcc_tab[qcoeff[i*64 + 0] + 255].code, |
|
369 |
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
370 |
|
|
371 |
if(pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) { |
372 |
{ |
const uint16_t *scan_table = |
373 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
374 |
|
scan_tables[2] : scan_tables[pMB->acpred_directions[i]]; |
375 |
|
|
376 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
377 |
|
|
378 |
CodeCoeff(bs, |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1); |
|
&qcoeff[i*64], |
|
|
intra_table, |
|
|
scan_tables[pMB->acpred_directions[i]], |
|
|
1); |
|
379 |
|
|
380 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
381 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
385 |
} |
} |
386 |
|
|
387 |
|
|
388 |
static void CodeBlockInter(const FRAMEINFO * frame, |
static void |
389 |
|
CodeBlockInter(const FRAMEINFO * const frame, |
390 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
391 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
392 |
Bitstream * bs, |
Bitstream * bs, |
395 |
|
|
396 |
int32_t i; |
int32_t i; |
397 |
uint32_t bits, mcbpc, cbpy; |
uint32_t bits, mcbpc, cbpy; |
398 |
|
int mcsel=0; |
399 |
|
|
400 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
401 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
402 |
|
|
403 |
// write mcbpc |
// write mcbpc |
404 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
405 |
|
mcbpc_inter_tab[mcbpc].len); |
406 |
|
|
407 |
|
if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) ) |
408 |
|
{ |
409 |
|
if (frame->quarterpel) { |
410 |
|
if ( (pMB->qmvs[0].x == frame->GMC_MV.x) && (pMB->qmvs[0].y == frame->GMC_MV.y) ) |
411 |
|
mcsel=1; |
412 |
|
} else { |
413 |
|
if ( (pMB->mvs[0].x == frame->GMC_MV.x) && (pMB->mvs[0].y == frame->GMC_MV.y) ) |
414 |
|
mcsel=1; |
415 |
|
} |
416 |
|
BitstreamPutBit(bs, mcsel); // mcsel: '0'=local motion, '1'=GMC |
417 |
|
} |
418 |
|
|
419 |
// write cbpy |
// write cbpy |
420 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
424 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
425 |
|
|
426 |
// interlacing |
// interlacing |
427 |
if (frame->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
428 |
{ |
if (pMB->cbp) { |
429 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
430 |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
431 |
|
} |
432 |
|
|
433 |
// if inter block, write field ME flag |
// if inter block, write field ME flag |
434 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
435 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, pMB->field_pred); |
436 |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
437 |
|
|
438 |
// write field prediction references |
// write field prediction references |
439 |
if (pMB->field_pred) |
if (pMB->field_pred) { |
|
{ |
|
440 |
BitstreamPutBit(bs, pMB->field_for_top); |
BitstreamPutBit(bs, pMB->field_for_top); |
441 |
BitstreamPutBit(bs, pMB->field_for_bot); |
BitstreamPutBit(bs, pMB->field_for_bot); |
442 |
} |
} |
443 |
} |
} |
444 |
} |
} |
445 |
|
// code motion vector(s) if motion is local |
446 |
// code motion vector(s) |
if (mcsel==0) |
447 |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
|
{ |
|
448 |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
449 |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
450 |
} |
} |
454 |
// code block coeffs |
// code block coeffs |
455 |
for(i = 0; i < 6; i++) |
for(i = 0; i < 6; i++) |
456 |
if(pMB->cbp & (1 << (5 - i))) |
if(pMB->cbp & (1 << (5 - i))) |
457 |
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
{ |
458 |
|
const uint16_t *scan_table = |
459 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
460 |
|
scan_tables[2] : scan_tables[0]; |
461 |
|
|
462 |
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0); |
463 |
|
} |
464 |
|
|
465 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
466 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
|
|
|
467 |
} |
} |
468 |
|
|
469 |
|
|
470 |
void MBCoding(const FRAMEINFO * frame, |
void |
471 |
|
MBCoding(const FRAMEINFO * const frame, |
472 |
MACROBLOCK *pMB, |
MACROBLOCK *pMB, |
473 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
474 |
Bitstream * bs, |
Bitstream * bs, |
475 |
Statistics * pStat) |
Statistics * pStat) |
476 |
{ |
{ |
477 |
|
if (frame->coding_type != I_VOP) |
478 |
|
BitstreamPutBit(bs, 0); // not_coded |
479 |
|
|
480 |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
|
|
|
|
if(frame->coding_type == P_VOP) { |
|
|
if(pMB->cbp == 0 && pMB->mode == MODE_INTER && |
|
|
pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
|
|
{ |
|
|
BitstreamPutBit(bs, 1); // not_coded |
|
|
return; |
|
|
} |
|
|
else |
|
|
BitstreamPutBit(bs, 0); // coded |
|
|
} |
|
|
|
|
|
if(intra) |
|
481 |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
482 |
else |
else |
483 |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
484 |
|
|
485 |
} |
} |
486 |
|
|
487 |
|
/* |
488 |
|
// moved to mbcoding.h so that in can be 'static __inline' |
489 |
|
void |
490 |
|
MBSkip(Bitstream * bs) |
491 |
|
{ |
492 |
|
BitstreamPutBit(bs, 1); // not coded |
493 |
|
} |
494 |
|
*/ |
495 |
|
|
496 |
/*************************************************************** |
/*************************************************************** |
497 |
* bframe encoding start |
* bframe encoding start |
498 |
***************************************************************/ |
***************************************************************/ |
505 |
3 0001b forward mc+q dbquant, mvdf |
3 0001b forward mc+q dbquant, mvdf |
506 |
*/ |
*/ |
507 |
|
|
508 |
void put_bvop_mbtype(Bitstream * bs, int value) |
static __inline void |
509 |
|
put_bvop_mbtype(Bitstream * bs, |
510 |
|
int value) |
511 |
{ |
{ |
512 |
switch(value) |
switch (value) { |
513 |
{ |
case MODE_FORWARD: |
|
case 0 : BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
case 1 : BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
case 2 : BitstreamPutBit(bs, 0); |
|
514 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
515 |
BitstreamPutBit(bs, 1); |
case MODE_BACKWARD: |
|
return; |
|
|
|
|
|
case 3 : BitstreamPutBit(bs, 0); |
|
516 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
517 |
|
case MODE_INTERPOLATE: |
518 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
519 |
|
case MODE_DIRECT: |
520 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
521 |
return; |
default: |
522 |
|
break; |
|
default : ; // invalid! |
|
|
|
|
523 |
} |
} |
|
|
|
524 |
} |
} |
525 |
|
|
526 |
/* |
/* |
530 |
+2 11b |
+2 11b |
531 |
*/ |
*/ |
532 |
|
|
533 |
void put_bvop_dbquant(Bitstream *bs, int value) |
static __inline void |
534 |
{ |
put_bvop_dbquant(Bitstream * bs, |
535 |
switch (value) |
int value) |
536 |
{ |
{ |
537 |
case 0 : BitstreamPutBit(bs, 0); |
switch (value) { |
538 |
|
case 0: |
539 |
|
BitstreamPutBit(bs, 0); |
540 |
return; |
return; |
541 |
|
|
542 |
case -2 : BitstreamPutBit(bs, 1); |
case -2: |
543 |
|
BitstreamPutBit(bs, 1); |
544 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
545 |
return; |
return; |
546 |
|
|
547 |
case 2 : BitstreamPutBit(bs, 1); |
case 2: |
548 |
|
BitstreamPutBit(bs, 1); |
549 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
550 |
return; |
return; |
551 |
|
|
555 |
|
|
556 |
|
|
557 |
|
|
558 |
void MBCodingBVOP(const MACROBLOCK * mb, |
void |
559 |
|
MBCodingBVOP(const MACROBLOCK * mb, |
560 |
const int16_t qcoeff[6*64], |
const int16_t qcoeff[6*64], |
561 |
const int32_t fcode, |
const int32_t fcode, |
562 |
const int32_t bcode, |
const int32_t bcode, |
563 |
Bitstream * bs, |
Bitstream * bs, |
564 |
Statistics * pStat) |
Statistics * pStat, |
565 |
|
int direction) |
566 |
{ |
{ |
567 |
int i; |
int vcode = fcode; |
568 |
|
unsigned int i; |
569 |
|
|
570 |
/* ------------------------------------------------------------------ |
/* ------------------------------------------------------------------ |
571 |
when a block is skipped it is decoded DIRECT(0,) |
when a block is skipped it is decoded DIRECT(0,0) |
572 |
hence are interpolated from forward & backward frames |
hence is interpolated from forward & backward frames |
573 |
------------------------------------------------------------------ */ |
------------------------------------------------------------------ */ |
574 |
|
|
575 |
if (mb->mode == 5) |
if (mb->mode == MODE_DIRECT_NONE_MV) { |
|
{ |
|
576 |
BitstreamPutBit(bs, 1); // skipped |
BitstreamPutBit(bs, 1); // skipped |
577 |
return; |
return; |
578 |
} |
} |
579 |
|
|
580 |
BitstreamPutBit(bs, 0); // not skipped |
BitstreamPutBit(bs, 0); // not skipped |
581 |
|
|
582 |
if (mb->cbp == 0) |
if (mb->cbp == 0) { |
|
{ |
|
583 |
BitstreamPutBit(bs, 1); // cbp == 0 |
BitstreamPutBit(bs, 1); // cbp == 0 |
584 |
} |
} else { |
|
else |
|
|
{ |
|
585 |
BitstreamPutBit(bs, 0); // cbp == xxx |
BitstreamPutBit(bs, 0); // cbp == xxx |
586 |
} |
} |
587 |
|
|
588 |
put_bvop_mbtype(bs, mb->mode); |
put_bvop_mbtype(bs, mb->mode); |
589 |
|
|
590 |
if (mb->cbp) |
if (mb->cbp) { |
|
{ |
|
591 |
BitstreamPutBits(bs, mb->cbp, 6); |
BitstreamPutBits(bs, mb->cbp, 6); |
592 |
} |
} |
593 |
|
|
594 |
if (mb->mode != MODE_DIRECT && mb->cbp != 0) |
if (mb->mode != MODE_DIRECT && mb->cbp != 0) { |
|
{ |
|
595 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
596 |
} |
} |
597 |
|
|
598 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
switch (mb->mode) { |
599 |
{ |
case MODE_INTERPOLATE: |
600 |
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode |
601 |
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
CodeVector(bs, mb->pmvs[1].y, vcode, pStat); |
602 |
} |
case MODE_BACKWARD: |
603 |
|
vcode = bcode; |
604 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
case MODE_FORWARD: |
605 |
{ |
CodeVector(bs, mb->pmvs[0].x, vcode, pStat); |
606 |
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
CodeVector(bs, mb->pmvs[0].y, vcode, pStat); |
607 |
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
break; |
608 |
} |
case MODE_DIRECT: |
609 |
|
CodeVector(bs, mb->pmvs[3].x, 1, pStat); // fcode is always 1 for delta vector |
610 |
if (mb->mode == MODE_DIRECT) |
CodeVector(bs, mb->pmvs[3].y, 1, pStat); // prediction is always (0,0) |
611 |
{ |
default: break; |
|
// TODO: direct |
|
612 |
} |
} |
613 |
|
|
614 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
615 |
{ |
if (mb->cbp & (1 << (5 - i))) { |
616 |
if (mb->cbp & (1 << (5 - i))) |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0); |
|
{ |
|
|
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
|
617 |
} |
} |
618 |
} |
} |
619 |
} |
} |
624 |
* decoding stuff starts here * |
* decoding stuff starts here * |
625 |
***************************************************************/ |
***************************************************************/ |
626 |
|
|
627 |
int get_mcbpc_intra(Bitstream * bs) |
|
628 |
|
// for IVOP addbits == 0 |
629 |
|
// for PVOP addbits == fcode - 1 |
630 |
|
// for BVOP addbits == max(fcode,bcode) - 1 |
631 |
|
// returns true or false |
632 |
|
int |
633 |
|
check_resync_marker(Bitstream * bs, int addbits) |
634 |
|
{ |
635 |
|
uint32_t nbits; |
636 |
|
uint32_t code; |
637 |
|
uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits; |
638 |
|
|
639 |
|
nbits = BitstreamNumBitsToByteAlign(bs); |
640 |
|
code = BitstreamShowBits(bs, nbits); |
641 |
|
|
642 |
|
if (code == (((uint32_t)1 << (nbits - 1)) - 1)) |
643 |
{ |
{ |
644 |
|
return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER; |
645 |
|
} |
646 |
|
|
647 |
|
return 0; |
648 |
|
} |
649 |
|
|
650 |
|
|
|
uint32_t index; |
|
651 |
|
|
652 |
while((index = BitstreamShowBits(bs, 9)) == 1) |
int |
653 |
BitstreamSkip(bs, 9); |
get_mcbpc_intra(Bitstream * bs) |
654 |
|
{ |
655 |
|
|
656 |
|
uint32_t index; |
657 |
|
|
658 |
|
index = BitstreamShowBits(bs, 9); |
659 |
index >>= 3; |
index >>= 3; |
660 |
|
|
661 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
664 |
|
|
665 |
} |
} |
666 |
|
|
667 |
int get_mcbpc_inter(Bitstream * bs) |
int |
668 |
|
get_mcbpc_inter(Bitstream * bs) |
669 |
{ |
{ |
670 |
|
|
671 |
uint32_t index; |
uint32_t index; |
672 |
|
|
673 |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
|
BitstreamSkip(bs, 9); |
|
674 |
|
|
675 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
676 |
|
|
678 |
|
|
679 |
} |
} |
680 |
|
|
681 |
int get_cbpy(Bitstream * bs, int intra) |
int |
682 |
|
get_cbpy(Bitstream * bs, |
683 |
|
int intra) |
684 |
{ |
{ |
685 |
|
|
686 |
int cbpy; |
int cbpy; |
696 |
|
|
697 |
} |
} |
698 |
|
|
699 |
int get_mv_data(Bitstream * bs) |
static __inline int |
700 |
|
get_mv_data(Bitstream * bs) |
701 |
{ |
{ |
702 |
|
|
703 |
uint32_t index; |
uint32_t index; |
707 |
|
|
708 |
index = BitstreamShowBits(bs, 12); |
index = BitstreamShowBits(bs, 12); |
709 |
|
|
710 |
if(index >= 512) |
if (index >= 512) { |
|
{ |
|
711 |
index = (index >> 8) - 2; |
index = (index >> 8) - 2; |
712 |
BitstreamSkip(bs, TMNMVtab0[index].len); |
BitstreamSkip(bs, TMNMVtab0[index].len); |
713 |
return TMNMVtab0[index].code; |
return TMNMVtab0[index].code; |
714 |
} |
} |
715 |
|
|
716 |
if(index >= 128) |
if (index >= 128) { |
|
{ |
|
717 |
index = (index >> 2) - 32; |
index = (index >> 2) - 32; |
718 |
BitstreamSkip(bs, TMNMVtab1[index].len); |
BitstreamSkip(bs, TMNMVtab1[index].len); |
719 |
return TMNMVtab1[index].code; |
return TMNMVtab1[index].code; |
726 |
|
|
727 |
} |
} |
728 |
|
|
729 |
int get_mv(Bitstream * bs, int fcode) |
int |
730 |
|
get_mv(Bitstream * bs, |
731 |
|
int fcode) |
732 |
{ |
{ |
733 |
|
|
734 |
int data; |
int data; |
748 |
|
|
749 |
} |
} |
750 |
|
|
751 |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
int |
752 |
|
get_dc_dif(Bitstream * bs, |
753 |
|
uint32_t dc_size) |
754 |
{ |
{ |
755 |
|
|
756 |
int code = BitstreamGetBits(bs, dc_size); |
int code = BitstreamGetBits(bs, dc_size); |
763 |
|
|
764 |
} |
} |
765 |
|
|
766 |
int get_dc_size_lum(Bitstream * bs) |
int |
767 |
|
get_dc_size_lum(Bitstream * bs) |
768 |
{ |
{ |
769 |
|
|
770 |
int code, i; |
int code, i; |
771 |
|
|
772 |
code = BitstreamShowBits(bs, 11); |
code = BitstreamShowBits(bs, 11); |
773 |
|
|
774 |
for(i = 11; i > 3; i--) { |
for(i = 11; i > 3; i--) { |
785 |
} |
} |
786 |
|
|
787 |
|
|
788 |
int get_dc_size_chrom(Bitstream * bs) |
int |
789 |
|
get_dc_size_chrom(Bitstream * bs) |
790 |
{ |
{ |
791 |
|
|
792 |
uint32_t code, i; |
uint32_t code, i; |
793 |
|
|
794 |
code = BitstreamShowBits(bs, 12); |
code = BitstreamShowBits(bs, 12); |
795 |
|
|
796 |
for(i = 12; i > 2; i--) { |
for(i = 12; i > 2; i--) { |
805 |
|
|
806 |
} |
} |
807 |
|
|
808 |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
void |
809 |
|
get_intra_block(Bitstream * bs, |
810 |
|
int16_t * block, |
811 |
|
int direction, |
812 |
|
int coeff) |
813 |
{ |
{ |
814 |
|
|
815 |
const uint16_t * scan = scan_tables[ direction ]; |
const uint16_t * scan = scan_tables[ direction ]; |
816 |
int level; |
int level, run, last; |
|
int run; |
|
|
int last; |
|
817 |
|
|
818 |
do |
do { |
|
{ |
|
819 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
820 |
if (run == -1) |
if (run == -1) { |
|
{ |
|
821 |
DEBUG("fatal: invalid run"); |
DEBUG("fatal: invalid run"); |
822 |
break; |
break; |
823 |
} |
} |
824 |
coeff += run; |
coeff += run; |
825 |
block[ scan[coeff] ] = level; |
block[ scan[coeff] ] = level; |
826 |
if (level < -127 || level > 127) |
|
827 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
828 |
|
//DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); |
829 |
|
|
830 |
|
if (level < -127 || level > 127) { |
831 |
DEBUG1("warning: intra_overflow", level); |
DEBUG1("warning: intra_overflow", level); |
832 |
} |
} |
833 |
coeff++; |
coeff++; |
835 |
|
|
836 |
} |
} |
837 |
|
|
838 |
void get_inter_block(Bitstream * bs, int16_t * block) |
void |
839 |
|
get_inter_block(Bitstream * bs, |
840 |
|
int16_t * block, |
841 |
|
int direction) |
842 |
{ |
{ |
843 |
|
|
844 |
const uint16_t * scan = scan_tables[0]; |
const uint16_t *scan = scan_tables[direction]; |
845 |
int p; |
int p; |
846 |
int level; |
int level; |
847 |
int run; |
int run; |
848 |
int last; |
int last; |
849 |
|
|
850 |
p = 0; |
p = 0; |
851 |
do |
do { |
|
{ |
|
852 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
853 |
if (run == -1) |
if (run == -1) { |
|
{ |
|
854 |
DEBUG("fatal: invalid run"); |
DEBUG("fatal: invalid run"); |
855 |
break; |
break; |
856 |
} |
} |
857 |
p += run; |
p += run; |
858 |
|
|
859 |
block[ scan[p] ] = level; |
block[ scan[p] ] = level; |
860 |
if (level < -127 || level > 127) |
|
861 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
862 |
|
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
863 |
|
|
864 |
|
if (level < -127 || level > 127) { |
865 |
DEBUG1("warning: inter_overflow", level); |
DEBUG1("warning: inter_overflow", level); |
866 |
} |
} |
867 |
p++; |
p++; |