FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_zoompan.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 0 157 0.0%
Branches: 0 52 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2013 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/eval.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29 #include "libswscale/swscale.h"
30
31 static const char *const var_names[] = {
32 "in_w", "iw",
33 "in_h", "ih",
34 "out_w", "ow",
35 "out_h", "oh",
36 "in",
37 "on",
38 "duration",
39 "pduration",
40 "in_time", "it",
41 "out_time", "time", "ot",
42 "frame",
43 "zoom",
44 "pzoom",
45 "x", "px",
46 "y", "py",
47 "a",
48 "sar",
49 "dar",
50 "hsub",
51 "vsub",
52 NULL
53 };
54
55 enum var_name {
56 VAR_IN_W, VAR_IW,
57 VAR_IN_H, VAR_IH,
58 VAR_OUT_W, VAR_OW,
59 VAR_OUT_H, VAR_OH,
60 VAR_IN,
61 VAR_ON,
62 VAR_DURATION,
63 VAR_PDURATION,
64 VAR_IN_TIME, VAR_IT,
65 VAR_TIME, VAR_OUT_TIME, VAR_OT,
66 VAR_FRAME,
67 VAR_ZOOM,
68 VAR_PZOOM,
69 VAR_X, VAR_PX,
70 VAR_Y, VAR_PY,
71 VAR_A,
72 VAR_SAR,
73 VAR_DAR,
74 VAR_HSUB,
75 VAR_VSUB,
76 VARS_NB
77 };
78
79 typedef struct ZPcontext {
80 const AVClass *class;
81 char *zoom_expr_str;
82 char *x_expr_str;
83 char *y_expr_str;
84 char *duration_expr_str;
85
86 AVExpr *zoom_expr, *x_expr, *y_expr;
87
88 int w, h;
89 double x, y;
90 double prev_zoom;
91 int prev_nb_frames;
92 struct SwsContext *sws;
93 int64_t frame_count;
94 const AVPixFmtDescriptor *desc;
95 AVFrame *in;
96 double var_values[VARS_NB];
97 int nb_frames;
98 int current_frame;
99 int finished;
100 AVRational framerate;
101 } ZPContext;
102
103 #define OFFSET(x) offsetof(ZPContext, x)
104 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
105 static const AVOption zoompan_options[] = {
106 { "zoom", "set the zoom expression", OFFSET(zoom_expr_str), AV_OPT_TYPE_STRING, {.str = "1" }, .flags = FLAGS },
107 { "z", "set the zoom expression", OFFSET(zoom_expr_str), AV_OPT_TYPE_STRING, {.str = "1" }, .flags = FLAGS },
108 { "x", "set the x expression", OFFSET(x_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags = FLAGS },
109 { "y", "set the y expression", OFFSET(y_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags = FLAGS },
110 { "d", "set the duration expression", OFFSET(duration_expr_str), AV_OPT_TYPE_STRING, {.str="90"}, .flags = FLAGS },
111 { "s", "set the output image size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, .flags = FLAGS },
112 { "fps", "set the output framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, .flags = FLAGS },
113 { NULL }
114 };
115
116 AVFILTER_DEFINE_CLASS(zoompan);
117
118 static av_cold int init(AVFilterContext *ctx)
119 {
120 ZPContext *s = ctx->priv;
121
122 s->prev_zoom = 1;
123 return 0;
124 }
125
126 static int config_output(AVFilterLink *outlink)
127 {
128 AVFilterContext *ctx = outlink->src;
129 ZPContext *s = ctx->priv;
130 int ret;
131
132 outlink->w = s->w;
133 outlink->h = s->h;
134 outlink->time_base = av_inv_q(s->framerate);
135 outlink->frame_rate = s->framerate;
136 s->desc = av_pix_fmt_desc_get(outlink->format);
137 s->finished = 1;
138
139 ret = av_expr_parse(&s->zoom_expr, s->zoom_expr_str, var_names, NULL, NULL, NULL, NULL, 0, ctx);
140 if (ret < 0)
141 return ret;
142
143 ret = av_expr_parse(&s->x_expr, s->x_expr_str, var_names, NULL, NULL, NULL, NULL, 0, ctx);
144 if (ret < 0)
145 return ret;
146
147 ret = av_expr_parse(&s->y_expr, s->y_expr_str, var_names, NULL, NULL, NULL, NULL, 0, ctx);
148 if (ret < 0)
149 return ret;
150
151 return 0;
152 }
153
154 static int output_single_frame(AVFilterContext *ctx, AVFrame *in, double *var_values, int i,
155 double *zoom, double *dx, double *dy)
156 {
157 ZPContext *s = ctx->priv;
158 AVFilterLink *outlink = ctx->outputs[0];
159 AVFilterLink *inlink = ctx->inputs[0];
160 int64_t pts = s->frame_count;
161 int k, x, y, w, h, ret = 0;
162 uint8_t *input[4];
163 int px[4], py[4];
164 AVFrame *out;
165
166 var_values[VAR_PX] = s->x;
167 var_values[VAR_PY] = s->y;
168 var_values[VAR_PZOOM] = s->prev_zoom;
169 var_values[VAR_PDURATION] = s->prev_nb_frames;
170 var_values[VAR_IN_TIME] = var_values[VAR_IT] = in->pts == AV_NOPTS_VALUE ?
171 NAN : in->pts * av_q2d(inlink->time_base);
172 var_values[VAR_OUT_TIME] = pts * av_q2d(outlink->time_base);
173 var_values[VAR_TIME] = var_values[VAR_OT] = var_values[VAR_OUT_TIME];
174 var_values[VAR_FRAME] = i;
175 var_values[VAR_ON] = outlink->frame_count_in;
176
177 *zoom = av_expr_eval(s->zoom_expr, var_values, NULL);
178
179 *zoom = av_clipd(*zoom, 1, 10);
180 var_values[VAR_ZOOM] = *zoom;
181 w = in->width * (1.0 / *zoom);
182 h = in->height * (1.0 / *zoom);
183
184 *dx = av_expr_eval(s->x_expr, var_values, NULL);
185
186 x = *dx = av_clipd(*dx, 0, FFMAX(in->width - w, 0));
187 var_values[VAR_X] = *dx;
188 x &= ~((1 << s->desc->log2_chroma_w) - 1);
189
190 *dy = av_expr_eval(s->y_expr, var_values, NULL);
191
192 y = *dy = av_clipd(*dy, 0, FFMAX(in->height - h, 0));
193 var_values[VAR_Y] = *dy;
194 y &= ~((1 << s->desc->log2_chroma_h) - 1);
195
196 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
197 if (!out) {
198 ret = AVERROR(ENOMEM);
199 return ret;
200 }
201
202 px[1] = px[2] = AV_CEIL_RSHIFT(x, s->desc->log2_chroma_w);
203 px[0] = px[3] = x;
204
205 py[1] = py[2] = AV_CEIL_RSHIFT(y, s->desc->log2_chroma_h);
206 py[0] = py[3] = y;
207
208 s->sws = sws_alloc_context();
209 if (!s->sws) {
210 ret = AVERROR(ENOMEM);
211 goto error;
212 }
213
214 for (k = 0; in->data[k]; k++)
215 input[k] = in->data[k] + py[k] * in->linesize[k] + px[k];
216
217 av_opt_set_int(s->sws, "srcw", w, 0);
218 av_opt_set_int(s->sws, "srch", h, 0);
219 av_opt_set_int(s->sws, "src_format", in->format, 0);
220 av_opt_set_int(s->sws, "dstw", outlink->w, 0);
221 av_opt_set_int(s->sws, "dsth", outlink->h, 0);
222 av_opt_set_int(s->sws, "dst_format", outlink->format, 0);
223 av_opt_set_int(s->sws, "sws_flags", SWS_BICUBIC, 0);
224
225 if ((ret = sws_init_context(s->sws, NULL, NULL)) < 0)
226 goto error;
227
228 sws_scale(s->sws, (const uint8_t *const *)&input, in->linesize, 0, h, out->data, out->linesize);
229
230 out->pts = pts;
231 s->frame_count++;
232
233 ret = ff_filter_frame(outlink, out);
234 sws_freeContext(s->sws);
235 s->sws = NULL;
236 s->current_frame++;
237
238 if (s->current_frame >= s->nb_frames) {
239 if (*dx != -1)
240 s->x = *dx;
241 if (*dy != -1)
242 s->y = *dy;
243 if (*zoom != -1)
244 s->prev_zoom = *zoom;
245 s->prev_nb_frames = s->nb_frames;
246 s->nb_frames = 0;
247 s->current_frame = 0;
248 av_frame_free(&s->in);
249 s->finished = 1;
250 }
251 return ret;
252 error:
253 sws_freeContext(s->sws);
254 s->sws = NULL;
255 av_frame_free(&out);
256 return ret;
257 }
258
259 static int activate(AVFilterContext *ctx)
260 {
261 ZPContext *s = ctx->priv;
262 AVFilterLink *inlink = ctx->inputs[0];
263 AVFilterLink *outlink = ctx->outputs[0];
264 int status, ret = 0;
265 int64_t pts;
266
267 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
268
269 if (s->in && ff_outlink_frame_wanted(outlink)) {
270 double zoom = -1, dx = -1, dy = -1;
271
272 ret = output_single_frame(ctx, s->in, s->var_values, s->current_frame,
273 &zoom, &dx, &dy);
274 if (ret < 0)
275 return ret;
276 }
277
278 if (!s->in && (ret = ff_inlink_consume_frame(inlink, &s->in)) > 0) {
279 double zoom = -1, dx = -1, dy = -1, nb_frames;
280
281 s->finished = 0;
282 s->var_values[VAR_IN_W] = s->var_values[VAR_IW] = s->in->width;
283 s->var_values[VAR_IN_H] = s->var_values[VAR_IH] = s->in->height;
284 s->var_values[VAR_OUT_W] = s->var_values[VAR_OW] = s->w;
285 s->var_values[VAR_OUT_H] = s->var_values[VAR_OH] = s->h;
286 s->var_values[VAR_IN] = inlink->frame_count_out - 1;
287 s->var_values[VAR_ON] = outlink->frame_count_in;
288 s->var_values[VAR_PX] = s->x;
289 s->var_values[VAR_PY] = s->y;
290 s->var_values[VAR_X] = 0;
291 s->var_values[VAR_Y] = 0;
292 s->var_values[VAR_PZOOM] = s->prev_zoom;
293 s->var_values[VAR_ZOOM] = 1;
294 s->var_values[VAR_PDURATION] = s->prev_nb_frames;
295 s->var_values[VAR_A] = (double) s->in->width / s->in->height;
296 s->var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
297 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
298 s->var_values[VAR_DAR] = s->var_values[VAR_A] * s->var_values[VAR_SAR];
299 s->var_values[VAR_HSUB] = 1 << s->desc->log2_chroma_w;
300 s->var_values[VAR_VSUB] = 1 << s->desc->log2_chroma_h;
301
302 if ((ret = av_expr_parse_and_eval(&nb_frames, s->duration_expr_str,
303 var_names, s->var_values,
304 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
305 av_frame_free(&s->in);
306 return ret;
307 }
308
309 s->var_values[VAR_DURATION] = s->nb_frames = nb_frames;
310
311 ret = output_single_frame(ctx, s->in, s->var_values, s->current_frame,
312 &zoom, &dx, &dy);
313 if (ret < 0)
314 return ret;
315 }
316 if (ret < 0) {
317 return ret;
318 } else if (s->finished && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
319 ff_outlink_set_status(outlink, status, pts);
320 return 0;
321 } else {
322 if (ff_outlink_frame_wanted(outlink) && s->finished)
323 ff_inlink_request_frame(inlink);
324 return 0;
325 }
326 }
327
328 static const enum AVPixelFormat pix_fmts[] = {
329 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
330 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
331 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
332 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P,
333 AV_PIX_FMT_YUVA420P,
334 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
335 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
336 AV_PIX_FMT_YUVJ411P,
337 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
338 AV_PIX_FMT_GRAY8,
339 AV_PIX_FMT_NONE
340 };
341
342 static av_cold void uninit(AVFilterContext *ctx)
343 {
344 ZPContext *s = ctx->priv;
345
346 sws_freeContext(s->sws);
347 s->sws = NULL;
348 av_expr_free(s->x_expr);
349 av_expr_free(s->y_expr);
350 av_expr_free(s->zoom_expr);
351 av_frame_free(&s->in);
352 }
353
354 static const AVFilterPad inputs[] = {
355 {
356 .name = "default",
357 .type = AVMEDIA_TYPE_VIDEO,
358 },
359 };
360
361 static const AVFilterPad outputs[] = {
362 {
363 .name = "default",
364 .type = AVMEDIA_TYPE_VIDEO,
365 .config_props = config_output,
366 },
367 };
368
369 const AVFilter ff_vf_zoompan = {
370 .name = "zoompan",
371 .description = NULL_IF_CONFIG_SMALL("Apply Zoom & Pan effect."),
372 .priv_size = sizeof(ZPContext),
373 .priv_class = &zoompan_class,
374 .init = init,
375 .uninit = uninit,
376 .activate = activate,
377 FILTER_INPUTS(inputs),
378 FILTER_OUTPUTS(outputs),
379 FILTER_PIXFMTS_ARRAY(pix_fmts),
380 };
381