Signed-off-by: Paul B Mahol <onemda@gmail.com>
Paul B Mahol authored on 2017/04/18 22:18:40... | ... |
@@ -6253,6 +6253,44 @@ Limit the maximum change for each plane, default is 65535. |
6253 | 6253 |
If 0, plane will remain unchanged. |
6254 | 6254 |
@end table |
6255 | 6255 |
|
6256 |
+@section deflicker |
|
6257 |
+ |
|
6258 |
+Remove temporal frame luminance variations. |
|
6259 |
+ |
|
6260 |
+It accepts the following options: |
|
6261 |
+ |
|
6262 |
+@table @option |
|
6263 |
+@item size, s |
|
6264 |
+Set moving-average filter size. Default is 5. Allowed range is 2 - 129. |
|
6265 |
+ |
|
6266 |
+@item mode, m |
|
6267 |
+Set averaging mode to smooth temporal luminance variations. |
|
6268 |
+ |
|
6269 |
+Available values are: |
|
6270 |
+@table @samp |
|
6271 |
+@item am |
|
6272 |
+Arithmetic mean |
|
6273 |
+ |
|
6274 |
+@item gm |
|
6275 |
+Geometric mean |
|
6276 |
+ |
|
6277 |
+@item hm |
|
6278 |
+Harmonic mean |
|
6279 |
+ |
|
6280 |
+@item qm |
|
6281 |
+Quadratic mean |
|
6282 |
+ |
|
6283 |
+@item cm |
|
6284 |
+Cubic mean |
|
6285 |
+ |
|
6286 |
+@item pm |
|
6287 |
+Power mean |
|
6288 |
+ |
|
6289 |
+@item median |
|
6290 |
+Median |
|
6291 |
+@end table |
|
6292 |
+@end table |
|
6293 |
+ |
|
6256 | 6294 |
@section dejudder |
6257 | 6295 |
|
6258 | 6296 |
Remove judder produced by partially interlaced telecined content. |
... | ... |
@@ -155,6 +155,7 @@ OBJS-$(CONFIG_DCTDNOIZ_FILTER) += vf_dctdnoiz.o |
155 | 155 |
OBJS-$(CONFIG_DEBAND_FILTER) += vf_deband.o |
156 | 156 |
OBJS-$(CONFIG_DECIMATE_FILTER) += vf_decimate.o |
157 | 157 |
OBJS-$(CONFIG_DEFLATE_FILTER) += vf_neighbor.o |
158 |
+OBJS-$(CONFIG_DEFLICKER_FILTER) += vf_deflicker.o |
|
158 | 159 |
OBJS-$(CONFIG_DEINTERLACE_QSV_FILTER) += vf_deinterlace_qsv.o |
159 | 160 |
OBJS-$(CONFIG_DEINTERLACE_VAAPI_FILTER) += vf_deinterlace_vaapi.o |
160 | 161 |
OBJS-$(CONFIG_DEJUDDER_FILTER) += vf_dejudder.o |
... | ... |
@@ -166,6 +166,7 @@ static void register_all(void) |
166 | 166 |
REGISTER_FILTER(DEBAND, deband, vf); |
167 | 167 |
REGISTER_FILTER(DECIMATE, decimate, vf); |
168 | 168 |
REGISTER_FILTER(DEFLATE, deflate, vf); |
169 |
+ REGISTER_FILTER(DEFLICKER, deflicker, vf); |
|
169 | 170 |
REGISTER_FILTER(DEINTERLACE_QSV,deinterlace_qsv,vf); |
170 | 171 |
REGISTER_FILTER(DEINTERLACE_VAAPI, deinterlace_vaapi, vf); |
171 | 172 |
REGISTER_FILTER(DEJUDDER, dejudder, vf); |
... | ... |
@@ -30,8 +30,8 @@ |
30 | 30 |
#include "libavutil/version.h" |
31 | 31 |
|
32 | 32 |
#define LIBAVFILTER_VERSION_MAJOR 6 |
33 |
-#define LIBAVFILTER_VERSION_MINOR 84 |
|
34 |
-#define LIBAVFILTER_VERSION_MICRO 101 |
|
33 |
+#define LIBAVFILTER_VERSION_MINOR 85 |
|
34 |
+#define LIBAVFILTER_VERSION_MICRO 100 |
|
35 | 35 |
|
36 | 36 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |
37 | 37 |
LIBAVFILTER_VERSION_MINOR, \ |
38 | 38 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,468 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2017 Paul B Mahol |
|
2 |
+ * |
|
3 |
+ * This file is part of FFmpeg. |
|
4 |
+ * |
|
5 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
6 |
+ * modify it under the terms of the GNU Lesser General Public |
|
7 |
+ * License as published by the Free Software Foundation; either |
|
8 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
9 |
+ * |
|
10 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
11 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
+ * Lesser General Public License for more details. |
|
14 |
+ * |
|
15 |
+ * You should have received a copy of the GNU Lesser General Public |
|
16 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
17 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
+ */ |
|
19 |
+ |
|
20 |
+#include "libavutil/imgutils.h" |
|
21 |
+#include "libavutil/opt.h" |
|
22 |
+#include "libavutil/pixdesc.h" |
|
23 |
+#include "libavutil/qsort.h" |
|
24 |
+#include "avfilter.h" |
|
25 |
+ |
|
26 |
+#define FF_BUFQUEUE_SIZE 129 |
|
27 |
+#include "bufferqueue.h" |
|
28 |
+ |
|
29 |
+#include "formats.h" |
|
30 |
+#include "internal.h" |
|
31 |
+#include "video.h" |
|
32 |
+ |
|
33 |
+#define SIZE FF_BUFQUEUE_SIZE |
|
34 |
+ |
|
35 |
+enum smooth_mode { |
|
36 |
+ ARITHMETIC_MEAN, |
|
37 |
+ GEOMETRIC_MEAN, |
|
38 |
+ HARMONIC_MEAN, |
|
39 |
+ QUADRATIC_MEAN, |
|
40 |
+ CUBIC_MEAN, |
|
41 |
+ POWER_MEAN, |
|
42 |
+ MEDIAN, |
|
43 |
+ NB_SMOOTH_MODE, |
|
44 |
+}; |
|
45 |
+ |
|
46 |
+typedef struct DeflickerContext { |
|
47 |
+ const AVClass *class; |
|
48 |
+ |
|
49 |
+ int size; |
|
50 |
+ int mode; |
|
51 |
+ |
|
52 |
+ int eof; |
|
53 |
+ int depth; |
|
54 |
+ int nb_planes; |
|
55 |
+ int planewidth[4]; |
|
56 |
+ int planeheight[4]; |
|
57 |
+ |
|
58 |
+ uint64_t *histogram; |
|
59 |
+ float luminance[SIZE]; |
|
60 |
+ float sorted[SIZE]; |
|
61 |
+ |
|
62 |
+ struct FFBufQueue q; |
|
63 |
+ int available; |
|
64 |
+ |
|
65 |
+ void (*get_factor)(AVFilterContext *ctx, float *f); |
|
66 |
+ float (*calc_avgy)(AVFilterContext *ctx, AVFrame *in); |
|
67 |
+ int (*deflicker)(AVFilterContext *ctx, const uint8_t *src, ptrdiff_t src_linesize, |
|
68 |
+ uint8_t *dst, ptrdiff_t dst_linesize, int w, int h, float f); |
|
69 |
+} DeflickerContext; |
|
70 |
+ |
|
71 |
+#define OFFSET(x) offsetof(DeflickerContext, x) |
|
72 |
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM |
|
73 |
+ |
|
74 |
+static const AVOption deflicker_options[] = { |
|
75 |
+ { "size", "set how many frames to use", OFFSET(size), AV_OPT_TYPE_INT, {.i64=5}, 2, SIZE, FLAGS }, |
|
76 |
+ { "s", "set how many frames to use", OFFSET(size), AV_OPT_TYPE_INT, {.i64=5}, 2, SIZE, FLAGS }, |
|
77 |
+ { "mode", "set how to smooth luminance", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SMOOTH_MODE-1, FLAGS, "mode" }, |
|
78 |
+ { "m", "set how to smooth luminance", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SMOOTH_MODE-1, FLAGS, "mode" }, |
|
79 |
+ { "am", "arithmetic mean", 0, AV_OPT_TYPE_CONST, {.i64=ARITHMETIC_MEAN}, 0, 0, FLAGS, "mode" }, |
|
80 |
+ { "gm", "geometric mean", 0, AV_OPT_TYPE_CONST, {.i64=GEOMETRIC_MEAN}, 0, 0, FLAGS, "mode" }, |
|
81 |
+ { "hm", "harmonic mean", 0, AV_OPT_TYPE_CONST, {.i64=HARMONIC_MEAN}, 0, 0, FLAGS, "mode" }, |
|
82 |
+ { "qm", "quadratic mean", 0, AV_OPT_TYPE_CONST, {.i64=QUADRATIC_MEAN}, 0, 0, FLAGS, "mode" }, |
|
83 |
+ { "cm", "cubic mean", 0, AV_OPT_TYPE_CONST, {.i64=CUBIC_MEAN}, 0, 0, FLAGS, "mode" }, |
|
84 |
+ { "pm", "power mean", 0, AV_OPT_TYPE_CONST, {.i64=POWER_MEAN}, 0, 0, FLAGS, "mode" }, |
|
85 |
+ { "median", "median", 0, AV_OPT_TYPE_CONST, {.i64=MEDIAN}, 0, 0, FLAGS, "mode" }, |
|
86 |
+ { NULL } |
|
87 |
+}; |
|
88 |
+ |
|
89 |
+AVFILTER_DEFINE_CLASS(deflicker); |
|
90 |
+ |
|
91 |
+static int query_formats(AVFilterContext *ctx) |
|
92 |
+{ |
|
93 |
+ static const enum AVPixelFormat pixel_fmts[] = { |
|
94 |
+ AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10, |
|
95 |
+ AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16, |
|
96 |
+ AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, |
|
97 |
+ AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, |
|
98 |
+ AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, |
|
99 |
+ AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, |
|
100 |
+ AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P, |
|
101 |
+ AV_PIX_FMT_YUVJ411P, |
|
102 |
+ AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, |
|
103 |
+ AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, |
|
104 |
+ AV_PIX_FMT_YUV440P10, |
|
105 |
+ AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, |
|
106 |
+ AV_PIX_FMT_YUV440P12, |
|
107 |
+ AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, |
|
108 |
+ AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, |
|
109 |
+ AV_PIX_FMT_NONE |
|
110 |
+ }; |
|
111 |
+ AVFilterFormats *formats = ff_make_format_list(pixel_fmts); |
|
112 |
+ if (!formats) |
|
113 |
+ return AVERROR(ENOMEM); |
|
114 |
+ return ff_set_common_formats(ctx, formats); |
|
115 |
+} |
|
116 |
+ |
|
117 |
+static int deflicker8(AVFilterContext *ctx, |
|
118 |
+ const uint8_t *src, ptrdiff_t src_linesize, |
|
119 |
+ uint8_t *dst, ptrdiff_t dst_linesize, |
|
120 |
+ int w, int h, float f) |
|
121 |
+{ |
|
122 |
+ int x, y; |
|
123 |
+ |
|
124 |
+ for (y = 0; y < h; y++) { |
|
125 |
+ for (x = 0; x < w; x++) { |
|
126 |
+ dst[x] = av_clip_uint8(src[x] * f); |
|
127 |
+ } |
|
128 |
+ |
|
129 |
+ dst += dst_linesize; |
|
130 |
+ src += src_linesize; |
|
131 |
+ } |
|
132 |
+ |
|
133 |
+ return 0; |
|
134 |
+} |
|
135 |
+ |
|
136 |
+static int deflicker16(AVFilterContext *ctx, |
|
137 |
+ const uint8_t *ssrc, ptrdiff_t src_linesize, |
|
138 |
+ uint8_t *ddst, ptrdiff_t dst_linesize, |
|
139 |
+ int w, int h, float f) |
|
140 |
+{ |
|
141 |
+ DeflickerContext *s = ctx->priv; |
|
142 |
+ const uint16_t *src = (const uint16_t *)ssrc; |
|
143 |
+ uint16_t *dst = (uint16_t *)ddst; |
|
144 |
+ const int max = (1 << s->depth) - 1; |
|
145 |
+ int x, y; |
|
146 |
+ |
|
147 |
+ for (y = 0; y < h; y++) { |
|
148 |
+ for (x = 0; x < w; x++) { |
|
149 |
+ dst[x] = av_clip(src[x] * f, 0, max); |
|
150 |
+ } |
|
151 |
+ |
|
152 |
+ dst += dst_linesize / 2; |
|
153 |
+ src += src_linesize / 2; |
|
154 |
+ } |
|
155 |
+ |
|
156 |
+ return 0; |
|
157 |
+} |
|
158 |
+ |
|
159 |
+static float calc_avgy8(AVFilterContext *ctx, AVFrame *in) |
|
160 |
+{ |
|
161 |
+ DeflickerContext *s = ctx->priv; |
|
162 |
+ const uint8_t *src = in->data[0]; |
|
163 |
+ int64_t sum = 0; |
|
164 |
+ int y, x; |
|
165 |
+ |
|
166 |
+ memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram)); |
|
167 |
+ |
|
168 |
+ for (y = 0; y < s->planeheight[0]; y++) { |
|
169 |
+ for (x = 0; x < s->planewidth[0]; x++) { |
|
170 |
+ s->histogram[src[x]]++; |
|
171 |
+ } |
|
172 |
+ src += in->linesize[0]; |
|
173 |
+ } |
|
174 |
+ |
|
175 |
+ for (y = 0; y < 1 << s->depth; y++) { |
|
176 |
+ sum += s->histogram[y] * y; |
|
177 |
+ } |
|
178 |
+ |
|
179 |
+ return 1.0f * sum / (s->planeheight[0] * s->planewidth[0]); |
|
180 |
+} |
|
181 |
+ |
|
182 |
+static float calc_avgy16(AVFilterContext *ctx, AVFrame *in) |
|
183 |
+{ |
|
184 |
+ DeflickerContext *s = ctx->priv; |
|
185 |
+ const uint16_t *src = (const uint16_t *)in->data[0]; |
|
186 |
+ int64_t sum = 0; |
|
187 |
+ int y, x; |
|
188 |
+ |
|
189 |
+ memset(s->histogram, 0, (1 << s->depth) * sizeof(*s->histogram)); |
|
190 |
+ |
|
191 |
+ for (y = 0; y < s->planeheight[0]; y++) { |
|
192 |
+ for (x = 0; x < s->planewidth[0]; x++) { |
|
193 |
+ s->histogram[src[x]]++; |
|
194 |
+ } |
|
195 |
+ src += in->linesize[0] / 2; |
|
196 |
+ } |
|
197 |
+ |
|
198 |
+ for (y = 0; y < 1 << s->depth; y++) { |
|
199 |
+ sum += s->histogram[y] * y; |
|
200 |
+ } |
|
201 |
+ |
|
202 |
+ return 1.0f * sum / (s->planeheight[0] * s->planewidth[0]); |
|
203 |
+} |
|
204 |
+ |
|
205 |
+static void get_am_factor(AVFilterContext *ctx, float *f) |
|
206 |
+{ |
|
207 |
+ DeflickerContext *s = ctx->priv; |
|
208 |
+ int y; |
|
209 |
+ |
|
210 |
+ *f = 0.0f; |
|
211 |
+ |
|
212 |
+ for (y = 0; y < s->size; y++) { |
|
213 |
+ *f += s->luminance[y]; |
|
214 |
+ } |
|
215 |
+ |
|
216 |
+ *f /= s->size; |
|
217 |
+ *f /= s->luminance[0]; |
|
218 |
+} |
|
219 |
+ |
|
220 |
+static void get_gm_factor(AVFilterContext *ctx, float *f) |
|
221 |
+{ |
|
222 |
+ DeflickerContext *s = ctx->priv; |
|
223 |
+ int y; |
|
224 |
+ |
|
225 |
+ *f = 1; |
|
226 |
+ |
|
227 |
+ for (y = 0; y < s->size; y++) { |
|
228 |
+ *f *= s->luminance[y]; |
|
229 |
+ } |
|
230 |
+ |
|
231 |
+ *f = pow(*f, 1.0f / s->size); |
|
232 |
+ *f /= s->luminance[0]; |
|
233 |
+} |
|
234 |
+ |
|
235 |
+static void get_hm_factor(AVFilterContext *ctx, float *f) |
|
236 |
+{ |
|
237 |
+ DeflickerContext *s = ctx->priv; |
|
238 |
+ int y; |
|
239 |
+ |
|
240 |
+ *f = 0.0f; |
|
241 |
+ |
|
242 |
+ for (y = 0; y < s->size; y++) { |
|
243 |
+ *f += 1.0f / s->luminance[y]; |
|
244 |
+ } |
|
245 |
+ |
|
246 |
+ *f = s->size / *f; |
|
247 |
+ *f /= s->luminance[0]; |
|
248 |
+} |
|
249 |
+ |
|
250 |
+static void get_qm_factor(AVFilterContext *ctx, float *f) |
|
251 |
+{ |
|
252 |
+ DeflickerContext *s = ctx->priv; |
|
253 |
+ int y; |
|
254 |
+ |
|
255 |
+ *f = 0.0f; |
|
256 |
+ |
|
257 |
+ for (y = 0; y < s->size; y++) { |
|
258 |
+ *f += s->luminance[y] * s->luminance[y]; |
|
259 |
+ } |
|
260 |
+ |
|
261 |
+ *f /= s->size; |
|
262 |
+ *f = sqrtf(*f); |
|
263 |
+ *f /= s->luminance[0]; |
|
264 |
+} |
|
265 |
+ |
|
266 |
+static void get_cm_factor(AVFilterContext *ctx, float *f) |
|
267 |
+{ |
|
268 |
+ DeflickerContext *s = ctx->priv; |
|
269 |
+ int y; |
|
270 |
+ |
|
271 |
+ *f = 0.0f; |
|
272 |
+ |
|
273 |
+ for (y = 0; y < s->size; y++) { |
|
274 |
+ *f += s->luminance[y] * s->luminance[y] * s->luminance[y]; |
|
275 |
+ } |
|
276 |
+ |
|
277 |
+ *f /= s->size; |
|
278 |
+ *f = cbrtf(*f); |
|
279 |
+ *f /= s->luminance[0]; |
|
280 |
+} |
|
281 |
+ |
|
282 |
+static void get_pm_factor(AVFilterContext *ctx, float *f) |
|
283 |
+{ |
|
284 |
+ DeflickerContext *s = ctx->priv; |
|
285 |
+ int y; |
|
286 |
+ |
|
287 |
+ *f = 0.0f; |
|
288 |
+ |
|
289 |
+ for (y = 0; y < s->size; y++) { |
|
290 |
+ *f += powf(s->luminance[y], s->size); |
|
291 |
+ } |
|
292 |
+ |
|
293 |
+ *f /= s->size; |
|
294 |
+ *f = powf(*f, 1.0f / s->size); |
|
295 |
+ *f /= s->luminance[0]; |
|
296 |
+} |
|
297 |
+ |
|
298 |
+static int comparef(const void *a, const void *b) |
|
299 |
+{ |
|
300 |
+ const float *aa = a, *bb = b; |
|
301 |
+ return round(aa - bb); |
|
302 |
+} |
|
303 |
+ |
|
304 |
+static void get_median_factor(AVFilterContext *ctx, float *f) |
|
305 |
+{ |
|
306 |
+ DeflickerContext *s = ctx->priv; |
|
307 |
+ |
|
308 |
+ memcpy(s->sorted, s->luminance, sizeof(s->sorted)); |
|
309 |
+ AV_QSORT(s->sorted, s->size, float, comparef); |
|
310 |
+ |
|
311 |
+ *f = s->sorted[s->size >> 1] / s->luminance[0]; |
|
312 |
+} |
|
313 |
+ |
|
314 |
+static int config_input(AVFilterLink *inlink) |
|
315 |
+{ |
|
316 |
+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
|
317 |
+ AVFilterContext *ctx = inlink->dst; |
|
318 |
+ DeflickerContext *s = ctx->priv; |
|
319 |
+ |
|
320 |
+ s->nb_planes = desc->nb_components; |
|
321 |
+ |
|
322 |
+ s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); |
|
323 |
+ s->planeheight[0] = s->planeheight[3] = inlink->h; |
|
324 |
+ s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); |
|
325 |
+ s->planewidth[0] = s->planewidth[3] = inlink->w; |
|
326 |
+ |
|
327 |
+ s->depth = desc->comp[0].depth; |
|
328 |
+ if (s->depth == 8) { |
|
329 |
+ s->deflicker = deflicker8; |
|
330 |
+ s->calc_avgy = calc_avgy8; |
|
331 |
+ } else { |
|
332 |
+ s->deflicker = deflicker16; |
|
333 |
+ s->calc_avgy = calc_avgy16; |
|
334 |
+ } |
|
335 |
+ |
|
336 |
+ s->histogram = av_calloc(1 << s->depth, sizeof(*s->histogram)); |
|
337 |
+ if (!s->histogram) |
|
338 |
+ return AVERROR(ENOMEM); |
|
339 |
+ |
|
340 |
+ switch (s->mode) { |
|
341 |
+ case MEDIAN: s->get_factor = get_median_factor; break; |
|
342 |
+ case ARITHMETIC_MEAN: s->get_factor = get_am_factor; break; |
|
343 |
+ case GEOMETRIC_MEAN: s->get_factor = get_gm_factor; break; |
|
344 |
+ case HARMONIC_MEAN: s->get_factor = get_hm_factor; break; |
|
345 |
+ case QUADRATIC_MEAN: s->get_factor = get_qm_factor; break; |
|
346 |
+ case CUBIC_MEAN: s->get_factor = get_cm_factor; break; |
|
347 |
+ case POWER_MEAN: s->get_factor = get_pm_factor; break; |
|
348 |
+ } |
|
349 |
+ |
|
350 |
+ return 0; |
|
351 |
+} |
|
352 |
+ |
|
353 |
+static int filter_frame(AVFilterLink *inlink, AVFrame *buf) |
|
354 |
+{ |
|
355 |
+ AVFilterContext *ctx = inlink->dst; |
|
356 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
357 |
+ DeflickerContext *s = ctx->priv; |
|
358 |
+ AVDictionary **metadata; |
|
359 |
+ AVFrame *out, *in; |
|
360 |
+ float f; |
|
361 |
+ int y; |
|
362 |
+ |
|
363 |
+ if (s->q.available < s->size && !s->eof) { |
|
364 |
+ s->luminance[s->available] = s->calc_avgy(ctx, buf); |
|
365 |
+ ff_bufqueue_add(ctx, &s->q, buf); |
|
366 |
+ s->available++; |
|
367 |
+ return 0; |
|
368 |
+ } |
|
369 |
+ |
|
370 |
+ in = ff_bufqueue_peek(&s->q, 0); |
|
371 |
+ |
|
372 |
+ out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
|
373 |
+ if (!out) { |
|
374 |
+ av_frame_free(&buf); |
|
375 |
+ return AVERROR(ENOMEM); |
|
376 |
+ } |
|
377 |
+ |
|
378 |
+ s->get_factor(ctx, &f); |
|
379 |
+ s->deflicker(ctx, in->data[0], in->linesize[0], out->data[0], out->linesize[0], |
|
380 |
+ outlink->w, outlink->h, f); |
|
381 |
+ for (y = 1; y < s->nb_planes; y++) { |
|
382 |
+ av_image_copy_plane(out->data[y], out->linesize[y], |
|
383 |
+ in->data[y], in->linesize[y], |
|
384 |
+ s->planewidth[y] * (1 + (s->depth > 8)), s->planeheight[y]); |
|
385 |
+ } |
|
386 |
+ |
|
387 |
+ av_frame_copy_props(out, in); |
|
388 |
+ metadata = avpriv_frame_get_metadatap(out); |
|
389 |
+ if (metadata) { |
|
390 |
+ uint8_t value[128]; |
|
391 |
+ |
|
392 |
+ snprintf(value, sizeof(value), "%f", s->luminance[0]); |
|
393 |
+ av_dict_set(metadata, "lavfi.deflicker.luminance", value, 0); |
|
394 |
+ |
|
395 |
+ snprintf(value, sizeof(value), "%f", s->luminance[0] * f); |
|
396 |
+ av_dict_set(metadata, "lavfi.deflicker.new_luminance", value, 0); |
|
397 |
+ |
|
398 |
+ snprintf(value, sizeof(value), "%f", f - 1.0f); |
|
399 |
+ av_dict_set(metadata, "lavfi.deflicker.relative_change", value, 0); |
|
400 |
+ } |
|
401 |
+ |
|
402 |
+ in = ff_bufqueue_get(&s->q); |
|
403 |
+ av_frame_free(&in); |
|
404 |
+ memmove(&s->luminance[0], &s->luminance[1], sizeof(*s->luminance) * (s->size - 1)); |
|
405 |
+ s->luminance[s->available - 1] = s->calc_avgy(ctx, buf); |
|
406 |
+ ff_bufqueue_add(ctx, &s->q, buf); |
|
407 |
+ |
|
408 |
+ return ff_filter_frame(outlink, out); |
|
409 |
+} |
|
410 |
+ |
|
411 |
+static int request_frame(AVFilterLink *outlink) |
|
412 |
+{ |
|
413 |
+ AVFilterContext *ctx = outlink->src; |
|
414 |
+ DeflickerContext *s = ctx->priv; |
|
415 |
+ int ret; |
|
416 |
+ |
|
417 |
+ ret = ff_request_frame(ctx->inputs[0]); |
|
418 |
+ if (ret == AVERROR_EOF && s->available > 0) { |
|
419 |
+ AVFrame *buf = av_frame_clone(ff_bufqueue_peek(&s->q, s->size - 1)); |
|
420 |
+ if (!buf) |
|
421 |
+ return AVERROR(ENOMEM); |
|
422 |
+ |
|
423 |
+ s->eof = 1; |
|
424 |
+ ret = filter_frame(ctx->inputs[0], buf); |
|
425 |
+ s->available--; |
|
426 |
+ } |
|
427 |
+ |
|
428 |
+ return ret; |
|
429 |
+} |
|
430 |
+ |
|
431 |
+static av_cold void uninit(AVFilterContext *ctx) |
|
432 |
+{ |
|
433 |
+ DeflickerContext *s = ctx->priv; |
|
434 |
+ |
|
435 |
+ ff_bufqueue_discard_all(&s->q); |
|
436 |
+ av_freep(&s->histogram); |
|
437 |
+} |
|
438 |
+ |
|
439 |
+static const AVFilterPad inputs[] = { |
|
440 |
+ { |
|
441 |
+ .name = "default", |
|
442 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
443 |
+ .filter_frame = filter_frame, |
|
444 |
+ .config_props = config_input, |
|
445 |
+ }, |
|
446 |
+ { NULL } |
|
447 |
+}; |
|
448 |
+ |
|
449 |
+static const AVFilterPad outputs[] = { |
|
450 |
+ { |
|
451 |
+ .name = "default", |
|
452 |
+ .type = AVMEDIA_TYPE_VIDEO, |
|
453 |
+ .request_frame = request_frame, |
|
454 |
+ }, |
|
455 |
+ { NULL } |
|
456 |
+}; |
|
457 |
+ |
|
458 |
+AVFilter ff_vf_deflicker = { |
|
459 |
+ .name = "deflicker", |
|
460 |
+ .description = NULL_IF_CONFIG_SMALL("Remove temporal frame luminance variations."), |
|
461 |
+ .priv_size = sizeof(DeflickerContext), |
|
462 |
+ .priv_class = &deflicker_class, |
|
463 |
+ .uninit = uninit, |
|
464 |
+ .query_formats = query_formats, |
|
465 |
+ .inputs = inputs, |
|
466 |
+ .outputs = outputs, |
|
467 |
+}; |