FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_feedback.c
Date: 2024-03-29 01:21:52
Exec Total Coverage
Lines: 0 144 0.0%
Functions: 0 8 0.0%
Branches: 0 90 0.0%

Line Branch Exec Source
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 "internal.h"
32 #include "video.h"
33
34 typedef struct FeedbackContext {
35 const AVClass *class;
36
37 int x, y;
38 int w, h;
39
40 int max_step[4];
41 int hsub, vsub;
42
43 AVFrame *feed;
44
45 AVFifo *fifo;
46 } FeedbackContext;
47
48 static void adjust_pos(AVFilterContext *ctx, FeedbackContext *s)
49 {
50 if (s->x + s->w > ctx->inputs[0]->w)
51 s->x = ctx->inputs[0]->w - s->w;
52 if (s->y + s->h > ctx->inputs[0]->h)
53 s->y = ctx->inputs[0]->h - s->h;
54 }
55
56 static void adjust_parameters(AVFilterContext *ctx, FeedbackContext *s)
57 {
58 if (s->x >= ctx->inputs[0]->w)
59 s->x = 0;
60 if (s->y >= ctx->inputs[0]->h)
61 s->y = 0;
62
63 if (s->w <= 0)
64 s->w = ctx->inputs[0]->w - s->x;
65 if (s->h <= 0)
66 s->h = ctx->inputs[0]->h - s->y;
67
68 if (s->w > ctx->inputs[0]->w)
69 s->w = ctx->inputs[0]->w;
70 if (s->h > ctx->inputs[0]->h)
71 s->h = ctx->inputs[0]->h;
72
73 adjust_pos(ctx, s);
74 }
75
76 static int config_input(AVFilterLink *inlink)
77 {
78 AVFilterContext *ctx = inlink->dst;
79 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
80 FeedbackContext *s = ctx->priv;
81
82 s->hsub = pix_desc->log2_chroma_w;
83 s->vsub = pix_desc->log2_chroma_h;
84
85 av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
86
87 adjust_parameters(ctx, s);
88
89 ctx->inputs[1]->w = s->w;
90 ctx->inputs[1]->h = s->h;
91
92 return 0;
93 }
94
95 static int config_output(AVFilterLink *outlink)
96 {
97 AVFilterContext *ctx = outlink->src;
98 FeedbackContext *s = ctx->priv;
99
100 adjust_parameters(ctx, s);
101
102 ctx->outputs[0]->w = ctx->inputs[0]->w;
103 ctx->outputs[0]->h = ctx->inputs[0]->h;
104 ctx->outputs[1]->w = s->w;
105 ctx->outputs[1]->h = s->h;
106
107 return 0;
108 }
109
110 static int query_formats(AVFilterContext *ctx)
111 {
112 return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, AV_PIX_FMT_FLAG_BITSTREAM |
113 AV_PIX_FMT_FLAG_HWACCEL |
114 AV_PIX_FMT_FLAG_PAL));
115 }
116
117 static int activate(AVFilterContext *ctx)
118 {
119 FeedbackContext *s = ctx->priv;
120 int status, ret;
121 int64_t pts;
122
123 adjust_pos(ctx, s);
124
125 for (int i = 0; i < ctx->nb_outputs; i++)
126 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[i], ctx);
127
128 if (!s->feed) {
129 ret = ff_inlink_consume_frame(ctx->inputs[1], &s->feed);
130 if (ret < 0)
131 return ret;
132 }
133
134 if (s->feed && av_fifo_can_read(s->fifo)) {
135 AVFrame *src = s->feed;
136 AVFrame *dst = NULL;
137
138 av_fifo_read(s->fifo, &dst, 1);
139 if (!dst)
140 return AVERROR_BUG;
141
142 if (!av_frame_is_writable(dst)) {
143 AVFrame *tmp = ff_get_video_buffer(ctx->outputs[0], ctx->outputs[0]->w, ctx->outputs[0]->h);
144
145 if (!tmp) {
146 av_frame_free(&dst);
147 return AVERROR(ENOMEM);
148 }
149
150 ret = av_frame_copy(tmp, dst);
151 if (ret < 0) {
152 av_frame_free(&dst);
153 av_frame_free(&tmp);
154 return ret;
155 }
156
157 av_frame_copy_props(tmp, dst);
158 av_frame_free(&dst);
159 dst = tmp;
160 }
161
162 for (int y = 0; y < src->height; y++) {
163 memmove(dst->data[0] + (s->y + y) * dst->linesize[0] + s->x * s->max_step[0],
164 src->data[0] + y * src->linesize[0], src->width * s->max_step[0]);
165 }
166
167 for (int i = 1; i < 3; i++) {
168 if (dst->data[i]) {
169 for (int y = 0; y < src->height; y++) {
170 memmove(dst->data[i] + ((s->y + y) >> s->vsub) * dst->linesize[i] + (s->x >> s->hsub) * s->max_step[i],
171 src->data[i] + (y >> s->vsub) * src->linesize[i], (src->width >> s->hsub) * s->max_step[i]);
172 }
173 }
174 }
175
176 if (dst->data[3]) {
177 for (int y = 0; y < src->height; y++) {
178 memmove(dst->data[3] + (s->y + y) * dst->linesize[3] + s->x * s->max_step[3],
179 src->data[3] + y * src->linesize[3], src->width * s->max_step[3]);
180 }
181 }
182
183 ret = ff_filter_frame(ctx->outputs[0], dst);
184 av_frame_free(&s->feed);
185 return ret;
186 }
187
188 if (!s->feed || ctx->is_disabled) {
189 AVFrame *in = NULL;
190
191 ret = ff_inlink_consume_frame(ctx->inputs[0], &in);
192 if (ret < 0)
193 return ret;
194
195 if (ret > 0 && ctx->is_disabled)
196 return ff_filter_frame(ctx->outputs[0], in);
197
198 if (ret > 0) {
199 AVFrame *frame;
200
201 ret = av_fifo_write(s->fifo, &in, 1);
202 if (ret < 0) {
203 av_frame_free(&in);
204 return ret;
205 }
206
207 frame = av_frame_clone(in);
208 if (!frame)
209 return AVERROR(ENOMEM);
210
211 frame->width = s->w;
212 frame->height = s->h;
213
214 frame->data[0] += s->y * frame->linesize[0];
215 frame->data[0] += s->x * s->max_step[0];
216
217 for (int i = 1; i < 3; i ++) {
218 if (frame->data[i]) {
219 frame->data[i] += (s->y >> s->vsub) * frame->linesize[i];
220 frame->data[i] += (s->x >> s->hsub) * s->max_step[i];
221 }
222 }
223
224 if (frame->data[3]) {
225 frame->data[3] += s->y * frame->linesize[3];
226 frame->data[3] += s->x * s->max_step[3];
227 }
228
229 return ff_filter_frame(ctx->outputs[1], frame);
230 }
231 }
232
233 if (ff_inlink_acknowledge_status(ctx->inputs[0], &status, &pts)) {
234 ff_outlink_set_status(ctx->outputs[0], status, pts);
235 ff_outlink_set_status(ctx->outputs[1], status, pts);
236 return 0;
237 }
238
239 if (ff_inlink_acknowledge_status(ctx->inputs[1], &status, &pts)) {
240 ff_outlink_set_status(ctx->outputs[0], status, pts);
241 ff_outlink_set_status(ctx->outputs[1], status, pts);
242 return 0;
243 }
244
245 if (!s->feed || ctx->is_disabled) {
246 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
247 ff_inlink_request_frame(ctx->inputs[0]);
248 if (!ctx->is_disabled)
249 ff_inlink_request_frame(ctx->inputs[1]);
250 return 0;
251 }
252 }
253
254 return FFERROR_NOT_READY;
255 }
256
257 static av_cold int init(AVFilterContext *ctx)
258 {
259 FeedbackContext *s = ctx->priv;
260
261 s->fifo = av_fifo_alloc2(8, sizeof(AVFrame *), AV_FIFO_FLAG_AUTO_GROW);
262 if (!s->fifo)
263 return AVERROR(ENOMEM);
264
265 return 0;
266 }
267
268 static av_cold void uninit(AVFilterContext *ctx)
269 {
270 FeedbackContext *s = ctx->priv;
271 if (s->fifo) {
272 size_t size = av_fifo_can_read(s->fifo);
273
274 for (size_t n = 0; n < size; n++) {
275 AVFrame *frame = NULL;
276
277 av_fifo_read(s->fifo, &frame, 1);
278
279 av_frame_free(&frame);
280 }
281
282 av_fifo_freep2(&s->fifo);
283 }
284 }
285
286 static const AVFilterPad inputs[] = {
287 {
288 .name = "default",
289 .type = AVMEDIA_TYPE_VIDEO,
290 .config_props = config_input,
291 },
292 {
293 .name = "feedin",
294 .type = AVMEDIA_TYPE_VIDEO,
295 .config_props = config_input,
296 },
297 };
298
299 static const AVFilterPad outputs[] = {
300 {
301 .name = "default",
302 .type = AVMEDIA_TYPE_VIDEO,
303 .config_props = config_output,
304 },
305 {
306 .name = "feedout",
307 .type = AVMEDIA_TYPE_VIDEO,
308 .config_props = config_output,
309 },
310 };
311
312 #define OFFSET(x) offsetof(FeedbackContext, x)
313 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
314 #define TFLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM)
315
316 static const AVOption feedback_options[] = {
317 { "x", "set top left crop position", OFFSET(x), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, TFLAGS },
318 { "y", "set top left crop position", OFFSET(y), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, TFLAGS },
319 { "w", "set crop size", OFFSET(w), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
320 { "h", "set crop size", OFFSET(h), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
321 { NULL }
322 };
323
324 AVFILTER_DEFINE_CLASS(feedback);
325
326 const AVFilter ff_vf_feedback = {
327 .name = "feedback",
328 .description = NULL_IF_CONFIG_SMALL("Apply feedback video filter."),
329 .priv_class = &feedback_class,
330 .priv_size = sizeof(FeedbackContext),
331 .activate = activate,
332 .init = init,
333 .uninit = uninit,
334 FILTER_INPUTS(inputs),
335 FILTER_OUTPUTS(outputs),
336 FILTER_QUERY_FUNC(query_formats),
337 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
338 .process_command = ff_filter_process_command,
339 };
340