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.2 2003-03-16 12:04:13 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 |
|
|
1705 |
#endif |
#endif |
1706 |
} |
} |
1707 |
|
|
|
/* swap two MACROBLOCK array */ |
|
|
void |
|
|
mb_swap(MACROBLOCK ** mb1, |
|
|
MACROBLOCK ** mb2) |
|
|
{ |
|
|
MACROBLOCK *temp = *mb1; |
|
|
|
|
|
*mb1 = *mb2; |
|
|
*mb2 = temp; |
|
|
} |
|
1708 |
|
|
1709 |
|
|
1710 |
/* perform post processing if necessary, and output the image */ |
/* perform post processing if necessary, and output the image */ |
1711 |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
1712 |
const XVID_DEC_FRAME * frame, int pp_disable) |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type) |
1713 |
{ |
{ |
1714 |
|
|
|
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; |
|
|
} |
|
1715 |
|
|
1716 |
image_output(img, dec->width, dec->height, |
image_output(img, dec->width, dec->height, |
1717 |
dec->edged_width, frame->image, frame->stride, |
dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride, |
1718 |
frame->colorspace, dec->interlacing); |
frame->output.csp, dec->interlacing); |
1719 |
|
|
1720 |
|
if (stats) |
1721 |
|
{ |
1722 |
|
stats->type = coding2type(coding_type); |
1723 |
|
stats->data.vop.time_base = (int)dec->time_base; |
1724 |
|
stats->data.vop.time_increment = 0; //XXX: todo |
1725 |
|
} |
1726 |
} |
} |
1727 |
|
|
1728 |
|
|
1729 |
int |
int |
1730 |
decoder_decode(DECODER * dec, |
decoder_decode(DECODER * dec, |
1731 |
XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats) |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats) |
1732 |
{ |
{ |
1733 |
|
|
1734 |
Bitstream bs; |
Bitstream bs; |
1739 |
uint32_t fcode_backward; |
uint32_t fcode_backward; |
1740 |
uint32_t intra_dc_threshold; |
uint32_t intra_dc_threshold; |
1741 |
WARPPOINTS gmc_warp; |
WARPPOINTS gmc_warp; |
1742 |
int vop_type; |
int coding_type; |
1743 |
int success = 0; |
int success, output, seen_something; |
|
int output = 0; |
|
|
int seen_something = 0; |
|
1744 |
|
|
1745 |
start_global_timer(); |
if (XVID_MAJOR(frame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1)) /* v1.x.x */ |
1746 |
|
return XVID_ERR_VERSION; |
1747 |
|
|
1748 |
dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY); |
start_global_timer(); |
|
dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL; |
|
1749 |
|
|
1750 |
if ((frame->general & XVID_DEC_DISCONTINUITY)) |
dec->low_delay_default = (frame->general & XVID_LOWDELAY); |
1751 |
|
if ((frame->general & XVID_DISCONTINUITY)) |
1752 |
dec->frames = 0; |
dec->frames = 0; |
1753 |
|
dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL; |
1754 |
|
|
1755 |
if (frame->length < 0) /* decoder flush */ |
if (frame->length < 0) /* decoder flush */ |
1756 |
{ |
{ |
1758 |
we have a reference frame, then outout the reference frame */ |
we have a reference frame, then outout the reference frame */ |
1759 |
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) |
1760 |
{ |
{ |
1761 |
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); |
1762 |
output = 1; |
}else{ |
1763 |
} |
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 */ |
|
1764 |
} |
} |
1765 |
|
|
1766 |
emms(); |
emms(); |
|
|
|
1767 |
stop_global_timer(); |
stop_global_timer(); |
1768 |
return XVID_ERR_OK; |
return 0; |
1769 |
} |
} |
1770 |
|
|
1771 |
BitstreamInit(&bs, frame->bitstream, frame->length); |
BitstreamInit(&bs, frame->bitstream, frame->length); |
1773 |
/* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */ |
/* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */ |
1774 |
if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f) |
if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f) |
1775 |
{ |
{ |
|
if (stats) |
|
|
stats->notify = XVID_DEC_VOP; |
|
|
frame->length = 1; |
|
1776 |
image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width, |
image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width, |
1777 |
frame->image, frame->stride, frame->colorspace, dec->interlacing); |
(uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing); |
1778 |
|
if (stats) stats->type = XVID_TYPE_NOTHING; |
1779 |
emms(); |
emms(); |
1780 |
return XVID_ERR_OK; |
return 1; /* one byte consumed */ |
1781 |
} |
} |
1782 |
|
|
1783 |
|
success = 0; |
1784 |
|
output = 0; |
1785 |
|
seen_something = 0; |
1786 |
|
|
1787 |
repeat: |
repeat: |
1788 |
|
|
1789 |
vop_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, |
coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution, |
1790 |
&quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp); |
&quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp); |
1791 |
|
|
1792 |
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", |
1793 |
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); |
1794 |
|
|
1795 |
if (vop_type == -1) |
if (coding_type == -1) /* nothing */ |
1796 |
{ |
{ |
1797 |
if (success) goto done; |
if (success) goto done; |
1798 |
|
if (stats) stats->type = XVID_TYPE_NOTHING; |
1799 |
emms(); |
emms(); |
1800 |
return XVID_ERR_FAIL; |
return BitstreamPos(&bs)/8; |
1801 |
} |
} |
1802 |
|
|
1803 |
if (vop_type == -2 || vop_type == -3) |
if (coding_type == -2 || coding_type == -3) /* vol and/or resize */ |
1804 |
{ |
{ |
1805 |
if (vop_type == -3) |
if (coding_type == -3) |
1806 |
decoder_resize(dec); |
decoder_resize(dec); |
1807 |
|
|
1808 |
if (stats) |
if (stats) |
1809 |
{ |
{ |
1810 |
stats->notify = XVID_DEC_VOL; |
stats->type = XVID_TYPE_VOL; |
1811 |
stats->data.vol.general = 0; |
stats->data.vol.general = 0; |
1812 |
if (dec->interlacing) |
/*XXX: if (dec->interlacing) |
1813 |
stats->data.vol.general |= XVID_INTERLACING; |
stats->data.vol.general |= ++INTERLACING; */ |
1814 |
stats->data.vol.width = dec->width; |
stats->data.vol.width = dec->width; |
1815 |
stats->data.vol.height = dec->height; |
stats->data.vol.height = dec->height; |
1816 |
stats->data.vol.aspect_ratio = dec->aspect_ratio; |
stats->data.vol.par = dec->aspect_ratio; |
1817 |
stats->data.vol.par_width = dec->par_width; |
stats->data.vol.par_width = dec->par_width; |
1818 |
stats->data.vol.par_height = dec->par_height; |
stats->data.vol.par_height = dec->par_height; |
|
frame->length = BitstreamPos(&bs) / 8; |
|
1819 |
emms(); |
emms(); |
1820 |
return XVID_ERR_OK; |
return BitstreamPos(&bs)/8; /* number of bytes consumed */ |
1821 |
} |
} |
1822 |
goto repeat; |
goto repeat; |
1823 |
} |
} |
1826 |
|
|
1827 |
|
|
1828 |
/* packed_mode: special-N_VOP treament */ |
/* packed_mode: special-N_VOP treament */ |
1829 |
if (dec->packed_mode && vop_type == N_VOP) |
if (dec->packed_mode && coding_type == N_VOP) |
1830 |
{ |
{ |
1831 |
if (dec->low_delay_default && dec->frames > 0) |
if (dec->low_delay_default && dec->frames > 0) |
1832 |
{ |
{ |
1833 |
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); |
1834 |
output = 1; |
output = 1; |
1835 |
} |
} |
1836 |
/* ignore otherwise */ |
/* ignore otherwise */ |
1837 |
} |
} |
1838 |
else if (vop_type != B_VOP) |
else if (coding_type != B_VOP) |
1839 |
{ |
{ |
1840 |
switch(vop_type) |
switch(coding_type) |
1841 |
{ |
{ |
1842 |
case I_VOP : |
case I_VOP : |
1843 |
decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold); |
decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold); |
1851 |
fcode_forward, intra_dc_threshold, &gmc_warp); |
fcode_forward, intra_dc_threshold, &gmc_warp); |
1852 |
break; |
break; |
1853 |
case N_VOP : |
case N_VOP : |
1854 |
|
// XXX: not_coded vops are not used for forward prediction |
1855 |
|
// we should not swap(last_mbs,mbs) |
1856 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1857 |
break; |
break; |
1858 |
} |
} |
1861 |
{ |
{ |
1862 |
image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs, |
image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs, |
1863 |
(dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width, |
(dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width, |
1864 |
16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV); |
16, 0); |
1865 |
} |
} |
1866 |
|
|
1867 |
/* 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 */ |
1869 |
{ |
{ |
1870 |
if (dec->low_delay) |
if (dec->low_delay) |
1871 |
{ |
{ |
1872 |
decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution); |
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type); |
1873 |
output = 1; |
output = 1; |
1874 |
} |
} |
1875 |
else if (dec->frames > 0) /* is the reference frame valid? */ |
else if (dec->frames > 0) /* is the reference frame valid? */ |
1876 |
{ |
{ |
1877 |
/* output the reference frame */ |
/* output the reference frame */ |
1878 |
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); |
1879 |
output = 1; |
output = 1; |
1880 |
} |
} |
1881 |
} |
} |
1882 |
|
|
1883 |
image_swap(&dec->refn[0], &dec->refn[1]); |
image_swap(&dec->refn[0], &dec->refn[1]); |
1884 |
image_swap(&dec->cur, &dec->refn[0]); |
image_swap(&dec->cur, &dec->refn[0]); |
1885 |
mb_swap(&dec->mbs, &dec->last_mbs); |
SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); |
1886 |
dec->last_reduced_resolution = reduced_resolution; |
dec->last_reduced_resolution = reduced_resolution; |
1887 |
|
dec->last_coding_type = coding_type; |
1888 |
|
|
1889 |
dec->frames++; |
dec->frames++; |
1890 |
seen_something = 1; |
seen_something = 1; |
1911 |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1912 |
} |
} |
1913 |
|
|
1914 |
decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution); |
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type); |
1915 |
output = 1; |
output = 1; |
1916 |
dec->frames++; |
dec->frames++; |
1917 |
} |
} |
1934 |
if (dec->packed_mode && seen_something) |
if (dec->packed_mode && seen_something) |
1935 |
{ |
{ |
1936 |
/* output the recently decoded frame */ |
/* output the recently decoded frame */ |
1937 |
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; |
|
1938 |
} |
} |
1939 |
else |
else |
1940 |
{ |
{ |
1944 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, |
1945 |
"bframe decoder lag"); |
"bframe decoder lag"); |
1946 |
|
|
1947 |
decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/); |
decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP); |
1948 |
} |
if (stats) stats->type = XVID_TYPE_NOTHING; |
|
} |
|
1949 |
|
|
1950 |
frame->length = BitstreamPos(&bs) / 8; |
} |
|
|
|
|
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 */ |
|
1951 |
} |
} |
1952 |
|
|
1953 |
emms(); |
emms(); |
|
|
|
1954 |
stop_global_timer(); |
stop_global_timer(); |
1955 |
|
|
1956 |
return XVID_ERR_OK; |
return BitstreamPos(&bs) / 8; /* number of bytes consumed */ |
1957 |
} |
} |