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 mbs_per_slice = ctx->mbs_per_slice;

    int pkt_size, ret;



    *avctx->coded_frame           = *pic;

    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;

    avctx->coded_frame->key_frame = 1;



    pkt_size = ctx->mb_width * ctx->mb_height * 64 * 3 * 12

               + ctx->num_slices * 2 + 200 + FF_MIN_BUFFER_SIZE;



    if ((ret = ff_alloc_packet(pkt, pkt_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, "Lavc", 4);     // creator

    bytestream_put_be16  (&buf, avctx->width);

    bytestream_put_be16  (&buf, avctx->height);

    bytestream_put_byte  (&buf, ctx->chroma_factor << 6); // frame flags

    bytestream_put_byte  (&buf, 0);             // reserved

    bytestream_put_byte  (&buf, 0);             // primaries

    bytestream_put_byte  (&buf, 0);             // transfer function

    bytestream_put_byte  (&buf, 6);             // colour matrix - ITU-R BT.601-4

    bytestream_put_byte  (&buf, 0x40);          // source format and alpha information

    bytestream_put_byte  (&buf, 0);             // reserved

    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->profile_info->quant[i]);

    // chroma quantisation matrix

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

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

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



    // 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->num_slices); // total number of slices

    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->num_slices * 2;



    // slices

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

        mbs_per_slice = ctx->mbs_per_slice;

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

            while (ctx->mb_width - x < mbs_per_slice)

                mbs_per_slice >>= 1;

            q = find_slice_quant(avctx, pic, (mb + 1) * TRELLIS_WIDTH, x, y,

                                 mbs_per_slice);

        }



        for (x = ctx->slices_width - 1; x >= 0; x--) {

            ctx->slice_q[x] = ctx->nodes[q].quant;

            q = ctx->nodes[q].prev_node;

        }



        mbs_per_slice = ctx->mbs_per_slice;

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

            q = ctx->slice_q[mb];



            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);

            encode_slice(avctx, pic, &pb, sizes, x, y, q, mbs_per_slice);



            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;

        }

    }



    orig_buf -= 8;

    frame_size = buf - orig_buf;

    picture_size = buf - picture_size_pos - 6;

    bytestream_put_be32(&orig_buf, frame_size);

    bytestream_put_be32(&picture_size_pos, picture_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 mbs_per_slice = ctx->mbs_per_slice;

    int pkt_size, ret;



    *avctx->coded_frame           = *pic;

    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;

    avctx->coded_frame->key_frame = 1;



    pkt_size = ctx->mb_width * ctx->mb_height * 64 * 3 * 12

               + ctx->num_slices * 2 + 200 + FF_MIN_BUFFER_SIZE;



    if ((ret = ff_alloc_packet(pkt, pkt_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, "Lavc", 4);     

    bytestream_put_be16  (&buf, avctx->width);

    bytestream_put_be16  (&buf, avctx->height);

    bytestream_put_byte  (&buf, ctx->chroma_factor << 6); 

    bytestream_put_byte  (&buf, 0);             

    bytestream_put_byte  (&buf, 0);             

    bytestream_put_byte  (&buf, 0);             

    bytestream_put_byte  (&buf, 6);             

    bytestream_put_byte  (&buf, 0x40);          

    bytestream_put_byte  (&buf, 0);             

    bytestream_put_byte  (&buf, 0x03);          

    

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

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

    

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

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

    bytestream_put_be16  (&tmp, buf - orig_buf); 



    

    picture_size_pos = buf + 1;

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

    buf += 4;                                   

    bytestream_put_be16  (&buf, ctx->num_slices); 

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



    

    slice_sizes = buf;

    buf += ctx->num_slices * 2;



    

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

        mbs_per_slice = ctx->mbs_per_slice;

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

            while (ctx->mb_width - x < mbs_per_slice)

                mbs_per_slice >>= 1;

            q = find_slice_quant(avctx, pic, (mb + 1) * TRELLIS_WIDTH, x, y,

                                 mbs_per_slice);

        }



        for (x = ctx->slices_width - 1; x >= 0; x--) {

            ctx->slice_q[x] = ctx->nodes[q].quant;

            q = ctx->nodes[q].prev_node;

        }



        mbs_per_slice = ctx->mbs_per_slice;

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

            q = ctx->slice_q[mb];



            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);

            encode_slice(avctx, pic, &pb, sizes, x, y, q, mbs_per_slice);



            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;

        }

    }



    orig_buf -= 8;

    frame_size = buf - orig_buf;

    picture_size = buf - picture_size_pos - 6;

    bytestream_put_be32(&orig_buf, frame_size);

    bytestream_put_be32(&picture_size_pos, picture_size);



    pkt->size   = frame_size;

    pkt->flags |= AV_PKT_FLAG_KEY;

    *got_packet = 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 = ctx->VAR_14;

    int VAR_15, VAR_16;



    *VAR_0->coded_frame           = *VAR_2;

    VAR_0->coded_frame->pict_type = AV_PICTURE_TYPE_I;

    VAR_0->coded_frame->key_frame = 1;



    VAR_15 = ctx->mb_width * ctx->mb_height * 64 * 3 * 12

               + ctx->num_slices * 2 + 200 + FF_MIN_BUFFER_SIZE;



    if ((VAR_16 = ff_alloc_packet(VAR_1, VAR_15)) < 0) {

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

        return VAR_16;

    }



    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, "Lavc", 4);     

    bytestream_put_be16  (&buf, VAR_0->width);

    bytestream_put_be16  (&buf, VAR_0->height);

    bytestream_put_byte  (&buf, ctx->chroma_factor << 6); 

    bytestream_put_byte  (&buf, 0);             

    bytestream_put_byte  (&buf, 0);             

    bytestream_put_byte  (&buf, 0);             

    bytestream_put_byte  (&buf, 6);             

    bytestream_put_byte  (&buf, 0x40);          

    bytestream_put_byte  (&buf, 0);             

    bytestream_put_byte  (&buf, 0x03);          

    

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

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

    

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

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

    bytestream_put_be16  (&tmp, buf - orig_buf); 



    

    picture_size_pos = buf + 1;

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

    buf += 4;                                   

    bytestream_put_be16  (&buf, ctx->num_slices); 

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



    

    slice_sizes = buf;

    buf += ctx->num_slices * 2;



    

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

        VAR_14 = ctx->VAR_14;

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

            while (ctx->mb_width - VAR_4 < VAR_14)

                VAR_14 >>= 1;

            VAR_8 = find_slice_quant(VAR_0, VAR_2, (VAR_7 + 1) * TRELLIS_WIDTH, VAR_4, VAR_5,

                                 VAR_14);

        }



        for (VAR_4 = ctx->slices_width - 1; VAR_4 >= 0; VAR_4--) {

            ctx->slice_q[VAR_4] = ctx->nodes[VAR_8].quant;

            VAR_8 = ctx->nodes[VAR_8].prev_node;

        }



        VAR_14 = ctx->VAR_14;

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

            VAR_8 = ctx->slice_q[VAR_7];



            while (ctx->mb_width - VAR_4 < VAR_14)

                VAR_14 >>= 1;



            bytestream_put_byte(&buf, VAR_10 << 3);

            slice_hdr = buf;

            buf += VAR_10 - 1;

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

            encode_slice(VAR_0, VAR_2, &pb, VAR_9, VAR_4, VAR_5, VAR_8, VAR_14);



            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;

        }

    }



    orig_buf -= 8;

    VAR_11 = buf - orig_buf;

    VAR_12 = buf - picture_size_pos - 6;

    bytestream_put_be32(&orig_buf, VAR_11);

    bytestream_put_be32(&picture_size_pos, VAR_12);



    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 = ctx->VAR_14; int VAR_15, VAR_16; *VAR_0->coded_frame           = *VAR_2; VAR_0->coded_frame->pict_type = AV_PICTURE_TYPE_I; VAR_0->coded_frame->key_frame = 1; VAR_15 = ctx->mb_width * ctx->mb_height * 64 * 3 * 12
