]> git.sesse.net Git - ffmpeg/blob - tests/checkasm/sw_scale.c
lavu: move LOCAL_ALIGNED from internal.h to mem_internal.h
[ffmpeg] / tests / checkasm / sw_scale.c
1 /*
2  *
3  * This file is part of FFmpeg.
4  *
5  * FFmpeg is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * FFmpeg is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <string.h>
21
22 #include "libavutil/common.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/mem_internal.h"
26
27 #include "libswscale/swscale.h"
28 #include "libswscale/swscale_internal.h"
29
30 #include "checkasm.h"
31
32 #define randomize_buffers(buf, size)      \
33     do {                                  \
34         int j;                            \
35         for (j = 0; j < size; j+=4)       \
36             AV_WN32(buf + j, rnd());      \
37     } while (0)
38
39 #define SRC_PIXELS 128
40
41 static void check_hscale(void)
42 {
43 #define MAX_FILTER_WIDTH 40
44 #define FILTER_SIZES 5
45     static const int filter_sizes[FILTER_SIZES] = { 4, 8, 16, 32, 40 };
46
47 #define HSCALE_PAIRS 2
48     static const int hscale_pairs[HSCALE_PAIRS][2] = {
49         { 8, 14 },
50         { 8, 18 },
51     };
52
53     int i, j, fsi, hpi, width;
54     struct SwsContext *ctx;
55
56     // padded
57     LOCAL_ALIGNED_32(uint8_t, src, [FFALIGN(SRC_PIXELS + MAX_FILTER_WIDTH - 1, 4)]);
58     LOCAL_ALIGNED_32(uint32_t, dst0, [SRC_PIXELS]);
59     LOCAL_ALIGNED_32(uint32_t, dst1, [SRC_PIXELS]);
60
61     // padded
62     LOCAL_ALIGNED_32(int16_t, filter, [SRC_PIXELS * MAX_FILTER_WIDTH + MAX_FILTER_WIDTH]);
63     LOCAL_ALIGNED_32(int32_t, filterPos, [SRC_PIXELS]);
64
65     // The dst parameter here is either int16_t or int32_t but we use void* to
66     // just cover both cases.
67     declare_func_emms(AV_CPU_FLAG_MMX, void, void *c, void *dst, int dstW,
68                       const uint8_t *src, const int16_t *filter,
69                       const int32_t *filterPos, int filterSize);
70
71     ctx = sws_alloc_context();
72     if (sws_init_context(ctx, NULL, NULL) < 0)
73         fail();
74
75     randomize_buffers(src, SRC_PIXELS + MAX_FILTER_WIDTH - 1);
76
77     for (hpi = 0; hpi < HSCALE_PAIRS; hpi++) {
78         for (fsi = 0; fsi < FILTER_SIZES; fsi++) {
79             width = filter_sizes[fsi];
80
81             ctx->srcBpc = hscale_pairs[hpi][0];
82             ctx->dstBpc = hscale_pairs[hpi][1];
83             ctx->hLumFilterSize = ctx->hChrFilterSize = width;
84
85             for (i = 0; i < SRC_PIXELS; i++) {
86                 filterPos[i] = i;
87
88                 // These filter cofficients are chosen to try break two corner
89                 // cases, namely:
90                 //
91                 // - Negative filter coefficients. The filters output signed
92                 //   values, and it should be possible to end up with negative
93                 //   output values.
94                 //
95                 // - Positive clipping. The hscale filter function has clipping
96                 //   at (1<<15) - 1
97                 //
98                 // The coefficients sum to the 1.0 point for the hscale
99                 // functions (1 << 14).
100
101                 for (j = 0; j < width; j++) {
102                     filter[i * width + j] = -((1 << 14) / (width - 1));
103                 }
104                 filter[i * width + (rnd() % width)] = ((1 << 15) - 1);
105             }
106
107             for (i = 0; i < MAX_FILTER_WIDTH; i++) {
108                 // These values should be unused in SIMD implementations but
109                 // may still be read, random coefficients here should help show
110                 // issues where they are used in error.
111
112                 filter[SRC_PIXELS * width + i] = rnd();
113             }
114             ff_getSwsFunc(ctx);
115
116             if (check_func(ctx->hcScale, "hscale_%d_to_%d_width%d", ctx->srcBpc, ctx->dstBpc + 1, width)) {
117                 memset(dst0, 0, SRC_PIXELS * sizeof(dst0[0]));
118                 memset(dst1, 0, SRC_PIXELS * sizeof(dst1[0]));
119
120                 call_ref(NULL, dst0, SRC_PIXELS, src, filter, filterPos, width);
121                 call_new(NULL, dst1, SRC_PIXELS, src, filter, filterPos, width);
122                 if (memcmp(dst0, dst1, SRC_PIXELS * sizeof(dst0[0])))
123                     fail();
124                 bench_new(NULL, dst0, SRC_PIXELS, src, filter, filterPos, width);
125             }
126         }
127     }
128     sws_freeContext(ctx);
129 }
130
131 void checkasm_check_sw_scale(void)
132 {
133     check_hscale();
134     report("hscale");
135 }