[FFmpeg-devel] [PATCH] avformat: add MIDI Sample Dump Standard demuxer

Paul B Mahol onemda at gmail.com
Fri Jan 20 22:27:46 EET 2017


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/sdsdec.c     | 165 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 167 insertions(+)
 create mode 100644 libavformat/sdsdec.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 872b30e..fbb28e3 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -435,6 +435,7 @@ OBJS-$(CONFIG_SAP_DEMUXER)               += sapdec.o
 OBJS-$(CONFIG_SAP_MUXER)                 += sapenc.o
 OBJS-$(CONFIG_SBG_DEMUXER)               += sbgdec.o
 OBJS-$(CONFIG_SDP_DEMUXER)               += rtsp.o
+OBJS-$(CONFIG_SDS_DEMUXER)               += sdsdec.o
 OBJS-$(CONFIG_SDR2_DEMUXER)              += sdr2.o
 OBJS-$(CONFIG_SEGAFILM_DEMUXER)          += segafilm.o
 OBJS-$(CONFIG_SEGMENT_MUXER)             += segment.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 6a79b75..8787c79 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -274,6 +274,7 @@ void av_register_all(void)
     REGISTER_MUXDEMUX(SAP,              sap);
     REGISTER_DEMUXER (SBG,              sbg);
     REGISTER_DEMUXER (SDP,              sdp);
+    REGISTER_DEMUXER (SDS,              sds);
     REGISTER_DEMUXER (SDR2,             sdr2);
 #if CONFIG_RTPDEC
     ff_register_rtp_dynamic_payload_handlers();
diff --git a/libavformat/sdsdec.c b/libavformat/sdsdec.c
new file mode 100644
index 0000000..727586a
--- /dev/null
+++ b/libavformat/sdsdec.c
@@ -0,0 +1,165 @@
+/*
+ * MIDI Sample Dump Standard format demuxer
+ * Copyright (c) 2017 Paul B Mahol
+ *
+ * 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 "libavutil/intreadwrite.h"
+#include "avformat.h"
+#include "internal.h"
+
+typedef struct SDSContext {
+    uint8_t data[120];
+    int bit_depth;
+    int size;
+    void (*read_block)(const uint8_t *src, int32_t *dst);
+} SDSContext;
+
+static int sds_probe(AVProbeData *p)
+{
+    if (AV_RB32(p->buf) == 0xF07E0001 && p->buf[20] == 0xF7 &&
+        p->buf[6] >= 8 && p->buf[6] <= 28)
+        return AVPROBE_SCORE_EXTENSION;
+    return 0;
+}
+
+static void byte2_read(const uint8_t *src, int32_t *dst)
+{
+    int i;
+
+    for (i = 0; i < 120; i += 2) {
+        unsigned sample = (src[i + 0] << 25) + (src[i + 1] << 18);
+
+        dst[i / 2] = sample;
+    }
+}
+
+static void byte3_read(const uint8_t *src, int32_t *dst)
+{
+    int i;
+
+    for (i = 0; i < 120; i += 3) {
+        unsigned sample;
+
+	sample = (src[i + 0] << 25) | (src[i + 1] << 18) | (src[i + 2] << 11);
+        dst[i / 3] = sample;
+    }
+}
+
+static void byte4_read(const uint8_t *src, int32_t *dst)
+{
+    int i;
+
+    for (i = 0; i < 120; i += 4) {
+        unsigned sample;
+
+	sample = (src[i + 0] << 25) | (src[i + 1] << 18) | (src[i + 2] << 11) | (src[i + 3] << 4);
+        dst[i / 4] = sample;
+    }
+}
+
+#define SDS_3BYTE_TO_INT_DECODE(x) (((x) & 0x7F) | (((x) & 0x7F00) >> 1) | (((x) & 0x7F0000) >> 2))
+
+static int sds_read_header(AVFormatContext *ctx)
+{
+    SDSContext *s = ctx->priv_data;
+    unsigned sample_period;
+    AVIOContext *pb = ctx->pb;
+    AVStream *st;
+
+    st = avformat_new_stream(ctx, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+
+    avio_skip(pb, 4);
+    avio_skip(pb, 2);
+
+    s->bit_depth = avio_r8(pb);
+    if (s->bit_depth < 8 || s->bit_depth > 28)
+        return AVERROR_INVALIDDATA;
+
+    if (s->bit_depth < 14) {
+        s->read_block = byte2_read;
+        s->size = 60 * 4;
+    } else if (s->bit_depth < 21) {
+        s->read_block = byte3_read;
+        s->size = 40 * 4;
+    } else {
+        s->read_block = byte4_read;
+        s->size = 30 * 4;
+    }
+    st->codecpar->codec_id = AV_CODEC_ID_PCM_U32LE;
+
+    sample_period = avio_rl24(pb);
+    sample_period = SDS_3BYTE_TO_INT_DECODE(sample_period);
+    avio_skip(pb, 11);
+
+    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
+    st->codecpar->channels = 1;
+    st->codecpar->sample_rate = sample_period ? 1000000000 / sample_period : 16000;
+    st->duration = (avio_size(pb) - 21) / (127) * s->size / 4;
+
+    avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
+
+    return 0;
+}
+
+static int sds_read_packet(AVFormatContext *ctx, AVPacket *pkt)
+{
+    SDSContext *s = ctx->priv_data;
+    AVIOContext *pb = ctx->pb;
+    int64_t pos;
+    int ret;
+
+    if (avio_feof(pb))
+        return AVERROR_EOF;
+
+    pos = avio_tell(pb);
+    if (avio_rb16(pb) != 0xF07E)
+        return AVERROR_INVALIDDATA;
+    avio_skip(pb, 3);
+
+    avio_read(pb, s->data, 120);
+
+    ret = av_new_packet(pkt, s->size);
+    if (ret < 0)
+        return ret;
+
+    s->read_block(s->data, (int32_t *)pkt->data);
+
+    avio_skip(pb, 1); // checksum
+    if (avio_r8(pb) != 0xF7)
+        return AVERROR_INVALIDDATA;
+
+    pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
+    pkt->stream_index = 0;
+    pkt->pos = pos;
+
+    return ret;
+}
+
+AVInputFormat ff_sds_demuxer = {
+    .name           = "sds",
+    .long_name      = NULL_IF_CONFIG_SMALL("MIDI Sample Dump Standard"),
+    .priv_data_size = sizeof(SDSContext),
+    .read_probe     = sds_probe,
+    .read_header    = sds_read_header,
+    .read_packet    = sds_read_packet,
+    .extensions     = "sds",
+    .flags          = AVFMT_GENERIC_INDEX,
+};
-- 
2.9.3



More information about the ffmpeg-devel mailing list