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
30 #include "libavutil/avstring.h"
31 #include "libavutil/eval.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "libswscale/swscale.h"
37 static const char *const var_names[] = {
68 struct SwsContext *sws; ///< software scaler context
71 * New dimensions. Special values are:
72 * 0 = original width/height
73 * -1 = keep original aspect
76 unsigned int flags; ///sws flags
78 int hsub, vsub; ///< chroma subsampling
79 int slice_y; ///< top of current output slice
80 int input_is_pal; ///< set to 1 if the input format is paletted
82 char w_expr[256]; ///< width expression string
83 char h_expr[256]; ///< height expression string
86 static av_cold int init(AVFilterContext *ctx, const char *args)
88 ScaleContext *scale = ctx->priv;
91 av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
92 av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
94 scale->flags = SWS_BILINEAR;
96 sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
97 p = strstr(args,"flags=");
99 const AVClass *class = sws_get_class();
100 const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
101 AV_OPT_SEARCH_FAKE_OBJ);
102 int ret = av_opt_eval_flags(&class, o, p + 6, &scale->flags);
112 static av_cold void uninit(AVFilterContext *ctx)
114 ScaleContext *scale = ctx->priv;
115 sws_freeContext(scale->sws);
119 static int query_formats(AVFilterContext *ctx)
121 AVFilterFormats *formats;
122 enum PixelFormat pix_fmt;
125 if (ctx->inputs[0]) {
127 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
128 if ( sws_isSupportedInput(pix_fmt)
129 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
130 ff_formats_unref(&formats);
133 ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
135 if (ctx->outputs[0]) {
137 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
138 if ( sws_isSupportedOutput(pix_fmt)
139 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
140 ff_formats_unref(&formats);
143 ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
149 static int config_props(AVFilterLink *outlink)
151 AVFilterContext *ctx = outlink->src;
152 AVFilterLink *inlink = outlink->src->inputs[0];
153 ScaleContext *scale = ctx->priv;
155 double var_values[VARS_NB], res;
159 var_values[VAR_PI] = M_PI;
160 var_values[VAR_PHI] = M_PHI;
161 var_values[VAR_E] = M_E;
162 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
163 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
164 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
165 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
166 var_values[VAR_DAR] = var_values[VAR_A] = (float) inlink->w / inlink->h;
167 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
168 (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
169 var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
170 var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
172 /* evaluate width and height */
173 av_expr_parse_and_eval(&res, (expr = scale->w_expr),
174 var_names, var_values,
175 NULL, NULL, NULL, NULL, NULL, 0, ctx);
176 scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
177 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
178 var_names, var_values,
179 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
181 scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
182 /* evaluate again the width, as it may depend on the output height */
183 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
184 var_names, var_values,
185 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
192 /* sanity check params */
193 if (w < -1 || h < -1) {
194 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
195 return AVERROR(EINVAL);
197 if (w == -1 && h == -1)
198 scale->w = scale->h = 0;
205 w = av_rescale(h, inlink->w, inlink->h);
207 h = av_rescale(w, inlink->h, inlink->w);
209 if (w > INT_MAX || h > INT_MAX ||
210 (h * inlink->w) > INT_MAX ||
211 (w * inlink->h) > INT_MAX)
212 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
217 /* TODO: make algorithm configurable */
218 av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
219 inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
220 outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
223 scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
224 av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PSEUDOPAL;
227 sws_freeContext(scale->sws);
228 if (inlink->w == outlink->w && inlink->h == outlink->h &&
229 inlink->format == outlink->format)
232 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
233 outlink->w, outlink->h, outlink->format,
234 scale->flags, NULL, NULL, NULL);
236 return AVERROR(EINVAL);
240 if (inlink->sample_aspect_ratio.num)
241 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
242 outlink->w*inlink->h},
243 inlink->sample_aspect_ratio);
245 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
250 av_log(NULL, AV_LOG_ERROR,
251 "Error when evaluating the expression '%s'\n", expr);
255 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
257 ScaleContext *scale = link->dst->priv;
258 AVFilterLink *outlink = link->dst->outputs[0];
259 AVFilterBufferRef *outpicref;
262 ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
266 scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
267 scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
269 outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
270 avfilter_copy_buffer_ref_props(outpicref, picref);
271 outpicref->video->w = outlink->w;
272 outpicref->video->h = outlink->h;
274 outlink->out_buf = outpicref;
276 av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
277 (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
278 (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
282 ff_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
285 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
287 ScaleContext *scale = link->dst->priv;
289 AVFilterBufferRef *cur_pic = link->cur_buf;
290 const uint8_t *data[4];
293 ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
297 if (scale->slice_y == 0 && slice_dir == -1)
298 scale->slice_y = link->dst->outputs[0]->h;
300 data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
301 data[1] = scale->input_is_pal ?
303 cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
304 data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
305 data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
307 out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
308 link->dst->outputs[0]->out_buf->data,
309 link->dst->outputs[0]->out_buf->linesize);
312 scale->slice_y -= out_h;
313 ff_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
315 scale->slice_y += out_h;
318 AVFilter avfilter_vf_scale = {
320 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
325 .query_formats = query_formats,
327 .priv_size = sizeof(ScaleContext),
329 .inputs = (AVFilterPad[]) {{ .name = "default",
330 .type = AVMEDIA_TYPE_VIDEO,
331 .start_frame = start_frame,
332 .draw_slice = draw_slice,
333 .min_perms = AV_PERM_READ, },
335 .outputs = (AVFilterPad[]) {{ .name = "default",
336 .type = AVMEDIA_TYPE_VIDEO,
337 .config_props = config_props, },