Skip to content

Commit 3497b7c

Browse files
author
Asura
authored
Ensure MDEC macroblocks decode fully before merging into output (#82)
1 parent 0858d6d commit 3497b7c

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

Diff for: src/device/mdec/algorithm.cpp

+14-12
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,28 @@ void MDEC::yuvToRgb(decodedBlock& output, int blockX, int blockY) {
7575
void MDEC::decodeMacroblocks() {
7676
for (auto src = input.begin(); src != input.end();) {
7777
auto block = decodeMacroblock(src);
78+
if (!block) continue;
7879

79-
output.insert(output.end(), block.begin(), block.end());
80+
output.insert(output.end(), (*block).begin(), (*block).end());
8081
}
8182
}
8283

83-
decodedBlock MDEC::decodeMacroblock(std::vector<uint16_t>::iterator& src) {
84-
decodeBlock(crblk, src, colorQuantTable);
85-
decodeBlock(cbblk, src, colorQuantTable);
84+
std::optional<decodedBlock> MDEC::decodeMacroblock(std::vector<uint16_t>::iterator& src) {
85+
if (!decodeBlock(crblk, src, colorQuantTable)) return std::nullopt;
86+
if (!decodeBlock(cbblk, src, colorQuantTable)) return std::nullopt;
8687

87-
decodeBlock(yblk[0], src, luminanceQuantTable);
88-
decodeBlock(yblk[1], src, luminanceQuantTable);
89-
decodeBlock(yblk[2], src, luminanceQuantTable);
90-
decodeBlock(yblk[3], src, luminanceQuantTable);
88+
if (!decodeBlock(yblk[0], src, luminanceQuantTable)) return std::nullopt;
89+
if (!decodeBlock(yblk[1], src, luminanceQuantTable)) return std::nullopt;
90+
if (!decodeBlock(yblk[2], src, luminanceQuantTable)) return std::nullopt;
91+
if (!decodeBlock(yblk[3], src, luminanceQuantTable)) return std::nullopt;
9192

9293
decodedBlock out;
9394
yuvToRgb(out, 0, 0);
9495
yuvToRgb(out, 0, 8);
9596
yuvToRgb(out, 8, 0);
9697
yuvToRgb(out, 8, 8);
9798

98-
return out;
99+
return std::optional<decodedBlock>(out);
99100
// Handle format conversion
100101
}
101102

@@ -119,7 +120,7 @@ union RLE {
119120
RLE(uint16_t val) : _(val){};
120121
};
121122

122-
void MDEC::decodeBlock(std::array<int16_t, 64>& blk, std::vector<uint16_t>::iterator& src, const std::array<uint8_t, 64>& table) {
123+
bool MDEC::decodeBlock(std::array<int16_t, 64>& blk, std::vector<uint16_t>::iterator& src, const std::array<uint8_t, 64>& table) {
123124
blk.fill(0);
124125

125126
// Block structure:
@@ -132,7 +133,7 @@ void MDEC::decodeBlock(std::array<int16_t, 64>& blk, std::vector<uint16_t>::iter
132133
src++; // Skip padding
133134
}
134135

135-
if (src == input.end()) return;
136+
if (src == input.end()) return false;
136137
DCT dct = *src++;
137138
int32_t current = extend_sign<10>(dct.dc);
138139

@@ -151,7 +152,7 @@ void MDEC::decodeBlock(std::array<int16_t, 64>& blk, std::vector<uint16_t>::iter
151152
blk.at(n) = value;
152153
}
153154

154-
if (src == input.end()) break;
155+
if (src == input.end()) return false;
155156
RLE rle = *src++;
156157
current = extend_sign<10>(rle.ac);
157158
n += rle.zeroes + 1;
@@ -160,6 +161,7 @@ void MDEC::decodeBlock(std::array<int16_t, 64>& blk, std::vector<uint16_t>::iter
160161
}
161162

162163
idct(blk);
164+
return true;
163165
}
164166

165167
void MDEC::idct(std::array<int16_t, 64>& src) {

Diff for: src/device/mdec/mdec.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <array>
33
#include <vector>
4+
#include <optional>
45
#include "device/device.h"
56

67
namespace mdec {
@@ -65,8 +66,8 @@ class MDEC {
6566
// Algorithm
6667
void decodeMacroblocks();
6768
void yuvToRgb(decodedBlock& output, int blockX, int blockY);
68-
decodedBlock decodeMacroblock(std::vector<uint16_t>::iterator& src);
69-
void decodeBlock(std::array<int16_t, 64>& blk, std::vector<uint16_t>::iterator& src, const std::array<uint8_t, 64>& table);
69+
std::optional<decodedBlock> decodeMacroblock(std::vector<uint16_t>::iterator& src);
70+
bool decodeBlock(std::array<int16_t, 64>& blk, std::vector<uint16_t>::iterator& src, const std::array<uint8_t, 64>& table);
7071
void idct(std::array<int16_t, 64>& src);
7172

7273
public:
@@ -84,4 +85,4 @@ class MDEC {
8485
ar(input, output, outputPtr);
8586
}
8687
};
87-
}; // namespace mdec
88+
}; // namespace mdec

0 commit comments

Comments
 (0)