diff mbox series

[FFmpeg-devel,v5,2/9] avformat/s337m: Split read_packet/get_packet

Message ID 20201019081929.1926-3-nicolas.gaullier@cji.paris
State New
Headers show
Series avformat: wav-s337m support + new probe_stream option
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished

Commit Message

Nicolas Gaullier Oct. 19, 2020, 8:19 a.m. UTC
Prepare use of s337m_get_packet from outside.
---
 libavformat/s337m.c | 26 ++++++++++++++++++++------
 libavformat/s337m.h | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 6 deletions(-)
 create mode 100644 libavformat/s337m.h
diff mbox series

Patch

diff --git a/libavformat/s337m.c b/libavformat/s337m.c
index 36e1047af8..7f6eecb25c 100644
--- a/libavformat/s337m.c
+++ b/libavformat/s337m.c
@@ -21,6 +21,7 @@ 
 #include "libavutil/intreadwrite.h"
 #include "avformat.h"
 #include "spdif.h"
+#include "s337m.h"
 
 #define MARKER_16LE         0x72F81F4E
 #define MARKER_20LE         0x20876FF0E154
@@ -141,18 +142,20 @@  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 ff_s337m_get_packet(AVIOContext *pb, AVPacket *pkt, int size, enum AVCodecID *codec, void *avc)
 {
-    AVIOContext *pb = s->pb;
     uint64_t state = 0;
     int ret, data_type, data_size, offset;
-    enum AVCodecID codec;
-    int64_t pos;
+    int64_t pos, orig_pos = avio_tell(pb);
 
     while (!IS_LE_MARKER(state)) {
         state = (state << 8) | avio_r8(pb);
         if (avio_feof(pb))
             return AVERROR_EOF;
+        if (avio_tell(pb) - orig_pos + 6 >= size) {
+            av_log(avc, AV_LOG_ERROR, "s337m : sync bytes not found at packet pos=0x%"PRIx64" size=%d\n", orig_pos, size);
+            return AVERROR_INVALIDDATA;
+        }
     }
 
     if (IS_16LE_MARKER(state)) {
@@ -165,10 +168,10 @@  static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     pos = avio_tell(pb);
 
-    if ((ret = s337m_get_offset_and_codec(s, state, data_type, data_size, &offset, &codec)) < 0)
+    if ((ret = s337m_get_offset_and_codec(avc, state, data_type, data_size, &offset, codec)) < 0)
         return ret;
 
-    if ((ret = av_new_packet(pkt, offset)) < 0)
+    if (ret = av_new_packet(pkt, FFMIN(offset, size - (pos - orig_pos))) < 0)
         return ret;
 
     pkt->pos = pos;
@@ -182,6 +185,17 @@  static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
     else
         bswap_buf24(pkt->data, pkt->size);
 
+    return 0;
+}
+
+static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    enum AVCodecID codec;
+    int ret;
+
+    if ((ret = ff_s337m_get_packet(s->pb, pkt, avio_size(s->pb), &codec, s)) < 0)
+        return ret;
+
     if (!s->nb_streams) {
         AVStream *st = avformat_new_stream(s, NULL);
         if (!st) {
diff --git a/libavformat/s337m.h b/libavformat/s337m.h
new file mode 100644
index 0000000000..f7bd0c16f6
--- /dev/null
+++ b/libavformat/s337m.h
@@ -0,0 +1,37 @@ 
+/*
+ * SMPTE ST 337 common header
+ *
+ * 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
+
+/**
+ * Read s337m packets in a PCM_S16LE/S24LE stereo stream
+ * Returns the first inner packet found
+ * Note that it does not require a clean guard band
+ * @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
+ * @param avc For av_log
+ * @return = 0 on success (an error is raised if no s337m was found)
+ */
+int ff_s337m_get_packet(AVIOContext *pb, AVPacket *pkt, int size, enum AVCodecID *codec, void *avc);
+
+#endif /* AVFORMAT_S337M_H */