[FFmpeg-cvslog] avcodec: add Direct Stream Transfer (DST) decoder

Peter Ross git at videolan.org
Sun May 15 01:11:59 CEST 2016


ffmpeg | branch: master | Peter Ross <pross at xvid.org> | Thu May  5 21:21:27 2016 +0200| [86e493a6ffac3b3705ea4b276060c380ee2f5e75] | committer: Paul B Mahol

avcodec: add Direct Stream Transfer (DST) decoder

Signed-off-by: Paul B Mahol <onemda at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=86e493a6ffac3b3705ea4b276060c380ee2f5e75
---

 libavcodec/Makefile       |    9 +-
 libavcodec/allcodecs.c    |    1 +
 libavcodec/avcodec.h      |    1 +
 libavcodec/codec_desc.c   |    7 +
 libavcodec/dsd.c          |   86 +++++++++++
 libavcodec/dsd.h          |   53 +++++++
 libavcodec/dsd_tablegen.h |   18 +--
 libavcodec/dsddec.c       |   63 +-------
 libavcodec/dstdec.c       |  374 +++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/utils.c        |    2 +
 libavformat/iff.c         |   85 ++++++++++-
 11 files changed, 611 insertions(+), 88 deletions(-)

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 998477c..3f0ffd1 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -239,13 +239,14 @@ OBJS-$(CONFIG_DNXHD_DECODER)           += dnxhddec.o dnxhddata.o
 OBJS-$(CONFIG_DNXHD_ENCODER)           += dnxhdenc.o dnxhddata.o
 OBJS-$(CONFIG_DPX_DECODER)             += dpx.o
 OBJS-$(CONFIG_DPX_ENCODER)             += dpxenc.o
-OBJS-$(CONFIG_DSD_LSBF_DECODER)        += dsddec.o
-OBJS-$(CONFIG_DSD_MSBF_DECODER)        += dsddec.o
-OBJS-$(CONFIG_DSD_LSBF_PLANAR_DECODER) += dsddec.o
-OBJS-$(CONFIG_DSD_MSBF_PLANAR_DECODER) += dsddec.o
+OBJS-$(CONFIG_DSD_LSBF_DECODER)        += dsddec.o dsd.o
+OBJS-$(CONFIG_DSD_MSBF_DECODER)        += dsddec.o dsd.o
+OBJS-$(CONFIG_DSD_LSBF_PLANAR_DECODER) += dsddec.o dsd.o
+OBJS-$(CONFIG_DSD_MSBF_PLANAR_DECODER) += dsddec.o dsd.o
 OBJS-$(CONFIG_DSICINAUDIO_DECODER)     += dsicinaudio.o
 OBJS-$(CONFIG_DSICINVIDEO_DECODER)     += dsicinvideo.o
 OBJS-$(CONFIG_DSS_SP_DECODER)          += dss_sp.o
+OBJS-$(CONFIG_DST_DECODER)             += dstdec.o dsd.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 d435136..44ebafd 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -395,6 +395,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER(DSD_MSBF_PLANAR,   dsd_msbf_planar);
     REGISTER_DECODER(DSICINAUDIO,       dsicinaudio);
     REGISTER_DECODER(DSS_SP,            dss_sp);
