/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 | 42.2k | size_t size) { |
20 | 42.2k | wb->error = 0; |
21 | 42.2k | wb->bit_offset = 0; |
22 | 42.2k | wb->size = size; |
23 | 42.2k | wb->bit_buffer = bit_buffer; |
24 | 42.2k | } |
25 | | |
26 | 84.5k | int vpx_wb_has_error(const struct vpx_write_bit_buffer *wb) { |
27 | 84.5k | return wb->error; |
28 | 84.5k | } |
29 | | |
30 | 42.2k | size_t vpx_wb_bytes_written(const struct vpx_write_bit_buffer *wb) { |
31 | 42.2k | assert(!wb->error); |
32 | 42.2k | return wb->bit_offset / CHAR_BIT + (wb->bit_offset % CHAR_BIT > 0); |
33 | 42.2k | } |
34 | | |
35 | 4.50M | void vpx_wb_write_bit(struct vpx_write_bit_buffer *wb, int bit) { |
36 | 4.50M | if (wb->error) return; |
37 | 4.50M | const int off = (int)wb->bit_offset; |
38 | 4.50M | const int p = off / CHAR_BIT; |
39 | 4.50M | const int q = CHAR_BIT - 1 - off % CHAR_BIT; |
40 | 4.50M | if ((size_t)p >= wb->size) { |
41 | 0 | wb->error = 1; |
42 | 0 | return; |
43 | 0 | } |
44 | 4.50M | if (q == CHAR_BIT - 1) { |
45 | 569k | wb->bit_buffer[p] = bit << q; |
46 | 3.93M | } else { |
47 | 3.93M | assert((wb->bit_buffer[p] & (1 << q)) == 0); |
48 | 3.93M | wb->bit_buffer[p] |= bit << q; |
49 | 3.93M | } |
50 | 4.50M | wb->bit_offset = off + 1; |
51 | 4.50M | } |
52 | | |
53 | 604k | void vpx_wb_write_literal(struct vpx_write_bit_buffer *wb, int data, int bits) { |
54 | 604k | int bit; |
55 | 4.22M | for (bit = bits - 1; bit >= 0; bit--) vpx_wb_write_bit(wb, (data >> bit) & 1); |
56 | 604k | } |
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 | } |