FFmpeg
Loading...
Searching...
No Matches
vf_lagfun.c
Go to the documentation of this file.
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 "libavutil/imgutils.h"
22#include "libavutil/mem.h"
23#include "libavutil/opt.h"
24#include "libavutil/pixdesc.h"
25
26#include "avfilter.h"
27#include "filters.h"
28#include "video.h"
29
30typedef struct LagfunContext {
31 const AVClass *class;
32 float decay;
33 int planes;
34
35 int depth;
37 int linesize[4];
38 int planewidth[4];
40
41 float *old[4];
42
43 int (*lagfun[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
45
68
69typedef struct ThreadData {
70 AVFrame *in, *out;
72
73#define LAGFUN(name, type, round, disabled) \
74static int lagfun_frame##name(AVFilterContext *ctx, void *arg, \
75 int jobnr, int nb_jobs) \
76{ \
77 LagfunContext *s = ctx->priv; \
78 const float decay = s->decay; \
79 ThreadData *td = arg; \
80 AVFrame *in = td->in; \
81 AVFrame *out = td->out; \
82 \
83 for (int p = 0; p < s->nb_planes; p++) { \
84 const int slice_start = ff_slice_pos(s->planeheight[p], jobnr, nb_jobs); \
85 const int slice_end = ff_slice_pos(s->planeheight[p], jobnr + 1, nb_jobs); \
86 const int width = s->planewidth[p]; \
87 const type *src = (const type *)in->data[p] + \
88 slice_start * in->linesize[p] / sizeof(type); \
89 float *osrc = s->old[p] + slice_start * s->planewidth[p]; \
90 type *dst = (type *)out->data[p] + \
91 slice_start * out->linesize[p] / sizeof(type); \
92 \
93 if (!((1 << p) & s->planes)) { \
94 av_image_copy_plane((uint8_t *)dst, out->linesize[p], \
95 (const uint8_t *)src, in->linesize[p], \
96 s->linesize[p], slice_end - slice_start); \
97 continue; \
98 } \
99 \
100 for (int y = slice_start; y < slice_end; y++) { \
101 for (int x = 0; x < width; x++) { \
102 const float v = fmaxf(src[x], osrc[x] * decay); \
103 \
104 osrc[x] = v; \
105 if (disabled) { \
106 dst[x] = src[x]; \
107 } else { \
108 dst[x] = round(v); \
109 } \
110 } \
111 \
112 src += in->linesize[p] / sizeof(type); \
113 osrc += width; \
114 dst += out->linesize[p] / sizeof(type); \
115 } \
116 } \
117 \
118 return 0; \
119}
120
121LAGFUN(8, uint8_t, lrintf, 0)
122LAGFUN(16, uint16_t, lrintf, 0)
123LAGFUN(32, float, , 0)
124
125LAGFUN(d8, uint8_t, lrintf, 1)
126LAGFUN(d16, uint16_t, lrintf, 1)
127LAGFUN(d32, float, , 1)
128
129static int config_output(AVFilterLink *outlink)
130{
131 AVFilterContext *ctx = outlink->src;
132 LagfunContext *s = ctx->priv;
133 AVFilterLink *inlink = ctx->inputs[0];
135 int ret;
136
137 desc = av_pix_fmt_desc_get(outlink->format);
138 if (!desc)
139 return AVERROR_BUG;
140 s->nb_planes = av_pix_fmt_count_planes(outlink->format);
141 s->depth = desc->comp[0].depth;
142 s->lagfun[0] = s->depth <= 8 ? lagfun_frame8 : s->depth <= 16 ? lagfun_frame16 : lagfun_frame32;
143 s->lagfun[1] = s->depth <= 8 ? lagfun_framed8 : s->depth <= 16 ? lagfun_framed16 : lagfun_framed32;
144
145 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
146 return ret;
147
148 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
149 s->planewidth[0] = s->planewidth[3] = inlink->w;
150 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
151 s->planeheight[0] = s->planeheight[3] = inlink->h;
152
153 for (int p = 0; p < s->nb_planes; p++) {
154 s->old[p] = av_calloc(s->planewidth[p] * s->planeheight[p], sizeof(*s->old[0]));
155 if (!s->old[p])
156 return AVERROR(ENOMEM);
157 }
158
159 return 0;
160}
161
162static int filter_frame(AVFilterLink *inlink, AVFrame *in)
163{
164 AVFilterContext *ctx = inlink->dst;
165 AVFilterLink *outlink = ctx->outputs[0];
166 LagfunContext *s = ctx->priv;
167 ThreadData td;
168 AVFrame *out;
169
170 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
171 if (!out) {
172 av_frame_free(&in);
173 return AVERROR(ENOMEM);
174 }
176
177 td.out = out;
178 td.in = in;
179 ff_filter_execute(ctx, s->lagfun[!!ctx->is_disabled], &td, NULL,
180 FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
181
182 av_frame_free(&in);
183 return ff_filter_frame(outlink, out);
184}
185
187{
188 LagfunContext *s = ctx->priv;
189
190 for (int p = 0; p < s->nb_planes; p++)
191 av_freep(&s->old[p]);
192}
193
194#define OFFSET(x) offsetof(LagfunContext, x)
195#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
196
197static const AVOption lagfun_options[] = {
198 { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_FLOAT, {.dbl=.95}, 0, 1, FLAGS },
199 { "planes", "set what planes to filter", OFFSET(planes), AV_OPT_TYPE_FLAGS, {.i64=15}, 0, 15, FLAGS },
200 { NULL },
201};
202
203static const AVFilterPad inputs[] = {
204 {
205 .name = "default",
206 .type = AVMEDIA_TYPE_VIDEO,
207 .filter_frame = filter_frame,
208 },
209};
210
211static const AVFilterPad outputs[] = {
212 {
213 .name = "default",
214 .type = AVMEDIA_TYPE_VIDEO,
215 .config_props = config_output,
216 },
217};
218
220
222 .p.name = "lagfun",
223 .p.description = NULL_IF_CONFIG_SMALL("Slowly update darker pixels."),
224 .p.priv_class = &lagfun_class,
226 .priv_size = sizeof(LagfunContext),
227 .uninit = uninit,
231 .process_command = ff_filter_process_command,
232};
static const AVFilterPad inputs[]
Definition af_aap.c:299
static const AVFilterPad outputs[]
Definition af_aap.c:310
const FFFilter ff_vf_lagfun
Definition vf_lagfun.c:221
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
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.
Definition avfilter.c:906
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:204
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition imgutils.c:89
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:244
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define lrintf(x)
Definition libm_mips.h:72
const char * desc
Definition libsvtav1.c:83
static const struct @257111027162314367033347246032313251342043035002 planes[]
#define FFMIN(a, b)
Definition macros.h:49
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:556
#define AV_PIX_FMT_GBRPF32
Definition pixfmt.h:584
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:544
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
#define AV_PIX_FMT_YUV440P12
Definition pixfmt.h:551
#define AV_PIX_FMT_GRAY9
Definition pixfmt.h:524
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:563
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:543
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:565
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:542
#define AV_PIX_FMT_GRAYF32
Definition pixfmt.h:588
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:553
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition pixfmt.h:107
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition pixfmt.h:283
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition pixfmt.h:86
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:554
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:527
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:557
#define AV_PIX_FMT_YUV440P10
Definition pixfmt.h:547
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:555
#define AV_PIX_FMT_GBRP14
Definition pixfmt.h:566
#define AV_PIX_FMT_GBRAPF32
Definition pixfmt.h:585
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:558
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
int planewidth[4]
Definition vf_lagfun.c:38
int planeheight[4]
Definition vf_lagfun.c:39
int linesize[4]
Definition vf_lagfun.c:37
float * old[4]
Definition vf_lagfun.c:41
int(* lagfun[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_lagfun.c:43
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * out
#define av_freep(p)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static enum AVPixelFormat pixel_fmts[]
Definition vf_amplify.c:52
#define LAGFUN(name, type, round, disabled)
Definition vf_lagfun.c:73
static const AVOption lagfun_options[]
Definition vf_lagfun.c:197
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition vf_lagfun.c:162
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_lagfun.c:186
#define OFFSET(x)
Definition vf_lagfun.c:194
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89