Browse code

Direct Stream Digital (DSD) decoder

Signed-off-by: Peter Ross <pross@xvid.org>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Peter Ross authored on 2014/04/14 17:22:32
Showing 10 changed files
... ...
@@ -17,6 +17,7 @@ version <next>:
17 17
 - GDI screen grabbing for Windows
18 18
 - alternative rendition support for HTTP Live Streaming
19 19
 - AVFoundation input device
20
+- Direct Stream Digital (DSD) decoder
20 21
 
21 22
 
22 23
 version 2.2:
... ...
@@ -898,6 +898,10 @@ following image formats are supported:
898 898
 @item DPCM Sol               @tab     @tab  X
899 899
 @item DPCM Xan               @tab     @tab  X
900 900
     @tab Used in Origin's Wing Commander IV AVI files.
901
+@item DSD (Direct Stream Digitial), least significant bit first  @tab  @tab  X
902
+@item DSD (Direct Stream Digitial), most significant bit first   @tab  @tab  X
903
+@item DSD (Direct Stream Digitial), least significant bit first, planar  @tab  @tab  X
904
+@item DSD (Direct Stream Digitial), most significant bit first, planar   @tab  @tab  X
901 905
 @item DSP Group TrueSpeech   @tab     @tab  X
902 906
 @item DV audio               @tab     @tab  X
903 907
 @item Enhanced AC-3          @tab  X  @tab  X
... ...
@@ -179,6 +179,8 @@ OBJS-$(CONFIG_DNXHD_DECODER)           += dnxhddec.o dnxhddata.o
179 179
 OBJS-$(CONFIG_DNXHD_ENCODER)           += dnxhdenc.o dnxhddata.o
180 180
 OBJS-$(CONFIG_DPX_DECODER)             += dpx.o
181 181
 OBJS-$(CONFIG_DPX_ENCODER)             += dpxenc.o
182
+OBJS-$(CONFIG_DSD_LSBF_DECODER)        += dsddec.o
183
+OBJS-$(CONFIG_DSD_MSBF_DECODER)        += dsddec.o
182 184
 OBJS-$(CONFIG_DSICINAUDIO_DECODER)     += dsicinav.o
183 185
 OBJS-$(CONFIG_DSICINVIDEO_DECODER)     += dsicinav.o
184 186
 OBJS-$(CONFIG_DVBSUB_DECODER)          += dvbsubdec.o
... ...
@@ -847,6 +849,7 @@ HOSTPROGS = aac_tablegen                                                \
847 847
             aacps_tablegen                                              \
848 848
             cbrt_tablegen                                               \
849 849
             cos_tablegen                                                \
850
+            dsd_tablegen                                                \
850 851
             dv_tablegen                                                 \
851 852
             motionpixels_tablegen                                       \
852 853
             mpegaudio_tablegen                                          \
... ...
@@ -871,7 +874,7 @@ else
871 871
 $(SUBDIR)%_tablegen$(HOSTEXESUF): HOSTCFLAGS += -DCONFIG_SMALL=0
872 872
 endif
873 873
 
874
-GEN_HEADERS = cbrt_tables.h aacps_tables.h aac_tables.h dv_tables.h     \
874
+GEN_HEADERS = cbrt_tables.h aacps_tables.h aac_tables.h dsd_tables.h dv_tables.h     \
875 875
               sinewin_tables.h mpegaudio_tables.h motionpixels_tables.h \
876 876
               pcm_tables.h qdm2_tables.h
877 877
 GEN_HEADERS := $(addprefix $(SUBDIR), $(GEN_HEADERS))
... ...
@@ -883,6 +886,7 @@ ifdef CONFIG_HARDCODED_TABLES
883 883
 $(SUBDIR)aacdec.o: $(SUBDIR)cbrt_tables.h
884 884
 $(SUBDIR)aacps.o: $(SUBDIR)aacps_tables.h
885 885
 $(SUBDIR)aactab.o: $(SUBDIR)aac_tables.h
886
+$(SUBDIR)dsddec.o: $(SUBDIR)dsd_tables.h
886 887
 $(SUBDIR)dvenc.o: $(SUBDIR)dv_tables.h
