FFmpeg
convolution.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2013 Oka Motofumi (chikuzen.mo at gmail dot com)
3  * Copyright (c) 2015 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef AVFILTER_CONVOLUTION_H
22 #define AVFILTER_CONVOLUTION_H
23 #include "avfilter.h"
24 #include "libavutil/intreadwrite.h"
25 
26 enum MatrixMode {
31 };
32 
33 typedef struct ConvolutionContext {
34  const AVClass *class;
35 
36  char *matrix_str[4];
37  float user_rdiv[4];
38  float bias[4];
39  int mode[4];
40  float scale;
41  float delta;
42  int planes;
43 
44  float rdiv[4];
45  int size[4];
46  int depth;
47  int max;
48  int bpc;
49  int nb_planes;
51  int planewidth[4];
52  int planeheight[4];
53  int matrix[4][49];
54  int matrix_length[4];
55  int copy[4];
56 
57  void (*setup[4])(int radius, const uint8_t *c[], const uint8_t *src, int stride,
58  int x, int width, int y, int height, int bpc);
59  void (*filter[4])(uint8_t *dst, int width,
60  float rdiv, float bias, const int *const matrix,
61  const uint8_t *c[], int peak, int radius,
62  int dstride, int stride, int size);
64 
66 void ff_sobel_init_x86(ConvolutionContext *s, int depth, int nb_planes);
67 
68 static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride,
69  int x, int w, int y, int h, int bpc)
70 {
71  int i;
72 
73  for (i = 0; i < 9; i++) {
74  int xoff = FFABS(x + ((i % 3) - 1));
75  int yoff = FFABS(y + (i / 3) - 1);
76 
77  xoff = xoff >= w ? 2 * w - 1 - xoff : xoff;
78  yoff = yoff >= h ? 2 * h - 1 - yoff : yoff;
79 
80  c[i] = src + xoff * bpc + yoff * stride;
81  }
82 }
83 
84 static void filter_sobel(uint8_t *dst, int width,
85  float scale, float delta, const int *const matrix,
86  const uint8_t *c[], int peak, int radius,
87  int dstride, int stride, int size)
88 {
89  const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
90  const uint8_t *c3 = c[3], *c5 = c[5];
91  const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
92  int x;
93 
94  for (x = 0; x < width; x++) {
95  float suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 +
96  c6[x] * 1 + c7[x] * 2 + c8[x] * 1;
97  float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -2 +
98  c5[x] * 2 + c6[x] * -1 + c8[x] * 1;
99 
100  dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
101  }
102 }
103 
104 static void filter16_sobel(uint8_t *dstp, int width,
105  float scale, float delta, const int *const matrix,
106  const uint8_t *c[], int peak, int radius,
107  int dstride, int stride, int size)
108 {
109  uint16_t *dst = (uint16_t *)dstp;
110  int x;
111 
112  for (x = 0; x < width; x++) {
113  float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -2 + AV_RN16A(&c[2][2 * x]) * -1 +
114  AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 2 + AV_RN16A(&c[8][2 * x]) * 1;
115  float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -2 +
116  AV_RN16A(&c[5][2 * x]) * 2 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
117 
118  dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
119  }
120 }
121 
122 static inline void ff_sobel_init(ConvolutionContext *s, int depth, int nb_planes)
123 {
124  for (int i = 0; i < 4; i++) {
125  s->filter[i] = filter_sobel;
126  s->copy[i] = !((1 << i) & s->planes);
127  s->size[i] = 3;
128  s->setup[i] = setup_3x3;
129  s->rdiv[i] = s->scale;
130  s->bias[i] = s->delta;
131  }
132  if (s->depth > 8)
133  for (int i = 0; i < 4; i++)
134  s->filter[i] = filter16_sobel;
135 #if ARCH_X86_64
136  ff_sobel_init_x86(s, depth, nb_planes);
137 #endif
138 }
139 #endif
setup_3x3
static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride, int x, int w, int y, int h, int bpc)
Definition: convolution.h:68
ConvolutionContext::max
int max
Definition: convolution.h:47
av_clip
#define av_clip
Definition: common.h:100
ConvolutionContext::rdiv
float rdiv[4]
Definition: convolution.h:44
matrix
Definition: vc1dsp.c:43
ConvolutionContext::copy
int copy[4]
Definition: convolution.h:55
filter16_sobel
static void filter16_sobel(uint8_t *dstp, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: convolution.h:104
ff_sobel_init_x86
void ff_sobel_init_x86(ConvolutionContext *s, int depth, int nb_planes)
Definition: vf_convolution_init.c:53
w
uint8_t w
Definition: llviddspenc.c:38
c1
static const uint64_t c1
Definition: murmur3.c:52
ConvolutionContext::matrix
int matrix[4][49]
Definition: convolution.h:53
ConvolutionContext::bias
float bias[4]
Definition: convolution.h:38
ConvolutionContext::nb_threads
int nb_threads
Definition: convolution.h:50
ConvolutionContext::planewidth
int planewidth[4]
Definition: convolution.h:51
MATRIX_NBMODES
@ MATRIX_NBMODES
Definition: convolution.h:30
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
MATRIX_SQUARE
@ MATRIX_SQUARE
Definition: convolution.h:27
ConvolutionContext::planeheight
int planeheight[4]
Definition: convolution.h:52
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
ConvolutionContext::user_rdiv
float user_rdiv[4]
Definition: convolution.h:37
MATRIX_ROW
@ MATRIX_ROW
Definition: convolution.h:28
ConvolutionContext::matrix_str
char * matrix_str[4]
Definition: convolution.h:36
sqrtf
static __device__ float sqrtf(float a)
Definition: cuda_runtime.h:184
ConvolutionContext::depth
int depth
Definition: convolution.h:46
ConvolutionContext
Definition: convolution.h:33
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
filter_sobel
static void filter_sobel(uint8_t *dst, int width, float scale, float delta, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: convolution.h:84
ConvolutionContext::scale
float scale
Definition: convolution.h:40
height
#define height
Definition: dsp.h:85
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
size
int size
Definition: twinvq_data.h:10344
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ConvolutionContext::nb_planes
int nb_planes
Definition: convolution.h:49
ConvolutionContext::size
int size[4]
Definition: convolution.h:45
delta
float delta
Definition: vorbis_enc_data.h:430
MatrixMode
MatrixMode
Definition: convolution.h:26
ConvolutionContext::bpc
int bpc
Definition: convolution.h:48
stride
#define stride
Definition: h264pred_template.c:537
c2
static const uint64_t c2
Definition: murmur3.c:53
mode
mode
Definition: ebur128.h:83
avfilter.h
AV_RN16A
#define AV_RN16A(p)
Definition: intreadwrite.h:518
ConvolutionContext::filter
void(* filter[4])(uint8_t *dst, int width, float rdiv, float bias, const int *const matrix, const uint8_t *c[], int peak, int radius, int dstride, int stride, int size)
Definition: convolution.h:59
ConvolutionContext::setup
void(* setup[4])(int radius, const uint8_t *c[], const uint8_t *src, int stride, int x, int width, int y, int height, int bpc)
Definition: convolution.h:57
av_clip_uint8
#define av_clip_uint8
Definition: common.h:106
MATRIX_COLUMN
@ MATRIX_COLUMN
Definition: convolution.h:29
ConvolutionContext::delta
float delta
Definition: convolution.h:41
ff_convolution_init_x86
void ff_convolution_init_x86(ConvolutionContext *s)
Definition: vf_convolution_init.c:37
ff_sobel_init
static void ff_sobel_init(ConvolutionContext *s, int depth, int nb_planes)
Definition: convolution.h:122
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:291
h
h
Definition: vp9dsp_template.c:2070
ConvolutionContext::matrix_length
int matrix_length[4]
Definition: convolution.h:54
width
#define width
Definition: dsp.h:85
src
#define src
Definition: vp8dsp.c:248
ConvolutionContext::planes
int planes
Definition: convolution.h:42