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 
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  }
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  }
128 }
129 
131 {
132  check_hscale();
133  report("hscale");
134 }
FILTER_SIZES
#define FILTER_SIZES
declare_func_emms
#define declare_func_emms(cpu_flags, ret,...)
Definition: checkasm.h:120
SwsContext::dstW
int dstW
Width of destination luma/alpha planes.
Definition: swscale_internal.h:478
check_func
#define check_func(func,...)
Definition: checkasm.h:114
filter
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
Definition: filter_design.txt:228
call_ref
#define call_ref(...)
Definition: checkasm.h:129
fail
#define fail()
Definition: checkasm.h:123
checkasm.h
check_hscale
static void check_hscale(void)
Definition: sw_scale.c:40
rnd
#define rnd()
Definition: checkasm.h:107
width
#define width
intreadwrite.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
int32_t
int32_t
Definition: audio_convert.c:194
HSCALE_PAIRS
#define HSCALE_PAIRS
SRC_PIXELS
#define SRC_PIXELS
Definition: sw_scale.c:38
call_new
#define call_new(...)
Definition: checkasm.h:201
NULL
#define NULL
Definition: coverity.c:32
src
#define src
Definition: vp8dsp.c:254
sws_alloc_context
struct SwsContext * sws_alloc_context(void)
Allocate an empty SwsContext.
Definition: utils.c:1084
c
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
ff_getSwsFunc
SwsFunc ff_getSwsFunc(SwsContext *c)
Return function pointer to fastest main scaler path function depending on architecture and available ...
Definition: swscale.c:583
report
#define report
Definition: checkasm.h:126
bench_new
#define bench_new(...)
Definition: checkasm.h:261
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
common.h
swscale_internal.h
uint8_t
uint8_t
Definition: audio_convert.c:194
randomize_buffers
#define randomize_buffers(buf, size)
Definition: sw_scale.c:31
sws_init_context
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:1170
AV_CPU_FLAG_MMX
#define AV_CPU_FLAG_MMX
standard MMX
Definition: cpu.h:31
sws_freeContext
void sws_freeContext(struct SwsContext *swsContext)
Free the swscaler context swsContext.
Definition: utils.c:2319
mem.h
MAX_FILTER_WIDTH
#define MAX_FILTER_WIDTH
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:48
LOCAL_ALIGNED_32
#define LOCAL_ALIGNED_32(t, v,...)
Definition: internal.h:137
checkasm_check_sw_scale
void checkasm_check_sw_scale(void)
Definition: sw_scale.c:130
SwsContext
Definition: swscale_internal.h:280
swscale.h