static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,

                        const AVFrame *pic, int *got_packet)

{

    ProresContext *ctx = avctx->priv_data;

    uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;

    uint8_t *picture_size_pos;

    PutBitContext pb;

    int x, y, i, mb, q = 0;

    int sizes[4] = { 0 };

    int slice_hdr_size = 2 + 2 * (ctx->num_planes - 1);

    int frame_size, picture_size, slice_size;

    int pkt_size, ret;

    uint8_t frame_flags;



    *avctx->coded_frame           = *pic;

    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;

    avctx->coded_frame->key_frame = 1;



    pkt_size = ctx->frame_size_upper_bound;



    if ((ret = ff_alloc_packet(pkt, pkt_size + FF_MIN_BUFFER_SIZE)) < 0) {

        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");

        return ret;

    }



    orig_buf = pkt->data;



    // frame atom

    orig_buf += 4;                              // frame size

    bytestream_put_be32  (&orig_buf, FRAME_ID); // frame container ID

    buf = orig_buf;



    // frame header

    tmp = buf;

    buf += 2;                                   // frame header size will be stored here

    bytestream_put_be16  (&buf, 0);             // version 1

    bytestream_put_buffer(&buf, ctx->vendor, 4);

    bytestream_put_be16  (&buf, avctx->width);

    bytestream_put_be16  (&buf, avctx->height);



    frame_flags = ctx->chroma_factor << 6;

    if (avctx->flags & CODEC_FLAG_INTERLACED_DCT)

        frame_flags |= pic->top_field_first ? 0x04 : 0x08;

    bytestream_put_byte  (&buf, frame_flags);



    bytestream_put_byte  (&buf, 0);             // reserved

    bytestream_put_byte  (&buf, avctx->color_primaries);

    bytestream_put_byte  (&buf, avctx->color_trc);

    bytestream_put_byte  (&buf, avctx->colorspace);

    bytestream_put_byte  (&buf, 0x40 | (ctx->alpha_bits >> 3));

    bytestream_put_byte  (&buf, 0);             // reserved

    if (ctx->quant_sel != QUANT_MAT_DEFAULT) {

        bytestream_put_byte  (&buf, 0x03);      // matrix flags - both matrices are present

        // luma quantisation matrix

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

            bytestream_put_byte(&buf, ctx->quant_mat[i]);

        // chroma quantisation matrix

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

            bytestream_put_byte(&buf, ctx->quant_mat[i]);

    } else {

        bytestream_put_byte  (&buf, 0x00);      // matrix flags - default matrices are used

    }

    bytestream_put_be16  (&tmp, buf - orig_buf); // write back frame header size



    for (ctx->cur_picture_idx = 0;

         ctx->cur_picture_idx < ctx->pictures_per_frame;

         ctx->cur_picture_idx++) {

        // picture header

        picture_size_pos = buf + 1;

        bytestream_put_byte  (&buf, 0x40);          // picture header size (in bits)

        buf += 4;                                   // picture data size will be stored here

        bytestream_put_be16  (&buf, ctx->slices_per_picture);

        bytestream_put_byte  (&buf, av_log2(ctx->mbs_per_slice) << 4); // slice width and height in MBs



        // seek table - will be filled during slice encoding

        slice_sizes = buf;

        buf += ctx->slices_per_picture * 2;



        // slices

        if (!ctx->force_quant) {

            ret = avctx->execute2(avctx, find_quant_thread, NULL, NULL,

                                  ctx->mb_height);

            if (ret)

                return ret;

        }



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

            int mbs_per_slice = ctx->mbs_per_slice;

            for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {

                q = ctx->force_quant ? ctx->force_quant

                                     : ctx->slice_q[mb + y * ctx->slices_width];



                while (ctx->mb_width - x < mbs_per_slice)

                    mbs_per_slice >>= 1;



                bytestream_put_byte(&buf, slice_hdr_size << 3);

                slice_hdr = buf;

                buf += slice_hdr_size - 1;

                init_put_bits(&pb, buf, (pkt_size - (buf - orig_buf)) * 8);

                ret = encode_slice(avctx, pic, &pb, sizes, x, y, q,

                                   mbs_per_slice);

                if (ret < 0)

                    return ret;



                bytestream_put_byte(&slice_hdr, q);

                slice_size = slice_hdr_size + sizes[ctx->num_planes - 1];

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

                    bytestream_put_be16(&slice_hdr, sizes[i]);

                    slice_size += sizes[i];

                }

                bytestream_put_be16(&slice_sizes, slice_size);

                buf += slice_size - slice_hdr_size;

            }

        }



        if (ctx->pictures_per_frame == 1)

            picture_size = buf - picture_size_pos - 6;

        else

            picture_size = buf - picture_size_pos + 1;

        bytestream_put_be32(&picture_size_pos, picture_size);

    }



    orig_buf -= 8;

    frame_size = buf - orig_buf;

    bytestream_put_be32(&orig_buf, frame_size);



    pkt->size   = frame_size;

    pkt->flags |= AV_PKT_FLAG_KEY;

    *got_packet = 1;



    return 0;

}

