80 |
#include "utils/mem_transfer.h" |
#include "utils/mem_transfer.h" |
81 |
#include "image/interpolate8x8.h" |
#include "image/interpolate8x8.h" |
82 |
#include "image/reduced.h" |
#include "image/reduced.h" |
83 |
|
#include "image/font.h" |
84 |
|
|
85 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
86 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
217 |
|
|
218 |
// add by chenm001 <chenm001@163.com> |
// add by chenm001 <chenm001@163.com> |
219 |
// for support B-frame to save reference frame's time |
// for support B-frame to save reference frame's time |
220 |
dec->frames = -1; |
dec->frames = 0; |
221 |
dec->time = dec->time_base = dec->last_time_base = 0; |
dec->time = dec->time_base = dec->last_time_base = 0; |
222 |
dec->low_delay = 0; |
dec->low_delay = 0; |
223 |
dec->packed_mode = 0; |
dec->packed_mode = 0; |
1606 |
*mb2 = temp; |
*mb2 = temp; |
1607 |
} |
} |
1608 |
|
|
1609 |
|
|
1610 |
int |
int |
1611 |
decoder_decode(DECODER * dec, |
decoder_decode(DECODER * dec, |
1612 |
XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats) |
XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats) |
1622 |
VECTOR gmc_mv[5]; |
VECTOR gmc_mv[5]; |
1623 |
uint32_t vop_type; |
uint32_t vop_type; |
1624 |
int success = 0; |
int success = 0; |
1625 |
|
int output = 0; |
1626 |
|
int seen_something = 0; |
1627 |
|
|
1628 |
start_global_timer(); |
start_global_timer(); |
1629 |
|
|
1630 |
|
dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY); |
1631 |
|
|
1632 |
dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL; |
dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL; |
1633 |
|
|
1634 |
BitstreamInit(&bs, frame->bitstream, frame->length); |
BitstreamInit(&bs, frame->bitstream, frame->length); |
1648 |
start: |
start: |
1649 |
// add by chenm001 <chenm001@163.com> |
// add by chenm001 <chenm001@163.com> |
1650 |
// for support B-frame to reference last 2 frame |
// for support B-frame to reference last 2 frame |
|
dec->frames++; |
|
1651 |
|
|
1652 |
xxx: |
xxx: |
1653 |
vop_type = |
vop_type = |
1654 |
BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, |
BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, |
1655 |
&quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv); |
&quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv); |
1656 |
|
|
1657 |
//DPRINTF(DPRINTF_HEADER, "vop_type=%i", vop_type); |
DPRINTF(DPRINTF_HEADER, "vop_type=%i, packed=%i, time=%i, time_pp=%i, time_bp=%i", |
1658 |
|
vop_type, dec->packed_mode, dec->time, dec->time_pp, dec->time_bp); |
1659 |
|
|
1660 |
if (vop_type == -1 && success) |
if (vop_type == - 1) |
1661 |
goto done; |
{ |
1662 |
|
if (success) goto done; |
1663 |
|
return XVID_ERR_FAIL; |
1664 |
|
} |
1665 |
|
|
1666 |
if (vop_type == -2 || vop_type == -3) |
if (vop_type == -2 || vop_type == -3) |
1667 |
{ |
{ |
1687 |
|
|
1688 |
dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0; // init pred vector to 0 |
dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0; // init pred vector to 0 |
1689 |
|
|
|
switch (vop_type) { |
|
|
case P_VOP: |
|
|
decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, |
|
|
fcode_forward, intra_dc_threshold, NULL); |
|
|
#ifdef BFRAMES_DEC |
|
|
DEBUG1("P_VOP Time=", dec->time); |
|
|
#endif |
|
|
break; |
|
1690 |
|
|
1691 |
|
/* packed_mode: special-N_VOP treament */ |
1692 |
|
if (dec->packed_mode && vop_type == N_VOP) |
1693 |
|
{ |
1694 |
|
if (dec->low_delay_default) |
1695 |
|
{ |
1696 |
|
image_output(&dec->refn[0], dec->width, dec->height, |
1697 |
|
dec->edged_width, frame->image, frame->stride, |
1698 |
|
frame->colorspace, dec->interlacing); |
1699 |
|
output = 1; |
1700 |
|
} |
1701 |
|
/* ignore otherwise */ |
1702 |
|
} |
1703 |
|
else if (vop_type != B_VOP) |
1704 |
|
{ |
1705 |
|
switch(vop_type) |
1706 |
|
{ |
1707 |
case I_VOP: |
case I_VOP: |
1708 |
decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold); |
decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold); |
|
#ifdef BFRAMES_DEC |
|
|
DEBUG1("I_VOP Time=", dec->time); |
|
|
#endif |
|
1709 |
break; |
break; |
1710 |
|
case P_VOP : |
1711 |
case B_VOP: |
decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, |
1712 |
#ifdef BFRAMES_DEC |
fcode_forward, intra_dc_threshold, NULL); |
|
if (dec->time_pp > dec->time_bp) { |
|
|
DEBUG1("B_VOP Time=", dec->time); |
|
|
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
|
|
} else { |
|
|
DEBUG("broken B-frame!"); |
|
|
} |
|
|
#else |
|
|
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
|
|
#endif |
|
1713 |
break; |
break; |
|
|
|
1714 |
case S_VOP : |
case S_VOP : |
1715 |
decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, |
decoder_pframe(dec, &bs, rounding, reduced_resolution, quant, |
1716 |
fcode_forward, intra_dc_threshold, gmc_mv); |
fcode_forward, intra_dc_threshold, gmc_mv); |
1717 |
break; |
break; |
1718 |
|
case N_VOP : |
|
case N_VOP: // vop not coded |
|
|
// when low_delay==0, N_VOP's should interpolate between the past and future frames |
|
1719 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
|
#ifdef BFRAMES_DEC |
|
|
DEBUG1("N_VOP Time=", dec->time); |
|
|
#endif |
|
1720 |
break; |
break; |
|
|
|
|
default: |
|
|
if (stats) |
|
|
stats->notify = 0; |
|
|
|
|
|
emms(); |
|
|
return XVID_ERR_FAIL; |
|
1721 |
} |
} |
1722 |
|
|
|
|
|
1723 |
if (reduced_resolution) |
if (reduced_resolution) |
1724 |
{ |
{ |
1725 |
image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs, |
image_deblock_rrv(&dec->refn[0], dec->edged_width, dec->mbs, |
1726 |
(dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width); |
(dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width); |
1727 |
} |
} |
1728 |
|
|
1729 |
BitstreamByteAlign(&bs); |
/* note: for packed_mode, output is performed when the special-N_VOP is decoded */ |
1730 |
|
if (!(dec->low_delay_default && dec->packed_mode)) |
1731 |
#ifdef BFRAMES_DEC |
{ |
1732 |
// test if no B_VOP |
if (dec->low_delay) |
1733 |
if (dec->low_delay || dec->frames == 0 || ((dec->packed_mode) && !(frame->length > BitstreamPos(&bs) / 8))) { |
{ |
|
#endif |
|
|
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
|
|
frame->image, frame->stride, frame->colorspace, dec->interlacing); |
|
|
|
|
|
#ifdef BFRAMES_DEC |
|
|
} else { |
|
|
if (dec->frames >= 1 && !(dec->packed_mode)) { |
|
|
start_timer(); |
|
|
if ((vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP)) { |
|
|
image_output(&dec->refn[0], dec->width, dec->height, |
|
|
dec->edged_width, frame->image, frame->stride, |
|
|
frame->colorspace, dec->interlacing); |
|
|
} else if (vop_type == B_VOP) { |
|
1734 |
image_output(&dec->cur, dec->width, dec->height, |
image_output(&dec->cur, dec->width, dec->height, |
1735 |
dec->edged_width, frame->image, frame->stride, |
dec->edged_width, frame->image, frame->stride, |
1736 |
frame->colorspace, dec->interlacing); |
frame->colorspace, dec->interlacing); |
1737 |
|
output = 1; |
1738 |
} |
} |
1739 |
stop_conv_timer(); |
else if (dec->frames > 0) /* is the reference frame valid? */ |
1740 |
|
{ |
1741 |
|
image_output(&dec->refn[0], dec->width, dec->height, |
1742 |
|
dec->edged_width, frame->image, frame->stride, |
1743 |
|
frame->colorspace, dec->interlacing); |
1744 |
|
output = 1; |
1745 |
} |
} |
1746 |
} |
} |
|
#endif |
|
1747 |
|
|
|
if (vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP) { |
|
1748 |
image_swap(&dec->refn[0], &dec->refn[1]); |
image_swap(&dec->refn[0], &dec->refn[1]); |
1749 |
image_swap(&dec->cur, &dec->refn[0]); |
image_swap(&dec->cur, &dec->refn[0]); |
|
|
|
|
// swap MACROBLOCK |
|
|
// the Divx will not set the low_delay flage some times |
|
|
// so follow code will wrong to not swap at that time |
|
|
// this will broken bitstream! so I'm change it, |
|
|
// But that is not the best way! can anyone tell me how |
|
|
// to do another way? |
|
|
// 18-07-2002 MinChen<chenm001@163.com> |
|
|
//if (!dec->low_delay && vop_type == P_VOP) |
|
|
if (vop_type == P_VOP) |
|
1750 |
mb_swap(&dec->mbs, &dec->last_mbs); |
mb_swap(&dec->mbs, &dec->last_mbs); |
1751 |
|
|
1752 |
|
dec->frames++; |
1753 |
|
seen_something = 1; |
1754 |
|
|
1755 |
|
}else{ /* B_VOP */ |
1756 |
|
|
1757 |
|
if (dec->low_delay) |
1758 |
|
{ |
1759 |
|
DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream"); |
1760 |
|
dec->low_delay = 1; |
1761 |
|
} |
1762 |
|
|
1763 |
|
if (dec->time_pp <= dec->time_bp) { |
1764 |
|
/* this occurs when dx50_bvop_compatibility==0 sequences are |
1765 |
|
decoded in vfw. */ |
1766 |
|
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1767 |
|
"broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp); |
1768 |
|
}else{ |
1769 |
|
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1770 |
|
} |
1771 |
|
|
1772 |
|
image_output(&dec->cur, dec->width, dec->height, |
1773 |
|
dec->edged_width, frame->image, frame->stride, |
1774 |
|
frame->colorspace, dec->interlacing); |
1775 |
|
output = 1; |
1776 |
|
dec->frames++; |
1777 |
} |
} |
1778 |
|
|
1779 |
|
emms(); |
1780 |
|
|
1781 |
|
BitstreamByteAlign(&bs); |
1782 |
|
|
1783 |
if (success == 0 && dec->packed_mode) |
/* low_delay_default mode: repeat in packed_mode */ |
1784 |
|
if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) |
1785 |
{ |
{ |
1786 |
success = 1; |
success = 1; |
|
// if (frame->length > BitstreamPos(&bs) / 8) // multiple vops packed together |
|
1787 |
goto start; |
goto start; |
1788 |
} |
} |
1789 |
|
|
1790 |
done : |
done : |
1791 |
|
|
1792 |
|
/* low_delay_default mode: if we've gotten here without outputting anything, |
1793 |
|
then output the recently decoded frame, or print an error message */ |
1794 |
|
if (dec->low_delay_default && output == 0) |
1795 |
|
{ |
1796 |
|
if (dec->packed_mode && seen_something) |
1797 |
|
{ |
1798 |
|
/* output the recently decoded frame */ |
1799 |
|
image_output(&dec->refn[0], dec->width, dec->height, |
1800 |
|
dec->edged_width, frame->image, frame->stride, |
1801 |
|
frame->colorspace, dec->interlacing); |
1802 |
|
} |
1803 |
|
else |
1804 |
|
{ |
1805 |
|
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1806 |
|
"warning: nothing to output"); |
1807 |
|
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, |
1808 |
|
"bframe decoder lag"); |
1809 |
|
|
1810 |
|
image_output(&dec->cur, dec->width, dec->height, |
1811 |
|
dec->edged_width, frame->image, frame->stride, |
1812 |
|
frame->colorspace, dec->interlacing); |
1813 |
|
} |
1814 |
|
output = 1; |
1815 |
|
} |
1816 |
|
|
1817 |
frame->length = BitstreamPos(&bs) / 8; |
frame->length = BitstreamPos(&bs) / 8; |
1818 |
|
|
1819 |
if (stats) |
if (stats) |
1820 |
{ |
{ |
1821 |
stats->notify = XVID_DEC_VOP; |
stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING; |
1822 |
stats->data.vop.time_base = (int)dec->time_base; |
stats->data.vop.time_base = (int)dec->time_base; |
1823 |
stats->data.vop.time_increment = 0; //XXX: todo |
stats->data.vop.time_increment = 0; //XXX: todo |
1824 |
} |
} |