static int decode_frame(AVCodecContext *avctx,

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

{

    TiffContext *const s = avctx->priv_data;

    AVFrame *const p = data;

    ThreadFrame frame = { .f = data };

    unsigned off;

    int le, ret, plane, planes;

    int i, j, entries, stride;

    unsigned soff, ssize;

    uint8_t *dst;

    GetByteContext stripsizes;

    GetByteContext stripdata;



    bytestream2_init(&s->gb, avpkt->data, avpkt->size);



    // parse image header

    if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {

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

        return ret;

    } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {

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

        return AVERROR_INVALIDDATA;

    }

    s->le          = le;

    // TIFF_BPP is not a required tag and defaults to 1

    s->bppcount    = s->bpp = 1;

    s->photometric = TIFF_PHOTOMETRIC_NONE;

    s->compr       = TIFF_RAW;

    s->fill_order  = 0;

    free_geotags(s);



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

    s->stripsizesoff = s->strippos = 0;

    /* parse image file directory */

    bytestream2_seek(&s->gb, off, SEEK_SET);

    entries = ff_tget_short(&s->gb, le);

    if (bytestream2_get_bytes_left(&s->gb) < entries * 12)

        return AVERROR_INVALIDDATA;

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

        if ((ret = tiff_decode_tag(s, p)) < 0)

            return ret;

    }



    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(&p->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->strippos && !s->stripoff) {

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

        return AVERROR_INVALIDDATA;

    }

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

    if ((ret = init_image(s, &frame)) < 0)

        return ret;



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

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

        s->stripsize = avpkt->size - s->stripoff;

    }



    if (s->stripsizesoff) {

        if (s->stripsizesoff >= (unsigned)avpkt->size)

            return AVERROR_INVALIDDATA;

        bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,

                         avpkt->size - s->stripsizesoff);

    }

    if (s->strippos) {

        if (s->strippos >= (unsigned)avpkt->size)

            return AVERROR_INVALIDDATA;

        bytestream2_init(&stripdata, avpkt->data + s->strippos,

                         avpkt->size - s->strippos);

    }



    if (s->rps <= 0 || s->rps % s->subsampling[1]) {

        av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);

        return AVERROR_INVALIDDATA;

    }



    planes = s->planar ? s->bppcount : 1;

    for (plane = 0; plane < planes; plane++) {

        stride = p->linesize[plane];

        dst = p->data[plane];

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

            if (s->stripsizesoff)

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

            else

                ssize = s->stripsize;



            if (s->strippos)

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

            else

                soff = s->stripoff;



            if (soff > avpkt->size || ssize > avpkt->size - soff) {

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

                return AVERROR_INVALIDDATA;

            }

            if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,

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

                if (avctx->err_recognition & AV_EF_EXPLODE)

                    return ret;

                break;

            }

            dst += s->rps * stride;

        }

        if (s->predictor == 2) {

            if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {

                av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");

                return AVERROR_PATCHWELCOME;

            }

            dst   = p->data[plane];

            soff  = s->bpp >> 3;

            if (s->planar)

                soff  = FFMAX(soff / s->bppcount, 1);

            ssize = s->width * soff;

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

                s->avctx->pix_fmt == AV_PIX_FMT_RGBA64LE ||

                s->avctx->pix_fmt == AV_PIX_FMT_GRAY16LE ||

                s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||

                s->avctx->pix_fmt == AV_PIX_FMT_GBRP16LE ||

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

                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 == AV_PIX_FMT_RGB48BE ||

                       s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE ||

                       s->avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||

                       s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||

                       s->avctx->pix_fmt == AV_PIX_FMT_GBRP16BE ||

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

                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->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {

            int c = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255);

            dst = p->data[plane];

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

                for (j = 0; j < stride; j++)

                    dst[j] = c - dst[j];

                dst += stride;

            }

        }

    }



    if (s->planar && s->bppcount > 2) {

        FFSWAP(uint8_t*, p->data[0],     p->data[2]);

        FFSWAP(int,      p->linesize[0], p->linesize[2]);

        FFSWAP(uint8_t*, p->data[0],     p->data[1]);

        FFSWAP(int,      p->linesize[0], p->linesize[1]);

    }



    *got_frame = 1;



    return avpkt->size;

}

static int decode_frame(AVCodecContext *avctx,

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

{

    TiffContext *const s = avctx->priv_data;

    AVFrame *const p = data;

    ThreadFrame frame = { .f = data };

    unsigned off;

    int le, ret, plane, planes;

    int i, j, entries, stride;

    unsigned soff, ssize;

    uint8_t *dst;

    GetByteContext stripsizes;

    GetByteContext stripdata;



    bytestream2_init(&s->gb, avpkt->data, avpkt->size);



    

    if ((ret = ff_tdecode_header(&s->gb, &le, &off))) {

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

        return ret;

    } else if (off >= UINT_MAX - 14 || avpkt->size < off + 14) {

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

        return AVERROR_INVALIDDATA;

    }

    s->le          = le;

    

    s->bppcount    = s->bpp = 1;

    s->photometric = TIFF_PHOTOMETRIC_NONE;

    s->compr       = TIFF_RAW;

    s->fill_order  = 0;

    free_geotags(s);



    

    s->stripsizesoff = s->strippos = 0;

    

    bytestream2_seek(&s->gb, off, SEEK_SET);

    entries = ff_tget_short(&s->gb, le);

    if (bytestream2_get_bytes_left(&s->gb) < entries * 12)

        return AVERROR_INVALIDDATA;

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

        if ((ret = tiff_decode_tag(s, p)) < 0)

            return ret;

    }



    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(&p->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->strippos && !s->stripoff) {

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

        return AVERROR_INVALIDDATA;

    }

    

    if ((ret = init_image(s, &frame)) < 0)

        return ret;



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

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

        s->stripsize = avpkt->size - s->stripoff;

    }



    if (s->stripsizesoff) {

        if (s->stripsizesoff >= (unsigned)avpkt->size)

            return AVERROR_INVALIDDATA;

        bytestream2_init(&stripsizes, avpkt->data + s->stripsizesoff,

                         avpkt->size - s->stripsizesoff);

    }

    if (s->strippos) {

        if (s->strippos >= (unsigned)avpkt->size)

            return AVERROR_INVALIDDATA;

        bytestream2_init(&stripdata, avpkt->data + s->strippos,

                         avpkt->size - s->strippos);

    }



    if (s->rps <= 0 || s->rps % s->subsampling[1]) {

        av_log(avctx, AV_LOG_ERROR, "rps %d invalid\n", s->rps);

        return AVERROR_INVALIDDATA;

    }



    planes = s->planar ? s->bppcount : 1;

    for (plane = 0; plane < planes; plane++) {

        stride = p->linesize[plane];

        dst = p->data[plane];

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

            if (s->stripsizesoff)

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

            else

                ssize = s->stripsize;



            if (s->strippos)

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

            else

                soff = s->stripoff;



            if (soff > avpkt->size || ssize > avpkt->size - soff) {

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

                return AVERROR_INVALIDDATA;

            }

            if ((ret = tiff_unpack_strip(s, p, dst, stride, avpkt->data + soff, ssize, i,

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

                if (avctx->err_recognition & AV_EF_EXPLODE)

                    return ret;

                break;

            }

            dst += s->rps * stride;

        }

        if (s->predictor == 2) {

            if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {

                av_log(s->avctx, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");

                return AVERROR_PATCHWELCOME;

            }

            dst   = p->data[plane];

            soff  = s->bpp >> 3;

            if (s->planar)

                soff  = FFMAX(soff / s->bppcount, 1);

            ssize = s->width * soff;

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

                s->avctx->pix_fmt == AV_PIX_FMT_RGBA64LE ||

                s->avctx->pix_fmt == AV_PIX_FMT_GRAY16LE ||

                s->avctx->pix_fmt == AV_PIX_FMT_YA16LE ||

                s->avctx->pix_fmt == AV_PIX_FMT_GBRP16LE ||

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

                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 == AV_PIX_FMT_RGB48BE ||

                       s->avctx->pix_fmt == AV_PIX_FMT_RGBA64BE ||

                       s->avctx->pix_fmt == AV_PIX_FMT_GRAY16BE ||

                       s->avctx->pix_fmt == AV_PIX_FMT_YA16BE ||

                       s->avctx->pix_fmt == AV_PIX_FMT_GBRP16BE ||

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

                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->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {

            int c = (s->avctx->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255);

            dst = p->data[plane];

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

                for (j = 0; j < stride; j++)

                    dst[j] = c - dst[j];

                dst += stride;

            }

        }

    }



    if (s->planar && s->bppcount > 2) {

        FFSWAP(uint8_t*, p->data[0],     p->data[2]);

        FFSWAP(int,      p->linesize[0], p->linesize[2]);

        FFSWAP(uint8_t*, p->data[0],     p->data[1]);

        FFSWAP(int,      p->linesize[0], p->linesize[1]);

    }



    *got_frame = 1;



    return avpkt->size;

}

            dst += s->rps * stride;
static int FUNC_0(AVCodecContext *VAR_0,

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

{

    TiffContext *const s = VAR_0->priv_data;

    AVFrame *const p = VAR_1;

    ThreadFrame frame = { .f = VAR_1 };

    unsigned VAR_4;

    int VAR_5, VAR_6, VAR_7, VAR_8;

    int VAR_9, VAR_10, VAR_11, VAR_12;

    unsigned VAR_13, VAR_14;

    uint8_t *dst;

    GetByteContext stripsizes;

    GetByteContext stripdata;



    bytestream2_init(&s->gb, VAR_3->VAR_1, VAR_3->size);



    

    if ((VAR_6 = ff_tdecode_header(&s->gb, &VAR_5, &VAR_4))) {

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

        return VAR_6;

    } else if (VAR_4 >= UINT_MAX - 14 || VAR_3->size < VAR_4 + 14) {

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

        return AVERROR_INVALIDDATA;

    }

    s->VAR_5          = VAR_5;

    

    s->bppcount    = s->bpp = 1;

    s->photometric = TIFF_PHOTOMETRIC_NONE;

    s->compr       = TIFF_RAW;

    s->fill_order  = 0;

    free_geotags(s);



    

    s->stripsizesoff = s->strippos = 0;

    

    bytestream2_seek(&s->gb, VAR_4, SEEK_SET);

    VAR_11 = ff_tget_short(&s->gb, VAR_5);

    if (bytestream2_get_bytes_left(&s->gb) < VAR_11 * 12)

        return AVERROR_INVALIDDATA;

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

        if ((VAR_6 = tiff_decode_tag(s, p)) < 0)

            return VAR_6;

    }



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

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

        if (!keyname) {

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

            continue;

        }

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

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

            continue;

        }

        VAR_6 = av_dict_set(&p->metadata, keyname, s->geotags[VAR_9].val, 0);

        if (VAR_6<0) {

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

            return VAR_6;

        }

    }



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

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

        return AVERROR_INVALIDDATA;

    }

    

    if ((VAR_6 = init_image(s, &frame)) < 0)

        return VAR_6;



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

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

        s->stripsize = VAR_3->size - s->stripoff;

    }



    if (s->stripsizesoff) {

        if (s->stripsizesoff >= (unsigned)VAR_3->size)

            return AVERROR_INVALIDDATA;

        bytestream2_init(&stripsizes, VAR_3->VAR_1 + s->stripsizesoff,

                         VAR_3->size - s->stripsizesoff);

    }

    if (s->strippos) {

        if (s->strippos >= (unsigned)VAR_3->size)

            return AVERROR_INVALIDDATA;

        bytestream2_init(&stripdata, VAR_3->VAR_1 + s->strippos,

                         VAR_3->size - s->strippos);

    }



    if (s->rps <= 0 || s->rps % s->subsampling[1]) {

        av_log(VAR_0, AV_LOG_ERROR, "rps %d invalid\n", s->rps);

        return AVERROR_INVALIDDATA;

    }



    VAR_8 = s->planar ? s->bppcount : 1;

    for (VAR_7 = 0; VAR_7 < VAR_8; VAR_7++) {

        VAR_12 = p->linesize[VAR_7];

        dst = p->VAR_1[VAR_7];

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

            if (s->stripsizesoff)

                VAR_14 = ff_tget(&stripsizes, s->sstype, VAR_5);

            else

                VAR_14 = s->stripsize;



            if (s->strippos)

                VAR_13 = ff_tget(&stripdata, s->sot, VAR_5);

            else

                VAR_13 = s->stripoff;



            if (VAR_13 > VAR_3->size || VAR_14 > VAR_3->size - VAR_13) {

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

                return AVERROR_INVALIDDATA;

            }

            if ((VAR_6 = tiff_unpack_strip(s, p, dst, VAR_12, VAR_3->VAR_1 + VAR_13, VAR_14, VAR_9,

                                         FFMIN(s->rps, s->height - VAR_9))) < 0) {

                if (VAR_0->err_recognition & AV_EF_EXPLODE)

                    return VAR_6;

                break;

            }

            dst += s->rps * VAR_12;

        }

        if (s->predictor == 2) {

            if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) {

                av_log(s->VAR_0, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported");

                return AVERROR_PATCHWELCOME;

            }

            dst   = p->VAR_1[VAR_7];

            VAR_13  = s->bpp >> 3;

            if (s->planar)

                VAR_13  = FFMAX(VAR_13 / s->bppcount, 1);

            VAR_14 = s->width * VAR_13;

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

                s->VAR_0->pix_fmt == AV_PIX_FMT_RGBA64LE ||

                s->VAR_0->pix_fmt == AV_PIX_FMT_GRAY16LE ||

                s->VAR_0->pix_fmt == AV_PIX_FMT_YA16LE ||

                s->VAR_0->pix_fmt == AV_PIX_FMT_GBRP16LE ||

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

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

                    for (VAR_10 = VAR_13; VAR_10 < VAR_14; VAR_10 += 2)

                        AV_WL16(dst + VAR_10, AV_RL16(dst + VAR_10) + AV_RL16(dst + VAR_10 - VAR_13));

                    dst += VAR_12;

                }

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

                       s->VAR_0->pix_fmt == AV_PIX_FMT_RGBA64BE ||

                       s->VAR_0->pix_fmt == AV_PIX_FMT_GRAY16BE ||

                       s->VAR_0->pix_fmt == AV_PIX_FMT_YA16BE ||

                       s->VAR_0->pix_fmt == AV_PIX_FMT_GBRP16BE ||

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

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

                    for (VAR_10 = VAR_13; VAR_10 < VAR_14; VAR_10 += 2)

                        AV_WB16(dst + VAR_10, AV_RB16(dst + VAR_10) + AV_RB16(dst + VAR_10 - VAR_13));

                    dst += VAR_12;

                }

            } else {

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

                    for (VAR_10 = VAR_13; VAR_10 < VAR_14; VAR_10++)

                        dst[VAR_10] += dst[VAR_10 - VAR_13];

                    dst += VAR_12;

                }

            }

        }



        if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) {

            int VAR_15 = (s->VAR_0->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255);

            dst = p->VAR_1[VAR_7];

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

                for (VAR_10 = 0; VAR_10 < VAR_12; VAR_10++)

                    dst[VAR_10] = VAR_15 - dst[VAR_10];

                dst += VAR_12;

            }

        }

    }



    if (s->planar && s->bppcount > 2) {

        FFSWAP(uint8_t*, p->VAR_1[0],     p->VAR_1[2]);

        FFSWAP(int,      p->linesize[0], p->linesize[2]);

        FFSWAP(uint8_t*, p->VAR_1[0],     p->VAR_1[1]);

        FFSWAP(int,      p->linesize[0], p->linesize[1]);

    }



    *VAR_2 = 1;



    return VAR_3->size;

}

