/src/libvpx/vpx_dsp/bitwriter_buffer.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2013 The WebM project authors. All Rights Reserved. |
3 | | * |
4 | | * Use of this source code is governed by a BSD-style license |
5 | | * that can be found in the LICENSE file in the root of the source |
6 | | * tree. An additional intellectual property rights grant can be found |
7 | | * in the file PATENTS. All contributing project authors may |
8 | | * be found in the AUTHORS file in the root of the source tree. |
9 | | */ |
10 | | |
11 | | #include <assert.h> |
12 | | #include <limits.h> |
13 | | #include <stdlib.h> |
14 | | |
15 | | #include "./vpx_config.h" |
16 | | #include "./bitwriter_buffer.h" |
17 | | |
18 | | void vpx_wb_init(struct vpx_write_bit_buffer *wb, uint8_t *bit_buffer, |
19 | 53.1k | size_t size) { |
20 | 53.1k | wb->error = 0; |
21 | 53.1k | wb->bit_offset = 0; |
22 | 53.1k | wb->size = size; |
23 | 53.1k | wb->bit_buffer = bit_buffer; |
24 | 53.1k | } |
25 | | |
26 | 106k | int vpx_wb_has_error(const struct vpx_write_bit_buffer *wb) { |
27 | 106k | return wb->error; |
28 | 106k | } |
29 | | |
30 | 53.1k | size_t vpx_wb_bytes_written(const struct vpx_write_bit_buffer *wb) { |
31 | 53.1k | assert(!wb->error); |
32 | 53.1k | return wb->bit_offset / CHAR_BIT + (wb->bit_offset % CHAR_BIT > 0); |
33 | 53.1k | } |
34 | | |
35 | 5.82M | void vpx_wb_write_bit(struct vpx_write_bit_buffer *wb, int bit) { |
36 | 5.82M | if (wb->error) return; |
37 | 5.82M | const int off = (int)wb->bit_offset; |
38 | 5.82M | const int p = off / CHAR_BIT; |
39 | 5.82M | const int q = CHAR_BIT - 1 - off % CHAR_BIT; |
40 | 5.82M | if ((size_t)p >= wb->size) { |
41 | 0 | wb->error = 1; |
42 | 0 | return; |
43 | 0 | } |
44 | 5.82M | if (q == CHAR_BIT - 1) { |
45 | 737k | wb->bit_buffer[p] = bit << q; |
46 | 5.08M | } else { |
47 | 5.08M | assert((wb->bit_buffer[p] & (1 << q)) == 0); |
48 | 5.08M | wb->bit_buffer[p] |= bit << q; |
49 | 5.08M | } |
50 | 5.82M | wb->bit_offset = off + 1; |
51 | 5.82M | } |
52 | | |
53 | 769k | void vpx_wb_write_literal(struct vpx_write_bit_buffer *wb, int data, int bits) { |
54 | 769k | int bit; |
55 | 5.48M | for (bit = bits - 1; bit >= 0; bit--) vpx_wb_write_bit(wb, (data >> bit) & 1); |
56 | 769k | } |
57 | | |
58 | | void vpx_wb_write_inv_signed_literal(struct vpx_write_bit_buffer *wb, int data, |
59 | 0 | int bits) { |
60 | 0 | vpx_wb_write_literal(wb, abs(data), bits); |
61 | 0 | vpx_wb_write_bit(wb, data < 0); |
62 | 0 | } |