Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Copyright (c) 2019 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 "config_components.h" |
22 |
|
|
|
23 |
|
|
#include "libavutil/avstring.h" |
24 |
|
|
#include "libavutil/imgutils.h" |
25 |
|
|
#include "libavutil/mem.h" |
26 |
|
|
#include "libavutil/opt.h" |
27 |
|
|
#include "libavutil/pixdesc.h" |
28 |
|
|
#include "libavutil/qsort.h" |
29 |
|
|
|
30 |
|
|
#include "avfilter.h" |
31 |
|
|
#include "filters.h" |
32 |
|
|
#include "framesync.h" |
33 |
|
|
#include "video.h" |
34 |
|
|
|
35 |
|
|
typedef struct XMedianContext { |
36 |
|
|
const AVClass *class; |
37 |
|
|
const AVPixFmtDescriptor *desc; |
38 |
|
|
int nb_inputs; |
39 |
|
|
int nb_frames; |
40 |
|
|
int nb_threads; |
41 |
|
|
int planes; |
42 |
|
|
float percentile; |
43 |
|
|
|
44 |
|
|
int xmedian; |
45 |
|
|
int radius; |
46 |
|
|
int index; |
47 |
|
|
int depth; |
48 |
|
|
int max; |
49 |
|
|
int nb_planes; |
50 |
|
|
int linesizes[4]; |
51 |
|
|
int width[4]; |
52 |
|
|
int height[4]; |
53 |
|
|
|
54 |
|
|
uint8_t **data; |
55 |
|
|
int *linesize; |
56 |
|
|
|
57 |
|
|
AVFrame **frames; |
58 |
|
|
FFFrameSync fs; |
59 |
|
|
|
60 |
|
|
int (*median_frames)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); |
61 |
|
|
} XMedianContext; |
62 |
|
|
|
63 |
|
|
#define OFFSET(x) offsetof(XMedianContext, x) |
64 |
|
|
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
65 |
|
|
#define TFLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM |
66 |
|
|
|
67 |
|
|
static const enum AVPixelFormat pixel_fmts[] = { |
68 |
|
|
AV_PIX_FMT_GRAY8, |
69 |
|
|
AV_PIX_FMT_GRAY9, |
70 |
|
|
AV_PIX_FMT_GRAY10, |
71 |
|
|
AV_PIX_FMT_GRAY12, |
72 |
|
|
AV_PIX_FMT_GRAY14, |
73 |
|
|
AV_PIX_FMT_GRAY16, |
74 |
|
|
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, |
75 |
|
|
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, |
76 |
|
|
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, |
77 |
|
|
AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, |
78 |
|
|
AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, |
79 |
|
|
AV_PIX_FMT_YUVJ411P, |
80 |
|
|
AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, |
81 |
|
|
AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, |
82 |
|
|
AV_PIX_FMT_YUV440P10, |
83 |
|
|
AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, |
84 |
|
|
AV_PIX_FMT_YUV440P12, |
85 |
|
|
AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, |
86 |
|
|
AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, |
87 |
|
|
AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, |
88 |
|
|
AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, |
89 |
|
|
AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P, |
90 |
|
|
AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16, |
91 |
|
|
AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16, |
92 |
|
|
AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16, |
93 |
|
|
AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, |
94 |
|
|
AV_PIX_FMT_NONE |
95 |
|
|
}; |
96 |
|
|
|
97 |
|
✗ |
static av_cold int init(AVFilterContext *ctx) |
98 |
|
|
{ |
99 |
|
✗ |
XMedianContext *s = ctx->priv; |
100 |
|
|
|
101 |
|
✗ |
if (!s->xmedian) |
102 |
|
✗ |
s->nb_inputs = s->radius * 2 + 1; |
103 |
|
|
|
104 |
|
✗ |
if (s->nb_inputs & 1) |
105 |
|
✗ |
s->index = s->radius * 2.f * s->percentile; |
106 |
|
|
else |
107 |
|
✗ |
s->index = av_clip(s->radius * 2.f * s->percentile, 1, s->nb_inputs - 1); |
108 |
|
✗ |
s->frames = av_calloc(s->nb_inputs, sizeof(*s->frames)); |
109 |
|
✗ |
if (!s->frames) |
110 |
|
✗ |
return AVERROR(ENOMEM); |
111 |
|
|
|
112 |
|
✗ |
return 0; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
typedef struct ThreadData { |
116 |
|
|
AVFrame **in, *out; |
117 |
|
|
} ThreadData; |
118 |
|
|
|
119 |
|
✗ |
static int compare8(const void *p1, const void *p2) |
120 |
|
|
{ |
121 |
|
✗ |
int left = *(const uint8_t *)p1; |
122 |
|
✗ |
int right = *(const uint8_t *)p2; |
123 |
|
✗ |
return FFDIFFSIGN(left, right); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
✗ |
static int compare16(const void *p1, const void *p2) |
127 |
|
|
{ |
128 |
|
✗ |
int left = *(const uint16_t *)p1; |
129 |
|
✗ |
int right = *(const uint16_t *)p2; |
130 |
|
✗ |
return FFDIFFSIGN(left, right); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
#define MEDIAN_SLICE(name, type, comparei) \ |
134 |
|
|
static int median_frames ## name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \ |
135 |
|
|
{ \ |
136 |
|
|
XMedianContext *s = ctx->priv; \ |
137 |
|
|
ThreadData *td = arg; \ |
138 |
|
|
AVFrame **in = td->in; \ |
139 |
|
|
AVFrame *out = td->out; \ |
140 |
|
|
const int nb_inputs = s->nb_inputs; \ |
141 |
|
|
uint8_t **srcf = s->data + jobnr * nb_inputs; \ |
142 |
|
|
int *linesize = s->linesize + jobnr * nb_inputs; \ |
143 |
|
|
const int radius = s->radius; \ |
144 |
|
|
const int index = s->index; \ |
145 |
|
|
type values[256]; \ |
146 |
|
|
\ |
147 |
|
|
for (int p = 0; p < s->nb_planes; p++) { \ |
148 |
|
|
const int slice_start = (s->height[p] * jobnr) / nb_jobs; \ |
149 |
|
|
const int slice_end = (s->height[p] * (jobnr+1)) / nb_jobs; \ |
150 |
|
|
const int width = s->width[p]; \ |
151 |
|
|
type *dst = (type *)(out->data[p] + slice_start * out->linesize[p]); \ |
152 |
|
|
ptrdiff_t dst_linesize = out->linesize[p] / sizeof(type); \ |
153 |
|
|
\ |
154 |
|
|
if (!((1 << p) & s->planes)) { \ |
155 |
|
|
av_image_copy_plane((uint8_t *)dst, out->linesize[p], \ |
156 |
|
|
in[radius]->data[p] + slice_start * in[radius]->linesize[p], \ |
157 |
|
|
in[radius]->linesize[p], \ |
158 |
|
|
s->linesizes[p], slice_end - slice_start); \ |
159 |
|
|
continue; \ |
160 |
|
|
} \ |
161 |
|
|
\ |
162 |
|
|
for (int i = 0; i < nb_inputs; i++) \ |
163 |
|
|
linesize[i] = in[i]->linesize[p]; \ |
164 |
|
|
\ |
165 |
|
|
for (int i = 0; i < nb_inputs; i++) \ |
166 |
|
|
srcf[i] = in[i]->data[p] + slice_start * linesize[i]; \ |
167 |
|
|
\ |
168 |
|
|
for (int y = slice_start; y < slice_end; y++) { \ |
169 |
|
|
for (int x = 0; x < width; x++) { \ |
170 |
|
|
for (int i = 0; i < nb_inputs; i++) { \ |
171 |
|
|
const type *src = (const type *)srcf[i]; \ |
172 |
|
|
values[i] = src[x]; \ |
173 |
|
|
} \ |
174 |
|
|
\ |
175 |
|
|
AV_QSORT(values, nb_inputs, type, comparei); \ |
176 |
|
|
if (nb_inputs & 1) \ |
177 |
|
|
dst[x] = values[index]; \ |
178 |
|
|
else \ |
179 |
|
|
dst[x] = (values[index] + values[index - 1]) >> 1; \ |
180 |
|
|
} \ |
181 |
|
|
\ |
182 |
|
|
dst += dst_linesize; \ |
183 |
|
|
for (int i = 0; i < nb_inputs; i++) \ |
184 |
|
|
srcf[i] += linesize[i]; \ |
185 |
|
|
} \ |
186 |
|
|
} \ |
187 |
|
|
\ |
188 |
|
|
return 0; \ |
189 |
|
|
} |
190 |
|
|
|
191 |
|
✗ |
MEDIAN_SLICE(8, uint8_t, compare8) |
192 |
|
✗ |
MEDIAN_SLICE(16, uint16_t, compare16) |
193 |
|
|
|
194 |
|
✗ |
static void update_index(XMedianContext *s) |
195 |
|
|
{ |
196 |
|
✗ |
if (s->nb_inputs & 1) |
197 |
|
✗ |
s->index = s->radius * 2.f * s->percentile; |
198 |
|
|
else |
199 |
|
✗ |
s->index = av_clip(s->radius * 2.f * s->percentile, 1, s->nb_inputs - 1); |
200 |
|
✗ |
} |
201 |
|
|
|
202 |
|
✗ |
static int process_frame(FFFrameSync *fs) |
203 |
|
|
{ |
204 |
|
✗ |
AVFilterContext *ctx = fs->parent; |
205 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
206 |
|
✗ |
XMedianContext *s = fs->opaque; |
207 |
|
✗ |
AVFrame **in = s->frames; |
208 |
|
|
AVFrame *out; |
209 |
|
|
ThreadData td; |
210 |
|
|
int i, ret; |
211 |
|
|
|
212 |
|
✗ |
update_index(s); |
213 |
|
|
|
214 |
|
✗ |
for (i = 0; i < s->nb_inputs; i++) { |
215 |
|
✗ |
if ((ret = ff_framesync_get_frame(&s->fs, i, &in[i], 0)) < 0) |
216 |
|
✗ |
return ret; |
217 |
|
|
} |
218 |
|
|
|
219 |
|
✗ |
if (ctx->is_disabled) { |
220 |
|
✗ |
out = av_frame_clone(in[0]); |
221 |
|
|
} else { |
222 |
|
✗ |
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
223 |
|
|
} |
224 |
|
✗ |
if (!out) |
225 |
|
✗ |
return AVERROR(ENOMEM); |
226 |
|
✗ |
out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base); |
227 |
|
|
|
228 |
|
✗ |
if (!ctx->is_disabled) { |
229 |
|
✗ |
td.in = in; |
230 |
|
✗ |
td.out = out; |
231 |
|
✗ |
ff_filter_execute(ctx, s->median_frames, &td, NULL, |
232 |
|
✗ |
FFMIN(s->height[1], s->nb_threads)); |
233 |
|
|
} |
234 |
|
|
|
235 |
|
✗ |
return ff_filter_frame(outlink, out); |
236 |
|
|
} |
237 |
|
|
|
238 |
|
✗ |
static int config_output(AVFilterLink *outlink) |
239 |
|
|
{ |
240 |
|
✗ |
AVFilterContext *ctx = outlink->src; |
241 |
|
✗ |
XMedianContext *s = ctx->priv; |
242 |
|
✗ |
AVFilterLink *inlink = ctx->inputs[0]; |
243 |
|
✗ |
FilterLink *il = ff_filter_link(inlink); |
244 |
|
✗ |
FilterLink *ol = ff_filter_link(outlink); |
245 |
|
✗ |
AVRational frame_rate = il->frame_rate; |
246 |
|
✗ |
AVRational sar = ctx->inputs[0]->sample_aspect_ratio; |
247 |
|
✗ |
int height = ctx->inputs[0]->h; |
248 |
|
✗ |
int width = ctx->inputs[0]->w; |
249 |
|
|
FFFrameSyncIn *in; |
250 |
|
|
int i, ret; |
251 |
|
|
|
252 |
|
✗ |
for (int i = 1; i < s->nb_inputs && s->xmedian; i++) { |
253 |
|
✗ |
if (ctx->inputs[i]->h != height || ctx->inputs[i]->w != width) { |
254 |
|
✗ |
av_log(ctx, AV_LOG_ERROR, "Input %d size (%dx%d) does not match input %d size (%dx%d).\n", i, ctx->inputs[i]->w, ctx->inputs[i]->h, 0, width, height); |
255 |
|
✗ |
return AVERROR(EINVAL); |
256 |
|
|
} |
257 |
|
|
} |
258 |
|
|
|
259 |
|
✗ |
s->desc = av_pix_fmt_desc_get(outlink->format); |
260 |
|
✗ |
if (!s->desc) |
261 |
|
✗ |
return AVERROR_BUG; |
262 |
|
✗ |
s->nb_planes = av_pix_fmt_count_planes(outlink->format); |
263 |
|
✗ |
s->depth = s->desc->comp[0].depth; |
264 |
|
✗ |
s->max = (1 << s->depth) - 1; |
265 |
|
✗ |
s->nb_threads = ff_filter_get_nb_threads(ctx); |
266 |
|
|
|
267 |
|
✗ |
if (s->depth <= 8) |
268 |
|
✗ |
s->median_frames = median_frames8; |
269 |
|
|
else |
270 |
|
✗ |
s->median_frames = median_frames16; |
271 |
|
|
|
272 |
|
✗ |
if ((ret = av_image_fill_linesizes(s->linesizes, inlink->format, inlink->w)) < 0) |
273 |
|
✗ |
return ret; |
274 |
|
|
|
275 |
|
✗ |
s->width[1] = s->width[2] = AV_CEIL_RSHIFT(inlink->w, s->desc->log2_chroma_w); |
276 |
|
✗ |
s->width[0] = s->width[3] = inlink->w; |
277 |
|
✗ |
s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->desc->log2_chroma_h); |
278 |
|
✗ |
s->height[0] = s->height[3] = inlink->h; |
279 |
|
|
|
280 |
|
✗ |
s->data = av_calloc(s->nb_threads * s->nb_inputs, sizeof(*s->data)); |
281 |
|
✗ |
if (!s->data) |
282 |
|
✗ |
return AVERROR(ENOMEM); |
283 |
|
|
|
284 |
|
✗ |
s->linesize = av_calloc(s->nb_threads * s->nb_inputs, sizeof(*s->linesize)); |
285 |
|
✗ |
if (!s->linesize) |
286 |
|
✗ |
return AVERROR(ENOMEM); |
287 |
|
|
|
288 |
|
✗ |
if (!s->xmedian) |
289 |
|
✗ |
return 0; |
290 |
|
|
|
291 |
|
✗ |
outlink->w = width; |
292 |
|
✗ |
outlink->h = height; |
293 |
|
✗ |
ol->frame_rate = frame_rate; |
294 |
|
✗ |
outlink->sample_aspect_ratio = sar; |
295 |
|
|
|
296 |
|
✗ |
if ((ret = ff_framesync_init(&s->fs, ctx, s->nb_inputs)) < 0) |
297 |
|
✗ |
return ret; |
298 |
|
|
|
299 |
|
✗ |
in = s->fs.in; |
300 |
|
✗ |
s->fs.opaque = s; |
301 |
|
✗ |
s->fs.on_event = process_frame; |
302 |
|
|
|
303 |
|
✗ |
for (i = 0; i < s->nb_inputs; i++) { |
304 |
|
✗ |
AVFilterLink *inlink = ctx->inputs[i]; |
305 |
|
|
|
306 |
|
✗ |
in[i].time_base = inlink->time_base; |
307 |
|
✗ |
in[i].sync = 1; |
308 |
|
✗ |
in[i].before = EXT_STOP; |
309 |
|
✗ |
in[i].after = EXT_INFINITY; |
310 |
|
|
} |
311 |
|
|
|
312 |
|
✗ |
ret = ff_framesync_configure(&s->fs); |
313 |
|
✗ |
outlink->time_base = s->fs.time_base; |
314 |
|
|
|
315 |
|
✗ |
return ret; |
316 |
|
|
} |
317 |
|
|
|
318 |
|
✗ |
static av_cold void uninit(AVFilterContext *ctx) |
319 |
|
|
{ |
320 |
|
✗ |
XMedianContext *s = ctx->priv; |
321 |
|
|
|
322 |
|
✗ |
ff_framesync_uninit(&s->fs); |
323 |
|
|
|
324 |
|
✗ |
for (int i = 0; i < s->nb_frames && s->frames && !s->xmedian; i++) |
325 |
|
✗ |
av_frame_free(&s->frames[i]); |
326 |
|
✗ |
av_freep(&s->frames); |
327 |
|
✗ |
av_freep(&s->data); |
328 |
|
✗ |
av_freep(&s->linesize); |
329 |
|
✗ |
} |
330 |
|
|
|
331 |
|
✗ |
static int activate(AVFilterContext *ctx) |
332 |
|
|
{ |
333 |
|
✗ |
XMedianContext *s = ctx->priv; |
334 |
|
✗ |
return ff_framesync_activate(&s->fs); |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
static const AVFilterPad outputs[] = { |
338 |
|
|
{ |
339 |
|
|
.name = "default", |
340 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
341 |
|
|
.config_props = config_output, |
342 |
|
|
}, |
343 |
|
|
}; |
344 |
|
|
|
345 |
|
|
#if CONFIG_XMEDIAN_FILTER |
346 |
|
✗ |
static av_cold int xmedian_init(AVFilterContext *ctx) |
347 |
|
|
{ |
348 |
|
✗ |
XMedianContext *s = ctx->priv; |
349 |
|
|
int ret; |
350 |
|
|
|
351 |
|
✗ |
s->xmedian = 1; |
352 |
|
|
|
353 |
|
✗ |
s->radius = s->nb_inputs / 2; |
354 |
|
|
|
355 |
|
✗ |
for (int i = 0; i < s->nb_inputs; i++) { |
356 |
|
✗ |
AVFilterPad pad = { 0 }; |
357 |
|
|
|
358 |
|
✗ |
pad.type = AVMEDIA_TYPE_VIDEO; |
359 |
|
✗ |
pad.name = av_asprintf("input%d", i); |
360 |
|
✗ |
if (!pad.name) |
361 |
|
✗ |
return AVERROR(ENOMEM); |
362 |
|
|
|
363 |
|
✗ |
if ((ret = ff_append_inpad_free_name(ctx, &pad)) < 0) |
364 |
|
✗ |
return ret; |
365 |
|
|
} |
366 |
|
|
|
367 |
|
✗ |
return init(ctx); |
368 |
|
|
} |
369 |
|
|
|
370 |
|
|
static const AVOption xmedian_options[] = { |
371 |
|
|
{ "inputs", "set number of inputs", OFFSET(nb_inputs), AV_OPT_TYPE_INT, {.i64=3}, 3, 255, .flags = FLAGS }, |
372 |
|
|
{ "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags =TFLAGS }, |
373 |
|
|
{ "percentile", "set percentile", OFFSET(percentile),AV_OPT_TYPE_FLOAT,{.dbl=0.5}, 0, 1, .flags =TFLAGS }, |
374 |
|
|
{ NULL }, |
375 |
|
|
}; |
376 |
|
|
|
377 |
|
✗ |
FRAMESYNC_DEFINE_CLASS(xmedian, XMedianContext, fs); |
378 |
|
|
|
379 |
|
|
const AVFilter ff_vf_xmedian = { |
380 |
|
|
.name = "xmedian", |
381 |
|
|
.description = NULL_IF_CONFIG_SMALL("Pick median pixels from several video inputs."), |
382 |
|
|
.priv_size = sizeof(XMedianContext), |
383 |
|
|
.priv_class = &xmedian_class, |
384 |
|
|
FILTER_OUTPUTS(outputs), |
385 |
|
|
FILTER_PIXFMTS_ARRAY(pixel_fmts), |
386 |
|
|
.preinit = xmedian_framesync_preinit, |
387 |
|
|
.init = xmedian_init, |
388 |
|
|
.uninit = uninit, |
389 |
|
|
.activate = activate, |
390 |
|
|
.flags = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_SLICE_THREADS | |
391 |
|
|
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, |
392 |
|
|
.process_command = ff_filter_process_command, |
393 |
|
|
}; |
394 |
|
|
|
395 |
|
|
#endif /* CONFIG_XMEDIAN_FILTER */ |
396 |
|
|
|
397 |
|
|
#if CONFIG_TMEDIAN_FILTER |
398 |
|
✗ |
static int tmedian_filter_frame(AVFilterLink *inlink, AVFrame *in) |
399 |
|
|
{ |
400 |
|
✗ |
AVFilterContext *ctx = inlink->dst; |
401 |
|
✗ |
AVFilterLink *outlink = ctx->outputs[0]; |
402 |
|
✗ |
XMedianContext *s = ctx->priv; |
403 |
|
|
ThreadData td; |
404 |
|
|
AVFrame *out; |
405 |
|
|
|
406 |
|
✗ |
update_index(s); |
407 |
|
|
|
408 |
|
✗ |
if (s->nb_frames < s->nb_inputs) { |
409 |
|
✗ |
s->frames[s->nb_frames] = in; |
410 |
|
✗ |
s->nb_frames++; |
411 |
|
✗ |
if (s->nb_frames < s->nb_inputs) |
412 |
|
✗ |
return 0; |
413 |
|
|
} else { |
414 |
|
✗ |
av_frame_free(&s->frames[0]); |
415 |
|
✗ |
memmove(&s->frames[0], &s->frames[1], sizeof(*s->frames) * (s->nb_inputs - 1)); |
416 |
|
✗ |
s->frames[s->nb_inputs - 1] = in; |
417 |
|
|
} |
418 |
|
|
|
419 |
|
✗ |
if (ctx->is_disabled) { |
420 |
|
✗ |
out = av_frame_clone(s->frames[0]); |
421 |
|
✗ |
if (!out) |
422 |
|
✗ |
return AVERROR(ENOMEM); |
423 |
|
✗ |
return ff_filter_frame(outlink, out); |
424 |
|
|
} |
425 |
|
|
|
426 |
|
✗ |
out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
427 |
|
✗ |
if (!out) |
428 |
|
✗ |
return AVERROR(ENOMEM); |
429 |
|
✗ |
out->pts = s->frames[0]->pts; |
430 |
|
|
|
431 |
|
✗ |
td.out = out; |
432 |
|
✗ |
td.in = s->frames; |
433 |
|
✗ |
ff_filter_execute(ctx, s->median_frames, &td, NULL, |
434 |
|
✗ |
FFMIN(s->height[1], s->nb_threads)); |
435 |
|
|
|
436 |
|
✗ |
return ff_filter_frame(outlink, out); |
437 |
|
|
} |
438 |
|
|
|
439 |
|
|
static const AVOption tmedian_options[] = { |
440 |
|
|
{ "radius", "set median filter radius", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=1}, 1, 127, .flags = FLAGS }, |
441 |
|
|
{ "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=15}, 0, 15, .flags =TFLAGS }, |
442 |
|
|
{ "percentile", "set percentile", OFFSET(percentile), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, .flags =TFLAGS }, |
443 |
|
|
{ NULL }, |
444 |
|
|
}; |
445 |
|
|
|
446 |
|
|
static const AVFilterPad tmedian_inputs[] = { |
447 |
|
|
{ |
448 |
|
|
.name = "default", |
449 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
450 |
|
|
.filter_frame = tmedian_filter_frame, |
451 |
|
|
}, |
452 |
|
|
}; |
453 |
|
|
|
454 |
|
|
AVFILTER_DEFINE_CLASS(tmedian); |
455 |
|
|
|
456 |
|
|
const AVFilter ff_vf_tmedian = { |
457 |
|
|
.name = "tmedian", |
458 |
|
|
.description = NULL_IF_CONFIG_SMALL("Pick median pixels from successive frames."), |
459 |
|
|
.priv_size = sizeof(XMedianContext), |
460 |
|
|
.priv_class = &tmedian_class, |
461 |
|
|
FILTER_INPUTS(tmedian_inputs), |
462 |
|
|
FILTER_OUTPUTS(outputs), |
463 |
|
|
FILTER_PIXFMTS_ARRAY(pixel_fmts), |
464 |
|
|
.init = init, |
465 |
|
|
.uninit = uninit, |
466 |
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, |
467 |
|
|
.process_command = ff_filter_process_command, |
468 |
|
|
}; |
469 |
|
|
|
470 |
|
|
#endif /* CONFIG_TMEDIAN_FILTER */ |
471 |
|
|
|