57 #define OFFSET(x) offsetof(BilateralContext, x)
58 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
98 float inv_sigma_range;
100 inv_sigma_range = 1.0f / (
s->sigmaR * ((1 <<
s->depth) - 1));
101 s->alpha =
expf(-sqrtf(2.f) /
s->sigmaS);
104 for (
int i = 0;
i < (1 <<
s->depth);
i++)
105 s->range_table[
i] =
s->alpha *
expf(-
i * inv_sigma_range);
116 s->depth =
desc->comp[0].depth;
121 s->planewidth[0] =
s->planewidth[3] = inlink->
w;
123 s->planeheight[0] =
s->planeheight[3] = inlink->
h;
127 s->img_out_f =
av_calloc(inlink->
w * inlink->
h,
sizeof(
float));
128 s->img_temp =
av_calloc(inlink->
w * inlink->
h,
sizeof(
float));
129 s->map_factor_a =
av_calloc(inlink->
w * inlink->
h,
sizeof(
float));
130 s->map_factor_b =
av_calloc(inlink->
w * inlink->
h,
sizeof(
float));
131 s->slice_factor_a =
av_calloc(inlink->
w,
sizeof(
float));
132 s->slice_factor_b =
av_calloc(inlink->
w,
sizeof(
float));
133 s->line_factor_a =
av_calloc(inlink->
w,
sizeof(
float));
134 s->line_factor_b =
av_calloc(inlink->
w,
sizeof(
float));
140 !
s->slice_factor_a ||
141 !
s->slice_factor_a ||
149 #define BILATERAL(type, name) \
150 static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *ddst, \
151 float sigma_spatial, float sigma_range, \
152 int width, int height, int src_linesize, int dst_linesize) \
154 type *dst = (type *)ddst; \
155 const type *src = (const type *)ssrc; \
156 float *img_out_f = s->img_out_f, *img_temp = s->img_temp; \
157 float *map_factor_a = s->map_factor_a, *map_factor_b = s->map_factor_b; \
158 float *slice_factor_a = s->slice_factor_a, *slice_factor_b = s->slice_factor_b; \
159 float *line_factor_a = s->line_factor_a, *line_factor_b = s->line_factor_b; \
160 const float *range_table = s->range_table; \
161 const float alpha = s->alpha; \
162 float ypr, ycr, *ycy, *ypy, *xcy, fp, fc; \
163 const float inv_alpha_ = 1.f - alpha; \
164 float *ycf, *ypf, *xcf, *in_factor; \
165 const type *tcy, *tpy; \
168 for (int y = 0; y < height; y++) { \
169 float *temp_factor_x, *temp_x = &img_temp[y * width]; \
170 const type *in_x = &src[y * src_linesize]; \
171 const type *texture_x = &src[y * src_linesize]; \
174 *temp_x++ = ypr = *in_x++; \
175 tpr = *texture_x++; \
177 temp_factor_x = &map_factor_a[y * width]; \
178 *temp_factor_x++ = fp = 1; \
180 for (int x = 1; x < width; x++) { \
183 type tcr = *texture_x++; \
184 type dr = abs(tcr - tpr); \
187 alpha_ = range_table[range_dist]; \
188 *temp_x++ = ycr = inv_alpha_*(*in_x++) + alpha_*ypr; \
191 *temp_factor_x++ = fc = inv_alpha_ + alpha_ * fp; \
194 --temp_x; *temp_x = 0.5f*((*temp_x) + (*--in_x)); \
195 tpr = *--texture_x; \
198 --temp_factor_x; *temp_factor_x = 0.5f*((*temp_factor_x) + 1); \
201 for (int x = width - 2; x >= 0; x--) { \
202 type tcr = *--texture_x; \
203 type dr = abs(tcr - tpr); \
204 int range_dist = dr; \
205 float alpha_ = range_table[range_dist]; \
207 ycr = inv_alpha_ * (*--in_x) + alpha_ * ypr; \
208 --temp_x; *temp_x = 0.5f*((*temp_x) + ycr); \
212 fc = inv_alpha_ + alpha_*fp; \
214 *temp_factor_x = 0.5f*((*temp_factor_x) + fc); \
218 memcpy(img_out_f, img_temp, sizeof(float) * width); \
220 in_factor = map_factor_a; \
221 memcpy(map_factor_b, in_factor, sizeof(float) * width); \
222 for (int y = 1; y < height; y++) { \
223 tpy = &src[(y - 1) * src_linesize]; \
224 tcy = &src[y * src_linesize]; \
225 xcy = &img_temp[y * width]; \
226 ypy = &img_out_f[(y - 1) * width]; \
227 ycy = &img_out_f[y * width]; \
229 xcf = &in_factor[y * width]; \
230 ypf = &map_factor_b[(y - 1) * width]; \
231 ycf = &map_factor_b[y * width]; \
232 for (int x = 0; x < width; x++) { \
233 type dr = abs((*tcy++) - (*tpy++)); \
234 int range_dist = dr; \
235 float alpha_ = range_table[range_dist]; \
237 *ycy++ = inv_alpha_*(*xcy++) + alpha_*(*ypy++); \
238 *ycf++ = inv_alpha_*(*xcf++) + alpha_*(*ypf++); \
242 ycf = line_factor_a; \
243 ypf = line_factor_b; \
244 memcpy(ypf, &in_factor[h1 * width], sizeof(float) * width); \
245 for (int x = 0; x < width; x++) \
246 map_factor_b[h1 * width + x] = 0.5f*(map_factor_b[h1 * width + x] + ypf[x]); \
248 ycy = slice_factor_a; \
249 ypy = slice_factor_b; \
250 memcpy(ypy, &img_temp[h1 * width], sizeof(float) * width); \
251 for (int x = 0, k = 0; x < width; x++) { \
252 int idx = h1 * width + x; \
253 img_out_f[idx] = 0.5f*(img_out_f[idx] + ypy[k++]) / map_factor_b[h1 * width + x]; \
256 for (int y = h1 - 1; y >= 0; y--) { \
257 float *ycf_, *ypf_, *factor_; \
258 float *ycy_, *ypy_, *out_; \
260 tpy = &src[(y + 1) * src_linesize]; \
261 tcy = &src[y * src_linesize]; \
262 xcy = &img_temp[y * width]; \
265 out_ = &img_out_f[y * width]; \
267 xcf = &in_factor[y * width]; \
270 factor_ = &map_factor_b[y * width]; \
271 for (int x = 0; x < width; x++) { \
272 type dr = abs((*tcy++) - (*tpy++)); \
273 int range_dist = dr; \
274 float alpha_ = range_table[range_dist]; \
275 float ycc, fcc = inv_alpha_*(*xcf++) + alpha_*(*ypf_++); \
278 *factor_ = 0.5f * (*factor_ + fcc); \
280 ycc = inv_alpha_*(*xcy++) + alpha_*(*ypy_++); \
282 *out_ = 0.5f * (*out_ + ycc) / (*factor_); \
291 for (int i = 0; i < height; i++) \
292 for (int j = 0; j < width; j++) \
293 dst[j + i * dst_linesize] = img_out_f[i * width + j]; \
313 for (
int plane = 0; plane <
s->nb_planes; plane++) {
314 if (!(
s->planes & (1 << plane))) {
316 in->data[plane],
in->linesize[plane],
317 s->planewidth[plane] * ((
s->depth + 7) / 8),
s->planeheight[plane]);
322 bilateral_byte(
s,
in->data[plane],
out->data[plane],
s->sigmaS,
s->sigmaR,
323 s->planewidth[plane],
s->planeheight[plane],
324 in->linesize[plane],
out->linesize[plane]);
326 bilateral_word(
s,
in->data[plane],
out->data[plane],
s->sigmaS,
s->sigmaR,
327 s->planewidth[plane],
s->planeheight[plane],
328 in->linesize[plane] / 2,
out->linesize[plane] / 2);
386 .priv_class = &bilateral_class,
static const AVFilterPad inputs[]
static const AVFilterPad outputs[]
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
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Main libavfilter public API header.
#define flags(name, subs,...)
#define AV_CEIL_RSHIFT(a, b)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static enum AVPixelFormat pix_fmts[]
static const struct @322 planes[]
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
#define AV_PIX_FMT_GBRAP12
#define AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P9
#define AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV440P12
#define AV_PIX_FMT_GBRAP16
#define AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_YUVA444P10
#define AV_PIX_FMT_YUVA420P16
#define AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUVA422P9
#define AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_GRAY12
#define AV_PIX_FMT_GBRP12
#define AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUVA420P9
#define AV_PIX_FMT_YUVA422P10
#define AV_PIX_FMT_YUV420P14
AVPixelFormat
Pixel format.
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
#define AV_PIX_FMT_YUV422P14
#define AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GRAY14
#define AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GBRAP10
#define AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_GBRP16
#define AV_PIX_FMT_YUV444P14
#define AV_PIX_FMT_YUVA444P9
#define AV_PIX_FMT_GBRP14
#define AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P10
Describe the class of an AVClass context structure.
A link between two filters.
int w
agreed upon image width
int h
agreed upon image height
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
const char * name
Pad name.
const char * name
Filter name.
This structure describes decoded (raw) audio or video data.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
static int process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
static int query_formats(AVFilterContext *ctx)
static const AVFilterPad bilateral_outputs[]
static int config_input(AVFilterLink *inlink)
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static const AVOption bilateral_options[]
#define BILATERAL(type, name)
AVFILTER_DEFINE_CLASS(bilateral)
static av_cold void uninit(AVFilterContext *ctx)
static int config_params(AVFilterContext *ctx)
static const AVFilterPad bilateral_inputs[]
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.