+    REGISTER_DECODER(DST,               dst);
     REGISTER_ENCDEC (EAC3,              eac3);
     REGISTER_DECODER(EVRC,              evrc);
     REGISTER_DECODER(FFWAVESYNTH,       ffwavesynth);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 97b2128..9ec9adf 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -591,6 +591,7 @@ enum AVCodecID {
     AV_CODEC_ID_INTERPLAY_ACM,
     AV_CODEC_ID_XMA1,
     AV_CODEC_ID_XMA2,
+    AV_CODEC_ID_DST,
 
     /* 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 57bd4ba..23d5911 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -2683,6 +2683,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("Xbox Media Audio 2"),
         .props     = AV_CODEC_PROP_LOSSY,
     },
+    {
+        .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/dsd.c b/libavcodec/dsd.c
new file mode 100644
index 0000000..9104f38
--- /dev/null
+++ b/libavcodec/dsd.c
@@ -0,0 +1,86 @@
+/*
+ * Direct Stream Digital (DSD) decoder
+ * based on BSD licensed dsd2pcm by Sebastian Gesemann
+ * Copyright (c) 2009, 2011 Sebastian Gesemann. All rights reserved.
+ * Copyright (c) 2014 Peter Ross
+ *
+ * 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
+ */
+
+#include "libavcodec/internal.h"
+#include "libavcodec/mathops.h"
+#include "avcodec.h"
+#include "dsd_tablegen.h"
+#include "dsd.h"
+
+static av_cold void dsd_ctables_tableinit(void)
+{
+    int t, e, m, sign;
+    double acc[CTABLES];
+    for (e = 0; e < 256; ++e) {
+        memset(acc, 0, sizeof(acc));
+        for (m = 0; m < 8; ++m) {
+            sign = (((e >> (7 - m)) & 1) * 2 - 1);
+            for (t = 0; t < CTABLES; ++t)
+                acc[t] += sign * htaps[t * 8 + m];
+        }
+        for (t = 0; t < CTABLES; ++t)
+            ctables[CTABLES - 1 - t][e] = acc[t];
+    }
+}
+
+av_cold void ff_init_dsd_data(void)
+{
+    static int done = 0;
+    if (done)
+        return;
+    dsd_ctables_tableinit();
+    done = 1;
+}
+
+void ff_dsd2pcm_translate(DSDContext* s, size_t samples, int lsbf,
+                          const unsigned char *src, ptrdiff_t src_stride,
+                          float *dst, ptrdiff_t dst_stride)
+{
+    unsigned pos, i;
+    unsigned char* p;
+    double sum;
+
+    pos = s->pos;
+
+    while (samples-- > 0) {
+        s->buf[pos] = lsbf ? ff_reverse[*src] : *src;
+        src += src_stride;
+
+        p = s->buf + ((pos - CTABLES) & FIFOMASK);
+        *p = ff_reverse[*p];
+
+        sum = 0.0;
+        for (i = 0; i < CTABLES; i++) {
+            unsigned char a = s->buf[(pos                   - i) & FIFOMASK];
+            unsigned char b = s->buf[(pos - (CTABLES*2 - 1) + i) & FIFOMASK];
+            sum += ctables[i][a] + ctables[i][b];
+        }
+
+        *dst = (float)sum;
+        dst += dst_stride;
+
+        pos = (pos + 1) & FIFOMASK;
+    }
+
+    s->pos = pos;
+}
diff --git a/libavcodec/dsd.h b/libavcodec/dsd.h
new file mode 100644
index 0000000..42a9a48
--- /dev/null
+++ b/libavcodec/dsd.h
@@ -0,0 +1,53 @@
+/*
+ * Direct Stream Digital (DSD) decoder
+ * based on BSD licensed dsd2pcm by Sebastian Gesemann
+ * Copyright (c) 2009, 2011 Sebastian Gesemann. All rights reserved.
+ * Copyright (c) 2014 Peter Ross
+ *
+ * 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
+ */
+
+#ifndef AVCODEC_DSD_H
+#define AVCODEC_DSD_H
+
+#include "libavcodec/internal.h"
+#include "libavcodec/mathops.h"
+#include "avcodec.h"
+#include "dsd_tablegen.h"
+
+#define HTAPS   48               /** number of FIR constants */
+#define FIFOSIZE 16              /** must be a power of two */
+#define FIFOMASK (FIFOSIZE - 1)  /** bit mask for FIFO offsets */
+
+#if FIFOSIZE * 8 < HTAPS * 2
+#error "FIFOSIZE too small"
+#endif
+
+/**
+ * Per-channel buffer
+ */
+typedef struct DSDContext {
+    unsigned char buf[FIFOSIZE];
+    unsigned pos;
+} DSDContext;
+
+void ff_init_dsd_data(void);
+
+void ff_dsd2pcm_translate(DSDContext* s, size_t samples, int lsbf,
+                          const unsigned char *src, ptrdiff_t src_stride,
+                          float *dst, ptrdiff_t dst_stride);
+#endif /* AVCODEC_DSD_H */
diff --git a/libavcodec/dsd_tablegen.h b/libavcodec/dsd_tablegen.h
index 990d57a..e5da86a 100644
--- a/libavcodec/dsd_tablegen.h
+++ b/libavcodec/dsd_tablegen.h
@@ -25,6 +25,7 @@
 
 #include <stdint.h>
 #include "libavutil/attributes.h"
+#include "dsd.h"
 
 #define HTAPS   48                /** number of FIR constants */
 #define CTABLES ((HTAPS + 7) / 8) /** number of "8 MACs" lookup tables */
@@ -71,21 +72,4 @@ static const double htaps[HTAPS] = {
 };
 
 static float ctables[CTABLES][256];
-
-static av_cold void dsd_ctables_tableinit(void)
-{
-    int t, e, m, sign;
-    double acc[CTABLES];
-    for (e = 0; e < 256; ++e) {
-        memset(acc, 0, sizeof(acc));
-        for (m = 0; m < 8; ++m) {
-            sign = (((e >> (7 - m)) & 1) * 2 - 1);
-            for (t = 0; t < CTABLES; ++t)
-                acc[t] += sign * htaps[t * 8 + m];
-        }
-        for (t = 0; t < CTABLES; ++t)
-            ctables[CTABLES - 1 - t][e] = acc[t];
-    }
-}
-
 #endif /* AVCODEC_DSD_TABLEGEN_H */
diff --git a/libavcodec/dsddec.c b/libavcodec/dsddec.c
index f1dfd4b..880d691 100644
--- a/libavcodec/dsddec.c
+++ b/libavcodec/dsddec.c
@@ -29,71 +29,14 @@
 #include "libavcodec/internal.h"
 #include "libavcodec/mathops.h"
 #include "avcodec.h"
-#include "dsd_tablegen.h"
-
-#define FIFOSIZE 16              /** must be a power of two */
-#define FIFOMASK (FIFOSIZE - 1)  /** bit mask for FIFO offsets */
-
-#if FIFOSIZE * 8 < HTAPS * 2
-#error "FIFOSIZE too small"
-#endif
-
-/**
- * Per-channel buffer
- */
-typedef struct {
-    unsigned char buf[FIFOSIZE];
-    unsigned pos;
-} DSDContext;
-
-static void dsd2pcm_translate(DSDContext* s, size_t samples, int lsbf,
-                              const unsigned char *src, ptrdiff_t src_stride,
-                              float *dst, ptrdiff_t dst_stride)
-{
-    unsigned pos, i;
-    unsigned char* p;
-    double sum;
-
-    pos = s->pos;
-
-    while (samples-- > 0) {
-        s->buf[pos] = lsbf ? ff_reverse[*src] : *src;
-        src += src_stride;
-
-        p = s->buf + ((pos - CTABLES) & FIFOMASK);
-        *p = ff_reverse[*p];
-
-        sum = 0.0;
-        for (i = 0; i < CTABLES; i++) {
-            unsigned char a = s->buf[(pos                   - i) & FIFOMASK];
-            unsigned char b = s->buf[(pos - (CTABLES*2 - 1) + i) & FIFOMASK];
-            sum += ctables[i][a] + ctables[i][b];
-        }
-
-        *dst = (float)sum;
-        dst += dst_stride;
-
-        pos = (pos + 1) & FIFOMASK;
-    }
-
-    s->pos = pos;
-}
-
-static av_cold void init_static_data(void)
-{
-    static int done = 0;
-    if (done)
-        return;
-    dsd_ctables_tableinit();
-    done = 1;
-}
+#include "dsd.h"
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     DSDContext * s;
     int i;
 
-    init_static_data();
+    ff_init_dsd_data();
 
     s = av_malloc_array(sizeof(DSDContext), avctx->channels);
     if (!s)
@@ -140,7 +83,7 @@ static int decode_frame(AVCodecContext *avctx, void *data,
 
     for (i = 0; i < avctx->channels; i++) {
         float * dst = ((float **)frame->extended_data)[i];
-        dsd2pcm_translate(&s[i], frame->nb_samples, lsbf,
+        ff_dsd2pcm_translate(&s[i], frame->nb_samples, lsbf,
             avpkt->data + i * src_next, src_stride,
             dst, 1);
     }
diff --git a/libavcodec/dstdec.c b/libavcodec/dstdec.c
new file mode 100644
index 0000000..13be24a
--- /dev/null
+++ b/libavcodec/dstdec.c
@@ -0,0 +1,374 @@
+/*
+ * 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 "libavutil/avassert.h"
+#include "libavutil/intreadwrite.h"
+#include "internal.h"
+#include "get_bits.h"
+#include "avcodec.h"
+#include "golomb.h"
+#include "mathops.h"
+#include "dsd.h"
+
+#define DST_MAX_CHANNELS 6
+#define DST_MAX_ELEMENTS (2 * DST_MAX_CHANNELS)
+
+#define DSD_FS44(sample_rate) (sample_rate * 8 / 44100)
+
+#define DST_SAMPLES_PER_FRAME(sample_rate) (588 * DSD_FS44(sample_rate))
+
+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 ArithCoder {
+    unsigned int a;
+    unsigned int c;
+} ArithCoder;
+
+typedef struct Table {
+    unsigned int elements;
+    unsigned int length[DST_MAX_ELEMENTS];
+    int coeff[DST_MAX_ELEMENTS][128];
+} Table;
+
+typedef struct DSTContext {
+    AVClass *class;
+
+    GetBitContext gb;
+    ArithCoder ac;
+    Table fsets, probs;
+    DECLARE_ALIGNED(64, uint8_t, status)[DST_MAX_CHANNELS][16];
+    DECLARE_ALIGNED(16, int16_t, filter)[DST_MAX_ELEMENTS][16][256];
+    DSDContext dsdctx[DST_MAX_CHANNELS];
+} DSTContext;
+
+static av_cold int decode_init(AVCodecContext *avctx)
+{
+    DSTContext *s = avctx->priv_data;
+    int i;
+
+    if (avctx->channels > DST_MAX_CHANNELS) {
+        avpriv_request_sample(avctx, "Channel count %d", avctx->channels);
+        return AVERROR_PATCHWELCOME;
+    }
+
+    avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
+
+    for (i = 0; i < avctx->channels; i++)
+        memset(s->dsdctx[i].buf, 0x69, sizeof(s->dsdctx[i].buf));
+
+    ff_init_dsd_data();
+
+    return 0;
+}
+
+static int read_map(GetBitContext *gb, Table *t, unsigned int map[DST_MAX_CHANNELS], int channels)
+{
+    int ch;
+    t->elements = 1;
+    map[0] = 0;
+    if (!get_bits1(gb)) {
+        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)
+{
+    int v = get_ur_golomb(gb, k, get_bits_left(gb), 0);
+    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)
+{
+    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;
+}
+
+static void ac_init(ArithCoder *ac, GetBitContext *gb)
+{
+    ac->a = 4095;
+    ac->c = get_bits(gb, 12);
+}
+
+static av_always_inline void ac_get(ArithCoder *ac, GetBitContext *gb, int p, int *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) | get_bits(gb, n);
+    }
+}
+
+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 = av_clip(length - j * 8, 0, 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)
+{
+    unsigned samples_per_frame = DST_SAMPLES_PER_FRAME(avctx->sample_rate);
+    unsigned map_ch_to_felem[DST_MAX_CHANNELS];
+    unsigned map_ch_to_pelem[DST_MAX_CHANNELS];
+    unsigned i, ch, same_map, dst_x_bit;
+    unsigned half_prob[DST_MAX_CHANNELS];
+    const int channels = avctx->channels;
+    DSTContext *s = avctx->priv_data;
+    GetBitContext *gb = &s->gb;
+    ArithCoder *ac = &s->ac;
+    AVFrame *frame = data;
+    uint8_t *dsd;
+    float *pcm;
+    int ret;
+
+    if (avpkt->size <= 1)
+        return AVERROR_INVALIDDATA;
+
+    frame->nb_samples = samples_per_frame / 8;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+        return ret;
+    dsd = frame->data[0];
+    pcm = (float *)frame->data[0];
+
+    if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
+        return ret;
+
+    if (!get_bits1(gb)) {
+        skip_bits1(gb);
+        if (get_bits(gb, 6))
+            return AVERROR_INVALIDDATA;
+        memcpy(frame->data[0], avpkt->data + 1, FFMIN(avpkt->size - 1, frame->nb_samples * avctx->channels));
+        goto dsd;
+    }
+
+    /* Segmentation (10.4, 10.5, 10.6) */
+
+    if (!get_bits1(gb)) {
+        avpriv_request_sample(avctx, "Not Same Segmentation");
+        return AVERROR_PATCHWELCOME;
+    }
+
+    if (!get_bits1(gb)) {
+        avpriv_request_sample(avctx, "Not Same Segmentation For All Channels");
+        return AVERROR_PATCHWELCOME;
+    }
+
+    if (!get_bits1(gb)) {
+        avpriv_request_sample(avctx, "Not End Of Channel Segmentation");
+        return AVERROR_PATCHWELCOME;
+    }
+
+    /* Mapping (10.7, 10.8, 10.9) */
+
+    same_map = get_bits1(gb);
+
+    if ((ret = read_map(gb, &s->fsets, map_ch_to_felem, avctx->channels)) < 0)
+        return ret;
+
+    if (same_map) {
+        s->probs.elements = s->fsets.elements;
+        memcpy(map_ch_to_pelem, map_ch_to_felem, sizeof(map_ch_to_felem));
+    } else {
+        avpriv_request_sample(avctx, "Not Same Mapping");
+        if ((ret = read_map(gb, &s->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, &s->fsets, fsets_code_pred_coeff, 7, 9, 1, 0);
+
+    /* Probability Tables (10.13) */
+
+    read_table(gb, &s->probs, probs_code_pred_coeff, 6, 7, 0, 1);
+
+    /* Arithmetic Coded Data (10.11) */
+
+    if (get_bits1(gb))
+        return AVERROR_INVALIDDATA;
+    ac_init(ac, gb);
+
+    build_filter(s->filter, &s->fsets);
+
+    memset(s->status, 0xAA, sizeof(s->status));
+    memset(dsd, 0, frame->nb_samples * 4 * avctx->channels);
+
+    ac_get(ac, gb, prob_dst_x_bit(s->fsets.coeff[0][0]), &dst_x_bit);
+
+    for (i = 0; i < samples_per_frame; i++) {
+        for (ch = 0; ch < channels; ch++) {
+            const unsigned felem = map_ch_to_felem[ch];
+            const int16_t (*filter)[256] = s->filter[felem];
+            uint8_t *status = s->status[ch];
+            int prob, residual, v;
+
+#define F(x) filter[(x)][status[(x)]]
+            const 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 >= s->fsets.length[felem]) {
+                unsigned pelem = map_ch_to_pelem[ch];
+                unsigned index = FFABS(predict) >> 3;
+                prob = s->probs.coeff[pelem][FFMIN(index, s->probs.length[pelem] - 1)];
+            } else {
+                prob = 128;
+            }
+
+            ac_get(ac, gb, prob, &residual);
+            v = ((predict >> 15) ^ residual) & 1;
+            dsd[((i >> 3) * channels + ch) << 2] |= v << (7 - (i & 0x7 ));
+
+            AV_WN64A(status + 8, (AV_RN64A(status + 8) << 1) | ((AV_RN64A(status) >> 63) & 1));
+            AV_WN64A(status, (AV_RN64A(status) << 1) | v);
+        }
+    }
+
+dsd:
+    for (i = 0; i < avctx->channels; i++) {
+        ff_dsd2pcm_translate(&s->dsdctx[i], frame->nb_samples, 0,
+                             frame->data[0] + i * 4,
+                             avctx->channels * 4, pcm + i, avctx->channels);
+    }
+
+    *got_frame_ptr = 1;
+
+    return avpkt->size;
+}
+
+AVCodec ff_dst_decoder = {
+    .name           = "dst",
+    .long_name      = NULL_IF_CONFIG_SMALL("DST (Digital Stream Transfer)"),
+    .type           = AVMEDIA_TYPE_AUDIO,
+    .id             = AV_CODEC_ID_DST,
+    .priv_data_size = sizeof(DSTContext),
+    .init           = decode_init,
+    .decode         = decode_frame,
+    .capabilities   = AV_CODEC_CAP_DR1,
+    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
+                                                      AV_SAMPLE_FMT_NONE },
+};
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index 8652b17..e5a832b 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -3490,6 +3490,8 @@ static int get_audio_frame_duration(enum AVCodecID id, int sr, int ch, int ba,
         /* calc from sample rate */
         if (id == AV_CODEC_ID_TTA)
             return 256 * sr / 245;
+        else if (id == AV_CODEC_ID_DST)
+            return 588 * sr / 44100;
 
         if (ch > 0) {
             /* calc from sample rate and channels */
diff --git a/libavformat/iff.c b/libavformat/iff.c
index 4fb79ed..f3db282 100644
--- a/libavformat/iff.c
+++ b/libavformat/iff.c
@@ -59,6 +59,10 @@
 #define ID_RGB8       MKTAG('R','G','B','8')
 #define ID_RGBN       MKTAG('R','G','B','N')
 #define ID_DSD        MKTAG('D','S','D',' ')
+#define ID_DST        MKTAG('D','S','T',' ')
+#define ID_DSTC       MKTAG('D','S','T','C')
+#define ID_DSTF       MKTAG('D','S','T','F')
+#define ID_FRTE       MKTAG('F','R','T','E')
 #define ID_ANIM       MKTAG('A','N','I','M')
 #define ID_ANHD       MKTAG('A','N','H','D')
 #define ID_DLTA       MKTAG('D','L','T','A')
@@ -159,6 +163,7 @@ static int iff_probe(AVProbeData *p)
 
 static const AVCodecTag dsd_codec_tags[] = {
     { AV_CODEC_ID_DSD_MSBF, ID_DSD },
+    { AV_CODEC_ID_DST,      ID_DST },
     { AV_CODEC_ID_NONE, 0 },
 };
 
@@ -287,7 +292,7 @@ static int parse_dsd_prop(AVFormatContext *s, AVStream *st, uint64_t eof)
         case MKTAG('C','M','P','R'):
             if (size < 4)
                 return AVERROR_INVALIDDATA;
-            tag = avio_rl32(pb);
+            st->codecpar->codec_tag = tag = avio_rl32(pb);
             st->codecpar->codec_id = ff_codec_get_id(dsd_codec_tags, tag);
             if (!st->codecpar->codec_id) {
                 av_log(s, AV_LOG_ERROR, "'%c%c%c%c' compression is not supported\n",
@@ -338,6 +343,63 @@ static int parse_dsd_prop(AVFormatContext *s, AVStream *st, uint64_t eof)
     return 0;
 }
 
+static int read_dst_frame(AVFormatContext *s, AVPacket *pkt)
+{
+    IffDemuxContext *iff = s->priv_data;
+    AVIOContext *pb = s->pb;
+    uint32_t chunk_id;
+    uint64_t chunk_pos, data_pos, data_size;
+    int ret = AVERROR_EOF;
+
+    while (!avio_feof(pb)) {
+        chunk_pos = avio_tell(pb);
+        if (chunk_pos >= iff->body_end)
+            return AVERROR_EOF;
+
+        chunk_id = avio_rl32(pb);
+        data_size = iff->is_64bit ? avio_rb64(pb) : avio_rb32(pb);
+        data_pos = avio_tell(pb);
+
+        if (data_size < 1)
+            return AVERROR_INVALIDDATA;
+
+        switch (chunk_id) {
+        case ID_DSTF:
+            if (!pkt) {
+                iff->body_pos  = avio_tell(pb) - (iff->is_64bit ? 12 : 8);
+                iff->body_size = iff->body_end - iff->body_pos;
+                return 0;
+            }
+            ret = av_get_packet(pb, pkt, data_size);
+            if (ret < 0)
+                return ret;
+            if (data_size & 1)
+                avio_skip(pb, 1);
+            pkt->flags |= AV_PKT_FLAG_KEY;
+            pkt->stream_index = 0;
+            pkt->duration = 588 * s->streams[0]->codecpar->sample_rate / 44100;
+            pkt->pos = chunk_pos;
+
+            chunk_pos = avio_tell(pb);
+            if (chunk_pos >= iff->body_end)
+                return 0;
+
+            avio_seek(pb, chunk_pos, SEEK_SET);
+            return 0;
+
+        case ID_FRTE:
+            if (data_size < 4)
+                return AVERROR_INVALIDDATA;
+            s->streams[0]->duration = avio_rb32(pb) * 588LL * s->streams[0]->codecpar->sample_rate / 44100;
+            break;
+        }
+
+        avio_skip(pb, data_size - (avio_tell(pb) - data_pos) + (data_size & 1));
+    }
+
+    return ret;
+}
+
 static const uint8_t deep_rgb24[] = {0, 0, 0, 3, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
 static const uint8_t deep_rgba[]  = {0, 0, 0, 4, 0, 1, 0, 8, 0, 2, 0, 8, 0, 3, 0, 8};
 static const uint8_t deep_bgra[]  = {0, 0, 0, 4, 0, 3, 0, 8, 0, 2, 0, 8, 0, 1, 0, 8};
@@ -425,10 +487,16 @@ static int iff_read_header(AVFormatContext *s)
         case ID_BODY:
         case ID_DBOD:
         case ID_DSD:
+        case ID_DST:
         case ID_MDAT:
             iff->body_pos = avio_tell(pb);
             iff->body_end = iff->body_pos + data_size;
             iff->body_size = data_size;
+            if (chunk_id == ID_DST) {
+                int ret = read_dst_frame(s, NULL);
+                if (ret < 0)
+                    return ret;
+            }
             break;
 
         case ID_CHAN:
@@ -654,7 +722,8 @@ static int iff_read_header(AVFormatContext *s)
                 avpriv_request_sample(s, "compression %d and bit depth %d", iff->maud_compression, iff->maud_bits);
                 return AVERROR_PATCHWELCOME;
             }
-        } else if (st->codecpar->codec_tag != ID_DSD) {
+        } else if (st->codecpar->codec_tag != ID_DSD &&
+                   st->codecpar->codec_tag != ID_DST) {
             switch (iff->svx8_compression) {
             case COMP_NONE:
                 st->codecpar->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
@@ -675,6 +744,8 @@ static int iff_read_header(AVFormatContext *s)
         st->codecpar->bits_per_coded_sample = av_get_bits_per_sample(st->codecpar->codec_id);
         st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate * st->codecpar->bits_per_coded_sample;
         st->codecpar->block_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample;
+        if (st->codecpar->codec_tag == ID_DSD && st->codecpar->block_align <= 0)
+            return AVERROR_INVALIDDATA;
         break;
 
     case AVMEDIA_TYPE_VIDEO:
@@ -745,16 +816,16 @@ static int iff_read_packet(AVFormatContext *s,
     int ret;
     int64_t pos = avio_tell(pb);
 
-    if (st->codecpar->codec_tag == ID_ANIM) {
-        if (avio_feof(pb))
-            return AVERROR_EOF;
-    } else if (pos >= iff->body_end) {
+    if (avio_feof(pb))
+        return AVERROR_EOF;
+    if (st->codecpar->codec_tag != ID_ANIM && pos >= iff->body_end)
         return AVERROR_EOF;
-    }
 
     if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
         if (st->codecpar->codec_tag == ID_DSD || st->codecpar->codec_tag == ID_MAUD) {
             ret = av_get_packet(pb, pkt, FFMIN(iff->body_end - pos, 1024 * st->codecpar->block_align));
+        } else if (st->codecpar->codec_tag == ID_DST) {
+            return read_dst_frame(s, pkt);
         } else {
             if (iff->body_size > INT_MAX)
                 return AVERROR_INVALIDDATA;



More information about the ffmpeg-cvslog mailing list