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 *const var_names[] = {
64 struct SwsContext *sws; ///< software scaler context
67 * New dimensions. Special values are:
68 * 0 = original width/height
69 * -1 = keep original aspect
72 unsigned int flags; ///sws flags
74 int hsub, vsub; ///< chroma subsampling
75 int slice_y; ///< top of current output slice
76 int input_is_pal; ///< set to 1 if the input format is paletted
78 char w_expr[256]; ///< width expression string
79 char h_expr[256]; ///< height expression string
82 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
84 ScaleContext *scale = ctx->priv;
87 av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
88 av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
90 scale->flags = SWS_BILINEAR;
92 sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
93 p = strstr(args,"flags=");
94 if (p) scale->flags = strtoul(p+6, NULL, 0);
100 static av_cold void uninit(AVFilterContext *ctx)
102 ScaleContext *scale = ctx->priv;
103 sws_freeContext(scale->sws);
107 static int query_formats(AVFilterContext *ctx)
109 AVFilterFormats *formats;
110 enum PixelFormat pix_fmt;
113 if (ctx->inputs[0]) {
115 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
116 if ( sws_isSupportedInput(pix_fmt)
117 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
118 avfilter_formats_unref(&formats);
121 avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
123 if (ctx->outputs[0]) {
125 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
126 if ( sws_isSupportedOutput(pix_fmt)
127 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
128 avfilter_formats_unref(&formats);
131 avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
137 static int config_props(AVFilterLink *outlink)
139 AVFilterContext *ctx = outlink->src;
140 AVFilterLink *inlink = outlink->src->inputs[0];
141 ScaleContext *scale = ctx->priv;
143 double var_values[VARS_NB], res;
147 var_values[VAR_PI] = M_PI;
148 var_values[VAR_PHI] = M_PHI;
149 var_values[VAR_E] = M_E;
150 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
151 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
152 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
153 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
154 var_values[VAR_DAR] = var_values[VAR_A] = (float) inlink->w / inlink->h;
155 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
156 (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
157 var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
158 var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
160 /* evaluate width and height */
161 av_expr_parse_and_eval(&res, (expr = scale->w_expr),
162 var_names, var_values,
163 NULL, NULL, NULL, NULL, NULL, 0, ctx);
164 scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
165 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
166 var_names, var_values,
167 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
169 scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
170 /* evaluate again the width, as it may depend on the output height */
171 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
172 var_names, var_values,
173 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
180 /* sanity check params */
181 if (w < -1 || h < -1) {
182 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
183 return AVERROR(EINVAL);
185 if (w == -1 && h == -1)
186 scale->w = scale->h = 0;
193 w = av_rescale(h, inlink->w, inlink->h);
195 h = av_rescale(w, inlink->h, inlink->w);
197 if (w > INT_MAX || h > INT_MAX ||
198 (h * inlink->w) > INT_MAX ||
199 (w * inlink->h) > INT_MAX)
200 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
205 /* TODO: make algorithm configurable */
206 av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
207 inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
208 outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
211 scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
212 av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PSEUDOPAL;
215 sws_freeContext(scale->sws);
216 if (inlink->w == outlink->w && inlink->h == outlink->h &&
217 inlink->format == outlink->format)
220 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
221 outlink->w, outlink->h, outlink->format,
222 scale->flags, NULL, NULL, NULL);
224 return AVERROR(EINVAL);
228 if (inlink->sample_aspect_ratio.num)
229 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
230 outlink->w*inlink->h},
231 inlink->sample_aspect_ratio);
233 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
238 av_log(NULL, AV_LOG_ERROR,
239 "Error when evaluating the expression '%s'\n", expr);
243 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
245 ScaleContext *scale = link->dst->priv;
246 AVFilterLink *outlink = link->dst->outputs[0];
247 AVFilterBufferRef *outpicref;
250 avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
254 scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
255 scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
257 outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
258 avfilter_copy_buffer_ref_props(outpicref, picref);
259 outpicref->video->w = outlink->w;
260 outpicref->video->h = outlink->h;
262 outlink->out_buf = outpicref;
264 av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
265 (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
266 (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
270 avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
273 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
275 ScaleContext *scale = link->dst->priv;
277 AVFilterBufferRef *cur_pic = link->cur_buf;
278 const uint8_t *data[4];
281 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
285 if (scale->slice_y == 0 && slice_dir == -1)
286 scale->slice_y = link->dst->outputs[0]->h;
288 data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
289 data[1] = scale->input_is_pal ?
291 cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
292 data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
293 data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
295 out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
296 link->dst->outputs[0]->out_buf->data,
297 link->dst->outputs[0]->out_buf->linesize);
300 scale->slice_y -= out_h;
301 avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
303 scale->slice_y += out_h;
306 AVFilter avfilter_vf_scale = {
308 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
313 .query_formats = query_formats,
315 .priv_size = sizeof(ScaleContext),
317 .inputs = (AVFilterPad[]) {{ .name = "default",
318 .type = AVMEDIA_TYPE_VIDEO,
319 .start_frame = start_frame,
320 .draw_slice = draw_slice,
321 .min_perms = AV_PERM_READ, },
323 .outputs = (AVFilterPad[]) {{ .name = "default",
324 .type = AVMEDIA_TYPE_VIDEO,
325 .config_props = config_props, },