static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,

                               const uint8_t *buf, int buf_size,

                               int first_field)

{

    static const uint8_t header_prefix[]    = { 0x00, 0x00, 0x02, 0x80, 0x01 };

    static const uint8_t header_prefix444[] = { 0x00, 0x00, 0x02, 0x80, 0x02 };

    int i, cid, ret;



    if (buf_size < 0x280)

        return AVERROR_INVALIDDATA;



    if (memcmp(buf, header_prefix, 5) && memcmp(buf, header_prefix444, 5)) {

        av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");

        return AVERROR_INVALIDDATA;

    }

    if (buf[5] & 2) { /* interlaced */

        ctx->cur_field = buf[5] & 1;

        frame->interlaced_frame = 1;

        frame->top_field_first  = first_field ^ ctx->cur_field;

        av_log(ctx->avctx, AV_LOG_DEBUG,

               "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);

    }



    ctx->height = AV_RB16(buf + 0x18);

    ctx->width  = AV_RB16(buf + 0x1a);



    av_dlog(ctx->avctx, "width %d, height %d\n", ctx->width, ctx->height);



    ctx->is_444 = 0;

    if (buf[0x4] == 0x2) {

        ctx->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;

        ctx->avctx->bits_per_raw_sample = 10;

        if (ctx->bit_depth != 10) {

            ff_blockdsp_init(&ctx->bdsp, ctx->avctx);

            ff_idctdsp_init(&ctx->idsp, ctx->avctx);

            ctx->bit_depth = 10;

            ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;

        }

        ctx->is_444 = 1;

    } else if (buf[0x21] & 0x40) {

        ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;

        ctx->avctx->bits_per_raw_sample = 10;

        if (ctx->bit_depth != 10) {

            ff_blockdsp_init(&ctx->bdsp, ctx->avctx);

            ff_idctdsp_init(&ctx->idsp, ctx->avctx);

            ctx->bit_depth = 10;

            ctx->decode_dct_block = dnxhd_decode_dct_block_10;

        }

    } else {

        ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;

        ctx->avctx->bits_per_raw_sample = 8;

        if (ctx->bit_depth != 8) {

            ff_blockdsp_init(&ctx->bdsp, ctx->avctx);

            ff_idctdsp_init(&ctx->idsp, ctx->avctx);

            ctx->bit_depth = 8;

            ctx->decode_dct_block = dnxhd_decode_dct_block_8;

        }

    }



    cid = AV_RB32(buf + 0x28);

    av_dlog(ctx->avctx, "compression id %d\n", cid);



    if ((ret = dnxhd_init_vlc(ctx, cid)) < 0)

        return ret;



    if (buf_size < ctx->cid_table->coding_unit_size) {

        av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");

        return AVERROR_INVALIDDATA;

    }



    ctx->mb_width  = ctx->width >> 4;

    ctx->mb_height = buf[0x16d];



    av_dlog(ctx->avctx,

            "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);



    if ((ctx->height + 15) >> 4 == ctx->mb_height && frame->interlaced_frame)

        ctx->height <<= 1;



    if (ctx->mb_height > 68 ||

        (ctx->mb_height << frame->interlaced_frame) > (ctx->height + 15) >> 4) {

        av_log(ctx->avctx, AV_LOG_ERROR,

               "mb height too big: %d\n", ctx->mb_height);

        return AVERROR_INVALIDDATA;

    }



    for (i = 0; i < ctx->mb_height; i++) {

        ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i << 2));

        av_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);

        if (buf_size < ctx->mb_scan_index[i] + 0x280LL) {

            av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");

            return AVERROR_INVALIDDATA;

        }

    }



    return 0;

}

static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,

                               const uint8_t *buf, int buf_size,

                               int first_field)

{

    static const uint8_t header_prefix[]    = { 0x00, 0x00, 0x02, 0x80, 0x01 };

    static const uint8_t header_prefix444[] = { 0x00, 0x00, 0x02, 0x80, 0x02 };

    int i, cid, ret;



    if (buf_size < 0x280)

        return AVERROR_INVALIDDATA;



    if (memcmp(buf, header_prefix, 5) && memcmp(buf, header_prefix444, 5)) {

        av_log(ctx->avctx, AV_LOG_ERROR, "error in header\n");

        return AVERROR_INVALIDDATA;

    }

    if (buf[5] & 2) { 

        ctx->cur_field = buf[5] & 1;

        frame->interlaced_frame = 1;

        frame->top_field_first  = first_field ^ ctx->cur_field;

        av_log(ctx->avctx, AV_LOG_DEBUG,

               "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);

    }



    ctx->height = AV_RB16(buf + 0x18);

    ctx->width  = AV_RB16(buf + 0x1a);



    av_dlog(ctx->avctx, "width %d, height %d\n", ctx->width, ctx->height);



    ctx->is_444 = 0;

    if (buf[0x4] == 0x2) {

        ctx->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;

        ctx->avctx->bits_per_raw_sample = 10;

        if (ctx->bit_depth != 10) {

            ff_blockdsp_init(&ctx->bdsp, ctx->avctx);

            ff_idctdsp_init(&ctx->idsp, ctx->avctx);

            ctx->bit_depth = 10;

            ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;

        }

        ctx->is_444 = 1;

    } else if (buf[0x21] & 0x40) {

        ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;

        ctx->avctx->bits_per_raw_sample = 10;

        if (ctx->bit_depth != 10) {

            ff_blockdsp_init(&ctx->bdsp, ctx->avctx);

            ff_idctdsp_init(&ctx->idsp, ctx->avctx);

            ctx->bit_depth = 10;

            ctx->decode_dct_block = dnxhd_decode_dct_block_10;

        }

    } else {

        ctx->avctx->pix_fmt = AV_PIX_FMT_YUV422P;

        ctx->avctx->bits_per_raw_sample = 8;

        if (ctx->bit_depth != 8) {

            ff_blockdsp_init(&ctx->bdsp, ctx->avctx);

            ff_idctdsp_init(&ctx->idsp, ctx->avctx);

            ctx->bit_depth = 8;

            ctx->decode_dct_block = dnxhd_decode_dct_block_8;

        }

    }



    cid = AV_RB32(buf + 0x28);

    av_dlog(ctx->avctx, "compression id %d\n", cid);



    if ((ret = dnxhd_init_vlc(ctx, cid)) < 0)

        return ret;



    if (buf_size < ctx->cid_table->coding_unit_size) {

        av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size\n");

        return AVERROR_INVALIDDATA;

    }



    ctx->mb_width  = ctx->width >> 4;

    ctx->mb_height = buf[0x16d];



    av_dlog(ctx->avctx,

            "mb width %d, mb height %d\n", ctx->mb_width, ctx->mb_height);



    if ((ctx->height + 15) >> 4 == ctx->mb_height && frame->interlaced_frame)

        ctx->height <<= 1;



    if (ctx->mb_height > 68 ||

        (ctx->mb_height << frame->interlaced_frame) > (ctx->height + 15) >> 4) {

        av_log(ctx->avctx, AV_LOG_ERROR,

               "mb height too big: %d\n", ctx->mb_height);

        return AVERROR_INVALIDDATA;

    }



    for (i = 0; i < ctx->mb_height; i++) {

        ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i << 2));

        av_dlog(ctx->avctx, "mb scan index %d\n", ctx->mb_scan_index[i]);

        if (buf_size < ctx->mb_scan_index[i] + 0x280LL) {

            av_log(ctx->avctx, AV_LOG_ERROR, "invalid mb scan index\n");

            return AVERROR_INVALIDDATA;

        }

    }



    return 0;

}

static int FUNC_0(DNXHDContext *VAR_0, AVFrame *VAR_1,

                               const uint8_t *VAR_2, int VAR_3,

                               int VAR_4)

{

    static const uint8_t VAR_5[]    = { 0x00, 0x00, 0x02, 0x80, 0x01 };

    static const uint8_t VAR_6[] = { 0x00, 0x00, 0x02, 0x80, 0x02 };

    int VAR_7, VAR_8, VAR_9;



    if (VAR_3 < 0x280)

        return AVERROR_INVALIDDATA;



    if (memcmp(VAR_2, VAR_5, 5) && memcmp(VAR_2, VAR_6, 5)) {

        av_log(VAR_0->avctx, AV_LOG_ERROR, "error in header\n");

        return AVERROR_INVALIDDATA;

    }

    if (VAR_2[5] & 2) { 

        VAR_0->cur_field = VAR_2[5] & 1;

        VAR_1->interlaced_frame = 1;

        VAR_1->top_field_first  = VAR_4 ^ VAR_0->cur_field;

        av_log(VAR_0->avctx, AV_LOG_DEBUG,

               "interlaced %d, cur field %d\n", VAR_2[5] & 3, VAR_0->cur_field);

    }



    VAR_0->height = AV_RB16(VAR_2 + 0x18);

    VAR_0->width  = AV_RB16(VAR_2 + 0x1a);



    av_dlog(VAR_0->avctx, "width %d, height %d\n", VAR_0->width, VAR_0->height);



    VAR_0->is_444 = 0;

    if (VAR_2[0x4] == 0x2) {

        VAR_0->avctx->pix_fmt = AV_PIX_FMT_YUV444P10;

        VAR_0->avctx->bits_per_raw_sample = 10;

        if (VAR_0->bit_depth != 10) {

            ff_blockdsp_init(&VAR_0->bdsp, VAR_0->avctx);

            ff_idctdsp_init(&VAR_0->idsp, VAR_0->avctx);

            VAR_0->bit_depth = 10;

            VAR_0->decode_dct_block = dnxhd_decode_dct_block_10_444;

        }

        VAR_0->is_444 = 1;

    } else if (VAR_2[0x21] & 0x40) {

        VAR_0->avctx->pix_fmt = AV_PIX_FMT_YUV422P10;

        VAR_0->avctx->bits_per_raw_sample = 10;

        if (VAR_0->bit_depth != 10) {

            ff_blockdsp_init(&VAR_0->bdsp, VAR_0->avctx);

            ff_idctdsp_init(&VAR_0->idsp, VAR_0->avctx);

            VAR_0->bit_depth = 10;

            VAR_0->decode_dct_block = dnxhd_decode_dct_block_10;

        }

    } else {

        VAR_0->avctx->pix_fmt = AV_PIX_FMT_YUV422P;

        VAR_0->avctx->bits_per_raw_sample = 8;

        if (VAR_0->bit_depth != 8) {

            ff_blockdsp_init(&VAR_0->bdsp, VAR_0->avctx);

            ff_idctdsp_init(&VAR_0->idsp, VAR_0->avctx);

            VAR_0->bit_depth = 8;

            VAR_0->decode_dct_block = dnxhd_decode_dct_block_8;

        }

    }



    VAR_8 = AV_RB32(VAR_2 + 0x28);

    av_dlog(VAR_0->avctx, "compression id %d\n", VAR_8);



    if ((VAR_9 = dnxhd_init_vlc(VAR_0, VAR_8)) < 0)

        return VAR_9;



    if (VAR_3 < VAR_0->cid_table->coding_unit_size) {

        av_log(VAR_0->avctx, AV_LOG_ERROR, "incorrect VAR_1 size\n");

        return AVERROR_INVALIDDATA;

    }



    VAR_0->mb_width  = VAR_0->width >> 4;

    VAR_0->mb_height = VAR_2[0x16d];



    av_dlog(VAR_0->avctx,

            "mb width %d, mb height %d\n", VAR_0->mb_width, VAR_0->mb_height);



    if ((VAR_0->height + 15) >> 4 == VAR_0->mb_height && VAR_1->interlaced_frame)

        VAR_0->height <<= 1;



    if (VAR_0->mb_height > 68 ||

        (VAR_0->mb_height << VAR_1->interlaced_frame) > (VAR_0->height + 15) >> 4) {

        av_log(VAR_0->avctx, AV_LOG_ERROR,

               "mb height too big: %d\n", VAR_0->mb_height);

        return AVERROR_INVALIDDATA;

    }



    for (VAR_7 = 0; VAR_7 < VAR_0->mb_height; VAR_7++) {

        VAR_0->mb_scan_index[VAR_7] = AV_RB32(VAR_2 + 0x170 + (VAR_7 << 2));

        av_dlog(VAR_0->avctx, "mb scan index %d\n", VAR_0->mb_scan_index[VAR_7]);

        if (VAR_3 < VAR_0->mb_scan_index[VAR_7] + 0x280LL) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "invalid mb scan index\n");

            return AVERROR_INVALIDDATA;

        }

    }



    return 0;

}

