summaryrefslogtreecommitdiff
path: root/yuvbench/yuvbench.c
blob: 5ef344c2f06e319291a71d48c0dadbafa8ca054f (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
#include "yuvbench.h"

#define KBENCH_IMPLEMENTATION
#include "kbench.h"

#ifdef YUVBENCH_ACCELERATE
Backend yuvbench_accelerate(void);
#endif
#ifdef YUVBENCH_BAD
Backend yuvbench_bad(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;
} 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 = 20;
    for (int i = 0; i < warmup; ++i) {
        b.convert_fn(&ctx);
    }

    printf("testing...\n");
    int tests = 1000;
    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);
    }

    double ts_min = -1.0f;
    double ts_max = -1.0f;
    double ts_avg = 0.0f;
    for (int i = 0; i < tests; ++i) {
        if (ts_min < 0 || tests_table[i] < ts_min) {
            ts_min = tests_table[i];
        }
        if (ts_max < 0 || tests_table[i] > ts_max) {
            ts_max = tests_table[i];
        }
        ts_avg += (tests_table[i] / (double)tests);
    }
    printf("min result: %fms\n", ts_min * 1000.0f);
    printf("max result: %fms\n", ts_max * 1000.0f);
    printf("avg result: %fms\n", ts_avg * 1000.0f);

#if 0 && (defined(__APPLE__) || defined(__linux__))
    // Display last result
    char cmd[512] = { 0 };
    snprintf(cmd, sizeof(cmd), "ffplay -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);
    }
    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
    run_backend(yuvbench_accelerate());
#endif
#ifdef YUVBENCH_BAD
    run_backend(yuvbench_bad());
#endif

}