History

avcodec/wmaprodec: add gapless support

Paul B Mahol committed 5 years ago

Commit 61c2c9e1 parent 8a6becc1 file · +99 −59Browse files

Unified · Split

libavcodec/wmaprodec.c+99−59
@@ -88,6 +88,7 @@ · 87 unmodified lines
8888
8989 #include <inttypes.h>
9090
91+#include "libavutil/audio_fifo.h"
9192 #include "libavutil/ffmath.h"
9293 #include "libavutil/float_dsp.h"
9394 #include "libavutil/intfloat.h"
@@ -192,6 +193,8 @@ typedef struct WMAProDecodeCtx { · 98 unmodified lines
192193 uint8_t dynamic_range_compression; ///< frame contains DRC data
193194 uint8_t bits_per_sample; ///< integer audio sample size for the unscaled IMDCT output (used to scale to [-1.0, 1.0])
194195 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
195198 uint16_t log2_frame_size;
196199 int8_t lfe_channel; ///< lfe channel index
197200 uint8_t max_num_subframes;
@@ -246,9 +249,9 @@ typedef struct XMADecodeCtx { · 48 unmodified lines
246249 AVFrame *frames[XMA_MAX_STREAMS];
247250 int current_stream;
248251 int num_streams;
249− float samples[XMA_MAX_CHANNELS][512 * 64];
250− int offset[XMA_MAX_STREAMS];
252+ AVAudioFifo *samples[2][XMA_MAX_CHANNELS];
251253 int start_channel[XMA_MAX_STREAMS];
254+ int trim_start, trim_end;
252255 } XMADecodeCtx;
253256
254257 /**
@@ -417,10 +420,7 @@ static av_cold int decode_init(WMAProDecodeCtx *s, AVCodecContext *avctx, int nu · 162 unmodified lines
417420 }
418421
419422 /** frame info */
420− if (avctx->codec_id != AV_CODEC_ID_WMAPRO)
421− s->skip_frame = 0;
422− else
423− s->skip_frame = 1; /* skip first frame */
423+ s->skip_frame = 1; /* skip first frame */
424424
425425 s->packet_loss = 1;
426426 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
14621462 ff_dlog(s->avctx, "drc_gain %i\n", s->drc_gain);
14631463 }
14641464
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 */
14671465 if (get_bits1(gb)) {
1468− int av_unused skip;
1469−
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− }
1466+ if (get_bits1(gb))
1467+ s->trim_start = get_bits(gb, av_log2(s->samples_per_frame * 2));
14811468
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;
14821473 }
14831474
14841475 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
16411632 s->samples_per_frame * sizeof(*s->channel[i].out) >> 1);
16421633 }
16431634
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−
16471635 s->eof_done = 1;
16481636 s->packet_done = 1;
16491637 *got_frame_ptr = 1;
@@ -1781,6 +1769,33 @@ static int decode_packet(AVCodecContext *avctx, WMAProDecodeCtx *s, · 131 unmodified lines
17811769 if (s->packet_loss)
17821770 return AVERROR_INVALIDDATA;
17831771
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+
17841799 return get_bits_count(gb) >> 3;
17851800 }
17861801
@@ -1814,34 +1829,40 @@ static int xma_decode_packet(AVCodecContext *avctx, void *data, · 27 unmodified lines
18141829 XMADecodeCtx *s = avctx->priv_data;
18151830 int got_stream_frame_ptr = 0;
18161831 AVFrame *frame = data;
1817− int i, ret, offset = INT_MAX;
1832+ int i, ret, eof;
18181833
18191834 if (!s->frames[s->current_stream]->data[0]) {
1835+ avctx->internal->skip_samples = 64;
18201836 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)
18221844 return ret;
1823− }
18241845 }
18251846 /* decode current stream packet */
18261847 ret = decode_packet(avctx, &s->xma[s->current_stream], s->frames[s->current_stream],
18271848 &got_stream_frame_ptr, avpkt);
1828−
1829− if (got_stream_frame_ptr && s->offset[s->current_stream] >= 64) {
1830− got_stream_frame_ptr = 0;
1831− ret = AVERROR_INVALIDDATA;
1832− }
1849+ eof = s->xma[s->current_stream].eof_done;
1850+ if (s->xma[s->current_stream].trim_start)
1851+ s->trim_start = s->xma[s->current_stream].trim_start;
1852+ if (s->xma[s->current_stream].trim_end)
1853+ s->trim_end = s->xma[s->current_stream].trim_end;
18331854
18341855 /* copy stream samples (1/2ch) to sample buffer (Nch) */
18351856 if (got_stream_frame_ptr) {
18361857 int start_ch = s->start_channel[s->current_stream];
1837− memcpy(&s->samples[start_ch + 0][s->offset[s->current_stream] * 512],
1838− s->frames[s->current_stream]->extended_data[0], 512 * 4);
1858+ const int nb_samples = s->frames[s->current_stream]->nb_samples;
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);
18391863 if (s->xma[s->current_stream].nb_channels > 1)
1840− memcpy(&s->samples[start_ch + 1][s->offset[s->current_stream] * 512],
1841− s->frames[s->current_stream]->extended_data[1], 512 * 4);
1842− s->offset[s->current_stream]++;
1864+ av_audio_fifo_write(s->samples[1][start_ch + 1], right, nb_samples);
18431865 } else if (ret < 0) {
1844− memset(s->offset, 0, sizeof(s->offset));
18451866 s->current_stream = 0;
18461867 return ret;
18471868 }
@@ -1851,6 +1872,7 @@ static int xma_decode_packet(AVCodecContext *avctx, void *data, · 3 unmodified lines
18511872 * (at start there is one packet per stream, then interleave non-linearly). */
18521873 if (s->xma[s->current_stream].packet_done ||
18531874 s->xma[s->current_stream].packet_loss) {
1875+ int nb_samples = INT_MAX;
18541876
18551877 /* select stream with 0 skip_packets (= uses next packet) */
18561878 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
18701892 }
18711893
18721894 /* all other streams skip next packet */
1873− for (i = 0; i < s->num_streams; i++) {
1895+ for (i = 0; i < s->num_streams; i++)
18741896 s->xma[i].skip_packets = FFMAX(0, s->xma[i].skip_packets - 1);
1875− }
18761897
1877− /* copy samples from buffer to output if possible */
18781898 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]));
18801902 }
1881− if (offset > 0) {
1903+
1904+ /* copy samples from buffer to output if possible */
1905+ if (nb_samples > 4096 || eof) {
18821906 int bret;
18831907
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;
18851915 if ((bret = ff_get_buffer(avctx, frame, 0)) < 0)
18861916 return bret;
18871917
1888− /* copy samples buffer (Nch) to frame samples (Nch), move unconsumed samples */
18891918 for (i = 0; i < s->num_streams; i++) {
1890− int start_ch = s->start_channel[i];
1891− memcpy(frame->extended_data[start_ch + 0], s->samples[start_ch + 0], frame->nb_samples * 4);
1892− if (s->xma[i].nb_channels > 1)
1893− memcpy(frame->extended_data[start_ch + 1], s->samples[start_ch + 1], frame->nb_samples * 4);
1894−
1895− s->offset[i] -= offset;
1896− if (s->offset[i]) {
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− }
1919+ const int start_ch = s->start_channel[s->current_stream];
1920+ void *left[1] = { frame->extended_data[0] };
1921+ void *right[1] = { frame->extended_data[1] };
1922+
1923+ av_audio_fifo_read(s->samples[0][start_ch + 0], left, nb_samples);
1924+ if (s->xma[s->current_stream].nb_channels > 1)
1925+ av_audio_fifo_read(s->samples[1][start_ch + 1], right, nb_samples);
19011926 }
19021927
19031928 *got_frame_ptr = 1;
@@ -1961,6 +1986,13 @@ static av_cold int xma_decode_init(AVCodecContext *avctx) · 57 unmodified lines
19611986 if (start_channels != avctx->channels)
19621987 return AVERROR_INVALIDDATA;
19631988
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+
19641996 return ret;
19651997 }
19661998
@@ -1975,6 +2007,11 @@ static av_cold int xma_decode_end(AVCodecContext *avctx) · 8 unmodified lines
19752007 }
19762008 s->num_streams = 0;
19772009
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+
19782015 return 0;
19792016 }
19802017
@@ -1989,9 +2026,9 @@ static void flush(WMAProDecodeCtx *s) · 8 unmodified lines
19892026 s->packet_loss = 1;
19902027 s->skip_packets = 0;
19912028 s->eof_done = 0;
2029+ s->skip_frame = 1;
19922030 }
19932031
1994−
19952032 /**
19962033 *@brief Clear decoder buffers (for seeking).
19972034 *@param avctx codec context
@@ -2008,14 +2045,17 @@ static void xma_flush(AVCodecContext *avctx) · 10 unmodified lines
20082045 XMADecodeCtx *s = avctx->priv_data;
20092046 int i;
20102047
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+
20112053 for (i = 0; i < s->num_streams; i++)
20122054 flush(&s->xma[i]);
20132055
2014− memset(s->offset, 0, sizeof(s->offset));
20152056 s->current_stream = 0;
20162057 }
20172058
2018−
20192059 /**
20202060 *@brief wmapro decoder
20212061 */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t