ADPCM Codecs
Loading...
Searching...
No Matches
ByteReader.h
1/*
2 * ByteReader (bounds checked byte stream reader)
3 * Adapted from the FFmpeg bytestream functions:
4 * copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@free.fr>
5 * Copyright (c) 2012 Aneesh Dogra (lionaneesh) <lionaneesh@gmail.com>
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#include <stdio.h>
17#include <string.h>
18
19#include "adpcm.h"
20#include "assert.h"
21#include "config-adpcm.h"
22
23namespace adpcm_ffmpeg {
24
26template <int N>
27inline uint32_t loadBigEndian(const uint8_t *p) {
28 uint32_t v = 0;
29 for (int i = 0; i < N; i++) v = (v << 8) | p[i];
30 return v;
31}
32
33template <int N>
34inline uint32_t loadLittleEndian(const uint8_t *p) {
35 uint32_t v = 0;
36 for (int i = N - 1; i >= 0; i--) v = (v << 8) | p[i];
37 return v;
38}
39
46 public:
47 ByteReader() = default;
48
49 ByteReader(const uint8_t *buf, int buf_size) { init(buf, buf_size); }
50
51 void init(const uint8_t *buf, int buf_size) {
52 av_assert(buf_size >= 0);
53 buffer_ = buf;
54 start_ = buf;
55 end_ = buf + buf_size;
56 }
57
59 int bytesLeft() const { return (int)(end_ - buffer_); }
61 int tell() const { return (int)(buffer_ - start_); }
63 int size() const { return (int)(end_ - start_); }
64
66 void skip(unsigned int size) { buffer_ += FFMIN(end_ - buffer_, size); }
68 void skipUnchecked(unsigned int size) { buffer_ += size; }
69
71 int seek(int offset, int whence) {
72 switch (whence) {
73 case SEEK_CUR:
74 offset = av_clip(offset, -(buffer_ - start_), end_ - buffer_);
75 buffer_ += offset;
76 break;
77 case SEEK_END:
78 offset = av_clip(offset, -(end_ - start_), 0);
79 buffer_ = end_ + offset;
80 break;
81 case SEEK_SET:
82 offset = av_clip(offset, 0, end_ - start_);
83 buffer_ = start_ + offset;
84 break;
85 default:
86 return AVERROR(AVERROR_INVALID);
87 }
88 return tell();
89 }
90
91 // ---- unchecked reads: advance the position by the size of the value ----
92 uint32_t getByteUnchecked() { return read<1, false>(); }
93 uint32_t getLe16Unchecked() { return read<2, false>(); }
94 uint32_t getBe16Unchecked() { return read<2, true>(); }
95 uint32_t getLe32Unchecked() { return read<4, false>(); }
96 uint32_t getBe32Unchecked() { return read<4, true>(); }
97
98 // ---- checked reads: return 0 and move to the end if the value is cut ----
99 uint32_t getByte() { return readChecked<1, false>(); }
100 uint32_t getLe16() { return readChecked<2, false>(); }
101 uint32_t getBe16() { return readChecked<2, true>(); }
102 uint32_t getLe32() { return readChecked<4, false>(); }
103 uint32_t getBe32() { return readChecked<4, true>(); }
104
105 private:
106 const uint8_t *buffer_ = nullptr;
107 const uint8_t *start_ = nullptr;
108 const uint8_t *end_ = nullptr;
109
110 template <int N, bool BigEndian>
111 uint32_t read() {
112 const uint32_t v = BigEndian ? loadBigEndian<N>(buffer_)
113 : loadLittleEndian<N>(buffer_);
114 buffer_ += N;
115 return v;
116 }
117
118 template <int N, bool BigEndian>
119 uint32_t readChecked() {
120 if (end_ - buffer_ < N) {
121 buffer_ = end_;
122 return 0;
123 }
124 return read<N, BigEndian>();
125 }
126};
127
128} // namespace adpcm_ffmpeg
Vector implementation which provides the most important methods as defined by std::vector....
Definition ADPCMVector.h:16
Bounds checked reader for a memory buffer. Reads past the end return 0 and move the position to the e...
Definition ByteReader.h:45
void skip(unsigned int size)
skips up to size bytes (stops at the end of the buffer)
Definition ByteReader.h:66
int seek(int offset, int whence)
moves the position; returns the new position or an error code
Definition ByteReader.h:71
int size() const
total size of the buffer
Definition ByteReader.h:63
int tell() const
current position relative to the start of the buffer
Definition ByteReader.h:61
void skipUnchecked(unsigned int size)
skips size bytes without checking the buffer end
Definition ByteReader.h:68
int bytesLeft() const
number of bytes between the current position and the end
Definition ByteReader.h:59