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
"
25
#include "
bitstreams/BitStreams.h
"
26
#include "
common/Common.h
"
27
#include <climits>
28
29
namespace
rawspeed
{
30
31
template
<BitOrder bo>
struct
BitStreamTraits
;
32
33
template
<BitOrder bo>
struct
BitStreamPosition
{
34
int
pos
;
35
int
fillLevel
;
36
};
37
38
template
<BitOrder bo>
struct
ByteStreamPosition
{
39
int
bytePos
;
40
int
numBitsToSkip
;
41
};
42
43
template
<BitOrder bo>
44
requires
BitStreamTraits<bo>::FixedSizeChunks
45
ByteStreamPosition<bo>
getAsByteStreamPosition
(
BitStreamPosition<bo>
state) {
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
62
ByteStreamPosition<bo>
res;
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
BitStreams.h
Casts.h
Invariant.h
invariant
#define invariant(expr)
Definition
Invariant.h:27
rawspeed
Definition
CoalescingOutputIteratorBenchmark.cpp:35
rawspeed::implicit_cast
constexpr RAWSPEED_READNONE Ttgt implicit_cast(Tsrc value)
Definition
Casts.h:32
rawspeed::roundUpDivision
constexpr uint64_t RAWSPEED_READNONE roundUpDivision(uint64_t value, uint64_t div)
Definition
Common.h:139
rawspeed::getAsByteStreamPosition
ByteStreamPosition< bo > getAsByteStreamPosition(BitStreamPosition< bo > state)
Definition
BitStreamPosition.h:45
Common.h
rawspeed::BitStreamPosition
Definition
BitStreamPosition.h:33
rawspeed::BitStreamPosition::pos
int pos
Definition
BitStreamPosition.h:34
rawspeed::BitStreamPosition::fillLevel
int fillLevel
Definition
BitStreamPosition.h:35
rawspeed::BitStreamTraits
Definition
BitStream.h:33
rawspeed::ByteStreamPosition
Definition
BitStreamPosition.h:38
rawspeed::ByteStreamPosition::numBitsToSkip
int numBitsToSkip
Definition
BitStreamPosition.h:40
rawspeed::ByteStreamPosition::bytePos
int bytePos
Definition
BitStreamPosition.h:39
librawspeed
bitstreams
BitStreamPosition.h
Generated by
1.15.0