FFmpeg
vf_feedback.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * feedback video filter
22  */
23 
24 #include "libavutil/fifo.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/internal.h"
28 #include "avfilter.h"
29 #include "filters.h"
30 #include "formats.h"
31 #include "video.h"
32 
33 typedef struct FeedbackContext {
34  const AVClass *class;
35 
36  int x, y;
37  int w, h;
38 
39  int max_step[4];
40  int hsub, vsub;
41 
43 
46 
48 {
49  if (s->x + s->w > ctx->inputs[0]->w)
50  s->x = ctx->inputs[0]->w - s->w;
51  if (s->y + s->h > ctx->inputs[0]->h)
52  s->y = ctx->inputs[0]->h - s->h;
53 }
54 
56 {
57  if (s->x >= ctx->inputs[0]->w)
58  s->x = 0;
59  if (s->y >= ctx->inputs[0]->h)
60  s->y = 0;
61 
62  if (s->w <= 0)
63  s->w = ctx->inputs[0]->w - s->x;
64  if (s->h <= 0)
65  s->h = ctx->inputs[0]->h - s->y;
66 
67  if (s->w > ctx->inputs[0]->w)
68  s->w = ctx->inputs[0]->w;
69  if (s->h > ctx->inputs[0]->h)
70  s->h = ctx->inputs[0]->h;
71 
72  adjust_pos(ctx, s);
73 }
74 
76 {
77  AVFilterContext *ctx = inlink->dst;
78  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
79  FeedbackContext *s = ctx->priv;
80 
81  s->hsub = pix_desc->log2_chroma_w;
82  s->vsub = pix_desc->log2_chroma_h;
83 
84  av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
85 
87 
88  ctx->inputs[1]->w = s->w;
89  ctx->inputs[1]->h = s->h;
90 
91  return 0;
92 }
93 
94 static int config_output(AVFilterLink *outlink)
95 {
96  AVFilterContext *ctx = outlink->src;
97  FeedbackContext *s = ctx->priv;
98 
100 
101  ctx->outputs[0]->w = ctx->inputs[0]->w;
102  ctx->outputs[0]->h = ctx->inputs[0]->h;
103  ctx->outputs[1]->w = s->w;
104  ctx->outputs[1]->h = s->h;
105 
106  return 0;
107 }
108 
110  AVFilterFormatsConfig **cfg_in,
111  AVFilterFormatsConfig **cfg_out)
112 {
113  return ff_set_common_formats2(ctx, cfg_in, cfg_out,
117 }
118 
120 {
121  FeedbackContext *s = ctx->priv;
122  int status, ret;
123  int64_t pts;
124 
125  adjust_pos(ctx, s);
126 
127  for (int i = 0; i < ctx->nb_outputs; i++)
129 
130  if (!s->feed) {
131  ret = ff_inlink_consume_frame(ctx->inputs[1], &s->feed);
132  if (ret < 0)
133  return ret;
134  }
135 
136  if (s->feed && av_fifo_can_read(s->fifo)) {
137  AVFrame *src = s->feed;
138  AVFrame *dst = NULL;
139 
140  av_fifo_read(s->fifo, &dst, 1);
141  if (!dst)
142  return AVERROR_BUG;
143 
144  if (!av_frame_is_writable(dst)) {
145  AVFrame *tmp = ff_get_video_buffer(ctx->outputs[0], ctx->outputs[0]->w, ctx->outputs[0]->h);
146 
147  if (!tmp) {
148  av_frame_free(&dst);
149  return AVERROR(ENOMEM);
150  }
151 
152  ret = av_frame_copy(tmp, dst);
153  if (ret < 0) {
154  av_frame_free(&dst);
155  av_frame_free(&tmp);
156  return ret;
157  }
158 
160  av_frame_free(&dst);
161  dst = tmp;
162  }
163 
164  for (int y = 0; y < src->height; y++) {
165  memmove(dst->data[0] + (s->y + y) * dst->linesize[0] + s->x * s->max_step[0],
166  src->data[0] + y * src->linesize[0], src->width * s->max_step[0]);
167  }
168 
169  for (int i = 1; i < 3; i++) {
170  if (dst->data[i]) {
171  for (int y = 0; y < src->height; y++) {
172  memmove(dst->data[i] + ((s->y + y) >> s->vsub) * dst->linesize[i] + (s->x >> s->hsub) * s->max_step[i],
173  src->data[i] + (y >> s->vsub) * src->linesize[i], (src->width >> s->hsub) * s->max_step[i]);
174  }
175  }
176  }
177 
178  if (dst->data[3]) {
179  for (int y = 0; y < src->height; y++) {
180  memmove(dst->data[3] + (s->y + y) * dst->linesize[3] + s->x * s->max_step[3],
181  src->data[3] + y * src->linesize[3], src->width * s->max_step[3]);
182  }
183  }
184 
185  ret = ff_filter_frame(ctx->outputs[0], dst);
186  av_frame_free(&s->feed);
187  return ret;
188  }
189 
190  if (!s->feed || ctx->is_disabled) {
191  AVFrame *in = NULL;
192 
193  ret = ff_inlink_consume_frame(ctx->inputs[0], &in);
194  if (ret < 0)
195  return ret;
196 
197  if (ret > 0 && ctx->is_disabled)
198  return ff_filter_frame(ctx->outputs[0], in);
199 
200  if (ret > 0) {
201  AVFrame *frame;
202 
203  ret = av_fifo_write(s->fifo, &in, 1);
204  if (ret < 0) {
205  av_frame_free(&in);
206  return ret;
207  }
208 
209  frame = av_frame_clone(in);
210  if (!frame)
211  return AVERROR(ENOMEM);
212 
213  frame->width = s->w;
214  frame->height = s->h;
215 
216  frame->data[0] += s->y * frame->linesize[0];
217  frame->data[0] += s->x * s->max_step[0];
218 
219  for (int i = 1; i < 3; i ++) {
220  if (frame->data[i]) {
221  frame->data[i] += (s->y >> s->vsub) * frame->linesize[i];
222  frame->data[i] += (s->x >> s->hsub) * s->max_step[i];
223  }
224  }
225 
226  if (frame->data[3]) {
227  frame->data[3] += s->y * frame->linesize[3];
228  frame->data[3] += s->x * s->max_step[3];
229  }
230 
231  return ff_filter_frame(ctx->outputs[1], frame);
232  }
233  }
234 
235  if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
236  ff_outlink_set_status(ctx->outputs[0], status, pts);
237  ff_outlink_set_status(ctx->outputs[1], status, pts);
238  return 0;
239  }
240 
241  if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
242  ff_outlink_set_status(ctx->outputs[0], status, pts);
243  ff_outlink_set_status(ctx->outputs[1], status, pts);
244  return 0;
245  }
246 
247  if (!s->feed || ctx->is_disabled) {
248  if (ff_outlink_frame_wanted(ctx->outputs[0])) {
249  ff_inlink_request_frame(ctx->inputs[0]);
250  if (!ctx->is_disabled)
251  ff_inlink_request_frame(ctx->inputs[1]);
252  return 0;
253  }
254  }
255 
256  return FFERROR_NOT_READY;
257 }
258 
260 {
261  FeedbackContext *s = ctx->priv;
262 
263  s->fifo = av_fifo_alloc2(8, sizeof(AVFrame *), AV_FIFO_FLAG_AUTO_GROW);
264  if (!s->fifo)
265  return AVERROR(ENOMEM);
266 
267  return 0;
268 }
269 
271 {
272  FeedbackContext *s = ctx->priv;
273  if (s->fifo) {
274  size_t size = av_fifo_can_read(s->fifo);
275 
276  for (size_t n = 0; n < size; n++) {
277  AVFrame *frame = NULL;
278 
279  av_fifo_read(s->fifo, &frame, 1);
280 
282  }
283 
284  av_fifo_freep2(&s->fifo);
285  }
286 }
287 
288 static const AVFilterPad inputs[] = {
289  {
290  .name = "default",
291  .type = AVMEDIA_TYPE_VIDEO,
292  .config_props = config_input,
293  },
294  {
295  .name = "feedin",
296  .type = AVMEDIA_TYPE_VIDEO,
297  .config_props = config_input,
298  },
299 };
300 
301 static const AVFilterPad outputs[] = {
302  {
303  .name = "default",
304  .type = AVMEDIA_TYPE_VIDEO,
305  .config_props = config_output,
306  },
307  {
308  .name = "feedout",
309  .type = AVMEDIA_TYPE_VIDEO,
310  .config_props = config_output,
311  },
312 };
313 
314 #define OFFSET(x) offsetof(FeedbackContext, x)
315 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
316 #define TFLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM)
317 
318 static const AVOption feedback_options[] = {
319  { "x", "set top left crop position", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, TFLAGS },
320  { "y", "set top left crop position", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, TFLAGS },
321  { "w", "set crop size", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
322  { "h", "set crop size", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
323  { NULL }
324 };
325 
326 AVFILTER_DEFINE_CLASS(feedback);
327 
329  .name = "feedback",
330  .description = NULL_IF_CONFIG_SMALL("Apply feedback video filter."),
331  .priv_class = &feedback_class,
332  .priv_size = sizeof(FeedbackContext),
333  .activate = activate,
334  .init = init,
335  .uninit = uninit,
340  .process_command = ff_filter_process_command,
341 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:116
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_feedback.c:75
FeedbackContext::h
int h
Definition: vf_feedback.c:37
AVERROR
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
opt.h
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1062
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3170
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
ff_set_common_formats2
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition: formats.c:1007
int64_t
long long int64_t
Definition: coverity.c:34
inlink
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
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:429
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
video.h
FeedbackContext::hsub
int hsub
Definition: vf_feedback.c:40
formats.h
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1491
FF_FILTER_FORWARD_STATUS_BACK_ALL
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:447
fifo.h
av_fifo_write
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition: fifo.c:188
AV_PIX_FMT_FLAG_HWACCEL
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:128
activate
static int activate(AVFilterContext *ctx)
Definition: vf_feedback.c:119
TFLAGS
#define TFLAGS
Definition: vf_feedback.c:316
pts
static int64_t pts
Definition: transcode_aac.c:644
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
adjust_parameters
static void adjust_parameters(AVFilterContext *ctx, FeedbackContext *s)
Definition: vf_feedback.c:55
av_cold
#define av_cold
Definition: attributes.h:90
av_fifo_read
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition: fifo.c:240
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:424
ff_inlink_request_frame
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1594
s
#define s(width, name)
Definition: cbs_vp9.c:198
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: vf_feedback.c:109
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:597
AVPixFmtDescriptor::log2_chroma_w
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
ff_vf_feedback
const AVFilter ff_vf_feedback
Definition: vf_feedback.c:328
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
FeedbackContext::y
int y
Definition: vf_feedback.c:36
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:713
FeedbackContext::x
int x
Definition: vf_feedback.c:36
av_fifo_can_read
size_t av_fifo_can_read(const AVFifo *f)
Definition: fifo.c:87
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(feedback)
ff_inlink_acknowledge_status
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1438
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:111
feedback_options
static const AVOption feedback_options[]
Definition: vf_feedback.c:318
config_output
static int config_output(AVFilterLink *outlink)
Definition: vf_feedback.c:94
AVFifo
Definition: fifo.c:35
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
av_frame_copy
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:1003
size
int size
Definition: twinvq_data.h:10344
AV_PIX_FMT_FLAG_BITSTREAM
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:124
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:649
ff_filter_process_command
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:901
FeedbackContext
Definition: vf_feedback.c:33
OFFSET
#define OFFSET(x)
Definition: vf_feedback.c:314
FeedbackContext::feed
AVFrame * feed
Definition: vf_feedback.c:42
ff_formats_pixdesc_filter
AVFilterFormats * ff_formats_pixdesc_filter(unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition: formats.c:553
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
internal.h
inputs
static const AVFilterPad inputs[]
Definition: vf_feedback.c:288
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:239
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
FeedbackContext::max_step
int max_step[4]
Definition: vf_feedback.c:39
FLAGS
#define FLAGS
Definition: vf_feedback.c:315
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
frame
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
Definition: filter_design.txt:264
av_fifo_alloc2
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition: fifo.c:47
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_feedback.c:259
status
ov_status_e status
Definition: dnn_backend_openvino.c:100
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
FeedbackContext::fifo
AVFifo * fifo
Definition: vf_feedback.c:44
FeedbackContext::w
int w
Definition: vf_feedback.c:37
av_image_fill_max_pixsteps
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
Definition: imgutils.c:35
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
#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:190
imgutils.h
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_fifo_freep2
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition: fifo.c:286
h
h
Definition: vp9dsp_template.c:2070
ff_outlink_frame_wanted
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
outputs
static const AVFilterPad outputs[]
Definition: vf_feedback.c:301
adjust_pos
static void adjust_pos(AVFilterContext *ctx, FeedbackContext *s)
Definition: vf_feedback.c:47
AV_PIX_FMT_FLAG_PAL
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:120
AVPixFmtDescriptor::log2_chroma_h
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
src
#define src
Definition: vp8dsp.c:248
AV_FIFO_FLAG_AUTO_GROW
#define AV_FIFO_FLAG_AUTO_GROW
Automatically resize the FIFO on writes, so that the data fits.
Definition: fifo.h:63
FeedbackContext::vsub
int vsub
Definition: vf_feedback.c:40
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_feedback.c:270