55 |
* 22.12.2001 lock based interpolation |
* 22.12.2001 lock based interpolation |
56 |
* 01.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
* 01.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
57 |
* |
* |
58 |
* $Id: decoder.c,v 1.37.2.26 2003-01-04 06:14:32 suxen_drol Exp $ |
* $Id: decoder.c,v 1.37.2.30 2003-01-12 13:11:50 chl Exp $ |
59 |
* |
* |
60 |
*************************************************************************/ |
*************************************************************************/ |
61 |
|
|
62 |
|
#include <stdio.h> |
63 |
#include <stdlib.h> |
#include <stdlib.h> |
64 |
#include <string.h> |
#include <string.h> |
65 |
|
|
105 |
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
106 |
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
107 |
|
|
108 |
|
image_destroy(&dec->gmc, dec->edged_width, dec->edged_height); |
109 |
|
|
110 |
if (dec->last_mbs) |
if (dec->last_mbs) |
111 |
xvid_free(dec->last_mbs); |
xvid_free(dec->last_mbs); |
112 |
if (dec->mbs) |
if (dec->mbs) |
156 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
157 |
} |
} |
158 |
|
|
159 |
|
if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) { |
160 |
|
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
161 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
162 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
163 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
164 |
|
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
165 |
|
xvid_free(dec); |
166 |
|
return XVID_ERR_MEMORY; |
167 |
|
} |
168 |
|
|
169 |
dec->mbs = |
dec->mbs = |
170 |
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
171 |
CACHE_LINE); |
CACHE_LINE); |
224 |
image_null(&dec->tmp); |
image_null(&dec->tmp); |
225 |
image_null(&dec->qtmp); |
image_null(&dec->qtmp); |
226 |
|
|
227 |
|
/* image based GMC */ |
228 |
|
image_null(&dec->gmc); |
229 |
|
|
230 |
|
|
231 |
dec->mbs = NULL; |
dec->mbs = NULL; |
232 |
dec->last_mbs = NULL; |
dec->last_mbs = NULL; |
233 |
|
|
254 |
{ |
{ |
255 |
xvid_free(dec->last_mbs); |
xvid_free(dec->last_mbs); |
256 |
xvid_free(dec->mbs); |
xvid_free(dec->mbs); |
257 |
|
|
258 |
|
image_destroy(&dec->gmc, dec->edged_width, dec->edged_height); /* image based GMC */ |
259 |
|
|
260 |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
261 |
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
262 |
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
412 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
413 |
const uint32_t x_pos, |
const uint32_t x_pos, |
414 |
const uint32_t y_pos, |
const uint32_t y_pos, |
415 |
const uint32_t acpred_flag, |
const uint32_t fcode, |
416 |
const uint32_t cbp, |
const uint32_t cbp, |
417 |
Bitstream * bs, |
Bitstream * bs, |
418 |
const uint32_t quant, |
const uint32_t quant, |
450 |
} |
} |
451 |
|
|
452 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
uv_dx = mv[0].x; |
|
|
uv_dy = mv[0].y; |
|
453 |
|
|
454 |
if (dec->quarterpel) |
uv_dx = mv[0].x / (1 + dec->quarterpel); |
455 |
{ |
uv_dy = mv[0].y / (1 + dec->quarterpel); |
|
uv_dx /= 2; |
|
|
uv_dy /= 2; |
|
|
} |
|
456 |
|
|
457 |
uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; |
uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; |
458 |
uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; |
uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; |
621 |
stop_transfer_timer(); |
stop_transfer_timer(); |
622 |
} |
} |
623 |
|
|
624 |
|
static __inline int gmc_sanitize(int value, int quarterpel, int fcode) |
625 |
|
{ |
626 |
|
int length = 1 << (fcode+4); |
627 |
|
|
628 |
|
// if (quarterpel) value *= 2; |
629 |
|
|
630 |
|
if (value < -length) |
631 |
|
return -length; |
632 |
|
else if (value >= length) |
633 |
|
return length-1; |
634 |
|
else return value; |
635 |
|
} |
636 |
|
|
637 |
|
|
638 |
|
static void |
639 |
|
decoder_mbgmc(DECODER * dec, |
640 |
|
MACROBLOCK * const pMB, |
641 |
|
const uint32_t x_pos, |
642 |
|
const uint32_t y_pos, |
643 |
|
const uint32_t fcode, |
644 |
|
const uint32_t cbp, |
645 |
|
Bitstream * bs, |
646 |
|
const uint32_t quant, |
647 |
|
const uint32_t rounding, |
648 |
|
const int reduced_resolution) /* no reduced res support */ |
649 |
|
{ |
650 |
|
|
651 |
|
DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
652 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
653 |
|
|
654 |
|
const uint32_t stride = dec->edged_width; |
655 |
|
const uint32_t stride2 = stride / 2; |
656 |
|
const uint32_t next_block = stride * (reduced_resolution ? 16 : 8); |
657 |
|
uint32_t i; |
658 |
|
const uint32_t iQuant = pMB->quant; |
659 |
|
uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
660 |
|
uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
661 |
|
uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
662 |
|
|
663 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
664 |
|
|
665 |
|
start_timer(); |
666 |
|
|
667 |
|
/* this is where the calculations are done */ |
668 |
|
|
669 |
|
{ |
670 |
|
pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos, |
671 |
|
stride, stride2, dec->quarterpel, rounding, &dec->cur); |
672 |
|
|
673 |
|
pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode); |
674 |
|
pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode); |
675 |
|
} |
676 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
677 |
|
|
678 |
|
/* transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos << 4), stride); |
679 |
|
transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos << 3), stride2); |
680 |
|
transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2); |
681 |
|
*/ |
682 |
|
|
683 |
|
|
684 |
|
stop_transfer_timer(); |
685 |
|
|
686 |
|
if (!cbp) return; |
687 |
|
|
688 |
|
for (i = 0; i < 6; i++) { |
689 |
|
int direction = dec->alternate_vertical_scan ? 2 : 0; |
690 |
|
|
691 |
|
if (cbp & (1 << (5 - i))) // coded |
692 |
|
{ |
693 |
|
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
694 |
|
|
695 |
|
start_timer(); |
696 |
|
get_inter_block(bs, &block[i * 64], direction); |
697 |
|
stop_coding_timer(); |
698 |
|
|
699 |
|
start_timer(); |
700 |
|
if (dec->quant_type == 0) { |
701 |
|
dequant_inter(&data[i * 64], &block[i * 64], iQuant); |
702 |
|
} else { |
703 |
|
dequant4_inter(&data[i * 64], &block[i * 64], iQuant); |
704 |
|
} |
705 |
|
stop_iquant_timer(); |
706 |
|
|
707 |
|
start_timer(); |
708 |
|
idct(&data[i * 64]); |
709 |
|
stop_idct_timer(); |
710 |
|
} |
711 |
|
} |
712 |
|
|
713 |
|
/* interlace + GMC is this possible ??? */ |
714 |
|
/* if (dec->interlacing && pMB->field_dct) { |
715 |
|
next_block = stride; |
716 |
|
stride *= 2; |
717 |
|
} |
718 |
|
*/ |
719 |
|
start_timer(); |
720 |
|
if (cbp & 32) |
721 |
|
transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
722 |
|
if (cbp & 16) |
723 |
|
transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
724 |
|
if (cbp & 8) |
725 |
|
transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
726 |
|
if (cbp & 4) |
727 |
|
transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride); |
728 |
|
if (cbp & 2) |
729 |
|
transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
730 |
|
if (cbp & 1) |
731 |
|
transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
732 |
|
stop_transfer_timer(); |
733 |
|
} |
734 |
|
|
735 |
|
|
736 |
void |
void |
737 |
decoder_iframe(DECODER * dec, |
decoder_iframe(DECODER * dec, |
839 |
mv.x = get_mv(bs, fcode); |
mv.x = get_mv(bs, fcode); |
840 |
mv.y = get_mv(bs, fcode); |
mv.y = get_mv(bs, fcode); |
841 |
|
|
842 |
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv.x, mv.y, pmv.x, pmv.y); |
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y); |
843 |
|
|
844 |
mv.x += pmv.x; |
mv.x += pmv.x; |
845 |
mv.y += pmv.y; |
mv.y += pmv.y; |
862 |
|
|
863 |
|
|
864 |
|
|
|
static __inline int gmc_sanitize(int value, int quarterpel, int fcode) |
|
|
{ |
|
|
int length = 1 << (fcode+4); |
|
|
|
|
|
if (quarterpel) value *= 2; |
|
865 |
|
|
|
if (value < -length) |
|
|
return -length; |
|
|
else if (value >= length) |
|
|
return length-1; |
|
|
else return value; |
|
|
} |
|
866 |
|
|
867 |
|
/* for P_VOP set gmc_warp to NULL */ |
|
/* for P_VOP set gmc_mv to NULL */ |
|
868 |
void |
void |
869 |
decoder_pframe(DECODER * dec, |
decoder_pframe(DECODER * dec, |
870 |
Bitstream * bs, |
Bitstream * bs, |
873 |
int quant, |
int quant, |
874 |
int fcode, |
int fcode, |
875 |
int intra_dc_threshold, |
int intra_dc_threshold, |
876 |
VECTOR * gmc_mv) |
const WARPPOINTS *const gmc_warp) |
877 |
{ |
{ |
878 |
|
|
879 |
uint32_t x, y; |
uint32_t x, y; |
893 |
dec->width, dec->height); |
dec->width, dec->height); |
894 |
stop_edges_timer(); |
stop_edges_timer(); |
895 |
|
|
896 |
|
if (gmc_warp) |
897 |
|
{ |
898 |
|
|
899 |
|
// accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 |
900 |
|
if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) ) |
901 |
|
{ |
902 |
|
fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n", |
903 |
|
dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy), |
904 |
|
dec->sprite_warping_points); |
905 |
|
} |
906 |
|
|
907 |
|
generate_GMCparameters( dec->sprite_warping_points, |
908 |
|
(2 << dec->sprite_warping_accuracy), gmc_warp, |
909 |
|
dec->width, dec->height, &dec->gmc_data); |
910 |
|
|
911 |
|
/* image warping is done block-based in decoder_mbgmc(), now */ |
912 |
|
/* |
913 |
|
generate_GMCimage(&dec->gmc_data, &dec->refn[0], |
914 |
|
mb_width, mb_height, |
915 |
|
dec->edged_width, dec->edged_width/2, |
916 |
|
fcode, dec->quarterpel, 0, |
917 |
|
rounding, dec->mbs, &dec->gmc); |
918 |
|
*/ |
919 |
|
} |
920 |
|
|
921 |
bound = 0; |
bound = 0; |
922 |
|
|
923 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
941 |
DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); |
DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); |
942 |
|
|
943 |
//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 |
944 |
if (!(BitstreamGetBit(bs))) // not_coded |
if (!(BitstreamGetBit(bs))) // block _is_ coded |
945 |
{ |
{ |
946 |
uint32_t mcbpc; |
uint32_t mcbpc; |
947 |
uint32_t cbpc; |
uint32_t cbpc; |
966 |
acpred_flag = BitstreamGetBit(bs); |
acpred_flag = BitstreamGetBit(bs); |
967 |
} |
} |
968 |
|
|
969 |
if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)) |
if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)) |
970 |
{ |
{ |
971 |
mcsel = BitstreamGetBit(bs); |
mcsel = BitstreamGetBit(bs); |
972 |
} |
} |
973 |
|
|
974 |
cbpy = get_cbpy(bs, intra); |
cbpy = get_cbpy(bs, intra); |
975 |
DPRINTF(DPRINTF_MB, "cbpy %i", cbpy); |
DPRINTF(DPRINTF_MB, "cbpy %i mcsel %i ", cbpy,mcsel); |
976 |
|
|
977 |
cbp = (cbpy << 2) | cbpc; |
cbp = (cbpy << 2) | cbpc; |
978 |
|
|
1008 |
} |
} |
1009 |
} |
} |
1010 |
|
|
1011 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
if (mcsel) { |
1012 |
|
decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant, |
1013 |
|
rounding, reduced_resolution); |
1014 |
|
continue; |
1015 |
|
|
1016 |
if (mcsel) |
} else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
|
{ |
|
|
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode); |
|
|
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode); |
|
1017 |
|
|
1018 |
} else if (dec->interlacing && mb->field_pred) { |
if (dec->interlacing && mb->field_pred) { |
1019 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
1020 |
fcode, bound); |
fcode, bound); |
1021 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], |
1023 |
} else { |
} else { |
1024 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
1025 |
fcode, bound); |
fcode, bound); |
1026 |
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
|
mb->mvs[0].x; |
|
|
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
|
|
mb->mvs[0].y; |
|
1027 |
} |
} |
1028 |
} else if (mb->mode == MODE_INTER4V ) { |
} else if (mb->mode == MODE_INTER4V ) { |
1029 |
|
|
1042 |
continue; |
continue; |
1043 |
} |
} |
1044 |
|
|
1045 |
decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant, |
1046 |
rounding, reduced_resolution); |
rounding, reduced_resolution); |
1047 |
|
|
1048 |
} |
} |
1049 |
else if (gmc_mv) /* not coded S_VOP macroblock */ |
else if (gmc_warp) /* a not coded S(GMC)-VOP macroblock */ |
1050 |
{ |
{ |
1051 |
mb->mode = MODE_NOT_CODED_GMC; |
mb->mode = MODE_NOT_CODED_GMC; |
1052 |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode); |
|
1053 |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode); |
start_timer(); |
1054 |
decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution); |
|
1055 |
|
decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant, |
1056 |
|
rounding, reduced_resolution); |
1057 |
|
|
1058 |
|
stop_transfer_timer(); |
1059 |
|
|
1060 |
|
if(dec->out_frm && cp_mb > 0) { |
1061 |
|
output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); |
1062 |
|
cp_mb = 0; |
1063 |
|
} |
1064 |
|
st_mb = x+1; |
1065 |
} |
} |
1066 |
else /* not coded P_VOP macroblock */ |
else /* not coded P_VOP macroblock */ |
1067 |
{ |
{ |
1611 |
mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv; |
mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv; |
1612 |
|
|
1613 |
// skip if the co-located P_VOP macroblock is not coded |
// skip if the co-located P_VOP macroblock is not coded |
1614 |
// note: gmc+not_coded isn't skipped |
// if not codec in co-located S_VOP macroblock is _not_ automatically skipped |
1615 |
|
|
1616 |
if (last_mb->mode == MODE_NOT_CODED) { |
if (last_mb->mode == MODE_NOT_CODED) { |
1617 |
//DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
//DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
1785 |
uint32_t fcode_forward; |
uint32_t fcode_forward; |
1786 |
uint32_t fcode_backward; |
uint32_t fcode_backward; |
1787 |
uint32_t intra_dc_threshold; |
uint32_t intra_dc_threshold; |
1788 |
VECTOR gmc_mv[5]; |
WARPPOINTS gmc_warp; |
1789 |
uint32_t vop_type; |
uint32_t vop_type; |
1790 |
int success = 0; |
int success = 0; |
1791 |
int output = 0; |
int output = 0; |
1840 |
repeat: |
repeat: |
1841 |
|
|
1842 |
vop_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, |
vop_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, |
1843 |
&quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv); |
&quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp); |
1844 |
|
|
1845 |
DPRINTF(DPRINTF_HEADER, "vop_type=%i, packed=%i, time=%i, time_pp=%i, time_bp=%i", |
DPRINTF(DPRINTF_HEADER, "vop_type=%i, packed=%i, time=%lli, time_pp=%i, time_bp=%i", |
1846 |
vop_type, dec->packed_mode, dec->time, dec->time_pp, dec->time_bp); |
vop_type, dec->packed_mode, dec->time, dec->time_pp, dec->time_bp); |
1847 |
|
|
1848 |
if (vop_type == - 1) |
if (vop_type == - 1) |
1901 |
break; |
break; |
1902 |
case S_VOP : |
case S_VOP : |
1903 |
decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, |
decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, |
1904 |
fcode_forward, intra_dc_threshold, gmc_mv); |
fcode_forward, intra_dc_threshold, &gmc_warp); |
1905 |
break; |
break; |
1906 |
case N_VOP : |
case N_VOP : |
1907 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |