ADPCM Codecs
Loading...
Searching...
No Matches
BitReader.h
1/*
2 * Bit reader (little endian bit order by default, or most significant bit
3 * first with setMsbFirst)
4 * Adapted from the FFmpeg get_bits functions:
5 * Copyright (c) 2000, 2001 Fabrice Bellard
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 */
12
13#pragma once
14
15#include <stdint.h>
16
17#include "adpcm.h"
18#include "assert.h"
19#include "config-adpcm.h"
20
21namespace adpcm_ffmpeg {
22
30class BitReader {
31 public:
32 BitReader() = default;
33
35
37 void setMsbFirst(bool msb) { msb_ = msb; }
38
41 int init(const uint8_t *buffer, int bit_size) {
42 int ret = 0;
43 if (bit_size < 0 || !buffer) {
44 bit_size = 0;
45 buffer = nullptr;
46 ret = AVERROR_INVALIDDATA;
47 }
48 buffer_ = buffer;
49 size_in_bits_ = bit_size;
50 byte_size_ = (bit_size + 7) >> 3;
51 pos_ = 0;
52 return ret;
53 }
54
56 int initBytes(const uint8_t *buffer, int byte_size) {
57 if (byte_size > INT32_MAX / 8 || byte_size < 0) byte_size = -1;
58 return init(buffer, byte_size * 8);
59 }
60
62 int bitsCount() const { return pos_; }
63
65 int bitsLeft() const { return size_in_bits_ - pos_; }
66
69 av_assert(n >= 0 && n <= 32);
70 uint32_t v = 0;
71 for (int k = 0; k < n; k++) {
72 const uint32_t b = (uint32_t)bit(pos_);
73 if (msb_)
74 v = (v << 1) | b;
75 else
76 v |= b << k;
77 pos_++;
78 }
79 return v;
80 }
81
84 av_assert(n >= 0 && n <= 31);
85 if (n == 0) return 0;
86 uint32_t v = read(n);
87 if (v & ((uint32_t)1 << (n - 1))) return (int32_t)(v - ((uint32_t)1 << n));
88 return (int32_t)v;
89 }
90
92 void skip(int n) { pos_ += n; }
93
94 private:
95 const uint8_t *buffer_ = nullptr;
96 int size_in_bits_ = 0;
97 int byte_size_ = 0;
98 int pos_ = 0;
99 bool msb_ = false;
100
101 int bit(int index) const {
102 const int byte = index >> 3;
103 if (byte >= byte_size_) return 0;
104 const int shift = msb_ ? 7 - (index & 7) : (index & 7);
105 return (buffer_[byte] >> shift) & 1;
106 }
107};
108
109} // namespace adpcm_ffmpeg
Vector implementation which provides the most important methods as defined by std::vector....
Definition ADPCMVector.h:16
Reads bits from a buffer. By default bits are taken from the least significant bit of each byte first...
Definition BitReader.h:30
int init(const uint8_t *buffer, int bit_size)
Definition BitReader.h:41
int bitsCount() const
number of bits consumed so far
Definition BitReader.h:62
int initBytes(const uint8_t *buffer, int byte_size)
Initializes the reader for byte_size bytes.
Definition BitReader.h:56
void skip(int n)
Skips n bits.
Definition BitReader.h:92
uint32_t read(int n)
Reads n bits (0 to 32)
Definition BitReader.h:68
int32_t readSigned(int n)
Reads n bits and sign extends them (0 to 31)
Definition BitReader.h:83
int bitsLeft() const
number of bits that are still available
Definition BitReader.h:65
void setMsbFirst(bool msb)
Selects the bit order used by read() and readSigned()
Definition BitReader.h:37