[FFmpeg-devel] [PATCH v2 2/2] avformat: add SMPTE 337M demuxer

foo86 foobaz86 at gmail.com
Sat Jul 1 17:06:27 EEST 2017


---
 Changelog                |   1 +
 doc/general.texi         |   1 +
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/s337m.c      | 206 +++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/version.h    |   2 +-
 6 files changed, 211 insertions(+), 1 deletion(-)
 create mode 100644 libavformat/s337m.c

diff --git a/Changelog b/Changelog
index a8726c6736..a909031849 100644
--- a/Changelog
+++ b/Changelog
@@ -26,6 +26,7 @@ version <next>:
   --x86asmexe=yasm to configure to restore the old behavior.
 - additional frame format support for Interplay MVE movies
 - support for decoding through D3D11VA in ffmpeg
+- Dolby E decoder and SMPTE 337M demuxer
 
 version 3.3:
 - CrystalHD decoder moved to new decode API
diff --git a/doc/general.texi b/doc/general.texi
index d95ef31fde..036c8c25d4 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -520,6 +520,7 @@ library:
     @tab Multimedia format used by many games.
 @item SMJPEG                    @tab X @tab X
     @tab Used in certain Loki game ports.
+ at item SMPTE 337M encapsulation  @tab   @tab X
 @item Smush                     @tab   @tab X
     @tab Multimedia format used in some LucasArts games.
 @item Sony OpenMG (OMA)         @tab X @tab X
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 80aeed22c0..b0ef82cdd4 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -432,6 +432,7 @@ OBJS-$(CONFIG_RTSP_DEMUXER)              += rtsp.o rtspdec.o httpauth.o \
                                             urldecode.o
 OBJS-$(CONFIG_RTSP_MUXER)                += rtsp.o rtspenc.o httpauth.o \
                                             urldecode.o
+OBJS-$(CONFIG_S337M_DEMUXER)             += s337m.o spdif.o
 OBJS-$(CONFIG_SAMI_DEMUXER)              += samidec.o subtitles.o
 OBJS-$(CONFIG_SAP_DEMUXER)               += sapdec.o
 OBJS-$(CONFIG_SAP_MUXER)                 += sapenc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index a0e2fb8c85..1ebc14231c 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -267,6 +267,7 @@ static void register_all(void)
     REGISTER_MUXDEMUX(RTP,              rtp);
     REGISTER_MUXER   (RTP_MPEGTS,       rtp_mpegts);
     REGISTER_MUXDEMUX(RTSP,             rtsp);
+    REGISTER_DEMUXER (S337M,            s337m);
     REGISTER_DEMUXER (SAMI,             sami);
     REGISTER_MUXDEMUX(SAP,              sap);
     REGISTER_DEMUXER (SBG,              sbg);
