FFmpeg  4.4.9
vf_threshold_init.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Paul B Mahol
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  */
20 
21 #include <stdint.h>
22 
23 #include "libavutil/attributes.h"
24 #include "libavutil/cpu.h"
25 #include "libavutil/x86/cpu.h"
26 #include "libavfilter/threshold.h"
27 
28 #define THRESHOLD_FUNC(depth, opt) \
29 void ff_threshold##depth##_##opt(const uint8_t *in, const uint8_t *threshold,\
30  const uint8_t *min, const uint8_t *max, \
31  uint8_t *out, \
32  ptrdiff_t ilinesize, ptrdiff_t tlinesize, \
33  ptrdiff_t flinesize, ptrdiff_t slinesize, \
34  ptrdiff_t olinesize, \
35  int w, int h);
36 
37 THRESHOLD_FUNC(8, sse4)
38 THRESHOLD_FUNC(8, avx2)
39 THRESHOLD_FUNC(16, sse4)
40 THRESHOLD_FUNC(16, avx2)
41 
42 static void threshold8_tail(const uint8_t *in, const uint8_t *threshold,
43  const uint8_t *min, const uint8_t *max,
44  uint8_t *out,
45  ptrdiff_t ilinesize, ptrdiff_t tlinesize,
46  ptrdiff_t flinesize, ptrdiff_t slinesize,
47  ptrdiff_t olinesize,
48  int start, int end, int h)
49 {
50  for (int y = 0; y < h; y++) {
51  for (int x = start; x < end; x++)
52  out[x] = in[x] < threshold[x] ? min[x] : max[x];
53  in += ilinesize;
54  threshold += tlinesize;
55  min += flinesize;
56  max += slinesize;
57  out += olinesize;
58  }
59 }
60 
61 static void threshold16_tail(const uint8_t *iin, const uint8_t *tthreshold,
62  const uint8_t *ffirst, const uint8_t *ssecond,
63  uint8_t *oout,
64  ptrdiff_t ilinesize, ptrdiff_t tlinesize,
65  ptrdiff_t flinesize, ptrdiff_t slinesize,
66  ptrdiff_t olinesize,
67  int start, int end, int h)
68 {
69  const uint16_t *in = (const uint16_t *)iin;
70  const uint16_t *threshold = (const uint16_t *)tthreshold;
71  const uint16_t *min = (const uint16_t *)ffirst;
72  const uint16_t *max = (const uint16_t *)ssecond;
73  uint16_t *out = (uint16_t *)oout;
74 
75  for (int y = 0; y < h; y++) {
76  for (int x = start; x < end; x++)
77  out[x] = in[x] < threshold[x] ? min[x] : max[x];
78  in += ilinesize / 2;
79  threshold += tlinesize / 2;
80  min += flinesize / 2;
81  max += slinesize / 2;
82  out += olinesize / 2;
83  }
84 }
85 
86 #define THRESHOLD_X86(depth, opt, vec_elements) \
87 static void threshold##depth##_##opt##_x86(const uint8_t *in, const uint8_t *threshold, \
88  const uint8_t *min, const uint8_t *max, \
89  uint8_t *out, \
90  ptrdiff_t ilinesize, ptrdiff_t tlinesize, \
91  ptrdiff_t flinesize, ptrdiff_t slinesize, \
92  ptrdiff_t olinesize, \
93  int w, int h) \
94 { \
95  int aligned_w = w & ~((vec_elements) - 1); \
96  if (aligned_w > 0) \
97  ff_threshold##depth##_##opt(in, threshold, min, max, out, \
98  ilinesize, tlinesize, flinesize, slinesize, olinesize, \
99  aligned_w, h); \
100  if (aligned_w < w) \
101  threshold##depth##_tail(in, threshold, min, max, out, \
102  ilinesize, tlinesize, flinesize, slinesize, olinesize, \
103  aligned_w, w, h); \
104 }
105 
106 THRESHOLD_X86(8, sse4, 16)
107 THRESHOLD_X86(8, avx2, 32)
108 THRESHOLD_X86(16, sse4, 8)
109 THRESHOLD_X86(16, avx2, 16)
110 
112 {
113  int cpu_flags = av_get_cpu_flags();
114 
115  if (s->depth == 8) {
116  if (EXTERNAL_SSE4(cpu_flags)) {
117  s->threshold = threshold8_sse4_x86;
118  }
120  s->threshold = threshold8_avx2_x86;
121  }
122  } else if (s->depth == 16) {
123  if (EXTERNAL_SSE4(cpu_flags)) {
124  s->threshold = threshold16_sse4_x86;
125  }
127  s->threshold = threshold16_avx2_x86;
128  }
129  }
130 }
Macro definitions for various function/variable attributes.
#define av_cold
Definition: attributes.h:88
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
uint8_t
#define s(width, name)
Definition: cbs_vp9.c:257
static atomic_int cpu_flags
Definition: cpu.c:50
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:95
#define max(a, b)
Definition: cuda_runtime.h:33
FILE * out
Definition: movenc.c:54
#define THRESHOLD_FUNC(depth, opt)
#define THRESHOLD_X86(depth, opt, vec_elements)
static void threshold16_tail(const uint8_t *iin, const uint8_t *tthreshold, const uint8_t *ffirst, const uint8_t *ssecond, uint8_t *oout, ptrdiff_t ilinesize, ptrdiff_t tlinesize, ptrdiff_t flinesize, ptrdiff_t slinesize, ptrdiff_t olinesize, int start, int end, int h)
static void threshold8_tail(const uint8_t *in, const uint8_t *threshold, const uint8_t *min, const uint8_t *max, uint8_t *out, ptrdiff_t ilinesize, ptrdiff_t tlinesize, ptrdiff_t flinesize, ptrdiff_t slinesize, ptrdiff_t olinesize, int start, int end, int h)
av_cold void ff_threshold_init_x86(ThresholdContext *s)
float min
#define EXTERNAL_AVX2_FAST(flags)
Definition: cpu.h:79
#define EXTERNAL_SSE4(flags)
Definition: cpu.h:68