+ ctx->num_slices * 2 + 200 + FF_MIN_BUFFER_SIZE; if ((VAR_16 = ff_alloc_packet(VAR_1, VAR_15)) < 0) { av_log(VAR_0, AV_LOG_ERROR, "Error getting output packet.\n"); return VAR_16; } 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, "Lavc", 4); bytestream_put_be16  (&buf, VAR_0->width); bytestream_put_be16  (&buf, VAR_0->height); bytestream_put_byte  (&buf, ctx->chroma_factor << 6); bytestream_put_byte  (&buf, 0); bytestream_put_byte  (&buf, 0); bytestream_put_byte  (&buf, 0); bytestream_put_byte  (&buf, 6); bytestream_put_byte  (&buf, 0x40); bytestream_put_byte  (&buf, 0); bytestream_put_byte  (&buf, 0x03); for (VAR_6 = 0; VAR_6 < 64; VAR_6++) bytestream_put_byte(&buf, ctx->profile_info->quant[VAR_6]); for (VAR_6 = 0; VAR_6 < 64; VAR_6++) bytestream_put_byte(&buf, ctx->profile_info->quant[VAR_6]); bytestream_put_be16  (&tmp, buf - orig_buf); picture_size_pos = buf + 1; bytestream_put_byte  (&buf, 0x40);           size (in bits) buf += 4; bytestream_put_be16  (&buf, ctx->num_slices); bytestream_put_byte  (&buf, av_log2(ctx->VAR_14) << 4); slice_sizes = buf; buf += ctx->num_slices * 2; for (VAR_5 = 0; VAR_5 < ctx->mb_height; VAR_5++) { VAR_14 = ctx->VAR_14; for (VAR_4 = VAR_7 = 0; VAR_4 < ctx->mb_width; VAR_4 += VAR_14, VAR_7++) { while (ctx->mb_width - VAR_4 < VAR_14)
VAR_14 >>= 1; VAR_8 = find_slice_quant(VAR_0, VAR_2, (VAR_7 + 1) * TRELLIS_WIDTH, VAR_4, VAR_5,
VAR_14); } for (VAR_4 = ctx->slices_width - 1; VAR_4 >= 0; VAR_4--) { ctx->slice_q[VAR_4] = ctx->nodes[VAR_8].quant; VAR_8 = ctx->nodes[VAR_8].prev_node; } VAR_14 = ctx->VAR_14; for (VAR_4 = VAR_7 = 0; VAR_4 < ctx->mb_width; VAR_4 += VAR_14, VAR_7++) { VAR_8 = ctx->slice_q[VAR_7]; while (ctx->mb_width - VAR_4 < VAR_14)
VAR_14 >>= 1; bytestream_put_byte(&buf, VAR_10 << 3); slice_hdr = buf; buf += VAR_10 - 1; init_put_bits(&pb, buf, (VAR_15 - (buf - orig_buf)) * 8); encode_slice(VAR_0, VAR_2, &pb, VAR_9, VAR_4, VAR_5, VAR_8, VAR_14); 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; } } orig_buf -= 8; VAR_11 = buf - orig_buf; VAR_12 = buf - picture_size_pos - 6; bytestream_put_be32(&orig_buf, VAR_11); bytestream_put_be32(&picture_size_pos, VAR_12); VAR_1->size   = VAR_11; VAR_1->flags |= AV_PKT_FLAG_KEY; *VAR_3 = 1; return 0; }