2 * Copyright (c) 2007 Bobby Bingham
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/avstring.h"
28 #include "libavutil/eval.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/pixdesc.h"
31 #include "libswscale/swscale.h"
33 static const char *var_names[] = {
62 struct SwsContext *sws; ///< software scaler context
65 * New dimensions. Special values are:
66 * 0 = original width/height
67 * -1 = keep original aspect
70 unsigned int flags; ///sws flags
72 int hsub, vsub; ///< chroma subsampling
73 int slice_y; ///< top of current output slice
74 int input_is_pal; ///< set to 1 if the input format is paletted
76 char w_expr[256]; ///< width expression string
77 char h_expr[256]; ///< height expression string
80 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
82 ScaleContext *scale = ctx->priv;
85 av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
86 av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
88 scale->flags = SWS_BILINEAR;
90 sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
91 p = strstr(args,"flags=");
92 if (p) scale->flags = strtoul(p+6, NULL, 0);
98 static av_cold void uninit(AVFilterContext *ctx)
100 ScaleContext *scale = ctx->priv;
101 sws_freeContext(scale->sws);
105 static int query_formats(AVFilterContext *ctx)
107 AVFilterFormats *formats;
108 enum PixelFormat pix_fmt;
111 if (ctx->inputs[0]) {
113 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
114 if ( sws_isSupportedInput(pix_fmt)
115 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
116 avfilter_formats_unref(&formats);
119 avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
121 if (ctx->outputs[0]) {
123 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
124 if ( sws_isSupportedOutput(pix_fmt)
125 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
126 avfilter_formats_unref(&formats);
129 avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
135 static int config_props(AVFilterLink *outlink)
137 AVFilterContext *ctx = outlink->src;
138 AVFilterLink *inlink = outlink->src->inputs[0];
139 ScaleContext *scale = ctx->priv;
141 double var_values[VARS_NB], res;
145 var_values[VAR_PI] = M_PI;
146 var_values[VAR_PHI] = M_PHI;
147 var_values[VAR_E] = M_E;
148 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
149 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
150 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
151 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
152 var_values[VAR_A] = (float) inlink->w / inlink->h;
153 var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
154 var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
156 /* evaluate width and height */
157 av_expr_parse_and_eval(&res, (expr = scale->w_expr),
158 var_names, var_values,
159 NULL, NULL, NULL, NULL, NULL, 0, ctx);
160 scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
161 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
162 var_names, var_values,
163 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
165 scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
166 /* evaluate again the width, as it may depend on the output height */
167 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
168 var_names, var_values,
169 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
176 /* sanity check params */
177 if (w < -1 || h < -1) {
178 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
179 return AVERROR(EINVAL);
181 if (w == -1 && h == -1)
182 scale->w = scale->h = 0;
189 w = av_rescale(h, inlink->w, inlink->h);
191 h = av_rescale(w, inlink->h, inlink->w);
193 if (w > INT_MAX || h > INT_MAX ||
194 (h * inlink->w) > INT_MAX ||
195 (w * inlink->h) > INT_MAX)
196 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
201 /* TODO: make algorithm configurable */
202 av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
203 inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
204 outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
207 scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL;
210 sws_freeContext(scale->sws);
211 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
212 outlink->w, outlink->h, outlink->format,
213 scale->flags, NULL, NULL, NULL);
215 return AVERROR(EINVAL);
220 av_log(NULL, AV_LOG_ERROR,
221 "Error when evaluating the expression '%s'\n", expr);
225 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
227 ScaleContext *scale = link->dst->priv;
228 AVFilterLink *outlink = link->dst->outputs[0];
229 AVFilterBufferRef *outpicref;
231 scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
232 scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
234 outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
235 avfilter_copy_buffer_ref_props(outpicref, picref);
236 outpicref->video->w = outlink->w;
237 outpicref->video->h = outlink->h;
239 outlink->out_buf = outpicref;
241 av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
242 (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
243 (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
247 avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
250 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
252 ScaleContext *scale = link->dst->priv;
254 AVFilterBufferRef *cur_pic = link->cur_buf;
255 const uint8_t *data[4];
257 if (scale->slice_y == 0 && slice_dir == -1)
258 scale->slice_y = link->dst->outputs[0]->h;
260 data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
261 data[1] = scale->input_is_pal ?
263 cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
264 data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
265 data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
267 out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
268 link->dst->outputs[0]->out_buf->data,
269 link->dst->outputs[0]->out_buf->linesize);
272 scale->slice_y -= out_h;
273 avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
275 scale->slice_y += out_h;
278 AVFilter avfilter_vf_scale = {
280 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
285 .query_formats = query_formats,
287 .priv_size = sizeof(ScaleContext),
289 .inputs = (AVFilterPad[]) {{ .name = "default",
290 .type = AVMEDIA_TYPE_VIDEO,
291 .start_frame = start_frame,
292 .draw_slice = draw_slice,
293 .min_perms = AV_PERM_READ, },
295 .outputs = (AVFilterPad[]) {{ .name = "default",
296 .type = AVMEDIA_TYPE_VIDEO,
297 .config_props = config_props, },