static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,

                        const AVFrame *pic, int *got_packet)

{

    ProresContext *ctx = avctx->priv_data;

    uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;

    uint8_t *picture_size_pos;

    PutBitContext pb;

    int x, y, i, mb, q = 0;

    int sizes[4] = { 0 };

    int slice_hdr_size = 2 + 2 * (ctx->num_planes - 1);

    int frame_size, picture_size, slice_size;

    int pkt_size, ret;

    uint8_t frame_flags;



    *avctx->coded_frame           = *pic;

    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;

    avctx->coded_frame->key_frame = 1;



    pkt_size = ctx->frame_size_upper_bound;



    if ((ret = ff_alloc_packet(pkt, pkt_size + FF_MIN_BUFFER_SIZE)) < 0) {

        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");

        return ret;

    }



    orig_buf = pkt->data;



    

    orig_buf += 4;                              

    bytestream_put_be32  (&orig_buf, FRAME_ID); 

    buf = orig_buf;



    

    tmp = buf;

    buf += 2;                                    size will be stored here

    bytestream_put_be16  (&buf, 0);             

    bytestream_put_buffer(&buf, ctx->vendor, 4);

    bytestream_put_be16  (&buf, avctx->width);

    bytestream_put_be16  (&buf, avctx->height);



    frame_flags = ctx->chroma_factor << 6;

    if (avctx->flags & CODEC_FLAG_INTERLACED_DCT)

        frame_flags |= pic->top_field_first ? 0x04 : 0x08;

    bytestream_put_byte  (&buf, frame_flags);



    bytestream_put_byte  (&buf, 0);             

    bytestream_put_byte  (&buf, avctx->color_primaries);

    bytestream_put_byte  (&buf, avctx->color_trc);

    bytestream_put_byte  (&buf, avctx->colorspace);

    bytestream_put_byte  (&buf, 0x40 | (ctx->alpha_bits >> 3));

    bytestream_put_byte  (&buf, 0);             

    if (ctx->quant_sel != QUANT_MAT_DEFAULT) {

        bytestream_put_byte  (&buf, 0x03);      

        

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

            bytestream_put_byte(&buf, ctx->quant_mat[i]);

        

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

            bytestream_put_byte(&buf, ctx->quant_mat[i]);

    } else {

        bytestream_put_byte  (&buf, 0x00);      

    }

    bytestream_put_be16  (&tmp, buf - orig_buf); 



    for (ctx->cur_picture_idx = 0;

         ctx->cur_picture_idx < ctx->pictures_per_frame;

         ctx->cur_picture_idx++) {

        

        picture_size_pos = buf + 1;

        bytestream_put_byte  (&buf, 0x40);           size (in bits)

        buf += 4;                                   

        bytestream_put_be16  (&buf, ctx->slices_per_picture);

        bytestream_put_byte  (&buf, av_log2(ctx->mbs_per_slice) << 4); 



        

        slice_sizes = buf;

        buf += ctx->slices_per_picture * 2;



        

        if (!ctx->force_quant) {

            ret = avctx->execute2(avctx, find_quant_thread, NULL, NULL,

                                  ctx->mb_height);

            if (ret)

                return ret;

        }



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

            int mbs_per_slice = ctx->mbs_per_slice;

            for (x = mb = 0; x < ctx->mb_width; x += mbs_per_slice, mb++) {

                q = ctx->force_quant ? ctx->force_quant

                                     : ctx->slice_q[mb + y * ctx->slices_width];



                while (ctx->mb_width - x < mbs_per_slice)

                    mbs_per_slice >>= 1;



                bytestream_put_byte(&buf, slice_hdr_size << 3);

                slice_hdr = buf;

                buf += slice_hdr_size - 1;

                init_put_bits(&pb, buf, (pkt_size - (buf - orig_buf)) * 8);

                ret = encode_slice(avctx, pic, &pb, sizes, x, y, q,

                                   mbs_per_slice);

                if (ret < 0)

                    return ret;



                bytestream_put_byte(&slice_hdr, q);

                slice_size = slice_hdr_size + sizes[ctx->num_planes - 1];

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

                    bytestream_put_be16(&slice_hdr, sizes[i]);

                    slice_size += sizes[i];

                }

                bytestream_put_be16(&slice_sizes, slice_size);

                buf += slice_size - slice_hdr_size;

            }

        }



        if (ctx->pictures_per_frame == 1)

            picture_size = buf - picture_size_pos - 6;

        else

            picture_size = buf - picture_size_pos + 1;

        bytestream_put_be32(&picture_size_pos, picture_size);

    }



    orig_buf -= 8;

    frame_size = buf - orig_buf;

    bytestream_put_be32(&orig_buf, frame_size);



    pkt->size   = frame_size;

    pkt->flags |= AV_PKT_FLAG_KEY;

    *got_packet = 1;



    return 0;

}

    int pkt_size, ret;
static int FUNC_0(AVCodecContext *VAR_0, AVPacket *VAR_1,

                        const AVFrame *VAR_2, int *VAR_3)

{

    ProresContext *ctx = VAR_0->priv_data;

    uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp;

    uint8_t *picture_size_pos;

    PutBitContext pb;

    int VAR_4, VAR_5, VAR_6, VAR_7, VAR_8 = 0;

    int VAR_9[4] = { 0 };

    int VAR_10 = 2 + 2 * (ctx->num_planes - 1);

    int VAR_11, VAR_12, VAR_13;

    int VAR_14, VAR_15;

    uint8_t frame_flags;



    *VAR_0->coded_frame           = *VAR_2;

    VAR_0->coded_frame->pict_type = AV_PICTURE_TYPE_I;

    VAR_0->coded_frame->key_frame = 1;



    VAR_14 = ctx->frame_size_upper_bound;



    if ((VAR_15 = ff_alloc_packet(VAR_1, VAR_14 + FF_MIN_BUFFER_SIZE)) < 0) {

        av_log(VAR_0, AV_LOG_ERROR, "Error getting output packet.\n");

        return VAR_15;

    }



    orig_buf = VAR_1->data;



    

    orig_buf += 4;                              

    bytestream_put_be32  (&orig_buf, FRAME_ID); 

    buf = orig_buf;



    

    tmp = buf;

    buf += 2;                                    size will be stored here

    bytestream_put_be16  (&buf, 0);             

    bytestream_put_buffer(&buf, ctx->vendor, 4);

    bytestream_put_be16  (&buf, VAR_0->width);

    bytestream_put_be16  (&buf, VAR_0->height);



    frame_flags = ctx->chroma_factor << 6;

    if (VAR_0->flags & CODEC_FLAG_INTERLACED_DCT)

        frame_flags |= VAR_2->top_field_first ? 0x04 : 0x08;

    bytestream_put_byte  (&buf, frame_flags);



    bytestream_put_byte  (&buf, 0);             

    bytestream_put_byte  (&buf, VAR_0->color_primaries);

    bytestream_put_byte  (&buf, VAR_0->color_trc);

    bytestream_put_byte  (&buf, VAR_0->colorspace);

    bytestream_put_byte  (&buf, 0x40 | (ctx->alpha_bits >> 3));

    bytestream_put_byte  (&buf, 0);             

    if (ctx->quant_sel != QUANT_MAT_DEFAULT) {

        bytestream_put_byte  (&buf, 0x03);      

        

        for (VAR_6 = 0; VAR_6 < 64; VAR_6++)

            bytestream_put_byte(&buf, ctx->quant_mat[VAR_6]);

        

        for (VAR_6 = 0; VAR_6 < 64; VAR_6++)

            bytestream_put_byte(&buf, ctx->quant_mat[VAR_6]);

    } else {

        bytestream_put_byte  (&buf, 0x00);      

    }

    bytestream_put_be16  (&tmp, buf - orig_buf); 



    for (ctx->cur_picture_idx = 0;

         ctx->cur_picture_idx < ctx->pictures_per_frame;

         ctx->cur_picture_idx++) {

        

        picture_size_pos = buf + 1;

        bytestream_put_byte  (&buf, 0x40);           size (in bits)

        buf += 4;                                   

        bytestream_put_be16  (&buf, ctx->slices_per_picture);

        bytestream_put_byte  (&buf, av_log2(ctx->mbs_per_slice) << 4); 



        

        slice_sizes = buf;

        buf += ctx->slices_per_picture * 2;



        

        if (!ctx->force_quant) {

            VAR_15 = VAR_0->execute2(VAR_0, find_quant_thread, NULL, NULL,

                                  ctx->mb_height);

            if (VAR_15)

                return VAR_15;

        }



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

            int mbs_per_slice = ctx->mbs_per_slice;

            for (VAR_4 = VAR_7 = 0; VAR_4 < ctx->mb_width; VAR_4 += mbs_per_slice, VAR_7++) {

                VAR_8 = ctx->force_quant ? ctx->force_quant

                                     : ctx->slice_q[VAR_7 + VAR_5 * ctx->slices_width];



                while (ctx->mb_width - VAR_4 < mbs_per_slice)

                    mbs_per_slice >>= 1;



                bytestream_put_byte(&buf, VAR_10 << 3);

                slice_hdr = buf;

                buf += VAR_10 - 1;

                init_put_bits(&pb, buf, (VAR_14 - (buf - orig_buf)) * 8);

                VAR_15 = encode_slice(VAR_0, VAR_2, &pb, VAR_9, VAR_4, VAR_5, VAR_8,

                                   mbs_per_slice);

                if (VAR_15 < 0)

                    return VAR_15;



                bytestream_put_byte(&slice_hdr, VAR_8);

                VAR_13 = VAR_10 + VAR_9[ctx->num_planes - 1];

                for (VAR_6 = 0; VAR_6 < ctx->num_planes - 1; VAR_6++) {

                    bytestream_put_be16(&slice_hdr, VAR_9[VAR_6]);

                    VAR_13 += VAR_9[VAR_6];

                }

                bytestream_put_be16(&slice_sizes, VAR_13);

                buf += VAR_13 - VAR_10;

            }

        }



        if (ctx->pictures_per_frame == 1)

            VAR_12 = buf - picture_size_pos - 6;

        else

            VAR_12 = buf - picture_size_pos + 1;

        bytestream_put_be32(&picture_size_pos, VAR_12);

    }



    orig_buf -= 8;

    VAR_11 = buf - orig_buf;

    bytestream_put_be32(&orig_buf, VAR_11);



    VAR_1->size   = VAR_11;

    VAR_1->flags |= AV_PKT_FLAG_KEY;

    *VAR_3 = 1;



    return 0;

}

