[FFmpeg-devel] [PATCH v2 2/5] avformat/s337m: Split read_packet/get_packet

Nicolas Gaullier nicolas.gaullier at arkena.com
Tue Aug 6 14:50:41 EEST 2019


Prepare use of s337m_get_packet from outside
---
 libavformat/s337m.c | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/libavformat/s337m.c b/libavformat/s337m.c
index 8956afb23f..22140297e6 100644
--- a/libavformat/s337m.c
+++ b/libavformat/s337m.c
@@ -31,6 +31,8 @@
 #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);
+
 static int s337m_get_offset_and_codec(void *avc,
                                       uint64_t state,
                                       int data_type, int data_size,
@@ -141,18 +143,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 avpriv_s337m_get_packet(void *avc, AVIOContext *pb, AVPacket *pkt, int size, enum AVCodecID *codec)
 {
-    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_max=%d\n", orig_pos, size);
+            return AVERROR_INVALIDDATA;
+        }
     }
 
     if (IS_16LE_MARKER(state)) {
@@ -165,10 +169,12 @@ 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 (pos - orig_pos > size)
+        return AVERROR_INVALIDDATA;
+    if (ret = av_new_packet(pkt, FFMIN(offset, size - (pos - orig_pos))) < 0)
         return ret;
 
     pkt->pos = pos;
@@ -183,6 +189,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 = avpriv_s337m_get_packet((void *)s, s->pb, pkt, avio_size(s->pb), &codec)) < 0)
+        return ret;
+
     if (!s->nb_streams) {
         AVStream *st = avformat_new_stream(s, NULL);
         if (!st) {
-- 
2.14.1.windows.1



More information about the ffmpeg-devel mailing list