FFmpeg
edge_template.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Thilo Borgmann <thilo.borgmann _at_ mail.de>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  * Redistribution and use in source and binary forms, with or without modification,
20  * are permitted provided that the following conditions are met:
21  */
22 
23 #include "libavutil/avassert.h"
24 #include "avfilter.h"
25 #include "internal.h"
26 #include "video.h"
27 
28 #undef pixel
29 #if DEPTH == 8
30 #define pixel uint8_t
31 #else
32 #define pixel uint16_t
33 #endif
34 
35 #undef fn
36 #undef fn2
37 #undef fn3
38 #define fn3(a,b) ff_##a##_##b
39 #define fn2(a,b) fn3(a,b)
40 #define fn(a) fn2(a, DEPTH)
41 
42 void fn(sobel)(int w, int h,
43  uint16_t *dst, int dst_linesize,
44  int8_t *dir, int dir_linesize,
45  const uint8_t *src, int src_linesize, int src_stride)
46 {
47  pixel *srcp = (pixel *)src;
48 
49  src_stride /= sizeof(pixel);
50  src_linesize /= sizeof(pixel);
51  dst_linesize /= sizeof(pixel);
52 
53  for (int j = 1; j < h - 1; j++) {
54  dst += dst_linesize;
55  dir += dir_linesize;
56  srcp += src_linesize;
57  for (int i = 1; i < w - 1; i++) {
58  const int gx =
59  -1*srcp[-src_linesize + (i-1)*src_stride] + 1*srcp[-src_linesize + (i+1)*src_stride]
60  -2*srcp[ (i-1)*src_stride] + 2*srcp[ (i+1)*src_stride]
61  -1*srcp[ src_linesize + (i-1)*src_stride] + 1*srcp[ src_linesize + (i+1)*src_stride];
62  const int gy =
63  -1*srcp[-src_linesize + (i-1)*src_stride] + 1*srcp[ src_linesize + (i-1)*src_stride]
64  -2*srcp[-src_linesize + (i )*src_stride] + 2*srcp[ src_linesize + (i )*src_stride]
65  -1*srcp[-src_linesize + (i+1)*src_stride] + 1*srcp[ src_linesize + (i+1)*src_stride];
66 
67  dst[i] = FFABS(gx) + FFABS(gy);
68  dir[i] = get_rounded_direction(gx, gy);
69  }
70  }
71 }
72 
73 void fn(gaussian_blur)(int w, int h,
74  uint8_t *dst, int dst_linesize,
75  const uint8_t *src, int src_linesize, int src_stride)
76 {
77  int j;
78  pixel *srcp = (pixel *)src;
79  pixel *dstp = (pixel *)dst;
80 
81  src_stride /= sizeof(pixel);
82  src_linesize /= sizeof(pixel);
83  dst_linesize /= sizeof(pixel);
84 
85  for (j = 0; j < FFMIN(h, 2); j++) {
86  memcpy(dstp, srcp, w*sizeof(pixel));
87  dstp += dst_linesize;
88  srcp += src_linesize;
89  }
90 
91  for (; j < h - 2; j++) {
92  int i;
93  for (i = 0; i < FFMIN(w, 2); i++)
94  dstp[i] = srcp[i*src_stride];
95  for (; i < w - 2; i++) {
96  /* Gaussian mask of size 5x5 with sigma = 1.4 */
97  dstp[i] = ((srcp[-2*src_linesize + (i-2)*src_stride] + srcp[2*src_linesize + (i-2)*src_stride]) * 2
98  + (srcp[-2*src_linesize + (i-1)*src_stride] + srcp[2*src_linesize + (i-1)*src_stride]) * 4
99  + (srcp[-2*src_linesize + (i )*src_stride] + srcp[2*src_linesize + (i )*src_stride]) * 5
100  + (srcp[-2*src_linesize + (i+1)*src_stride] + srcp[2*src_linesize + (i+1)*src_stride]) * 4
101  + (srcp[-2*src_linesize + (i+2)*src_stride] + srcp[2*src_linesize + (i+2)*src_stride]) * 2
102 
103  + (srcp[ -src_linesize + (i-2)*src_stride] + srcp[ src_linesize + (i-2)*src_stride]) * 4
104  + (srcp[ -src_linesize + (i-1)*src_stride] + srcp[ src_linesize + (i-1)*src_stride]) * 9
105  + (srcp[ -src_linesize + (i )*src_stride] + srcp[ src_linesize + (i )*src_stride]) * 12
106  + (srcp[ -src_linesize + (i+1)*src_stride] + srcp[ src_linesize + (i+1)*src_stride]) * 9
107  + (srcp[ -src_linesize + (i+2)*src_stride] + srcp[ src_linesize + (i+2)*src_stride]) * 4
108 
109  + srcp[(i-2)*src_stride] * 5
110  + srcp[(i-1)*src_stride] * 12
111  + srcp[(i )*src_stride] * 15
112  + srcp[(i+1)*src_stride] * 12
113  + srcp[(i+2)*src_stride] * 5) / 159;
114  }
115  for (; i < w; i++)
116  dstp[i] = srcp[i*src_stride];
117 
118  dstp += dst_linesize;
119  srcp += src_linesize;
120  }
121  for (; j < h; j++) {
122  memcpy(dstp, srcp, w*sizeof(pixel));
123  dstp += dst_linesize;
124  srcp += src_linesize;
125  }
126 }
gaussian_blur
void fn() gaussian_blur(int w, int h, uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int src_stride)
Definition: edge_template.c:73
w
uint8_t w
Definition: llviddspenc.c:38
fn
#define fn(a)
Definition: edge_template.c:40
video.h
get_rounded_direction
static int get_rounded_direction(int gx, int gy)
Definition: edge_common.c:22
avassert.h
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:65
pixel
uint8_t pixel
Definition: tiny_ssim.c:41
internal.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
pixel
#define pixel
Definition: edge_template.c:32
avfilter.h
sobel
void fn() sobel(int w, int h, uint16_t *dst, int dst_linesize, int8_t *dir, int dir_linesize, const uint8_t *src, int src_linesize, int src_stride)
Definition: edge_template.c:42
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
h
h
Definition: vp9dsp_template.c:2038