[FFmpeg-devel] [RFC] Direct Stream Transfer (DST) decoder

Peter Ross pross at xvid.org
Tue Sep 30 15:42:20 CEST 2014


Signed-off-by: Peter Ross <pross at xvid.org>
---
DST (described in 14496 Part 3 Subpart 10) is the lossless compression standard
for DSD samples. My implementation is ~45% faster than the reference decoder.
Even so, single threaded decoding is still very demanding and there does not
appear to much scope for SIMD optimisation.

The decoder outputs to AV_SAMPLE_FMT_DSD, which is not in FFmpeg yet.
(I guess it could be converted to output PCM, though that kind of defeats
the purpose having 1-bit audio.)

 Changelog               |   1 +
 libavcodec/Makefile     |   1 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/avcodec.h    |   1 +
 libavcodec/codec_desc.c |   7 +
 libavcodec/dst.h        |  28 ++++
 libavcodec/dstdec.c     | 346 ++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 385 insertions(+)
 create mode 100644 libavcodec/dst.h
 create mode 100644 libavcodec/dstdec.c

diff --git a/Changelog b/Changelog
index 8964774..9d8e2b3 100644
--- a/Changelog
+++ b/Changelog
@@ -6,6 +6,7 @@ version <next>:
 - SUP/PGS subtitle demuxer
 - DoP (DSD-over-PCM)
 - Wideband Single-bit Data (WSD) demuxer
+- Direct Stream Transfer (DST) decoder
 
 version 2.4:
 - Icecast protocol
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 08f724e..325ebe1 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -204,6 +204,7 @@ OBJS-$(CONFIG_DSD_MSBF_PLANAR_DECODER) += dsddec.o
 OBJS-$(CONFIG_DSD_MSBF_PLANAR_ENCODER) += dsddec.o
 OBJS-$(CONFIG_DSICINAUDIO_DECODER)     += dsicinaudio.o
 OBJS-$(CONFIG_DSICINVIDEO_DECODER)     += dsicinvideo.o
+OBJS-$(CONFIG_DST_DECODER)             += dstdec.o
 OBJS-$(CONFIG_DVBSUB_DECODER)          += dvbsubdec.o
 OBJS-$(CONFIG_DVBSUB_ENCODER)          += dvbsub.o
 OBJS-$(CONFIG_DVDSUB_DECODER)          += dvdsubdec.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index c988bd1..a1b699d 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -343,6 +343,7 @@ void avcodec_register_all(void)
     REGISTER_ENCDEC (DSD_MSBF,          dsd_msbf);
     REGISTER_ENCDEC (DSD_LSBF_PLANAR,   dsd_lsbf_planar);
     REGISTER_ENCDEC (DSD_MSBF_PLANAR,   dsd_msbf_planar);
+    REGISTER_DECODER(DST,               dst);
     REGISTER_DECODER(DSICINAUDIO,       dsicinaudio);
     REGISTER_ENCDEC (EAC3,              eac3);
     REGISTER_DECODER(EVRC,              evrc);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index ccffbd0..2d176d6 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -498,6 +498,7 @@ enum AVCodecID {
     AV_CODEC_ID_DSD_LSBF_PLANAR = MKBETAG('D','S','D','1'),
     AV_CODEC_ID_DSD_MSBF_PLANAR = MKBETAG('D','S','D','8'),
     AV_CODEC_ID_DOP_S24LE   = MKBETAG('D','O','P','L'),
+    AV_CODEC_ID_DST         = MKBETAG('D','S','T',' '),
 
     /* subtitle codecs */
     AV_CODEC_ID_FIRST_SUBTITLE = 0x17000,          ///< A dummy ID pointing at the start of subtitle codecs.
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 7c202cc..6d0813e 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -2515,6 +2515,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("DoP (DSD-over-PCM); signed 24-bit little-endian"),
         .props     = AV_CODEC_PROP_LOSSLESS,
     },
