| 131 | |
| 132 | |
| 133 | static int encode_bitstream(FlashSVContext *s, const AVFrame *p, uint8_t *buf, |
| 134 | int buf_size, int block_width, int block_height, |
| 135 | const uint8_t *previous_frame, int *I_frame) |
| 136 | { |
| 137 | |
| 138 | PutBitContext pb; |
| 139 | int h_blocks, v_blocks, h_part, v_part, i, j; |
| 140 | int buf_pos, res; |
| 141 | int pred_blocks = 0; |
| 142 | |
| 143 | init_put_bits(&pb, buf, buf_size); |
| 144 | |
| 145 | put_bits(&pb, 4, block_width / 16 - 1); |
| 146 | put_bits(&pb, 12, s->image_width); |
| 147 | put_bits(&pb, 4, block_height / 16 - 1); |
| 148 | put_bits(&pb, 12, s->image_height); |
| 149 | flush_put_bits(&pb); |
| 150 | buf_pos = 4; |
| 151 | |
| 152 | h_blocks = s->image_width / block_width; |
| 153 | h_part = s->image_width % block_width; |
| 154 | v_blocks = s->image_height / block_height; |
| 155 | v_part = s->image_height % block_height; |
| 156 | |
| 157 | /* loop over all block columns */ |
| 158 | for (j = 0; j < v_blocks + (v_part ? 1 : 0); j++) { |
| 159 | |
| 160 | int y_pos = j * block_height; // vertical position in frame |
| 161 | int cur_blk_height = (j < v_blocks) ? block_height : v_part; |
| 162 | |
| 163 | /* loop over all block rows */ |
| 164 | for (i = 0; i < h_blocks + (h_part ? 1 : 0); i++) { |
| 165 | int x_pos = i * block_width; // horizontal position in frame |
| 166 | int cur_blk_width = (i < h_blocks) ? block_width : h_part; |
| 167 | int ret = Z_OK; |
| 168 | uint8_t *ptr = buf + buf_pos; |
| 169 | |
| 170 | /* copy the block to the temp buffer before compression |
| 171 | * (if it differs from the previous frame's block) */ |
| 172 | res = copy_region_enc(p->data[0], s->tmpblock, |
| 173 | s->image_height - (y_pos + cur_blk_height + 1), |
| 174 | x_pos, cur_blk_height, cur_blk_width, |
| 175 | p->linesize[0], previous_frame); |
| 176 | |
| 177 | if (res || *I_frame) { |
| 178 | unsigned long zsize = 3 * block_width * block_height + 12; |
| 179 | ret = compress2(ptr + 2, &zsize, s->tmpblock, |
| 180 | 3 * cur_blk_width * cur_blk_height, |
| 181 | s->compression_level); |
| 182 | |
| 183 | if (ret != Z_OK) |
| 184 | av_log(s->avctx, AV_LOG_ERROR, |
| 185 | "error while compressing block %dx%d\n", i, j); |
| 186 | |
| 187 | bytestream_put_be16(&ptr, zsize); |
| 188 | buf_pos += zsize + 2; |
| 189 | ff_dlog(s->avctx, "buf_pos = %d\n", buf_pos); |
| 190 | } else { |
no test coverage detected