[FFmpeg-devel] [PATCH]Fix LZW/ZLIB compressed 4bit tiff

Carl Eugen Hoyos cehoyos at ag.or.at
Sun Sep 25 15:53:34 CEST 2011


On Sunday 25 September 2011 03:07:52 pm Reimar Döffinger wrote:
> On Sun, Sep 25, 2011 at 02:21:10PM +0200, Carl Eugen Hoyos wrote:
> > +    int i;
> > +    for(i = width - 1; i >= 0; i--){
> 
> That looks the same as
> while (--width >= 0)

Yes, thank you.

> However for cache reasons going through it backwards is questionable.

It is needed for LZW where src == dts.

[...]

> > @@ -148,7 +158,11 @@ static int tiff_unpack_strip(TiffContext *s,
> > uint8_t* dst, int stride, const uin }
> >          src = zbuf;
> >          for(line = 0; line < lines; line++){
> > -            memcpy(dst, src, width);
> > +            if(s->bpp == 4){
> > +                split_nibbles(src, dst, width);
> > +            }else{
> > +                memcpy(dst, src, width);
> > +            }
> >              dst += stride;
> >              src += width;
> >          }
> > @@ -238,6 +252,8 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t*
> > dst, int stride, const uin av_log(s->avctx, AV_LOG_ERROR, "Decoded only
> > %i bytes of %i\n", pixels, width); return -1;
> >              }
> > +            if(s->bpp == 4)
> > +                split_nibbles(dst, dst, width);
> 
> That doubles the amount of data, are the buffer size/size checks still
> correct after that change?

I was more surprised that I did not have to change any checks to make decoding 
work. Since width for bpp=4 is exactly half of (real) "width" I believe it is 
not worse than before.

Updated patch attached, Carl Eugen
-------------- next part --------------
diff --git a/libavcodec/tiff.c b/libavcodec/tiff.c
index e2c80ef..a6d5cca 100644
--- a/libavcodec/tiff.c
+++ b/libavcodec/tiff.c
@@ -129,6 +129,15 @@ static void av_always_inline horizontal_fill(unsigned int bpp, uint8_t* dst,
     }
 }
 
+static void av_always_inline split_nibbles(const uint8_t *src, uint8_t *dst,
+                                           int width)
+{
+    while (--width >= 0) {
+        dst[width * 2 + 1] = src[width] & 0xF;
+        dst[width * 2 + 0] = src[width] >> 4;
+    }
+}
+
 static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uint8_t *src, int size, int lines){
     int c, line, pixels, code;
     const uint8_t *ssrc = src;
@@ -148,7 +157,11 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uin
         }
         src = zbuf;
         for(line = 0; line < lines; line++){
-            memcpy(dst, src, width);
+            if(s->bpp == 4){
+                split_nibbles(src, dst, width);
+            }else{
+                memcpy(dst, src, width);
+            }
             dst += stride;
             src += width;
         }
@@ -238,6 +251,8 @@ static int tiff_unpack_strip(TiffContext *s, uint8_t* dst, int stride, const uin
                 av_log(s->avctx, AV_LOG_ERROR, "Decoded only %i bytes of %i\n", pixels, width);
                 return -1;
             }
+            if(s->bpp == 4)
+                split_nibbles(dst, dst, width);
             break;
         }
         dst += stride;


More information about the ffmpeg-devel mailing list