+    {
+        .id        = AV_CODEC_ID_DST,
+        .type      = AVMEDIA_TYPE_AUDIO,
+        .name      = "dst",
+        .long_name = NULL_IF_CONFIG_SMALL("DST (Direct Stream Transfer)"),
+        .props     = AV_CODEC_PROP_LOSSLESS,
+    },
 
     /* subtitle codecs */
     {
diff --git a/libavcodec/dst.h b/libavcodec/dst.h
new file mode 100644
index 0000000..83c3f7f
--- /dev/null
+++ b/libavcodec/dst.h
@@ -0,0 +1,28 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * Calculate FS44 ratio
+ */
+#define DSD_FS44(sample_rate) (sample_rate / 44100)
+
+/**
+ * Calculate DST frame size
+ * @return samples per frame (1-bit samples)
+ */
+#define DST_SAMPLES_PER_FRAME(sample_rate) (588 * DSD_FS44(sample_rate))
diff --git a/libavcodec/dstdec.c b/libavcodec/dstdec.c
new file mode 100644
index 0000000..51802ac
--- /dev/null
+++ b/libavcodec/dstdec.c
@@ -0,0 +1,346 @@
+/*
+ * Direct Stream Transfer (DST) decoder
+ * Copyright (c) 2014 Peter Ross <pross at xvid.org>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * Direct Stream Transfer (DST) decoder
+ * ISO/IEC 14496-3 Part 3 Subpart 10: Technical description of lossless coding of oversampled audio
+ */
+
+#include "libavcodec/internal.h"
+#include "get_bits.h"
+#include "avcodec.h"
+#include "dst.h"
+#include "golomb.h"
+#include "mathops.h"
+
+#include "libavutil/avassert.h"
+#include "libavutil/intreadwrite.h"
+
+#define DST_MAX_CHANNELS 6
+#define DST_MAX_ELEMENTS (2 * DST_MAX_CHANNELS)
+
+static const int8_t fsets_code_pred_coeff[3][3] = {
+    { -8 },
+    { -16, 8 },
+    { -9, -5, 6 },
+};
+
+static const int8_t probs_code_pred_coeff[3][3] = {
+    { -8 },
+    { -16, 8 },
+    { -24, 24, -8 },
+};
+
+typedef struct {
+    unsigned int elements;
+    unsigned int length[DST_MAX_ELEMENTS];
+    int coeff[DST_MAX_ELEMENTS][128];
+} Table;
+
+static av_cold int decode_init(AVCodecContext *avctx)
+{
+    if (avctx->channels > DST_MAX_CHANNELS) {
+        avpriv_request_sample(avctx, "Channel count %d", avctx->channels);
+        return AVERROR_PATCHWELCOME;
+    }
+
+    avctx->sample_fmt = AV_SAMPLE_FMT_DSD;
+    return 0;
+}
+
+static int read_map(GetBitContext *gb, Table *t, unsigned int map[DST_MAX_CHANNELS], int channels)
+{
+    int ch;
+    t->elements = 1;
+    if (!get_bits1(gb)){
+        map[0] = 0;
+        for (ch = 1; ch < channels; ch++) {
+            int bits = av_log2(t->elements) + 1;
+            map[ch] = get_bits(gb, bits);
+            if (map[ch] == t->elements) {
+                t->elements++;
+                if (t->elements >= DST_MAX_ELEMENTS)
+                    return AVERROR_INVALIDDATA;
+            } else if (map[ch] > t->elements) {
+                return AVERROR_INVALIDDATA;
+            }
+        }
+    } else {
+        memset(map, 0, sizeof(*map) * DST_MAX_CHANNELS);
+    }
+    return 0;
+}
+
+static av_always_inline int get_sr_golomb_dst(GetBitContext *gb, unsigned int k)
+{
+#if 0
+    /* 'run_length' upper bound is not specified; we can never be sure it will fit into get_bits cache */
+    int v = get_ur_golomb(gb, k, INT_MAX, 0);
+#else
+    int v = 0;
+    while (!get_bits1(gb))
+        v++;
+    if (k)
+        v = (v << k) | get_bits(gb, k);
+#endif
+    if (v && get_bits1(gb))
+        v = -v;
+    return v;
+}
+
+static void read_uncoded_coeff(GetBitContext *gb, int *dst, unsigned int elements, int coeff_bits, int is_signed, int offset)
+{
+    unsigned int i;
+    for (i = 0; i < elements; i++)
+        dst[i] = (is_signed ? get_sbits(gb, coeff_bits) : get_bits(gb, coeff_bits)) + offset;
+}
+
+static int read_table(GetBitContext *gb, Table *t, const int8_t code_pred_coeff[3][3], int length_bits, int coeff_bits, int is_signed, int offset)
+{
+    unsigned int i, j, k;
+    for (i = 0; i < t->elements; i++) {
+        t->length[i] = get_bits(gb, length_bits) + 1;
+        if (!get_bits1(gb)) {
+            read_uncoded_coeff(gb, t->coeff[i], t->length[i], coeff_bits, is_signed, offset);
+        } else {
+            int method = get_bits(gb, 2), lsb_size;
+            if (method == 3)
+                return AVERROR_INVALIDDATA;
+
+            read_uncoded_coeff(gb, t->coeff[i], method + 1, coeff_bits, is_signed, offset);
+
+            lsb_size  = get_bits(gb, 3);
+            for (j = method + 1; j < t->length[i]; j++) {
+                int c, x = 0;
+                for (k = 0; k < method + 1; k++)
+                    x += code_pred_coeff[method][k] * t->coeff[i][j - k - 1];
+                c = get_sr_golomb_dst(gb, lsb_size);
+                if (x >= 0)
+                    c -= (x + 4) / 8;
+                else
+                    c += (-x + 3) / 8;
+                t->coeff[i][j] = c;
+            }
+        }
+    }
+    return 0;
+}
+
+typedef struct {
+    unsigned int a;
+    unsigned int c;
+} Arith;
+
+static void ac_init(Arith * ac, GetBitContext *gb)
+{
+    ac->a = 4095;
+    ac->c = get_bits(gb, 12);
+}
+
+#define AC_GET(ac, re, gb, p, e) \
+{ \
+    unsigned int k = ((ac)->a >> 8) | (((ac)->a >> 7) & 1); \
+    unsigned int q = k * p; \
+    unsigned int a_q = (ac)->a - q; \
+    e = (ac)->c < a_q; \
+    if (e) \
+        (ac)->a  = a_q; \
+    else { \
+        (ac)->a  = q; \
+        (ac)->c -= a_q; \
+    } \
+    if ((ac)->a < 2048) { \
+        int n = 11 - av_log2((ac)->a); \
+        (ac)->a <<= n; \
+        (ac)->c = ((ac)->c << n) | SHOW_UBITS(re, gb, n); \
+        SKIP_BITS(re, pb, n); \
+        UPDATE_CACHE(re, gb); \
+    } \
+}
+
+static uint8_t prob_dst_x_bit(int c)
+{
+    return (ff_reverse[c & 127] >> 1) + 1;
+}
+
+static void build_filter(int16_t table[DST_MAX_ELEMENTS][16][256], const Table *fsets)
+{
+    int i, j, k, l;
+    for (i = 0; i < fsets->elements; i++) {
+        int length = fsets->length[i];
+        for (j = 0; j < 16; j++) {
+            int total = FFMAX(0, FFMIN(length - j * 8, 8));
+            for (k = 0; k < 256; k++) {
+                int v = 0;
+                for (l = 0; l < total; l++)
+                    v += (((k >> l) & 1) * 2 - 1) * fsets->coeff[i][j * 8 + l];
+                table[i][j][k] = v;
+            }
+        }
+    }
+}
+
+static int decode_frame(AVCodecContext *avctx, void *data,
+                        int *got_frame_ptr, AVPacket *avpkt)
+{
+    AVFrame *frame = data;
+    unsigned int samples_per_frame = DST_SAMPLES_PER_FRAME(avctx->sample_rate);
+    int ret;
+    unsigned int i, ch, same;
+    GetBitContext gb;
+    Arith ac;
+    Table fsets, probs;
+    unsigned int half_prob[DST_MAX_CHANNELS];
+    unsigned int map_ch_to_felem[DST_MAX_CHANNELS];
+    unsigned int map_ch_to_pelem[DST_MAX_CHANNELS];
+    DECLARE_ALIGNED(16, uint8_t, status)[DST_MAX_CHANNELS][16];
+    DECLARE_ALIGNED(16, int16_t, filter)[DST_MAX_ELEMENTS][16][256];
+
+    frame->nb_samples = samples_per_frame / 8;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
+
+    if (!(avpkt->data[0] & 1)) {
+        if (frame->nb_samples > avpkt->size - 1)
+            av_log(avctx, AV_LOG_WARNING, "short frame");
+        memcpy(frame->data[0], avpkt->data + 1, FFMIN(frame->nb_samples * avctx->channels, avpkt->size - 1));
+        *got_frame_ptr = 1;
+        return avpkt->size;
+    }
+
+    if ((ret = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
+        return ret;
+
+    skip_bits1(&gb);
+
+    /* Segmentation (10.4, 10.5, 10.6) */
+
+    if (!get_bits1(&gb)){
+        avpriv_request_sample(avctx, "Same_Segmentation=0");
+        return AVERROR_PATCHWELCOME;
+    }
+
+    if (!get_bits1(&gb)){
+        avpriv_request_sample(avctx, "Same_Segm_For_All_Channels=0");
+        return AVERROR_PATCHWELCOME;
+    }
+
+    if (!get_bits1(&gb)){
+        avpriv_request_sample(avctx, "End_Of_Channel_Segm=0");
+        return AVERROR_PATCHWELCOME;
+    }
+
+    /* Mapping (10.7, 10.8, 10.9) */
+
+    same = get_bits1(&gb);
+
+    if ((ret = read_map(&gb, &fsets, map_ch_to_felem, avctx->channels)) < 0)
+        return ret;
+
+    if (same) {
+        probs.elements = fsets.elements;
+        memcpy(map_ch_to_pelem, map_ch_to_felem, sizeof(map_ch_to_felem));
+    } else {
+        avpriv_request_sample(avctx, "Same_Mapping=0");
+        if ((ret = read_map(&gb, &probs, map_ch_to_pelem, avctx->channels)) < 0)
+            return ret;
+    }
+
+    /* Half Probability (10.10) */
+
+    for (ch = 0; ch < avctx->channels; ch++)
+        half_prob[ch] = get_bits1(&gb);
+
+    /* Filter Coef Sets (10.12) */
+
+    read_table(&gb, &fsets, fsets_code_pred_coeff, 7, 9, 1, 0);
+
+    /* Probability Tables (10.13) */
+
+    read_table(&gb, &probs, probs_code_pred_coeff, 6, 7, 0, 1);
+
+    /* Arithmetic Coded Data (10.11) */
+
+    memset(frame->data[0], 0, avctx->channels * frame->nb_samples);
+
+    skip_bits1(&gb);
+    ac_init(&ac, &gb);
+
+    build_filter(filter, &fsets);
+    memset(status, 0xAA, sizeof(status));
+
+    {
+        unsigned int dst_x_bit;
+        OPEN_READER(re, &gb);
+        UPDATE_CACHE(re, &gb);
+        AC_GET(&ac, re, &gb, prob_dst_x_bit(fsets.coeff[0][0]), dst_x_bit);
+
+        for (i = 0; i < samples_per_frame; i++) {
+            for (ch = 0; ch < avctx->channels; ch++) {
+                unsigned int felem = map_ch_to_felem[ch], prob, residual, v;
+                uint64_t * s = (uint64_t*)status[ch];
+
+#define F(i) filter[felem][i][status[ch][i]]
+                int16_t predict = F( 0) + F( 1) + F( 2) + F( 3) +
+                                  F( 4) + F( 5) + F( 6) + F( 7) +
+                                  F( 8) + F( 9) + F(10) + F(11) +
+                                  F(12) + F(13) + F(14) + F(15);
+#undef F
+
+                if (!half_prob[ch] || i >= fsets.length[felem]) {
+                    unsigned int pelem = map_ch_to_pelem[ch];
+                    unsigned int index = FFABS(predict) >> 3;
+                    prob = probs.coeff[pelem][FFMIN(index, probs.length[pelem] - 1)];
+                } else {
+                    prob = 128;
+                }
+
+                AC_GET(&ac, re, &gb, prob, residual);
+                v = ((predict >> 15) ^ residual) & 1;
+                frame->data[0][ (i >> 3) * avctx->channels + ch] |= v << (7 - (i & 0x7 ));
+
+#if HAVE_BIGENDIAN
+                /* FIXME: not tested */
+                s[0] = (s[0] << 1) | ((s[1] >> 63) & 1);
+                s[1] = (s[1] << 1) | v;
+#else
+                s[1] = (s[1] << 1) | ((s[0] >> 63) & 1);
+                s[0] = (s[0] << 1) | v;
+#endif
+            }
+        }
+    }
+
+    *got_frame_ptr = 1;
+    return avpkt->size;
+}
+
+AVCodec ff_dst_decoder = {
+    .name         = "dst",
+    .long_name    = NULL_IF_CONFIG_SMALL("Digital Stream Transfer (DST)"),
+    .type         = AVMEDIA_TYPE_AUDIO,
+    .id           = AV_CODEC_ID_DST,
+    .init         = decode_init,
+    .decode       = decode_frame,
+    .sample_fmts  = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_DSD,
+                                                   AV_SAMPLE_FMT_NONE },
+};
-- 
1.9.1

-- Peter
(A907 E02F A6E5 0CD2 34CD 20D2 6760 79C5 AC40 DD6B)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20140930/e53edc6c/attachment.asc>


More information about the ffmpeg-devel mailing list