diff --git a/libavformat/s337m.c b/libavformat/s337m.c
new file mode 100644
index 0000000000..1f4ba5edaf
--- /dev/null
+++ b/libavformat/s337m.c
@@ -0,0 +1,206 @@
+/*
+ * Copyright (C) 2017 foo86
+ *
+ * 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 "spdif.h"
+
+#define MARKER_16LE         0x72F81F4E
+#define MARKER_20LE         0x20876FF0E154
+#define MARKER_24LE         0x72F8961F4EA5
+
+#define IS_16LE_MARKER(state)   ((state & 0xFFFFFFFF) == MARKER_16LE)
+#define IS_20LE_MARKER(state)   ((state & 0xF0FFFFF0FFFF) == MARKER_20LE)
+#define IS_24LE_MARKER(state)   ((state & 0xFFFFFFFFFFFF) == MARKER_24LE)
+#define IS_LE_MARKER(state)     (IS_16LE_MARKER(state) || IS_20LE_MARKER(state) || IS_24LE_MARKER(state))
+
+static int s337m_get_offset_and_codec(AVFormatContext *s,
+                                      uint64_t state,
+                                      int data_type, int data_size,
+                                      int *offset, enum AVCodecID *codec)
+{
+    int word_bits;
+
+    if (IS_16LE_MARKER(state)) {
+        word_bits = 16;
+    } else if (IS_20LE_MARKER(state)) {
+        data_type >>= 8;
+        data_size >>= 4;
+        word_bits = 20;
+    } else {
+        data_type >>= 8;
+        word_bits = 24;
+    }
+
+    if ((data_type & 0x1F) != 0x1C) {
+        if (s)
+            avpriv_report_missing_feature(s, "Data type %#x in SMPTE 337M", data_type & 0x1F);
+        return AVERROR_PATCHWELCOME;
+    }
+
+    if (codec)
+        *codec = AV_CODEC_ID_DOLBY_E;
+
+    switch (data_size / word_bits) {
+    case 3648:
+        *offset = 1920;
+        break;
+    case 3644:
+        *offset = 2002;
+        break;
+    case 3640:
+        *offset = 2000;
+        break;
+    case 3040:
+        *offset = 1601;
+        break;
+    default:
+        if (s)
+            avpriv_report_missing_feature(s, "Dolby E data size %d in SMPTE 337M", data_size);
+        return AVERROR_PATCHWELCOME;
+    }
+
+    *offset -= 4;
+    *offset *= (word_bits + 7 >> 3) * 2;
+    return 0;
+}
+
+static int s337m_probe(AVProbeData *p)
+{
+    uint64_t state = 0;
+    int markers[3] = { 0 };
+    int i, sum, max, data_type, data_size, offset;
+    uint8_t *buf;
+
+    for (buf = p->buf; buf < p->buf + p->buf_size; buf++) {
+        state = (state << 8) | *buf;
+        if (!IS_LE_MARKER(state))
+            continue;
+
+        if (IS_16LE_MARKER(state)) {
+            data_type = AV_RL16(buf + 1);
+            data_size = AV_RL16(buf + 3);
+            buf += 4;
+        } else {
+            data_type = AV_RL24(buf + 1);
+            data_size = AV_RL24(buf + 4);
+            buf += 6;
+        }
+
+        if (s337m_get_offset_and_codec(NULL, state, data_type, data_size, &offset, NULL))
+            continue;
+
+        i = IS_16LE_MARKER(state) ? 0 : IS_20LE_MARKER(state) ? 1 : 2;
+        markers[i]++;
+
+        buf  += offset;
+        state = 0;
+    }
+
+    sum = max = 0;
+    for (i = 0; i < FF_ARRAY_ELEMS(markers); i++) {
+        sum += markers[i];
+        if (markers[max] < markers[i])
+            max = i;
+    }
+
+    if (markers[max] > 3 && markers[max] * 4 > sum * 3)
+        return AVPROBE_SCORE_EXTENSION + 1;
+
+    return 0;
+}
+
+static int s337m_read_header(AVFormatContext *s)
+{
+    s->ctx_flags |= AVFMTCTX_NOHEADER;
+    return 0;
+}
+
+static void bswap_buf24(uint8_t *data, int size)
+{
+    int i;
+
+    for (i = 0; i < size / 3; i++, data += 3)
+        FFSWAP(uint8_t, data[0], data[2]);
+}
+
+static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    AVIOContext *pb = s->pb;
+    uint64_t state = 0;
+    int ret, data_type, data_size, offset;
+    enum AVCodecID codec;
+    int64_t pos;
+
+    while (!IS_LE_MARKER(state)) {
+        state = (state << 8) | avio_r8(pb);
+        if (avio_feof(pb))
+            return AVERROR_EOF;
+    }
+
+    if (IS_16LE_MARKER(state)) {
+        data_type = avio_rl16(pb);
+        data_size = avio_rl16(pb);
+    } else {
+        data_type = avio_rl24(pb);
+        data_size = avio_rl24(pb);
+    }
+
+    pos = avio_tell(pb);
+
+    if ((ret = s337m_get_offset_and_codec(s, state, data_type, data_size, &offset, &codec)) < 0)
+        return ret;
+
+    if ((ret = av_new_packet(pkt, offset)) < 0)
+        return ret;
+
+    pkt->pos = pos;
+
+    if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
+        av_packet_unref(pkt);
+        return AVERROR_EOF;
+    }
+
+    if (IS_16LE_MARKER(state))
+        ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
+    else
+        bswap_buf24(pkt->data, pkt->size);
+
+    if (!s->nb_streams) {
+        AVStream *st = avformat_new_stream(s, NULL);
+        if (!st) {
+            av_packet_unref(pkt);
+            return AVERROR(ENOMEM);
+        }
+        st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
+        st->codecpar->codec_id   = codec;
+    }
+
+    return 0;
+}
+
+AVInputFormat ff_s337m_demuxer = {
+    .name           = "s337m",
+    .long_name      = NULL_IF_CONFIG_SMALL("SMPTE 337M"),
+    .read_probe     = s337m_probe,
+    .read_header    = s337m_read_header,
+    .read_packet    = s337m_read_packet,
+    .flags          = AVFMT_GENERIC_INDEX,
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 44c16ac75a..48b81f2e48 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  57
-#define LIBAVFORMAT_VERSION_MINOR  75
+#define LIBAVFORMAT_VERSION_MINOR  76
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.11.0



More information about the ffmpeg-devel mailing list