static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,

                               const int data_size, AVCodecContext *avctx)

{

    int hdr_size, width, height, flags;

    int version;

    const uint8_t *ptr;



    hdr_size = AV_RB16(buf);

    av_dlog(avctx, "header size %d\n", hdr_size);

    if (hdr_size > data_size) {

        av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n");

        return AVERROR_INVALIDDATA;

    }



    version = AV_RB16(buf + 2);

    av_dlog(avctx, "%.4s version %d\n", buf+4, version);

    if (version > 1) {

        av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version);

        return AVERROR_PATCHWELCOME;

    }



    width  = AV_RB16(buf + 8);

    height = AV_RB16(buf + 10);

    if (width != avctx->width || height != avctx->height) {

        av_log(avctx, AV_LOG_ERROR, "picture resolution change: %dx%d -> %dx%d\n",

               avctx->width, avctx->height, width, height);

        return AVERROR_PATCHWELCOME;

    }



    ctx->frame_type = (buf[12] >> 2) & 3;

    ctx->alpha_info = buf[17] & 0xf;



    if (ctx->alpha_info > 2) {

        av_log(avctx, AV_LOG_ERROR, "Invalid alpha mode %d\n", ctx->alpha_info);

        return AVERROR_INVALIDDATA;

    }

    if (avctx->skip_alpha) ctx->alpha_info = 0;



    av_dlog(avctx, "frame type %d\n", ctx->frame_type);



    if (ctx->frame_type == 0) {

        ctx->scan = ctx->progressive_scan; // permuted

    } else {

        ctx->scan = ctx->interlaced_scan; // permuted

        ctx->frame->interlaced_frame = 1;

        ctx->frame->top_field_first = ctx->frame_type == 1;

    }



    if (ctx->alpha_info) {

        avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P10 : AV_PIX_FMT_YUVA422P10;

    } else {

        avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10;

    }



    ptr   = buf + 20;

    flags = buf[19];

    av_dlog(avctx, "flags %x\n", flags);



    if (flags & 2) {

        if(buf + data_size - ptr < 64) {

            av_log(avctx, AV_LOG_ERROR, "Header truncated\n");

            return AVERROR_INVALIDDATA;

        }

        permute(ctx->qmat_luma, ctx->prodsp.idct_permutation, ptr);

        ptr += 64;

    } else {

        memset(ctx->qmat_luma, 4, 64);

    }



    if (flags & 1) {

        if(buf + data_size - ptr < 64) {

            av_log(avctx, AV_LOG_ERROR, "Header truncated\n");

            return AVERROR_INVALIDDATA;

        }

        permute(ctx->qmat_chroma, ctx->prodsp.idct_permutation, ptr);

    } else {

        memset(ctx->qmat_chroma, 4, 64);

    }



    return hdr_size;

}

static int decode_frame_header(ProresContext *ctx, const uint8_t *buf,

                               const int data_size, AVCodecContext *avctx)

