-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastcvSimpleTest.cpp
More file actions
290 lines (242 loc) · 9.45 KB
/
fastcvSimpleTest.cpp
File metadata and controls
290 lines (242 loc) · 9.45 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <stdint.h>
#include <jpeglib.h>
#include "fastcv.h"
static uint64_t getTimeUs()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000ULL + tv.tv_usec;
}
static uint8_t* loadJpeg(const char* filename, uint32_t* width, uint32_t* height)
{
FILE* fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Error: cannot open %s\n", filename);
return NULL;
}
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, fp);
jpeg_read_header(&cinfo, TRUE);
cinfo.out_color_space = JCS_RGB;
jpeg_start_decompress(&cinfo);
*width = cinfo.output_width;
*height = cinfo.output_height;
int row_stride = cinfo.output_width * cinfo.output_components;
uint8_t* data = (uint8_t*)malloc(row_stride * cinfo.output_height);
if (!data) {
fprintf(stderr, "Error: malloc failed for JPEG data\n");
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(fp);
return NULL;
}
while (cinfo.output_scanline < cinfo.output_height) {
uint8_t* row = data + cinfo.output_scanline * row_stride;
jpeg_read_scanlines(&cinfo, &row, 1);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(fp);
return data;
}
static int saveJpeg(const char* filename, const uint8_t* data,
uint32_t width, uint32_t height, int quality)
{
FILE* fp = fopen(filename, "wb");
if (!fp) {
fprintf(stderr, "Error: cannot create %s\n", filename);
return -1;
}
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, fp);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
int row_stride = width * 3;
while (cinfo.next_scanline < cinfo.image_height) {
const uint8_t* row = data + cinfo.next_scanline * row_stride;
jpeg_write_scanlines(&cinfo, (JSAMPARRAY)&row, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
fclose(fp);
return 0;
}
static void splitChannels(const uint8_t* rgb, uint8_t* r, uint8_t* g, uint8_t* b,
uint32_t width, uint32_t height)
{
uint32_t n = width * height;
for (uint32_t i = 0; i < n; i++) {
r[i] = rgb[i * 3 + 0];
g[i] = rgb[i * 3 + 1];
b[i] = rgb[i * 3 + 2];
}
}
static void mergeChannels(uint8_t* rgb, const uint8_t* r, const uint8_t* g, const uint8_t* b,
uint32_t width, uint32_t height)
{
uint32_t n = width * height;
for (uint32_t i = 0; i < n; i++) {
rgb[i * 3 + 0] = r[i];
rgb[i * 3 + 1] = g[i];
rgb[i * 3 + 2] = b[i];
}
}
static void runScaleDown(const char* modeName, fcvOperationMode mode,
const uint8_t* rgbSrc, uint32_t srcW, uint32_t srcH,
uint8_t* rgbDst, uint32_t dstW, uint32_t dstH,
uint32_t loop)
{
printf("\n===== OperationMode: %s (loop=%u) =====\n", modeName, loop);
uint64_t t0, t1, fcvTotal = 0;
t0 = getTimeUs();
fcvSetOperationMode(mode);
fcvMemInit();
t1 = getTimeUs();
fcvTotal += t1 - t0;
printf(" fcvSetOperationMode + fcvMemInit: %llu us\n", (unsigned long long)(t1 - t0));
uint32_t srcPixels = srcW * srcH;
uint32_t dstPixels = dstW * dstH;
t0 = getTimeUs();
uint8_t* srcR = (uint8_t*)fcvMemAlloc(srcPixels, 16);
uint8_t* srcG = (uint8_t*)fcvMemAlloc(srcPixels, 16);
uint8_t* srcB = (uint8_t*)fcvMemAlloc(srcPixels, 16);
uint8_t* dstR = (uint8_t*)fcvMemAlloc(dstPixels, 16);
uint8_t* dstG = (uint8_t*)fcvMemAlloc(dstPixels, 16);
uint8_t* dstB = (uint8_t*)fcvMemAlloc(dstPixels, 16);
t1 = getTimeUs();
fcvTotal += t1 - t0;
printf(" fcvMemAlloc (6 buffers): %llu us\n", (unsigned long long)(t1 - t0));
if (!srcR || !srcG || !srcB || !dstR || !dstG || !dstB) {
fprintf(stderr, "Error: fcvMemAlloc failed\n");
goto cleanup;
}
{
uint64_t splitTotal = 0, scaleRTotal = 0, scaleGTotal = 0, scaleBTotal = 0, mergeTotal = 0;
uint64_t loopStart = getTimeUs();
for (uint32_t i = 0; i < loop; i++) {
t0 = getTimeUs();
splitChannels(rgbSrc, srcR, srcG, srcB, srcW, srcH);
t1 = getTimeUs();
splitTotal += t1 - t0;
t0 = getTimeUs();
fcvScaleDownMNu8(srcR, srcW, srcH, srcW, dstR, dstW, dstH, dstW);
t1 = getTimeUs();
scaleRTotal += t1 - t0;
t0 = getTimeUs();
fcvScaleDownMNu8(srcG, srcW, srcH, srcW, dstG, dstW, dstH, dstW);
t1 = getTimeUs();
scaleGTotal += t1 - t0;
t0 = getTimeUs();
fcvScaleDownMNu8(srcB, srcW, srcH, srcW, dstB, dstW, dstH, dstW);
t1 = getTimeUs();
scaleBTotal += t1 - t0;
t0 = getTimeUs();
mergeChannels(rgbDst, dstR, dstG, dstB, dstW, dstH);
t1 = getTimeUs();
mergeTotal += t1 - t0;
}
uint64_t loopEnd = getTimeUs();
uint64_t loopElapsed = loopEnd - loopStart;
uint64_t fcvScaleTotal = scaleRTotal + scaleGTotal + scaleBTotal;
printf(" --- Per-frame average (%u frames) ---\n", loop);
printf(" splitChannels: %llu us\n", (unsigned long long)(splitTotal / loop));
printf(" fcvScaleDownMNu8 (R): %llu us\n", (unsigned long long)(scaleRTotal / loop));
printf(" fcvScaleDownMNu8 (G): %llu us\n", (unsigned long long)(scaleGTotal / loop));
printf(" fcvScaleDownMNu8 (B): %llu us\n", (unsigned long long)(scaleBTotal / loop));
printf(" mergeChannels: %llu us\n", (unsigned long long)(mergeTotal / loop));
printf(" --- Totals ---\n");
printf(" Loop wall time: %llu us\n", (unsigned long long)loopElapsed);
printf(" Avg frame time: %llu us\n", (unsigned long long)(loopElapsed / loop));
printf(" fcvScaleDownMNu8 total (RGB): %llu us (avg %llu us/frame)\n",
(unsigned long long)fcvScaleTotal, (unsigned long long)(fcvScaleTotal / loop));
double wallFps = (loopElapsed > 0) ? (loop * 1000000.0 / loopElapsed) : 0;
double fcvFps = (fcvScaleTotal > 0) ? (loop * 1000000.0 / fcvScaleTotal) : 0;
printf(" FPS (wall, full pipeline): %.2f\n", wallFps);
printf(" FPS (fcvScaleDownMNu8 only): %.2f\n", fcvFps);
fcvTotal += fcvScaleTotal;
}
cleanup:
t0 = getTimeUs();
if (srcR) fcvMemFree(srcR);
if (srcG) fcvMemFree(srcG);
if (srcB) fcvMemFree(srcB);
if (dstR) fcvMemFree(dstR);
if (dstG) fcvMemFree(dstG);
if (dstB) fcvMemFree(dstB);
t1 = getTimeUs();
fcvTotal += t1 - t0;
printf(" fcvMemFree (6 buffers): %llu us\n", (unsigned long long)(t1 - t0));
t0 = getTimeUs();
fcvMemDeInit();
fcvCleanUp();
t1 = getTimeUs();
fcvTotal += t1 - t0;
printf(" fcvMemDeInit + fcvCleanUp: %llu us\n", (unsigned long long)(t1 - t0));
printf(" ---- FastCV total: %llu us\n", (unsigned long long)fcvTotal);
}
int main(int argc, char* argv[])
{
uint32_t loop = 100;
if (argc > 1) {
int val = atoi(argv[1]);
if (val > 0) loop = (uint32_t)val;
}
const char* inputFile = "3840x2160.jpg";
const char* outputCpuPerf = "output_cpu_performance.jpg";
const char* outputPerf = "output_performance.jpg";
const uint32_t dstW = 1920;
const uint32_t dstH = 1080;
const int jpegQuality = 90;
uint32_t srcW = 0, srcH = 0;
uint64_t t0, t1, totalStart, totalEnd;
totalStart = getTimeUs();
printf("Reading %s ...\n", inputFile);
t0 = getTimeUs();
uint8_t* rgbSrc = loadJpeg(inputFile, &srcW, &srcH);
t1 = getTimeUs();
printf(" loadJpeg: %llu us (%ux%u)\n", (unsigned long long)(t1 - t0), srcW, srcH);
if (!rgbSrc) {
fprintf(stderr, "Error: failed to load %s\n", inputFile);
return 1;
}
if (srcW < dstW || srcH < dstH) {
fprintf(stderr, "Error: source (%ux%u) smaller than target (%ux%u)\n",
srcW, srcH, dstW, dstH);
free(rgbSrc);
return 1;
}
uint8_t* rgbDst = (uint8_t*)malloc(dstW * dstH * 3);
if (!rgbDst) {
fprintf(stderr, "Error: malloc failed for output buffer\n");
free(rgbSrc);
return 1;
}
// --- FASTCV_OP_CPU_PERFORMANCE ---
runScaleDown("FASTCV_OP_CPU_PERFORMANCE (CPU)", FASTCV_OP_CPU_PERFORMANCE,
rgbSrc, srcW, srcH, rgbDst, dstW, dstH, loop);
saveJpeg(outputCpuPerf, rgbDst, dstW, dstH, jpegQuality);
// --- FASTCV_OP_PERFORMANCE ---
runScaleDown("FASTCV_OP_PERFORMANCE (CDSP)", FASTCV_OP_PERFORMANCE,
rgbSrc, srcW, srcH, rgbDst, dstW, dstH, loop);
saveJpeg(outputPerf, rgbDst, dstW, dstH, jpegQuality);
free(rgbSrc);
free(rgbDst);
totalEnd = getTimeUs();
printf("\nTotal time: %llu us\n", (unsigned long long)(totalEnd - totalStart));
return 0;
}