1 |
/************************************************************************** |
/************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* decoder main |
* - Decoder main module - |
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* This program is an implementation of a part of one or more MPEG-4 |
7 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
12 |
* editors and their companies, will have no liability for use of this |
* editors and their companies, will have no liability for use of this |
13 |
* software or modifications or derivatives thereof. |
* software or modifications or derivatives thereof. |
14 |
* |
* |
15 |
* This program is xvid_free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
16 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
17 |
* the xvid_free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
18 |
* (at your option) any later version. |
* (at your option) any later version. |
19 |
* |
* |
20 |
* This program is distributed in the hope that it will be useful, |
* This program is distributed in the hope that it will be useful, |
23 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
24 |
* |
* |
25 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
26 |
* along with this program; if not, write to the xvid_free Software |
* along with this program; if not, write to the Free Software |
27 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
28 |
* |
* |
29 |
*************************************************************************/ |
*************************************************************************/ |
30 |
|
|
32 |
* |
* |
33 |
* History: |
* History: |
34 |
* |
* |
35 |
|
* 02.05.2002 add B-frame decode support(have some problem); |
36 |
|
* MinChen <chenm001@163.com> |
37 |
* 22.04.2002 add some B-frame decode support; chenm001 <chenm001@163.com> |
* 22.04.2002 add some B-frame decode support; chenm001 <chenm001@163.com> |
38 |
* 29.03.2002 interlacing fix - compensated block wasn't being used when |
* 29.03.2002 interlacing fix - compensated block wasn't being used when |
39 |
* reconstructing blocks, thus artifacts |
* reconstructing blocks, thus artifacts |
41 |
* interlaced decoding should be as fast as progressive now |
* interlaced decoding should be as fast as progressive now |
42 |
* 26.03.2002 interlacing support - moved transfers outside decode loop |
* 26.03.2002 interlacing support - moved transfers outside decode loop |
43 |
* 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block |
* 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block |
44 |
* 22.12.2001 block based interpolation |
* 22.12.2001 lock based interpolation |
45 |
* 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> |
46 |
* |
* |
47 |
|
* $Id$ |
48 |
|
* |
49 |
*************************************************************************/ |
*************************************************************************/ |
50 |
|
|
51 |
#include <stdlib.h> |
#include <stdlib.h> |
52 |
#include <string.h> // memset |
#include <string.h> |
53 |
|
|
54 |
#include "xvid.h" |
#include "xvid.h" |
55 |
#include "portab.h" |
#include "portab.h" |
115 |
xvid_free(dec); |
xvid_free(dec); |
116 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
117 |
} |
} |
118 |
|
if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) |
119 |
|
{ |
120 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
121 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
122 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
123 |
|
xvid_free(dec); |
124 |
|
return XVID_ERR_MEMORY; |
125 |
|
} |
126 |
|
|
127 |
dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE); |
dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE); |
128 |
if (dec->mbs == NULL) |
if (dec->mbs == NULL) |
129 |
{ |
{ |
130 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
131 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
132 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
133 |
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
134 |
|
xvid_free(dec); |
135 |
|
return XVID_ERR_MEMORY; |
136 |
|
} |
137 |
|
// add by chenm001 <chenm001@163.com> |
138 |
|
// for skip MB flag |
139 |
|
dec->last_mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE); |
140 |
|
if (dec->last_mbs == NULL) |
141 |
|
{ |
142 |
|
xvid_free(dec->mbs); |
143 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
144 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
145 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
146 |
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
147 |
xvid_free(dec); |
xvid_free(dec); |
148 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
149 |
} |
} |
150 |
|
|
151 |
init_timer(); |
init_timer(); |
152 |
|
|
153 |
|
// add by chenm001 <chenm001@163.com> |
154 |
|
// for support B-frame to save reference frame's time |
155 |
|
dec->frames = -1; |
156 |
|
dec->time = dec->time_base = dec->last_time_base = 0; |
157 |
|
|
158 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
159 |
} |
} |
160 |
|
|
161 |
|
|
162 |
int decoder_destroy(DECODER * dec) |
int decoder_destroy(DECODER * dec) |
163 |
{ |
{ |
164 |
|
xvid_free(dec->last_mbs); |
165 |
xvid_free(dec->mbs); |
xvid_free(dec->mbs); |
166 |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
167 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
168 |
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
169 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
170 |
xvid_free(dec); |
xvid_free(dec); |
171 |
|
|
181 |
}; |
}; |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
// decode an intra macroblock |
// decode an intra macroblock |
187 |
|
|
188 |
void decoder_mbintra(DECODER * dec, |
void decoder_mbintra(DECODER * dec, |
473 |
int range = (64 * scale_fac); |
int range = (64 * scale_fac); |
474 |
|
|
475 |
VECTOR pmv[4]; |
VECTOR pmv[4]; |
476 |
uint32_t psad[4]; |
int32_t psad[4]; |
477 |
|
|
478 |
int mv_x, mv_y; |
int mv_x, mv_y; |
479 |
int pmv_x, pmv_y; |
int pmv_x, pmv_y; |
529 |
{ |
{ |
530 |
MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x]; |
MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x]; |
531 |
|
|
532 |
if (!BitstreamGetBit(bs)) // not_coded |
//if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded |
533 |
|
if (!(BitstreamGetBit(bs))) // not_coded |
534 |
{ |
{ |
535 |
uint32_t mcbpc; |
uint32_t mcbpc; |
536 |
uint32_t cbpc; |
uint32_t cbpc; |
627 |
} |
} |
628 |
else // not coded |
else // not coded |
629 |
{ |
{ |
630 |
|
//DEBUG2("P-frame MB at (X,Y)=",x,y); |
631 |
mb->mode = MODE_NOT_CODED; |
mb->mode = MODE_NOT_CODED; |
632 |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; |
633 |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; |
666 |
} |
} |
667 |
} |
} |
668 |
|
|
669 |
|
|
670 |
|
// add by MinChen <chenm001@163.com> |
671 |
|
// decode B-frame motion vector |
672 |
|
void get_b_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, VECTOR * mv, int fcode, const VECTOR pmv) |
673 |
|
{ |
674 |
|
int scale_fac = 1 << (fcode - 1); |
675 |
|
int high = (32 * scale_fac) - 1; |
676 |
|
int low = ((-32) * scale_fac); |
677 |
|
int range = (64 * scale_fac); |
678 |
|
|
679 |
|
int mv_x, mv_y; |
680 |
|
int pmv_x, pmv_y; |
681 |
|
|
682 |
|
pmv_x = pmv.x; |
683 |
|
pmv_y = pmv.y; |
684 |
|
|
685 |
|
mv_x = get_mv(bs, fcode); |
686 |
|
mv_y = get_mv(bs, fcode); |
687 |
|
|
688 |
|
mv_x += pmv_x; |
689 |
|
mv_y += pmv_y; |
690 |
|
|
691 |
|
if (mv_x < low) |
692 |
|
{ |
693 |
|
mv_x += range; |
694 |
|
} |
695 |
|
else if (mv_x > high) |
696 |
|
{ |
697 |
|
mv_x -= range; |
698 |
|
} |
699 |
|
|
700 |
|
if (mv_y < low) |
701 |
|
{ |
702 |
|
mv_y += range; |
703 |
|
} |
704 |
|
else if (mv_y > high) |
705 |
|
{ |
706 |
|
mv_y -= range; |
707 |
|
} |
708 |
|
|
709 |
|
mv->x = mv_x; |
710 |
|
mv->y = mv_y; |
711 |
|
} |
712 |
|
|
713 |
|
|
714 |
|
// add by MinChen <chenm001@163.com> |
715 |
|
// decode an B-frame forward & backward inter macroblock |
716 |
|
void decoder_bf_mbinter(DECODER * dec, |
717 |
|
const MACROBLOCK * pMB, |
718 |
|
const uint32_t x_pos, |
719 |
|
const uint32_t y_pos, |
720 |
|
const uint32_t cbp, |
721 |
|
Bitstream * bs, |
722 |
|
const uint32_t quant, |
723 |
|
const uint8_t ref) |
724 |
|
{ |
725 |
|
|
726 |
|
DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE); |
727 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
728 |
|
|
729 |
|
uint32_t stride = dec->edged_width; |
730 |
|
uint32_t stride2 = stride / 2; |
731 |
|
uint32_t next_block = stride * 8; |
732 |
|
uint32_t i; |
733 |
|
uint32_t iQuant = pMB->quant; |
734 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
735 |
|
int uv_dx, uv_dy; |
736 |
|
|
737 |
|
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
738 |
|
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
739 |
|
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
740 |
|
|
741 |
|
if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) |
742 |
|
{ |
743 |
|
uv_dx = pMB->mvs[0].x; |
744 |
|
uv_dy = pMB->mvs[0].y; |
745 |
|
|
746 |
|
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
747 |
|
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
748 |
|
} |
749 |
|
else |
750 |
|
{ |
751 |
|
int sum; |
752 |
|
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
753 |
|
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
754 |
|
|
755 |
|
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
756 |
|
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
757 |
|
} |
758 |
|
|
759 |
|
start_timer(); |
760 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos , pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); |
761 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos , pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); |
762 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); |
763 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0); |
764 |
|
interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
765 |
|
interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
766 |
|
stop_comp_timer(); |
767 |
|
|
768 |
|
for (i = 0; i < 6; i++) |
769 |
|
{ |
770 |
|
if (cbp & (1 << (5-i))) // coded |
771 |
|
{ |
772 |
|
memset(&block[i*64], 0, 64 * sizeof(int16_t)); // clear |
773 |
|
|
774 |
|
start_timer(); |
775 |
|
get_inter_block(bs, &block[i*64]); |
776 |
|
stop_coding_timer(); |
777 |
|
|
778 |
|
start_timer(); |
779 |
|
if (dec->quant_type == 0) |
780 |
|
{ |
781 |
|
dequant_inter(&data[i*64], &block[i*64], iQuant); |
782 |
|
} |
783 |
|
else |
784 |
|
{ |
785 |
|
dequant4_inter(&data[i*64], &block[i*64], iQuant); |
786 |
|
} |
787 |
|
stop_iquant_timer(); |
788 |
|
|
789 |
|
data[0]=10; |
790 |
|
start_timer(); |
791 |
|
idct(&data[i*64]); |
792 |
|
stop_idct_timer(); |
793 |
|
} |
794 |
|
} |
795 |
|
|
796 |
|
if (dec->interlacing && pMB->field_dct) |
797 |
|
{ |
798 |
|
next_block = stride; |
799 |
|
stride *= 2; |
800 |
|
} |
801 |
|
|
802 |
|
start_timer(); |
803 |
|
if (cbp & 32) |
804 |
|
transfer_16to8add(pY_Cur, &data[0*64], stride); |
805 |
|
if (cbp & 16) |
806 |
|
transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
807 |
|
if (cbp & 8) |
808 |
|
transfer_16to8add(pY_Cur + next_block, &data[2*64], stride); |
809 |
|
if (cbp & 4) |
810 |
|
transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride); |
811 |
|
if (cbp & 2) |
812 |
|
transfer_16to8add(pU_Cur, &data[4*64], stride2); |
813 |
|
if (cbp & 1) |
814 |
|
transfer_16to8add(pV_Cur, &data[5*64], stride2); |
815 |
|
stop_transfer_timer(); |
816 |
|
} |
817 |
|
|
818 |
|
|
819 |
|
// add by MinChen <chenm001@163.com> |
820 |
|
// decode an B-frame direct & inter macroblock |
821 |
|
void decoder_bf_interpolate_mbinter(DECODER * dec, |
822 |
|
IMAGE forward, IMAGE backward, |
823 |
|
const MACROBLOCK * pMB, |
824 |
|
const uint32_t x_pos, |
825 |
|
const uint32_t y_pos, |
826 |
|
const uint32_t cbp, |
827 |
|
Bitstream * bs ) |
828 |
|
{ |
829 |
|
|
830 |
|
DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE); |
831 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
832 |
|
|
833 |
|
uint32_t stride = dec->edged_width; |
834 |
|
uint32_t stride2 = stride / 2; |
835 |
|
uint32_t next_block = stride * 8; |
836 |
|
uint32_t iQuant = pMB->quant; |
837 |
|
int uv_dx, uv_dy; |
838 |
|
int b_uv_dx, b_uv_dy; |
839 |
|
uint32_t i; |
840 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
841 |
|
|
842 |
|
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
843 |
|
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
844 |
|
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
845 |
|
|
846 |
|
if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) |
847 |
|
{ |
848 |
|
uv_dx = pMB->mvs[0].x; |
849 |
|
uv_dy = pMB->mvs[0].y; |
850 |
|
|
851 |
|
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
852 |
|
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
853 |
|
|
854 |
|
b_uv_dx = pMB->b_mvs[0].x; |
855 |
|
b_uv_dy = pMB->b_mvs[0].y; |
856 |
|
|
857 |
|
b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
858 |
|
b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
859 |
|
} |
860 |
|
else |
861 |
|
{ |
862 |
|
int sum; |
863 |
|
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
864 |
|
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
865 |
|
|
866 |
|
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
867 |
|
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
868 |
|
|
869 |
|
sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
870 |
|
b_uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
871 |
|
|
872 |
|
sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
873 |
|
b_uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
874 |
|
} |
875 |
|
|
876 |
|
|
877 |
|
start_timer(); |
878 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos, 16*y_pos , pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); |
879 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos , pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); |
880 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos, 16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); |
881 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0); |
882 |
|
interpolate8x8_switch(dec->cur.u, forward.u, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
883 |
|
interpolate8x8_switch(dec->cur.v, forward.v, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
884 |
|
|
885 |
|
|
886 |
|
interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos, 16*y_pos , pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0); |
887 |
|
interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos + 8, 16*y_pos , pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0); |
888 |
|
interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos, 16*y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0); |
889 |
|
interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos + 8, 16*y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0); |
890 |
|
interpolate8x8_switch(dec->refn[2].u, backward.u, 8*x_pos, 8*y_pos, b_uv_dx, b_uv_dy, stride2, 0); |
891 |
|
interpolate8x8_switch(dec->refn[2].v, backward.v, 8*x_pos, 8*y_pos, b_uv_dx, b_uv_dy, stride2, 0); |
892 |
|
|
893 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos, 16*y_pos , stride); |
894 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos , stride); |
895 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos, 16*y_pos + 8, stride); |
896 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos + 8, stride); |
897 |
|
interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8*x_pos, 8*y_pos, stride2); |
898 |
|
interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8*x_pos, 8*y_pos, stride2); |
899 |
|
|
900 |
|
stop_comp_timer(); |
901 |
|
|
902 |
|
for (i = 0; i < 6; i++) |
903 |
|
{ |
904 |
|
if (cbp & (1 << (5-i))) // coded |
905 |
|
{ |
906 |
|
memset(&block[i*64], 0, 64 * sizeof(int16_t)); // clear |
907 |
|
|
908 |
|
start_timer(); |
909 |
|
get_inter_block(bs, &block[i*64]); |
910 |
|
stop_coding_timer(); |
911 |
|
|
912 |
|
start_timer(); |
913 |
|
if (dec->quant_type == 0) |
914 |
|
{ |
915 |
|
dequant_inter(&data[i*64], &block[i*64], iQuant); |
916 |
|
} |
917 |
|
else |
918 |
|
{ |
919 |
|
dequant4_inter(&data[i*64], &block[i*64], iQuant); |
920 |
|
} |
921 |
|
stop_iquant_timer(); |
922 |
|
|
923 |
|
start_timer(); |
924 |
|
idct(&data[i*64]); |
925 |
|
stop_idct_timer(); |
926 |
|
} |
927 |
|
} |
928 |
|
|
929 |
|
if (dec->interlacing && pMB->field_dct) |
930 |
|
{ |
931 |
|
next_block = stride; |
932 |
|
stride *= 2; |
933 |
|
} |
934 |
|
|
935 |
|
start_timer(); |
936 |
|
if (cbp & 32) |
937 |
|
transfer_16to8add(pY_Cur, &data[0*64], stride); |
938 |
|
if (cbp & 16) |
939 |
|
transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
940 |
|
if (cbp & 8) |
941 |
|
transfer_16to8add(pY_Cur + next_block, &data[2*64], stride); |
942 |
|
if (cbp & 4) |
943 |
|
transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride); |
944 |
|
if (cbp & 2) |
945 |
|
transfer_16to8add(pU_Cur, &data[4*64], stride2); |
946 |
|
if (cbp & 1) |
947 |
|
transfer_16to8add(pV_Cur, &data[5*64], stride2); |
948 |
|
stop_transfer_timer(); |
949 |
|
} |
950 |
|
|
951 |
|
|
952 |
|
// add by MinChen <chenm001@163.com> |
953 |
|
// for decode B-frame dbquant |
954 |
|
int32_t __inline get_dbquant(Bitstream * bs) |
955 |
|
{ |
956 |
|
if (!BitstreamGetBit(bs)) // '0' |
957 |
|
return(0); |
958 |
|
else if (!BitstreamGetBit(bs)) // '10' |
959 |
|
return(-2); |
960 |
|
else |
961 |
|
return(2); // '11' |
962 |
|
} |
963 |
|
|
964 |
|
// add by MinChen <chenm001@163.com> |
965 |
|
// for decode B-frame mb_type |
966 |
|
// bit ret_value |
967 |
|
// 1 0 |
968 |
|
// 01 1 |
969 |
|
// 001 2 |
970 |
|
// 0001 3 |
971 |
|
int32_t __inline get_mbtype(Bitstream * bs) |
972 |
|
{ |
973 |
|
int32_t mb_type; |
974 |
|
for(mb_type=0;mb_type<=3;mb_type++){ |
975 |
|
if (BitstreamGetBit(bs)) |
976 |
|
break; |
977 |
|
} |
978 |
|
|
979 |
|
if (mb_type<=3) |
980 |
|
return(mb_type); |
981 |
|
else |
982 |
|
return(-1); |
983 |
|
} |
984 |
|
|
985 |
|
void decoder_bframe(DECODER * dec, Bitstream * bs, int quant, int fcode_forward, int fcode_backward) |
986 |
|
{ |
987 |
|
|
988 |
|
uint32_t x, y; |
989 |
|
VECTOR mv, zeromv; |
990 |
|
|
991 |
|
start_timer(); |
992 |
|
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
993 |
|
//image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
994 |
|
stop_edges_timer(); |
995 |
|
|
996 |
|
|
997 |
|
for (y = 0; y < dec->mb_height; y++) |
998 |
|
{ |
999 |
|
// Initialize Pred Motion Vector |
1000 |
|
dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0; |
1001 |
|
for (x = 0; x < dec->mb_width; x++) |
1002 |
|
{ |
1003 |
|
MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x]; |
1004 |
|
MACROBLOCK * last_mb = &dec->last_mbs[y*dec->mb_width + x]; |
1005 |
|
|
1006 |
|
mb->mvs[0].x=mb->mvs[0].y=zeromv.x = zeromv.y = mv.x = mv.y = 0; |
1007 |
|
|
1008 |
|
// the last P_VOP is skip macroblock ? |
1009 |
|
if (last_mb->mode == MODE_NOT_CODED){ |
1010 |
|
//DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
1011 |
|
mb->mb_type = MODE_FORWARD; |
1012 |
|
mb->cbp = 0; |
1013 |
|
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1014 |
|
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1015 |
|
mb->quant = 8; |
1016 |
|
|
1017 |
|
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1018 |
|
continue; |
1019 |
|
} |
1020 |
|
|
1021 |
|
//t=BitstreamShowBits(bs,32); |
1022 |
|
|
1023 |
|
if (!BitstreamGetBit(bs)){ // modb=='0' |
1024 |
|
const uint8_t modb2=BitstreamGetBit(bs); |
1025 |
|
|
1026 |
|
mb->mb_type = get_mbtype(bs); |
1027 |
|
|
1028 |
|
if (!modb2){ // modb=='00' |
1029 |
|
mb->cbp = BitstreamGetBits(bs,6); |
1030 |
|
} else { |
1031 |
|
mb->cbp = 0; |
1032 |
|
} |
1033 |
|
if (mb->mb_type && mb->cbp){ |
1034 |
|
quant += get_dbquant(bs); |
1035 |
|
|
1036 |
|
if (quant > 31) |
1037 |
|
{ |
1038 |
|
quant = 31; |
1039 |
|
} |
1040 |
|
else if (mb->quant < 1) |
1041 |
|
{ |
1042 |
|
quant = 1; |
1043 |
|
} |
1044 |
|
} else { |
1045 |
|
quant = 8; |
1046 |
|
} |
1047 |
|
mb->quant = quant; |
1048 |
|
} else { |
1049 |
|
mb->mb_type = MODE_DIRECT_NONE_MV; |
1050 |
|
mb->cbp = 0; |
1051 |
|
} |
1052 |
|
|
1053 |
|
mb->mode = MODE_INTER; |
1054 |
|
//DEBUG1("Switch bm_type=",mb->mb_type); |
1055 |
|
|
1056 |
|
switch(mb->mb_type) |
1057 |
|
{ |
1058 |
|
case MODE_DIRECT: |
1059 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv); |
1060 |
|
|
1061 |
|
case MODE_DIRECT_NONE_MV: |
1062 |
|
{ // Because this file is a C file not C++ so I use '{' to define var |
1063 |
|
const int64_t TRB=dec->time_pp-dec->time_bp, |
1064 |
|
TRD=dec->time_pp; |
1065 |
|
int i; |
1066 |
|
for(i=0;i<4;i++){ |
1067 |
|
mb->mvs[i].x = (int32_t)((TRB * last_mb->mvs[i].x)/TRD+mb->mvs[0].x); |
1068 |
|
mb->b_mvs[i].x = (int32_t)((mb->mvs[0].x==0)?((TRB-TRD)*last_mb->mvs[i].x)/TRD:mb->mvs[i].x-last_mb->mvs[i].x); |
1069 |
|
mb->mvs[i].y = (int32_t)((TRB * last_mb->mvs[i].y)/TRD+mb->mvs[0].y); |
1070 |
|
mb->b_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); |
1071 |
|
} |
1072 |
|
//DEBUG("B-frame Direct!\n"); |
1073 |
|
} |
1074 |
|
mb->mode = MODE_INTER4V; |
1075 |
|
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, mb->cbp, bs); |
1076 |
|
break; |
1077 |
|
|
1078 |
|
case MODE_INTERPOLATE: |
1079 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv); |
1080 |
|
dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1081 |
|
dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1082 |
|
|
1083 |
|
get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], fcode_backward, dec->p_bmv); |
1084 |
|
dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x = mb->b_mvs[3].x = mb->b_mvs[0].x; |
1085 |
|
dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y = mb->b_mvs[3].y = mb->b_mvs[0].y; |
1086 |
|
|
1087 |
|
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, mb->cbp, bs); |
1088 |
|
//DEBUG("B-frame Bidir!\n"); |
1089 |
|
break; |
1090 |
|
|
1091 |
|
case MODE_BACKWARD: |
1092 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, dec->p_bmv); |
1093 |
|
dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1094 |
|
dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1095 |
|
|
1096 |
|
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
1097 |
|
//DEBUG("B-frame Backward!\n"); |
1098 |
|
break; |
1099 |
|
|
1100 |
|
case MODE_FORWARD: |
1101 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv); |
1102 |
|
dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1103 |
|
dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1104 |
|
|
1105 |
|
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1106 |
|
//DEBUG("B-frame Forward!\n"); |
1107 |
|
break; |
1108 |
|
|
1109 |
|
default: |
1110 |
|
DEBUG1("Not support B-frame mb_type =",mb->mb_type); |
1111 |
|
} |
1112 |
|
|
1113 |
|
} // end of FOR |
1114 |
|
} |
1115 |
|
} |
1116 |
|
|
1117 |
|
// swap two MACROBLOCK array |
1118 |
|
void mb_swap(MACROBLOCK **mb1, MACROBLOCK **mb2) |
1119 |
|
{ |
1120 |
|
MACROBLOCK *temp=*mb1; |
1121 |
|
*mb1=*mb2; |
1122 |
|
*mb2=temp; |
1123 |
|
} |
1124 |
|
|
1125 |
int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame) |
int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame) |
1126 |
{ |
{ |
1127 |
|
|
1128 |
Bitstream bs; |
Bitstream bs; |
1129 |
uint32_t rounding; |
uint32_t rounding; |
1130 |
uint32_t quant; |
uint32_t quant; |
1131 |
uint32_t fcode; |
uint32_t fcode_forward; |
1132 |
|
uint32_t fcode_backward; |
1133 |
uint32_t intra_dc_threshold; |
uint32_t intra_dc_threshold; |
1134 |
uint32_t vop_type; |
uint32_t vop_type; |
1135 |
|
|
1139 |
|
|
1140 |
// add by chenm001 <chenm001@163.com> |
// add by chenm001 <chenm001@163.com> |
1141 |
// for support B-frame to reference last 2 frame |
// for support B-frame to reference last 2 frame |
1142 |
vop_type=BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold); |
dec->frames ++; |
1143 |
|
vop_type=BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold); |
|
if (vop_type==I_VOP || vop_type==P_VOP){ |
|
|
image_swap(&dec->refn[0], &dec->refn[1]); |
|
|
image_swap(&dec->cur, &dec->refn[0]); |
|
|
} |
|
1144 |
|
|
1145 |
|
dec->p_bmv.x=dec->p_bmv.y=dec->p_fmv.y=dec->p_fmv.y=0; // init pred vector to 0 |
1146 |
switch (vop_type) |
switch (vop_type) |
1147 |
{ |
{ |
1148 |
case P_VOP : |
case P_VOP : |
1149 |
decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold); |
decoder_pframe(dec, &bs, rounding, quant, fcode_forward, intra_dc_threshold); |
1150 |
|
DEBUG1("P_VOP Time=",dec->time); |
1151 |
break; |
break; |
1152 |
|
|
1153 |
case I_VOP : |
case I_VOP : |
|
//DEBUG1("",intra_dc_threshold); |
|
1154 |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
1155 |
|
DEBUG1("I_VOP Time=",dec->time); |
1156 |
break; |
break; |
1157 |
|
|
1158 |
case B_VOP : // ignore |
case B_VOP : |
1159 |
|
if (dec->time_pp > dec->time_bp){ |
1160 |
|
DEBUG1("B_VOP Time=",dec->time); |
1161 |
|
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1162 |
|
} else { |
1163 |
|
DEBUG("broken B-frame!"); |
1164 |
|
} |
1165 |
break; |
break; |
1166 |
|
|
1167 |
case N_VOP : // vop not coded |
case N_VOP : // vop not coded |
1173 |
|
|
1174 |
frame->length = BitstreamPos(&bs) / 8; |
frame->length = BitstreamPos(&bs) / 8; |
1175 |
|
|
1176 |
|
if (dec->frames >= 1){ |
1177 |
start_timer(); |
start_timer(); |
1178 |
|
if ((vop_type == I_VOP || vop_type == P_VOP)) |
1179 |
|
{ |
1180 |
|
image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width, |
1181 |
|
frame->image, frame->stride, frame->colorspace); |
1182 |
|
} else if (vop_type == B_VOP) { |
1183 |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
1184 |
frame->image, frame->stride, frame->colorspace); |
frame->image, frame->stride, frame->colorspace); |
1185 |
|
} |
1186 |
stop_conv_timer(); |
stop_conv_timer(); |
1187 |
|
} |
1188 |
|
if (vop_type==I_VOP || vop_type==P_VOP){ |
1189 |
|
image_swap(&dec->refn[0], &dec->refn[1]); |
1190 |
|
image_swap(&dec->cur, &dec->refn[0]); |
1191 |
|
// swap MACROBLOCK |
1192 |
|
if (vop_type==P_VOP) |
1193 |
|
mb_swap(&dec->mbs, &dec->last_mbs); |
1194 |
|
} |
1195 |
|
|
1196 |
emms(); |
emms(); |
1197 |
|
|
1198 |
stop_global_timer(); |
stop_global_timer(); |
1199 |
|
|
1200 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
|
|
|
1201 |
} |
} |