diff mbox

[FFmpeg-devel,1/3] avformat/s337m: Make available as subdemuxer

Message ID 20190726164517.8428-2-nicolas.gaullier@arkena.com
State New
Headers show

Commit Message

Gaullier Nicolas July 26, 2019, 4:45 p.m. UTC
---
 libavformat/s337m.c | 57 +++++++++++++++++++++++++++++++++++++++++++----------
 libavformat/s337m.h | 48 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+), 10 deletions(-)
 create mode 100644 libavformat/s337m.h

Comments

Michael Niedermayer July 26, 2019, 10:55 p.m. UTC | #1
On Fri, Jul 26, 2019 at 06:45:15PM +0200, Nicolas Gaullier wrote:
> ---
>  libavformat/s337m.c | 57 +++++++++++++++++++++++++++++++++++++++++++----------
>  libavformat/s337m.h | 48 ++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 95 insertions(+), 10 deletions(-)
>  create mode 100644 libavformat/s337m.h
> 
> diff --git a/libavformat/s337m.c b/libavformat/s337m.c
> index 48ab66a6da..589fec53a9 100644
> --- a/libavformat/s337m.c
> +++ b/libavformat/s337m.c
> @@ -21,15 +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))
> +#include "s337m.h"
>  
>  static int s337m_get_offset_and_codec(AVFormatContext *s,
>                                        uint64_t state,
> @@ -141,7 +133,7 @@ static void bswap_buf24(uint8_t *data, int size)
>          FFSWAP(uint8_t, data[0], data[2]);
>  }
>  
> -static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
> +int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)

this would need a ff or avpriv prefix 

thx

[...]
diff mbox

Patch

diff --git a/libavformat/s337m.c b/libavformat/s337m.c
index 48ab66a6da..589fec53a9 100644
--- a/libavformat/s337m.c
+++ b/libavformat/s337m.c
@@ -21,15 +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))
+#include "s337m.h"
 
 static int s337m_get_offset_and_codec(AVFormatContext *s,
                                       uint64_t state,
@@ -141,7 +133,7 @@  static void bswap_buf24(uint8_t *data, int size)
         FFSWAP(uint8_t, data[0], data[2]);
 }
 
-static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
+int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     AVIOContext *pb = s->pb;
     uint64_t state = 0;
@@ -204,3 +196,48 @@  AVInputFormat ff_s337m_demuxer = {
     .read_packet    = s337m_read_packet,
     .flags          = AVFMT_GENERIC_INDEX,
 };
+
+int s337m_probe_stream(AVFormatContext *s, AVStream **st)
+{
+    if (((*st)->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE || (*st)->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) &&
+               (*st)->codecpar->channels == 2)
+    {
+        int64_t avio_current_offset;
+        int bufsize, pos = 0;
+        double s337m_phase;
+        uint8_t *buf = av_mallocz(S337M_MAX_OFFSET);
+        if (!buf)
+            return AVERROR(ENOMEM);
+        avio_current_offset = avio_tell(s->pb);
+        bufsize = avio_read(s->pb, buf, S337M_MAX_OFFSET);
+        avio_seek(s->pb, avio_current_offset, SEEK_SET);
+        while (pos < bufsize - 5 && buf[pos] == 0)
+            pos++;
+        s337m_phase = (double)pos * 4 / (*st)->codecpar->bits_per_coded_sample / (*st)->codecpar->sample_rate;
+        if (s337m_phase >= S337M_PHASE_PROBE_MIN && pos < bufsize - 5) {
+            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);
+                }
+            }
+            av_freep(&buf);
+            if (data_type >= 0 && !s337m_get_offset_and_codec(s, state, data_type, data_size, &offset, &(*st)->codecpar->codec_id))
+            {
+                av_log(s, 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(s, AV_LOG_WARNING, "Dolby E phase is out of valid range (%.6fs-%.6fs)\n", DOLBY_E_PHASE_MIN, DOLBY_E_PHASE_MAX);
+            }
+        }
+    }
+    return 0;
+}
diff --git a/libavformat/s337m.h b/libavformat/s337m.h
new file mode 100644
index 0000000000..900b966233
--- /dev/null
+++ b/libavformat/s337m.h
@@ -0,0 +1,48 @@ 
+/*
+ * 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_MAX_OFFSET 2002*6
+
+#define S337M_PHASE_PROBE_MIN   0.000000
+#define DOLBY_E_PHASE_MIN       0.000610
+#define DOLBY_E_PHASE_MAX       0.001050
+
+/**
+ * Detect s337m synch words in a PCM_S16LE/S24LE stereo stream
+ * On success, codec is updated
+ * Requires first sample to have enough and clean guard band (S337M_PHASE_PROBE_MIN, set to zero)
+ */
+int s337m_probe_stream(AVFormatContext *s, AVStream **st);
+
+int s337m_read_packet(AVFormatContext *s, AVPacket *pkt);
+
+#endif /* AVFORMAT_S337M_H */