-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_ext.cpp
More file actions
340 lines (298 loc) · 9.97 KB
/
Copy path_ext.cpp
File metadata and controls
340 lines (298 loc) · 9.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <cstdint>
#include <cstring>
#include <limits>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
namespace py = pybind11;
namespace {
class BufferView {
public:
explicit BufferView(py::handle object) {
if (PyObject_GetBuffer(object.ptr(), &view_, PyBUF_CONTIG_RO) != 0) {
throw py::type_error(
"input must be a contiguous bytes-like object");
}
if (view_.len < 0) {
PyBuffer_Release(&view_);
throw py::value_error("input buffer has a negative length");
}
acquired_ = true;
}
BufferView(const BufferView&) = delete;
BufferView& operator=(const BufferView&) = delete;
~BufferView() {
if (acquired_) {
PyBuffer_Release(&view_);
}
}
const uint8_t* data() const {
return static_cast<const uint8_t*>(view_.buf);
}
size_t size() const {
return static_cast<size_t>(view_.len);
}
private:
Py_buffer view_{};
bool acquired_ = false;
};
struct Vp8Frame {
const uint8_t* data = nullptr;
size_t size = 0;
uint16_t width = 0;
uint16_t height = 0;
};
uint16_t read_le16(const uint8_t* data) {
return static_cast<uint16_t>(data[0]) |
(static_cast<uint16_t>(data[1]) << 8);
}
uint32_t read_le32(const uint8_t* data) {
return static_cast<uint32_t>(data[0]) |
(static_cast<uint32_t>(data[1]) << 8) |
(static_cast<uint32_t>(data[2]) << 16) |
(static_cast<uint32_t>(data[3]) << 24);
}
bool tag_equals(const uint8_t* tag, const char (&expected)[5]) {
return std::memcmp(tag, expected, 4) == 0;
}
[[noreturn]] void unsupported(const std::string& message) {
throw py::value_error("unsupported WebP: " + message);
}
Vp8Frame validate_vp8_payload(const uint8_t* data, size_t size) {
if (size < 10) {
throw py::value_error("invalid VP8 payload: key-frame header is truncated");
}
if ((data[0] & 1U) != 0) {
throw py::value_error(
"invalid VP8 payload: WebP must contain a VP8 key frame");
}
if (data[3] != 0x9d || data[4] != 0x01 || data[5] != 0x2a) {
throw py::value_error("invalid VP8 payload: key-frame start code is missing");
}
const uint16_t width = read_le16(data + 6) & 0x3fffU;
const uint16_t height = read_le16(data + 8) & 0x3fffU;
if (width == 0 || height == 0) {
throw py::value_error("invalid VP8 payload: dimensions must be non-zero");
}
return Vp8Frame{data, size, width, height};
}
Vp8Frame parse_webp(const uint8_t* data, size_t size) {
if (size < 12) {
throw py::value_error("invalid WebP: RIFF header is truncated");
}
if (!tag_equals(data, "RIFF")) {
throw py::value_error("invalid WebP: missing RIFF signature");
}
if (!tag_equals(data + 8, "WEBP")) {
throw py::value_error("invalid WebP: missing WEBP signature");
}
const uint32_t riff_size = read_le32(data + 4);
if (riff_size < 4) {
throw py::value_error("invalid WebP: RIFF size is smaller than WEBP form");
}
const uint64_t riff_end64 = 8ULL + static_cast<uint64_t>(riff_size);
if (riff_end64 > size) {
throw py::value_error("invalid WebP: RIFF payload is truncated");
}
const size_t riff_end = static_cast<size_t>(riff_end64);
Vp8Frame result;
bool found_vp8 = false;
size_t position = 12;
while (position < riff_end) {
if (riff_end - position < 8) {
throw py::value_error("invalid WebP: RIFF chunk header is truncated");
}
const uint8_t* tag = data + position;
const uint32_t chunk_size = read_le32(tag + 4);
const uint64_t payload_start = static_cast<uint64_t>(position) + 8;
const uint64_t payload_end =
payload_start + static_cast<uint64_t>(chunk_size);
const uint64_t padded_end = payload_end + (chunk_size & 1U);
if (payload_end > riff_end || padded_end > riff_end) {
throw py::value_error("invalid WebP: RIFF chunk payload is truncated");
}
const uint8_t* payload = data + static_cast<size_t>(payload_start);
if (tag_equals(tag, "VP8 ")) {
if (found_vp8) {
throw py::value_error("invalid WebP: multiple VP8 chunks");
}
result = validate_vp8_payload(payload, chunk_size);
found_vp8 = true;
} else if (tag_equals(tag, "VP8L")) {
unsupported("VP8L lossless bitstreams cannot be decoded by NVDEC");
} else if (tag_equals(tag, "ALPH")) {
unsupported("embedded ALPH chunks require a separate alpha decoder");
} else if (tag_equals(tag, "ANIM") || tag_equals(tag, "ANMF")) {
unsupported("animated WebP is not supported");
}
position = static_cast<size_t>(padded_end);
}
if (!found_vp8) {
throw py::value_error("invalid WebP: no lossy VP8 chunk found");
}
return result;
}
void append_le16(std::string& output, uint16_t value) {
output.push_back(static_cast<char>(value & 0xffU));
output.push_back(static_cast<char>((value >> 8) & 0xffU));
}
void append_le32(std::string& output, uint32_t value) {
for (int shift = 0; shift < 32; shift += 8) {
output.push_back(static_cast<char>((value >> shift) & 0xffU));
}
}
void append_le64(std::string& output, uint64_t value) {
for (int shift = 0; shift < 64; shift += 8) {
output.push_back(static_cast<char>((value >> shift) & 0xffU));
}
}
void validate_ivf_arguments(
size_t frame_count,
uint16_t width,
uint16_t height,
uint32_t rate,
uint32_t scale) {
if (frame_count == 0) {
throw py::value_error("at least one VP8 frame is required");
}
if (frame_count > std::numeric_limits<uint32_t>::max()) {
throw std::overflow_error("IVF frame count exceeds uint32");
}
if (width == 0 || height == 0) {
throw py::value_error("IVF dimensions must be non-zero");
}
if (rate == 0 || scale == 0) {
throw py::value_error("IVF rate and scale must be non-zero");
}
}
std::string make_ivf(
const std::vector<Vp8Frame>& frames,
uint16_t width,
uint16_t height,
uint32_t rate,
uint32_t scale) {
validate_ivf_arguments(frames.size(), width, height, rate, scale);
uint64_t output_size = 32;
for (const Vp8Frame& frame : frames) {
if (frame.width != width || frame.height != height) {
throw py::value_error(
"all VP8 frames in an IVF stream must have identical dimensions");
}
if (frame.size > std::numeric_limits<uint32_t>::max()) {
throw std::overflow_error("VP8 frame size exceeds IVF uint32 field");
}
output_size += 12ULL + frame.size;
if (output_size >
static_cast<uint64_t>(std::numeric_limits<Py_ssize_t>::max())) {
throw std::overflow_error(
"IVF output is too large for a Python bytes object");
}
}
std::string output;
output.reserve(static_cast<size_t>(output_size));
output.append("DKIF", 4);
append_le16(output, 0);
append_le16(output, 32);
output.append("VP80", 4);
append_le16(output, width);
append_le16(output, height);
append_le32(output, rate);
append_le32(output, scale);
append_le32(output, static_cast<uint32_t>(frames.size()));
append_le32(output, 0);
for (size_t index = 0; index < frames.size(); ++index) {
const Vp8Frame& frame = frames[index];
append_le32(output, static_cast<uint32_t>(frame.size));
append_le64(output, static_cast<uint64_t>(index));
output.append(
reinterpret_cast<const char*>(frame.data),
static_cast<std::string::size_type>(frame.size));
}
return output;
}
py::tuple extract_vp8(py::handle webp) {
BufferView input(webp);
const Vp8Frame frame = parse_webp(input.data(), input.size());
return py::make_tuple(
py::bytes(reinterpret_cast<const char*>(frame.data), frame.size),
frame.width,
frame.height);
}
py::bytes build_ivf(
py::iterable vp8_payloads,
uint16_t width,
uint16_t height,
uint32_t rate,
uint32_t scale) {
std::vector<std::string> owned_payloads;
for (py::handle payload : vp8_payloads) {
BufferView input(payload);
owned_payloads.emplace_back(
reinterpret_cast<const char*>(input.data()), input.size());
}
std::vector<Vp8Frame> frames;
frames.reserve(owned_payloads.size());
for (const std::string& payload : owned_payloads) {
frames.push_back(validate_vp8_payload(
reinterpret_cast<const uint8_t*>(payload.data()), payload.size()));
}
const std::string output = make_ivf(frames, width, height, rate, scale);
return py::bytes(output);
}
py::bytes webp_batch_to_ivf(
py::iterable webps,
uint32_t rate,
uint32_t scale) {
std::vector<std::string> owned_payloads;
uint16_t width = 0;
uint16_t height = 0;
for (py::handle webp : webps) {
BufferView input(webp);
const Vp8Frame frame = parse_webp(input.data(), input.size());
if (owned_payloads.empty()) {
width = frame.width;
height = frame.height;
} else if (frame.width != width || frame.height != height) {
throw py::value_error(
"all WebPs in an IVF stream must have identical dimensions");
}
owned_payloads.emplace_back(
reinterpret_cast<const char*>(frame.data), frame.size);
}
std::vector<Vp8Frame> frames;
frames.reserve(owned_payloads.size());
for (const std::string& payload : owned_payloads) {
frames.push_back(validate_vp8_payload(
reinterpret_cast<const uint8_t*>(payload.data()), payload.size()));
}
const std::string output = make_ivf(frames, width, height, rate, scale);
return py::bytes(output);
}
} // namespace
PYBIND11_MODULE(_vp8_ivf_ext, module) {
module.doc() = "Strict lossy-WebP VP8 parser and in-memory IVF builder";
module.def(
"extract_vp8",
&extract_vp8,
py::arg("webp"),
"Return (VP8 payload, width, height) from a supported lossy WebP.");
module.def(
"build_ivf",
&build_ivf,
py::arg("vp8_payloads"),
py::arg("width"),
py::arg("height"),
py::arg("rate") = 25,
py::arg("scale") = 1,
"Build an IVF byte stream from same-resolution VP8 key frames.");
module.def(
"webp_batch_to_ivf",
&webp_batch_to_ivf,
py::arg("webps"),
py::arg("rate") = 25,
py::arg("scale") = 1,
"Extract same-resolution lossy WebPs and return one IVF byte stream.");
}