2 * Copyright (c) 2007 Bobby Bingham
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
27 #include "libavutil/avstring.h"
28 #include "libavutil/eval.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/avassert.h"
31 #include "libswscale/swscale.h"
33 static const char *var_names[] = {
62 struct SwsContext *sws; ///< software scaler context
63 struct SwsContext *isws[2]; ///< software scaler context for interlaced material
66 * New dimensions. Special values are:
67 * 0 = original width/height
68 * -1 = keep original aspect
71 unsigned int flags; ///sws flags
73 int hsub, vsub; ///< chroma subsampling
74 int slice_y; ///< top of current output slice
75 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);
95 if(strstr(args,"interl=1")){
97 }else if(strstr(args,"interl=-1"))
104 static av_cold void uninit(AVFilterContext *ctx)
106 ScaleContext *scale = ctx->priv;
107 sws_freeContext(scale->sws);
108 sws_freeContext(scale->isws[0]);
109 sws_freeContext(scale->isws[1]);
113 static int query_formats(AVFilterContext *ctx)
115 AVFilterFormats *formats;
116 enum PixelFormat pix_fmt;
119 if (ctx->inputs[0]) {
121 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
122 if ( sws_isSupportedInput(pix_fmt)
123 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
124 avfilter_formats_unref(&formats);
127 avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
129 if (ctx->outputs[0]) {
131 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
132 if ( sws_isSupportedOutput(pix_fmt)
133 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
134 avfilter_formats_unref(&formats);
137 avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
143 static int config_props(AVFilterLink *outlink)
145 AVFilterContext *ctx = outlink->src;
146 AVFilterLink *inlink = outlink->src->inputs[0];
147 ScaleContext *scale = ctx->priv;
149 double var_values[VARS_NB], res;
153 var_values[VAR_PI] = M_PI;
154 var_values[VAR_PHI] = M_PHI;
155 var_values[VAR_E] = M_E;
156 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
157 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
158 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
159 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
160 var_values[VAR_A] = (float) inlink->w / inlink->h;
161 var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
162 var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
164 /* evaluate width and height */
165 av_expr_parse_and_eval(&res, (expr = scale->w_expr),
166 var_names, var_values,
167 NULL, NULL, NULL, NULL, NULL, 0, ctx);
168 scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
169 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
170 var_names, var_values,
171 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
173 scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
174 /* evaluate again the width, as it may depend on the output height */
175 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
176 var_names, var_values,
177 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
184 /* sanity check params */
185 if (w < -1 || h < -1) {
186 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
187 return AVERROR(EINVAL);
189 if (w == -1 && h == -1)
190 scale->w = scale->h = 0;
197 w = av_rescale(h, inlink->w, inlink->h);
199 h = av_rescale(w, inlink->h, inlink->w);
201 if (w > INT_MAX || h > INT_MAX ||
202 (h * inlink->w) > INT_MAX ||
203 (w * inlink->h) > INT_MAX)
204 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
209 /* TODO: make algorithm configurable */
210 av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
211 inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
212 outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
215 scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL;
218 sws_freeContext(scale->sws);
219 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
220 outlink->w, outlink->h, outlink->format,
221 scale->flags, NULL, NULL, NULL);
223 sws_freeContext(scale->isws[0]);
224 scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
225 outlink->w, outlink->h/2, outlink->format,
226 scale->flags, NULL, NULL, NULL);
228 sws_freeContext(scale->isws[1]);
229 scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
230 outlink->w, outlink->h/2, outlink->format,
231 scale->flags, NULL, NULL, NULL);
233 return AVERROR(EINVAL);
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;
249 scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
250 scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
252 outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
253 avfilter_copy_buffer_ref_props(outpicref, picref);
254 outpicref->video->w = outlink->w;
255 outpicref->video->h = outlink->h;
257 outlink->out_buf = outpicref;
259 av_reduce(&outpicref->video->sample_aspect_ratio.num, &outpicref->video->sample_aspect_ratio.den,
260 (int64_t)picref->video->sample_aspect_ratio.num * outlink->h * link->w,
261 (int64_t)picref->video->sample_aspect_ratio.den * outlink->w * link->h,
265 avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
268 static int scale_slice(AVFilterLink *link, struct SwsContext *sws, int y, int h, int mul, int field)
270 ScaleContext *scale = link->dst->priv;
271 AVFilterBufferRef *cur_pic = link->cur_buf;
272 AVFilterBufferRef *out_buf = link->dst->outputs[0]->out_buf;
273 const uint8_t *in[4], *out[4];
274 int in_stride[4],out_stride[4];
278 int vsub= ((i+1)&2) ? scale->vsub : 0;
279 in_stride[i] = cur_pic->linesize[i] * mul;
280 out_stride[i] = out_buf->linesize[i] * mul;
281 in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
282 out[i] = out_buf->data[i] + field * out_buf->linesize[i];
284 if(scale->input_is_pal){
285 in[1] = cur_pic->data[1];
286 out[1] = out_buf->data[1];
289 return sws_scale(sws, in, in_stride, y/mul, h,
293 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
295 ScaleContext *scale = link->dst->priv;
298 if (scale->slice_y == 0 && slice_dir == -1)
299 scale->slice_y = link->dst->outputs[0]->h;
301 if(scale->interlaced>0 || (scale->interlaced<0 && link->cur_buf->video->interlaced)){
302 av_assert0(y%4 == 0);
303 out_h = scale_slice(link, scale->isws[0], y, (h+1)/2, 2, 0);
304 out_h+= scale_slice(link, scale->isws[1], y, h /2, 2, 1);
306 out_h = scale_slice(link, scale->sws, y, h, 1, 0);
310 scale->slice_y -= out_h;
311 avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
313 scale->slice_y += out_h;
316 AVFilter avfilter_vf_scale = {
318 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
323 .query_formats = query_formats,
325 .priv_size = sizeof(ScaleContext),
327 .inputs = (AVFilterPad[]) {{ .name = "default",
328 .type = AVMEDIA_TYPE_VIDEO,
329 .start_frame = start_frame,
330 .draw_slice = draw_slice,
331 .min_perms = AV_PERM_READ, },
333 .outputs = (AVFilterPad[]) {{ .name = "default",
334 .type = AVMEDIA_TYPE_VIDEO,
335 .config_props = config_props, },