FFmpeg
Loading...
Searching...
No Matches
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/internal.h"
26
33
34typedef struct ConvolutionContext {
35 const AVClass *class;
36
37 char *matrix_str[4];
38 float user_rdiv[4];
39 float bias[4];
40 int mode[4];
42 float scale;
43 float delta;
44 int planes;
45
46 float rdiv[4];
47 int size[4];
48 int depth;
49 int max;
50 int bpc;
53 int planewidth[4];
55 int matrix[4][49];
57 int copy[4];
58
59 void (*setup[4])(int radius, const uint8_t *c[], const uint8_t *src, int stride,
60 int x, int width, int y, int height, int bpc);
61 void (*filter[4])(uint8_t *dst, int width,
62 float rdiv, float bias, const int *const matrix,
63 const uint8_t *c[], int peak, int radius,
64 int dstride, int stride, int size);
66
68void ff_sobel_init_x86(ConvolutionContext *s, int depth, int nb_planes);
69
70static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride,
71 int x, int w, int y, int h, int bpc)
72{
73 int i;
74
75 for (i = 0; i < 9; i++) {
76 int xoff = avpriv_mirror(x + (i % 3) - 1, w - 1);
77 int yoff = avpriv_mirror(y + (i / 3) - 1, h - 1);
78
79 c[i] = src + xoff * bpc + yoff * stride;
80 }
81}
82
83static void filter_sobel(uint8_t *dst, int width,
84 float scale, float delta, const int *const matrix,
85 const uint8_t *c[], int peak, int radius,
86 int dstride, int stride, int size)
87{
88 const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2];
89 const uint8_t *c3 = c[3], *c5 = c[5];
90 const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8];
91 int x;
92
93 for (x = 0; x < width; x++) {
94 float suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 +
95 c6[x] * 1 + c7[x] * 2 + c8[x] * 1;
96 float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -2 +
97 c5[x] * 2 + c6[x] * -1 + c8[x] * 1;
98
99 dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta);
100 }
101}
102
103static void filter16_sobel(uint8_t *dstp, int width,
104 float scale, float delta, const int *const matrix,
105 const uint8_t *c[], int peak, int radius,
106 int dstride, int stride, int size)
107{
108 uint16_t *dst = (uint16_t *)dstp;
109 int x;
110
111 for (x = 0; x < width; x++) {
112 float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -2 + AV_RN16A(&c[2][2 * x]) * -1 +
113 AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 2 + AV_RN16A(&c[8][2 * x]) * 1;
114 float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -2 +
115 AV_RN16A(&c[5][2 * x]) * 2 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1;
116
117 dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak);
118 }
119}
120
121static inline void ff_sobel_init(ConvolutionContext *s, int depth, int nb_planes)
122{
123 for (int i = 0; i < 4; i++) {
124 s->filter[i] = filter_sobel;
125 s->copy[i] = !((1 << i) & s->planes);
126 s->size[i] = 3;
127 s->setup[i] = setup_3x3;
128 s->rdiv[i] = s->scale;
129 s->bias[i] = s->delta;
130 }
131 if (s->depth > 8)
132 for (int i = 0; i < 4; i++)
133 s->filter[i] = filter16_sobel;
134#if ARCH_X86_64 && HAVE_X86ASM
135 ff_sobel_init_x86(s, depth, nb_planes);
136#endif
137}
138#endif
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define av_clip
Definition common.h:100
#define av_clip_uint8
Definition common.h:106
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)
void ff_convolution_init_x86(ConvolutionContext *s)
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:83
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:70
MatrixMode
Definition convolution.h:27
@ MATRIX_SQUARE
Definition convolution.h:28
@ MATRIX_COLUMN
Definition convolution.h:30
@ MATRIX_ROW
Definition convolution.h:29
@ MATRIX_NBMODES
Definition convolution.h:31
void ff_sobel_init_x86(ConvolutionContext *s, int depth, int nb_planes)
static void ff_sobel_init(ConvolutionContext *s, int depth, int nb_planes)
static __device__ float sqrtf(float a)
static av_always_inline void scale(vec_s32 res[4], vec_s16 res_packed[2], const int shift)
#define AV_RN16A(p)
common internal API header
static av_always_inline av_const int avpriv_mirror(int x, int w)
Definition internal.h:144
uint8_t w
Definition llvidencdsp.c:39
static const uint64_t c2
Definition murmur3.c:53
static const uint64_t c1
Definition murmur3.c:52
Describe the class of an AVClass context structure.
Definition log.h:76
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:61
int matrix[4][49]
Definition convolution.h:55
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:59
char * matrix_str[4]
Definition convolution.h:37
#define stride
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
int size
float delta
static double c[64]