{

    int hdr_size, width, height, flags;

    int version;

    const uint8_t *ptr;



    hdr_size = AV_RB16(buf);

    av_dlog(avctx, "header size %d\n", hdr_size);

    if (hdr_size > data_size) {

        av_log(avctx, AV_LOG_ERROR, "error, wrong header size\n");

        return AVERROR_INVALIDDATA;

    }



    version = AV_RB16(buf + 2);

    av_dlog(avctx, "%.4s version %d\n", buf+4, version);

    if (version > 1) {

        av_log(avctx, AV_LOG_ERROR, "unsupported version: %d\n", version);

        return AVERROR_PATCHWELCOME;

    }



    width  = AV_RB16(buf + 8);

    height = AV_RB16(buf + 10);

    if (width != avctx->width || height != avctx->height) {

        av_log(avctx, AV_LOG_ERROR, "picture resolution change: %dx%d -> %dx%d\n",

               avctx->width, avctx->height, width, height);

        return AVERROR_PATCHWELCOME;

    }



    ctx->frame_type = (buf[12] >> 2) & 3;

    ctx->alpha_info = buf[17] & 0xf;



    if (ctx->alpha_info > 2) {

        av_log(avctx, AV_LOG_ERROR, "Invalid alpha mode %d\n", ctx->alpha_info);

        return AVERROR_INVALIDDATA;

    }

    if (avctx->skip_alpha) ctx->alpha_info = 0;



    av_dlog(avctx, "frame type %d\n", ctx->frame_type);



    if (ctx->frame_type == 0) {

        ctx->scan = ctx->progressive_scan; 

    } else {

        ctx->scan = ctx->interlaced_scan; 

        ctx->frame->interlaced_frame = 1;

        ctx->frame->top_field_first = ctx->frame_type == 1;

    }



    if (ctx->alpha_info) {

        avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P10 : AV_PIX_FMT_YUVA422P10;

    } else {

        avctx->pix_fmt = (buf[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10;

    }



    ptr   = buf + 20;

    flags = buf[19];

    av_dlog(avctx, "flags %x\n", flags);



    if (flags & 2) {

        if(buf + data_size - ptr < 64) {

            av_log(avctx, AV_LOG_ERROR, "Header truncated\n");

            return AVERROR_INVALIDDATA;

        }

        permute(ctx->qmat_luma, ctx->prodsp.idct_permutation, ptr);

        ptr += 64;

    } else {

        memset(ctx->qmat_luma, 4, 64);

    }



    if (flags & 1) {

        if(buf + data_size - ptr < 64) {

            av_log(avctx, AV_LOG_ERROR, "Header truncated\n");

            return AVERROR_INVALIDDATA;

        }

        permute(ctx->qmat_chroma, ctx->prodsp.idct_permutation, ptr);

    } else {

        memset(ctx->qmat_chroma, 4, 64);

    }



    return hdr_size;

}

static int FUNC_0(ProresContext *VAR_0, const uint8_t *VAR_1,

                               const int VAR_2, AVCodecContext *VAR_3)

{

    int VAR_4, VAR_5, VAR_6, VAR_7;

    int VAR_8;

    const uint8_t *VAR_9;



    VAR_4 = AV_RB16(VAR_1);

    av_dlog(VAR_3, "header size %d\n", VAR_4);

    if (VAR_4 > VAR_2) {

        av_log(VAR_3, AV_LOG_ERROR, "error, wrong header size\n");

        return AVERROR_INVALIDDATA;

    }



    VAR_8 = AV_RB16(VAR_1 + 2);

    av_dlog(VAR_3, "%.4s VAR_8 %d\n", VAR_1+4, VAR_8);

    if (VAR_8 > 1) {

        av_log(VAR_3, AV_LOG_ERROR, "unsupported VAR_8: %d\n", VAR_8);

        return AVERROR_PATCHWELCOME;

    }



    VAR_5  = AV_RB16(VAR_1 + 8);

    VAR_6 = AV_RB16(VAR_1 + 10);

    if (VAR_5 != VAR_3->VAR_5 || VAR_6 != VAR_3->VAR_6) {

        av_log(VAR_3, AV_LOG_ERROR, "picture resolution change: %dx%d -> %dx%d\n",

               VAR_3->VAR_5, VAR_3->VAR_6, VAR_5, VAR_6);

        return AVERROR_PATCHWELCOME;

    }



    VAR_0->frame_type = (VAR_1[12] >> 2) & 3;

    VAR_0->alpha_info = VAR_1[17] & 0xf;



    if (VAR_0->alpha_info > 2) {

        av_log(VAR_3, AV_LOG_ERROR, "Invalid alpha mode %d\n", VAR_0->alpha_info);

        return AVERROR_INVALIDDATA;

    }

    if (VAR_3->skip_alpha) VAR_0->alpha_info = 0;



    av_dlog(VAR_3, "frame type %d\n", VAR_0->frame_type);



    if (VAR_0->frame_type == 0) {

        VAR_0->scan = VAR_0->progressive_scan; 

    } else {

        VAR_0->scan = VAR_0->interlaced_scan; 

        VAR_0->frame->interlaced_frame = 1;

        VAR_0->frame->top_field_first = VAR_0->frame_type == 1;

    }



    if (VAR_0->alpha_info) {

        VAR_3->pix_fmt = (VAR_1[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P10 : AV_PIX_FMT_YUVA422P10;

    } else {

        VAR_3->pix_fmt = (VAR_1[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10;

    }



    VAR_9   = VAR_1 + 20;

    VAR_7 = VAR_1[19];

    av_dlog(VAR_3, "VAR_7 %x\n", VAR_7);



    if (VAR_7 & 2) {

        if(VAR_1 + VAR_2 - VAR_9 < 64) {

            av_log(VAR_3, AV_LOG_ERROR, "Header truncated\n");

            return AVERROR_INVALIDDATA;

        }

        permute(VAR_0->qmat_luma, VAR_0->prodsp.idct_permutation, VAR_9);

        VAR_9 += 64;

    } else {

        memset(VAR_0->qmat_luma, 4, 64);

    }



    if (VAR_7 & 1) {

        if(VAR_1 + VAR_2 - VAR_9 < 64) {

            av_log(VAR_3, AV_LOG_ERROR, "Header truncated\n");

            return AVERROR_INVALIDDATA;

        }

        permute(VAR_0->qmat_chroma, VAR_0->prodsp.idct_permutation, VAR_9);

    } else {

        memset(VAR_0->qmat_chroma, 4, 64);

    }



    return VAR_4;

}

static int FUNC_0(ProresContext *VAR_0, const uint8_t *VAR_1,
const int VAR_2, AVCodecContext *VAR_3)
{ int VAR_4, VAR_5, VAR_6, VAR_7; int VAR_8; const uint8_t *VAR_9; VAR_4 = AV_RB16(VAR_1); av_dlog(VAR_3, "header size %d\n", VAR_4); if (VAR_4 > VAR_2) { av_log(VAR_3, AV_LOG_ERROR, "error, wrong header size\n"); return AVERROR_INVALIDDATA; } VAR_8 = AV_RB16(VAR_1 + 2); av_dlog(VAR_3, "%.4s VAR_8 %d\n", VAR_1+4, VAR_8); if (VAR_8 > 1) { av_log(VAR_3, AV_LOG_ERROR, "unsupported VAR_8: %d\n", VAR_8); return AVERROR_PATCHWELCOME; } VAR_5  = AV_RB16(VAR_1 + 8); VAR_6 = AV_RB16(VAR_1 + 10); if (VAR_5 != VAR_3->VAR_5 || VAR_6 != VAR_3->VAR_6) { av_log(VAR_3, AV_LOG_ERROR, "picture resolution change: %dx%d -> %dx%d\n",
VAR_3->VAR_5, VAR_3->VAR_6, VAR_5, VAR_6); return AVERROR_PATCHWELCOME; } VAR_0->frame_type = (VAR_1[12] >> 2) & 3; VAR_0->alpha_info = VAR_1[17] & 0xf; if (VAR_0->alpha_info > 2) { av_log(VAR_3, AV_LOG_ERROR, "Invalid alpha mode %d\n", VAR_0->alpha_info); return AVERROR_INVALIDDATA; } if (VAR_3->skip_alpha) VAR_0->alpha_info = 0; av_dlog(VAR_3, "frame type %d\n", VAR_0->frame_type); if (VAR_0->frame_type == 0) { VAR_0->scan = VAR_0->progressive_scan; } else { VAR_0->scan = VAR_0->interlaced_scan; VAR_0->frame->interlaced_frame = 1; VAR_0->frame->top_field_first = VAR_0->frame_type == 1; } if (VAR_0->alpha_info) { VAR_3->pix_fmt = (VAR_1[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUVA444P10 : AV_PIX_FMT_YUVA422P10; } else { VAR_3->pix_fmt = (VAR_1[12] & 0xC0) == 0xC0 ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV422P10; } VAR_9   = VAR_1 + 20; VAR_7 = VAR_1[19]; av_dlog(VAR_3, "VAR_7 %x\n", VAR_7); if (VAR_7 & 2) { if(VAR_1 + VAR_2 - VAR_9 < 64) { av_log(VAR_3, AV_LOG_ERROR, "Header truncated\n"); return AVERROR_INVALIDDATA; } permute(VAR_0->qmat_luma, VAR_0->prodsp.idct_permutation, VAR_9); VAR_9 += 64; } else { memset(VAR_0->qmat_luma, 4, 64); } if (VAR_7 & 1) { if(VAR_1 + VAR_2 - VAR_9 < 64) { av_log(VAR_3, AV_LOG_ERROR, "Header truncated\n"); return AVERROR_INVALIDDATA; } permute(VAR_0->qmat_chroma, VAR_0->prodsp.idct_permutation, VAR_9); } else { memset(VAR_0->qmat_chroma, 4, 64); } return VAR_4; }