diff mbox series

[FFmpeg-devel] avformat/img2dec: probe JFIF/Exif header

Message ID 1637868248.utcsp6528a.none@localhost
State Accepted
Commit 6fdd7fe0b89f5f76314bf06a7d28fb18d0196f84
Headers show
Series [FFmpeg-devel] avformat/img2dec: probe JFIF/Exif header
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Alex Xu (Hello71) Nov. 25, 2021, 7:36 p.m. UTC
Due to reasons, mpv doesn't pass filename when probing. mpv also sets
default probescore threshold to 26. Since the current jpeg_probe
implementation returns 25 until EOI, it means that the whole image needs
to be probed to succeed. Worse, the whole image is not passed at once;
increasingly large buffers are tried before that. Adding it up together,
if many demuxers are enabled, moderately large JPEG files (few MB) can
take several seconds to open, despite taking less than 1 second to
actually decode.

Therefore, adjust the heuristic to be more optimistic if proper JFIF or
Exif segments are found. While not strictly required, the vast majority
of JPEG-ish files have one or the other or both.
---
 libavformat/img2dec.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

Michael Niedermayer Nov. 27, 2021, 5:21 p.m. UTC | #1
On Thu, Nov 25, 2021 at 02:36:46PM -0500, Alex Xu (Hello71) wrote:
> Due to reasons, mpv doesn't pass filename when probing. mpv also sets
> default probescore threshold to 26. Since the current jpeg_probe
> implementation returns 25 until EOI, it means that the whole image needs
> to be probed to succeed. Worse, the whole image is not passed at once;
> increasingly large buffers are tried before that. Adding it up together,
> if many demuxers are enabled, moderately large JPEG files (few MB) can
> take several seconds to open, despite taking less than 1 second to
> actually decode.
> 
> Therefore, adjust the heuristic to be more optimistic if proper JFIF or
> Exif segments are found. While not strictly required, the vast majority
> of JPEG-ish files have one or the other or both.
> ---
>  libavformat/img2dec.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)

will apply

thx

[...]
diff mbox series

Patch

diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index b535831e1c..a6084ceef0 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -749,7 +749,7 @@  static int j2k_probe(const AVProbeData *p)
 static int jpeg_probe(const AVProbeData *p)
 {
     const uint8_t *b = p->buf;
-    int i, state = SOI;
+    int i, state = SOI, got_header = 0;
 
     if (AV_RB16(b) != 0xFFD8 ||
         AV_RB32(b) == 0xFFD8FFF7)
@@ -789,7 +789,11 @@  static int jpeg_probe(const AVProbeData *p)
             break;
         case DQT:
         case APP0:
+            if (AV_RL32(&b[i + 4]) == MKTAG('J','F','I','F'))
+                got_header = 1;
         case APP1:
+            if (AV_RL32(&b[i + 4]) == MKTAG('E','x','i','f'))
+                got_header = 1;
         case APP2:
         case APP3:
         case APP4:
@@ -817,7 +821,7 @@  static int jpeg_probe(const AVProbeData *p)
     if (state == EOI)
         return AVPROBE_SCORE_EXTENSION + 1;
     if (state == SOS)
-        return AVPROBE_SCORE_EXTENSION / 2;
+        return AVPROBE_SCORE_EXTENSION / 2 + got_header;
     return AVPROBE_SCORE_EXTENSION / 8 + 1;
 }