static int decode_frame(AVCodecContext *avctx,

                        void *data, int *data_size, AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size = avpkt->size;

    TiffContext *const s = avctx->priv_data;

    AVFrame *picture = data;

    AVFrame *const p = &s->picture;

    const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;

    unsigned off;

    int id, le, ret;

    int i, j, entries;

    int stride;

    unsigned soff, ssize;

    uint8_t *dst;



    //parse image header

    if (end_buf - buf < 8)

        return AVERROR_INVALIDDATA;

    id = AV_RL16(buf);

    buf += 2;

    if (id == 0x4949)

        le = 1;

    else if (id == 0x4D4D)

        le = 0;

    else {

        av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");

        return -1;

    }

    s->le = le;

    s->invert = 0;

    s->compr = TIFF_RAW;

    s->fill_order = 0;

    free_geotags(s);

    /* free existing metadata */

    av_dict_free(&s->picture.metadata);



    // As TIFF 6.0 specification puts it "An arbitrary but carefully chosen number

    // that further identifies the file as a TIFF file"

    if (tget_short(&buf, le) != 42) {

        av_log(avctx, AV_LOG_ERROR,

               "The answer to life, universe and everything is not correct!\n");

        return -1;

    }

    // Reset these pointers so we can tell if they were set this frame

    s->stripsizes = s->stripdata = NULL;

    /* parse image file directory */

    off = tget_long(&buf, le);

    if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {

        av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");

        return AVERROR_INVALIDDATA;

    }

    buf = orig_buf + off;

    entries = tget_short(&buf, le);

    for (i = 0; i < entries; i++) {

        if (tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)

            return -1;

        buf += 12;

    }



    for (i = 0; i<s->geotag_count; i++) {

        const char *keyname = get_geokey_name(s->geotags[i].key);

        if (!keyname) {

            av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);

            continue;

        }

        if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {

            av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);

            continue;

        }

        ret = av_dict_set(&s->picture.metadata, keyname, s->geotags[i].val, 0);

        if (ret<0) {

            av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);

            return ret;

        }

    }



    if (!s->stripdata && !s->stripoff) {

        av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");

        return -1;

    }

    /* now we have the data and may start decoding */

    if ((ret = init_image(s)) < 0)

        return ret;



    if (s->strips == 1 && !s->stripsize) {

        av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");

        s->stripsize = buf_size - s->stripoff;

    }

    stride = p->linesize[0];

    dst = p->data[0];

    for (i = 0; i < s->height; i += s->rps) {

        if (s->stripsizes) {

            if (s->stripsizes >= end_buf)

                return AVERROR_INVALIDDATA;

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

        } else

            ssize = s->stripsize;



        if (s->stripdata) {

            if (s->stripdata >= end_buf)

                return AVERROR_INVALIDDATA;

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

        } else

            soff = s->stripoff;



        if (soff > buf_size || ssize > buf_size - soff) {

            av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");

            return -1;

        }

        if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,

                              FFMIN(s->rps, s->height - i)) < 0)

            break;

        dst += s->rps * stride;

    }

    if (s->predictor == 2) {

        dst = p->data[0];

        soff = s->bpp >> 3;

        ssize = s->width * soff;

        if (s->avctx->pix_fmt == PIX_FMT_RGB48LE ||

            s->avctx->pix_fmt == PIX_FMT_RGBA64LE) {

            for (i = 0; i < s->height; i++) {

                for (j = soff; j < ssize; j += 2)

                    AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));

                dst += stride;

            }

        } else if (s->avctx->pix_fmt == PIX_FMT_RGB48BE ||

                   s->avctx->pix_fmt == PIX_FMT_RGBA64BE) {

            for (i = 0; i < s->height; i++) {

                for (j = soff; j < ssize; j += 2)

                    AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));

                dst += stride;

            }

        } else {

            for (i = 0; i < s->height; i++) {

                for (j = soff; j < ssize; j++)

                    dst[j] += dst[j - soff];

                dst += stride;

            }

        }

    }



    if (s->invert) {

        dst = s->picture.data[0];

        for (i = 0; i < s->height; i++) {

            for (j = 0; j < s->picture.linesize[0]; j++)

                dst[j] = (s->avctx->pix_fmt == PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];

            dst += s->picture.linesize[0];

        }

    }

    *picture   = s->picture;

    *data_size = sizeof(AVPicture);



    return buf_size;

}

static int decode_frame(AVCodecContext *avctx,

                        void *data, int *data_size, AVPacket *avpkt)

