59 |
* |
* |
60 |
*************************************************************************/ |
*************************************************************************/ |
61 |
|
|
62 |
|
#include <stdio.h> |
63 |
#include <stdlib.h> |
#include <stdlib.h> |
64 |
#include <string.h> |
#include <string.h> |
65 |
|
|
69 |
|
|
70 |
#include "xvid.h" |
#include "xvid.h" |
71 |
#include "portab.h" |
#include "portab.h" |
72 |
|
#include "global.h" |
73 |
|
|
74 |
#include "decoder.h" |
#include "decoder.h" |
75 |
#include "bitstream/bitstream.h" |
#include "bitstream/bitstream.h" |
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); |
405 |
|
|
406 |
|
|
407 |
|
|
|
|
|
|
#define SIGN(X) (((X)>0)?1:-1) |
|
|
#define ABS(X) (((X)>0)?(X):-(X)) |
|
|
|
|
408 |
// decode an inter macroblock |
// decode an inter macroblock |
409 |
|
|
410 |
void |
void |
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]; |
622 |
} |
} |
623 |
|
|
624 |
|
|
625 |
|
static void |
626 |
|
decoder_mbgmc(DECODER * dec, |
627 |
|
MACROBLOCK * const pMB, |
628 |
|
const uint32_t x_pos, |
629 |
|
const uint32_t y_pos, |
630 |
|
const uint32_t acpred_flag, |
631 |
|
const uint32_t cbp, |
632 |
|
Bitstream * bs, |
633 |
|
const uint32_t quant, |
634 |
|
const uint32_t rounding, |
635 |
|
const int reduced_resolution) /* no reduced res support */ |
636 |
|
{ |
637 |
|
|
638 |
|
DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
639 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
640 |
|
|
641 |
|
const uint32_t stride = dec->edged_width; |
642 |
|
const uint32_t stride2 = stride / 2; |
643 |
|
const uint32_t next_block = stride * (reduced_resolution ? 16 : 8); |
644 |
|
uint32_t i; |
645 |
|
const uint32_t iQuant = pMB->quant; |
646 |
|
uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
647 |
|
uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
648 |
|
uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
649 |
|
|
650 |
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
651 |
|
|
652 |
|
start_timer(); |
653 |
|
transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos << 4), stride); |
654 |
|
transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos << 3), stride2); |
655 |
|
transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2); |
656 |
|
stop_transfer_timer(); |
657 |
|
|
658 |
|
if (!cbp) return; |
659 |
|
|
660 |
|
for (i = 0; i < 6; i++) { |
661 |
|
int direction = dec->alternate_vertical_scan ? 2 : 0; |
662 |
|
|
663 |
|
if (cbp & (1 << (5 - i))) // coded |
664 |
|
{ |
665 |
|
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
666 |
|
|
667 |
|
start_timer(); |
668 |
|
get_inter_block(bs, &block[i * 64], direction); |
669 |
|
stop_coding_timer(); |
670 |
|
|
671 |
|
start_timer(); |
672 |
|
if (dec->quant_type == 0) { |
673 |
|
dequant_inter(&data[i * 64], &block[i * 64], iQuant); |
674 |
|
} else { |
675 |
|
dequant4_inter(&data[i * 64], &block[i * 64], iQuant); |
676 |
|
} |
677 |
|
stop_iquant_timer(); |
678 |
|
|
679 |
|
start_timer(); |
680 |
|
idct(&data[i * 64]); |
681 |
|
stop_idct_timer(); |
682 |
|
} |
683 |
|
} |
684 |
|
|
685 |
|
/* interlace + GMC is this possible ??? */ |
686 |
|
/* if (dec->interlacing && pMB->field_dct) { |
687 |
|
next_block = stride; |
688 |
|
stride *= 2; |
689 |
|
} |
690 |
|
*/ |
691 |
|
start_timer(); |
692 |
|
if (cbp & 32) |
693 |
|
transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
694 |
|
if (cbp & 16) |
695 |
|
transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
696 |
|
if (cbp & 8) |
697 |
|
transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
698 |
|
if (cbp & 4) |
699 |
|
transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride); |
700 |
|
if (cbp & 2) |
701 |
|
transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
702 |
|
if (cbp & 1) |
703 |
|
transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
704 |
|
stop_transfer_timer(); |
705 |
|
} |
706 |
|
|
707 |
|
|
708 |
void |
void |
709 |
decoder_iframe(DECODER * dec, |
decoder_iframe(DECODER * dec, |
710 |
Bitstream * bs, |
Bitstream * bs, |
811 |
mv.x = get_mv(bs, fcode); |
mv.x = get_mv(bs, fcode); |
812 |
mv.y = get_mv(bs, fcode); |
mv.y = get_mv(bs, fcode); |
813 |
|
|
814 |
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); |
815 |
|
|
816 |
mv.x += pmv.x; |
mv.x += pmv.x; |
817 |
mv.y += pmv.y; |
mv.y += pmv.y; |
848 |
} |
} |
849 |
|
|
850 |
|
|
851 |
/* for P_VOP set gmc_mv to NULL */ |
/* for P_VOP set gmc_warp to NULL */ |
852 |
void |
void |
853 |
decoder_pframe(DECODER * dec, |
decoder_pframe(DECODER * dec, |
854 |
Bitstream * bs, |
Bitstream * bs, |
857 |
int quant, |
int quant, |
858 |
int fcode, |
int fcode, |
859 |
int intra_dc_threshold, |
int intra_dc_threshold, |
860 |
VECTOR * gmc_mv) |
const WARPPOINTS *const gmc_warp) |
861 |
{ |
{ |
862 |
|
|
863 |
uint32_t x, y; |
uint32_t x, y; |
866 |
uint32_t mb_width = dec->mb_width; |
uint32_t mb_width = dec->mb_width; |
867 |
uint32_t mb_height = dec->mb_height; |
uint32_t mb_height = dec->mb_height; |
868 |
|
|
869 |
|
static int framecount=0; |
870 |
if (reduced_resolution) |
if (reduced_resolution) |
871 |
{ |
{ |
872 |
mb_width = (dec->width + 31) / 32; |
mb_width = (dec->width + 31) / 32; |
878 |
dec->width, dec->height); |
dec->width, dec->height); |
879 |
stop_edges_timer(); |
stop_edges_timer(); |
880 |
|
|
881 |
|
if (gmc_warp) |
882 |
|
{ |
883 |
|
|
884 |
|
// accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 |
885 |
|
if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) ) |
886 |
|
{ |
887 |
|
fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n", |
888 |
|
dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy), |
889 |
|
dec->sprite_warping_points); |
890 |
|
} |
891 |
|
|
892 |
|
generate_GMCparameters( dec->sprite_warping_points, |
893 |
|
(2 << dec->sprite_warping_accuracy), gmc_warp, |
894 |
|
dec->width, dec->height, &dec->gmc_data); |
895 |
|
|
896 |
|
generate_GMCimage(&dec->gmc_data, &dec->refn[0], |
897 |
|
mb_width, mb_height, |
898 |
|
dec->edged_width, dec->edged_width/2, |
899 |
|
fcode, dec->quarterpel, 0, |
900 |
|
rounding, dec->mbs, &dec->gmc); |
901 |
|
|
902 |
|
} |
903 |
|
|
904 |
bound = 0; |
bound = 0; |
905 |
|
|
906 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
924 |
DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); |
DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); |
925 |
|
|
926 |
//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 |
927 |
if (!(BitstreamGetBit(bs))) // not_coded |
if (!(BitstreamGetBit(bs))) // block _is_ coded |
928 |
{ |
{ |
929 |
uint32_t mcbpc; |
uint32_t mcbpc; |
930 |
uint32_t cbpc; |
uint32_t cbpc; |
949 |
acpred_flag = BitstreamGetBit(bs); |
acpred_flag = BitstreamGetBit(bs); |
950 |
} |
} |
951 |
|
|
952 |
if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)) |
if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)) |
953 |
{ |
{ |
954 |
mcsel = BitstreamGetBit(bs); |
mcsel = BitstreamGetBit(bs); |
955 |
} |
} |
956 |
|
|
957 |
cbpy = get_cbpy(bs, intra); |
cbpy = get_cbpy(bs, intra); |
958 |
DPRINTF(DPRINTF_MB, "cbpy %i", cbpy); |
DPRINTF(DPRINTF_MB, "cbpy %i mcsel %i ", cbpy,mcsel); |
959 |
|
|
960 |
cbp = (cbpy << 2) | cbpc; |
cbp = (cbpy << 2) | cbpc; |
961 |
|
|
991 |
} |
} |
992 |
} |
} |
993 |
|
|
994 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
if (mcsel) { |
995 |
|
decoder_mbgmc(dec, mb, x, y, 0, cbp, bs, quant, |
996 |
|
rounding, reduced_resolution); |
997 |
|
continue; |
998 |
|
|
999 |
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); |
|
1000 |
|
|
1001 |
} else if (dec->interlacing && mb->field_pred) { |
if (dec->interlacing && mb->field_pred) { |
1002 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
1003 |
fcode, bound); |
fcode, bound); |
1004 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], |
1006 |
} else { |
} else { |
1007 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
1008 |
fcode, bound); |
fcode, bound); |
1009 |
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; |
|
1010 |
} |
} |
1011 |
} else if (mb->mode == MODE_INTER4V ) { |
} else if (mb->mode == MODE_INTER4V ) { |
1012 |
|
|
1025 |
continue; |
continue; |
1026 |
} |
} |
1027 |
|
|
1028 |
decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
decoder_mbinter(dec, mb, x, y, 0, cbp, bs, quant, |
1029 |
rounding, reduced_resolution); |
rounding, reduced_resolution); |
1030 |
|
|
1031 |
} |
} |
1032 |
else if (gmc_mv) /* not coded S_VOP macroblock */ |
else if (gmc_warp) /* a not coded S(GMC)-VOP macroblock */ |
1033 |
{ |
{ |
1034 |
mb->mode = MODE_NOT_CODED_GMC; |
mb->mode = MODE_NOT_CODED_GMC; |
1035 |
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); |
|
1036 |
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(); |
1037 |
decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution); |
|
1038 |
|
decoder_mbgmc(dec, mb, x, y, 0, 0x00, bs, quant, |
1039 |
|
rounding, reduced_resolution); |
1040 |
|
|
1041 |
|
stop_transfer_timer(); |
1042 |
|
|
1043 |
|
if(dec->out_frm && cp_mb > 0) { |
1044 |
|
output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); |
1045 |
|
cp_mb = 0; |
1046 |
|
} |
1047 |
|
st_mb = x+1; |
1048 |
} |
} |
1049 |
else /* not coded P_VOP macroblock */ |
else /* not coded P_VOP macroblock */ |
1050 |
{ |
{ |
1594 |
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; |
1595 |
|
|
1596 |
// skip if the co-located P_VOP macroblock is not coded |
// skip if the co-located P_VOP macroblock is not coded |
1597 |
// note: gmc+not_coded isn't skipped |
// if not codec in co-located S_VOP macroblock is _not_ automatically skipped |
1598 |
|
|
1599 |
if (last_mb->mode == MODE_NOT_CODED) { |
if (last_mb->mode == MODE_NOT_CODED) { |
1600 |
//DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
//DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
1768 |
uint32_t fcode_forward; |
uint32_t fcode_forward; |
1769 |
uint32_t fcode_backward; |
uint32_t fcode_backward; |
1770 |
uint32_t intra_dc_threshold; |
uint32_t intra_dc_threshold; |
1771 |
VECTOR gmc_mv[5]; |
WARPPOINTS gmc_warp; |
1772 |
uint32_t vop_type; |
uint32_t vop_type; |
1773 |
int success = 0; |
int success = 0; |
1774 |
int output = 0; |
int output = 0; |
1823 |
repeat: |
repeat: |
1824 |
|
|
1825 |
vop_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, |
vop_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, |
1826 |
&quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv); |
&quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp); |
1827 |
|
|
1828 |
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", |
1829 |
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); |
1830 |
|
|
1831 |
if (vop_type == - 1) |
if (vop_type == - 1) |
1832 |
{ |
{ |
1833 |
if (success) goto done; |
if (success) goto done; |
1834 |
|
emms(); |
1835 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
1836 |
} |
} |
1837 |
|
|
1852 |
stats->data.vol.par_width = dec->par_width; |
stats->data.vol.par_width = dec->par_width; |
1853 |
stats->data.vol.par_height = dec->par_height; |
stats->data.vol.par_height = dec->par_height; |
1854 |
frame->length = BitstreamPos(&bs) / 8; |
frame->length = BitstreamPos(&bs) / 8; |
1855 |
|
emms(); |
1856 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1857 |
} |
} |
1858 |
goto repeat; |
goto repeat; |
1884 |
break; |
break; |
1885 |
case S_VOP : |
case S_VOP : |
1886 |
decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, |
decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, |
1887 |
fcode_forward, intra_dc_threshold, gmc_mv); |
fcode_forward, intra_dc_threshold, &gmc_warp); |
1888 |
break; |
break; |
1889 |
case N_VOP : |
case N_VOP : |
1890 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |