static int dds_decode(AVCodecContext *avctx, void *data,

                      int *got_frame, AVPacket *avpkt)

{

    DDSContext *ctx = avctx->priv_data;

    GetByteContext *gbc = &ctx->gbc;

    AVFrame *frame = data;

    int mipmap;

    int ret;



    ff_texturedsp_init(&ctx->texdsp);

    bytestream2_init(gbc, avpkt->data, avpkt->size);



    if (bytestream2_get_bytes_left(gbc) < 128) {

        av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n",

               bytestream2_get_bytes_left(gbc));

        return AVERROR_INVALIDDATA;

    }



    if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') ||

        bytestream2_get_le32(gbc) != 124) { // header size

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

        return AVERROR_INVALIDDATA;

    }



    bytestream2_skip(gbc, 4); // flags



    avctx->height = bytestream2_get_le32(gbc);

    avctx->width  = bytestream2_get_le32(gbc);

    ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);

    if (ret < 0) {

        av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",

               avctx->width, avctx->height);

        return ret;

    }



    /* Since codec is based on 4x4 blocks, size is aligned to 4. */

    avctx->coded_width  = FFALIGN(avctx->width,  TEXTURE_BLOCK_W);

    avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);



    bytestream2_skip(gbc, 4); // pitch

    bytestream2_skip(gbc, 4); // depth

    mipmap = bytestream2_get_le32(gbc);

    if (mipmap != 0)

        av_log(avctx, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", mipmap);



    /* Extract pixel format information, considering additional elements

     * in reserved1 and reserved2. */

    ret = parse_pixel_format(avctx);

    if (ret < 0)

        return ret;



    ret = ff_get_buffer(avctx, frame, 0);

    if (ret < 0)

        return ret;



    if (ctx->compressed) {

        int size = (avctx->coded_height / TEXTURE_BLOCK_H) *

                   (avctx->coded_width / TEXTURE_BLOCK_W) * ctx->tex_ratio;

        ctx->slice_count = av_clip(avctx->thread_count, 1,

                                   avctx->coded_height / TEXTURE_BLOCK_H);



        if (bytestream2_get_bytes_left(gbc) < size) {

            av_log(avctx, AV_LOG_ERROR,

                   "Compressed Buffer is too small (%d < %d).\n",

                   bytestream2_get_bytes_left(gbc), size);

            return AVERROR_INVALIDDATA;

        }



        /* Use the decompress function on the texture, one block per thread. */

        ctx->tex_data = gbc->buffer;

        avctx->execute2(avctx, decompress_texture_thread, frame, NULL, ctx->slice_count);

    } else if (!ctx->paletted && ctx->bpp == 4 && avctx->pix_fmt == AV_PIX_FMT_PAL8) {

        uint8_t *dst = frame->data[0];

        int x, y, i;



        /* Use the first 64 bytes as palette, then copy the rest. */

        bytestream2_get_buffer(gbc, frame->data[1], 16 * 4);

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

            AV_WN32(frame->data[1] + i*4,

                    (frame->data[1][2+i*4]<<0)+

                    (frame->data[1][1+i*4]<<8)+

                    (frame->data[1][0+i*4]<<16)+

                    (frame->data[1][3+i*4]<<24)

            );

        }

        frame->palette_has_changed = 1;



        if (bytestream2_get_bytes_left(gbc) < frame->height * frame->width / 2) {

            av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",

                   bytestream2_get_bytes_left(gbc), frame->height * frame->width / 2);

            return AVERROR_INVALIDDATA;

        }



        for (y = 0; y < frame->height; y++) {

            for (x = 0; x < frame->width; x += 2) {

                uint8_t val = bytestream2_get_byte(gbc);

                dst[x    ] = val & 0xF;

                dst[x + 1] = val >> 4;

            }

            dst += frame->linesize[0];

        }

    } else {

        int linesize = av_image_get_linesize(avctx->pix_fmt, frame->width, 0);



        if (ctx->paletted) {

            int i;

            /* Use the first 1024 bytes as palette, then copy the rest. */

            bytestream2_get_buffer(gbc, frame->data[1], 256 * 4);

            for (i = 0; i < 256; i++)

                AV_WN32(frame->data[1] + i*4,

                        (frame->data[1][2+i*4]<<0)+

                        (frame->data[1][1+i*4]<<8)+

                        (frame->data[1][0+i*4]<<16)+

                        (frame->data[1][3+i*4]<<24)

                );



            frame->palette_has_changed = 1;

        }



        if (bytestream2_get_bytes_left(gbc) < frame->height * linesize) {

            av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",

                   bytestream2_get_bytes_left(gbc), frame->height * linesize);

            return AVERROR_INVALIDDATA;

        }



        av_image_copy_plane(frame->data[0], frame->linesize[0],

                            gbc->buffer, linesize,

                            linesize, frame->height);

    }



    /* Run any post processing here if needed. */

    if (ctx->postproc != DDS_NONE)

        run_postproc(avctx, frame);



    /* Frame is ready to be output. */

    frame->pict_type = AV_PICTURE_TYPE_I;

    frame->key_frame = 1;

    *got_frame = 1;



    return avpkt->size;

}

static int dds_decode(AVCodecContext *avctx, void *data,

                      int *got_frame, AVPacket *avpkt)

{

    DDSContext *ctx = avctx->priv_data;

    GetByteContext *gbc = &ctx->gbc;

    AVFrame *frame = data;

    int mipmap;

    int ret;



    ff_texturedsp_init(&ctx->texdsp);

    bytestream2_init(gbc, avpkt->data, avpkt->size);



    if (bytestream2_get_bytes_left(gbc) < 128) {

        av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n",

               bytestream2_get_bytes_left(gbc));

        return AVERROR_INVALIDDATA;

    }



    if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') ||

        bytestream2_get_le32(gbc) != 124) { 

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

        return AVERROR_INVALIDDATA;

    }



    bytestream2_skip(gbc, 4); 



    avctx->height = bytestream2_get_le32(gbc);

    avctx->width  = bytestream2_get_le32(gbc);

    ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);

    if (ret < 0) {

        av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n",

               avctx->width, avctx->height);

        return ret;

    }



    

    avctx->coded_width  = FFALIGN(avctx->width,  TEXTURE_BLOCK_W);

    avctx->coded_height = FFALIGN(avctx->height, TEXTURE_BLOCK_H);



    bytestream2_skip(gbc, 4); 

    bytestream2_skip(gbc, 4); 

    mipmap = bytestream2_get_le32(gbc);

    if (mipmap != 0)

        av_log(avctx, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", mipmap);



    



    ret = parse_pixel_format(avctx);

    if (ret < 0)

        return ret;



    ret = ff_get_buffer(avctx, frame, 0);

    if (ret < 0)

        return ret;



    if (ctx->compressed) {

        int size = (avctx->coded_height / TEXTURE_BLOCK_H) *

                   (avctx->coded_width / TEXTURE_BLOCK_W) * ctx->tex_ratio;

        ctx->slice_count = av_clip(avctx->thread_count, 1,

                                   avctx->coded_height / TEXTURE_BLOCK_H);



        if (bytestream2_get_bytes_left(gbc) < size) {

            av_log(avctx, AV_LOG_ERROR,

                   "Compressed Buffer is too small (%d < %d).\n",

                   bytestream2_get_bytes_left(gbc), size);

            return AVERROR_INVALIDDATA;

        }



        

        ctx->tex_data = gbc->buffer;

        avctx->execute2(avctx, decompress_texture_thread, frame, NULL, ctx->slice_count);

    } else if (!ctx->paletted && ctx->bpp == 4 && avctx->pix_fmt == AV_PIX_FMT_PAL8) {

        uint8_t *dst = frame->data[0];

        int x, y, i;



        

        bytestream2_get_buffer(gbc, frame->data[1], 16 * 4);

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

            AV_WN32(frame->data[1] + i*4,

                    (frame->data[1][2+i*4]<<0)+

                    (frame->data[1][1+i*4]<<8)+

                    (frame->data[1][0+i*4]<<16)+

                    (frame->data[1][3+i*4]<<24)

            );

        }

        frame->palette_has_changed = 1;



        if (bytestream2_get_bytes_left(gbc) < frame->height * frame->width / 2) {

            av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",

                   bytestream2_get_bytes_left(gbc), frame->height * frame->width / 2);

            return AVERROR_INVALIDDATA;

        }



        for (y = 0; y < frame->height; y++) {

            for (x = 0; x < frame->width; x += 2) {

                uint8_t val = bytestream2_get_byte(gbc);

                dst[x    ] = val & 0xF;

                dst[x + 1] = val >> 4;

            }

            dst += frame->linesize[0];

        }

    } else {

        int linesize = av_image_get_linesize(avctx->pix_fmt, frame->width, 0);



        if (ctx->paletted) {

            int i;

            

            bytestream2_get_buffer(gbc, frame->data[1], 256 * 4);

            for (i = 0; i < 256; i++)

                AV_WN32(frame->data[1] + i*4,

                        (frame->data[1][2+i*4]<<0)+

                        (frame->data[1][1+i*4]<<8)+

                        (frame->data[1][0+i*4]<<16)+

                        (frame->data[1][3+i*4]<<24)

                );



            frame->palette_has_changed = 1;

        }



        if (bytestream2_get_bytes_left(gbc) < frame->height * linesize) {

            av_log(avctx, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",

                   bytestream2_get_bytes_left(gbc), frame->height * linesize);

            return AVERROR_INVALIDDATA;

        }



        av_image_copy_plane(frame->data[0], frame->linesize[0],

                            gbc->buffer, linesize,

                            linesize, frame->height);

    }



    

    if (ctx->postproc != DDS_NONE)

        run_postproc(avctx, frame);



    

    frame->pict_type = AV_PICTURE_TYPE_I;

    frame->key_frame = 1;

    *got_frame = 1;



    return avpkt->size;

}

                        (frame->data[1][3+i*4]<<24)
static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1,

                      int *VAR_2, AVPacket *VAR_3)

{

    DDSContext *ctx = VAR_0->priv_data;

    GetByteContext *gbc = &ctx->gbc;

    AVFrame *frame = VAR_1;

    int VAR_4;

    int VAR_5;



    ff_texturedsp_init(&ctx->texdsp);

    bytestream2_init(gbc, VAR_3->VAR_1, VAR_3->VAR_6);



    if (bytestream2_get_bytes_left(gbc) < 128) {

        av_log(VAR_0, AV_LOG_ERROR, "Frame is too small (%d).\n",

               bytestream2_get_bytes_left(gbc));

        return AVERROR_INVALIDDATA;

    }



    if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') ||

        bytestream2_get_le32(gbc) != 124) { 

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

        return AVERROR_INVALIDDATA;

    }



    bytestream2_skip(gbc, 4); 



    VAR_0->height = bytestream2_get_le32(gbc);

    VAR_0->width  = bytestream2_get_le32(gbc);

    VAR_5 = av_image_check_size(VAR_0->width, VAR_0->height, 0, VAR_0);

    if (VAR_5 < 0) {

        av_log(VAR_0, AV_LOG_ERROR, "Invalid image VAR_6 %dx%d.\n",

               VAR_0->width, VAR_0->height);

        return VAR_5;

    }



    

    VAR_0->coded_width  = FFALIGN(VAR_0->width,  TEXTURE_BLOCK_W);

    VAR_0->coded_height = FFALIGN(VAR_0->height, TEXTURE_BLOCK_H);



    bytestream2_skip(gbc, 4); 

    bytestream2_skip(gbc, 4); 

    VAR_4 = bytestream2_get_le32(gbc);

    if (VAR_4 != 0)

        av_log(VAR_0, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", VAR_4);



    



    VAR_5 = parse_pixel_format(VAR_0);

    if (VAR_5 < 0)

        return VAR_5;



    VAR_5 = ff_get_buffer(VAR_0, frame, 0);

    if (VAR_5 < 0)

        return VAR_5;



    if (ctx->compressed) {

        int VAR_6 = (VAR_0->coded_height / TEXTURE_BLOCK_H) *

                   (VAR_0->coded_width / TEXTURE_BLOCK_W) * ctx->tex_ratio;

        ctx->slice_count = av_clip(VAR_0->thread_count, 1,

                                   VAR_0->coded_height / TEXTURE_BLOCK_H);



        if (bytestream2_get_bytes_left(gbc) < VAR_6) {

            av_log(VAR_0, AV_LOG_ERROR,

                   "Compressed Buffer is too small (%d < %d).\n",

                   bytestream2_get_bytes_left(gbc), VAR_6);

            return AVERROR_INVALIDDATA;

        }



        

        ctx->tex_data = gbc->buffer;

        VAR_0->execute2(VAR_0, decompress_texture_thread, frame, NULL, ctx->slice_count);

    } else if (!ctx->paletted && ctx->bpp == 4 && VAR_0->pix_fmt == AV_PIX_FMT_PAL8) {

        uint8_t *dst = frame->VAR_1[0];

        int VAR_7, VAR_8, VAR_11;



        

        bytestream2_get_buffer(gbc, frame->VAR_1[1], 16 * 4);

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

            AV_WN32(frame->VAR_1[1] + VAR_11*4,

                    (frame->VAR_1[1][2+VAR_11*4]<<0)+

                    (frame->VAR_1[1][1+VAR_11*4]<<8)+

                    (frame->VAR_1[1][0+VAR_11*4]<<16)+

                    (frame->VAR_1[1][3+VAR_11*4]<<24)

            );

        }

        frame->palette_has_changed = 1;



        if (bytestream2_get_bytes_left(gbc) < frame->height * frame->width / 2) {

            av_log(VAR_0, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",

                   bytestream2_get_bytes_left(gbc), frame->height * frame->width / 2);

            return AVERROR_INVALIDDATA;

        }



        for (VAR_8 = 0; VAR_8 < frame->height; VAR_8++) {

            for (VAR_7 = 0; VAR_7 < frame->width; VAR_7 += 2) {

                uint8_t val = bytestream2_get_byte(gbc);

                dst[VAR_7    ] = val & 0xF;

                dst[VAR_7 + 1] = val >> 4;

            }

            dst += frame->VAR_10[0];

        }

    } else {

        int VAR_10 = av_image_get_linesize(VAR_0->pix_fmt, frame->width, 0);



        if (ctx->paletted) {

            int VAR_11;

            

            bytestream2_get_buffer(gbc, frame->VAR_1[1], 256 * 4);

            for (VAR_11 = 0; VAR_11 < 256; VAR_11++)

                AV_WN32(frame->VAR_1[1] + VAR_11*4,

                        (frame->VAR_1[1][2+VAR_11*4]<<0)+

                        (frame->VAR_1[1][1+VAR_11*4]<<8)+

                        (frame->VAR_1[1][0+VAR_11*4]<<16)+

                        (frame->VAR_1[1][3+VAR_11*4]<<24)

                );



            frame->palette_has_changed = 1;

        }



        if (bytestream2_get_bytes_left(gbc) < frame->height * VAR_10) {

            av_log(VAR_0, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",

                   bytestream2_get_bytes_left(gbc), frame->height * VAR_10);

            return AVERROR_INVALIDDATA;

        }



        av_image_copy_plane(frame->VAR_1[0], frame->VAR_10[0],

                            gbc->buffer, VAR_10,

                            VAR_10, frame->height);

    }



    

    if (ctx->postproc != DDS_NONE)

        run_postproc(VAR_0, frame);



    

    frame->pict_type = AV_PICTURE_TYPE_I;

    frame->key_frame = 1;

    *VAR_2 = 1;



    return VAR_3->VAR_6;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1,
int *VAR_2, AVPacket *VAR_3)
{ DDSContext *ctx = VAR_0->priv_data; GetByteContext *gbc = &ctx->gbc; AVFrame *frame = VAR_1; int VAR_4; int VAR_5; ff_texturedsp_init(&ctx->texdsp); bytestream2_init(gbc, VAR_3->VAR_1, VAR_3->VAR_6); if (bytestream2_get_bytes_left(gbc) < 128) { av_log(VAR_0, AV_LOG_ERROR, "Frame is too small (%d).\n",
bytestream2_get_bytes_left(gbc)); return AVERROR_INVALIDDATA; } if (bytestream2_get_le32(gbc) != MKTAG('D', 'D', 'S', ' ') ||
bytestream2_get_le32(gbc) != 124) { av_log(VAR_0, AV_LOG_ERROR, "Invalid DDS header.\n"); return AVERROR_INVALIDDATA; } bytestream2_skip(gbc, 4); VAR_0->height = bytestream2_get_le32(gbc); VAR_0->width  = bytestream2_get_le32(gbc); VAR_5 = av_image_check_size(VAR_0->width, VAR_0->height, 0, VAR_0); if (VAR_5 < 0) { av_log(VAR_0, AV_LOG_ERROR, "Invalid image VAR_6 %dx%d.\n",
VAR_0->width, VAR_0->height); return VAR_5; } VAR_0->coded_width  = FFALIGN(VAR_0->width,  TEXTURE_BLOCK_W); VAR_0->coded_height = FFALIGN(VAR_0->height, TEXTURE_BLOCK_H); bytestream2_skip(gbc, 4); bytestream2_skip(gbc, 4); VAR_4 = bytestream2_get_le32(gbc); if (VAR_4 != 0)
av_log(VAR_0, AV_LOG_VERBOSE, "Found %d mipmaps (ignored).\n", VAR_4); VAR_5 = parse_pixel_format(VAR_0); if (VAR_5 < 0)
return VAR_5; VAR_5 = ff_get_buffer(VAR_0, frame, 0); if (VAR_5 < 0)
return VAR_5; if (ctx->compressed) { int VAR_6 = (VAR_0->coded_height / TEXTURE_BLOCK_H) *
(VAR_0->coded_width / TEXTURE_BLOCK_W) * ctx->tex_ratio; ctx->slice_count = av_clip(VAR_0->thread_count, 1,
VAR_0->coded_height / TEXTURE_BLOCK_H); if (bytestream2_get_bytes_left(gbc) < VAR_6) { av_log(VAR_0, AV_LOG_ERROR,
"Compressed Buffer is too small (%d < %d).\n",
bytestream2_get_bytes_left(gbc), VAR_6); return AVERROR_INVALIDDATA; } ctx->tex_data = gbc->buffer; VAR_0->execute2(VAR_0, decompress_texture_thread, frame, NULL, ctx->slice_count); } else if (!ctx->paletted && ctx->bpp == 4 && VAR_0->pix_fmt == AV_PIX_FMT_PAL8) { uint8_t *dst = frame->VAR_1[0]; int VAR_7, VAR_8, VAR_11; bytestream2_get_buffer(gbc, frame->VAR_1[1], 16 * 4); for (VAR_11 = 0; VAR_11 < 16; VAR_11++) { AV_WN32(frame->VAR_1[1] + VAR_11*4,
(frame->VAR_1[1][2+VAR_11*4]<<0)+
(frame->VAR_1[1][1+VAR_11*4]<<8)+
(frame->VAR_1[1][0+VAR_11*4]<<16)+
(frame->VAR_1[1][3+VAR_11*4]<<24)
); } frame->palette_has_changed = 1; if (bytestream2_get_bytes_left(gbc) < frame->height * frame->width / 2) { av_log(VAR_0, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",
bytestream2_get_bytes_left(gbc), frame->height * frame->width / 2); return AVERROR_INVALIDDATA; } for (VAR_8 = 0; VAR_8 < frame->height; VAR_8++) { for (VAR_7 = 0; VAR_7 < frame->width; VAR_7 += 2) { uint8_t val = bytestream2_get_byte(gbc); dst[VAR_7    ] = val & 0xF; dst[VAR_7 + 1] = val >> 4; } dst += frame->VAR_10[0]; } } else { int VAR_10 = av_image_get_linesize(VAR_0->pix_fmt, frame->width, 0); if (ctx->paletted) { int VAR_11; bytestream2_get_buffer(gbc, frame->VAR_1[1], 256 * 4); for (VAR_11 = 0; VAR_11 < 256; VAR_11++) AV_WN32(frame->VAR_1[1] + VAR_11*4,
(frame->VAR_1[1][2+VAR_11*4]<<0)+
(frame->VAR_1[1][1+VAR_11*4]<<8)+
(frame->VAR_1[1][0+VAR_11*4]<<16)+
(frame->VAR_1[1][3+VAR_11*4]<<24)
); frame->palette_has_changed = 1; } if (bytestream2_get_bytes_left(gbc) < frame->height * VAR_10) { av_log(VAR_0, AV_LOG_ERROR, "Buffer is too small (%d < %d).\n",
bytestream2_get_bytes_left(gbc), frame->height * VAR_10); return AVERROR_INVALIDDATA; } av_image_copy_plane(frame->VAR_1[0], frame->VAR_10[0],
gbc->buffer, VAR_10,
VAR_10, frame->height); } if (ctx->postproc != DDS_NONE)
run_postproc(VAR_0, frame); frame->pict_type = AV_PICTURE_TYPE_I; frame->key_frame = 1; *VAR_2 = 1; return VAR_3->VAR_6; }