FFmpeg
pixelutils.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <stdio.h>
20 
21 #include "libavutil/internal.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/pixelutils.c"
24 
25 #define W1 320
26 #define H1 240
27 #define W2 640
28 #define H2 480
29 
30 static int run_single_test(const char *test,
31  const uint8_t *block1, ptrdiff_t stride1,
32  const uint8_t *block2, ptrdiff_t stride2,
33  int align, int n)
34 {
35  int out, ref;
36  av_pixelutils_sad_fn f_ref = sad_c[n - 1];
38 
39  switch (align) {
40  case 0: block1++; block2++; break;
41  case 1: block2++; break;
42  case 2: break;
43  }
44 
45  out = f_out(block1, stride1, block2, stride2);
46  ref = f_ref(block1, stride1, block2, stride2);
47  printf("[%s] [%c%c] SAD [%s] %dx%d=%d ref=%d\n",
48  out == ref ? "OK" : "FAIL",
49  align ? 'A' : 'U', align == 2 ? 'A' : 'U',
50  test, 1<<n, 1<<n, out, ref);
51  return out != ref;
52 }
53 
54 static int run_test(const char *test,
55  const uint8_t *b1, const uint8_t *b2)
56 {
57  int i, a, ret = 0;
58 
59  for (a = 0; a < 3; a++) {
60  const uint8_t *block1 = b1;
61  const uint8_t *block2 = b2;
62 
63  switch (a) {
64  case 0: block1++; block2++; break;
65  case 1: block2++; break;
66  case 2: break;
67  }
68  for (i = 1; i <= FF_ARRAY_ELEMS(sad_c); i++) {
69  int r = run_single_test(test, b1, W1, b2, W2, a, i);
70  if (r)
71  ret = r;
72  }
73  }
74  return ret;
75 }
76 
77 int main(void)
78 {
79  int i, align, ret;
80  uint8_t *buf1 = av_malloc(W1*H1);
81  uint8_t *buf2 = av_malloc(W2*H2);
82  uint32_t state = 0;
83 
84  if (!buf1 || !buf2) {
85  fprintf(stderr, "malloc failure\n");
86  ret = 1;
87  goto end;
88  }
89 
91 
92 #define RANDOM_INIT(buf, size) do { \
93  int k; \
94  for (k = 0; k < size; k++) { \
95  state = state * 1664525 + 1013904223; \
96  buf[k] = state>>24; \
97  } \
98 } while (0)
99 
100  /* Normal test with different strides */
101  RANDOM_INIT(buf1, W1*H1);
102  RANDOM_INIT(buf2, W2*H2);
103  ret = run_test("random", buf1, buf2);
104  if (ret < 0)
105  goto end;
106 
107  /* Check for maximum SAD */
108  memset(buf1, 0xff, W1*H1);
109  memset(buf2, 0x00, W2*H2);
110  ret = run_test("max", buf1, buf2);
111  if (ret < 0)
112  goto end;
113 
114  /* Check for minimum SAD */
115  memset(buf1, 0x90, W1*H1);
116  memset(buf2, 0x90, W2*H2);
117  ret = run_test("min", buf1, buf2);
118  if (ret < 0)
119  goto end;
120 
121  /* Exact buffer sizes, to check for overreads */
122  for (i = 1; i <= 5; i++) {
123  for (align = 0; align < 3; align++) {
124  int size1, size2;
125 
126  av_freep(&buf1);
127  av_freep(&buf2);
128 
129  size1 = size2 = 1 << (i << 1);
130 
131  switch (align) {
132  case 0: size1++; size2++; break;
133  case 1: size2++; break;
134  case 2: break;
135  }
136 
137  buf1 = av_malloc(size1);
138  buf2 = av_malloc(size2);
139  if (!buf1 || !buf2) {
140  fprintf(stderr, "malloc failure\n");
141  ret = 1;
142  goto end;
143  }
144  RANDOM_INIT(buf1, size1);
145  RANDOM_INIT(buf2, size2);
146  ret = run_single_test("small", buf1, 1<<i, buf2, 1<<i, align, i);
147  if (ret < 0)
148  goto end;
149  }
150  }
151 
152 end:
153  av_free(buf1);
154  av_free(buf2);
155  return ret;
156 }
r
const char * r
Definition: vf_curves.c:116
out
FILE * out
Definition: movenc.c:54
av_pixelutils_sad_fn
int(* av_pixelutils_sad_fn)(const uint8_t *src1, ptrdiff_t stride1, const uint8_t *src2, ptrdiff_t stride2)
Sum of abs(src1[x] - src2[x])
Definition: pixelutils.h:28
H2
#define H2
Definition: pixelutils.c:28
test
Definition: idctdsp.c:34
W2
#define W2
Definition: pixelutils.c:27
run_single_test
static int run_single_test(const char *test, const uint8_t *block1, ptrdiff_t stride1, const uint8_t *block2, ptrdiff_t stride2, int align, int n)
Definition: pixelutils.c:30
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:1771
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
NULL
#define NULL
Definition: coverity.c:32
main
int main(void)
Definition: pixelutils.c:77
state
static struct @327 state
printf
printf("static const uint8_t my_array[100] = {\n")
b2
static double b2(void *priv, double x, double y)
Definition: vf_xfade.c:1772
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
ff_check_pixfmt_descriptors
void ff_check_pixfmt_descriptors(void)
Definition: pixdesc.c:2717
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
internal.h
W1
#define W1
Definition: pixelutils.c:25
ret
ret
Definition: filter_design.txt:187
av_pixelutils_get_sad_fn
av_pixelutils_sad_fn av_pixelutils_get_sad_fn(int w_bits, int h_bits, int aligned, void *log_ctx)
Get a potentially optimized pointer to a Sum-of-absolute-differences function (see the av_pixelutils_...
Definition: pixelutils.c:72
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
H1
#define H1
Definition: pixelutils.c:26
mem.h
RANDOM_INIT
#define RANDOM_INIT(buf, size)
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
pixelutils.c
block1
static int16_t block1[64]
Definition: dct.c:118
run_test
static int run_test(const char *test, const uint8_t *b1, const uint8_t *b2)
Definition: pixelutils.c:54