xref: /third_party/ffmpeg/tests/checkasm/sw_rgb.c (revision cabdff1a)
1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci *
3cabdff1aSopenharmony_ci * This file is part of FFmpeg.
4cabdff1aSopenharmony_ci *
5cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or modify
6cabdff1aSopenharmony_ci * it under the terms of the GNU General Public License as published by
7cabdff1aSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or
8cabdff1aSopenharmony_ci * (at your option) any later version.
9cabdff1aSopenharmony_ci *
10cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
11cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
12cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13cabdff1aSopenharmony_ci * GNU General Public License for more details.
14cabdff1aSopenharmony_ci *
15cabdff1aSopenharmony_ci * You should have received a copy of the GNU General Public License along
16cabdff1aSopenharmony_ci * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
17cabdff1aSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18cabdff1aSopenharmony_ci */
19cabdff1aSopenharmony_ci
20cabdff1aSopenharmony_ci#include <string.h>
21cabdff1aSopenharmony_ci
22cabdff1aSopenharmony_ci#include "libavutil/common.h"
23cabdff1aSopenharmony_ci#include "libavutil/intreadwrite.h"
24cabdff1aSopenharmony_ci#include "libavutil/mem_internal.h"
25cabdff1aSopenharmony_ci
26cabdff1aSopenharmony_ci#include "libswscale/rgb2rgb.h"
27cabdff1aSopenharmony_ci
28cabdff1aSopenharmony_ci#include "checkasm.h"
29cabdff1aSopenharmony_ci
30cabdff1aSopenharmony_ci#define randomize_buffers(buf, size)      \
31cabdff1aSopenharmony_ci    do {                                  \
32cabdff1aSopenharmony_ci        int j;                            \
33cabdff1aSopenharmony_ci        for (j = 0; j < size; j+=4)       \
34cabdff1aSopenharmony_ci            AV_WN32(buf + j, rnd());      \
35cabdff1aSopenharmony_ci    } while (0)
36cabdff1aSopenharmony_ci
37cabdff1aSopenharmony_cistatic const uint8_t width[] = {12, 16, 20, 32, 36, 128};
38cabdff1aSopenharmony_cistatic const struct {uint8_t w, h, s;} planes[] = {
39cabdff1aSopenharmony_ci    {12,16,12}, {16,16,16}, {20,23,25}, {32,18,48}, {8,128,16}, {128,128,128}
40cabdff1aSopenharmony_ci};
41cabdff1aSopenharmony_ci
42cabdff1aSopenharmony_ci#define MAX_STRIDE 128
43cabdff1aSopenharmony_ci#define MAX_HEIGHT 128
44cabdff1aSopenharmony_ci
45cabdff1aSopenharmony_cistatic void check_shuffle_bytes(void * func, const char * report)
46cabdff1aSopenharmony_ci{
47cabdff1aSopenharmony_ci    int i;
48cabdff1aSopenharmony_ci    LOCAL_ALIGNED_32(uint8_t, src0, [MAX_STRIDE]);
49cabdff1aSopenharmony_ci    LOCAL_ALIGNED_32(uint8_t, src1, [MAX_STRIDE]);
50cabdff1aSopenharmony_ci    LOCAL_ALIGNED_32(uint8_t, dst0, [MAX_STRIDE]);
51cabdff1aSopenharmony_ci    LOCAL_ALIGNED_32(uint8_t, dst1, [MAX_STRIDE]);
52cabdff1aSopenharmony_ci
53cabdff1aSopenharmony_ci    declare_func_emms(AV_CPU_FLAG_MMX, void, const uint8_t *src, uint8_t *dst, int src_size);
54cabdff1aSopenharmony_ci
55cabdff1aSopenharmony_ci    memset(dst0, 0, MAX_STRIDE);
56cabdff1aSopenharmony_ci    memset(dst1, 0, MAX_STRIDE);
57cabdff1aSopenharmony_ci    randomize_buffers(src0, MAX_STRIDE);
58cabdff1aSopenharmony_ci    memcpy(src1, src0, MAX_STRIDE);
59cabdff1aSopenharmony_ci
60cabdff1aSopenharmony_ci    if (check_func(func, "%s", report)) {
61cabdff1aSopenharmony_ci        for (i = 0; i < 6; i ++) {
62cabdff1aSopenharmony_ci            call_ref(src0, dst0, width[i]);
63cabdff1aSopenharmony_ci            call_new(src1, dst1, width[i]);
64cabdff1aSopenharmony_ci            if (memcmp(dst0, dst1, MAX_STRIDE))
65cabdff1aSopenharmony_ci                fail();
66cabdff1aSopenharmony_ci        }
67cabdff1aSopenharmony_ci        bench_new(src0, dst0, width[5]);
68cabdff1aSopenharmony_ci    }
69cabdff1aSopenharmony_ci}
70cabdff1aSopenharmony_ci
71cabdff1aSopenharmony_cistatic void check_uyvy_to_422p(void)
72cabdff1aSopenharmony_ci{
73cabdff1aSopenharmony_ci    int i;
74cabdff1aSopenharmony_ci
75cabdff1aSopenharmony_ci    LOCAL_ALIGNED_32(uint8_t, src0, [MAX_STRIDE * MAX_HEIGHT * 2]);
76cabdff1aSopenharmony_ci    LOCAL_ALIGNED_32(uint8_t, src1, [MAX_STRIDE * MAX_HEIGHT * 2]);
77cabdff1aSopenharmony_ci    LOCAL_ALIGNED_32(uint8_t, dst_y_0, [MAX_STRIDE * MAX_HEIGHT]);
78cabdff1aSopenharmony_ci    LOCAL_ALIGNED_32(uint8_t, dst_y_1, [MAX_STRIDE * MAX_HEIGHT]);
79cabdff1aSopenharmony_ci    LOCAL_ALIGNED_32(uint8_t, dst_u_0, [(MAX_STRIDE/2) * MAX_HEIGHT]);
80cabdff1aSopenharmony_ci    LOCAL_ALIGNED_32(uint8_t, dst_u_1, [(MAX_STRIDE/2) * MAX_HEIGHT]);
81cabdff1aSopenharmony_ci    LOCAL_ALIGNED_32(uint8_t, dst_v_0, [(MAX_STRIDE/2) * MAX_HEIGHT]);
82cabdff1aSopenharmony_ci    LOCAL_ALIGNED_32(uint8_t, dst_v_1, [(MAX_STRIDE/2) * MAX_HEIGHT]);
83cabdff1aSopenharmony_ci
84cabdff1aSopenharmony_ci    declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
85cabdff1aSopenharmony_ci                      const uint8_t *src, int width, int height,
86cabdff1aSopenharmony_ci                      int lumStride, int chromStride, int srcStride);
87cabdff1aSopenharmony_ci
88cabdff1aSopenharmony_ci    randomize_buffers(src0, MAX_STRIDE * MAX_HEIGHT * 2);
89cabdff1aSopenharmony_ci    memcpy(src1, src0, MAX_STRIDE * MAX_HEIGHT * 2);
90cabdff1aSopenharmony_ci
91cabdff1aSopenharmony_ci    if (check_func(uyvytoyuv422, "uyvytoyuv422")) {
92cabdff1aSopenharmony_ci        for (i = 0; i < 6; i ++) {
93cabdff1aSopenharmony_ci            memset(dst_y_0, 0, MAX_STRIDE * MAX_HEIGHT);
94cabdff1aSopenharmony_ci            memset(dst_y_1, 0, MAX_STRIDE * MAX_HEIGHT);
95cabdff1aSopenharmony_ci            memset(dst_u_0, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
96cabdff1aSopenharmony_ci            memset(dst_u_1, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
97cabdff1aSopenharmony_ci            memset(dst_v_0, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
98cabdff1aSopenharmony_ci            memset(dst_v_1, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
99cabdff1aSopenharmony_ci
100cabdff1aSopenharmony_ci            call_ref(dst_y_0, dst_u_0, dst_v_0, src0, planes[i].w, planes[i].h,
101cabdff1aSopenharmony_ci                     MAX_STRIDE, MAX_STRIDE / 2, planes[i].s);
102cabdff1aSopenharmony_ci            call_new(dst_y_1, dst_u_1, dst_v_1, src1, planes[i].w, planes[i].h,
103cabdff1aSopenharmony_ci                     MAX_STRIDE, MAX_STRIDE / 2, planes[i].s);
104cabdff1aSopenharmony_ci            if (memcmp(dst_y_0, dst_y_1, MAX_STRIDE * MAX_HEIGHT) ||
105cabdff1aSopenharmony_ci                memcmp(dst_u_0, dst_u_1, (MAX_STRIDE/2) * MAX_HEIGHT) ||
106cabdff1aSopenharmony_ci                memcmp(dst_v_0, dst_v_1, (MAX_STRIDE/2) * MAX_HEIGHT))
107cabdff1aSopenharmony_ci                fail();
108cabdff1aSopenharmony_ci        }
109cabdff1aSopenharmony_ci        bench_new(dst_y_1, dst_u_1, dst_v_1, src1, planes[5].w, planes[5].h,
110cabdff1aSopenharmony_ci                  MAX_STRIDE, MAX_STRIDE / 2, planes[5].s);
111cabdff1aSopenharmony_ci    }
112cabdff1aSopenharmony_ci}
113cabdff1aSopenharmony_ci
114cabdff1aSopenharmony_cistatic void check_interleave_bytes(void)
115cabdff1aSopenharmony_ci{
116cabdff1aSopenharmony_ci    LOCAL_ALIGNED_16(uint8_t, src0_buf, [MAX_STRIDE*MAX_HEIGHT+1]);
117cabdff1aSopenharmony_ci    LOCAL_ALIGNED_16(uint8_t, src1_buf, [MAX_STRIDE*MAX_HEIGHT+1]);
118cabdff1aSopenharmony_ci    LOCAL_ALIGNED_16(uint8_t, dst0_buf, [2*MAX_STRIDE*MAX_HEIGHT+2]);
119cabdff1aSopenharmony_ci    LOCAL_ALIGNED_16(uint8_t, dst1_buf, [2*MAX_STRIDE*MAX_HEIGHT+2]);
120cabdff1aSopenharmony_ci    // Intentionally using unaligned buffers, as this function doesn't have
121cabdff1aSopenharmony_ci    // any alignment requirements.
122cabdff1aSopenharmony_ci    uint8_t *src0 = src0_buf + 1;
123cabdff1aSopenharmony_ci    uint8_t *src1 = src1_buf + 1;
124cabdff1aSopenharmony_ci    uint8_t *dst0 = dst0_buf + 2;
125cabdff1aSopenharmony_ci    uint8_t *dst1 = dst1_buf + 2;
126cabdff1aSopenharmony_ci
127cabdff1aSopenharmony_ci    declare_func_emms(AV_CPU_FLAG_MMX, void, const uint8_t *, const uint8_t *,
128cabdff1aSopenharmony_ci                                       uint8_t *, int, int, int, int, int);
129cabdff1aSopenharmony_ci
130cabdff1aSopenharmony_ci    randomize_buffers(src0, MAX_STRIDE * MAX_HEIGHT);
131cabdff1aSopenharmony_ci    randomize_buffers(src1, MAX_STRIDE * MAX_HEIGHT);
132cabdff1aSopenharmony_ci
133cabdff1aSopenharmony_ci    if (check_func(interleaveBytes, "interleave_bytes")) {
134cabdff1aSopenharmony_ci        for (int i = 0; i <= 16; i++) {
135cabdff1aSopenharmony_ci            // Try all widths [1,16], and try one random width.
136cabdff1aSopenharmony_ci
137cabdff1aSopenharmony_ci            int w = i > 0 ? i : (1 + (rnd() % (MAX_STRIDE-2)));
138cabdff1aSopenharmony_ci            int h = 1 + (rnd() % (MAX_HEIGHT-2));
139cabdff1aSopenharmony_ci
140cabdff1aSopenharmony_ci            int src0_offset = 0, src0_stride = MAX_STRIDE;
141cabdff1aSopenharmony_ci            int src1_offset = 0, src1_stride = MAX_STRIDE;
142cabdff1aSopenharmony_ci            int dst_offset  = 0, dst_stride  = 2 * MAX_STRIDE;
143cabdff1aSopenharmony_ci
144cabdff1aSopenharmony_ci            memset(dst0, 0, 2 * MAX_STRIDE * MAX_HEIGHT);
145cabdff1aSopenharmony_ci            memset(dst1, 0, 2 * MAX_STRIDE * MAX_HEIGHT);
146cabdff1aSopenharmony_ci
147cabdff1aSopenharmony_ci            // Try different combinations of negative strides
148cabdff1aSopenharmony_ci            if (i & 1) {
149cabdff1aSopenharmony_ci                src0_offset = (h-1)*src0_stride;
150cabdff1aSopenharmony_ci                src0_stride = -src0_stride;
151cabdff1aSopenharmony_ci            }
152cabdff1aSopenharmony_ci            if (i & 2) {
153cabdff1aSopenharmony_ci                src1_offset = (h-1)*src1_stride;
154cabdff1aSopenharmony_ci                src1_stride = -src1_stride;
155cabdff1aSopenharmony_ci            }
156cabdff1aSopenharmony_ci            if (i & 4) {
157cabdff1aSopenharmony_ci                dst_offset = (h-1)*dst_stride;
158cabdff1aSopenharmony_ci                dst_stride = -dst_stride;
159cabdff1aSopenharmony_ci            }
160cabdff1aSopenharmony_ci
161cabdff1aSopenharmony_ci            call_ref(src0 + src0_offset, src1 + src1_offset, dst0 + dst_offset,
162cabdff1aSopenharmony_ci                     w, h, src0_stride, src1_stride, dst_stride);
163cabdff1aSopenharmony_ci            call_new(src0 + src0_offset, src1 + src1_offset, dst1 + dst_offset,
164cabdff1aSopenharmony_ci                     w, h, src0_stride, src1_stride, dst_stride);
165cabdff1aSopenharmony_ci            // Check a one pixel-pair edge around the destination area,
166cabdff1aSopenharmony_ci            // to catch overwrites past the end.
167cabdff1aSopenharmony_ci            checkasm_check(uint8_t, dst0, 2*MAX_STRIDE, dst1, 2*MAX_STRIDE,
168cabdff1aSopenharmony_ci                           2 * w + 2, h + 1, "dst");
169cabdff1aSopenharmony_ci        }
170cabdff1aSopenharmony_ci
171cabdff1aSopenharmony_ci        bench_new(src0, src1, dst1, 127, MAX_HEIGHT,
172cabdff1aSopenharmony_ci                  MAX_STRIDE, MAX_STRIDE, 2*MAX_STRIDE);
173cabdff1aSopenharmony_ci    }
174cabdff1aSopenharmony_ci    if (check_func(interleaveBytes, "interleave_bytes_aligned")) {
175cabdff1aSopenharmony_ci        // Bench the function in a more typical case, with aligned
176cabdff1aSopenharmony_ci        // buffers and widths.
177cabdff1aSopenharmony_ci        bench_new(src0_buf, src1_buf, dst1_buf, 128, MAX_HEIGHT,
178cabdff1aSopenharmony_ci                  MAX_STRIDE, MAX_STRIDE, 2*MAX_STRIDE);
179cabdff1aSopenharmony_ci    }
180cabdff1aSopenharmony_ci}
181cabdff1aSopenharmony_ci
182cabdff1aSopenharmony_civoid checkasm_check_sw_rgb(void)
183cabdff1aSopenharmony_ci{
184cabdff1aSopenharmony_ci    ff_sws_rgb2rgb_init();
185cabdff1aSopenharmony_ci
186cabdff1aSopenharmony_ci    check_shuffle_bytes(shuffle_bytes_2103, "shuffle_bytes_2103");
187cabdff1aSopenharmony_ci    report("shuffle_bytes_2103");
188cabdff1aSopenharmony_ci
189cabdff1aSopenharmony_ci    check_shuffle_bytes(shuffle_bytes_0321, "shuffle_bytes_0321");
190cabdff1aSopenharmony_ci    report("shuffle_bytes_0321");
191cabdff1aSopenharmony_ci
192cabdff1aSopenharmony_ci    check_shuffle_bytes(shuffle_bytes_1230, "shuffle_bytes_1230");
193cabdff1aSopenharmony_ci    report("shuffle_bytes_1230");
194cabdff1aSopenharmony_ci
195cabdff1aSopenharmony_ci    check_shuffle_bytes(shuffle_bytes_3012, "shuffle_bytes_3012");
196cabdff1aSopenharmony_ci    report("shuffle_bytes_3012");
197cabdff1aSopenharmony_ci
198cabdff1aSopenharmony_ci    check_shuffle_bytes(shuffle_bytes_3210, "shuffle_bytes_3210");
199cabdff1aSopenharmony_ci    report("shuffle_bytes_3210");
200cabdff1aSopenharmony_ci
201cabdff1aSopenharmony_ci    check_uyvy_to_422p();
202cabdff1aSopenharmony_ci    report("uyvytoyuv422");
203cabdff1aSopenharmony_ci
204cabdff1aSopenharmony_ci    check_interleave_bytes();
205cabdff1aSopenharmony_ci    report("interleave_bytes");
206cabdff1aSopenharmony_ci}
207