static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf,

                            int buf_size, int block_width, int block_height,

                            uint8_t *previous_frame, int *I_frame)

{



    PutBitContext pb;

    int h_blocks, v_blocks, h_part, v_part, i, j;

    int buf_pos, res;

    int pred_blocks = 0;



    init_put_bits(&pb, buf, buf_size * 8);



    put_bits(&pb,  4, block_width / 16 - 1);

    put_bits(&pb, 12, s->image_width);

    put_bits(&pb,  4, block_height / 16 - 1);

    put_bits(&pb, 12, s->image_height);

    flush_put_bits(&pb);

    buf_pos = 4;



    h_blocks = s->image_width  / block_width;

    h_part   = s->image_width  % block_width;

    v_blocks = s->image_height / block_height;

    v_part   = s->image_height % block_height;



    /* loop over all block columns */

    for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {



        int y_pos = j * block_height; // vertical position in frame

        int cur_blk_height = (j < v_blocks) ? block_height : v_part;



        /* loop over all block rows */

        for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {

            int x_pos = i * block_width; // horizontal position in frame

            int cur_blk_width = (i < h_blocks) ? block_width : h_part;

            int ret = Z_OK;

            uint8_t *ptr = buf + buf_pos;



            /* copy the block to the temp buffer before compression

             * (if it differs from the previous frame's block) */

            res = copy_region_enc(p->data[0], s->tmpblock,

                                  s->image_height - (y_pos + cur_blk_height + 1),

                                  x_pos, cur_blk_height, cur_blk_width,

                                  p->linesize[0], previous_frame);



            if (res || *I_frame) {

                unsigned long zsize = 3 * block_width * block_height;

                ret = compress2(ptr + 2, &zsize, s->tmpblock,

                                3 * cur_blk_width * cur_blk_height, 9);



                //ret = deflateReset(&s->zstream);

                if (ret != Z_OK)

                    av_log(s->avctx, AV_LOG_ERROR,

                           "error while compressing block %dx%d\n", i, j);



                bytestream_put_be16(&ptr, zsize);

                buf_pos += zsize + 2;

                av_dlog(s->avctx, "buf_pos = %d\n", buf_pos);

            } else {

                pred_blocks++;

                bytestream_put_be16(&ptr, 0);

                buf_pos += 2;

            }

        }

    }



    if (pred_blocks)

        *I_frame = 0;

    else

        *I_frame = 1;



    return buf_pos;

}

static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf,

                            int buf_size, int block_width, int block_height,

                            uint8_t *previous_frame, int *I_frame)

{



    PutBitContext pb;

    int h_blocks, v_blocks, h_part, v_part, i, j;

    int buf_pos, res;

    int pred_blocks = 0;



    init_put_bits(&pb, buf, buf_size * 8);



    put_bits(&pb,  4, block_width / 16 - 1);

    put_bits(&pb, 12, s->image_width);

    put_bits(&pb,  4, block_height / 16 - 1);

    put_bits(&pb, 12, s->image_height);

    flush_put_bits(&pb);

    buf_pos = 4;



    h_blocks = s->image_width  / block_width;

    h_part   = s->image_width  % block_width;

    v_blocks = s->image_height / block_height;

    v_part   = s->image_height % block_height;



    

    for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) {



        int y_pos = j * block_height; 

        int cur_blk_height = (j < v_blocks) ? block_height : v_part;



        

        for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) {

            int x_pos = i * block_width; 

            int cur_blk_width = (i < h_blocks) ? block_width : h_part;

            int ret = Z_OK;

            uint8_t *ptr = buf + buf_pos;



            



            res = copy_region_enc(p->data[0], s->tmpblock,

                                  s->image_height - (y_pos + cur_blk_height + 1),

                                  x_pos, cur_blk_height, cur_blk_width,

                                  p->linesize[0], previous_frame);



            if (res || *I_frame) {

                unsigned long zsize = 3 * block_width * block_height;

                ret = compress2(ptr + 2, &zsize, s->tmpblock,

                                3 * cur_blk_width * cur_blk_height, 9);



                

                if (ret != Z_OK)

                    av_log(s->avctx, AV_LOG_ERROR,

                           "error while compressing block %dx%d\n", i, j);



                bytestream_put_be16(&ptr, zsize);

                buf_pos += zsize + 2;

                av_dlog(s->avctx, "buf_pos = %d\n", buf_pos);

            } else {

                pred_blocks++;

                bytestream_put_be16(&ptr, 0);

                buf_pos += 2;

            }

        }

    }



    if (pred_blocks)

        *I_frame = 0;

    else

        *I_frame = 1;



    return buf_pos;

}

    init_put_bits(&pb, buf, buf_size * 8);     init_put_bits(&pb, buf, buf_size * 8);
static int FUNC_0(FlashSVContext *VAR_0, const AVFrame *VAR_1, uint8_t *VAR_2,

                            int VAR_3, int VAR_4, int VAR_5,

                            uint8_t *VAR_6, int *VAR_7)

