diff mbox

[FFmpeg-devel] lavf/img2dec: Auto-detect svg images

Message ID CAB0OVGqcWTMsOQ1qdKk=Wab41=tAXUK-52EP8s0U8eLUAeMuXw@mail.gmail.com
State Accepted
Headers show

Commit Message

Carl Eugen Hoyos Oct. 2, 2017, 9:42 a.m. UTC
2017-10-02 7:52 GMT+02:00 Clément Bœsch <u@pkh.me>:
> On Mon, Oct 02, 2017 at 01:20:15AM +0200, Carl Eugen Hoyos wrote:
>> Hi!
>>
>> Attached patch implements auto-detection of svg images.
>>
>> Please review, Carl Eugen
>
>> From f06137f38f166740565e58d5c7c88777508f59ec Mon Sep 17 00:00:00 2001
>> From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
>> Date: Mon, 2 Oct 2017 01:13:29 +0200
>> Subject: [PATCH] lavf/img2dec: Auto-detect svg images.
>>
>> ---
>>  libavformat/img2dec.c |   17 +++++++++++++++--
>>  1 file changed, 15 insertions(+), 2 deletions(-)
>>
>> diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
>> index 19cae87..468c820 100644
>> --- a/libavformat/img2dec.c
>> +++ b/libavformat/img2dec.c
>> @@ -34,6 +34,7 @@
>>  #include "internal.h"
>>  #include "img2.h"
>>  #include "libavcodec/mjpeg.h"
>> +#include "subtitles.h"
>>
>>  #if HAVE_GLOB
>>  /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
>> @@ -875,8 +876,20 @@ static int sunrast_probe(AVProbeData *p)
>>
>>  static int svg_probe(AVProbeData *p)
>>  {
>> -    if (av_match_ext(p->filename, "svg") || av_match_ext(p->filename, "svgz"))
>> -        return AVPROBE_SCORE_EXTENSION + 1;
>> +    const uint8_t *b = p->buf;
>> +    const uint8_t *end = p->buf + p->buf_size;
>> +    if (memcmp(p->buf, "<?xml", 5))
>> +        return 0;
>> +    while (b < end) {
>> +        b += ff_subtitles_next_line(b);
>> +        if (b >= end)
>> +            return 0;
>
>> +        if (!strstr(b, "<!DOCTYPE "))
>> +            continue;
>
> at least the svg from inkscape do not have a doctype

Wrong check, thank you.

>> +        b += 9;
>> +        if (strstr(b, "svg"))
>> +            return AVPROBE_SCORE_MAX;
>> +    }
>
> don't you want to keep an extension fallback?

That's done by the image2 demuxer.

> also, I would guess strstr() is going to be slow, so maybe
> just look for a line starting with "<svg".

Done, thank you.

New patch attached, Carl Eugen

Comments

Carl Eugen Hoyos Oct. 7, 2017, 1:43 a.m. UTC | #1
2017-10-02 11:42 GMT+02:00 Carl Eugen Hoyos <ceffmpeg@gmail.com>:
> 2017-10-02 7:52 GMT+02:00 Clément Bœsch <u@pkh.me>:
>> On Mon, Oct 02, 2017 at 01:20:15AM +0200, Carl Eugen Hoyos wrote:
>>> Hi!
>>>
>>> Attached patch implements auto-detection of svg images.
>>>
>>> Please review, Carl Eugen
>>
>>> From f06137f38f166740565e58d5c7c88777508f59ec Mon Sep 17 00:00:00 2001
>>> From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
>>> Date: Mon, 2 Oct 2017 01:13:29 +0200
>>> Subject: [PATCH] lavf/img2dec: Auto-detect svg images.
>>>
>>> ---
>>>  libavformat/img2dec.c |   17 +++++++++++++++--
>>>  1 file changed, 15 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
>>> index 19cae87..468c820 100644
>>> --- a/libavformat/img2dec.c
>>> +++ b/libavformat/img2dec.c
>>> @@ -34,6 +34,7 @@
>>>  #include "internal.h"
>>>  #include "img2.h"
>>>  #include "libavcodec/mjpeg.h"
>>> +#include "subtitles.h"
>>>
>>>  #if HAVE_GLOB
>>>  /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
>>> @@ -875,8 +876,20 @@ static int sunrast_probe(AVProbeData *p)
>>>
>>>  static int svg_probe(AVProbeData *p)
>>>  {
>>> -    if (av_match_ext(p->filename, "svg") || av_match_ext(p->filename, "svgz"))
>>> -        return AVPROBE_SCORE_EXTENSION + 1;
>>> +    const uint8_t *b = p->buf;
>>> +    const uint8_t *end = p->buf + p->buf_size;
>>> +    if (memcmp(p->buf, "<?xml", 5))
>>> +        return 0;
>>> +    while (b < end) {
>>> +        b += ff_subtitles_next_line(b);
>>> +        if (b >= end)
>>> +            return 0;
>>
>>> +        if (!strstr(b, "<!DOCTYPE "))
>>> +            continue;
>>
>> at least the svg from inkscape do not have a doctype
>
> Wrong check, thank you.
>
>>> +        b += 9;
>>> +        if (strstr(b, "svg"))
>>> +            return AVPROBE_SCORE_MAX;
>>> +    }
>>
>> don't you want to keep an extension fallback?
>
> That's done by the image2 demuxer.
>
>> also, I would guess strstr() is going to be slow, so maybe
>> just look for a line starting with "<svg".
>
> Done, thank you.
>
> New patch attached, Carl Eugen

Patch applied.

Carl Eugen
diff mbox

Patch

From 84448be4bffe0dbb2f18f1855a0c686bd15a056c Mon Sep 17 00:00:00 2001
From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
Date: Mon, 2 Oct 2017 11:38:34 +0200
Subject: [PATCH] lavf/img2dec: Auto-detect svg images.

---
 libavformat/img2dec.c |   14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index 19cae87..ecf64ea 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -34,6 +34,7 @@ 
 #include "internal.h"
 #include "img2.h"
 #include "libavcodec/mjpeg.h"
+#include "subtitles.h"
 
 #if HAVE_GLOB
 /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
@@ -875,8 +876,17 @@  static int sunrast_probe(AVProbeData *p)
 
 static int svg_probe(AVProbeData *p)
 {
-    if (av_match_ext(p->filename, "svg") || av_match_ext(p->filename, "svgz"))
-        return AVPROBE_SCORE_EXTENSION + 1;
+    const uint8_t *b = p->buf;
+    const uint8_t *end = p->buf + p->buf_size;
+    if (memcmp(p->buf, "<?xml", 5))
+        return 0;
+    while (b < end) {
+        b += ff_subtitles_next_line(b);
+        if (b >= end - 4)
+            return 0;
+        if (!memcmp(b, "<svg", 4))
+            return AVPROBE_SCORE_EXTENSION + 1;
+    }
     return 0;
 }
 
-- 
1.7.10.4