]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/siff.c
Merge commit '75439bb31b3ab76f9a4d30c5de4ff9f407ec8128'
[ffmpeg] / libavformat / siff.c
index 3a74c81c36dda2670e2b8024135c996059e64533..3b54e90777811de04fa586346d0c930d869668da 100644 (file)
@@ -2,20 +2,20 @@
  * Beam Software SIFF demuxer
  * Copyright (c) 2007 Konstantin Shishkov
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * 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.
  *
- * Libav is distributed in the hope that it will be useful,
+ * 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 Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -24,6 +24,7 @@
 
 #include "avformat.h"
 #include "internal.h"
+#include "avio_internal.h"
 
 enum SIFFTags {
     TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
@@ -76,7 +77,7 @@ static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
     AVStream *ast;
     ast = avformat_new_stream(s, NULL);
     if (!ast)
-        return -1;
+        return AVERROR(ENOMEM);
     ast->codec->codec_type            = AVMEDIA_TYPE_AUDIO;
     ast->codec->codec_id              = AV_CODEC_ID_PCM_U8;
     ast->codec->channels              = 1;
@@ -95,15 +96,15 @@ static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
 
     if (avio_rl32(pb) != TAG_VBHD) {
         av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     if (avio_rb32(pb) != 32) {
         av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     if (avio_rl16(pb) != 1) {
         av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     width  = avio_rl16(pb);
     height = avio_rl16(pb);
@@ -111,7 +112,7 @@ static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
     c->frames = avio_rl16(pb);
     if (!c->frames) {
         av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     c->bits        = avio_rl16(pb);
     c->rate        = avio_rl16(pb);
@@ -121,13 +122,15 @@ static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
 
     st = avformat_new_stream(s, NULL);
     if (!st)
-        return -1;
+        return AVERROR(ENOMEM);
     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
     st->codec->codec_id   = AV_CODEC_ID_VB;
     st->codec->codec_tag  = MKTAG('V', 'B', 'V', '1');
     st->codec->width      = width;
     st->codec->height     = height;
     st->codec->pix_fmt    = AV_PIX_FMT_PAL8;
+    st->nb_frames         =
+    st->duration          = c->frames;
     avpriv_set_pts_info(st, 16, 1, 12);
 
     c->cur_frame = 0;
@@ -135,7 +138,7 @@ static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
     c->has_audio = !!c->rate;
     c->curstrm   = -1;
     if (c->has_audio && create_audio_stream(s, c) < 0)
-        return -1;
+        return AVERROR(ENOMEM);
     return 0;
 }
 
@@ -143,11 +146,11 @@ static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
 {
     if (avio_rl32(pb) != TAG_SHDR) {
         av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     if (avio_rb32(pb) != 8) {
         av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     avio_skip(pb, 4); // unknown value
     c->rate        = avio_rl16(pb);
@@ -161,24 +164,25 @@ static int siff_read_header(AVFormatContext *s)
     AVIOContext *pb = s->pb;
     SIFFContext *c  = s->priv_data;
     uint32_t tag;
+    int ret;
 
     if (avio_rl32(pb) != TAG_SIFF)
-        return -1;
+        return AVERROR_INVALIDDATA;
     avio_skip(pb, 4); // ignore size
     tag = avio_rl32(pb);
 
     if (tag != TAG_VBV1 && tag != TAG_SOUN) {
         av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
-    if (tag == TAG_VBV1 && siff_parse_vbv1(s, c, pb) < 0)
-        return -1;
-    if (tag == TAG_SOUN && siff_parse_soun(s, c, pb) < 0)
-        return -1;
+    if (tag == TAG_VBV1 && (ret = siff_parse_vbv1(s, c, pb)) < 0)
+        return ret;
+    if (tag == TAG_SOUN && (ret = siff_parse_soun(s, c, pb)) < 0)
+        return ret;
     if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')) {
         av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
     avio_skip(pb, 4); // ignore size
 
@@ -192,7 +196,7 @@ static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
 
     if (c->has_video) {
         if (c->cur_frame >= c->frames)
-            return AVERROR(EIO);
+            return AVERROR_EOF;
         if (c->curstrm == -1) {
             c->pktsize = avio_rl32(s->pb) - 4;
             c->flags   = avio_rl16(s->pb);
@@ -204,13 +208,19 @@ static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
         }
 
         if (!c->curstrm) {
-            size = c->pktsize - c->sndsize;
-            if (av_new_packet(pkt, size) < 0)
+            size = c->pktsize - c->sndsize - c->gmcsize - 2;
+            size = ffio_limit(s->pb, size);
+            if(size < 0 || c->pktsize < c->sndsize)
+                return AVERROR_INVALIDDATA;
+            if (av_new_packet(pkt, size + c->gmcsize + 2) < 0)
                 return AVERROR(ENOMEM);
             AV_WL16(pkt->data, c->flags);
             if (c->gmcsize)
                 memcpy(pkt->data + 2, c->gmc, c->gmcsize);
-            avio_read(s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
+            if (avio_read(s->pb, pkt->data + 2 + c->gmcsize, size) != size) {
+                av_free_packet(pkt);
+                return AVERROR_INVALIDDATA;
+            }
             pkt->stream_index = 0;
             c->curstrm        = -1;
         } else {
@@ -226,7 +236,9 @@ static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
             c->cur_frame++;
     } else {
         size = av_get_packet(s->pb, pkt, c->block_align);
-        if (size <= 0)
+        if (!size)
+            return AVERROR_EOF;
+        if (size < 0)
             return AVERROR(EIO);
         pkt->duration = size;
     }