887 888
 $(SUBDIR)sinewin.o: $(SUBDIR)sinewin_tables.h
888 889
 $(SUBDIR)mpegaudiodec_fixed.o: $(SUBDIR)mpegaudio_tables.h
... ...
@@ -337,6 +337,10 @@ void avcodec_register_all(void)
337 337
     REGISTER_DECODER(BMV_AUDIO,         bmv_audio);
338 338
     REGISTER_DECODER(COOK,              cook);
339 339
     REGISTER_ENCDEC (DCA,               dca);
340
+    REGISTER_DECODER(DSD_LSBF,          dsd_lsbf);
341
+    REGISTER_DECODER(DSD_MSBF,          dsd_msbf);
342
+    REGISTER_DECODER(DSD_LSBF_PLANAR,   dsd_lsbf_planar);
343
+    REGISTER_DECODER(DSD_MSBF_PLANAR,   dsd_msbf_planar);
340 344
     REGISTER_DECODER(DSICINAUDIO,       dsicinaudio);
341 345
     REGISTER_ENCDEC (EAC3,              eac3);
342 346
     REGISTER_DECODER(EVRC,              evrc);
... ...
@@ -489,6 +489,10 @@ enum AVCodecID {
489 489
     AV_CODEC_ID_TAK         = MKBETAG('t','B','a','K'),
490 490
     AV_CODEC_ID_EVRC        = MKBETAG('s','e','v','c'),
491 491
     AV_CODEC_ID_SMV         = MKBETAG('s','s','m','v'),
492
+    AV_CODEC_ID_DSD_LSBF    = MKBETAG('D','S','D','L'),
493
+    AV_CODEC_ID_DSD_MSBF    = MKBETAG('D','S','D','M'),
494
+    AV_CODEC_ID_DSD_LSBF_PLANAR = MKBETAG('D','S','D','1'),
495
+    AV_CODEC_ID_DSD_MSBF_PLANAR = MKBETAG('D','S','D','8'),
492 496
 
493 497
     /* subtitle codecs */
494 498
     AV_CODEC_ID_FIRST_SUBTITLE = 0x17000,          ///< A dummy ID pointing at the start of subtitle codecs.
... ...
@@ -2460,6 +2460,34 @@ static const AVCodecDescriptor codec_descriptors[] = {
2460 2460
         .long_name = NULL_IF_CONFIG_SMALL("SMV (Selectable Mode Vocoder)"),
2461 2461
         .props     = AV_CODEC_PROP_LOSSY,
2462 2462
     },
2463
+    {
2464
+        .id        = AV_CODEC_ID_DSD_LSBF,
2465
+        .type      = AVMEDIA_TYPE_AUDIO,
2466
+        .name      = "dsd_lsbf",
2467
+        .long_name = NULL_IF_CONFIG_SMALL("DSD (Direct Stream Digital), least significant bit first"),
2468
+        .props     = AV_CODEC_PROP_LOSSY,
2469
+    },
2470
+    {
2471
+        .id        = AV_CODEC_ID_DSD_MSBF,
2472
+        .type      = AVMEDIA_TYPE_AUDIO,
2473
+        .name      = "dsd_msbf",
2474
+        .long_name = NULL_IF_CONFIG_SMALL("DSD (Direct Stream Digital), most significant bit first"),
2475
+        .props     = AV_CODEC_PROP_LOSSY,
2476
+    },
2477
+    {
2478
+        .id        = AV_CODEC_ID_DSD_LSBF_PLANAR,
2479
+        .type      = AVMEDIA_TYPE_AUDIO,
2480
+        .name      = "dsd_lsbf_planar",
2481
+        .long_name = NULL_IF_CONFIG_SMALL("DSD (Direct Stream Digital), least significant bit first, planar"),
2482
+        .props     = AV_CODEC_PROP_LOSSY,
2483
+    },
2484
+    {
2485
+        .id        = AV_CODEC_ID_DSD_MSBF_PLANAR,
2486
+        .type      = AVMEDIA_TYPE_AUDIO,
2487
+        .name      = "dsd_msbf_planar",
2488
+        .long_name = NULL_IF_CONFIG_SMALL("DSD (Direct Stream Digital), most significant bit first, planar"),
2489
+        .props     = AV_CODEC_PROP_LOSSY,
2490
+    },
2463 2491
 
2464 2492
     /* subtitle codecs */
2465 2493
     {
2466 2494
new file mode 100644
... ...
@@ -0,0 +1,38 @@
0
+/*
1
+ * Generate a header file for hardcoded DSD tables
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include <stdlib.h>
21
+#define CONFIG_HARDCODED_TABLES 0
22
+#include "dsd_tablegen.h"
23
+#include "tableprint.h"
24
+#include <inttypes.h>
25
+
26
+int main(void)
27
+{
28
+    dsd_ctables_tableinit();
29
+
30
+    write_fileheader();
31
+
32
+    printf("static const double ctables[CTABLES][256] = {\n");
33
+    write_float_2d_array(ctables, CTABLES, 256);
34
+    printf("};\n");
35
+
36
+    return 0;
37
+}
0 38
new file mode 100644
... ...
@@ -0,0 +1,95 @@
0
+/*
1
+ * Header file for hardcoded DSD tables
2
+ * based on BSD licensed dsd2pcm by Sebastian Gesemann
3
+ * Copyright (c) 2009, 2011 Sebastian Gesemann. All rights reserved.
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#ifndef AVCODEC_DSD_TABLEGEN_H
23
+#define AVCODEC_DSD_TABLEGEN_H
24
+
25
+#include <stdint.h>
26
+#include "libavutil/attributes.h"
27
+
28
+#define HTAPS   48                /** number of FIR constants */
29
+#define CTABLES ((HTAPS + 7) / 8) /** number of "8 MACs" lookup tables */
30
+
31
+#if CONFIG_HARDCODED_TABLES
32
+#define dsd_ctables_tableinit()
33
+#include "libavcodec/dsd_tables.h"
34
+#else
35
+#include "libavutil/common.h"
36
+
37
+/*
38
+ * Properties of this 96-tap lowpass filter when applied on a signal
39
+ * with sampling rate of 44100*64 Hz:
40
+ *
41
+ * () has a delay of 17 microseconds.
42
+ *
43
+ * () flat response up to 48 kHz
44
+ *
45
+ * () if you downsample afterwards by a factor of 8, the
46
+ *    spectrum below 70 kHz is practically alias-free.
47
+ *
48
+ * () stopband rejection is about 160 dB
49
+ *
50
+ * The coefficient tables ("ctables") take only 6 Kibi Bytes and
51
+ * should fit into a modern processor's fast cache.
52
+ */
53
+
54
+/**
55
+ * The 2nd half (48 coeffs) of a 96-tap symmetric lowpass filter
56
+ */
57
+static const double htaps[HTAPS] = {
58
+     0.09950731974056658,    0.09562845727714668,    0.08819647126516944,
59
+     0.07782552527068175,    0.06534876523171299,    0.05172629311427257,
60
+     0.0379429484910187,     0.02490921351762261,    0.0133774746265897,
61
+     0.003883043418804416,  -0.003284703416210726,  -0.008080250212687497,
62
+    -0.01067241812471033,   -0.01139427235000863,   -0.0106813877974587,
63
+    -0.009007905078766049,  -0.006828859761015335,  -0.004535184322001496,
64
+    -0.002425035959059578,  -0.0006922187080790708,  0.0005700762133516592,
65
+     0.001353838005269448,   0.001713709169690937,   0.001742046839472948,
66
+     0.001545601648013235,   0.001226696225277855,   0.0008704322683580222,
67
+     0.0005381636200535649,  0.000266446345425276,   7.002968738383528e-05,
68
+    -5.279407053811266e-05, -0.0001140625650874684, -0.0001304796361231895,
69
+    -0.0001189970287491285, -9.396247155265073e-05, -6.577634378272832e-05,
70
+    -4.07492895872535e-05,  -2.17407957554587e-05,  -9.163058931391722e-06,
71
+    -2.017460145032201e-06,  1.249721855219005e-06,  2.166655190537392e-06,
72
+     1.930520892991082e-06,  1.319400334374195e-06,  7.410039764949091e-07,
73
+     3.423230509967409e-07,  1.244182214744588e-07,  3.130441005359396e-08
74
+};
75
+
76
+static float ctables[CTABLES][256];
77
+
78
+static av_cold void dsd_ctables_tableinit(void)
79
+{
80
+    int t, e, m, k;
81
+    double acc;
82
+    for (t = 0; t < CTABLES; ++t) {
83
+        k = FFMIN(HTAPS - t * 8, 8);
84
+        for (e = 0; e < 256; ++e) {
85
+            acc = 0.0;
86
+            for (m = 0; m < k; ++m)
87
+                acc += (((e >> (7 - m)) & 1) * 2 - 1) * htaps[t * 8 + m];
88
+            ctables[CTABLES - 1 - t][e] = (float)acc;
89
+        }
90
+    }
91
+}
92
+#endif /* CONFIG_HARDCODED_TABLES */
93
+
94
+#endif /* AVCODEC_DSD_TABLEGEN_H */
0 95
new file mode 100644
... ...
@@ -0,0 +1,167 @@
0
+/*
1
+ * Direct Stream Digital (DSD) decoder
2
+ * based on BSD licensed dsd2pcm by Sebastian Gesemann
3
+ * Copyright (c) 2009, 2011 Sebastian Gesemann. All rights reserved.
4
+ * Copyright (c) 2014 Peter Ross
5
+ *
6
+ * This file is part of FFmpeg.
7
+ *
8
+ * FFmpeg is free software; you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation; either
11
+ * version 2.1 of the License, or (at your option) any later version.
12
+ *
13
+ * FFmpeg is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
+ * Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public
19
+ * License along with FFmpeg; if not, write to the Free Software
20
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
+ */
22
+
23
+/**
24
+ * @file
25
+ * Direct Stream Digital (DSD) decoder
26
+ */
27
+
28
+#include "libavcodec/internal.h"
29
+#include "libavcodec/mathops.h"
30
+#include "avcodec.h"
31
+#include "dsd_tablegen.h"
32
+
33
+#define FIFOSIZE 16              /** must be a power of two */
34
+#define FIFOMASK (FIFOSIZE - 1)  /** bit mask for FIFO offsets */
35
+
36
+#if FIFOSIZE * 8 < HTAPS * 2
37
+#error "FIFOSIZE too small"
38
+#endif
39
+
40
+/**
41
+ * Per-channel buffer
42
+ */
43
+typedef struct {
44
+    unsigned char buf[FIFOSIZE];
45
+    unsigned pos;
46
+} DSDContext;
47
+
48
+static void dsd2pcm_translate(DSDContext* s, size_t samples, int lsbf,
49
+                              const unsigned char *src, ptrdiff_t src_stride,
50
+                              float *dst, ptrdiff_t dst_stride)
51
+{
52
+    unsigned pos, i;
53
+    unsigned char* p;
54
+    double sum;
55
+
56
+    pos = s->pos;
57
+
58
+    while (samples-- > 0) {
59
+        s->buf[pos] = lsbf ? ff_reverse[*src] : *src;
60
+        src += src_stride;
61
+
62
+        p = s->buf + ((pos - CTABLES) & FIFOMASK);
63
+        *p = ff_reverse[*p];
64
+
65
+        sum = 0.0;
66
+        for (i = 0; i < CTABLES; i++) {
67
+            unsigned char a = s->buf[(pos                   - i) & FIFOMASK];
68
+            unsigned char b = s->buf[(pos - (CTABLES*2 - 1) + i) & FIFOMASK];
69
+            sum += ctables[i][a] + ctables[i][b];
70
+        }
71
+
72
+        *dst = (float)sum;
73
+        dst += dst_stride;
74
+
75
+        pos = (pos + 1) & FIFOMASK;
76
+    }
77
+
78
+    s->pos = pos;
79
+}
80
+
81
+static av_cold void init_static_data(void)
82
+{
83
+    static int done = 0;
84
+    if (done)
85
+        return;
86
+    dsd_ctables_tableinit();
87
+    done = 1;
88
+}
89
+
90
+static av_cold int decode_init(AVCodecContext *avctx)
91
+{
92
+    DSDContext * s;
93
+    int i;
94
+
95
+    init_static_data();
96
+
97
+    s = av_malloc(sizeof(DSDContext) * avctx->channels);
98
+    if (!s)
99
+        return AVERROR(ENOMEM);
100
+
101
+    for (i = 0; i < avctx->channels; i++) {
102
+        s[i].pos = 0;
103
+        memset(s[i].buf, 0x69, sizeof(s[i].buf));
104
+
105
+        /* 0x69 = 01101001
106
+         * This pattern "on repeat" makes a low energy 352.8 kHz tone
107
+         * and a high energy 1.0584 MHz tone which should be filtered
108
+         * out completely by any playback system --> silence
109
+         */
110
+    }
111
+
112
+    avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
113
+    avctx->priv_data  = s;
114
+    return 0;
115
+}
116
+
117
+static int decode_frame(AVCodecContext *avctx, void *data,
118
+                        int *got_frame_ptr, AVPacket *avpkt)
119
+{
120
+    DSDContext * s = avctx->priv_data;
121
+    AVFrame *frame = data;
122
+    int ret, i;
123
+    int lsbf = avctx->codec_id == AV_CODEC_ID_DSD_LSBF || avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR;
124
+    int src_next;
125
+    int src_stride;
126
+
127
+    frame->nb_samples = avpkt->size / avctx->channels;
128
+
129
+    if (avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR || avctx->codec_id == AV_CODEC_ID_DSD_MSBF_PLANAR) {
130
+        src_next   = frame->nb_samples;
131
+        src_stride = 1;
132
+    } else {
133
+        src_next   = 1;
134
+        src_stride = avctx->channels;
135
+    }
136
+
137
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
138
+        return ret;
139
+
140
+    for (i = 0; i < avctx->channels; i++) {
141
+        float * dst = ((float **)frame->extended_data)[i];
142
+        dsd2pcm_translate(&s[i], frame->nb_samples, lsbf,
143
+            avpkt->data + i * src_next, src_stride,
144
+            dst, 1);
145
+    }
146
+
147
+    *got_frame_ptr = 1;
148
+    return frame->nb_samples * avctx->channels;
149
+}
150
+
151
+#define DSD_DECODER(id_, name_, long_name_) \
152
+AVCodec ff_##name_##_decoder = { \
153
+    .name         = #name_, \
154
+    .long_name    = NULL_IF_CONFIG_SMALL(long_name_), \
155
+    .type         = AVMEDIA_TYPE_AUDIO, \
156
+    .id           = AV_CODEC_ID_##id_, \
157
+    .init         = decode_init, \
158
+    .decode       = decode_frame, \
159
+    .sample_fmts  = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP, \
160
+                                                   AV_SAMPLE_FMT_NONE }, \
161
+};
162
+
163
+DSD_DECODER(DSD_LSBF, dsd_lsbf, "DSD (Direct Stream Digital), least significant bit first")
164
+DSD_DECODER(DSD_MSBF, dsd_msbf, "DSD (Direct Stream Digital), most significant bit first")
165
+DSD_DECODER(DSD_MSBF_PLANAR, dsd_msbf_planar, "DSD (Direct Stream Digital), most significant bit first, planar")
166
+DSD_DECODER(DSD_LSBF_PLANAR, dsd_lsbf_planar, "DSD (Direct Stream Digital), least significant bit first, planar")
... ...
@@ -2994,6 +2994,10 @@ int av_get_exact_bits_per_sample(enum AVCodecID codec_id)
2994 2994
     case AV_CODEC_ID_ADPCM_G722:
2995 2995
     case AV_CODEC_ID_ADPCM_YAMAHA:
2996 2996
         return 4;
2997
+    case AV_CODEC_ID_DSD_LSBF:
2998
+    case AV_CODEC_ID_DSD_MSBF:
2999
+    case AV_CODEC_ID_DSD_LSBF_PLANAR:
3000
+    case AV_CODEC_ID_DSD_MSBF_PLANAR:
2997 3001
     case AV_CODEC_ID_PCM_ALAW:
2998 3002
     case AV_CODEC_ID_PCM_MULAW:
2999 3003
     case AV_CODEC_ID_PCM_S8: