[FFmpeg-devel] [PATCH] avformat/img2: add ff_guess_image2_codec2()

Michael Niedermayer michaelni at gmx.at
Thu Mar 27 03:44:41 CET 2014


Fixes brender detection
If more input formats need this, then the code should be moved into some
probe_codec() in each AVCodec or some img2 specific system of per codec
probe functions inside img2.
But this seems overkill for just 1 format needing it.

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
---
 libavformat/img2.c     |   24 ++++++++++++++++++++++++
 libavformat/img2dec.c  |    7 ++++++-
 libavformat/internal.h |    1 +
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/libavformat/img2.c b/libavformat/img2.c
index 183bf73..338d4b4 100644
--- a/libavformat/img2.c
+++ b/libavformat/img2.c
@@ -98,6 +98,30 @@ static enum AVCodecID av_str2id(const IdStrMap *tags, const char *str)
     return AV_CODEC_ID_NONE;
 }
 
+enum AVCodecID ff_guess_image2_codec2(const char *filename, AVIOContext *pb)
+{
+    enum AVCodecID codec_id = av_str2id(img_tags, filename);
+    uint8_t header[16];
+    int ret;
+    static const uint8_t brender_magic[16] = {
+        0,0,0,0x12,0,0,0,8,0,0,0,2,0,0,0,2
+    };
+
+    if (codec_id == AV_CODEC_ID_ALIAS_PIX) {
+        if (!pb)
+            return AV_CODEC_ID_NONE;
+
+        ret = avio_read(pb, header, sizeof(header));
+        avio_skip(pb, -sizeof(header));
+        if (ret != sizeof(header))
+            return AV_CODEC_ID_NONE;
+
+        if (!memcmp(header, brender_magic, sizeof(brender_magic)))
+            codec_id = AV_CODEC_ID_BRENDER_PIX;
+    }
+    return codec_id;
+}
+
 enum AVCodecID ff_guess_image2_codec(const char *filename)
 {
     return av_str2id(img_tags, filename);
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index 5163e69..f5c1083 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -317,7 +317,7 @@ static int img_read_header(AVFormatContext *s1)
         const char *str = strrchr(s->path, '.');
         s->split_planes       = str && !av_strcasecmp(str + 1, "y");
         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
-        st->codec->codec_id   = ff_guess_image2_codec(s->path);
+        st->codec->codec_id = ff_guess_image2_codec2(s->path, NULL);
         if (st->codec->codec_id == AV_CODEC_ID_LJPEG)
             st->codec->codec_id = AV_CODEC_ID_MJPEG;
     }
@@ -370,6 +370,11 @@ static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
                 break;
             filename[strlen(filename) - 1] = 'U' + i;
         }
+        if (codec->codec_id == AV_CODEC_ID_NONE) {
+            codec->codec_id = ff_guess_image2_codec2(s->path, f[0]);
+            if (codec->codec_id == AV_CODEC_ID_LJPEG)
+                codec->codec_id = AV_CODEC_ID_MJPEG;
+        }
 
         if (codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->width)
             infer_size(&codec->width, &codec->height, size[0]);
diff --git a/libavformat/internal.h b/libavformat/internal.h
index f19cebf..585b264 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -228,6 +228,7 @@ AVChapter *avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base,
 void ff_reduce_index(AVFormatContext *s, int stream_index);
 
 enum AVCodecID ff_guess_image2_codec(const char *filename);
+enum AVCodecID ff_guess_image2_codec2(const char *filename, AVIOContext *pb);
 
 /**
  * Convert a date string in ISO8601 format to Unix timestamp.
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list