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
33 #include "libavutil/avstring.h"
34 #include "libavutil/eval.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/pixdesc.h"
39 #include "libswscale/swscale.h"
41 static const char *const var_names[] = {
71 typedef struct ScaleContext {
73 struct SwsContext *sws; ///< software scaler context
76 * New dimensions. Special values are:
77 * 0 = original width/height
78 * -1 = keep original aspect
81 unsigned int flags; ///sws flags
83 int hsub, vsub; ///< chroma subsampling
84 int slice_y; ///< top of current output slice
85 int input_is_pal; ///< set to 1 if the input format is paletted
87 char *w_expr; ///< width expression string
88 char *h_expr; ///< height expression string
92 static av_cold int init(AVFilterContext *ctx)
94 ScaleContext *scale = ctx->priv;
96 if (scale->flags_str) {
97 const AVClass *class = sws_get_class();
98 const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
99 AV_OPT_SEARCH_FAKE_OBJ);
100 int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
109 static av_cold void uninit(AVFilterContext *ctx)
111 ScaleContext *scale = ctx->priv;
112 sws_freeContext(scale->sws);
116 static int query_formats(AVFilterContext *ctx)
118 AVFilterFormats *formats;
119 enum AVPixelFormat pix_fmt;
122 if (ctx->inputs[0]) {
123 const AVPixFmtDescriptor *desc = NULL;
125 while ((desc = av_pix_fmt_desc_next(desc))) {
126 pix_fmt = av_pix_fmt_desc_get_id(desc);
127 if ((sws_isSupportedInput(pix_fmt) ||
128 sws_isSupportedEndiannessConversion(pix_fmt))
129 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
130 ff_formats_unref(&formats);
134 ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
136 if (ctx->outputs[0]) {
137 const AVPixFmtDescriptor *desc = NULL;
139 while ((desc = av_pix_fmt_desc_next(desc))) {
140 pix_fmt = av_pix_fmt_desc_get_id(desc);
141 if ((sws_isSupportedOutput(pix_fmt) ||
142 sws_isSupportedEndiannessConversion(pix_fmt))
143 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
144 ff_formats_unref(&formats);
148 ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
154 static int config_props(AVFilterLink *outlink)
156 AVFilterContext *ctx = outlink->src;
157 AVFilterLink *inlink = outlink->src->inputs[0];
158 ScaleContext *scale = ctx->priv;
159 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
161 double var_values[VARS_NB], res;
165 var_values[VAR_PI] = M_PI;
166 var_values[VAR_PHI] = M_PHI;
167 var_values[VAR_E] = M_E;
168 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
169 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
170 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
171 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
172 var_values[VAR_A] = (double) inlink->w / inlink->h;
173 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
174 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
175 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
176 var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
177 var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
179 /* evaluate width and height */
180 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
181 var_names, var_values,
182 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
184 scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
185 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
186 var_names, var_values,
187 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
189 scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
190 /* evaluate again the width, as it may depend on the output height */
191 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
192 var_names, var_values,
193 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
200 /* sanity check params */
201 if (w < -1 || h < -1) {
202 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
203 return AVERROR(EINVAL);
205 if (w == -1 && h == -1)
206 scale->w = scale->h = 0;
213 w = av_rescale(h, inlink->w, inlink->h);
215 h = av_rescale(w, inlink->h, inlink->w);
217 if (w > INT_MAX || h > INT_MAX ||
218 (h * inlink->w) > INT_MAX ||
219 (w * inlink->h) > INT_MAX)
220 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
225 /* TODO: make algorithm configurable */
226 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
227 inlink ->w, inlink ->h, av_get_pix_fmt_name(inlink->format),
228 outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
231 scale->input_is_pal = desc->flags & AV_PIX_FMT_FLAG_PAL ||
232 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL;
235 sws_freeContext(scale->sws);
236 if (inlink->w == outlink->w && inlink->h == outlink->h &&
237 inlink->format == outlink->format)
240 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
241 outlink->w, outlink->h, outlink->format,
242 scale->flags, NULL, NULL, NULL);
244 return AVERROR(EINVAL);
248 if (inlink->sample_aspect_ratio.num)
249 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
250 outlink->w*inlink->h},
251 inlink->sample_aspect_ratio);
253 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
258 av_log(NULL, AV_LOG_ERROR,
259 "Error when evaluating the expression '%s'\n", expr);
263 static int filter_frame(AVFilterLink *link, AVFrame *in)
265 ScaleContext *scale = link->dst->priv;
266 AVFilterLink *outlink = link->dst->outputs[0];
268 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
271 return ff_filter_frame(outlink, in);
273 scale->hsub = desc->log2_chroma_w;
274 scale->vsub = desc->log2_chroma_h;
276 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
279 return AVERROR(ENOMEM);
282 av_frame_copy_props(out, in);
283 out->width = outlink->w;
284 out->height = outlink->h;
286 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
287 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
288 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
291 sws_scale(scale->sws, in->data, in->linesize, 0, in->height,
292 out->data, out->linesize);
295 return ff_filter_frame(outlink, out);
298 #define OFFSET(x) offsetof(ScaleContext, x)
299 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
300 static const AVOption options[] = {
301 { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
302 { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
303 { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
307 static const AVClass scale_class = {
308 .class_name = "scale",
309 .item_name = av_default_item_name,
311 .version = LIBAVUTIL_VERSION_INT,
314 static const AVFilterPad avfilter_vf_scale_inputs[] = {
317 .type = AVMEDIA_TYPE_VIDEO,
318 .filter_frame = filter_frame,
323 static const AVFilterPad avfilter_vf_scale_outputs[] = {
326 .type = AVMEDIA_TYPE_VIDEO,
327 .config_props = config_props,
332 AVFilter ff_vf_scale = {
334 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
339 .query_formats = query_formats,
341 .priv_size = sizeof(ScaleContext),
342 .priv_class = &scale_class,
344 .inputs = avfilter_vf_scale_inputs,
345 .outputs = avfilter_vf_scale_outputs,