static int FUNC_0(DNXHDContext *VAR_0, AVFrame *VAR_1,
const uint8_t *VAR_2, int VAR_3,
int VAR_4)
{ static const uint8_t VAR_5[]    = { 0x00, 0x00, 0x02, 0x80, 0x01 }; static const uint8_t VAR_6[] = { 0x00, 0x00, 0x02, 0x80, 0x02 }; int VAR_7, VAR_8, VAR_9; if (VAR_3 < 0x280)
return AVERROR_INVALIDDATA; if (memcmp(VAR_2, VAR_5, 5) && memcmp(VAR_2, VAR_6, 5)) { av_log(VAR_0->avctx, AV_LOG_ERROR, "error in header\n"); return AVERROR_INVALIDDATA; } if (VAR_2[5] & 2) { VAR_0->cur_field = VAR_2[5] & 1; VAR_1->interlaced_frame = 1; VAR_1->top_field_first  = VAR_4 ^ VAR_0->cur_field; av_log(VAR_0->avctx, AV_LOG_DEBUG,
"interlaced %d, cur field %d\n", VAR_2[5] & 3, VAR_0->cur_field); } VAR_0->height = AV_RB16(VAR_2 + 0x18); VAR_0->width  = AV_RB16(VAR_2 + 0x1a); av_dlog(VAR_0->avctx, "width %d, height %d\n", VAR_0->width, VAR_0->height); VAR_0->is_444 = 0; if (VAR_2[0x4] == 0x2) { VAR_0->avctx->pix_fmt = AV_PIX_FMT_YUV444P10; VAR_0->avctx->bits_per_raw_sample = 10; if (VAR_0->bit_depth != 10) { ff_blockdsp_init(&VAR_0->bdsp, VAR_0->avctx); ff_idctdsp_init(&VAR_0->idsp, VAR_0->avctx); VAR_0->bit_depth = 10; VAR_0->decode_dct_block = dnxhd_decode_dct_block_10_444; } VAR_0->is_444 = 1; } else if (VAR_2[0x21] & 0x40) { VAR_0->avctx->pix_fmt = AV_PIX_FMT_YUV422P10; VAR_0->avctx->bits_per_raw_sample = 10; if (VAR_0->bit_depth != 10) { ff_blockdsp_init(&VAR_0->bdsp, VAR_0->avctx); ff_idctdsp_init(&VAR_0->idsp, VAR_0->avctx); VAR_0->bit_depth = 10; VAR_0->decode_dct_block = dnxhd_decode_dct_block_10; } } else { VAR_0->avctx->pix_fmt = AV_PIX_FMT_YUV422P; VAR_0->avctx->bits_per_raw_sample = 8; if (VAR_0->bit_depth != 8) { ff_blockdsp_init(&VAR_0->bdsp, VAR_0->avctx); ff_idctdsp_init(&VAR_0->idsp, VAR_0->avctx); VAR_0->bit_depth = 8; VAR_0->decode_dct_block = dnxhd_decode_dct_block_8; } } VAR_8 = AV_RB32(VAR_2 + 0x28); av_dlog(VAR_0->avctx, "compression id %d\n", VAR_8); if ((VAR_9 = dnxhd_init_vlc(VAR_0, VAR_8)) < 0)
return VAR_9; if (VAR_3 < VAR_0->cid_table->coding_unit_size) { av_log(VAR_0->avctx, AV_LOG_ERROR, "incorrect VAR_1 size\n"); return AVERROR_INVALIDDATA; } VAR_0->mb_width  = VAR_0->width >> 4; VAR_0->mb_height = VAR_2[0x16d]; av_dlog(VAR_0->avctx,
"mb width %d, mb height %d\n", VAR_0->mb_width, VAR_0->mb_height); if ((VAR_0->height + 15) >> 4 == VAR_0->mb_height && VAR_1->interlaced_frame)
VAR_0->height <<= 1; if (VAR_0->mb_height > 68 ||
(VAR_0->mb_height << VAR_1->interlaced_frame) > (VAR_0->height + 15) >> 4) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"mb height too big: %d\n", VAR_0->mb_height); return AVERROR_INVALIDDATA; } for (VAR_7 = 0; VAR_7 < VAR_0->mb_height; VAR_7++) { VAR_0->mb_scan_index[VAR_7] = AV_RB32(VAR_2 + 0x170 + (VAR_7 << 2)); av_dlog(VAR_0->avctx, "mb scan index %d\n", VAR_0->mb_scan_index[VAR_7]); if (VAR_3 < VAR_0->mb_scan_index[VAR_7] + 0x280LL) { av_log(VAR_0->avctx, AV_LOG_ERROR, "invalid mb scan index\n"); return AVERROR_INVALIDDATA; } } return 0; }