aboutsummaryrefslogtreecommitdiffstats
path: root/tests/checkasm/sw_yuv2rgb.c
blob: 02ed9a74d59dda73ab2d333b19050b1f37867db4 (plain) (blame)
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
/*
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <string.h>

#include "libavutil/common.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mem_internal.h"
#include "libavutil/pixdesc.h"

#include "libswscale/swscale.h"
#include "libswscale/swscale_internal.h"

#include "checkasm.h"

#define randomize_buffers(buf, size)      \
    do {                                  \
        for (int j = 0; j < size; j += 4) \
            AV_WN32(buf + j, rnd());      \
    } while (0)

static const int dst_fmts[] = {
//     AV_PIX_FMT_BGR48BE,
//     AV_PIX_FMT_BGR48LE,
//     AV_PIX_FMT_RGB48BE,
//     AV_PIX_FMT_RGB48LE,
    AV_PIX_FMT_ARGB,
    AV_PIX_FMT_ABGR,
    AV_PIX_FMT_RGBA,
    AV_PIX_FMT_BGRA,
    AV_PIX_FMT_RGB24,
    AV_PIX_FMT_BGR24,
    AV_PIX_FMT_RGB565,
    AV_PIX_FMT_BGR565,
    AV_PIX_FMT_RGB555,
    AV_PIX_FMT_BGR555,
//     AV_PIX_FMT_RGB444,
//     AV_PIX_FMT_BGR444,
//     AV_PIX_FMT_RGB8,
//     AV_PIX_FMT_BGR8,
//     AV_PIX_FMT_RGB4,
//     AV_PIX_FMT_BGR4,
//     AV_PIX_FMT_RGB4_BYTE,
//     AV_PIX_FMT_BGR4_BYTE,
//     AV_PIX_FMT_MONOBLACK,
};

static int cmp_off_by_n(const uint8_t *ref, const uint8_t *test, size_t n, int accuracy)
{
    for (size_t i = 0; i < n; i++) {
        if (abs(ref[i] - test[i]) > accuracy)
            return 1;
    }
    return 0;
}

static int cmp_555_by_n(const uint8_t *ref, const uint8_t *test, size_t n, int accuracy)
{
    const uint16_t *ref16  = (const uint16_t *) ref;
    const uint16_t *test16 = (const uint16_t *) test;
    for (size_t i = 0; i < n; i++) {
        if (abs(( ref16[i]        & 0x1f) - ( test16[i]        & 0x1f)) > accuracy)
            return 1;
        if (abs(((ref16[i] >>  5) & 0x1f) - ((test16[i] >>  5) & 0x1f)) > accuracy)
            return 1;
        if (abs(((ref16[i] >> 10) & 0x1f) - ((test16[i] >> 10) & 0x1f)) > accuracy)
            return 1;
    }
    return 0;
}

static int cmp_565_by_n(const uint8_t *ref, const uint8_t *test, size_t n, int accuracy)
{
    const uint16_t *ref16  = (const uint16_t *) ref;
    const uint16_t *test16 = (const uint16_t *) test;
    for (size_t i = 0; i < n; i++) {
        if (abs(( ref16[i]        & 0x1f) - ( test16[i]        & 0x1f)) > accuracy)
            return 1;
        if (abs(((ref16[i] >>  5) & 0x3f) - ((test16[i] >>  5) & 0x3f)) > accuracy)
            return 1;
        if (abs(((ref16[i] >> 11) & 0x1f) - ((test16[i] >> 11) & 0x1f)) > accuracy)
            return 1;
    }
    return 0;
}

static void check_yuv2rgb(int src_pix_fmt)
{
    const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
#define MAX_LINE_SIZE 1920
    static const int input_sizes[] = {8, 128, 1080, MAX_LINE_SIZE};

    declare_func_emms(AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT,
                      int, SwsContext *c, const uint8_t *src[],
                           int srcStride[], int srcSliceY, int srcSliceH,
                           uint8_t *dst[], int dstStride[]);

    LOCAL_ALIGNED_8(uint8_t, src_y, [MAX_LINE_SIZE * 2]);
    LOCAL_ALIGNED_8(uint8_t, src_u, [MAX_LINE_SIZE]);
    LOCAL_ALIGNED_8(uint8_t, src_v, [MAX_LINE_SIZE]);
    LOCAL_ALIGNED_8(uint8_t, src_a, [MAX_LINE_SIZE * 2]);
    const uint8_t *src[4] = { src_y, src_u, src_v, src_a };

    LOCAL_ALIGNED_8(uint8_t, dst0_, [2 * MAX_LINE_SIZE * 6]);
    uint8_t *dst0[4] = { dst0_ };
    uint8_t *lines0[2] = { dst0_, dst0_ + MAX_LINE_SIZE * 6 };

    LOCAL_ALIGNED_8(uint8_t, dst1_, [2 * MAX_LINE_SIZE * 6]);
    uint8_t *dst1[4] = { dst1_ };
    uint8_t *lines1[2] = { dst1_, dst1_ + MAX_LINE_SIZE * 6 };

    randomize_buffers(src_y, MAX_LINE_SIZE * 2);
    randomize_buffers(src_u, MAX_LINE_SIZE);
    randomize_buffers(src_v, MAX_LINE_SIZE);
    randomize_buffers(src_a, MAX_LINE_SIZE * 2);

    for (int dfi = 0; dfi < FF_ARRAY_ELEMS(dst_fmts); dfi++) {
        int dst_pix_fmt = dst_fmts[dfi];
        const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
        int sample_size = av_get_padded_bits_per_pixel(dst_desc) >> 3;
        for (int isi = 0; isi < FF_ARRAY_ELEMS(input_sizes); isi++) {
            struct SwsContext *ctx;
            int log_level;
            int width = input_sizes[isi];
            int srcSliceY = 0;
            int srcSliceH = 2;
            int srcStride[4] = {
                width,
                width >> src_desc->log2_chroma_w,
                width >> src_desc->log2_chroma_w,
                width,
            };
            int dstStride[4] = { MAX_LINE_SIZE * 6 };

            // override log level to prevent spamming of the message
            // "No accelerated colorspace conversion found from %s to %s"
            log_level = av_log_get_level();
            av_log_set_level(AV_LOG_ERROR);
            ctx = sws_getContext(width, srcSliceH, src_pix_fmt,
                                 width, srcSliceH, dst_pix_fmt,
                                 0, NULL, NULL, NULL);
            av_log_set_level(log_level);
            if (!ctx)
                fail();

            if (check_func(ctx->convert_unscaled, "%s_%s_%d", src_desc->name, dst_desc->name, width)) {
                memset(dst0_, 0xFF, 2 * MAX_LINE_SIZE * 6);
                memset(dst1_, 0xFF, 2 * MAX_LINE_SIZE * 6);

                call_ref(ctx, src, srcStride, srcSliceY,
                         srcSliceH, dst0, dstStride);
                call_new(ctx, src, srcStride, srcSliceY,
                         srcSliceH, dst1, dstStride);

                if (dst_pix_fmt == AV_PIX_FMT_ARGB  ||
                    dst_pix_fmt == AV_PIX_FMT_ABGR  ||
                    dst_pix_fmt == AV_PIX_FMT_RGBA  ||
                    dst_pix_fmt == AV_PIX_FMT_BGRA  ||
                    dst_pix_fmt == AV_PIX_FMT_RGB24 ||
                    dst_pix_fmt == AV_PIX_FMT_BGR24) {
                    if (cmp_off_by_n(lines0[0], lines1[0], width * sample_size, 3) ||
                        cmp_off_by_n(lines0[1], lines1[1], width * sample_size, 3))
                        fail();
                } else if (dst_pix_fmt == AV_PIX_FMT_RGB565 ||
                           dst_pix_fmt == AV_PIX_FMT_BGR565) {
                    if (cmp_565_by_n(lines0[0], lines1[0], width, 2) ||
                        cmp_565_by_n(lines0[1], lines1[1], width, 2))
                        fail();
                } else if (dst_pix_fmt == AV_PIX_FMT_RGB555 ||
                           dst_pix_fmt == AV_PIX_FMT_BGR555) {
                    if (cmp_555_by_n(lines0[0], lines1[0], width, 2) ||
                        cmp_555_by_n(lines0[1], lines1[1], width, 2))
                        fail();
                } else {
                    fail();
                }

                bench_new(ctx, src, srcStride, srcSliceY,
                          srcSliceH, dst0, dstStride);
            }
            sws_freeContext(ctx);
        }
    }
}

#undef MAX_LINE_SIZE

void checkasm_check_sw_yuv2rgb(void)
{
    check_yuv2rgb(AV_PIX_FMT_YUV420P);
    report("yuv420p");
    check_yuv2rgb(AV_PIX_FMT_YUV422P);
    report("yuv422p");
    check_yuv2rgb(AV_PIX_FMT_YUVA420P);
    report("yuva420p");
}