Commit faa7fcfe authored by Thomas Davies's avatar Thomas Davies Committed by Yaowu Xu

Add overwrite functions which do not zero bytes.

In several places bits are overwritten in the bitstream. These
functions avoid zeroing bytes during writing so that this can
happen correctly when the number of bits is not 8*N.

Re-addresses the attempted fix in
133c57c3

which broke threaded encoding tests, which relied on re-using
byte buffers.

Change-Id: I682c5e3a7869eac7ad475584db8bf170d47a56c9
parent e0f8c559
......@@ -24,6 +24,7 @@ void aom_wb_write_bit(struct aom_write_bit_buffer *wb, int bit) {
const int p = off / CHAR_BIT;
const int q = CHAR_BIT - 1 - off % CHAR_BIT;
if (q == CHAR_BIT - 1) {
// Zero next char and write bit
wb->bit_buffer[p] = bit << q;
} else {
wb->bit_buffer[p] &= ~(1 << q);
......@@ -32,11 +33,28 @@ void aom_wb_write_bit(struct aom_write_bit_buffer *wb, int bit) {
wb->bit_offset = off + 1;
}
void aom_wb_overwrite_bit(struct aom_write_bit_buffer *wb, int bit) {
// Do not zero bytes but overwrite exisiting values
const int off = (int)wb->bit_offset;
const int p = off / CHAR_BIT;
const int q = CHAR_BIT - 1 - off % CHAR_BIT;
wb->bit_buffer[p] &= ~(1 << q);
wb->bit_buffer[p] |= bit << q;
wb->bit_offset = off + 1;
}
void aom_wb_write_literal(struct aom_write_bit_buffer *wb, int data, int bits) {
int bit;
for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1);
}
void aom_wb_overwrite_literal(struct aom_write_bit_buffer *wb, int data,
int bits) {
int bit;
for (bit = bits - 1; bit >= 0; bit--)
aom_wb_overwrite_bit(wb, (data >> bit) & 1);
}
void aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer *wb, int data,
int bits) {
aom_wb_write_literal(wb, data, bits + 1);
......
......@@ -27,8 +27,13 @@ size_t aom_wb_bytes_written(const struct aom_write_bit_buffer *wb);
void aom_wb_write_bit(struct aom_write_bit_buffer *wb, int bit);
void aom_wb_overwrite_bit(struct aom_write_bit_buffer *wb, int bit);
void aom_wb_write_literal(struct aom_write_bit_buffer *wb, int data, int bits);
void aom_wb_overwrite_literal(struct aom_write_bit_buffer *wb, int data,
int bits);
void aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer *wb, int data,
int bits);
......
......@@ -3516,7 +3516,7 @@ static uint32_t write_tiles(AV1_COMP *const cpi, uint8_t *const dst,
uncompressed_hdr_size = aom_wb_bytes_written(wb);
dst = wb->bit_buffer;
comp_hdr_size = write_compressed_header(cpi, dst + uncompressed_hdr_size);
aom_wb_write_literal(&comp_hdr_len_wb, (int)(comp_hdr_size), 16);
aom_wb_overwrite_literal(&comp_hdr_len_wb, (int)(comp_hdr_size), 16);
hdr_size = uncompressed_hdr_size + comp_hdr_size;
total_size += hdr_size;
#endif
......@@ -3559,9 +3559,9 @@ static uint32_t write_tiles(AV1_COMP *const cpi, uint8_t *const dst,
uncompressed_hdr_size * sizeof(uint8_t));
// Write the number of tiles in the group into the last uncompressed
// header before the one we've just inserted
aom_wb_write_literal(&tg_params_wb, tile_idx - tile_count,
n_log2_tiles);
aom_wb_write_literal(&tg_params_wb, tile_count - 2, n_log2_tiles);
aom_wb_overwrite_literal(&tg_params_wb, tile_idx - tile_count,
n_log2_tiles);
aom_wb_overwrite_literal(&tg_params_wb, tile_count - 2, n_log2_tiles);
// Update the pointer to the last TG params
tg_params_wb.bit_offset = saved_offset + 8 * old_total_size;
// Copy compressed header
......@@ -3578,9 +3578,9 @@ static uint32_t write_tiles(AV1_COMP *const cpi, uint8_t *const dst,
uncompressed_hdr_size * sizeof(uint8_t));
// Write the number of tiles in the group into the last uncompressed
// header
aom_wb_write_literal(&tg_params_wb, tile_idx - tile_count,
n_log2_tiles);
aom_wb_write_literal(&tg_params_wb, tile_count - 1, n_log2_tiles);
aom_wb_overwrite_literal(&tg_params_wb, tile_idx - tile_count,
n_log2_tiles);
aom_wb_overwrite_literal(&tg_params_wb, tile_count - 1, n_log2_tiles);
tg_params_wb.bit_offset = saved_offset + 8 * total_size;
// Copy compressed header
memmove(dst + total_size + uncompressed_hdr_size,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment