| Line | Branch | Exec | Source |
|---|---|---|---|
| 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" | ||
| 25 | #include "libavutil/intreadwrite.h" | ||
| 26 | |||
| 27 | enum MatrixMode { | ||
| 28 | MATRIX_SQUARE, | ||
| 29 | MATRIX_ROW, | ||
| 30 | MATRIX_COLUMN, | ||
| 31 | MATRIX_NBMODES, | ||
| 32 | }; | ||
| 33 | |||
| 34 | typedef 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]; | ||
| 41 | float scale; | ||
| 42 | float delta; | ||
| 43 | int planes; | ||
| 44 | |||
| 45 | float rdiv[4]; | ||
| 46 | int size[4]; | ||
| 47 | int depth; | ||
| 48 | int max; | ||
| 49 | int bpc; | ||
| 50 | int nb_planes; | ||
| 51 | int nb_threads; | ||
| 52 | int planewidth[4]; | ||
| 53 | int planeheight[4]; | ||
| 54 | int matrix[4][49]; | ||
| 55 | int matrix_length[4]; | ||
| 56 | int copy[4]; | ||
| 57 | |||
| 58 | void (*setup[4])(int radius, const uint8_t *c[], const uint8_t *src, int stride, | ||
| 59 | int x, int width, int y, int height, int bpc); | ||
| 60 | void (*filter[4])(uint8_t *dst, int width, | ||
| 61 | float rdiv, float bias, const int *const matrix, | ||
| 62 | const uint8_t *c[], int peak, int radius, | ||
| 63 | int dstride, int stride, int size); | ||
| 64 | } ConvolutionContext; | ||
| 65 | |||
| 66 | void ff_convolution_init_x86(ConvolutionContext *s); | ||
| 67 | void ff_sobel_init_x86(ConvolutionContext *s, int depth, int nb_planes); | ||
| 68 | |||
| 69 | 512 | static void setup_3x3(int radius, const uint8_t *c[], const uint8_t *src, int stride, | |
| 70 | int x, int w, int y, int h, int bpc) | ||
| 71 | { | ||
| 72 | int i; | ||
| 73 | |||
| 74 |
2/2✓ Branch 0 taken 4608 times.
✓ Branch 1 taken 512 times.
|
5120 | for (i = 0; i < 9; i++) { |
| 75 | 4608 | int xoff = avpriv_mirror(x + (i % 3) - 1, w - 1); | |
| 76 | 4608 | int yoff = avpriv_mirror(y + (i / 3) - 1, h - 1); | |
| 77 | |||
| 78 | 4608 | c[i] = src + xoff * bpc + yoff * stride; | |
| 79 | } | ||
| 80 | 512 | } | |
| 81 | |||
| 82 | 1024 | static void filter_sobel(uint8_t *dst, int width, | |
| 83 | float scale, float delta, const int *const matrix, | ||
| 84 | const uint8_t *c[], int peak, int radius, | ||
| 85 | int dstride, int stride, int size) | ||
| 86 | { | ||
| 87 | 1024 | const uint8_t *c0 = c[0], *c1 = c[1], *c2 = c[2]; | |
| 88 | 1024 | const uint8_t *c3 = c[3], *c5 = c[5]; | |
| 89 | 1024 | const uint8_t *c6 = c[6], *c7 = c[7], *c8 = c[8]; | |
| 90 | int x; | ||
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 522240 times.
✓ Branch 1 taken 1024 times.
|
523264 | for (x = 0; x < width; x++) { |
| 93 | 522240 | float suma = c0[x] * -1 + c1[x] * -2 + c2[x] * -1 + | |
| 94 | 522240 | c6[x] * 1 + c7[x] * 2 + c8[x] * 1; | |
| 95 | 522240 | float sumb = c0[x] * -1 + c2[x] * 1 + c3[x] * -2 + | |
| 96 | 522240 | c5[x] * 2 + c6[x] * -1 + c8[x] * 1; | |
| 97 | |||
| 98 | 522240 | dst[x] = av_clip_uint8(sqrtf(suma*suma + sumb*sumb) * scale + delta); | |
| 99 | } | ||
| 100 | 1024 | } | |
| 101 | |||
| 102 | ✗ | static void filter16_sobel(uint8_t *dstp, int width, | |
| 103 | float scale, float delta, const int *const matrix, | ||
| 104 | const uint8_t *c[], int peak, int radius, | ||
| 105 | int dstride, int stride, int size) | ||
| 106 | { | ||
| 107 | ✗ | uint16_t *dst = (uint16_t *)dstp; | |
| 108 | int x; | ||
| 109 | |||
| 110 | ✗ | for (x = 0; x < width; x++) { | |
| 111 | ✗ | float suma = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[1][2 * x]) * -2 + AV_RN16A(&c[2][2 * x]) * -1 + | |
| 112 | ✗ | AV_RN16A(&c[6][2 * x]) * 1 + AV_RN16A(&c[7][2 * x]) * 2 + AV_RN16A(&c[8][2 * x]) * 1; | |
| 113 | ✗ | float sumb = AV_RN16A(&c[0][2 * x]) * -1 + AV_RN16A(&c[2][2 * x]) * 1 + AV_RN16A(&c[3][2 * x]) * -2 + | |
| 114 | ✗ | AV_RN16A(&c[5][2 * x]) * 2 + AV_RN16A(&c[6][2 * x]) * -1 + AV_RN16A(&c[8][2 * x]) * 1; | |
| 115 | |||
| 116 | ✗ | dst[x] = av_clip(sqrtf(suma*suma + sumb*sumb) * scale + delta, 0, peak); | |
| 117 | } | ||
| 118 | ✗ | } | |
| 119 | |||
| 120 | 14 | static inline void ff_sobel_init(ConvolutionContext *s, int depth, int nb_planes) | |
| 121 | { | ||
| 122 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 14 times.
|
70 | for (int i = 0; i < 4; i++) { |
| 123 | 56 | s->filter[i] = filter_sobel; | |
| 124 | 56 | s->copy[i] = !((1 << i) & s->planes); | |
| 125 | 56 | s->size[i] = 3; | |
| 126 | 56 | s->setup[i] = setup_3x3; | |
| 127 | 56 | s->rdiv[i] = s->scale; | |
| 128 | 56 | s->bias[i] = s->delta; | |
| 129 | } | ||
| 130 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (s->depth > 8) |
| 131 | ✗ | for (int i = 0; i < 4; i++) | |
| 132 | ✗ | s->filter[i] = filter16_sobel; | |
| 133 | #if ARCH_X86_64 && HAVE_X86ASM | ||
| 134 | 14 | ff_sobel_init_x86(s, depth, nb_planes); | |
| 135 | #endif | ||
| 136 | 14 | } | |
| 137 | #endif | ||
| 138 |