[FFmpeg-devel] [PATCH v3 3/6] avformat/s337m: Make available as subdemuxer

Nicolas Gaullier nicolas.gaullier at arkena.com
Tue Aug 6 18:08:12 EEST 2019


---
 libavformat/s337m.c | 64 ++++++++++++++++++++++++++++++++++++++++++++---------
 libavformat/s337m.h | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+), 11 deletions(-)
 create mode 100644 libavformat/s337m.h

diff --git a/libavformat/s337m.c b/libavformat/s337m.c
index 22140297e6..3ec78d36e4 100644
--- a/libavformat/s337m.c
+++ b/libavformat/s337m.c
@@ -21,17 +21,7 @@
 #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))
-
-int avpriv_s337m_get_packet(void *avc, AVIOContext *pb, AVPacket *pkt, int size, enum AVCodecID *codec);
+#include "s337m.h"
 
 static int s337m_get_offset_and_codec(void *avc,
                                       uint64_t state,
@@ -129,6 +119,58 @@ static int s337m_probe(const AVProbeData *p)
     return 0;
 }
 
+int avpriv_s337m_probe_stream(void *avc, AVIOContext *pb, AVStream **st, int size)
+{
+    if ( size >= S337M_MIN_OFFSET &&
+        ((*st)->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE || (*st)->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) &&
+               (*st)->codecpar->channels == 2) {
+        uint8_t *buf;
+        int64_t data_offset;
+        int pos = 0;
+
+        size = FFMIN(size, S337M_MAX_OFFSET);
+        if (!(buf = av_mallocz(size))) {
+            return AVERROR(ENOMEM);
+        }
+        data_offset = avio_tell(pb);
+        if (avio_read(pb, buf, size) != size) {
+            av_freep(&buf);
+            return AVERROR(EIO);
+        }
+        avio_seek(pb, data_offset, SEEK_SET);
+
+        while (pos < size - 9 && !buf[pos]) {
+            pos++;
+        }
+        if (pos < size - 9 && pos >= S337M_PROBE_GUARDBAND_MIN_BYTES) {
+            uint64_t state;
+            int data_type = -1, data_size, offset;
+
+            if ((*st)->codecpar->bits_per_coded_sample == 16) {
+                state = AV_RB32(buf + pos);
+                if (IS_16LE_MARKER(state)) {
+                    data_type = AV_RL16(buf + pos + 4);
+                    data_size = AV_RL16(buf + pos + 6);
+                }
+            } else if ((*st)->codecpar->bits_per_coded_sample == 24) {
+                state = AV_RB48(buf + pos);
+                if (IS_20LE_MARKER(state) || IS_24LE_MARKER(state)) {
+                    data_type = AV_RL24(buf + pos + 6);
+                    data_size = AV_RL24(buf + pos + 9);
+                }
+            }
+            if (data_type >= 0 && !s337m_get_offset_and_codec(avc, state, data_type, data_size, &offset, &(*st)->codecpar->codec_id)) {
+                double s337m_phase = pos * 4 / (*st)->codecpar->bits_per_coded_sample / (*st)->codecpar->sample_rate;
+                av_log(avc, AV_LOG_INFO, "s337m detected with phase = %.6fs\n", s337m_phase);
+                if ((*st)->codecpar->codec_id == AV_CODEC_ID_DOLBY_E && (s337m_phase < DOLBY_E_PHASE_MIN || s337m_phase > DOLBY_E_PHASE_MAX))
+                        av_log(avc, AV_LOG_WARNING, "Dolby E phase is out of valid range (%.6fs-%.6fs)\n", DOLBY_E_PHASE_MIN, DOLBY_E_PHASE_MAX);
+            }
+        }
+        av_freep(&buf);
+    }
+    return 0;
+}
+
 static int s337m_read_header(AVFormatContext *s)
 {
     s->ctx_flags |= AVFMTCTX_NOHEADER;
diff --git a/libavformat/s337m.h b/libavformat/s337m.h
new file mode 100644
index 0000000000..0f21a23a30
--- /dev/null
+++ b/libavformat/s337m.h
@@ -0,0 +1,64 @@
+/*
+ * 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
+ */
+
+#ifndef AVFORMAT_S337M_H
+#define AVFORMAT_S337M_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))
+
+#define S337M_MIN_OFFSET 1601*4
+#define S337M_MAX_OFFSET 2002*6
+
+#define S337M_PROBE_GUARDBAND_MIN_BYTES     0
+#define DOLBY_E_PHASE_MIN       0.000610
+#define DOLBY_E_PHASE_MAX       0.001050
+
+/**
+ * Detect s337m packets in a PCM_S16LE/S24LE stereo stream
+ * If found, the codec of the stream is updated
+ * Requires a single sample with enough (S337M_PROBE_GUARDBAND_MIN_BYTES) and clean (set to zero) guard band
+ * @param avc For av_log
+ * @param pb Associated IO context
+ * @param st Streams
+ * @param size Maximum IO read size available for probing at current position
+ * @return = 0 if no error encountered (even if no s337m was found)
+ */
+int avpriv_s337m_probe_stream(void *avc, AVIOContext *pb, AVStream **st, int size);
+
+/**
+ * Read s337m packets in a PCM_S16LE/S24LE stereo stream
+ * Returns the first inner packet found
+ * @param avc For av_log
+ * @param pb Associated IO context
+ * @param pkt On success, returns a DOLBY E packet
+ * @param size Maximum IO read size available for reading at current position
+ * @param codec Returns AV_CODEC_ID_DOLBY_E
+ * @return = 0 on success (an error is raised if no s337m was found)
+ */
+int avpriv_s337m_get_packet(void *avc, AVIOContext *pb, AVPacket *pkt, int size, enum AVCodecID *codec);
+
+#endif /* AVFORMAT_S337M_H */
-- 
2.14.1.windows.1



More information about the ffmpeg-devel mailing list