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
26 #include "libswscale/swscale.h"
27 #include "libswscale/swscale_internal.h"
28
29 #include "checkasm.h"
30
31 #define randomize_buffers(buf, size) \
32 do { \
33 int j; \
34 for (j = 0; j < size; j+=4) \
35 AV_WN32(buf + j, rnd()); \
36 } while (0)
37
38 #define SRC_PIXELS 128
39
check_hscale(void)40 static void check_hscale(void)
41 {
42 #define MAX_FILTER_WIDTH 40
43 #define FILTER_SIZES 5
44 static const int filter_sizes[FILTER_SIZES] = { 4, 8, 16, 32, 40 };
45
46 #define HSCALE_PAIRS 2
47 static const int hscale_pairs[HSCALE_PAIRS][2] = {
48 { 8, 14 },
49 { 8, 18 },
50 };
51
52 int i, j, fsi, hpi, width;
53 struct SwsContext *ctx;
54
55 // padded
56 LOCAL_ALIGNED_32(uint8_t, src, [FFALIGN(SRC_PIXELS + MAX_FILTER_WIDTH - 1, 4)]);
57 LOCAL_ALIGNED_32(uint32_t, dst0, [SRC_PIXELS]);
58 LOCAL_ALIGNED_32(uint32_t, dst1, [SRC_PIXELS]);
59
60 // padded
61 LOCAL_ALIGNED_32(int16_t, filter, [SRC_PIXELS * MAX_FILTER_WIDTH + MAX_FILTER_WIDTH]);
62 LOCAL_ALIGNED_32(int32_t, filterPos, [SRC_PIXELS]);
63
64 // The dst parameter here is either int16_t or int32_t but we use void* to
65 // just cover both cases.
66 declare_func_emms(AV_CPU_FLAG_MMX, void, void *c, void *dst, int dstW,
67 const uint8_t *src, const int16_t *filter,
68 const int32_t *filterPos, int filterSize);
69
70 ctx = sws_alloc_context();
71 if (sws_init_context(ctx, NULL, NULL) < 0)
72 fail();
73
74 randomize_buffers(src, SRC_PIXELS + MAX_FILTER_WIDTH - 1);
75
76 for (hpi = 0; hpi < HSCALE_PAIRS; hpi++) {
77 for (fsi = 0; fsi < FILTER_SIZES; fsi++) {
78 width = filter_sizes[fsi];
79
80 ctx->srcBpc = hscale_pairs[hpi][0];
81 ctx->dstBpc = hscale_pairs[hpi][1];
82 ctx->hLumFilterSize = ctx->hChrFilterSize = width;
83
84 for (i = 0; i < SRC_PIXELS; i++) {
85 filterPos[i] = i;
86
87 // These filter cofficients are chosen to try break two corner
88 // cases, namely:
89 //
90 // - Negative filter coefficients. The filters output signed
91 // values, and it should be possible to end up with negative
92 // output values.
93 //
94 // - Positive clipping. The hscale filter function has clipping
95 // at (1<<15) - 1
96 //
97 // The coefficients sum to the 1.0 point for the hscale
98 // functions (1 << 14).
99
100 for (j = 0; j < width; j++) {
101 filter[i * width + j] = -((1 << 14) / (width - 1));
102 }
103 filter[i * width + (rnd() % width)] = ((1 << 15) - 1);
104 }
105
106 for (i = 0; i < MAX_FILTER_WIDTH; i++) {
107 // These values should be unused in SIMD implementations but
108 // may still be read, random coefficients here should help show
109 // issues where they are used in error.
110
111 filter[SRC_PIXELS * width + i] = rnd();
112 }
113 ff_getSwsFunc(ctx);
114
115 if (check_func(ctx->hcScale, "hscale_%d_to_%d_width%d", ctx->srcBpc, ctx->dstBpc + 1, width)) {
116 memset(dst0, 0, SRC_PIXELS * sizeof(dst0[0]));
117 memset(dst1, 0, SRC_PIXELS * sizeof(dst1[0]));
118
119 call_ref(NULL, dst0, SRC_PIXELS, src, filter, filterPos, width);
120 call_new(NULL, dst1, SRC_PIXELS, src, filter, filterPos, width);
121 if (memcmp(dst0, dst1, SRC_PIXELS * sizeof(dst0[0])))
122 fail();
123 bench_new(NULL, dst0, SRC_PIXELS, src, filter, filterPos, width);
124 }
125 }
126 }
127 sws_freeContext(ctx);
128 }
129
checkasm_check_sw_scale(void)130 void checkasm_check_sw_scale(void)
131 {
132 check_hscale();
133 report("hscale");
134 }
135