diff mbox

[FFmpeg-devel,v16,05/15] lavc/jpegtables: Handle multiple mappings to the same value

Message ID 20190828122723.19640-5-velocityra@gmail.com
State Superseded
Headers show

Commit Message

velocityra@gmail.com Aug. 28, 2019, 12:27 p.m. UTC
From: Nick Renieris <velocityra@gmail.com>

Some JPEGs [1] have incorrect DHT entries that map 2 codes to
the same value.

The second (last) mapping does not ever actually appear in the
code stream, therefore ignoring any mappings after the first one
fixes this.

Without this, an "mjpeg_decode_dc: bad vlc: 0:0" error is thrown.

In all known files, the 2 codes are mapped to symbol 0 so only
that case is checked.

[1]: Embedded JPEGs in "X7 RAW" and "X7 CinemaDNG" samples here:
     https://www.dji.com/gr/zenmuse-x7/info#downloads

Signed-off-by: Nick Renieris <velocityra@gmail.com>
---
 libavcodec/jpegtables.c | 17 ++++++++++++++---
 libavcodec/tiff.c       |  6 +++---
 2 files changed, 17 insertions(+), 6 deletions(-)

Comments

Michael Niedermayer Aug. 29, 2019, 1 p.m. UTC | #1
On Wed, Aug 28, 2019 at 03:27:13PM +0300, Nick Renieris wrote:
> From: Nick Renieris <velocityra@gmail.com>
> 
> Some JPEGs [1] have incorrect DHT entries that map 2 codes to
> the same value.
> 
> The second (last) mapping does not ever actually appear in the
> code stream, therefore ignoring any mappings after the first one
> fixes this.
> 
> Without this, an "mjpeg_decode_dc: bad vlc: 0:0" error is thrown.
> 
> In all known files, the 2 codes are mapped to symbol 0 so only
> that case is checked.
> 
> [1]: Embedded JPEGs in "X7 RAW" and "X7 CinemaDNG" samples here:
>      https://www.dji.com/gr/zenmuse-x7/info#downloads
> 
> Signed-off-by: Nick Renieris <velocityra@gmail.com>
> ---
>  libavcodec/jpegtables.c | 17 ++++++++++++++---
>  libavcodec/tiff.c       |  6 +++---
>  2 files changed, 17 insertions(+), 6 deletions(-)
> 
> diff --git a/libavcodec/jpegtables.c b/libavcodec/jpegtables.c
> index cbe5523cb4..fa5c6f9fc5 100644
> --- a/libavcodec/jpegtables.c
> +++ b/libavcodec/jpegtables.c
> @@ -130,14 +130,25 @@ void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
>  {
>      int i, j, k,nb, code, sym;
>  
> -    code = 0;
> +    /* Some badly encoded files [1] map 2 different codes to symbol 0.
> +       Only the first one is valid, so we zero-initialize this here and
> +       make sure we only set it once (the first time) in the loop below.
> +
> +       [1]: Embedded JPEGs in "X7 RAW" and "X7 CinemaDNG" samples here:
> +            https://www.dji.com/gr/zenmuse-x7/info#downloads
> +     */
> +    huff_size[0] = 0;
> +
>      k = 0;
> +    code = 0;
>      for(i=1;i<=16;i++) {
>          nb = bits_table[i];
>          for(j=0;j<nb;j++) {
>              sym = val_table[k++];
> -            huff_size[sym] = i;
> -            huff_code[sym] = code;
> +            if (sym != 0 || huff_size[sym] == 0) { /* see comment above */
> +                huff_size[sym] = i;
> +                huff_code[sym] = code;
> +            }
>              code++;
>          }
>          code <<= 1;

> diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
> index dd1295fad6..78b4bd5301 100644
> --- a/libavcodec/tiff.c
> +++ b/libavcodec/tiff.c
> @@ -754,10 +754,10 @@ static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int strid
>  }
>  
>  static float av_always_inline linear_to_srgb(float value) {
> -    if (value <= 0.0031308)
> -        return value * 12.92;
> +    if (value <= 0.0031308f)
> +        return value * 12.92f;
>      else
> -        return pow(value * 1.055, 1.0 / 2.4) - 0.055;
> +        return powf(value * 1.055f, 1.0f / 2.4f) - 0.055f;
>  }

This looks unrelated


[...]
velocityra@gmail.com Aug. 29, 2019, 1:02 p.m. UTC | #2
Στις Πέμ, 29 Αυγ 2019 στις 4:00 μ.μ., ο/η Michael Niedermayer
<michael@niedermayer.cc> έγραψε:
> This looks unrelated


Messed up when rebasing, will fix in a bit.
diff mbox

Patch

diff --git a/libavcodec/jpegtables.c b/libavcodec/jpegtables.c
index cbe5523cb4..fa5c6f9fc5 100644
--- a/libavcodec/jpegtables.c
+++ b/libavcodec/jpegtables.c
@@ -130,14 +130,25 @@  void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
 {
     int i, j, k,nb, code, sym;
 
-    code = 0;
+    /* Some badly encoded files [1] map 2 different codes to symbol 0.
+       Only the first one is valid, so we zero-initialize this here and
+       make sure we only set it once (the first time) in the loop below.
+
+       [1]: Embedded JPEGs in "X7 RAW" and "X7 CinemaDNG" samples here:
+            https://www.dji.com/gr/zenmuse-x7/info#downloads
+     */
+    huff_size[0] = 0;
+
     k = 0;
+    code = 0;
     for(i=1;i<=16;i++) {
         nb = bits_table[i];
         for(j=0;j<nb;j++) {
             sym = val_table[k++];
-            huff_size[sym] = i;
-            huff_code[sym] = code;
+            if (sym != 0 || huff_size[sym] == 0) { /* see comment above */
+                huff_size[sym] = i;
+                huff_code[sym] = code;
+            }
             code++;
         }
         code <<= 1;
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index dd1295fad6..78b4bd5301 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -754,10 +754,10 @@  static int tiff_unpack_strip(TiffContext *s, AVFrame *p, uint8_t *dst, int strid
 }
 
 static float av_always_inline linear_to_srgb(float value) {
-    if (value <= 0.0031308)
-        return value * 12.92;
+    if (value <= 0.0031308f)
+        return value * 12.92f;
     else
-        return pow(value * 1.055, 1.0 / 2.4) - 0.055;
+        return powf(value * 1.055f, 1.0f / 2.4f) - 0.055f;
 }
 
 /**