summaryrefslogtreecommitdiff
path: root/yuvbench/yuvbench.c
blob: 669c339f1456511c0f49a750711251ff5a32b469 (plain)
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
#include "yuvbench.h"

#define KBENCH_IMPLEMENTATION
#include "kbench.h"

#include <math.h>

static int cmp_double(const void* a, const void* b)
{
    double da = *(const double*)a, db = *(const double*)b;
    return (da > db) - (da < db);
}

#ifdef YUVBENCH_ACCELERATE
Backend yuvbench_accelerate(void);
#endif
#ifdef YUVBENCH_BAD
Backend yuvbench_bad(void);
#endif
#ifdef YUVBENCH_LIBYUV
Backend yuvbench_libyuv(void);
#endif
#ifdef YUVBENCH_SWSCALE
Backend yuvbench_swscale(void);
#endif
#ifdef YUVBENCH_CLAUDE
Backend yuvbench_claude(void);
#endif

static struct
{
    uint32_t inp_w;
    uint32_t inp_h;
    void* inp_buf;
    size_t inp_len;
    void* out_buf;
    size_t out_len;
    bool show;
} G = { 0 };

static void run_backend(Backend b)
{
    Ctx ctx = { 0 };
    ctx.inp_w = G.inp_w;
    ctx.inp_h = G.inp_h;
    ctx.inp_buf = G.inp_buf;
    ctx.inp_len = G.inp_len;
    ctx.out_buf = G.out_buf;
    ctx.out_len = G.out_len;

    if (b.init_fn && !b.init_fn(&ctx)) {
        printf("Init failed\n");
        return;
    }

    if (!b.convert_fn) {
        printf("No convert function");
        return;
    }

    printf("    running warnups...\n");
    int warmup = 100;
    for (int i = 0; i < warmup; ++i) {
        b.convert_fn(&ctx);
    }

    printf("    testing...\n");
    int tests = 2500;
    double* tests_table = calloc(tests, sizeof(double)); assert(tests_table);
    for (int i = 0; i < tests; ++i) {
        uintptr_t t0 = KBenchTS();
        if (!b.convert_fn(&ctx)) {
            printf("Conversion failed\n");
            return;
        }
        uintptr_t t1 = KBenchTS();
        tests_table[i] = KBenchElapsedTime(t0, t1);
    }

    if (b.deinit_fn) {
        b.deinit_fn(&ctx);
    }

    // Sort for percentiles
    qsort(tests_table, tests, sizeof(double), cmp_double);

    double ts_min = tests_table[0];
    double ts_max = tests_table[tests - 1];
    double ts_p50 = tests_table[tests / 2];
    double ts_p95 = tests_table[(int)(tests * 0.95)];
    double ts_p99 = tests_table[(int)(tests * 0.99)];
    double ts_avg = 0.0;
    for (int i = 0; i < tests; ++i) ts_avg += tests_table[i] / (double)tests;
    double ts_var = 0.0;
    for (int i = 0; i < tests; ++i) {
        double d = tests_table[i] - ts_avg;
        ts_var += d * d / (double)tests;
    }
    double ts_stddev = sqrt(ts_var);

    #define MS(t) ((t) * 1000.0)
    printf("    min %8.3fms  p50 %8.3fms  p95 %8.3fms  p99 %8.3fms  max %8.3fms  avg %8.3fms  σ %7.3fms\n",
           MS(ts_min), MS(ts_p50), MS(ts_p95), MS(ts_p99), MS(ts_max), MS(ts_avg), MS(ts_stddev));
    #undef MS


    if (G.show) {
        // Display last result
#if 1 && (defined(__APPLE__) || defined(__linux__))
        char cmd[512] = { 0 };
        snprintf(cmd, sizeof(cmd), "ffplay -hide_banner -f rawvideo -pixel_format rgb24 -video_size %dx%d -", G.inp_w, G.inp_h);
        FILE* pipe_fp = popen(cmd, "w");
        if (!pipe_fp) {
            printf("Failed to open ffplay: %s\n", strerror(errno));
            abort();
        }
        fwrite(G.out_buf, 1, G.out_len, pipe_fp);
        fflush(pipe_fp);
        if (pclose(pipe_fp) == -1) {
            printf("Failed to close ffplay: %s\n", strerror(errno));
            abort();
        }
#endif
    }
}

