Skip to content

Commit 3981592

Browse files
committed
PanasonicV8Decompressor: rewrite decodeNextDiffValue()
https://godbolt.org/z/nW1oEdMve https://alive2.llvm.org/ce/z/EJS8tG
1 parent 0cd9d53 commit 3981592

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

src/librawspeed/decompressors/PanasonicV8Decompressor.cpp

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "bitstreams/BitStreamer.h"
3434
#include "bitstreams/BitStreamerMSB.h" // IWYU pragma: keep
3535
#include "bitstreams/BitStreams.h"
36+
#include "codes/AbstractPrefixCode.h"
37+
#include "codes/AbstractPrefixCodeDecoder.h"
3638
#include "common/Common.h"
3739
#include "common/RawImage.h"
3840
#include "common/RawspeedException.h"
@@ -340,30 +342,18 @@ int32_t inline PanasonicV8Decompressor::InternalHuffDecoder::
340342
// Retrieve the difference category, which indicates magnitude of the
341343
// difference between the predicted and actual value.
342344
const auto next16 = uint16_t(mBitPump.peekBits(16));
343-
const auto& [bits, diffCat] = mLUT(next16);
344-
if (diffCat == 0 && bits == 7)
345+
const auto& [codeLen, codeValue] = mLUT(next16);
346+
if (codeValue == 0 && codeLen == 7)
345347
ThrowRDE("Huffman decoding encountered an invalid value!");
346-
mBitPump.skipBits(bits); // Skip the bits that encoded the difference category
347-
348-
if (diffCat > 0) {
349-
// Decode difference value. The scheme here encodes signed integers in a
350-
// manner similar to offset binary encoding. Here, the encoding is biased by
351-
// the difference category such that abs(diff) is in the range
352-
// [2^{diffCat-1}, 2^{diffCat}).
353-
const uint32_t rawDiffBits = mBitPump.getBits(diffCat);
354-
const uint32_t sign = rawDiffBits >> (diffCat - 1);
355-
const uint32_t val = rawDiffBits << 0;
356-
357-
// In comments below, n = diffCat
358-
if (sign == 1)
359-
// Positive value in range [2^{n-1}, 2^{n})
360-
return val;
361-
// Negative value in interval (-2^{n}, -2^{n-1}]
362-
return static_cast<int32_t>(val) + static_cast<int32_t>(~0U << diffCat) + 1;
363-
}
364-
// diffBitCount of zero indicates no difference (next pixel is same as
365-
// predicted)
366-
return 0;
348+
mBitPump.skipBits(
349+
codeLen); // Skip the bits that encoded the difference category
350+
int diffLen = codeValue;
351+
352+
if (diffLen == 0)
353+
return 0;
354+
355+
const uint32_t diff = mBitPump.getBits(diffLen);
356+
return AbstractPrefixCodeDecoder<BaselineCodeTag>::extend(diff, diffLen);
367357
}
368358

369359
} // namespace rawspeed

0 commit comments

Comments
 (0)