]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/img2dec.c
avformat/img2dec: improve bmp probe
[ffmpeg] / libavformat / img2dec.c
index a8f87efb674fd83545e16b5e1a21d4cb518ae61f..b1fa8cc987c2cc0c703522bb566fec88fc69244a 100644 (file)
@@ -29,6 +29,7 @@
 #include "libavutil/parseutils.h"
 #include "libavutil/intreadwrite.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "internal.h"
 #include "img2.h"
 
@@ -304,16 +305,23 @@ int ff_img_read_header(AVFormatContext *s1)
         s->split_planes       = str && !av_strcasecmp(str + 1, "y");
         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
         if (s1->pb) {
-            uint8_t probe_buffer[AVPROBE_PADDING_SIZE] = {0};
+            int probe_buffer_size = 2048;
+            uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE);
             AVInputFormat *fmt = NULL;
             AVProbeData pd;
-            int ret = avio_read(s1->pb, probe_buffer, 8);
-            if (ret < 8)
-                return AVERROR(EINVAL);
-            avio_seek(s1->pb, -8, SEEK_CUR);
+
+            if (!probe_buffer)
+                return AVERROR(ENOMEM);
+
+            probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size);
+            if (probe_buffer_size < 0) {
+                av_free(probe_buffer);
+                return probe_buffer_size;
+            }
+            memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE);
 
             pd.buf = probe_buffer;
-            pd.buf_size = 8;
+            pd.buf_size = probe_buffer_size;
             pd.filename = s1->filename;
 
             while ((fmt = av_iformat_next(fmt))) {
@@ -327,6 +335,7 @@ int ff_img_read_header(AVFormatContext *s1)
                     break;
                 }
             }
+            ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
         }
         if (st->codec->codec_id == AV_CODEC_ID_NONE)
             st->codec->codec_id = ff_guess_image2_codec(s->path);
@@ -555,13 +564,20 @@ AVInputFormat ff_image2pipe_demuxer = {
 static int bmp_probe(AVProbeData *p)
 {
     const uint8_t *b = p->buf;
+    int ihsize;
 
-    if (AV_RB16(b) == 0x424d)
-        if (!AV_RN32(b + 6)) {
-            return AVPROBE_SCORE_EXTENSION + 1;
-        } else {
-            return AVPROBE_SCORE_EXTENSION / 4;
-        }
+    if (AV_RB16(b) != 0x424d)
+        return 0;
+
+    ihsize = AV_RL32(b+14);
+    if (ihsize < 12 || ihsize > 255)
+        return 0;
+
+    if (!AV_RN32(b + 6)) {
+        return AVPROBE_SCORE_EXTENSION + 1;
+    } else {
+        return AVPROBE_SCORE_EXTENSION / 4;
+    }
     return 0;
 }
 
@@ -583,6 +599,16 @@ static int exr_probe(AVProbeData *p)
     return 0;
 }
 
+static int j2k_probe(AVProbeData *p)
+{
+    const uint8_t *b = p->buf;
+
+    if (AV_RB64(b) == 0x0000000c6a502020 ||
+        AV_RB32(b) == 0xff4fff51)
+        return AVPROBE_SCORE_EXTENSION + 1;
+    return 0;
+}
+
 static int pictor_probe(AVProbeData *p)
 {
     const uint8_t *b = p->buf;
@@ -653,6 +679,7 @@ AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
 IMAGEAUTO_DEMUXER(bmp,     AV_CODEC_ID_BMP)
 IMAGEAUTO_DEMUXER(dpx,     AV_CODEC_ID_DPX)
 IMAGEAUTO_DEMUXER(exr,     AV_CODEC_ID_EXR)
+IMAGEAUTO_DEMUXER(j2k,     AV_CODEC_ID_JPEG2000)
 IMAGEAUTO_DEMUXER(pictor,  AV_CODEC_ID_PICTOR)
 IMAGEAUTO_DEMUXER(png,     AV_CODEC_ID_PNG)
 IMAGEAUTO_DEMUXER(sgi,     AV_CODEC_ID_SGI)