RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
BitStreamPosition.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2024 Roman Lebedev
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#pragma once
22
23#include "adt/Casts.h"
24#include "adt/Invariant.h"
26#include "common/Common.h"
27#include <climits>
28
29namespace rawspeed {
30
31template <BitOrder bo> struct BitStreamTraits;
32
33template <BitOrder bo> struct BitStreamPosition {
34 int pos;
36};
37
38template <BitOrder bo> struct ByteStreamPosition {
41};
42
43template <BitOrder bo>
46 const int MinByteStepMultiple = BitStreamTraits<bo>::MinLoadStepByteMultiple;
47
48 invariant(state.pos >= 0);
49 invariant(state.pos % MinByteStepMultiple == 0);
50 invariant(state.fillLevel >= 0);
51
52 auto numBytesToBacktrack = implicit_cast<int>(
53 MinByteStepMultiple *
54 roundUpDivision(state.fillLevel, CHAR_BIT * MinByteStepMultiple));
55 invariant(numBytesToBacktrack >= 0);
56 invariant(numBytesToBacktrack <= state.pos);
57 invariant(numBytesToBacktrack % MinByteStepMultiple == 0);
58
59 auto numBitsToBacktrack = CHAR_BIT * numBytesToBacktrack;
60 invariant(numBitsToBacktrack >= 0);
61
63 invariant(state.pos >= numBytesToBacktrack);
64 res.bytePos = state.pos - numBytesToBacktrack;
65 invariant(numBitsToBacktrack >= state.fillLevel);
66 res.numBitsToSkip = numBitsToBacktrack - state.fillLevel;
67 invariant(res.numBitsToSkip >= 0);
68 invariant(res.numBitsToSkip < CHAR_BIT * MinByteStepMultiple);
69
70 invariant(res.bytePos >= 0);
71 invariant(res.bytePos <= state.pos);
72 invariant(res.bytePos % MinByteStepMultiple == 0);
73 invariant(res.numBitsToSkip >= 0);
74 return res;
75}
76
77} // namespace rawspeed
#define invariant(expr)
Definition Invariant.h:27
constexpr RAWSPEED_READNONE Ttgt implicit_cast(Tsrc value)
Definition Casts.h:32
constexpr uint64_t RAWSPEED_READNONE roundUpDivision(uint64_t value, uint64_t div)
Definition Common.h:139
ByteStreamPosition< bo > getAsByteStreamPosition(BitStreamPosition< bo > state)