FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "config.h"
20 #include "common.h"
21 #include "pixelutils.h"
22 #include "internal.h"
23 
24 #if CONFIG_PIXELUTILS
25 
26 #include "x86/pixelutils.h"
27 
28 static av_always_inline int sad_wxh(const uint8_t *src1, ptrdiff_t stride1,
29  const uint8_t *src2, ptrdiff_t stride2,
30  int w, int h)
31 {
32  int x, y, sum = 0;
33 
34  for (y = 0; y < h; y++) {
35  for (x = 0; x < w; x++)
36  sum += abs(src1[x] - src2[x]);
37  src1 += stride1;
38  src2 += stride2;
39  }
40  return sum;
41 }
42 
43 #define DECLARE_BLOCK_FUNCTIONS(size) \
44 static int block_sad_##size##x##size##_c(const uint8_t *src1, ptrdiff_t stride1, \
45  const uint8_t *src2, ptrdiff_t stride2) \
46 { \
47  return sad_wxh(src1, stride1, src2, stride2, size, size); \
48 }
49 
50 DECLARE_BLOCK_FUNCTIONS(2)
51 DECLARE_BLOCK_FUNCTIONS(4)
52 DECLARE_BLOCK_FUNCTIONS(8)
53 DECLARE_BLOCK_FUNCTIONS(16)
54 
55 static const av_pixelutils_sad_fn sad_c[] = {
56  block_sad_2x2_c,
57  block_sad_4x4_c,
58  block_sad_8x8_c,
59  block_sad_16x16_c,
60 };
61 
62 #endif /* CONFIG_PIXELUTILS */
63 
64 av_pixelutils_sad_fn av_pixelutils_get_sad_fn(int w_bits, int h_bits, int aligned, void *log_ctx)
65 {
66 #if !CONFIG_PIXELUTILS
67  av_log(log_ctx, AV_LOG_ERROR, "pixelutils support is required "
68  "but libavutil is not compiled with it\n");
69  return NULL;
70 #else
72 
73  memcpy(sad, sad_c, sizeof(sad));
74 
75  if (w_bits < 1 || w_bits > FF_ARRAY_ELEMS(sad) ||
76  h_bits < 1 || h_bits > FF_ARRAY_ELEMS(sad))
77  return NULL;
78  if (w_bits != h_bits) // only squared sad for now
79  return NULL;
80 
81 #if ARCH_X86
82  ff_pixelutils_sad_init_x86(sad, aligned);
83 #endif
84 
85  return sad[w_bits - 1];
86 #endif
87 }
88 
89 #ifdef TEST
90 #define W1 320
91 #define H1 240
92 #define W2 640
93 #define H2 480
94 
95 static int run_single_test(const char *test,
96  const uint8_t *block1, ptrdiff_t stride1,
97  const uint8_t *block2, ptrdiff_t stride2,
98  int align, int n)
99 {
100  int out, ref;
101  av_pixelutils_sad_fn f_ref = sad_c[n - 1];
102  av_pixelutils_sad_fn f_out = av_pixelutils_get_sad_fn(n, n, align, NULL);
103 
104  switch (align) {
105  case 0: block1++; block2++; break;
106  case 1: block2++; break;
107  case 2: break;
108  }
109 
110  out = f_out(block1, stride1, block2, stride2);
111  ref = f_ref(block1, stride1, block2, stride2);
112  printf("[%s] [%c%c] SAD [%s] %dx%d=%d ref=%d\n",
113  out == ref ? "OK" : "FAIL",
114  align ? 'A' : 'U', align == 2 ? 'A' : 'U',
115  test, 1<<n, 1<<n, out, ref);
116  return out != ref;
117 }
118 
119 static int run_test(const char *test,
120  const uint8_t *b1, const uint8_t *b2)
121 {
122  int i, a, ret = 0;
123 
124  for (a = 0; a < 3; a++) {
125  const uint8_t *block1 = b1;
126  const uint8_t *block2 = b2;
127 
128  switch (a) {
129  case 0: block1++; block2++; break;
130  case 1: block2++; break;
131  case 2: break;
132  }
133  for (i = 1; i <= FF_ARRAY_ELEMS(sad_c); i++) {
134  int r = run_single_test(test, b1, W1, b2, W2, a, i);
135  if (r)
136  ret = r;
137  }
138  }
139  return ret;
140 }
141 
142 int main(void)
143 {
144  int i, align, ret;
145  uint8_t *buf1 = av_malloc(W1*H1);
146  uint8_t *buf2 = av_malloc(W2*H2);
147  uint32_t state = 0;
148 
149  if (!buf1 || !buf2) {
150  fprintf(stderr, "malloc failure\n");
151  ret = 1;
152  goto end;
153  }
154 
156 
157 #define RANDOM_INIT(buf, size) do { \
158  int k; \
159  for (k = 0; k < size; k++) { \
160  state = state * 1664525 + 1013904223; \
161  buf[k] = state>>24; \
162  } \
163 } while (0)
164 
165  /* Normal test with different strides */
166  RANDOM_INIT(buf1, W1*H1);
167  RANDOM_INIT(buf2, W2*H2);
168  ret = run_test("random", buf1, buf2);
169  if (ret < 0)
170  goto end;
171 
172  /* Check for maximum SAD */
173  memset(buf1, 0xff, W1*H1);
174  memset(buf2, 0x00, W2*H2);
175  ret = run_test("max", buf1, buf2);
176  if (ret < 0)
177  goto end;
178 
179  /* Check for minimum SAD */
180  memset(buf1, 0x90, W1*H1);
181  memset(buf2, 0x90, W2*H2);
182  ret = run_test("min", buf1, buf2);
183  if (ret < 0)
184  goto end;
185 
186  /* Exact buffer sizes, to check for overreads */
187  for (i = 1; i <= 4; i++) {
188  for (align = 0; align < 3; align++) {
189  int size1, size2;
190 
191  av_freep(&buf1);
192  av_freep(&buf2);
193 
194  size1 = size2 = 1 << (i << 1);
195 
196  switch (align) {
197  case 0: size1++; size2++; break;
198  case 1: size2++; break;
199  case 2: break;
200  }
201 
202  buf1 = av_malloc(size1);
203  buf2 = av_malloc(size2);
204  if (!buf1 || !buf2) {
205  fprintf(stderr, "malloc failure\n");
206  ret = 1;
207  goto end;
208  }
209  RANDOM_INIT(buf1, size1);
210  RANDOM_INIT(buf2, size2);
211  ret = run_single_test("small", buf1, 1<<i, buf2, 1<<i, align, i);
212  if (ret < 0)
213  goto end;
214  }
215  }
216 
217 end:
218  av_free(buf1);
219  av_free(buf2);
220  return ret;
221 }
222 #endif /* TEST */
#define NULL
Definition: coverity.c:32
static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, AVCodecContext *dec_ctx)
#define W2
#define W1
uint8_t
#define av_malloc(s)
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void ff_pixelutils_sad_init_x86(av_pixelutils_sad_fn *sad, int aligned)
const char * r
Definition: vf_curves.c:107
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:29
common internal API header
static void test(const char *pattern, const char *host)
Definition: noproxy-test.c:23
float y
static struct @197 state
int n
Definition: avisynth_c.h:547
#define FF_ARRAY_ELEMS(a)
const AVS_VideoInfo int align
Definition: avisynth_c.h:658
#define src1
Definition: h264pred.c:139
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:64
static int16_t block1[64]
Definition: dct-test.c:111
common internal and external API header
void ff_check_pixfmt_descriptors(void)
Definition: pixdesc.c:2184
#define av_free(p)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
#define av_freep(p)
#define av_always_inline
Definition: attributes.h:37
int main(int argc, char **argv)
Definition: main.c:22