FFmpeg
edge_common.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "edge_common.h"
20 
21 // Internal helper for ff_sobel()
22 static int get_rounded_direction(int gx, int gy)
23 {
24  /* reference angles:
25  * tan( pi/8) = sqrt(2)-1
26  * tan(3pi/8) = sqrt(2)+1
27  * Gy/Gx is the tangent of the angle (theta), so Gy/Gx is compared against
28  * <ref-angle>, or more simply Gy against <ref-angle>*Gx
29  *
30  * Gx and Gy bounds = [-1020;1020], using 16-bit arithmetic:
31  * round((sqrt(2)-1) * (1<<16)) = 27146
32  * round((sqrt(2)+1) * (1<<16)) = 158218
33  */
34  if (gx) {
35  int tanpi8gx, tan3pi8gx;
36 
37  if (gx < 0)
38  gx = -gx, gy = -gy;
39  gy *= (1 << 16);
40  tanpi8gx = 27146 * gx;
41  tan3pi8gx = 158218 * gx;
42  if (gy > -tan3pi8gx && gy < -tanpi8gx) return DIRECTION_45UP;
43  if (gy > -tanpi8gx && gy < tanpi8gx) return DIRECTION_HORIZONTAL;
44  if (gy > tanpi8gx && gy < tan3pi8gx) return DIRECTION_45DOWN;
45  }
46  return DIRECTION_VERTICAL;
47 }
48 
49 #undef DEPTH
50 #define DEPTH 8
51 #include "edge_template.c"
52 
53 #undef DEPTH
54 #define DEPTH 16
55 #include "edge_template.c"
56 
57 // Filters rounded gradients to drop all non-maxima
58 // Expects gradients generated by ff_sobel()
59 // Expects zero's destination buffer
61  uint8_t *dst, int dst_linesize,
62  const int8_t *dir, int dir_linesize,
63  const uint16_t *src, int src_linesize)
64 {
65  int i, j;
66 
67 #define COPY_MAXIMA(ay, ax, by, bx) do { \
68  if (src[i] > src[(ay)*src_linesize + i+(ax)] && \
69  src[i] > src[(by)*src_linesize + i+(bx)]) \
70  dst[i] = av_clip_uint8(src[i]); \
71 } while (0)
72 
73  for (j = 1; j < h - 1; j++) {
74  dst += dst_linesize;
75  dir += dir_linesize;
76  src += src_linesize;
77  for (i = 1; i < w - 1; i++) {
78  switch (dir[i]) {
79  case DIRECTION_45UP: COPY_MAXIMA( 1, -1, -1, 1); break;
80  case DIRECTION_45DOWN: COPY_MAXIMA(-1, -1, 1, 1); break;
81  case DIRECTION_HORIZONTAL: COPY_MAXIMA( 0, -1, 0, 1); break;
82  case DIRECTION_VERTICAL: COPY_MAXIMA(-1, 0, 1, 0); break;
83  }
84  }
85  }
86 }
87 
88 // Filter to keep all pixels > high, and keep all pixels > low where all surrounding pixels > high
89 void ff_double_threshold(int low, int high, int w, int h,
90  uint8_t *dst, int dst_linesize,
91  const uint8_t *src, int src_linesize)
92 {
93  int i, j;
94 
95  for (j = 0; j < h; j++) {
96  for (i = 0; i < w; i++) {
97  if (src[i] > high) {
98  dst[i] = src[i];
99  continue;
100  }
101 
102  if (!(!i || i == w - 1 || !j || j == h - 1) &&
103  src[i] > low &&
104  (src[-src_linesize + i-1] > high ||
105  src[-src_linesize + i ] > high ||
106  src[-src_linesize + i+1] > high ||
107  src[ i-1] > high ||
108  src[ i+1] > high ||
109  src[ src_linesize + i-1] > high ||
110  src[ src_linesize + i ] > high ||
111  src[ src_linesize + i+1] > high))
112  dst[i] = src[i];
113  else
114  dst[i] = 0;
115  }
116  dst += dst_linesize;
117  src += src_linesize;
118  }
119 }
w
uint8_t w
Definition: llviddspenc.c:38
edge_common.h
DIRECTION_VERTICAL
@ DIRECTION_VERTICAL
Definition: edge_common.h:36
edge_template.c
get_rounded_direction
static int get_rounded_direction(int gx, int gy)
Definition: edge_common.c:22
DIRECTION_45DOWN
@ DIRECTION_45DOWN
Definition: edge_common.h:34
COPY_MAXIMA
#define COPY_MAXIMA(ay, ax, by, bx)
DIRECTION_HORIZONTAL
@ DIRECTION_HORIZONTAL
Definition: edge_common.h:35
ff_non_maximum_suppression
void ff_non_maximum_suppression(int w, int h, uint8_t *dst, int dst_linesize, const int8_t *dir, int dir_linesize, const uint16_t *src, int src_linesize)
Filters rounded gradients to drop all non-maxima pixels in the magnitude image Expects gradients gene...
Definition: edge_common.c:60
ff_double_threshold
void ff_double_threshold(int low, int high, int w, int h, uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize)
Filters all pixels in src to keep all pixels > high, and keep all pixels > low where all surrounding ...
Definition: edge_common.c:89
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
DIRECTION_45UP
@ DIRECTION_45UP
Definition: edge_common.h:33
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
h
h
Definition: vp9dsp_template.c:2038