FFmpeg: libavfilter/vf_guided.c Source File
Go to the documentation of this file. 1 /*
2 * Copyright (c) 2021 Xuewei Meng
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
29
34 };
35
40 };
41
45
52
55
60
69
74
77
78 #define OFFSET(x) offsetof(GuidedContext, x)
79 #define TFLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
80 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
81
94 };
95
97
106
108 {
111
118 const int radius =
s->radius;
119 const float *
src = t->
src;
121
123 int numPix;
124 w = (radius << 1) + 1;
127 for (
int j = 0;j <
width;j++) {
129 for (int row = -radius;row <= radius;row++) {
130 for (int col = -radius;col <= radius;col++) {
132 int y = j + col;
134 y = (y < 0) ? 0 : (y >=
width ?
width - 1 : y);
135 temp +=
src[x * src_stride + y];
136 }
137 }
138 dst[
i * dst_stride + j] =
temp / numPix;
139 }
140 }
141 return 0;
142 }
143
163 };
164
166 {
170
173 }
else if (
s->mode ==
FAST) {
174 if (
s->radius >=
s->sub)
175 s->radius =
s->radius /
s->sub;
176 else {
178 }
179 }
180
181 s->depth =
desc->comp[0].depth;
182 s->width =
ctx->inputs[0]->w;
183 s->height =
ctx->inputs[0]->h;
184
186 s->planewidth[0] =
s->planewidth[3] =
inlink->w;
188 s->planeheight[0] =
s->planeheight[3] =
inlink->h;
189
192 return 0;
193 }
194
195 #define GUIDED(type, name) \
196 static int guided_##name(AVFilterContext *ctx, GuidedContext *s, \
197 const uint8_t *ssrc, const uint8_t *ssrcRef, \
198 uint8_t *ddst, int radius, float eps, int width, int height, \
199 int src_stride, int src_ref_stride, int dst_stride, \
200 float maxval) \
201 { \
202 int ret = 0; \
203 type *dst = (type *)ddst; \
204 const type *src = (const type *)ssrc; \
205 const type *srcRef = (const type *)ssrcRef; \
206 \
207 int sub = s->sub; \
208 int h = (height % sub) == 0 ? height / sub : height / sub + 1; \
209 int w = (width % sub) == 0 ? width / sub : width / sub + 1; \
210 \
211 ThreadData t; \
212 const int nb_threads = ff_filter_get_nb_threads(ctx); \
213 float *I = s->I; \
214 float *II = s->II; \
215 float *P = s->P; \
216 float *IP = s->IP; \
217 float *meanI = s->meanI; \
218 float *meanII = s->meanII; \
219 float *meanP = s->meanP; \
220 float *meanIP = s->meanIP; \
221 float *A = s->A; \
222 float *B = s->B; \
223 float *meanA = s->meanA; \
224 float *meanB = s->meanB; \
225 \
226 for (int i = 0;i < h;i++) { \
227 for (int j = 0;j < w;j++) { \
228 int x = i * w + j; \
229 I[x] = src[(i * src_stride + j) * sub] / maxval; \
230 II[x] = I[x] * I[x]; \
231 P[x] = srcRef[(i * src_ref_stride + j) * sub] / maxval; \
232 IP[x] = I[x] * P[x]; \
233 } \
234 } \
235 \
236 t.width = w; \
237 t.height = h; \
238 t.srcStride = w; \
239 t.dstStride = w; \
240 t.src = I; \
241 t.dst = meanI; \
242 ff_filter_execute(ctx, s->box_slice, &t, NULL, FFMIN(h, nb_threads)); \
243 t.src = II; \
244 t.dst = meanII; \
245 ff_filter_execute(ctx, s->box_slice, &t, NULL, FFMIN(h, nb_threads)); \
246 t.src = P; \
247 t.dst = meanP; \
248 ff_filter_execute(ctx, s->box_slice, &t, NULL, FFMIN(h, nb_threads)); \
249 t.src = IP; \
250 t.dst = meanIP; \
251 ff_filter_execute(ctx, s->box_slice, &t, NULL, FFMIN(h, nb_threads)); \
252 \
253 for (int i = 0;i < h;i++) { \
254 for (int j = 0;j < w;j++) { \
255 int x = i * w + j; \
256 float varI = meanII[x] - (meanI[x] * meanI[x]); \
257 float covIP = meanIP[x] - (meanI[x] * meanP[x]); \
258 A[x] = covIP / (varI + eps); \
259 B[x] = meanP[x] - A[x] * meanI[x]; \
260 } \
261 } \
262 \
263 t.src = A; \
264 t.dst = meanA; \
265 ff_filter_execute(ctx, s->box_slice, &t, NULL, FFMIN(h, nb_threads)); \
266 t.src = B; \
267 t.dst = meanB; \
268 ff_filter_execute(ctx, s->box_slice, &t, NULL, FFMIN(h, nb_threads)); \
269 \
270 for (int i = 0;i < height;i++) { \
271 for (int j = 0;j < width;j++) { \
272 int x = i / sub * w + j / sub; \
273 dst[i * dst_stride + j] = meanA[x] * src[i * src_stride + j] + \
274 meanB[x] * maxval; \
275 } \
276 } \
277 \
278 return ret; \
279 }
280
283
285 {
292
293 for (
int plane = 0; plane <
s->nb_planes; plane++) {
294 if (!(
s->planes & (1 << plane))) {
296 in->data[plane], in->linesize[plane],
297 s->planewidth[plane] * ((
s->depth + 7) / 8),
s->planeheight[plane]);
298 continue;
299 }
301 guided_byte(
ctx,
s, in->data[plane],
ref->data[plane], (*out)->data[plane],
s->radius,
s->eps,
302 s->planewidth[plane],
s->planeheight[plane],
303 in->linesize[plane],
ref->linesize[plane], (*out)->linesize[plane], (1 <<
s->depth) - 1.f);
304 else
305 guided_word(
ctx,
s, in->data[plane],
ref->data[plane], (*out)->data[plane],
s->radius,
s->eps,
306 s->planewidth[plane],
s->planeheight[plane],
307 in->linesize[plane] / 2,
ref->linesize[plane] / 2, (*out)->linesize[plane] / 2, (1 <<
s->depth) - 1.f);
308 }
309
310 return 0;
311 }
312
314 {
322
323 if (
ctx->is_disabled)
325
330
332 }
333
335 {
343
344 if (
s->guidance ==
ON) {
345 if (
ctx->inputs[0]->w !=
ctx->inputs[1]->w ||
346 ctx->inputs[0]->h !=
ctx->inputs[1]->h) {
349 }
350 }
351
352 outlink->
w =
w = mainlink->
w;
353 outlink->
h =
h = mainlink->
h;
357
366
371
372 if (!
s->I || !
s->II || !
s->P || !
s->IP || !
s->meanI || !
s->meanII || !
s->meanP ||
373 !
s->meanIP || !
s->A || !
s->B || !
s->meanA || !
s->meanB)
375
376 if (
s->guidance ==
OFF)
377 return 0;
378
381
383
395
397 }
398
400 {
410
412
414 if (
ctx->is_disabled)
416
422 }
427 return 0;
428 }
431 return 0;
432 }
433
435 {
439
443
446
447 if (
s->guidance ==
ON) {
449 pad.
name =
"guidance";
451
454 }
455
456 return 0;
457 }
458
460 {
462 if (
s->guidance ==
ON)
464
477
478 return;
479 }
480
482 {
486 },
487 };
488
492 .p.priv_class = &guided_class,
503 };
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
#define AV_PIX_FMT_YUVA422P16
#define AV_PIX_FMT_GBRAP16
AVRational time_base
Time base for the incoming frames.
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
AVPixelFormat
Pixel format.
static int config_input(AVFilterLink *inlink)
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
#define FILTER_PIXFMTS_ARRAY(array)
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
static int filter_frame(AVFilterContext *ctx, AVFrame **out, AVFrame *in, AVFrame *ref)
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
static int activate(AVFilterContext *ctx)
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define AV_PIX_FMT_YUVA422P9
This structure describes decoded (raw) audio or video data.
AVFILTER_DEFINE_CLASS(guided)
#define AV_PIX_FMT_YUVA420P16
#define GUIDED(type, name)
#define AV_PIX_FMT_YUVA420P10
#define AV_PIX_FMT_YUV420P10
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
static const AVFilterPad guided_outputs[]
const char * name
Filter name.
@ EXT_INFINITY
Extend the frame to infinity.
A link between two filters.
#define AV_PIX_FMT_YUVA422P10
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
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.
Link properties exposed to filter code, but not external callers.
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
#define AV_PIX_FMT_YUVA420P9
static const AVOption guided_options[]
#define AV_PIX_FMT_GBRP14
int ff_append_inpad(AVFilterContext *f, AVFilterPad *p)
Append a new input/output pad to the filter's list of such pads.
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
#define AV_PIX_FMT_GBRP10
#define AV_PIX_FMT_YUVA444P16
#define AV_PIX_FMT_YUV422P9
#define AV_PIX_FMT_GRAY16
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events.
#define AVFILTER_FLAG_DYNAMIC_INPUTS
The number of the filter inputs is not determined just by AVFilter.inputs.
A filter pad used for either input or output.
#define AV_PIX_FMT_YUV444P10
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_PIX_FMT_YUV422P16
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
#define AV_PIX_FMT_GBRAP10
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
#define AV_PIX_FMT_GBRAP12
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
#define AV_PIX_FMT_YUV444P16
#define AV_CEIL_RSHIFT(a, b)
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
const FFFilter ff_vf_guided
#define AV_PIX_FMT_YUV420P9
#define AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_GRAY14
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FILTER_OUTPUTS(array)
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
#define AV_PIX_FMT_GRAY10
#define AV_PIX_FMT_GBRP16
Describe the class of an AVClass context structure.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
#define fs(width, name, subs,...)
@ 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_YUV422P10
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
static int box_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
static FilterLink * ff_filter_link(AVFilterLink *link)
int(* config_props)(AVFilterLink *link)
Link configuration callback.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
#define AV_PIX_FMT_YUV422P12
static int ref_frame(VVCFrame *dst, const VVCFrame *src)
#define AV_PIX_FMT_YUV444P12
AVFilterContext * src
source 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.
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
static av_cold int init(AVFilterContext *ctx)
#define AV_PIX_FMT_YUVA444P10
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
#define i(width, name, range_min, range_max)
int w
agreed upon image width
#define AV_PIX_FMT_GBRP12
Used for passing data between threads.
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
const char * name
Pad name.
void * av_calloc(size_t nmemb, size_t size)
#define AV_PIX_FMT_YUV444P9
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
static enum AVPixelFormat pix_fmts[]
enum AVMediaType type
AVFilterPad type.
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define AV_PIX_FMT_YUVA444P9
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
#define AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV422P14
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
int(* box_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
int h
agreed upon image height
@ AV_OPT_TYPE_INT
Underlying C type is int.
static av_cold void uninit(AVFilterContext *ctx)
static int ref[MAX_W *MAX_W]
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link.
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
AVFilter p
The public AVFilter.
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
static int config_output(AVFilterLink *outlink)
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
static const struct @543 planes[]
#define AV_PIX_FMT_YUV440P12
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
AVRational frame_rate
Frame rate of the stream on the link, or 1/0 if unknown or variable.
#define AV_PIX_FMT_YUV444P14
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
#define AV_PIX_FMT_GRAY12
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
static int process_frame(FFFrameSync *fs)
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
#define AV_PIX_FMT_YUV420P14
Generated on Wed Nov 19 2025 19:22:58 for FFmpeg by
doxygen
1.8.17