{



    PutBitContext pb;

    int VAR_8, VAR_9, VAR_10, VAR_11, VAR_12, VAR_13;

    int VAR_14, VAR_15;

    int VAR_16 = 0;



    init_put_bits(&pb, VAR_2, VAR_3 * 8);



    put_bits(&pb,  4, VAR_4 / 16 - 1);

    put_bits(&pb, 12, VAR_0->image_width);

    put_bits(&pb,  4, VAR_5 / 16 - 1);

    put_bits(&pb, 12, VAR_0->image_height);

    flush_put_bits(&pb);

    VAR_14 = 4;



    VAR_8 = VAR_0->image_width  / VAR_4;

    VAR_10   = VAR_0->image_width  % VAR_4;

    VAR_9 = VAR_0->image_height / VAR_5;

    VAR_11   = VAR_0->image_height % VAR_5;



    

    for (VAR_13 = 0; VAR_13 < VAR_9 + (VAR_11 ? 1 : 0); VAR_13++) {



        int VAR_17 = VAR_13 * VAR_5; 

        int VAR_18 = (VAR_13 < VAR_9) ? VAR_5 : VAR_11;



        

        for (VAR_12 = 0; VAR_12 < VAR_8 + (VAR_10 ? 1 : 0); VAR_12++) {

            int VAR_19 = VAR_12 * VAR_4; 

            int VAR_20 = (VAR_12 < VAR_8) ? VAR_4 : VAR_10;

            int VAR_21 = Z_OK;

            uint8_t *ptr = VAR_2 + VAR_14;



            



            VAR_15 = copy_region_enc(VAR_1->data[0], VAR_0->tmpblock,

                                  VAR_0->image_height - (VAR_17 + VAR_18 + 1),

                                  VAR_19, VAR_18, VAR_20,

                                  VAR_1->linesize[0], VAR_6);



            if (VAR_15 || *VAR_7) {

                unsigned long VAR_22 = 3 * VAR_4 * VAR_5;

                VAR_21 = compress2(ptr + 2, &VAR_22, VAR_0->tmpblock,

                                3 * VAR_20 * VAR_18, 9);



                

                if (VAR_21 != Z_OK)

                    av_log(VAR_0->avctx, AV_LOG_ERROR,

                           "error while compressing block %dx%d\n", VAR_12, VAR_13);



                bytestream_put_be16(&ptr, VAR_22);

                VAR_14 += VAR_22 + 2;

                av_dlog(VAR_0->avctx, "VAR_14 = %d\n", VAR_14);

            } else {

                VAR_16++;

                bytestream_put_be16(&ptr, 0);

                VAR_14 += 2;

            }

        }

    }



    if (VAR_16)

        *VAR_7 = 0;

    else

        *VAR_7 = 1;



    return VAR_14;

}

static int FUNC_0(FlashSVContext *VAR_0, const AVFrame *VAR_1, uint8_t *VAR_2,
int VAR_3, int VAR_4, int VAR_5,
uint8_t *VAR_6, int *VAR_7)
{ PutBitContext pb; int VAR_8, VAR_9, VAR_10, VAR_11, VAR_12, VAR_13; int VAR_14, VAR_15; int VAR_16 = 0; init_put_bits(&pb, VAR_2, VAR_3 * 8); put_bits(&pb,  4, VAR_4 / 16 - 1); put_bits(&pb, 12, VAR_0->image_width); put_bits(&pb,  4, VAR_5 / 16 - 1); put_bits(&pb, 12, VAR_0->image_height); flush_put_bits(&pb); VAR_14 = 4; VAR_8 = VAR_0->image_width  / VAR_4; VAR_10   = VAR_0->image_width  % VAR_4; VAR_9 = VAR_0->image_height / VAR_5; VAR_11   = VAR_0->image_height % VAR_5; for (VAR_13 = 0; VAR_13 < VAR_9 + (VAR_11 ? 1 : 0); VAR_13++) { int VAR_17 = VAR_13 * VAR_5; int VAR_18 = (VAR_13 < VAR_9) ? VAR_5 : VAR_11; for (VAR_12 = 0; VAR_12 < VAR_8 + (VAR_10 ? 1 : 0); VAR_12++) { int VAR_19 = VAR_12 * VAR_4; int VAR_20 = (VAR_12 < VAR_8) ? VAR_4 : VAR_10; int VAR_21 = Z_OK; uint8_t *ptr = VAR_2 + VAR_14; VAR_15 = copy_region_enc(VAR_1->data[0], VAR_0->tmpblock,
VAR_0->image_height - (VAR_17 + VAR_18 + 1),
VAR_19, VAR_18, VAR_20,
VAR_1->linesize[0], VAR_6); if (VAR_15 || *VAR_7) { unsigned long VAR_22 = 3 * VAR_4 * VAR_5; VAR_21 = compress2(ptr + 2, &VAR_22, VAR_0->tmpblock,
3 * VAR_20 * VAR_18, 9); if (VAR_21 != Z_OK)
av_log(VAR_0->avctx, AV_LOG_ERROR,
"error while compressing block %dx%d\n", VAR_12, VAR_13); bytestream_put_be16(&ptr, VAR_22); VAR_14 += VAR_22 + 2; av_dlog(VAR_0->avctx, "VAR_14 = %d\n", VAR_14); } else { VAR_16++; bytestream_put_be16(&ptr, 0); VAR_14 += 2; } } } if (VAR_16)
*VAR_7 = 0; else
*VAR_7 = 1; return VAR_14; }