32 |
* |
* |
33 |
* History: |
* History: |
34 |
* |
* |
35 |
|
* 15.07.2002 fix a bug in B-frame decode at DIRECT mode |
36 |
|
* MinChen <chenm001@163.com> |
37 |
|
* 10.07.2002 added BFRAMES_DEC_DEBUG support |
38 |
|
* Fix a little bug for low_delay flage |
39 |
|
* MinChen <chenm001@163.com> |
40 |
|
* 28.06.2002 added basic resync support to iframe/pframe_decode() |
41 |
* 22.06.2002 added primative N_VOP support |
* 22.06.2002 added primative N_VOP support |
42 |
* #define BFRAMES_DEC now enables Minchenm's bframe decoder |
* #define BFRAMES_DEC now enables Minchen's bframe decoder |
43 |
* 08.05.2002 add low_delay support for B_VOP decode |
* 08.05.2002 add low_delay support for B_VOP decode |
44 |
* MinChen <chenm001@163.com> |
* MinChen <chenm001@163.com> |
45 |
* 05.05.2002 fix some B-frame decode problem |
* 05.05.2002 fix some B-frame decode problem |
62 |
#include <stdlib.h> |
#include <stdlib.h> |
63 |
#include <string.h> |
#include <string.h> |
64 |
|
|
65 |
|
#ifdef BFRAMES_DEC_DEBUG |
66 |
|
#define BFRAMES_DEC |
67 |
|
#endif |
68 |
|
|
69 |
#include "xvid.h" |
#include "xvid.h" |
70 |
#include "portab.h" |
#include "portab.h" |
71 |
|
|
178 |
} |
} |
179 |
|
|
180 |
|
|
|
|
|
|
/* **************************************************************** |
|
|
NIC 28.06.2002 |
|
|
riscritta la 'decoder_create' per cambiarne l'interfaccia e poterla |
|
|
usare nella dll caricata dall'IM1 player |
|
|
IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame) |
|
|
**************************************************************** */ |
|
|
//XVID_DEC_FRAME * frame |
|
|
|
|
|
int |
|
|
IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame) |
|
|
{ |
|
|
DECODER *dec; |
|
|
Bitstream bs; |
|
|
uint32_t rounding; |
|
|
uint32_t quant; |
|
|
uint32_t fcode_forward; |
|
|
uint32_t fcode_backward; |
|
|
uint32_t intra_dc_threshold; |
|
|
|
|
|
dec = xvid_malloc(sizeof(DECODER), CACHE_LINE); |
|
|
if (dec == NULL) { |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
param->handle = dec; |
|
|
|
|
|
|
|
|
BitstreamInit(&bs, frame->bitstream, frame->length);//NIC |
|
|
|
|
|
//NIC |
|
|
BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, |
|
|
&fcode_backward, &intra_dc_threshold); |
|
|
|
|
|
|
|
|
param->width = dec->width; //NIC added |
|
|
param->height = dec->height; //NIC added |
|
|
//dec->width = param->width; //NIC commentate |
|
|
//dec->height = param->height; //NIC commentate |
|
|
|
|
|
dec->mb_width = (dec->width + 15) / 16; |
|
|
dec->mb_height = (dec->height + 15) / 16; |
|
|
|
|
|
dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE; |
|
|
dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE; |
|
|
dec->low_delay = 0; |
|
|
|
|
|
if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) { |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) { |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
// add by chenm001 <chenm001@163.com> |
|
|
// for support B-frame to reference last 2 frame |
|
|
if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) { |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) { |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
dec->mbs = |
|
|
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
|
|
CACHE_LINE); |
|
|
if (dec->mbs == NULL) { |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
|
|
|
|
|
// add by chenm001 <chenm001@163.com> |
|
|
// for skip MB flag |
|
|
dec->last_mbs = |
|
|
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
|
|
CACHE_LINE); |
|
|
if (dec->last_mbs == NULL) { |
|
|
xvid_free(dec->mbs); |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
|
|
|
|
|
init_timer(); |
|
|
|
|
|
// add by chenm001 <chenm001@163.com> |
|
|
// for support B-frame to save reference frame's time |
|
|
dec->frames = -1; |
|
|
dec->time = dec->time_base = dec->last_time_base = 0; |
|
|
|
|
|
return XVID_ERR_OK; |
|
|
} |
|
|
/* **************************************************************** |
|
|
END NIC |
|
|
**************************************************************** */ |
|
|
|
|
181 |
int |
int |
182 |
decoder_destroy(DECODER * dec) |
decoder_destroy(DECODER * dec) |
183 |
{ |
{ |
213 |
const uint32_t cbp, |
const uint32_t cbp, |
214 |
Bitstream * bs, |
Bitstream * bs, |
215 |
const uint32_t quant, |
const uint32_t quant, |
216 |
const uint32_t intra_dc_threshold) |
const uint32_t intra_dc_threshold, |
217 |
|
const unsigned int bound) |
218 |
{ |
{ |
219 |
|
|
220 |
DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
240 |
|
|
241 |
start_timer(); |
start_timer(); |
242 |
predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64], |
predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64], |
243 |
iQuant, iDcScaler, predictors); |
iQuant, iDcScaler, predictors, bound); |
244 |
if (!acpred_flag) { |
if (!acpred_flag) { |
245 |
pMB->acpred_directions[i] = 0; |
pMB->acpred_directions[i] = 0; |
246 |
} |
} |
259 |
|
|
260 |
block[i * 64 + 0] = dc_dif; |
block[i * 64 + 0] = dc_dif; |
261 |
start_coeff = 1; |
start_coeff = 1; |
262 |
|
|
263 |
|
DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif); |
264 |
} else { |
} else { |
265 |
start_coeff = 0; |
start_coeff = 0; |
266 |
} |
} |
435 |
int quant, |
int quant, |
436 |
int intra_dc_threshold) |
int intra_dc_threshold) |
437 |
{ |
{ |
438 |
|
uint32_t bound; |
439 |
uint32_t x, y; |
uint32_t x, y; |
440 |
|
|
441 |
|
bound = 0; |
442 |
|
|
443 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
444 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
445 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb; |
|
|
|
446 |
uint32_t mcbpc; |
uint32_t mcbpc; |
447 |
uint32_t cbpc; |
uint32_t cbpc; |
448 |
uint32_t acpred_flag; |
uint32_t acpred_flag; |
449 |
uint32_t cbpy; |
uint32_t cbpy; |
450 |
uint32_t cbp; |
uint32_t cbp; |
451 |
|
|
452 |
|
while (BitstreamShowBits(bs, 9) == 1) |
453 |
|
BitstreamSkip(bs, 9); |
454 |
|
|
455 |
|
if (check_resync_marker(bs, 0)) |
456 |
|
{ |
457 |
|
bound = read_video_packet_header(bs, 0, &quant); |
458 |
|
x = bound % dec->mb_width; |
459 |
|
y = bound / dec->mb_width; |
460 |
|
} |
461 |
|
mb = &dec->mbs[y * dec->mb_width + x]; |
462 |
|
|
463 |
|
DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); |
464 |
|
|
465 |
mcbpc = get_mcbpc_intra(bs); |
mcbpc = get_mcbpc_intra(bs); |
466 |
mb->mode = mcbpc & 7; |
mb->mode = mcbpc & 7; |
467 |
cbpc = (mcbpc >> 4); |
cbpc = (mcbpc >> 4); |
468 |
|
|
469 |
acpred_flag = BitstreamGetBit(bs); |
acpred_flag = BitstreamGetBit(bs); |
470 |
|
|
|
if (mb->mode == MODE_STUFFING) { |
|
|
DEBUG("-- STUFFING ?"); |
|
|
continue; |
|
|
} |
|
|
|
|
471 |
cbpy = get_cbpy(bs, 1); |
cbpy = get_cbpy(bs, 1); |
472 |
cbp = (cbpy << 2) | cbpc; |
cbp = (cbpy << 2) | cbpc; |
473 |
|
|
480 |
} |
} |
481 |
} |
} |
482 |
mb->quant = quant; |
mb->quant = quant; |
483 |
|
mb->mvs[0].x = mb->mvs[0].y = |
484 |
|
mb->mvs[1].x = mb->mvs[1].y = |
485 |
|
mb->mvs[2].x = mb->mvs[2].y = |
486 |
|
mb->mvs[3].x = mb->mvs[3].y =0; |
487 |
|
|
488 |
if (dec->interlacing) { |
if (dec->interlacing) { |
489 |
mb->field_dct = BitstreamGetBit(bs); |
mb->field_dct = BitstreamGetBit(bs); |
491 |
} |
} |
492 |
|
|
493 |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
494 |
intra_dc_threshold); |
intra_dc_threshold, bound); |
495 |
} |
} |
496 |
|
if(dec->out_frm) |
497 |
|
output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width); |
498 |
|
|
499 |
} |
} |
500 |
|
|
501 |
} |
} |
508 |
int y, |
int y, |
509 |
int k, |
int k, |
510 |
VECTOR * mv, |
VECTOR * mv, |
511 |
int fcode) |
int fcode, |
512 |
|
const int bound) |
513 |
{ |
{ |
514 |
|
|
515 |
int scale_fac = 1 << (fcode - 1); |
int scale_fac = 1 << (fcode - 1); |
517 |
int low = ((-32) * scale_fac); |
int low = ((-32) * scale_fac); |
518 |
int range = (64 * scale_fac); |
int range = (64 * scale_fac); |
519 |
|
|
520 |
VECTOR pmv[4]; |
VECTOR pmv; |
|
int32_t psad[4]; |
|
|
|
|
521 |
int mv_x, mv_y; |
int mv_x, mv_y; |
|
int pmv_x, pmv_y; |
|
|
|
|
|
|
|
|
get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad); |
|
522 |
|
|
523 |
pmv_x = pmv[0].x; |
pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k); |
|
pmv_y = pmv[0].y; |
|
524 |
|
|
525 |
mv_x = get_mv(bs, fcode); |
mv_x = get_mv(bs, fcode); |
526 |
mv_y = get_mv(bs, fcode); |
mv_y = get_mv(bs, fcode); |
527 |
|
|
528 |
mv_x += pmv_x; |
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y); |
529 |
mv_y += pmv_y; |
|
530 |
|
mv_x += pmv.x; |
531 |
|
mv_y += pmv.y; |
532 |
|
|
533 |
if (mv_x < low) { |
if (mv_x < low) { |
534 |
mv_x += range; |
mv_x += range; |
558 |
{ |
{ |
559 |
|
|
560 |
uint32_t x, y; |
uint32_t x, y; |
561 |
|
uint32_t bound; |
562 |
|
int cp_mb, st_mb; |
563 |
|
|
564 |
start_timer(); |
start_timer(); |
565 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
566 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height, dec->interlacing); |
567 |
stop_edges_timer(); |
stop_edges_timer(); |
568 |
|
|
569 |
|
bound = 0; |
570 |
|
|
571 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
572 |
|
cp_mb = st_mb = 0; |
573 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
574 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb; |
575 |
|
|
576 |
|
// skip stuffing |
577 |
|
while (BitstreamShowBits(bs, 10) == 1) |
578 |
|
BitstreamSkip(bs, 10); |
579 |
|
|
580 |
|
if (check_resync_marker(bs, fcode - 1)) |
581 |
|
{ |
582 |
|
bound = read_video_packet_header(bs, fcode - 1, &quant); |
583 |
|
x = bound % dec->mb_width; |
584 |
|
y = bound / dec->mb_width; |
585 |
|
} |
586 |
|
mb = &dec->mbs[y * dec->mb_width + x]; |
587 |
|
|
588 |
|
DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); |
589 |
|
|
590 |
//if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded |
//if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded |
591 |
if (!(BitstreamGetBit(bs))) // not_coded |
if (!(BitstreamGetBit(bs))) // not_coded |
597 |
uint32_t cbp; |
uint32_t cbp; |
598 |
uint32_t intra; |
uint32_t intra; |
599 |
|
|
600 |
|
cp_mb++; |
601 |
mcbpc = get_mcbpc_inter(bs); |
mcbpc = get_mcbpc_inter(bs); |
602 |
mb->mode = mcbpc & 7; |
mb->mode = mcbpc & 7; |
603 |
cbpc = (mcbpc >> 4); |
cbpc = (mcbpc >> 4); |
604 |
|
|
605 |
|
DPRINTF(DPRINTF_MB, "mode %i", mb->mode); |
606 |
|
DPRINTF(DPRINTF_MB, "cbpc %i", cbpc); |
607 |
acpred_flag = 0; |
acpred_flag = 0; |
608 |
|
|
609 |
intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q); |
intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q); |
612 |
acpred_flag = BitstreamGetBit(bs); |
acpred_flag = BitstreamGetBit(bs); |
613 |
} |
} |
614 |
|
|
|
if (mb->mode == MODE_STUFFING) { |
|
|
DEBUG("-- STUFFING ?"); |
|
|
continue; |
|
|
} |
|
|
|
|
615 |
cbpy = get_cbpy(bs, intra); |
cbpy = get_cbpy(bs, intra); |
616 |
|
DPRINTF(DPRINTF_MB, "cbpy %i", cbpy); |
617 |
|
|
618 |
cbp = (cbpy << 2) | cbpc; |
cbp = (cbpy << 2) | cbpc; |
619 |
|
|
620 |
if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) { |
if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) { |
621 |
quant += dquant_table[BitstreamGetBits(bs, 2)]; |
int dquant = dquant_table[BitstreamGetBits(bs, 2)]; |
622 |
|
DPRINTF(DPRINTF_MB, "dquant %i", dquant); |
623 |
|
quant += dquant; |
624 |
if (quant > 31) { |
if (quant > 31) { |
625 |
quant = 31; |
quant = 31; |
626 |
} else if (mb->quant < 1) { |
} else if (quant < 1) { |
627 |
quant = 1; |
quant = 1; |
628 |
} |
} |
629 |
|
DPRINTF(DPRINTF_MB, "quant %i", quant); |
630 |
} |
} |
631 |
mb->quant = quant; |
mb->quant = quant; |
632 |
|
|
650 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
651 |
if (dec->interlacing && mb->field_pred) { |
if (dec->interlacing && mb->field_pred) { |
652 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
653 |
fcode); |
fcode, bound); |
654 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], |
655 |
fcode); |
fcode, bound); |
656 |
} else { |
} else { |
657 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
658 |
fcode); |
fcode, bound); |
659 |
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
660 |
mb->mvs[0].x; |
mb->mvs[0].x; |
661 |
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
663 |
} |
} |
664 |
} else if (mb->mode == |
} else if (mb->mode == |
665 |
MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) { |
MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) { |
666 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound); |
667 |
get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode); |
get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound); |
668 |
get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode); |
get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound); |
669 |
get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode); |
get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound); |
670 |
} else // MODE_INTRA, MODE_INTRA_Q |
} else // MODE_INTRA, MODE_INTRA_Q |
671 |
{ |
{ |
672 |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
674 |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
675 |
0; |
0; |
676 |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
677 |
intra_dc_threshold); |
intra_dc_threshold, bound); |
678 |
continue; |
continue; |
679 |
} |
} |
680 |
|
|
724 |
dec->refn[0].v + |
dec->refn[0].v + |
725 |
(8 * y) * dec->edged_width / 2 + (8 * x), |
(8 * y) * dec->edged_width / 2 + (8 * x), |
726 |
dec->edged_width / 2); |
dec->edged_width / 2); |
|
|
|
727 |
stop_transfer_timer(); |
stop_transfer_timer(); |
728 |
|
if(dec->out_frm && cp_mb > 0) { |
729 |
|
output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); |
730 |
|
cp_mb = 0; |
731 |
|
} |
732 |
|
st_mb = x+1; |
733 |
} |
} |
734 |
} |
} |
735 |
|
if(dec->out_frm && cp_mb > 0) |
736 |
|
output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); |
737 |
} |
} |
738 |
} |
} |
739 |
|
|
811 |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
812 |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
813 |
|
|
814 |
|
|
815 |
if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
816 |
uv_dx = pMB->mvs[0].x; |
uv_dx = pMB->mvs[0].x; |
817 |
uv_dy = pMB->mvs[0].y; |
uv_dy = pMB->mvs[0].y; |
905 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
906 |
const uint32_t x_pos, |
const uint32_t x_pos, |
907 |
const uint32_t y_pos, |
const uint32_t y_pos, |
|
const uint32_t cbp, |
|
908 |
Bitstream * bs) |
Bitstream * bs) |
909 |
{ |
{ |
910 |
|
|
919 |
int b_uv_dx, b_uv_dy; |
int b_uv_dx, b_uv_dy; |
920 |
uint32_t i; |
uint32_t i; |
921 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
922 |
|
const uint32_t cbp = pMB->cbp; |
923 |
|
|
924 |
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
925 |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
926 |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
927 |
|
|
928 |
|
|
929 |
if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
930 |
uv_dx = pMB->mvs[0].x; |
uv_dx = pMB->mvs[0].x; |
931 |
uv_dy = pMB->mvs[0].y; |
uv_dy = pMB->mvs[0].y; |
1015 |
stride2); |
stride2); |
1016 |
interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos, |
interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos, |
1017 |
stride2); |
stride2); |
|
|
|
1018 |
stop_comp_timer(); |
stop_comp_timer(); |
1019 |
|
|
1020 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
1105 |
int fcode_forward, |
int fcode_forward, |
1106 |
int fcode_backward) |
int fcode_backward) |
1107 |
{ |
{ |
|
|
|
1108 |
uint32_t x, y; |
uint32_t x, y; |
1109 |
VECTOR mv, zeromv; |
VECTOR mv; |
1110 |
|
const VECTOR zeromv = {0,0}; |
1111 |
|
#ifdef BFRAMES_DEC_DEBUG |
1112 |
|
FILE *fp; |
1113 |
|
static char first=0; |
1114 |
|
#define BFRAME_DEBUG if (!first && fp){ \ |
1115 |
|
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \ |
1116 |
|
} |
1117 |
|
#endif |
1118 |
|
|
1119 |
start_timer(); |
start_timer(); |
1120 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1121 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height, dec->interlacing); |
1122 |
//image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
1123 |
|
dec->width, dec->height, dec->interlacing); |
1124 |
stop_edges_timer(); |
stop_edges_timer(); |
1125 |
|
|
1126 |
|
#ifdef BFRAMES_DEC_DEBUG |
1127 |
|
if (!first){ |
1128 |
|
fp=fopen("C:\\XVIDDBG.TXT","w"); |
1129 |
|
} |
1130 |
|
#endif |
1131 |
|
|
1132 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
1133 |
// Initialize Pred Motion Vector |
// Initialize Pred Motion Vector |
1134 |
dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0; |
dec->p_fmv = dec->p_bmv = zeromv; |
1135 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
1136 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
1137 |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
1138 |
|
|
1139 |
mb->mvs[0].x = mb->mvs[0].y = zeromv.x = zeromv.y = mv.x = mv.y = |
mv = |
1140 |
0; |
mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = |
1141 |
|
mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv; |
1142 |
|
|
1143 |
// the last P_VOP is skip macroblock ? |
// the last P_VOP is skip macroblock ? |
1144 |
if (last_mb->mode == MODE_NOT_CODED) { |
if (last_mb->mode == MODE_NOT_CODED) { |
1145 |
//DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
//DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
|
mb->mb_type = MODE_FORWARD; |
|
1146 |
mb->cbp = 0; |
mb->cbp = 0; |
1147 |
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
#ifdef BFRAMES_DEC_DEBUG |
1148 |
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
mb->mb_type = MODE_NOT_CODED; |
1149 |
mb->quant = 8; |
BFRAME_DEBUG |
1150 |
|
#endif |
1151 |
|
mb->mb_type = MODE_FORWARD; |
1152 |
|
mb->quant = last_mb->quant; |
1153 |
|
//mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1154 |
|
//mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1155 |
|
|
1156 |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1); |
1157 |
continue; |
continue; |
1158 |
} |
} |
|
//t=BitstreamShowBits(bs,32); |
|
1159 |
|
|
1160 |
if (!BitstreamGetBit(bs)) { // modb=='0' |
if (!BitstreamGetBit(bs)) { // modb=='0' |
1161 |
const uint8_t modb2 = BitstreamGetBit(bs); |
const uint8_t modb2 = BitstreamGetBit(bs); |
1172 |
|
|
1173 |
if (quant > 31) { |
if (quant > 31) { |
1174 |
quant = 31; |
quant = 31; |
1175 |
} else if (mb->quant < 1) { |
} else if (quant < 1) { |
1176 |
quant = 1; |
quant = 1; |
1177 |
} |
} |
|
} else { |
|
|
quant = 8; |
|
1178 |
} |
} |
|
mb->quant = quant; |
|
1179 |
} else { |
} else { |
1180 |
mb->mb_type = MODE_DIRECT_NONE_MV; |
mb->mb_type = MODE_DIRECT_NONE_MV; |
1181 |
mb->cbp = 0; |
mb->cbp = 0; |
1182 |
} |
} |
1183 |
|
|
1184 |
mb->mode = MODE_INTER; |
mb->quant = quant; |
1185 |
|
mb->mode = MODE_INTER4V; |
1186 |
//DEBUG1("Switch bm_type=",mb->mb_type); |
//DEBUG1("Switch bm_type=",mb->mb_type); |
1187 |
|
|
1188 |
|
#ifdef BFRAMES_DEC_DEBUG |
1189 |
|
BFRAME_DEBUG |
1190 |
|
#endif |
1191 |
switch (mb->mb_type) { |
switch (mb->mb_type) { |
1192 |
case MODE_DIRECT: |
case MODE_DIRECT: |
1193 |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv); |
get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv); |
1194 |
|
|
1195 |
case MODE_DIRECT_NONE_MV: |
case MODE_DIRECT_NONE_MV: |
1196 |
{ // Because this file is a C file not C++ so I use '{' to define var |
{ // Because this file is a C file not C++ so I use '{' to define var |
1197 |
const int64_t TRB = dec->time_pp - dec->time_bp, TRD = |
const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp; |
|
dec->time_pp; |
|
1198 |
int i; |
int i; |
1199 |
|
|
1200 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { |
1201 |
mb->mvs[i].x = |
mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) |
1202 |
(int32_t) ((TRB * last_mb->mvs[i].x) / TRD + |
/ TRD + mv.x); |
1203 |
mb->mvs[0].x); |
mb->b_mvs[i].x = (int32_t) ((mv.x == 0) |
1204 |
mb->b_mvs[i].x = |
? ((TRB - TRD) * last_mb->mvs[i].x) |
1205 |
(int32_t) ((mb->mvs[0].x == |
/ TRD |
1206 |
0) ? ((TRB - |
: mb->mvs[i].x - last_mb->mvs[i].x); |
1207 |
TRD) * last_mb->mvs[i].x) / |
mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) |
1208 |
TRD : mb->mvs[i].x - last_mb->mvs[i].x); |
/ TRD + mv.y); |
1209 |
mb->mvs[i].y = |
mb->b_mvs[i].y = (int32_t) ((mv.y == 0) |
1210 |
(int32_t) ((TRB * last_mb->mvs[i].y) / TRD + |
? ((TRB - TRD) * last_mb->mvs[i].y) |
1211 |
mb->mvs[0].y); |
/ TRD |
1212 |
mb->b_mvs[i].y = |
: mb->mvs[i].y - last_mb->mvs[i].y); |
|
(int32_t) ((mb->mvs[0].y == |
|
|
0) ? ((TRB - |
|
|
TRD) * last_mb->mvs[i].y) / |
|
|
TRD : mb->mvs[i].y - last_mb->mvs[i].y); |
|
1213 |
} |
} |
1214 |
//DEBUG("B-frame Direct!\n"); |
//DEBUG("B-frame Direct!\n"); |
1215 |
} |
} |
|
mb->mode = MODE_INTER4V; |
|
1216 |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1217 |
mb, x, y, mb->cbp, bs); |
mb, x, y, bs); |
1218 |
break; |
break; |
1219 |
|
|
1220 |
case MODE_INTERPOLATE: |
case MODE_INTERPOLATE: |
1221 |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
1222 |
dec->p_fmv); |
dec->p_fmv); |
1223 |
dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
|
mb->mvs[0].x; |
|
|
dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
|
|
mb->mvs[0].y; |
|
1224 |
|
|
1225 |
get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], |
get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], |
1226 |
fcode_backward, dec->p_bmv); |
fcode_backward, dec->p_bmv); |
1227 |
dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x = |
dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = |
1228 |
mb->b_mvs[3].x = mb->b_mvs[0].x; |
mb->b_mvs[3] = mb->b_mvs[0]; |
|
dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y = |
|
|
mb->b_mvs[3].y = mb->b_mvs[0].y; |
|
1229 |
|
|
1230 |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1231 |
mb, x, y, mb->cbp, bs); |
mb, x, y, bs); |
1232 |
//DEBUG("B-frame Bidir!\n"); |
//DEBUG("B-frame Bidir!\n"); |
1233 |
break; |
break; |
1234 |
|
|
1235 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
1236 |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, |
1237 |
dec->p_bmv); |
dec->p_bmv); |
1238 |
dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
|
mb->mvs[0].x; |
|
|
dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
|
|
mb->mvs[0].y; |
|
1239 |
|
|
1240 |
|
mb->mode = MODE_INTER; |
1241 |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
1242 |
//DEBUG("B-frame Backward!\n"); |
//DEBUG("B-frame Backward!\n"); |
1243 |
break; |
break; |
1245 |
case MODE_FORWARD: |
case MODE_FORWARD: |
1246 |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
1247 |
dec->p_fmv); |
dec->p_fmv); |
1248 |
dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
|
mb->mvs[0].x; |
|
|
dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
|
|
mb->mvs[0].y; |
|
1249 |
|
|
1250 |
|
mb->mode = MODE_INTER; |
1251 |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1252 |
//DEBUG("B-frame Forward!\n"); |
//DEBUG("B-frame Forward!\n"); |
1253 |
break; |
break; |
1254 |
|
|
1255 |
default: |
default: |
1256 |
DEBUG1("Not support B-frame mb_type =", mb->mb_type); |
//DEBUG1("Not support B-frame mb_type =", mb->mb_type); |
1257 |
|
; |
1258 |
} |
} |
1259 |
|
|
1260 |
} // end of FOR |
} // end of FOR |
1261 |
} |
} |
1262 |
|
#ifdef BFRAMES_DEC_DEBUG |
1263 |
|
if (!first){ |
1264 |
|
first=1; |
1265 |
|
if (fp) |
1266 |
|
fclose(fp); |
1267 |
|
} |
1268 |
|
#endif |
1269 |
} |
} |
1270 |
|
|
1271 |
// swap two MACROBLOCK array |
// swap two MACROBLOCK array |
1294 |
|
|
1295 |
start_global_timer(); |
start_global_timer(); |
1296 |
|
|
1297 |
|
dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL; |
1298 |
|
|
1299 |
BitstreamInit(&bs, frame->bitstream, frame->length); |
BitstreamInit(&bs, frame->bitstream, frame->length); |
1300 |
|
|
1301 |
// add by chenm001 <chenm001@163.com> |
// add by chenm001 <chenm001@163.com> |
1311 |
case P_VOP: |
case P_VOP: |
1312 |
decoder_pframe(dec, &bs, rounding, quant, fcode_forward, |
decoder_pframe(dec, &bs, rounding, quant, fcode_forward, |
1313 |
intra_dc_threshold); |
intra_dc_threshold); |
1314 |
printf("P_VOP Time=%ld\n",dec->time); |
#ifdef BFRAMES_DEC |
1315 |
DEBUG1("P_VOP Time=", dec->time); |
DEBUG1("P_VOP Time=", dec->time); |
1316 |
|
#endif |
1317 |
break; |
break; |
1318 |
|
|
1319 |
case I_VOP: |
case I_VOP: |
1320 |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
1321 |
printf("I_VOP Time=%ld\n",dec->time); |
#ifdef BFRAMES_DEC |
1322 |
DEBUG1("I_VOP Time=", dec->time); |
DEBUG1("I_VOP Time=", dec->time); |
1323 |
|
#endif |
1324 |
break; |
break; |
1325 |
|
|
1326 |
case B_VOP: |
case B_VOP: |
1327 |
#ifdef BFRAMES_DEC |
#ifdef BFRAMES_DEC |
1328 |
if (dec->time_pp > dec->time_bp) { |
if (dec->time_pp > dec->time_bp) { |
|
printf("I_VOP Time=%ld\n",dec->time); |
|
1329 |
DEBUG1("B_VOP Time=", dec->time); |
DEBUG1("B_VOP Time=", dec->time); |
1330 |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1331 |
} else { |
} else { |
1332 |
DEBUG("broken B-frame!"); |
DEBUG("broken B-frame!"); |
1333 |
} |
} |
1334 |
#else |
#else |
|
printf("Che minchia ne so\n"); |
|
1335 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1336 |
#endif |
#endif |
1337 |
break; |
break; |
1339 |
case N_VOP: // vop not coded |
case N_VOP: // vop not coded |
1340 |
// when low_delay==0, N_VOP's should interpolate between the past and future frames |
// when low_delay==0, N_VOP's should interpolate between the past and future frames |
1341 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
|
printf("N_VOP vop not coded\n"); |
|
1342 |
break; |
break; |
1343 |
|
|
1344 |
default: |
default: |
1345 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
1346 |
} |
} |
1347 |
|
|
1348 |
|
#ifdef BFRAMES_DEC_DEBUG |
1349 |
|
if (frame->length != BitstreamPos(&bs) / 8){ |
1350 |
|
DEBUG2("InLen/UseLen",frame->length, BitstreamPos(&bs) / 8); |
1351 |
|
} |
1352 |
|
#endif |
1353 |
frame->length = BitstreamPos(&bs) / 8; |
frame->length = BitstreamPos(&bs) / 8; |
1354 |
|
|
1355 |
|
|
1356 |
#ifdef BFRAMES_DEC |
#ifdef BFRAMES_DEC |
1357 |
// test if no B_VOP |
// test if no B_VOP |
1358 |
if (dec->low_delay) { |
if (dec->low_delay) { |
1359 |
#endif |
#endif |
1360 |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
1361 |
frame->image, frame->stride, frame->colorspace); |
frame->image, frame->stride, frame->colorspace); |
1362 |
|
|
1363 |
#ifdef BFRAMES_DEC |
#ifdef BFRAMES_DEC |
1364 |
} else { |
} else { |
1365 |
if (dec->frames >= 1) { |
if (dec->frames >= 0) { |
1366 |
start_timer(); |
start_timer(); |
1367 |
if ((vop_type == I_VOP || vop_type == P_VOP)) { |
if ((vop_type == I_VOP || vop_type == P_VOP)) { |
1368 |
image_output(&dec->refn[0], dec->width, dec->height, |
image_output(&dec->refn[0], dec->width, dec->height, |
1381 |
if (vop_type == I_VOP || vop_type == P_VOP) { |
if (vop_type == I_VOP || vop_type == P_VOP) { |
1382 |
image_swap(&dec->refn[0], &dec->refn[1]); |
image_swap(&dec->refn[0], &dec->refn[1]); |
1383 |
image_swap(&dec->cur, &dec->refn[0]); |
image_swap(&dec->cur, &dec->refn[0]); |
1384 |
|
|
1385 |
// swap MACROBLOCK |
// swap MACROBLOCK |
1386 |
if (dec->low_delay && vop_type == P_VOP) |
// the Divx will not set the low_delay flage some times |
1387 |
|
// so follow code will wrong to not swap at that time |
1388 |
|
// this will broken bitstream! so I'm change it, |
1389 |
|
// But that is not the best way! can anyone tell me how |
1390 |
|
// to do another way? |
1391 |
|
// 18-07-2002 MinChen<chenm001@163.com> |
1392 |
|
//if (!dec->low_delay && vop_type == P_VOP) |
1393 |
|
if (vop_type == P_VOP) |
1394 |
mb_swap(&dec->mbs, &dec->last_mbs); |
mb_swap(&dec->mbs, &dec->last_mbs); |
1395 |
} |
} |
1396 |
|
|