[Ffmpeg-devel] [RFC] TIFF image decoder

Michael Niedermayer michaelni
Thu Oct 12 10:46:12 CEST 2006


Hi

On Wed, Oct 11, 2006 at 05:58:56PM +0300, Kostya wrote:
> Here is another image format support. For now it's 24-bpp only.

[...]
> +#define TGET_SHORT(src, le)  ((le) ? LE_16(src) : BE_16(src))
> +#define TGET_LONG(src, le)  ((le) ? LE_32(src) : BE_32(src))

id make these static functions to reduce code size (the compiler can then
choose if it wants to inlines them or not
also id add +=2 / +=4 in them as practically every use of them does that
to the pointer afterwards

static int tget_short(uint8_t **p, int le){
    int v= le ? LE_16(*src) : BE_16(*src);
    *src+=2;
    return v;
}

static int tget_long(uint8_t **p, int le){
    int v= le ? LE_32(*src) : BE_32(*src);
    *src+=4;
    return v;
}

static int tget(uint8_t **p, int type, int le){
    switch(type){
    case TIFF_BYTE : return *(*p)++;
    case TIFF_SHORT: return tget_short(p, le);
    case TIFF_LONG : return tget_long (p, le);
    default        : return -1;
    }
}


[...]
> +        case TIFF_PACKBITS:
> +            for(pixels = 0; pixels < width;){
> +                code = (int8_t)*src++;
> +                if(code >= 0){
> +                    code++;
> +                    if(pixels + code > width){
> +                        av_log(s->avctx, AV_LOG_ERROR, "Copy went out of bounds\n");
> +                        return -1;
> +                    }
> +                    memcpy(dst + pixels, src, code);
> +                    src += code;
> +                    pixels += code;
> +                }else if(code != -128){ // -127..-1
> +                    code = (-code) + 1;
> +                    if(pixels + code > width){
> +                        av_log(s->avctx, AV_LOG_ERROR, "Run went out of bounds\n");
> +                        return -1;
> +                    }
> +                    c = *src++;
> +                    memset(dst + pixels, c, code);
> +                    pixels += code;
> +                }
> +            }
> +            break;

one day someone should look through all the rle decoding functions in
libav* and try to merge them ...


[...]
> +static int tiff_decode_tag(TiffContext *s, uint8_t *start, uint8_t *buf, uint8_t *end_buf, AVFrame *pic)
> +{
> +    int tag, type, count, off, value = 0;
> +    uint8_t* data = NULL, *src, *dst;
> +    int i, j, ssize, soff, stride;
> +
> +    tag = TGET_SHORT(buf, s->le); buf += 2;
> +    type = TGET_SHORT(buf, s->le); buf += 2;
> +    count = TGET_LONG(buf, s->le); buf += 4;
> +    off = TGET_LONG(buf, s->le); buf += 4;

if buf isnt used/usefull afterwards (it doesnt seems used) then buf could
be used instead of data after this


> +
> +    if(count == 1){
> +        switch(type){
> +        case TIFF_BYTE:
> +            value = s->le ? (off & 0xFF) : ((off >> 24) & 0xFF);
> +            break;
> +        case TIFF_SHORT:
> +            value = s->le ? (off & 0xFFFF) : ((off >> 16) & 0xFFFF);
> +            break;
> +        case TIFF_LONG:
> +            value = off;
> +            break;
> +        default:
> +            value = -1;
> +            data = start + off;
> +        }
> +    }else{
> +        data = start + off;
> +    }

if(count == 1)
    value= tget(&buf, type, s->le);


[...]
> +                if(type == TIFF_SHORT){
> +                    ssize = TGET_SHORT(s->stripsizes, s->le);
> +                    s->stripsizes += 2;
> +                }else{
> +                    ssize = TGET_LONG(s->stripsizes, s->le);
> +                    s->stripsizes += 4;
> +                }

ssize= tget(&s->stripsizes, type, s->le);


> +            }else
> +                ssize = s->stripsize;
> +
> +            if(s->stripdata){
> +                if(s->sot == TIFF_SHORT){
> +                    soff = TGET_SHORT(s->stripdata, s->le);
> +                    s->stripdata += 2;
> +                }else{
> +                    soff = TGET_LONG(s->stripdata, s->le);
> +                    s->stripdata += 4;
> +                }

soff= tget(&s->stripdata, s->sot, s->le);


[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

In the past you could go to a library and read, borrow or copy any book
Today you'd get arrested for mere telling someone where the library is




More information about the ffmpeg-devel mailing list