| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | #ifndef AVFILTER_COLORDETECTDSP_H | ||
| 20 | #define AVFILTER_COLORDETECTDSP_H | ||
| 21 | |||
| 22 | #include <stddef.h> | ||
| 23 | #include <stdint.h> | ||
| 24 | |||
| 25 | #include "config.h" | ||
| 26 | |||
| 27 | #include "libavutil/attributes.h" | ||
| 28 | #include "libavutil/avassert.h" | ||
| 29 | #include "libavutil/pixfmt.h" | ||
| 30 | |||
| 31 | enum FFAlphaDetect { | ||
| 32 | FF_ALPHA_NONE = -1, | ||
| 33 | FF_ALPHA_UNDETERMINED = 0, | ||
| 34 | FF_ALPHA_TRANSPARENT = 1 << 0, ///< alpha < alpha_max | ||
| 35 | FF_ALPHA_STRAIGHT = (1 << 1) | FF_ALPHA_TRANSPARENT, ///< alpha < pixel | ||
| 36 | /* No way to positively identify premultiplied alpha */ | ||
| 37 | }; | ||
| 38 | |||
| 39 | typedef struct FFColorDetectDSPContext { | ||
| 40 | /* Returns 1 if an out-of-range value was detected, 0 otherwise */ | ||
| 41 | int (*detect_range)(const uint8_t *data, ptrdiff_t stride, | ||
| 42 | ptrdiff_t width, ptrdiff_t height, | ||
| 43 | int mpeg_min, int mpeg_max); | ||
| 44 | |||
| 45 | /* Returns an FFAlphaDetect enum value */ | ||
| 46 | int (*detect_alpha)(const uint8_t *color, ptrdiff_t color_stride, | ||
| 47 | const uint8_t *alpha, ptrdiff_t alpha_stride, | ||
| 48 | ptrdiff_t width, ptrdiff_t height, | ||
| 49 | int alpha_max, int mpeg_range, int offset); | ||
| 50 | } FFColorDetectDSPContext; | ||
| 51 | |||
| 52 | void ff_color_detect_dsp_init_aarch64(FFColorDetectDSPContext *dsp, int depth, | ||
| 53 | enum AVColorRange color_range); | ||
| 54 | void ff_color_detect_dsp_init_x86(FFColorDetectDSPContext *dsp, int depth, | ||
| 55 | int offset, enum AVColorRange color_range); | ||
| 56 | |||
| 57 | #define DECL_DETECT_RANGE_IMPL(TYPE, NAME) \ | ||
| 58 | static inline int NAME(const uint8_t* data, ptrdiff_t stride, \ | ||
| 59 | ptrdiff_t width, ptrdiff_t height, \ | ||
| 60 | int mpeg_min, int mpeg_max) \ | ||
| 61 | { \ | ||
| 62 | const TYPE min = mpeg_min; \ | ||
| 63 | const TYPE max = mpeg_max; \ | ||
| 64 | av_assume(min == mpeg_min); \ | ||
| 65 | av_assume(max == mpeg_max); \ | ||
| 66 | \ | ||
| 67 | while (height--) { \ | ||
| 68 | const TYPE *row = (const TYPE *) data; \ | ||
| 69 | uint8_t cond = 0; \ | ||
| 70 | for (int x = 0; x < width; x++) { \ | ||
| 71 | const TYPE val = row[x]; \ | ||
| 72 | cond |= val < min || val > max; \ | ||
| 73 | } \ | ||
| 74 | if (cond) \ | ||
| 75 | return 1; \ | ||
| 76 | data += stride; \ | ||
| 77 | } \ | ||
| 78 | \ | ||
| 79 | return 0; \ | ||
| 80 | } | ||
| 81 | |||
| 82 |
11/14✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 49 times.
✓ Branch 6 taken 212297 times.
✓ Branch 7 taken 15 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 212297 times.
✓ Branch 10 taken 212312 times.
✓ Branch 11 taken 394 times.
✓ Branch 12 taken 15 times.
✓ Branch 13 taken 379 times.
✓ Branch 14 taken 394 times.
✓ Branch 15 taken 34 times.
|
212740 | DECL_DETECT_RANGE_IMPL(uint8_t, ff_detect_range_c) |
| 83 |
11/14✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 49 times.
✓ Branch 6 taken 106141 times.
✓ Branch 7 taken 15 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 106141 times.
✓ Branch 10 taken 106156 times.
✓ Branch 11 taken 394 times.
✓ Branch 12 taken 15 times.
✓ Branch 13 taken 379 times.
✓ Branch 14 taken 394 times.
✓ Branch 15 taken 34 times.
|
106584 | DECL_DETECT_RANGE_IMPL(uint16_t, ff_detect_range16_c) |
| 84 | |||
| 85 | #define DECL_DETECT_ALPHA_FULL_IMPL(TYPE, INTER, NAME) \ | ||
| 86 | static inline int NAME(const uint8_t* color, ptrdiff_t color_stride, \ | ||
| 87 | const uint8_t* alpha, ptrdiff_t alpha_stride, \ | ||
| 88 | ptrdiff_t width, ptrdiff_t height, int alpha_max, \ | ||
| 89 | int mpeg_range, int offset) \ | ||
| 90 | { \ | ||
| 91 | const TYPE max = alpha_max; \ | ||
| 92 | const INTER off = offset; \ | ||
| 93 | av_assume(max == alpha_max); \ | ||
| 94 | av_assume(off == offset); \ | ||
| 95 | (void) mpeg_range; \ | ||
| 96 | \ | ||
| 97 | uint8_t transparent = 0; \ | ||
| 98 | while (height--) { \ | ||
| 99 | const TYPE *col = (const TYPE *) color; \ | ||
| 100 | const TYPE *alp = (const TYPE *) alpha; \ | ||
| 101 | uint8_t straight = 0; \ | ||
| 102 | for (int x = 0; x < width; x++) { \ | ||
| 103 | straight |= col[x] > alp[x] + off; \ | ||
| 104 | transparent |= alp[x] != max; \ | ||
| 105 | } \ | ||
| 106 | if (straight) \ | ||
| 107 | return FF_ALPHA_STRAIGHT; \ | ||
| 108 | color += color_stride; \ | ||
| 109 | alpha += alpha_stride; \ | ||
| 110 | } \ | ||
| 111 | return transparent ? FF_ALPHA_TRANSPARENT : 0; \ | ||
| 112 | } | ||
| 113 | |||
| 114 |
8/10✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 104 times.
✓ Branch 6 taken 411664 times.
✓ Branch 7 taken 764 times.
✓ Branch 8 taken 48 times.
✓ Branch 9 taken 716 times.
✓ Branch 10 taken 764 times.
✓ Branch 11 taken 56 times.
|
412484 | DECL_DETECT_ALPHA_FULL_IMPL(uint8_t, uint16_t, ff_detect_alpha_full_c) |
| 115 |
8/10✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 104 times.
✓ Branch 6 taken 230132 times.
✓ Branch 7 taken 854 times.
✓ Branch 8 taken 36 times.
✓ Branch 9 taken 818 times.
✓ Branch 10 taken 854 times.
✓ Branch 11 taken 68 times.
|
231054 | DECL_DETECT_ALPHA_FULL_IMPL(uint16_t, uint32_t, ff_detect_alpha16_full_c) |
| 116 | |||
| 117 | #define DECL_DETECT_ALPHA_LIMITED_IMPL(TYPE, INTER, NAME) \ | ||
| 118 | static inline int NAME(const uint8_t* color, ptrdiff_t color_stride, \ | ||
| 119 | const uint8_t* alpha, ptrdiff_t alpha_stride, \ | ||
| 120 | ptrdiff_t width, ptrdiff_t height, int alpha_max, \ | ||
| 121 | int mpeg_range, int offset) \ | ||
| 122 | { \ | ||
| 123 | const TYPE max = alpha_max; \ | ||
| 124 | const INTER off = offset; \ | ||
| 125 | const INTER range = mpeg_range; \ | ||
| 126 | av_assume(max == alpha_max); \ | ||
| 127 | av_assume(off == offset); \ | ||
| 128 | av_assume(range == mpeg_range); \ | ||
| 129 | \ | ||
| 130 | uint8_t transparent = 0; \ | ||
| 131 | while (height--) { \ | ||
| 132 | const TYPE *col = (const TYPE *) color; \ | ||
| 133 | const TYPE *alp = (const TYPE *) alpha; \ | ||
| 134 | uint8_t straight = 0; \ | ||
| 135 | for (int x = 0; x < width; x++) { \ | ||
| 136 | straight |= (INTER) max * col[x] - off > range * alp[x]; \ | ||
| 137 | transparent |= alp[x] != max; \ | ||
| 138 | } \ | ||
| 139 | if (straight) \ | ||
| 140 | return FF_ALPHA_STRAIGHT; \ | ||
| 141 | color += color_stride; \ | ||
| 142 | alpha += alpha_stride; \ | ||
| 143 | } \ | ||
| 144 | return transparent ? FF_ALPHA_TRANSPARENT : 0; \ | ||
| 145 | } | ||
| 146 | |||
| 147 |
9/12✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 52 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 52 times.
✓ Branch 9 taken 60032 times.
✓ Branch 10 taken 112 times.
✓ Branch 11 taken 48 times.
✓ Branch 12 taken 64 times.
✓ Branch 13 taken 112 times.
✓ Branch 14 taken 4 times.
|
60148 | DECL_DETECT_ALPHA_LIMITED_IMPL(uint8_t, int32_t, ff_detect_alpha_limited_c) |
| 148 |
9/12✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 52 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 52 times.
✓ Branch 9 taken 30016 times.
✓ Branch 10 taken 112 times.
✓ Branch 11 taken 48 times.
✓ Branch 12 taken 64 times.
✓ Branch 13 taken 112 times.
✓ Branch 14 taken 4 times.
|
30132 | DECL_DETECT_ALPHA_LIMITED_IMPL(uint16_t, int64_t, ff_detect_alpha16_limited_c) |
| 149 | |||
| 150 | static av_cold inline void | ||
| 151 | 112 | ff_color_detect_dsp_init(FFColorDetectDSPContext *dsp, int depth, int offset, | |
| 152 | enum AVColorRange color_range) | ||
| 153 | { | ||
| 154 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 56 times.
|
112 | dsp->detect_range = depth > 8 ? ff_detect_range16_c : ff_detect_range_c; |
| 155 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 56 times.
|
112 | if (color_range == AVCOL_RANGE_JPEG) { |
| 156 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
|
56 | dsp->detect_alpha = depth > 8 ? ff_detect_alpha16_full_c : ff_detect_alpha_full_c; |
| 157 | } else { | ||
| 158 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
|
56 | dsp->detect_alpha = depth > 8 ? ff_detect_alpha16_limited_c : ff_detect_alpha_limited_c; |
| 159 | } | ||
| 160 | |||
| 161 | #if ARCH_AARCH64 | ||
| 162 | ff_color_detect_dsp_init_aarch64(dsp, depth, color_range); | ||
| 163 | #elif ARCH_X86 && HAVE_X86ASM | ||
| 164 | 112 | ff_color_detect_dsp_init_x86(dsp, depth, offset, color_range); | |
| 165 | #endif | ||
| 166 | 112 | } | |
| 167 | |||
| 168 | #endif /* AVFILTER_COLORDETECTDSP_H */ | ||
| 169 |