{

    const uint8_t *buf = avpkt->data;

    int buf_size = avpkt->size;

    TiffContext *const s = avctx->priv_data;

    AVFrame *picture = data;

    AVFrame *const p = &s->picture;

    const uint8_t *orig_buf = buf, *end_buf = buf + buf_size;

    unsigned off;

    int id, le, ret;

    int i, j, entries;

    int stride;

    unsigned soff, ssize;

    uint8_t *dst;



    

    if (end_buf - buf < 8)

        return AVERROR_INVALIDDATA;

    id = AV_RL16(buf);

    buf += 2;

    if (id == 0x4949)

        le = 1;

    else if (id == 0x4D4D)

        le = 0;

    else {

        av_log(avctx, AV_LOG_ERROR, "TIFF header not found\n");

        return -1;

    }

    s->le = le;

    s->invert = 0;

    s->compr = TIFF_RAW;

    s->fill_order = 0;

    free_geotags(s);

    

    av_dict_free(&s->picture.metadata);



    

    

    if (tget_short(&buf, le) != 42) {

        av_log(avctx, AV_LOG_ERROR,

               "The answer to life, universe and everything is not correct!\n");

        return -1;

    }

    

    s->stripsizes = s->stripdata = NULL;

    

    off = tget_long(&buf, le);

    if (off >= UINT_MAX - 14 || end_buf - orig_buf < off + 14) {

        av_log(avctx, AV_LOG_ERROR, "IFD offset is greater than image size\n");

        return AVERROR_INVALIDDATA;

    }

    buf = orig_buf + off;

    entries = tget_short(&buf, le);

    for (i = 0; i < entries; i++) {

        if (tiff_decode_tag(s, orig_buf, buf, end_buf) < 0)

            return -1;

        buf += 12;

    }



    for (i = 0; i<s->geotag_count; i++) {

        const char *keyname = get_geokey_name(s->geotags[i].key);

        if (!keyname) {

            av_log(avctx, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[i].key);

            continue;

        }

        if (get_geokey_type(s->geotags[i].key) != s->geotags[i].type) {

            av_log(avctx, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[i].key);

            continue;

        }

        ret = av_dict_set(&s->picture.metadata, keyname, s->geotags[i].val, 0);

        if (ret<0) {

            av_log(avctx, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);

            return ret;

        }

    }



    if (!s->stripdata && !s->stripoff) {

        av_log(avctx, AV_LOG_ERROR, "Image data is missing\n");

        return -1;

    }

    

    if ((ret = init_image(s)) < 0)

        return ret;



    if (s->strips == 1 && !s->stripsize) {

        av_log(avctx, AV_LOG_WARNING, "Image data size missing\n");

        s->stripsize = buf_size - s->stripoff;

    }

    stride = p->linesize[0];

    dst = p->data[0];

    for (i = 0; i < s->height; i += s->rps) {

        if (s->stripsizes) {

            if (s->stripsizes >= end_buf)

                return AVERROR_INVALIDDATA;

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

        } else

            ssize = s->stripsize;



        if (s->stripdata) {

            if (s->stripdata >= end_buf)

                return AVERROR_INVALIDDATA;

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

        } else

            soff = s->stripoff;



        if (soff > buf_size || ssize > buf_size - soff) {

            av_log(avctx, AV_LOG_ERROR, "Invalid strip size/offset\n");

            return -1;

        }

        if (tiff_unpack_strip(s, dst, stride, orig_buf + soff, ssize,

                              FFMIN(s->rps, s->height - i)) < 0)

            break;

        dst += s->rps * stride;

    }

    if (s->predictor == 2) {

        dst = p->data[0];

        soff = s->bpp >> 3;

        ssize = s->width * soff;

        if (s->avctx->pix_fmt == PIX_FMT_RGB48LE ||

            s->avctx->pix_fmt == PIX_FMT_RGBA64LE) {

            for (i = 0; i < s->height; i++) {

                for (j = soff; j < ssize; j += 2)

                    AV_WL16(dst + j, AV_RL16(dst + j) + AV_RL16(dst + j - soff));

                dst += stride;

            }

        } else if (s->avctx->pix_fmt == PIX_FMT_RGB48BE ||

                   s->avctx->pix_fmt == PIX_FMT_RGBA64BE) {

            for (i = 0; i < s->height; i++) {

                for (j = soff; j < ssize; j += 2)

                    AV_WB16(dst + j, AV_RB16(dst + j) + AV_RB16(dst + j - soff));

                dst += stride;

            }

        } else {

            for (i = 0; i < s->height; i++) {

                for (j = soff; j < ssize; j++)

                    dst[j] += dst[j - soff];

                dst += stride;

            }

        }

    }



    if (s->invert) {

        dst = s->picture.data[0];

        for (i = 0; i < s->height; i++) {

            for (j = 0; j < s->picture.linesize[0]; j++)

                dst[j] = (s->avctx->pix_fmt == PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[j];

            dst += s->picture.linesize[0];

        }

    }

    *picture   = s->picture;

    *data_size = sizeof(AVPicture);



    return buf_size;

}

static int FUNC_0(AVCodecContext *VAR_0,

                        void *VAR_1, int *VAR_2, AVPacket *VAR_3)

{

    const uint8_t *VAR_4 = VAR_3->VAR_1;

    int VAR_5 = VAR_3->size;

    TiffContext *const s = VAR_0->priv_data;

    AVFrame *picture = VAR_1;

    AVFrame *const p = &s->picture;

    const uint8_t *VAR_6 = VAR_4, *end_buf = VAR_4 + VAR_5;

    unsigned VAR_7;

    int VAR_8, VAR_9, VAR_10;

    int VAR_11, VAR_12, VAR_13;

    int VAR_14;

    unsigned VAR_15, VAR_16;

    uint8_t *dst;



    

    if (end_buf - VAR_4 < 8)

        return AVERROR_INVALIDDATA;

    VAR_8 = AV_RL16(VAR_4);

    VAR_4 += 2;

    if (VAR_8 == 0x4949)

        VAR_9 = 1;

    else if (VAR_8 == 0x4D4D)

        VAR_9 = 0;

    else {

        av_log(VAR_0, AV_LOG_ERROR, "TIFF header not found\n");

        return -1;

    }

    s->VAR_9 = VAR_9;

    s->invert = 0;

    s->compr = TIFF_RAW;

    s->fill_order = 0;

    free_geotags(s);

    

    av_dict_free(&s->picture.metadata);



    

    

    if (tget_short(&VAR_4, VAR_9) != 42) {

        av_log(VAR_0, AV_LOG_ERROR,

               "The answer to life, universe and everything is not correct!\n");

        return -1;

    }

    

    s->stripsizes = s->stripdata = NULL;

    

    VAR_7 = tget_long(&VAR_4, VAR_9);

    if (VAR_7 >= UINT_MAX - 14 || end_buf - VAR_6 < VAR_7 + 14) {

        av_log(VAR_0, AV_LOG_ERROR, "IFD offset is greater than image size\n");

        return AVERROR_INVALIDDATA;

    }

    VAR_4 = VAR_6 + VAR_7;

    VAR_13 = tget_short(&VAR_4, VAR_9);

    for (VAR_11 = 0; VAR_11 < VAR_13; VAR_11++) {

        if (tiff_decode_tag(s, VAR_6, VAR_4, end_buf) < 0)

            return -1;

        VAR_4 += 12;

    }



    for (VAR_11 = 0; VAR_11<s->geotag_count; VAR_11++) {

        const char *keyname = get_geokey_name(s->geotags[VAR_11].key);

        if (!keyname) {

            av_log(VAR_0, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[VAR_11].key);

            continue;

        }

        if (get_geokey_type(s->geotags[VAR_11].key) != s->geotags[VAR_11].type) {

            av_log(VAR_0, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[VAR_11].key);

            continue;

        }

        VAR_10 = av_dict_set(&s->picture.metadata, keyname, s->geotags[VAR_11].val, 0);

        if (VAR_10<0) {

            av_log(VAR_0, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname);

            return VAR_10;

        }

    }



    if (!s->stripdata && !s->stripoff) {

        av_log(VAR_0, AV_LOG_ERROR, "Image VAR_1 is missing\n");

        return -1;

    }

    

    if ((VAR_10 = init_image(s)) < 0)

        return VAR_10;



    if (s->strips == 1 && !s->stripsize) {

        av_log(VAR_0, AV_LOG_WARNING, "Image VAR_1 size missing\n");

        s->stripsize = VAR_5 - s->stripoff;

    }

    VAR_14 = p->linesize[0];

    dst = p->VAR_1[0];

    for (VAR_11 = 0; VAR_11 < s->height; VAR_11 += s->rps) {

        if (s->stripsizes) {

            if (s->stripsizes >= end_buf)

                return AVERROR_INVALIDDATA;

            VAR_16 = tget(&s->stripsizes, s->sstype, s->VAR_9);

        } else

            VAR_16 = s->stripsize;



        if (s->stripdata) {

            if (s->stripdata >= end_buf)

                return AVERROR_INVALIDDATA;

            VAR_15 = tget(&s->stripdata, s->sot, s->VAR_9);

        } else

            VAR_15 = s->stripoff;



        if (VAR_15 > VAR_5 || VAR_16 > VAR_5 - VAR_15) {

            av_log(VAR_0, AV_LOG_ERROR, "Invalid strip size/offset\n");

            return -1;

        }

        if (tiff_unpack_strip(s, dst, VAR_14, VAR_6 + VAR_15, VAR_16,

                              FFMIN(s->rps, s->height - VAR_11)) < 0)

            break;

        dst += s->rps * VAR_14;

    }

    if (s->predictor == 2) {

        dst = p->VAR_1[0];

        VAR_15 = s->bpp >> 3;

        VAR_16 = s->width * VAR_15;

        if (s->VAR_0->pix_fmt == PIX_FMT_RGB48LE ||

            s->VAR_0->pix_fmt == PIX_FMT_RGBA64LE) {

            for (VAR_11 = 0; VAR_11 < s->height; VAR_11++) {

                for (VAR_12 = VAR_15; VAR_12 < VAR_16; VAR_12 += 2)

                    AV_WL16(dst + VAR_12, AV_RL16(dst + VAR_12) + AV_RL16(dst + VAR_12 - VAR_15));

                dst += VAR_14;

            }

        } else if (s->VAR_0->pix_fmt == PIX_FMT_RGB48BE ||

                   s->VAR_0->pix_fmt == PIX_FMT_RGBA64BE) {

            for (VAR_11 = 0; VAR_11 < s->height; VAR_11++) {

                for (VAR_12 = VAR_15; VAR_12 < VAR_16; VAR_12 += 2)

                    AV_WB16(dst + VAR_12, AV_RB16(dst + VAR_12) + AV_RB16(dst + VAR_12 - VAR_15));

                dst += VAR_14;

            }

        } else {

            for (VAR_11 = 0; VAR_11 < s->height; VAR_11++) {

                for (VAR_12 = VAR_15; VAR_12 < VAR_16; VAR_12++)

                    dst[VAR_12] += dst[VAR_12 - VAR_15];

                dst += VAR_14;

            }

        }

    }



    if (s->invert) {

        dst = s->picture.VAR_1[0];

        for (VAR_11 = 0; VAR_11 < s->height; VAR_11++) {

            for (VAR_12 = 0; VAR_12 < s->picture.linesize[0]; VAR_12++)

                dst[VAR_12] = (s->VAR_0->pix_fmt == PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[VAR_12];

            dst += s->picture.linesize[0];

        }

    }

    *picture   = s->picture;

    *VAR_2 = sizeof(AVPicture);



    return VAR_5;

}

static int FUNC_0(AVCodecContext *VAR_0,
void *VAR_1, int *VAR_2, AVPacket *VAR_3)
{ const uint8_t *VAR_4 = VAR_3->VAR_1; int VAR_5 = VAR_3->size; TiffContext *const s = VAR_0->priv_data; AVFrame *picture = VAR_1; AVFrame *const p = &s->picture; const uint8_t *VAR_6 = VAR_4, *end_buf = VAR_4 + VAR_5; unsigned VAR_7; int VAR_8, VAR_9, VAR_10; int VAR_11, VAR_12, VAR_13; int VAR_14; unsigned VAR_15, VAR_16; uint8_t *dst; if (end_buf - VAR_4 < 8)
return AVERROR_INVALIDDATA; VAR_8 = AV_RL16(VAR_4); VAR_4 += 2; if (VAR_8 == 0x4949)
VAR_9 = 1; else if (VAR_8 == 0x4D4D)
VAR_9 = 0; else { av_log(VAR_0, AV_LOG_ERROR, "TIFF header not found\n"); return -1; } s->VAR_9 = VAR_9; s->invert = 0; s->compr = TIFF_RAW; s->fill_order = 0; free_geotags(s); av_dict_free(&s->picture.metadata); if (tget_short(&VAR_4, VAR_9) != 42) { av_log(VAR_0, AV_LOG_ERROR,
"The answer to life, universe and everything is not correct!\n"); return -1; } s->stripsizes = s->stripdata = NULL; VAR_7 = tget_long(&VAR_4, VAR_9); if (VAR_7 >= UINT_MAX - 14 || end_buf - VAR_6 < VAR_7 + 14) { av_log(VAR_0, AV_LOG_ERROR, "IFD offset is greater than image size\n"); return AVERROR_INVALIDDATA; } VAR_4 = VAR_6 + VAR_7; VAR_13 = tget_short(&VAR_4, VAR_9); for (VAR_11 = 0; VAR_11 < VAR_13; VAR_11++) { if (tiff_decode_tag(s, VAR_6, VAR_4, end_buf) < 0)
return -1; VAR_4 += 12; } for (VAR_11 = 0; VAR_11<s->geotag_count; VAR_11++) { const char *keyname = get_geokey_name(s->geotags[VAR_11].key); if (!keyname) { av_log(VAR_0, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[VAR_11].key); continue; } if (get_geokey_type(s->geotags[VAR_11].key) != s->geotags[VAR_11].type) { av_log(VAR_0, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[VAR_11].key); continue; } VAR_10 = av_dict_set(&s->picture.metadata, keyname, s->geotags[VAR_11].val, 0); if (VAR_10<0) { av_log(VAR_0, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname); return VAR_10; } } if (!s->stripdata && !s->stripoff) { av_log(VAR_0, AV_LOG_ERROR, "Image VAR_1 is missing\n"); return -1; } if ((VAR_10 = init_image(s)) < 0)
return VAR_10; if (s->strips == 1 && !s->stripsize) { av_log(VAR_0, AV_LOG_WARNING, "Image VAR_1 size missing\n"); s->stripsize = VAR_5 - s->stripoff; } VAR_14 = p->linesize[0]; dst = p->VAR_1[0]; for (VAR_11 = 0; VAR_11 < s->height; VAR_11 += s->rps) { if (s->stripsizes) { if (s->stripsizes >= end_buf)
return AVERROR_INVALIDDATA; VAR_16 = tget(&s->stripsizes, s->sstype, s->VAR_9); } else VAR_16 = s->stripsize; if (s->stripdata) { if (s->stripdata >= end_buf)
return AVERROR_INVALIDDATA; VAR_15 = tget(&s->stripdata, s->sot, s->VAR_9); } else VAR_15 = s->stripoff; if (VAR_15 > VAR_5 || VAR_16 > VAR_5 - VAR_15) { av_log(VAR_0, AV_LOG_ERROR, "Invalid strip size/offset\n"); return -1; } if (tiff_unpack_strip(s, dst, VAR_14, VAR_6 + VAR_15, VAR_16,
FFMIN(s->rps, s->height - VAR_11)) < 0)
break; dst += s->rps * VAR_14; } if (s->predictor == 2) { dst = p->VAR_1[0]; VAR_15 = s->bpp >> 3; VAR_16 = s->width * VAR_15; if (s->VAR_0->pix_fmt == PIX_FMT_RGB48LE ||
s->VAR_0->pix_fmt == PIX_FMT_RGBA64LE) { for (VAR_11 = 0; VAR_11 < s->height; VAR_11++) { for (VAR_12 = VAR_15; VAR_12 < VAR_16; VAR_12 += 2) AV_WL16(dst + VAR_12, AV_RL16(dst + VAR_12) + AV_RL16(dst + VAR_12 - VAR_15)); dst += VAR_14; } } else if (s->VAR_0->pix_fmt == PIX_FMT_RGB48BE || s->VAR_0->pix_fmt == PIX_FMT_RGBA64BE) { for (VAR_11 = 0; VAR_11 < s->height; VAR_11++) { for (VAR_12 = VAR_15; VAR_12 < VAR_16; VAR_12 += 2) AV_WB16(dst + VAR_12, AV_RB16(dst + VAR_12) + AV_RB16(dst + VAR_12 - VAR_15)); dst += VAR_14; } } else { for (VAR_11 = 0; VAR_11 < s->height; VAR_11++) { for (VAR_12 = VAR_15; VAR_12 < VAR_16; VAR_12++) dst[VAR_12] += dst[VAR_12 - VAR_15]; dst += VAR_14; } } } if (s->invert) { dst = s->picture.VAR_1[0]; for (VAR_11 = 0; VAR_11 < s->height; VAR_11++) { for (VAR_12 = 0; VAR_12 < s->picture.linesize[0]; VAR_12++) dst[VAR_12] = (s->VAR_0->pix_fmt == PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255) - dst[VAR_12]; dst += s->picture.linesize[0]; } } *picture   = s->picture; *VAR_2 = sizeof(AVPicture); return VAR_5; }