FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_tiltandshift.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 107 129 82.9%
Functions: 7 7 100.0%
Branches: 49 82 59.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2014 Vittorio Giovara
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 /**
22 * @file vf_tiltandshift.c
23 * Simple time and space inverter.
24 */
25
26 #include <string.h>
27
28 #include "libavutil/avassert.h"
29 #include "libavutil/fifo.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34
35 #include "avfilter.h"
36 #include "filters.h"
37 #include "video.h"
38
39 enum PaddingOption {
40 TILT_NONE,
41 TILT_FRAME,
42 TILT_BLACK,
43 TILT_OPT_MAX,
44 };
45
46 typedef struct TiltandshiftContext {
47 const AVClass *class;
48
49 /* set when all input frames have been processed and we have to
50 * empty buffers, pad and then return */
51 int eof_recv;
52
53 /* live or static sliding */
54 int tilt;
55
56 /* initial or final actions to perform (pad/hold a frame/black/nothing) */
57 int start;
58 int end;
59
60 /* columns to hold or pad at the beginning or at the end (respectively) */
61 int hold;
62 int pad;
63
64 /* buffers for black columns */
65 uint8_t *black_buffers[4];
66 int black_linesizes[4];
67
68 /* FIFO containing all input frames */
69 AVFifo *input;
70 AVFrame *prev;
71
72 const AVPixFmtDescriptor *desc;
73 } TiltandshiftContext;
74
75 static const enum AVPixelFormat pix_fmts[] = {
76 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
77 AV_PIX_FMT_YUV410P,
78 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
79 AV_PIX_FMT_YUVJ440P,
80 AV_PIX_FMT_NONE
81 };
82
83 8 static av_cold int init(AVFilterContext *ctx)
84 {
85 8 TiltandshiftContext *s = ctx->priv;
86
87 8 s->input = av_fifo_alloc2(32, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!s->input)
89 return AVERROR(ENOMEM);
90
91 8 return 0;
92 }
93
94 8 static av_cold void uninit(AVFilterContext *ctx)
95 {
96 8 TiltandshiftContext *s = ctx->priv;
97
98
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s->input) {
99 AVFrame *frame;
100
101
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 while (av_fifo_read(s->input, &frame, 1) >= 0)
102 av_frame_free(&frame);
103
104 8 av_fifo_freep2(&s->input);
105 }
106
107 8 av_freep(&s->black_buffers);
108 8 }
109
110 4 static int config_props(AVFilterLink *outlink)
111 {
112 4 AVFilterContext *ctx = outlink->src;
113 4 TiltandshiftContext *s = ctx->priv;
114
115 4 outlink->w = ctx->inputs[0]->w;
116 4 outlink->h = ctx->inputs[0]->h;
117 4 outlink->format = ctx->inputs[0]->format;
118
119 // when we have to pad black or a frame at the start, skip navigating
120 // the list and use either the frame or black for the requested value
121
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (s->start != TILT_NONE && !s->hold)
122 s->hold = outlink->w;
123
124 // Init black buffers if we pad with black at the start or at the end.
125 // For the end, we always have to init on NONE and BLACK because we never
126 // know if there are going to be enough input frames to fill an output one.
127
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (s->start == TILT_BLACK || s->end != TILT_FRAME) {
128 int i, j, ret;
129 4 uint8_t black_data[] = { 0x10, 0x80, 0x80, 0x10 };
130 4 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!desc)
132 return AVERROR_BUG;
133
134
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (outlink->format == AV_PIX_FMT_YUVJ420P ||
135
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 outlink->format == AV_PIX_FMT_YUVJ422P ||
136
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 outlink->format == AV_PIX_FMT_YUVJ444P ||
137
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 outlink->format == AV_PIX_FMT_YUVJ440P ||
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 outlink->color_range == AVCOL_RANGE_JPEG)
139 black_data[0] = black_data[3] = 0;
140
141 4 ret = av_image_alloc(s->black_buffers, s->black_linesizes, 1,
142 4 outlink->h, outlink->format, 1);
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
144 return ret;
145
146
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (i = 0; i < FFMIN(desc->nb_components, 4); i++)
147
2/2
✓ Branch 0 taken 2736 times.
✓ Branch 1 taken 12 times.
2760 for (j = 0; j < (!i ? outlink->h
148
2/2
✓ Branch 0 taken 1156 times.
✓ Branch 1 taken 1592 times.
2748 : -((-outlink->h) >> desc->log2_chroma_h)); j++)
149 2736 memset(s->black_buffers[i] + j * s->black_linesizes[i],
150 2736 black_data[i], 1);
151
152 4 av_log(ctx, AV_LOG_VERBOSE, "Padding buffers initialized.\n");
153 }
154
155 4 s->desc = av_pix_fmt_desc_get(outlink->format);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!s->desc)
157 return AVERROR_BUG;
158
159 4 return 0;
160 }
161
162
163 70400 static void copy_column(AVFilterLink *outlink,
164 uint8_t *dst_data[4], int dst_linesizes[4],
165 const uint8_t *src_data[4], const int src_linesizes[4],
166 int ncol, int tilt)
167 {
168 70400 AVFilterContext *ctx = outlink->src;
169 70400 TiltandshiftContext *s = ctx->priv;
170 uint8_t *dst[4];
171 const uint8_t *src[4];
172
173 70400 dst[0] = dst_data[0] + ncol;
174 70400 dst[1] = dst_data[1] + (ncol >> s->desc->log2_chroma_w);
175 70400 dst[2] = dst_data[2] + (ncol >> s->desc->log2_chroma_w);
176
177
2/2
✓ Branch 0 taken 65300 times.
✓ Branch 1 taken 5100 times.
70400 if (!tilt)
178 65300 ncol = 0;
179 70400 src[0] = src_data[0] + ncol;
180 70400 src[1] = src_data[1] + (ncol >> s->desc->log2_chroma_w);
181 70400 src[2] = src_data[2] + (ncol >> s->desc->log2_chroma_w);
182
183 70400 av_image_copy(dst, dst_linesizes, src, src_linesizes, outlink->format, 1, outlink->h);
184 70400 }
185
186 200 static int output_frame(AVFilterLink *outlink)
187 {
188 200 TiltandshiftContext *s = outlink->src->priv;
189 AVFrame *head;
190 int ret;
191 int nb_buffered;
192
193 200 int ncol = 0;
194 200 AVFrame *dst = ff_get_video_buffer(outlink, outlink->w, outlink->h);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (!dst)
196 return AVERROR(ENOMEM);
197
198 // in case we have to do any initial black padding
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (s->start == TILT_BLACK) {
200 for ( ; ncol < s->hold; ncol++)
201 copy_column(outlink, dst->data, dst->linesize,
202 (const uint8_t **)s->black_buffers, s->black_linesizes,
203 ncol, 0);
204 }
205
206 200 nb_buffered = av_fifo_can_read(s->input);
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 av_assert0(nb_buffered);
208 // copy a column from each input frame
209
2/2
✓ Branch 0 taken 5100 times.
✓ Branch 1 taken 200 times.
5300 for (int in_idx = 0; ncol < nb_buffered; ncol++) {
210 AVFrame *src;
211
212 5100 av_fifo_peek(s->input, &src, 1, in_idx);
213
214 5100 copy_column(outlink, dst->data, dst->linesize,
215 5100 (const uint8_t **)src->data, src->linesize,
216 ncol, s->tilt);
217
218 // keep track of the last known frame in case we need it below
219 5100 s->prev = src;
220 // advance to the next frame unless we have to hold it
221
1/2
✓ Branch 0 taken 5100 times.
✗ Branch 1 not taken.
5100 if (s->hold <= ncol)
222 5100 in_idx++;
223 }
224
225 // pad any remaining space with black or last frame
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (s->end == TILT_FRAME) {
227 for ( ; ncol < outlink->w; ncol++)
228 copy_column(outlink, dst->data, dst->linesize,
229 (const uint8_t **)s->prev->data,
230 s->prev->linesize, ncol, 1);
231 } else { // TILT_BLACK and TILT_NONE
232
2/2
✓ Branch 0 taken 65300 times.
✓ Branch 1 taken 200 times.
65500 for ( ; ncol < outlink->w; ncol++)
233 65300 copy_column(outlink, dst->data, dst->linesize,
234 65300 (const uint8_t **)s->black_buffers, s->black_linesizes,
235 ncol, 0);
236 }
237
238 // set correct timestamps and props as long as there is proper input
239 200 av_fifo_read(s->input, &head, 1);
240 200 ret = av_frame_copy_props(dst, head);
241 200 av_frame_free(&head);
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (ret < 0) {
243 av_frame_free(&dst);
244 return ret;
245 }
246
247 // and it is safe to reduce the hold value (even if unused)
248 200 s->hold--;
249
250 // output
251 200 return ff_filter_frame(outlink, dst);
252 }
253
254 // This function just polls for new frames and queues them on a list
255 200 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
256 {
257 200 AVFilterLink *outlink = inlink->dst->outputs[0];
258 200 AVFilterContext *ctx = outlink->src;
259 200 TiltandshiftContext *s = inlink->dst->priv;
260 int ret;
261
262 200 ret = av_fifo_write(s->input, &frame, 1);
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (ret < 0) {
264 av_frame_free(&frame);
265 return ret;
266 }
267
268 // load up enough frames to fill a frame and keep the queue filled on subsequent
269 // calls, until we receive EOF, and then we either pad or end
270
1/2
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
200 if (!s->eof_recv &&
271
1/2
✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
200 av_fifo_can_read(s->input) < outlink->w - s->pad) {
272 200 av_log(ctx, AV_LOG_DEBUG,
273 "Not enough frames in the list (%zu/%d), waiting for more.\n",
274 200 av_fifo_can_read(s->input), outlink->w - s->pad);
275 200 return 0;
276 }
277
278 return output_frame(outlink);
279 }
280
281 404 static int request_frame(AVFilterLink *outlink)
282 {
283 404 AVFilterContext *ctx = outlink->src;
284 404 TiltandshiftContext *s = ctx->priv;
285 404 size_t nb_buffered = av_fifo_can_read(s->input);
286 int ret;
287
288 // signal job finished when list is empty or when padding is either
289 // limited or disabled and eof was received
290
3/4
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 400 times.
✗ Branch 3 not taken.
404 if ((nb_buffered <= 0 || nb_buffered == outlink->w - s->pad ||
291
3/4
✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 400 times.
404 s->end == TILT_NONE) && s->eof_recv) {
292 4 return AVERROR_EOF;
293 }
294
295 400 ret = ff_request_frame(ctx->inputs[0]);
296
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 396 times.
400 if (ret == AVERROR_EOF) {
297 4 s->eof_recv = 1;
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 396 times.
396 } else if (ret < 0) {
299 return ret;
300 }
301
302
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 396 times.
400 if (s->eof_recv) {
303
2/2
✓ Branch 1 taken 200 times.
✓ Branch 2 taken 4 times.
204 while (av_fifo_can_read(s->input)) {
304 200 av_log(ctx, AV_LOG_DEBUG, "Emptying buffers (%zu/%d).\n",
305 200 av_fifo_can_read(s->input), outlink->w - s->pad);
306 200 ret = output_frame(outlink);
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (ret < 0) {
308 return ret;
309 }
310 }
311 }
312
313 400 return 0;
314 }
315
316 #define OFFSET(x) offsetof(TiltandshiftContext, x)
317 #define V AV_OPT_FLAG_VIDEO_PARAM
318 static const AVOption tiltandshift_options[] = {
319 { "tilt", "Tilt the video horizontally while shifting", OFFSET(tilt), AV_OPT_TYPE_INT,
320 { .i64 = 1 }, 0, 1, .flags = V, .unit = "tilt" },
321
322 { "start", "Action at the start of input", OFFSET(start), AV_OPT_TYPE_INT,
323 { .i64 = TILT_NONE }, 0, TILT_OPT_MAX, .flags = V, .unit = "start" },
324 { "none", "Start immediately (default)", 0, AV_OPT_TYPE_CONST,
325 { .i64 = TILT_NONE }, INT_MIN, INT_MAX, .flags = V, .unit = "start" },
326 { "frame", "Use the first frames", 0, AV_OPT_TYPE_CONST,
327 { .i64 = TILT_FRAME }, INT_MIN, INT_MAX, .flags = V, .unit = "start" },
328 { "black", "Fill with black", 0, AV_OPT_TYPE_CONST,
329 { .i64 = TILT_BLACK }, INT_MIN, INT_MAX, .flags = V, .unit = "start" },
330
331 { "end", "Action at the end of input", OFFSET(end), AV_OPT_TYPE_INT,
332 { .i64 = TILT_NONE }, 0, TILT_OPT_MAX, .flags = V, .unit = "end" },
333 { "none", "Do not pad at the end (default)", 0, AV_OPT_TYPE_CONST,
334 { .i64 = TILT_NONE }, INT_MIN, INT_MAX, .flags = V, .unit = "end" },
335 { "frame", "Use the last frame", 0, AV_OPT_TYPE_CONST,
336 { .i64 = TILT_FRAME }, INT_MIN, INT_MAX, .flags = V, .unit = "end" },
337 { "black", "Fill with black", 0, AV_OPT_TYPE_CONST,
338 { .i64 = TILT_BLACK }, INT_MIN, INT_MAX, .flags = V, .unit = "end" },
339
340 { "hold", "Number of columns to hold at the start of the video", OFFSET(hold), AV_OPT_TYPE_INT,
341 { .i64 = 0 }, 0, INT_MAX, .flags = V, .unit = "hold" },
342 { "pad", "Number of columns to pad at the end of the video", OFFSET(pad), AV_OPT_TYPE_INT,
343 { .i64 = 0 }, 0, INT_MAX, .flags = V, .unit = "pad" },
344
345 { NULL },
346 };
347
348 AVFILTER_DEFINE_CLASS(tiltandshift);
349
350 static const AVFilterPad tiltandshift_inputs[] = {
351 {
352 .name = "in",
353 .type = AVMEDIA_TYPE_VIDEO,
354 .filter_frame = filter_frame,
355 },
356 };
357
358 static const AVFilterPad tiltandshift_outputs[] = {
359 {
360 .name = "out",
361 .type = AVMEDIA_TYPE_VIDEO,
362 .config_props = config_props,
363 .request_frame = request_frame,
364 },
365 };
366
367 const FFFilter ff_vf_tiltandshift = {
368 .p.name = "tiltandshift",
369 .p.description = NULL_IF_CONFIG_SMALL("Generate a tilt-and-shift'd video."),
370 .p.priv_class = &tiltandshift_class,
371 .priv_size = sizeof(TiltandshiftContext),
372 .init = init,
373 .uninit = uninit,
374 FILTER_INPUTS(tiltandshift_inputs),
375 FILTER_OUTPUTS(tiltandshift_outputs),
376 FILTER_PIXFMTS_ARRAY(pix_fmts),
377 };
378