int main(int argc, char** argv)
{
    // The source file should be raw YUV 4:2:0 pixel data packed with no padding.
    // File name should be [whatever]-<width>x<height>.yuv
    if (argc < 2) {
        printf("No file supplied\n");
        return 1;
    }

    // Decode with and height from file name
    {
        // Scan for last "-"
        const char* suffix = 0;
        for (const char* s = argv[1]; *s; ++s) {
            if (*s == '-') {
                suffix = s + 1;
            }
        }
        if (!suffix) {
            printf("Invalid file name. See comments in yuvbench.c\n");
            return 1;
        }
        // Decode width and height
        for (; *suffix; ++suffix) {
            const char c = *suffix;
            if (c >= '0' && c <= '9') {
                G.inp_w *= 10;
                G.inp_w += (int)(c - '0');
            } else if (c == 'x') {
                ++suffix;
                break;
            } else {
                printf("Invalid file name. See comments in yuvbench.c\n");
                return 1;
            }
        }
        for (; *suffix; ++suffix) {
            const char c = *suffix;
            if (c >= '0' && c <= '9') {
                G.inp_h *= 10;
                G.inp_h += (int)(c - '0');
            } else if (c == '.') {
                break;
            } else {
                printf("Invalid file name. See comments in yuvbench.c\n");
                return 1;
            }
        }
        // Read from disk
        FILE* f = fopen(argv[1], "rb");
        if (!f) {
            printf("Failed to open file: %s\n", strerror(errno));
            return 1;
        }
        fseek(f, 0, SEEK_END);
        G.inp_len = ftell(f);
        fseek(f, 0, SEEK_SET);
        assert(G.inp_len && "Zero-length file");
        G.inp_buf = calloc(1, G.inp_len);
        fread(G.inp_buf, 1, G.inp_len, f);
        fclose(f);
    }

    // Parse other CLI
    for (int i = 2; i < argc; ++i) {
        const char* s = argv[i];
        if (!strcmp(s, "show")) {
            G.show = true;
        }
    }
    printf("Input image is %dx%d YUV 4:2:0\n", G.inp_w, G.inp_h);
    printf("Input buffer size: %lu\n", G.inp_len);
    // 1x full res luminance plane + 2x half res chroma planes
    const size_t expected_len = (G.inp_w * G.inp_h) + ((G.inp_w / 2 * G.inp_h / 2) * 2);
    if (G.inp_len != expected_len) {
        printf("Expected file length %lu but got %lu. Allowing.\n", expected_len, G.inp_len);
    }

    G.out_len = G.inp_w * G.inp_h * 3; // RGB888
    G.out_buf = calloc(1, G.out_len);

#ifdef YUVBENCH_ACCELERATE
    printf("YUVBENCH_ACCELERATE\n");
    run_backend(yuvbench_accelerate());
#endif
#ifdef YUVBENCH_BAD
    printf("YUVBENCH_BAD\n");
    run_backend(yuvbench_bad());
#endif
#ifdef YUVBENCH_LIBYUV
    printf("YUVBENCH_LIBYUV\n");
    run_backend(yuvbench_libyuv());
#endif
#ifdef YUVBENCH_SWSCALE
    printf("YUVBENCH_SWSCALE\n");
    run_backend(yuvbench_swscale());
#endif
#ifdef YUVBENCH_CLAUDE
    printf("YUVBENCH_CLAUDE\n");
    run_backend(yuvbench_claude());
#endif
}