static int FUNC_0(AVCodecContext *VAR_0,
void *VAR_1, int *VAR_2, AVPacket *VAR_3)
{ TiffContext *const s = VAR_0->priv_data; AVFrame *const p = VAR_1; ThreadFrame frame = { .f = VAR_1 }; unsigned VAR_4; int VAR_5, VAR_6, VAR_7, VAR_8; int VAR_9, VAR_10, VAR_11, VAR_12; unsigned VAR_13, VAR_14; uint8_t *dst; GetByteContext stripsizes; GetByteContext stripdata; bytestream2_init(&s->gb, VAR_3->VAR_1, VAR_3->size); if ((VAR_6 = ff_tdecode_header(&s->gb, &VAR_5, &VAR_4))) { av_log(VAR_0, AV_LOG_ERROR, "Invalid TIFF header\n"); return VAR_6; } else if (VAR_4 >= UINT_MAX - 14 || VAR_3->size < VAR_4 + 14) { av_log(VAR_0, AV_LOG_ERROR, "IFD offset is greater than image size\n"); return AVERROR_INVALIDDATA; } s->VAR_5          = VAR_5; s->bppcount    = s->bpp = 1; s->photometric = TIFF_PHOTOMETRIC_NONE; s->compr       = TIFF_RAW; s->fill_order  = 0; free_geotags(s); s->stripsizesoff = s->strippos = 0; bytestream2_seek(&s->gb, VAR_4, SEEK_SET); VAR_11 = ff_tget_short(&s->gb, VAR_5); if (bytestream2_get_bytes_left(&s->gb) < VAR_11 * 12)
return AVERROR_INVALIDDATA; for (VAR_9 = 0; VAR_9 < VAR_11; VAR_9++) { if ((VAR_6 = tiff_decode_tag(s, p)) < 0)
return VAR_6; } for (VAR_9 = 0; VAR_9<s->geotag_count; VAR_9++) { const char *keyname = get_geokey_name(s->geotags[VAR_9].key); if (!keyname) { av_log(VAR_0, AV_LOG_WARNING, "Unknown or unsupported GeoTIFF key %d\n", s->geotags[VAR_9].key); continue; } if (get_geokey_type(s->geotags[VAR_9].key) != s->geotags[VAR_9].type) { av_log(VAR_0, AV_LOG_WARNING, "Type of GeoTIFF key %d is wrong\n", s->geotags[VAR_9].key); continue; } VAR_6 = av_dict_set(&p->metadata, keyname, s->geotags[VAR_9].val, 0); if (VAR_6<0) { av_log(VAR_0, AV_LOG_ERROR, "Writing metadata with key '%s' failed\n", keyname); return VAR_6; } } if (!s->strippos && !s->stripoff) { av_log(VAR_0, AV_LOG_ERROR, "Image VAR_1 is missing\n"); return AVERROR_INVALIDDATA; } if ((VAR_6 = init_image(s, &frame)) < 0)
return VAR_6; if (s->strips == 1 && !s->stripsize) { av_log(VAR_0, AV_LOG_WARNING, "Image VAR_1 size missing\n"); s->stripsize = VAR_3->size - s->stripoff; } if (s->stripsizesoff) { if (s->stripsizesoff >= (unsigned)VAR_3->size)
return AVERROR_INVALIDDATA; bytestream2_init(&stripsizes, VAR_3->VAR_1 + s->stripsizesoff,
VAR_3->size - s->stripsizesoff); } if (s->strippos) { if (s->strippos >= (unsigned)VAR_3->size)
return AVERROR_INVALIDDATA; bytestream2_init(&stripdata, VAR_3->VAR_1 + s->strippos,
VAR_3->size - s->strippos); } if (s->rps <= 0 || s->rps % s->subsampling[1]) { av_log(VAR_0, AV_LOG_ERROR, "rps %d invalid\n", s->rps); return AVERROR_INVALIDDATA; } VAR_8 = s->planar ? s->bppcount : 1; for (VAR_7 = 0; VAR_7 < VAR_8; VAR_7++) { VAR_12 = p->linesize[VAR_7]; dst = p->VAR_1[VAR_7]; for (VAR_9 = 0; VAR_9 < s->height; VAR_9 += s->rps) { if (s->stripsizesoff)
VAR_14 = ff_tget(&stripsizes, s->sstype, VAR_5); else
VAR_14 = s->stripsize; if (s->strippos)
VAR_13 = ff_tget(&stripdata, s->sot, VAR_5); else
VAR_13 = s->stripoff; if (VAR_13 > VAR_3->size || VAR_14 > VAR_3->size - VAR_13) { av_log(VAR_0, AV_LOG_ERROR, "Invalid strip size/offset\n"); return AVERROR_INVALIDDATA; } if ((VAR_6 = tiff_unpack_strip(s, p, dst, VAR_12, VAR_3->VAR_1 + VAR_13, VAR_14, VAR_9,
FFMIN(s->rps, s->height - VAR_9))) < 0) { if (VAR_0->err_recognition & AV_EF_EXPLODE)
return VAR_6; break; } dst += s->rps * VAR_12; } if (s->predictor == 2) { if (s->photometric == TIFF_PHOTOMETRIC_YCBCR) { av_log(s->VAR_0, AV_LOG_ERROR, "predictor == 2 with YUV is unsupported"); return AVERROR_PATCHWELCOME; } dst   = p->VAR_1[VAR_7]; VAR_13  = s->bpp >> 3; if (s->planar)
VAR_13  = FFMAX(VAR_13 / s->bppcount, 1); VAR_14 = s->width * VAR_13; if (s->VAR_0->pix_fmt == AV_PIX_FMT_RGB48LE ||
s->VAR_0->pix_fmt == AV_PIX_FMT_RGBA64LE ||
s->VAR_0->pix_fmt == AV_PIX_FMT_GRAY16LE ||
s->VAR_0->pix_fmt == AV_PIX_FMT_YA16LE ||
s->VAR_0->pix_fmt == AV_PIX_FMT_GBRP16LE ||
s->VAR_0->pix_fmt == AV_PIX_FMT_GBRAP16LE) { for (VAR_9 = 0; VAR_9 < s->height; VAR_9++) { for (VAR_10 = VAR_13; VAR_10 < VAR_14; VAR_10 += 2) AV_WL16(dst + VAR_10, AV_RL16(dst + VAR_10) + AV_RL16(dst + VAR_10 - VAR_13)); dst += VAR_12; } } else if (s->VAR_0->pix_fmt == AV_PIX_FMT_RGB48BE || s->VAR_0->pix_fmt == AV_PIX_FMT_RGBA64BE ||
s->VAR_0->pix_fmt == AV_PIX_FMT_GRAY16BE ||
s->VAR_0->pix_fmt == AV_PIX_FMT_YA16BE ||
s->VAR_0->pix_fmt == AV_PIX_FMT_GBRP16BE ||
s->VAR_0->pix_fmt == AV_PIX_FMT_GBRAP16BE) { for (VAR_9 = 0; VAR_9 < s->height; VAR_9++) { for (VAR_10 = VAR_13; VAR_10 < VAR_14; VAR_10 += 2) AV_WB16(dst + VAR_10, AV_RB16(dst + VAR_10) + AV_RB16(dst + VAR_10 - VAR_13)); dst += VAR_12; } } else { for (VAR_9 = 0; VAR_9 < s->height; VAR_9++) { for (VAR_10 = VAR_13; VAR_10 < VAR_14; VAR_10++) dst[VAR_10] += dst[VAR_10 - VAR_13]; dst += VAR_12; } } } if (s->photometric == TIFF_PHOTOMETRIC_WHITE_IS_ZERO) { int VAR_15 = (s->VAR_0->pix_fmt == AV_PIX_FMT_PAL8 ? (1<<s->bpp) - 1 : 255); dst = p->VAR_1[VAR_7]; for (VAR_9 = 0; VAR_9 < s->height; VAR_9++) { for (VAR_10 = 0; VAR_10 < VAR_12; VAR_10++) dst[VAR_10] = VAR_15 - dst[VAR_10]; dst += VAR_12; } } } if (s->planar && s->bppcount > 2) { FFSWAP(uint8_t*, p->VAR_1[0],     p->VAR_1[2]); FFSWAP(int,      p->linesize[0], p->linesize[2]); FFSWAP(uint8_t*, p->VAR_1[0],     p->VAR_1[1]); FFSWAP(int,      p->linesize[0], p->linesize[1]); } *VAR_2 = 1; return VAR_3->size; }