19 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
22 |
* $Id: decoder.c,v 1.49 2003-02-19 21:59:30 edgomez Exp $ |
* $Id: decoder.c,v 1.49.2.1 2003-02-22 08:49:44 suxen_drol Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
158 |
|
|
159 |
memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
160 |
|
|
161 |
return XVID_ERR_OK; |
return 0; |
162 |
} |
} |
163 |
|
|
164 |
|
|
165 |
int |
int |
166 |
decoder_create(XVID_DEC_PARAM * param) |
decoder_create(xvid_dec_create_t * create) |
167 |
{ |
{ |
168 |
DECODER *dec; |
DECODER *dec; |
169 |
|
|
170 |
|
if (XVID_MAJOR(create->version) != 1) /* v1.x.x */ |
171 |
|
return XVID_ERR_VERSION; |
172 |
|
|
173 |
dec = xvid_malloc(sizeof(DECODER), CACHE_LINE); |
dec = xvid_malloc(sizeof(DECODER), CACHE_LINE); |
174 |
if (dec == NULL) { |
if (dec == NULL) { |
175 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
176 |
} |
} |
177 |
memset(dec, 0, sizeof(DECODER)); |
memset(dec, 0, sizeof(DECODER)); |
178 |
|
|
179 |
param->handle = dec; |
create->handle = dec; |
180 |
|
|
181 |
dec->width = param->width; |
dec->width = create->width; |
182 |
dec->height = param->height; |
dec->height = create->height; |
183 |
|
|
184 |
image_null(&dec->cur); |
image_null(&dec->cur); |
185 |
image_null(&dec->refn[0]); |
image_null(&dec->refn[0]); |
207 |
if (dec->fixed_dimensions) |
if (dec->fixed_dimensions) |
208 |
return decoder_resize(dec); |
return decoder_resize(dec); |
209 |
else |
else |
210 |
return XVID_ERR_OK; |
return 0; |
211 |
} |
} |
212 |
|
|
213 |
|
|
228 |
xvid_free(dec); |
xvid_free(dec); |
229 |
|
|
230 |
write_timer(); |
write_timer(); |
231 |
return XVID_ERR_OK; |
return 0; |
232 |
} |
} |
233 |
|
|
234 |
|
|
1719 |
|
|
1720 |
/* perform post processing if necessary, and output the image */ |
/* perform post processing if necessary, and output the image */ |
1721 |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
1722 |
const XVID_DEC_FRAME * frame, int pp_disable) |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type) |
1723 |
{ |
{ |
1724 |
|
|
|
if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable) /* post process */ |
|
|
{ |
|
|
/* note: image is stored to tmp */ |
|
|
image_copy(&dec->tmp, img, dec->edged_width, dec->height); |
|
|
image_deblock_rrv(&dec->tmp, dec->edged_width, |
|
|
mbs, dec->mb_width, dec->mb_height, dec->mb_width, |
|
|
8, frame->general); |
|
|
img = &dec->tmp; |
|
|
} |
|
1725 |
|
|
1726 |
image_output(img, dec->width, dec->height, |
image_output(img, dec->width, dec->height, |
1727 |
dec->edged_width, frame->image, frame->stride, |
dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride, |
1728 |
frame->colorspace, dec->interlacing); |
frame->output.csp, dec->interlacing); |
1729 |
|
|
1730 |
|
if (stats) |
1731 |
|
{ |
1732 |
|
stats->type = coding2type(coding_type); |
1733 |
|
stats->data.vop.time_base = (int)dec->time_base; |
1734 |
|
stats->data.vop.time_increment = 0; //XXX: todo |
1735 |
|
} |
1736 |
} |
} |
1737 |
|
|
1738 |
|
|
1739 |
int |
int |
1740 |
decoder_decode(DECODER * dec, |
decoder_decode(DECODER * dec, |
1741 |
XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats) |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats) |
1742 |
{ |
{ |
1743 |
|
|
1744 |
Bitstream bs; |
Bitstream bs; |
1749 |
uint32_t fcode_backward; |
uint32_t fcode_backward; |
1750 |
uint32_t intra_dc_threshold; |
uint32_t intra_dc_threshold; |
1751 |
WARPPOINTS gmc_warp; |
WARPPOINTS gmc_warp; |
1752 |
int vop_type; |
int coding_type; |
1753 |
int success = 0; |
int success, output, seen_something; |
|
int output = 0; |
|
|
int seen_something = 0; |
|
1754 |
|
|
1755 |
start_global_timer(); |
if (XVID_MAJOR(frame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1)) /* v1.x.x */ |
1756 |
|
return XVID_ERR_VERSION; |
1757 |
|
|
1758 |
dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY); |
start_global_timer(); |
|
dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL; |
|
1759 |
|
|
1760 |
if ((frame->general & XVID_DEC_DISCONTINUITY)) |
dec->low_delay_default = (frame->general & XVID_LOWDELAY); |
1761 |
|
if ((frame->general & XVID_DISCONTINUITY)) |
1762 |
dec->frames = 0; |
dec->frames = 0; |
1763 |
|
dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL; |
1764 |
|
|
1765 |
if (frame->length < 0) /* decoder flush */ |
if (frame->length < 0) /* decoder flush */ |
1766 |
{ |
{ |
1768 |
we have a reference frame, then outout the reference frame */ |
we have a reference frame, then outout the reference frame */ |
1769 |
if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) |
if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) |
1770 |
{ |
{ |
1771 |
decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution); |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type); |
1772 |
output = 1; |
}else{ |
1773 |
} |
if (stats) stats->type = XVID_TYPE_NOTHING; |
|
|
|
|
frame->length = 0; |
|
|
if (stats) |
|
|
{ |
|
|
stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING; |
|
|
stats->data.vop.time_base = (int)dec->time_base; |
|
|
stats->data.vop.time_increment = 0; /* XXX: todo */ |
|
1774 |
} |
} |
1775 |
|
|
1776 |
emms(); |
emms(); |
|
|
|
1777 |
stop_global_timer(); |
stop_global_timer(); |
1778 |
return XVID_ERR_OK; |
return 0; |
1779 |
} |
} |
1780 |
|
|
1781 |
BitstreamInit(&bs, frame->bitstream, frame->length); |
BitstreamInit(&bs, frame->bitstream, frame->length); |
1783 |
/* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */ |
/* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */ |
1784 |
if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f) |
if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f) |
1785 |
{ |
{ |
|
if (stats) |
|
|
stats->notify = XVID_DEC_VOP; |
|
|
frame->length = 1; |
|
1786 |
image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width, |
image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width, |
1787 |
frame->image, frame->stride, frame->colorspace, dec->interlacing); |
(uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing); |
1788 |
|
if (stats) stats->type = XVID_TYPE_NOTHING; |
1789 |
emms(); |
emms(); |
1790 |
return XVID_ERR_OK; |
return 1; /* one byte consumed */ |
1791 |
} |
} |
1792 |
|
|
1793 |
|
success = 0; |
1794 |
|
output = 0; |
1795 |
|
seen_something = 0; |
1796 |
|
|
1797 |
repeat: |
repeat: |
1798 |
|
|
1799 |
vop_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, |
coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, |
1800 |
&quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp); |
&quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp); |
1801 |
|
|
1802 |
DPRINTF(DPRINTF_HEADER, "vop_type=%i, packed=%i, time=%lli, time_pp=%i, time_bp=%i", |
DPRINTF(DPRINTF_HEADER, "coding_type=%i, packed=%i, time=%lli, time_pp=%i, time_bp=%i", |
1803 |
vop_type, dec->packed_mode, dec->time, dec->time_pp, dec->time_bp); |
coding_type, dec->packed_mode, dec->time, dec->time_pp, dec->time_bp); |
1804 |
|
|
1805 |
if (vop_type == -1) |
if (coding_type == -1) /* nothing */ |
1806 |
{ |
{ |
1807 |
if (success) goto done; |
if (success) goto done; |
1808 |
|
if (stats) stats->type = XVID_TYPE_NOTHING; |
1809 |
emms(); |
emms(); |
1810 |
return XVID_ERR_FAIL; |
return BitstreamPos(&bs)/8; |
1811 |
} |
} |
1812 |
|
|
1813 |
if (vop_type == -2 || vop_type == -3) |
if (coding_type == -2 || coding_type == -3) /* vol and/or resize */ |
1814 |
{ |
{ |
1815 |
if (vop_type == -3) |
if (coding_type == -3) |
1816 |
decoder_resize(dec); |
decoder_resize(dec); |
1817 |
|
|
1818 |
if (stats) |
if (stats) |
1819 |
{ |
{ |
1820 |
stats->notify = XVID_DEC_VOL; |
stats->type = XVID_TYPE_VOL; |
1821 |
stats->data.vol.general = 0; |
stats->data.vol.general = 0; |
1822 |
if (dec->interlacing) |
/*XXX: if (dec->interlacing) |
1823 |
stats->data.vol.general |= XVID_INTERLACING; |
stats->data.vol.general |= ++INTERLACING; */ |
1824 |
stats->data.vol.width = dec->width; |
stats->data.vol.width = dec->width; |
1825 |
stats->data.vol.height = dec->height; |
stats->data.vol.height = dec->height; |
1826 |
stats->data.vol.aspect_ratio = dec->aspect_ratio; |
stats->data.vol.par = dec->aspect_ratio; |
1827 |
stats->data.vol.par_width = dec->par_width; |
stats->data.vol.par_width = dec->par_width; |
1828 |
stats->data.vol.par_height = dec->par_height; |
stats->data.vol.par_height = dec->par_height; |
|
frame->length = BitstreamPos(&bs) / 8; |
|
1829 |
emms(); |
emms(); |
1830 |
return XVID_ERR_OK; |
return BitstreamPos(&bs)/8; /* number of bytes consumed */ |
1831 |
} |
} |
1832 |
goto repeat; |
goto repeat; |
1833 |
} |
} |
1836 |
|
|
1837 |
|
|
1838 |
/* packed_mode: special-N_VOP treament */ |
/* packed_mode: special-N_VOP treament */ |
1839 |
if (dec->packed_mode && vop_type == N_VOP) |
if (dec->packed_mode && coding_type == N_VOP) |
1840 |
{ |
{ |
1841 |
if (dec->low_delay_default && dec->frames > 0) |
if (dec->low_delay_default && dec->frames > 0) |
1842 |
{ |
{ |
1843 |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution); |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type); |
1844 |
output = 1; |
output = 1; |
1845 |
} |
} |
1846 |
/* ignore otherwise */ |
/* ignore otherwise */ |
1847 |
} |
} |
1848 |
else if (vop_type != B_VOP) |
else if (coding_type != B_VOP) |
1849 |
{ |
{ |
1850 |
switch(vop_type) |
switch(coding_type) |
1851 |
{ |
{ |
1852 |
case I_VOP : |
case I_VOP : |
1853 |
decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold); |
decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold); |
1861 |
fcode_forward, intra_dc_threshold, &gmc_warp); |
fcode_forward, intra_dc_threshold, &gmc_warp); |
1862 |
break; |
break; |
1863 |
case N_VOP : |
case N_VOP : |
1864 |
|
// XXX: not_coded vops are not used for forward prediction |
1865 |
|
// we should not swap(last_mbs,mbs) |
1866 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1867 |
break; |
break; |
1868 |
} |
} |
1871 |
{ |
{ |
1872 |
image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs, |
image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs, |
1873 |
(dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width, |
(dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width, |
1874 |
16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); |
16, 0); |
1875 |
} |
} |
1876 |
|
|
1877 |
/* note: for packed_mode, output is performed when the special-N_VOP is decoded */ |
/* note: for packed_mode, output is performed when the special-N_VOP is decoded */ |
1879 |
{ |
{ |
1880 |
if (dec->low_delay) |
if (dec->low_delay) |
1881 |
{ |
{ |
1882 |
decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution); |
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type); |
1883 |
output = 1; |
output = 1; |
1884 |
} |
} |
1885 |
else if (dec->frames > 0) /* is the reference frame valid? */ |
else if (dec->frames > 0) /* is the reference frame valid? */ |
1886 |
{ |
{ |
1887 |
/* output the reference frame */ |
/* output the reference frame */ |
1888 |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution); |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type); |
1889 |
output = 1; |
output = 1; |
1890 |
} |
} |
1891 |
} |
} |
1894 |
image_swap(&dec->cur, &dec->refn[0]); |
image_swap(&dec->cur, &dec->refn[0]); |
1895 |
mb_swap(&dec->mbs, &dec->last_mbs); |
mb_swap(&dec->mbs, &dec->last_mbs); |
1896 |
dec->last_reduced_resolution = reduced_resolution; |
dec->last_reduced_resolution = reduced_resolution; |
1897 |
|
dec->last_coding_type = coding_type; |
1898 |
|
|
1899 |
dec->frames++; |
dec->frames++; |
1900 |
seen_something = 1; |
seen_something = 1; |
1921 |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1922 |
} |
} |
1923 |
|
|
1924 |
decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution); |
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type); |
1925 |
output = 1; |
output = 1; |
1926 |
dec->frames++; |
dec->frames++; |
1927 |
} |
} |
1944 |
if (dec->packed_mode && seen_something) |
if (dec->packed_mode && seen_something) |
1945 |
{ |
{ |
1946 |
/* output the recently decoded frame */ |
/* output the recently decoded frame */ |
1947 |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution); |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type); |
|
output = 1; |
|
1948 |
} |
} |
1949 |
else |
else |
1950 |
{ |
{ |
1954 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, |
1955 |
"bframe decoder lag"); |
"bframe decoder lag"); |
1956 |
|
|
1957 |
decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/); |
decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP); |
1958 |
} |
if (stats) stats->type = XVID_TYPE_NOTHING; |
|
} |
|
|
|
|
|
frame->length = BitstreamPos(&bs) / 8; |
|
1959 |
|
|
1960 |
if (stats) |
} |
|
{ |
|
|
stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING; |
|
|
stats->data.vop.time_base = (int)dec->time_base; |
|
|
stats->data.vop.time_increment = 0; /* XXX: todo */ |
|
1961 |
} |
} |
1962 |
|
|
1963 |
emms(); |
emms(); |
|
|
|
1964 |
stop_global_timer(); |
stop_global_timer(); |
1965 |
|
|
1966 |
return XVID_ERR_OK; |
return BitstreamPos(&bs) / 8; /* number of bytes consumed */ |
1967 |
} |
} |