Unified · Split
| @@ -88,6 +88,7 @@ · 87 unmodified lines | |||
| 88 | 88 | ||
| 89 | #include <inttypes.h> | 89 | #include <inttypes.h> |
| 90 | 90 | ||
| 91 | #include "libavutil/audio_fifo.h" | ||
| 91 | #include "libavutil/ffmath.h" | 92 | #include "libavutil/ffmath.h" |
| 92 | #include "libavutil/float_dsp.h" | 93 | #include "libavutil/float_dsp.h" |
| 93 | #include "libavutil/intfloat.h" | 94 | #include "libavutil/intfloat.h" |
| @@ -192,6 +193,8 @@ typedef struct WMAProDecodeCtx { · 98 unmodified lines | |||
| 192 | uint8_t dynamic_range_compression; ///< frame contains DRC data | 193 | uint8_t dynamic_range_compression; ///< frame contains DRC data |
| 193 | uint8_t bits_per_sample; ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0]) | 194 | uint8_t bits_per_sample; ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0]) |
| 194 | uint16_t samples_per_frame; ///< number of samples to output | 195 | uint16_t samples_per_frame; ///< number of samples to output |
| 196 | uint16_t trim_start; ///< number of samples to skip at start | ||
| 197 | uint16_t trim_end; ///< number of samples to skip at end | ||
| 195 | uint16_t log2_frame_size; | 198 | uint16_t log2_frame_size; |
| 196 | int8_t lfe_channel; ///< lfe channel index | 199 | int8_t lfe_channel; ///< lfe channel index |
| 197 | uint8_t max_num_subframes; | 200 | uint8_t max_num_subframes; |
| @@ -246,9 +249,9 @@ typedef struct XMADecodeCtx { · 48 unmodified lines | |||
| 246 | AVFrame *frames[XMA_MAX_STREAMS]; | 249 | AVFrame *frames[XMA_MAX_STREAMS]; |
| 247 | int current_stream; | 250 | int current_stream; |
| 248 | int num_streams; | 251 | int num_streams; |
| 249 | float samples[XMA_MAX_CHANNELS][512 * 64]; | 252 | AVAudioFifo *samples[2][XMA_MAX_CHANNELS]; |
| 250 | int offset[XMA_MAX_STREAMS]; | ||
| 251 | int start_channel[XMA_MAX_STREAMS]; | 253 | int start_channel[XMA_MAX_STREAMS]; |
| 254 | int trim_start, trim_end; | ||
| 252 | } XMADecodeCtx; | 255 | } XMADecodeCtx; |
| 253 | 256 | ||
| 254 | /** | 257 | /** |
| @@ -417,10 +420,7 @@ static av_cold int decode_init(WMAProDecodeCtx *s, AVCodecContext *avctx, int nu · 162 unmodified lines | |||
| 417 | } | 420 | } |
| 418 | 421 | ||
| 419 | /** frame info */ | 422 | /** frame info */ |
| 420 | if (avctx->codec_id != AV_CODEC_ID_WMAPRO) | 423 | s->skip_frame = 1; /* skip first frame */ |
| 421 | s->skip_frame = 0; | ||
| 422 | else | ||
| 423 | s->skip_frame = 1; /* skip first frame */ | ||
| 424 | 424 | ||
| 425 | s->packet_loss = 1; | 425 | s->packet_loss = 1; |
| 426 | s->len_prefix = (s->decode_flags & 0x40); | 426 | s->len_prefix = (s->decode_flags & 0x40); |
| @@ -1462,23 +1462,14 @@ static int decode_frame(WMAProDecodeCtx *s, AVFrame *frame, int *got_frame_ptr) · 1035 unmodified lines | |||
| 1462 | ff_dlog(s->avctx, "drc_gain %i\n", s->drc_gain); | 1462 | ff_dlog(s->avctx, "drc_gain %i\n", s->drc_gain); |
| 1463 | } | 1463 | } |
| 1464 | 1464 | ||
| 1465 | /** no idea what these are for, might be the number of samples | ||
| 1466 | that need to be skipped at the beginning or end of a stream */ | ||
| 1467 | if (get_bits1(gb)) { | 1465 | if (get_bits1(gb)) { |
| 1468 | int av_unused skip; | 1466 | if (get_bits1(gb)) |
| 1469 | 1467 | s->trim_start = get_bits(gb, av_log2(s->samples_per_frame * 2)); | |
| 1470 | /** usually true for the first frame */ | ||
| 1471 | if (get_bits1(gb)) { | ||
| 1472 | skip = get_bits(gb, av_log2(s->samples_per_frame * 2)); | ||
| 1473 | ff_dlog(s->avctx, "start skip: %i\n", skip); | ||
| 1474 | } | ||
| 1475 | |||
| 1476 | /** sometimes true for the last frame */ | ||
| 1477 | if (get_bits1(gb)) { | ||
| 1478 | skip = get_bits(gb, av_log2(s->samples_per_frame * 2)); | ||
| 1479 | ff_dlog(s->avctx, "end skip: %i\n", skip); | ||
| 1480 | } | ||
| 1481 | 1468 | ||
| 1469 | if (get_bits1(gb)) | ||
| 1470 | s->trim_end = get_bits(gb, av_log2(s->samples_per_frame * 2)); | ||
| 1471 | } else { | ||
| 1472 | s->trim_start = s->trim_end = 0; | ||
| 1482 | } | 1473 | } |
| 1483 | 1474 | ||
| 1484 | ff_dlog(s->avctx, "BITSTREAM: frame header length was %i\n", | 1475 | ff_dlog(s->avctx, "BITSTREAM: frame header length was %i\n", |
| @@ -1641,9 +1632,6 @@ static int decode_packet(AVCodecContext *avctx, WMAProDecodeCtx *s, · 156 unmodified lines | |||
| 1641 | s->samples_per_frame * sizeof(*s->channel[i].out) >> 1); | 1632 | s->samples_per_frame * sizeof(*s->channel[i].out) >> 1); |
| 1642 | } | 1633 | } |
| 1643 | 1634 | ||
| 1644 | /* TODO: XMA should output 128 samples only (instead of 512) and WMAPRO | ||
| 1645 | * maybe 768 (with 2048), XMA needs changes in multi-stream handling though. */ | ||
| 1646 | |||
| 1647 | s->eof_done = 1; | 1635 | s->eof_done = 1; |
| 1648 | s->packet_done = 1; | 1636 | s->packet_done = 1; |
| 1649 | *got_frame_ptr = 1; | 1637 | *got_frame_ptr = 1; |
| @@ -1781,6 +1769,33 @@ static int decode_packet(AVCodecContext *avctx, WMAProDecodeCtx *s, · 131 unmodified lines | |||
| 1781 | if (s->packet_loss) | 1769 | if (s->packet_loss) |
| 1782 | return AVERROR_INVALIDDATA; | 1770 | return AVERROR_INVALIDDATA; |
| 1783 | 1771 | ||
| 1772 | if (s->trim_start && avctx->codec_id == AV_CODEC_ID_WMAPRO) { | ||
| 1773 | AVFrame *frame = data; | ||
| 1774 | |||
| 1775 | if (s->trim_start < frame->nb_samples) { | ||
| 1776 | for (int ch = 0; ch < frame->channels; ch++) | ||
| 1777 | frame->extended_data[ch] += s->trim_start * 4; | ||
| 1778 | |||
| 1779 | frame->nb_samples -= s->trim_start; | ||
| 1780 | } else { | ||
| 1781 | *got_frame_ptr = 0; | ||
| 1782 | } | ||
| 1783 | |||
| 1784 | s->trim_start = 0; | ||
| 1785 | } | ||
| 1786 | |||
| 1787 | if (s->trim_end && avctx->codec_id == AV_CODEC_ID_WMAPRO) { | ||
| 1788 | AVFrame *frame = data; | ||
| 1789 | |||
| 1790 | if (s->trim_end < frame->nb_samples) { | ||
| 1791 | frame->nb_samples -= s->trim_end; | ||
| 1792 | } else { | ||
| 1793 | *got_frame_ptr = 0; | ||
| 1794 | } | ||
| 1795 | |||
| 1796 | s->trim_end = 0; | ||
| 1797 | } | ||
| 1798 | |||
| 1784 | return get_bits_count(gb) >> 3; | 1799 | return get_bits_count(gb) >> 3; |
| 1785 | } | 1800 | } |
| 1786 | 1801 | ||
| @@ -1814,34 +1829,40 @@ static int xma_decode_packet(AVCodecContext *avctx, void *data, · 27 unmodified lines | |||
| 1814 | XMADecodeCtx *s = avctx->priv_data; | 1829 | XMADecodeCtx *s = avctx->priv_data; |
| 1815 | int got_stream_frame_ptr = 0; | 1830 | int got_stream_frame_ptr = 0; |
| 1816 | AVFrame *frame = data; | 1831 | AVFrame *frame = data; |
| 1817 | int i, ret, offset = INT_MAX; | 1832 | int i, ret, eof; |
| 1818 | 1833 | ||
| 1819 | if (!s->frames[s->current_stream]->data[0]) { | 1834 | if (!s->frames[s->current_stream]->data[0]) { |
| 1835 | avctx->internal->skip_samples = 64; | ||
| 1820 | s->frames[s->current_stream]->nb_samples = 512; | 1836 | s->frames[s->current_stream]->nb_samples = 512; |
| 1821 | if ((ret = ff_get_buffer(avctx, s->frames[s->current_stream], 0)) < 0) { | 1837 | if ((ret = ff_get_buffer(avctx, s->frames[s->current_stream], 0)) < 0) |
| 1838 | return ret; | ||
| 1839 | } else if (s->frames[s->current_stream]->nb_samples != 512) { | ||
| 1840 | avctx->internal->skip_samples = 64; | ||
| 1841 | av_frame_unref(s->frames[s->current_stream]); | ||
| 1842 | s->frames[s->current_stream]->nb_samples = 512; | ||
| 1843 | if ((ret = ff_get_buffer(avctx, s->frames[s->current_stream], 0)) < 0) | ||
| 1822 | return ret; | 1844 | return ret; |
| 1823 | } | ||
| 1824 | } | 1845 | } |
| 1825 | /* decode current stream packet */ | 1846 | /* decode current stream packet */ |
| 1826 | ret = decode_packet(avctx, &s->xma[s->current_stream], s->frames[s->current_stream], | 1847 | ret = decode_packet(avctx, &s->xma[s->current_stream], s->frames[s->current_stream], |
| 1827 | &got_stream_frame_ptr, avpkt); | 1848 | &got_stream_frame_ptr, avpkt); |
| 1828 | 1849 | eof = s->xma[s->current_stream].eof_done; | |
| 1829 | if (got_stream_frame_ptr && s->offset[s->current_stream] >= 64) { | 1850 | if (s->xma[s->current_stream].trim_start) |
| 1830 | got_stream_frame_ptr = 0; | 1851 | s->trim_start = s->xma[s->current_stream].trim_start; |
| 1831 | ret = AVERROR_INVALIDDATA; | 1852 | if (s->xma[s->current_stream].trim_end) |
| 1832 | } | 1853 | s->trim_end = s->xma[s->current_stream].trim_end; |
| 1833 | 1854 | ||
| 1834 | /* copy stream samples (1/2ch) to sample buffer (Nch) */ | 1855 | /* copy stream samples (1/2ch) to sample buffer (Nch) */ |
| 1835 | if (got_stream_frame_ptr) { | 1856 | if (got_stream_frame_ptr) { |
| 1836 | int start_ch = s->start_channel[s->current_stream]; | 1857 | int start_ch = s->start_channel[s->current_stream]; |
| 1837 | memcpy(&s->samples[start_ch + 0][s->offset[s->current_stream] * 512], | 1858 | const int nb_samples = s->frames[s->current_stream]->nb_samples; |
| 1838 | s->frames[s->current_stream]->extended_data[0], 512 * 4); | 1859 | void *left[1] = { s->frames[s->current_stream]->extended_data[0] }; |
| 1860 | void *right[1] = { s->frames[s->current_stream]->extended_data[1] }; | ||
| 1861 | |||
| 1862 | av_audio_fifo_write(s->samples[0][start_ch + 0], left, nb_samples); | ||
| 1839 | if (s->xma[s->current_stream].nb_channels > 1) | 1863 | if (s->xma[s->current_stream].nb_channels > 1) |
| 1840 | memcpy(&s->samples[start_ch + 1][s->offset[s->current_stream] * 512], | 1864 | av_audio_fifo_write(s->samples[1][start_ch + 1], right, nb_samples); |
| 1841 | s->frames[s->current_stream]->extended_data[1], 512 * 4); | ||
| 1842 | s->offset[s->current_stream]++; | ||
| 1843 | } else if (ret < 0) { | 1865 | } else if (ret < 0) { |
| 1844 | memset(s->offset, 0, sizeof(s->offset)); | ||
| 1845 | s->current_stream = 0; | 1866 | s->current_stream = 0; |
| 1846 | return ret; | 1867 | return ret; |
| 1847 | } | 1868 | } |
| @@ -1851,6 +1872,7 @@ static int xma_decode_packet(AVCodecContext *avctx, void *data, · 3 unmodified lines | |||
| 1851 | * (at start there is one packet per stream, then interleave non-linearly). */ | 1872 | * (at start there is one packet per stream, then interleave non-linearly). */ |
| 1852 | if (s->xma[s->current_stream].packet_done || | 1873 | if (s->xma[s->current_stream].packet_done || |
| 1853 | s->xma[s->current_stream].packet_loss) { | 1874 | s->xma[s->current_stream].packet_loss) { |
| 1875 | int nb_samples = INT_MAX; | ||
| 1854 | 1876 | ||
| 1855 | /* select stream with 0 skip_packets (= uses next packet) */ | 1877 | /* select stream with 0 skip_packets (= uses next packet) */ |
| 1856 | if (s->xma[s->current_stream].skip_packets != 0) { | 1878 | if (s->xma[s->current_stream].skip_packets != 0) { |
| @@ -1870,34 +1892,37 @@ static int xma_decode_packet(AVCodecContext *avctx, void *data, · 13 unmodified lines | |||
| 1870 | } | 1892 | } |
| 1871 | 1893 | ||
| 1872 | /* all other streams skip next packet */ | 1894 | /* all other streams skip next packet */ |
| 1873 | for (i = 0; i < s->num_streams; i++) { | 1895 | for (i = 0; i < s->num_streams; i++) |
| 1874 | s->xma[i].skip_packets = FFMAX(0, s->xma[i].skip_packets - 1); | 1896 | s->xma[i].skip_packets = FFMAX(0, s->xma[i].skip_packets - 1); |
| 1875 | } | ||
| 1876 | 1897 | ||
| 1877 | /* copy samples from buffer to output if possible */ | ||
| 1878 | for (i = 0; i < s->num_streams; i++) { | 1898 | for (i = 0; i < s->num_streams; i++) { |
| 1879 | offset = FFMIN(offset, s->offset[i]); | 1899 | int start_ch = s->start_channel[s->current_stream]; |
| 1900 | |||
| 1901 | nb_samples = FFMIN(nb_samples, av_audio_fifo_size(s->samples[0][start_ch])); | ||
| 1880 | } | 1902 | } |
| 1881 | if (offset > 0) { | 1903 | |
| 1904 | /* copy samples from buffer to output if possible */ | ||
| 1905 | if (nb_samples > 4096 || eof) { | ||
| 1882 | int bret; | 1906 | int bret; |
| 1883 | 1907 | ||
| 1884 | frame->nb_samples = 512 * offset; | 1908 | if (eof) { |
| 1909 | nb_samples -= FFMIN(nb_samples, s->trim_end + s->trim_start - 128 - 64); | ||
| 1910 | s->trim_end = s->trim_start = 0; | ||
| 1911 | } | ||
| 1912 | frame->nb_samples = nb_samples; | ||
| 1913 | if (frame->nb_samples <= 0) | ||
| 1914 | return ret; | ||
| 1885 | if ((bret = ff_get_buffer(avctx, frame, 0)) < 0) | 1915 | if ((bret = ff_get_buffer(avctx, frame, 0)) < 0) |
| 1886 | return bret; | 1916 | return bret; |
| 1887 | 1917 | ||
| 1888 | /* copy samples buffer (Nch) to frame samples (Nch), move unconsumed samples */ | ||
| 1889 | for (i = 0; i < s->num_streams; i++) { | 1918 | for (i = 0; i < s->num_streams; i++) { |
| 1890 | int start_ch = s->start_channel[i]; | 1919 | const int start_ch = s->start_channel[s->current_stream]; |
| 1891 | memcpy(frame->extended_data[start_ch + 0], s->samples[start_ch + 0], frame->nb_samples * 4); | 1920 | void *left[1] = { frame->extended_data[0] }; |
| 1892 | if (s->xma[i].nb_channels > 1) | 1921 | void *right[1] = { frame->extended_data[1] }; |
| 1893 | memcpy(frame->extended_data[start_ch + 1], s->samples[start_ch + 1], frame->nb_samples * 4); | 1922 | |
| 1894 | 1923 | av_audio_fifo_read(s->samples[0][start_ch + 0], left, nb_samples); | |
| 1895 | s->offset[i] -= offset; | 1924 | if (s->xma[s->current_stream].nb_channels > 1) |
| 1896 | if (s->offset[i]) { | 1925 | av_audio_fifo_read(s->samples[1][start_ch + 1], right, nb_samples); |
| 1897 | memmove(s->samples[start_ch + 0], s->samples[start_ch + 0] + frame->nb_samples, s->offset[i] * 4 * 512); | ||
| 1898 | if (s->xma[i].nb_channels > 1) | ||
| 1899 | memmove(s->samples[start_ch + 1], s->samples[start_ch + 1] + frame->nb_samples, s->offset[i] * 4 * 512); | ||
| 1900 | } | ||
| 1901 | } | 1926 | } |
| 1902 | 1927 | ||
| 1903 | *got_frame_ptr = 1; | 1928 | *got_frame_ptr = 1; |
| @@ -1961,6 +1986,13 @@ static av_cold int xma_decode_init(AVCodecContext *avctx) · 57 unmodified lines | |||
| 1961 | if (start_channels != avctx->channels) | 1986 | if (start_channels != avctx->channels) |
| 1962 | return AVERROR_INVALIDDATA; | 1987 | return AVERROR_INVALIDDATA; |
| 1963 | 1988 | ||
| 1989 | for (int i = 0; i < XMA_MAX_CHANNELS; i++) { | ||
| 1990 | s->samples[0][i] = av_audio_fifo_alloc(avctx->sample_fmt, 1, 64 * 512); | ||
| 1991 | s->samples[1][i] = av_audio_fifo_alloc(avctx->sample_fmt, 1, 64 * 512); | ||
| 1992 | if (!s->samples[0][i] || !s->samples[1][i]) | ||
| 1993 | return AVERROR(ENOMEM); | ||
| 1994 | } | ||
| 1995 | |||
| 1964 | return ret; | 1996 | return ret; |
| 1965 | } | 1997 | } |
| 1966 | 1998 | ||
| @@ -1975,6 +2007,11 @@ static av_cold int xma_decode_end(AVCodecContext *avctx) · 8 unmodified lines | |||
| 1975 | } | 2007 | } |
| 1976 | s->num_streams = 0; | 2008 | s->num_streams = 0; |
| 1977 | 2009 | ||
| 2010 | for (i = 0; i < XMA_MAX_CHANNELS; i++) { | ||
| 2011 | av_audio_fifo_free(s->samples[0][i]); | ||
| 2012 | av_audio_fifo_free(s->samples[1][i]); | ||
| 2013 | } | ||
| 2014 | |||
| 1978 | return 0; | 2015 | return 0; |
| 1979 | } | 2016 | } |
| 1980 | 2017 | ||
| @@ -1989,9 +2026,9 @@ static void flush(WMAProDecodeCtx *s) · 8 unmodified lines | |||
| 1989 | s->packet_loss = 1; | 2026 | s->packet_loss = 1; |
| 1990 | s->skip_packets = 0; | 2027 | s->skip_packets = 0; |
| 1991 | s->eof_done = 0; | 2028 | s->eof_done = 0; |
| 2029 | s->skip_frame = 1; | ||
| 1992 | } | 2030 | } |
| 1993 | 2031 | ||
| 1994 | |||
| 1995 | /** | 2032 | /** |
| 1996 | *@brief Clear decoder buffers (for seeking). | 2033 | *@brief Clear decoder buffers (for seeking). |
| 1997 | *@param avctx codec context | 2034 | *@param avctx codec context |
| @@ -2008,14 +2045,17 @@ static void xma_flush(AVCodecContext *avctx) · 10 unmodified lines | |||
| 2008 | XMADecodeCtx *s = avctx->priv_data; | 2045 | XMADecodeCtx *s = avctx->priv_data; |
| 2009 | int i; | 2046 | int i; |
| 2010 | 2047 | ||
| 2048 | for (i = 0; i < XMA_MAX_CHANNELS; i++) { | ||
| 2049 | av_audio_fifo_reset(s->samples[0][i]); | ||
| 2050 | av_audio_fifo_reset(s->samples[1][i]); | ||
| 2051 | } | ||
| 2052 | |||
| 2011 | for (i = 0; i < s->num_streams; i++) | 2053 | for (i = 0; i < s->num_streams; i++) |
| 2012 | flush(&s->xma[i]); | 2054 | flush(&s->xma[i]); |
| 2013 | 2055 | ||
| 2014 | memset(s->offset, 0, sizeof(s->offset)); | ||
| 2015 | s->current_stream = 0; | 2056 | s->current_stream = 0; |
| 2016 | } | 2057 | } |
| 2017 | 2058 | ||
| 2018 | |||
| 2019 | /** | 2059 | /** |
| 2020 | *@brief wmapro decoder | 2060 | *@brief wmapro decoder |
| 2021 | */ | 2061 | */ |