static int FUNC_0(AVCodecContext *VAR_0, AVPacket *VAR_1,
const AVFrame *VAR_2, int *VAR_3)
{ ProresContext *ctx = VAR_0->priv_data; uint8_t *orig_buf, *buf, *slice_hdr, *slice_sizes, *tmp; uint8_t *picture_size_pos; PutBitContext pb; int VAR_4, VAR_5, VAR_6, VAR_7, VAR_8 = 0; int VAR_9[4] = { 0 }; int VAR_10 = 2 + 2 * (ctx->num_planes - 1); int VAR_11, VAR_12, VAR_13; int VAR_14, VAR_15; uint8_t frame_flags; *VAR_0->coded_frame           = *VAR_2; VAR_0->coded_frame->pict_type = AV_PICTURE_TYPE_I; VAR_0->coded_frame->key_frame = 1; VAR_14 = ctx->frame_size_upper_bound; if ((VAR_15 = ff_alloc_packet(VAR_1, VAR_14 + FF_MIN_BUFFER_SIZE)) < 0) { av_log(VAR_0, AV_LOG_ERROR, "Error getting output packet.\n"); return VAR_15; } orig_buf = VAR_1->data; orig_buf += 4; bytestream_put_be32  (&orig_buf, FRAME_ID); buf = orig_buf; tmp = buf; buf += 2;                                    size will be stored here bytestream_put_be16  (&buf, 0); bytestream_put_buffer(&buf, ctx->vendor, 4); bytestream_put_be16  (&buf, VAR_0->width); bytestream_put_be16  (&buf, VAR_0->height); frame_flags = ctx->chroma_factor << 6; if (VAR_0->flags & CODEC_FLAG_INTERLACED_DCT)
frame_flags |= VAR_2->top_field_first ? 0x04 : 0x08; bytestream_put_byte  (&buf, frame_flags); bytestream_put_byte  (&buf, 0); bytestream_put_byte  (&buf, VAR_0->color_primaries); bytestream_put_byte  (&buf, VAR_0->color_trc); bytestream_put_byte  (&buf, VAR_0->colorspace); bytestream_put_byte  (&buf, 0x40 | (ctx->alpha_bits >> 3)); bytestream_put_byte  (&buf, 0); if (ctx->quant_sel != QUANT_MAT_DEFAULT) { bytestream_put_byte  (&buf, 0x03); for (VAR_6 = 0; VAR_6 < 64; VAR_6++) bytestream_put_byte(&buf, ctx->quant_mat[VAR_6]); for (VAR_6 = 0; VAR_6 < 64; VAR_6++) bytestream_put_byte(&buf, ctx->quant_mat[VAR_6]); } else { bytestream_put_byte  (&buf, 0x00); } bytestream_put_be16  (&tmp, buf - orig_buf); for (ctx->cur_picture_idx = 0; ctx->cur_picture_idx < ctx->pictures_per_frame; ctx->cur_picture_idx++) { picture_size_pos = buf + 1; bytestream_put_byte  (&buf, 0x40);           size (in bits) buf += 4; bytestream_put_be16  (&buf, ctx->slices_per_picture); bytestream_put_byte  (&buf, av_log2(ctx->mbs_per_slice) << 4); slice_sizes = buf; buf += ctx->slices_per_picture * 2; if (!ctx->force_quant) { VAR_15 = VAR_0->execute2(VAR_0, find_quant_thread, NULL, NULL,
ctx->mb_height); if (VAR_15)
return VAR_15; } for (VAR_5 = 0; VAR_5 < ctx->mb_height; VAR_5++) { int mbs_per_slice = ctx->mbs_per_slice; for (VAR_4 = VAR_7 = 0; VAR_4 < ctx->mb_width; VAR_4 += mbs_per_slice, VAR_7++) { VAR_8 = ctx->force_quant ? ctx->force_quant
: ctx->slice_q[VAR_7 + VAR_5 * ctx->slices_width]; while (ctx->mb_width - VAR_4 < mbs_per_slice)
mbs_per_slice >>= 1; bytestream_put_byte(&buf, VAR_10 << 3); slice_hdr = buf; buf += VAR_10 - 1; init_put_bits(&pb, buf, (VAR_14 - (buf - orig_buf)) * 8); VAR_15 = encode_slice(VAR_0, VAR_2, &pb, VAR_9, VAR_4, VAR_5, VAR_8,
mbs_per_slice); if (VAR_15 < 0)
return VAR_15; bytestream_put_byte(&slice_hdr, VAR_8); VAR_13 = VAR_10 + VAR_9[ctx->num_planes - 1]; for (VAR_6 = 0; VAR_6 < ctx->num_planes - 1; VAR_6++) { bytestream_put_be16(&slice_hdr, VAR_9[VAR_6]); VAR_13 += VAR_9[VAR_6]; } bytestream_put_be16(&slice_sizes, VAR_13); buf += VAR_13 - VAR_10; } } if (ctx->pictures_per_frame == 1)
VAR_12 = buf - picture_size_pos - 6; else
VAR_12 = buf - picture_size_pos + 1; bytestream_put_be32(&picture_size_pos, VAR_12); } orig_buf -= 8; VAR_11 = buf - orig_buf; bytestream_put_be32(&orig_buf, VAR_11); VAR_1->size   = VAR_11; VAR_1->flags |= AV_PKT_FLAG_KEY; *VAR_3 = 1; return 0; }