-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdecode.cpp
More file actions
287 lines (235 loc) · 7.41 KB
/
decode.cpp
File metadata and controls
287 lines (235 loc) · 7.41 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
// Copyright (c) 2025 Hans-Kristian Arntzen
// SPDX-License-Identifier: MIT
#include <string.h>
#include "global_managers_init.hpp"
#include "device.hpp"
#include "context.hpp"
#include "pyrowave_decoder.hpp"
#include "pyrowave_common.hpp"
#include "yuv4mpeg.hpp"
#include "shaders/slangmosh.hpp"
using namespace Granite;
using namespace Vulkan;
struct DecodedBuffer
{
BufferHandle planes[3];
Fence fence;
};
static DecodedBuffer run_decoder_frame(CommandBufferHandle &cmd,
PyroWave::Decoder &dec,
const PyroWave::ViewBuffers &outputs,
uint32_t frame_index)
{
auto &device = cmd->get_device();
DecodedBuffer decoded;
for (int i = 0; i < 3; i++)
{
BufferCreateInfo bufinfo = {};
bufinfo.domain = BufferDomain::CachedHost;
bufinfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
bufinfo.size = format_get_layer_size(outputs.planes[i]->get_format(),
VK_IMAGE_ASPECT_COLOR_BIT,
outputs.planes[i]->get_view_width(),
outputs.planes[i]->get_view_height(), 1);
decoded.planes[i] = device.create_buffer(bufinfo);
}
dec.decode(*cmd, outputs);
for (auto &plane : outputs.planes)
{
cmd->image_barrier(plane->get_image(),
VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT,
VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_READ_BIT);
}
for (int i = 0; i < 3; i++)
{
cmd->copy_image_to_buffer(*decoded.planes[i], outputs.planes[i]->get_image(), 0,
{}, { outputs.planes[i]->get_view_width(),
outputs.planes[i]->get_view_height(),
outputs.planes[i]->get_view_depth() },
0, 0, { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 });
}
cmd->barrier(VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_HOST_READ_BIT);
device.submit(cmd, &decoded.fence);
device.next_frame_context();
LOGI("Submitted frame %06u ...\n", frame_index);
return decoded;
}
struct YCbCrImages
{
Vulkan::ImageHandle images[3];
PyroWave::ViewBuffers views;
};
static YCbCrImages create_ycbcr_images(Device &device, int width, int height, VkFormat fmt, PyroWave::ChromaSubsampling chroma)
{
YCbCrImages images;
auto info = ImageCreateInfo::immutable_2d_image(width, height, fmt);
info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
info.initial_layout = VK_IMAGE_LAYOUT_UNDEFINED;
images.images[0] = device.create_image(info);
device.set_name(*images.images[0], "Y");
if (chroma == PyroWave::ChromaSubsampling::Chroma420)
{
info.width >>= 1;
info.height >>= 1;
}
images.images[1] = device.create_image(info);
device.set_name(*images.images[1], "Cb");
images.images[2] = device.create_image(info);
device.set_name(*images.images[2], "Cr");
for (int i = 0; i < 3; i++)
images.views.planes[i] = &images.images[i]->get_view();
return images;
}
static bool write_payload(YUV4MPEGFile &file, Device &device, const DecodedBuffer &decoded)
{
if (!file.begin_frame())
return false;
for (auto &plane_ptr : decoded.planes)
{
auto *plane = device.map_host_buffer(*plane_ptr, MEMORY_ACCESS_READ_BIT);
if (!file.write(plane, plane_ptr->get_create_info().size))
return false;
}
return true;
}
static bool read_payload(FILE *file, PyroWave::Decoder &decoder)
{
std::vector<uint8_t> packetized_data;
uint32_t u32_size;
for (;;)
{
if (fread(&u32_size, sizeof(u32_size), 1, file) != 1)
return false;
packetized_data.resize(u32_size);
if (fread(packetized_data.data(), 1, u32_size, file) != u32_size)
return false;
if (!decoder.push_packet(packetized_data.data(), packetized_data.size()))
return false;
if (decoder.decode_is_ready(false))
return true;
}
}
static const char *format_to_str(YUV4MPEGFile::Format fmt)
{
switch (fmt)
{
case YUV4MPEGFile::Format::YUV420P:
return "C420";
case YUV4MPEGFile::Format::YUV420P16:
return "C420p16";
case YUV4MPEGFile::Format::YUV444P:
return "C444";
case YUV4MPEGFile::Format::YUV444P16:
return "C444p16";
default:
return "???";
}
}
static void run_decoder(Device &device, const char *out_path, const char *in_path)
{
struct FILEDeleter { void operator()(FILE *file) { if (file) fclose(file); } };
std::unique_ptr<FILE, FILEDeleter> infile;
infile.reset(fopen(in_path, "rb"));
if (!infile)
{
LOGE("Failed to open input file.\n");
return;
}
char magic[9] = {};
if (fread(magic, 1, 8, infile.get()) != 8)
{
LOGE("Failed to read magic.\n");
return;
}
if (strcmp(magic, "PYROWAVE") != 0)
{
LOGE("Invalid magic.\n");
return;
}
int32_t u32_params[8];
if (fread(u32_params, sizeof(u32_params), 1, infile.get()) != 1)
{
LOGE("Failed to read parameters.\n");
return;
}
PyroWave::Decoder dec;
int width = u32_params[0];
int height = u32_params[1];
auto format = YUV4MPEGFile::Format(u32_params[2]);
auto chroma = PyroWave::ChromaSubsampling(u32_params[3]);
bool is_full = u32_params[4] != 0;
int frame_rate_num = u32_params[5];
int frame_rate_den = u32_params[6];
// Unused chroma siting. YUV4MPEG doesn't seem to have proper support for that.
if (!dec.init(&device, width, height, chroma))
return;
YUV4MPEGFile output;
char params[1024];
snprintf(params, sizeof(params), "YUV4MPEG2 W%d H%d F%d:%d Ip A1:1 XCOLORRANGE=%s %s\n",
width, height, frame_rate_num, frame_rate_den, is_full ? "FULL" : "LIMITED", format_to_str(format));
if (!output.open_write(out_path, params))
{
LOGE("Failed to open input file.\n");
return;
}
auto fmt = YUV4MPEGFile::format_to_bytes_per_component(output.get_format()) == 2 ? VK_FORMAT_R16_UNORM : VK_FORMAT_R8_UNORM;
auto outputs = create_ycbcr_images(device, width, height, fmt, chroma);
DecodedBuffer queue[2];
uint32_t frame_index = 0;
for (;;)
{
auto &q = queue[frame_index & 1];
if (q.fence)
{
q.fence->wait();
q.fence.reset();
if (!write_payload(output, device, q))
{
LOGE("Failed to write payload.\n");
break;
}
}
if (!read_payload(infile.get(), dec))
break;
auto cmd = device.request_command_buffer();
for (auto &img : outputs.images)
{
cmd->image_barrier(*img, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_GENERAL,
VK_PIPELINE_STAGE_2_COPY_BIT, 0,
VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT);
}
queue[frame_index & 1] = run_decoder_frame(cmd, dec, outputs.views, frame_index);
frame_index++;
}
frame_index--;
auto &q = queue[frame_index & 1];
if (q.fence)
{
q.fence->wait();
if (!write_payload(output, device, q))
LOGE("Failed to write payload.\n");
}
}
static void run_decoder(const char *out_path, const char *in_path)
{
if (!Context::init_loader(nullptr))
return;
Context ctx;
if (!ctx.init_instance_and_device(nullptr, 0, nullptr, 0, CONTEXT_CREATION_ENABLE_PUSH_DESCRIPTOR_BIT))
return;
Device dev;
dev.set_context(ctx);
run_decoder(dev, out_path, in_path);
}
int main(int argc, char **argv)
{
if (argc != 3)
{
LOGE("Usage: pyrowave-encode <input.pyrowave> <output.y4m>\n");
return EXIT_FAILURE;
}
run_decoder(argv[2], argv[1]);
}