2014-03-04 03:40:13 +04:00
|
|
|
/*
|
2016-09-02 00:32:49 +03:00
|
|
|
* Copyright (c) 2016, Alliance for Open Media. All rights reserved
|
2014-03-04 03:40:13 +04:00
|
|
|
*
|
2016-09-02 00:32:49 +03:00
|
|
|
* This source code is subject to the terms of the BSD 2 Clause License and
|
|
|
|
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
|
|
|
|
* was not distributed with this source code in the LICENSE file, you can
|
|
|
|
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
|
|
|
|
* Media Patent License 1.0 was not distributed with this source code in the
|
|
|
|
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
|
2014-03-04 03:40:13 +04:00
|
|
|
*/
|
|
|
|
|
2014-08-03 01:14:22 +04:00
|
|
|
#include <limits.h>
|
2015-09-08 21:26:42 +03:00
|
|
|
#include <stdlib.h>
|
2015-07-20 21:05:04 +03:00
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
#include "./aom_config.h"
|
2015-07-20 21:18:57 +03:00
|
|
|
#include "./bitwriter_buffer.h"
|
2014-03-04 03:40:13 +04:00
|
|
|
|
2017-04-21 02:03:13 +03:00
|
|
|
uint32_t aom_wb_bytes_written(const struct aom_write_bit_buffer *wb) {
|
2014-03-04 03:40:13 +04:00
|
|
|
return wb->bit_offset / CHAR_BIT + (wb->bit_offset % CHAR_BIT > 0);
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
void aom_wb_write_bit(struct aom_write_bit_buffer *wb, int bit) {
|
2014-03-04 03:40:13 +04:00
|
|
|
const int off = (int)wb->bit_offset;
|
|
|
|
const int p = off / CHAR_BIT;
|
|
|
|
const int q = CHAR_BIT - 1 - off % CHAR_BIT;
|
2016-11-11 07:48:29 +03:00
|
|
|
if (q == CHAR_BIT - 1) {
|
2016-11-14 14:59:43 +03:00
|
|
|
// Zero next char and write bit
|
2016-11-11 07:48:29 +03:00
|
|
|
wb->bit_buffer[p] = bit << q;
|
|
|
|
} else {
|
|
|
|
wb->bit_buffer[p] &= ~(1 << q);
|
|
|
|
wb->bit_buffer[p] |= bit << q;
|
|
|
|
}
|
2014-03-04 03:40:13 +04:00
|
|
|
wb->bit_offset = off + 1;
|
|
|
|
}
|
|
|
|
|
2016-11-14 14:59:43 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
void aom_wb_write_literal(struct aom_write_bit_buffer *wb, int data, int bits) {
|
2014-03-04 03:40:13 +04:00
|
|
|
int bit;
|
2016-08-31 00:01:10 +03:00
|
|
|
for (bit = bits - 1; bit >= 0; bit--) aom_wb_write_bit(wb, (data >> bit) & 1);
|
2014-03-04 03:40:13 +04:00
|
|
|
}
|
2015-09-08 21:26:42 +03:00
|
|
|
|
2016-11-14 14:59:43 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-08-31 00:01:10 +03:00
|
|
|
void aom_wb_write_inv_signed_literal(struct aom_write_bit_buffer *wb, int data,
|
2016-08-09 08:59:08 +03:00
|
|
|
int bits) {
|
2016-08-31 00:01:10 +03:00
|
|
|
aom_wb_write_literal(wb, data, bits + 1);
|
2015-09-08 21:26:42 +03:00
|
|
|
}
|