2 * Copyright (c) 2013 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/eval.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/pixdesc.h"
29 #include "libswscale/swscale.h"
31 static const char *const var_names[] = {
77 typedef struct ZPcontext {
82 char *duration_expr_str;
87 struct SwsContext *sws;
89 const AVPixFmtDescriptor *desc;
91 double var_values[VARS_NB];
98 #define OFFSET(x) offsetof(ZPContext, x)
99 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
100 static const AVOption zoompan_options[] = {
101 { "zoom", "set the zoom expression", OFFSET(zoom_expr_str), AV_OPT_TYPE_STRING, {.str = "1" }, .flags = FLAGS },
102 { "z", "set the zoom expression", OFFSET(zoom_expr_str), AV_OPT_TYPE_STRING, {.str = "1" }, .flags = FLAGS },
103 { "x", "set the x expression", OFFSET(x_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags = FLAGS },
104 { "y", "set the y expression", OFFSET(y_expr_str), AV_OPT_TYPE_STRING, {.str="0"}, .flags = FLAGS },
105 { "d", "set the duration expression", OFFSET(duration_expr_str), AV_OPT_TYPE_STRING, {.str="90"}, .flags = FLAGS },
106 { "s", "set the output image size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, .flags = FLAGS },
107 { "fps", "set the output framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, .flags = FLAGS },
111 AVFILTER_DEFINE_CLASS(zoompan);
113 static av_cold int init(AVFilterContext *ctx)
115 ZPContext *s = ctx->priv;
121 static int config_output(AVFilterLink *outlink)
123 AVFilterContext *ctx = outlink->src;
124 ZPContext *s = ctx->priv;
128 outlink->time_base = av_inv_q(s->framerate);
129 outlink->frame_rate = s->framerate;
130 s->desc = av_pix_fmt_desc_get(outlink->format);
135 static int output_single_frame(AVFilterContext *ctx, AVFrame *in, double *var_values, int i,
136 double *zoom, double *dx, double *dy)
138 ZPContext *s = ctx->priv;
139 AVFilterLink *outlink = ctx->outputs[0];
140 int64_t pts = s->frame_count;
141 int k, x, y, w, h, ret = 0;
146 var_values[VAR_TIME] = pts * av_q2d(outlink->time_base);
147 var_values[VAR_FRAME] = i;
148 var_values[VAR_ON] = outlink->frame_count + 1;
149 if ((ret = av_expr_parse_and_eval(zoom, s->zoom_expr_str,
150 var_names, var_values,
151 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
154 *zoom = av_clipd(*zoom, 1, 10);
155 var_values[VAR_ZOOM] = *zoom;
156 w = in->width * (1.0 / *zoom);
157 h = in->height * (1.0 / *zoom);
159 if ((ret = av_expr_parse_and_eval(dx, s->x_expr_str,
160 var_names, var_values,
161 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
163 x = *dx = av_clipd(*dx, 0, FFMAX(in->width - w, 0));
164 var_values[VAR_X] = *dx;
165 x &= ~((1 << s->desc->log2_chroma_w) - 1);
167 if ((ret = av_expr_parse_and_eval(dy, s->y_expr_str,
168 var_names, var_values,
169 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
171 y = *dy = av_clipd(*dy, 0, FFMAX(in->height - h, 0));
172 var_values[VAR_Y] = *dy;
173 y &= ~((1 << s->desc->log2_chroma_h) - 1);
175 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
177 ret = AVERROR(ENOMEM);
181 px[1] = px[2] = AV_CEIL_RSHIFT(x, s->desc->log2_chroma_w);
184 py[1] = py[2] = AV_CEIL_RSHIFT(y, s->desc->log2_chroma_h);
187 s->sws = sws_alloc_context();
189 ret = AVERROR(ENOMEM);
193 for (k = 0; in->data[k]; k++)
194 input[k] = in->data[k] + py[k] * in->linesize[k] + px[k];
196 av_opt_set_int(s->sws, "srcw", w, 0);
197 av_opt_set_int(s->sws, "srch", h, 0);
198 av_opt_set_int(s->sws, "src_format", in->format, 0);
199 av_opt_set_int(s->sws, "dstw", outlink->w, 0);
200 av_opt_set_int(s->sws, "dsth", outlink->h, 0);
201 av_opt_set_int(s->sws, "dst_format", outlink->format, 0);
202 av_opt_set_int(s->sws, "sws_flags", SWS_BICUBIC, 0);
204 if ((ret = sws_init_context(s->sws, NULL, NULL)) < 0)
207 sws_scale(s->sws, (const uint8_t *const *)&input, in->linesize, 0, h, out->data, out->linesize);
212 ret = ff_filter_frame(outlink, out);
213 sws_freeContext(s->sws);
219 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
221 AVFilterContext *ctx = inlink->dst;
222 AVFilterLink *outlink = ctx->outputs[0];
223 ZPContext *s = ctx->priv;
227 av_assert0(s->in == NULL);
230 s->var_values[VAR_IN_W] = s->var_values[VAR_IW] = in->width;
231 s->var_values[VAR_IN_H] = s->var_values[VAR_IH] = in->height;
232 s->var_values[VAR_OUT_W] = s->var_values[VAR_OW] = s->w;
233 s->var_values[VAR_OUT_H] = s->var_values[VAR_OH] = s->h;
234 s->var_values[VAR_IN] = inlink->frame_count + 1;
235 s->var_values[VAR_ON] = outlink->frame_count + 1;
236 s->var_values[VAR_PX] = s->x;
237 s->var_values[VAR_PY] = s->y;
238 s->var_values[VAR_X] = 0;
239 s->var_values[VAR_Y] = 0;
240 s->var_values[VAR_PZOOM] = s->prev_zoom;
241 s->var_values[VAR_ZOOM] = 1;
242 s->var_values[VAR_PDURATION] = s->prev_nb_frames;
243 s->var_values[VAR_A] = (double) in->width / in->height;
244 s->var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
245 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
246 s->var_values[VAR_DAR] = s->var_values[VAR_A] * s->var_values[VAR_SAR];
247 s->var_values[VAR_HSUB] = 1 << s->desc->log2_chroma_w;
248 s->var_values[VAR_VSUB] = 1 << s->desc->log2_chroma_h;
250 if ((ret = av_expr_parse_and_eval(&nb_frames, s->duration_expr_str,
251 var_names, s->var_values,
252 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
257 s->var_values[VAR_DURATION] = s->nb_frames = nb_frames;
263 static int request_frame(AVFilterLink *outlink)
265 AVFilterContext *ctx = outlink->src;
266 ZPContext *s = ctx->priv;
268 double zoom=1, dx=0, dy=0;
272 ret = output_single_frame(ctx, in, s->var_values, s->current_frame,
278 if (s->current_frame >= s->nb_frames) {
282 s->prev_nb_frames = s->nb_frames;
284 s->current_frame = 0;
285 av_frame_free(&s->in);
287 ret = ff_request_frame(ctx->inputs[0]);
291 sws_freeContext(s->sws);
297 static int poll_frame(AVFilterLink *link)
299 ZPContext *s = link->src->priv;
300 return s->nb_frames - s->current_frame;
303 static int query_formats(AVFilterContext *ctx)
305 static const enum AVPixelFormat pix_fmts[] = {
306 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
307 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
308 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
309 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P,
311 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
312 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
314 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
319 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
321 return AVERROR(ENOMEM);
322 return ff_set_common_formats(ctx, fmts_list);
325 static av_cold void uninit(AVFilterContext *ctx)
327 ZPContext *s = ctx->priv;
329 sws_freeContext(s->sws);
333 static const AVFilterPad inputs[] = {
336 .type = AVMEDIA_TYPE_VIDEO,
337 .filter_frame = filter_frame,
343 static const AVFilterPad outputs[] = {
346 .type = AVMEDIA_TYPE_VIDEO,
347 .config_props = config_output,
348 .poll_frame = poll_frame,
349 .request_frame = request_frame,
354 AVFilter ff_vf_zoompan = {
356 .description = NULL_IF_CONFIG_SMALL("Apply Zoom & Pan effect."),
357 .priv_size = sizeof(ZPContext),
358 .priv_class = &zoompan_class,
361 .query_formats = query_formats,
364 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,