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 
26 #include "libswscale/swscale.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 
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
57  LOCAL_ALIGNED_32(uint32_t, dst0, [SRC_PIXELS]);
58  LOCAL_ALIGNED_32(uint32_t, dst1, [SRC_PIXELS]);
59 
60  // padded
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 
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 
131 {
132  check_hscale();
133  report("hscale");
134 }
#define NULL
Definition: coverity.c:32
void checkasm_check_sw_scale(void)
Definition: sw_scale.c:130
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:40
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:1174
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
external API header
#define FFALIGN(x, a)
Definition: macros.h:48
#define src
Definition: vp8dsp.c:254
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:583
#define SRC_PIXELS
Definition: sw_scale.c:38
#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:2332
#define FILTER_SIZES
#define randomize_buffers(buf, size)
Definition: sw_scale.c:31
#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:1088
#define LOCAL_ALIGNED_32(t, v,...)
Definition: internal.h:137
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