FFmpeg
sw_scale.c
Go to the documentation of this file.
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"
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
58  LOCAL_ALIGNED_32(uint32_t, dst0, [SRC_PIXELS]);
59  LOCAL_ALIGNED_32(uint32_t, dst1, [SRC_PIXELS]);
60 
61  // padded
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 
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 
132 {
133  check_hscale();
134  report("hscale");
135 }
#define NULL
Definition: coverity.c:32
void checkasm_check_sw_scale(void)
Definition: sw_scale.c:131
void(* hcScale)(struct SwsContext *c, int16_t *dst, int dstW, const uint8_t *src, const int16_t *filter, const int32_t *filterPos, int filterSize)
#define HSCALE_PAIRS
static void check_hscale(void)
Definition: sw_scale.c:41
Memory handling functions.
#define report
Definition: checkasm.h:126
uint8_t
av_warn_unused_result int sws_init_context(struct SwsContext *sws_context, SwsFilter *srcFilter, SwsFilter *dstFilter)
Initialize the swscaler context sws_context.
Definition: utils.c:1179
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
#define LOCAL_ALIGNED_32(t, v,...)
Definition: mem_internal.h:136
external API header
#define FFALIGN(x, a)
Definition: macros.h:48
#define src
Definition: vp8dsp.c:255
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
int hLumFilterSize
Horizontal filter size for luma/alpha pixels.
SwsFunc ff_getSwsFunc(SwsContext *c)
Return function pointer to fastest main scaler path function depending on architecture and available ...
Definition: swscale.c:584
#define SRC_PIXELS
Definition: sw_scale.c:39
#define fail()
Definition: checkasm.h:123
int hChrFilterSize
Horizontal filter size for chroma pixels.
#define width
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:2337
#define FILTER_SIZES
#define randomize_buffers(buf, size)
Definition: sw_scale.c:32
#define declare_func_emms(cpu_flags, ret,...)
Definition: checkasm.h:120
int dstW
Width of destination luma/alpha planes.
#define call_ref(...)
Definition: checkasm.h:129
#define AV_CPU_FLAG_MMX
standard MMX
Definition: cpu.h:31
#define MAX_FILTER_WIDTH
#define check_func(func,...)
Definition: checkasm.h:114
struct SwsContext * sws_alloc_context(void)
Allocate an empty SwsContext.
Definition: utils.c:1093
common internal and external API header
#define rnd()
Definition: checkasm.h:107
#define bench_new(...)
Definition: checkasm.h:261
#define call_new(...)
Definition: checkasm.h:201
int i
Definition: input.c:407