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