-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathconvert.cpp
More file actions
398 lines (335 loc) · 12.3 KB
/
convert.cpp
File metadata and controls
398 lines (335 loc) · 12.3 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* SPDX-License-Identifier: BSD-2-Clause */
/*
* Copyright (C) 2025 Raspberry Pi Ltd
*
* convert.cpp - libpisp simple image converter example
*/
#include <algorithm>
#include <array>
#include <assert.h>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <stdint.h>
#include <string>
#include <linux/media.h>
#include <cxxopts.hpp>
#include "helpers/backend_device.hpp"
#include "helpers/media_device.hpp"
#include "libpisp/backend/backend.hpp"
#include "libpisp/common/logging.hpp"
#include "libpisp/common/utils.hpp"
#include "libpisp/variants/variant.hpp"
using Buffer = libpisp::helpers::Buffer;
void read_plane(uint8_t *mem, std::ifstream &in, unsigned int width, unsigned int height, unsigned int file_stride,
unsigned int buffer_stride)
{
width = std::min(width, file_stride);
for (unsigned int y = 0; y < height; y++)
{
in.read((char *)mem + y * buffer_stride, width);
in.seekg(file_stride - width, std::ios_base::cur);
}
}
void write_plane(std::ofstream &out, uint8_t *mem, unsigned int width, unsigned int height, unsigned int file_stride,
unsigned int buffer_stride)
{
width = std::min(width, file_stride);
for (unsigned int y = 0; y < height; y++)
{
out.write((char *)mem + y * buffer_stride, width);
for (unsigned int i = 0; i < file_stride - width; i++)
out.put(0);
}
}
void read_rgb888(const std::array<uint8_t *, 3> &mem, std::ifstream &in, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride)
{
read_plane((uint8_t *)mem[0], in, width * 3, height, file_stride, buffer_stride);
}
void write_rgb888(std::ofstream &out, std::array<uint8_t *, 3> &mem, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride)
{
write_plane(out, (uint8_t *)mem[0], width * 3, height, file_stride, buffer_stride);
}
void read_32(const std::array<uint8_t *, 3> &mem, std::ifstream &in, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride)
{
read_plane((uint8_t *)mem[0], in, width * 4, height, file_stride, buffer_stride);
}
void write_32(std::ofstream &out, std::array<uint8_t *, 3> &mem, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride)
{
write_plane(out, (uint8_t *)mem[0], width * 4, height, file_stride, buffer_stride);
}
void read_yuv(const std::array<uint8_t *, 3> &mem, std::ifstream &in, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride, unsigned int ss_x, unsigned int ss_y)
{
uint8_t *dst = mem[0];
// Y
read_plane(dst, in, width, height, file_stride, buffer_stride);
// U
dst = mem[1] ? mem[1] : dst + buffer_stride * height;
read_plane(dst, in, width / ss_x, height / ss_y, file_stride / ss_x, buffer_stride / ss_x);
// V
dst = mem[2] ? mem[2] : dst + buffer_stride / ss_x * height / ss_y;
read_plane(dst, in, width / ss_x, height / ss_y, file_stride / ss_x, buffer_stride / ss_x);
}
void write_yuv(std::ofstream &out, std::array<uint8_t *, 3> &mem, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride, unsigned int ss_x, unsigned int ss_y)
{
uint8_t *src = mem[0];
// Y
write_plane(out, src, width, height, file_stride, buffer_stride);
// U
src = mem[1] ? mem[1] : src + buffer_stride * height;
write_plane(out, src, width / ss_x, height / ss_y, file_stride / ss_x, buffer_stride / ss_x);
// V
src = mem[2] ? mem[2] : src + buffer_stride / ss_x * height / ss_y;
write_plane(out, src, width / ss_x, height / ss_y, file_stride / ss_x, buffer_stride / ss_x);
}
void read_yuv420(const std::array<uint8_t *, 3> &mem, std::ifstream &in, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride)
{
read_yuv(mem, in, width, height, file_stride, buffer_stride, 2, 2);
}
void read_yuv422p(const std::array<uint8_t *, 3> &mem, std::ifstream &in, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride)
{
read_yuv(mem, in, width, height, file_stride, buffer_stride, 2, 1);
}
void read_yuv444p(const std::array<uint8_t *, 3> &mem, std::ifstream &in, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride)
{
read_yuv(mem, in, width, height, file_stride, buffer_stride, 1, 1);
}
void read_yuv422i(const std::array<uint8_t *, 3> &mem, std::ifstream &in, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride)
{
read_plane(mem[0], in, width * 2, height, file_stride, buffer_stride);
}
void write_yuv420(std::ofstream &out, std::array<uint8_t *, 3> &mem, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride)
{
write_yuv(out, mem, width, height, file_stride, buffer_stride, 2, 2);
}
void write_yuv422p(std::ofstream &out, std::array<uint8_t *, 3> &mem, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride)
{
write_yuv(out, mem, width, height, file_stride, buffer_stride, 2, 1);
}
void write_yuv444p(std::ofstream &out, std::array<uint8_t *, 3> &mem, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride)
{
write_yuv(out, mem, width, height, file_stride, buffer_stride, 1, 1);
}
void write_yuv422i(std::ofstream &out, std::array<uint8_t *, 3> &mem, unsigned int width, unsigned int height,
unsigned int file_stride, unsigned int buffer_stride)
{
write_plane(out, mem[0], width * 2, height, file_stride, buffer_stride);
}
// clang-format off
struct FormatFuncs
{
std::function<void(const std::array<uint8_t *, 3> &, std::ifstream &, unsigned int, unsigned int, unsigned int,
unsigned int)> read_file;
std::function<void(std::ofstream &, std::array<uint8_t *, 3> &, unsigned int, unsigned int, unsigned int,
unsigned int)> write_file;
};
const std::map<std::string, FormatFuncs> Formats =
{
{ "RGB888", { read_rgb888, write_rgb888 } },
{ "RGBX8888", { read_32, write_32 } },
{ "YUV420P", { read_yuv420, write_yuv420 } },
{ "YUV422P", { read_yuv422p, write_yuv422p } },
{ "YUV444P", { read_yuv444p, write_yuv444p } },
{ "YUYV", { read_yuv422i, write_yuv422i } },
{ "UYVY", { read_yuv422i, write_yuv422i } },
};
// clang-format on
struct Format
{
unsigned int width;
unsigned int height;
unsigned int stride;
std::string format;
};
Format parse_format(const std::string &fmt)
{
Format format;
size_t pos = 0, start = 0;
pos = fmt.find(':', start);
if (pos == std::string::npos)
return {};
format.width = std::stoi(fmt.substr(start, pos - start));
start = pos + 1;
pos = fmt.find(':', start);
if (pos == std::string::npos)
return {};
format.height = std::stoi(fmt.substr(start, pos - start));
start = pos + 1;
pos = fmt.find(':', start);
if (pos == std::string::npos)
return {};
format.stride = std::stoi(fmt.substr(start, pos - start));
start = pos + 1;
format.format = fmt.substr(start);
return format;
}
int main(int argc, char *argv[])
{
libpisp::helpers::MediaDevice devices;
libpisp::logging_init();
cxxopts::Options options(argv[0], "PiSP Image Converter");
// clang-format off
options.add_options()
("input", "Input file", cxxopts::value<std::string>())
("output", "Output file", cxxopts::value<std::string>())
("input-format", "Input format in the form width:height:stride:format\n"
"Bit-depth is assumed to be 8-bit.",cxxopts::value<std::string>()->default_value(""))
("output-format", "Output format in the form width:height:stride:format\n"
"Bit-depth is assumed to be 8-bit.", cxxopts::value<std::string>()->default_value(""))
("f,formats", "List available format strings that can be used")
("l,list", "Enumerate the media device nodes")
("h,help", "Print usage")
;
// clang-format on
options.parse_positional({ "input", "output" });
options.positional_help("<input file> <output file>");
options.set_width(120);
auto args = options.parse(argc, argv);
if (args.count("help"))
{
std::cerr << options.help() << std::endl;
exit(0);
}
else if (args.count("list"))
{
std::cerr << devices.List() << std::endl;
exit(0);
}
else if (args.count("formats"))
{
for (const auto &f : Formats)
std::cerr << f.first << " ";
std::cerr << std::endl;
exit(0);
}
libpisp::helpers::BackendDevice backend_device {};
std::cerr << "Acquired device " << backend_device.MediaDev() << std::endl;
libpisp::BackEnd *be = backend_device.Get();
const libpisp::PiSPVariant &variant = backend_device.Variant();
auto in_file = parse_format(args["input-format"].as<std::string>());
if (!Formats.count(in_file.format))
{
std::cerr << "Invalid input-format specified" << std::endl;
exit(-1);
}
auto out_file = parse_format(args["output-format"].as<std::string>());
if (!Formats.count(out_file.format))
{
std::cerr << "Invalid output-format specified" << std::endl;
exit(-1);
}
pisp_be_global_config global;
be->GetGlobal(global);
global.bayer_enables = 0;
global.rgb_enables = PISP_BE_RGB_ENABLE_INPUT + PISP_BE_RGB_ENABLE_OUTPUT0;
if (in_file.format == "RGBX8888" && !variant.BackendRGB32Supported(0))
{
std::cerr << "Backend hardware does not support RGBX input" << std::endl;
exit(-1);
}
pisp_image_format_config i = {};
i.width = in_file.width;
i.height = in_file.height;
i.format = libpisp::get_pisp_image_format(in_file.format);
assert(i.format);
libpisp::compute_optimal_stride(i);
be->SetInputFormat(i);
pisp_be_output_format_config o = {};
if (out_file.format == "RGBX8888" && !variant.BackendRGB32Supported(0))
{
// Hack to generate RGBX even when BE_MINOR_VERSION < 1 using Resample
if (out_file.width < i.width)
std::cerr << "Backend hardware has limited RGBX support; resize artifacts may be present" << std::endl;
o.image.width = out_file.width * 2 - 1;
o.image.height = out_file.height;
o.image.format = libpisp::get_pisp_image_format("UYVY");
pisp_be_ccm_config csc = {}; // Define a matrix to swap components [0] and [1]
csc.coeffs[1] = 1024;
csc.coeffs[3] = 1024;
csc.coeffs[8] = 1024;
csc.offsets[0] = 131072; // round to nearest after Resample, for 8-bit output
csc.offsets[1] = 131072;
csc.offsets[2] = 131072;
be->SetCsc(0, csc);
global.rgb_enables |= PISP_BE_RGB_ENABLE_CSC0;
}
else
{
o.image.width = out_file.width;
o.image.height = out_file.height;
o.image.format = libpisp::get_pisp_image_format(out_file.format);
}
assert(o.image.format);
libpisp::compute_optimal_stride(o.image, true);
be->SetOutputFormat(0, o);
if (!out_file.stride)
out_file.stride = o.image.stride;
if (in_file.format >= "U")
{
pisp_be_ccm_config csc;
be->InitialiseYcbcrInverse(csc, "jpeg");
be->SetCcm(csc);
global.rgb_enables |= PISP_BE_RGB_ENABLE_CCM;
}
if (out_file.format >= "U")
{
pisp_be_ccm_config csc;
be->InitialiseYcbcr(csc, "jpeg");
be->SetCsc(0, csc);
global.rgb_enables |= PISP_BE_RGB_ENABLE_CSC0;
}
be->SetGlobal(global);
be->SetCrop(0, { 0, 0, i.width, i.height });
be->SetSmartResize(0, { o.image.width, o.image.height });
backend_device.Prepare();
auto buffers = backend_device.GetBufferSlice();
std::string input_filename = args["input"].as<std::string>();
std::ifstream in(input_filename, std::ios::binary);
if (!in.is_open())
{
std::cerr << "Unable to open " << input_filename << std::endl;
exit(-1);
}
std::cerr << "Reading " << input_filename << " " << in_file.width << ":" << in_file.height << ":" << in_file.stride
<< ":" << in_file.format << std::endl;
{
Buffer::Sync input(buffers.at("pispbe-input"), Buffer::Sync::Access::ReadWrite);
Formats.at(in_file.format).read_file(input.Get(), in, in_file.width, in_file.height, in_file.stride, i.stride);
in.close();
}
int ret = backend_device.Run(buffers);
if (ret)
{
std::cerr << "Job run error!" << std::endl;
exit(-1);
}
std::string output_file = args["output"].as<std::string>();
std::ofstream out(output_file, std::ios::binary);
if (!out.is_open())
{
std::cerr << "Unable to open " << output_file << std::endl;
exit(-1);
}
Buffer::Sync output(buffers.at("pispbe-output0"), Buffer::Sync::Access::Read);
Formats.at(out_file.format)
.write_file(out, const_cast<std::array<uint8_t *, 3> &>(output.Get()), out_file.width, out_file.height,
out_file.stride, o.image.stride);
out.close();
std::cerr << "Writing " << output_file << " " << out_file.width << ":" << out_file.height << ":" << out_file.stride
<< ":" << out_file.format << std::endl;
return 0;
}