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
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/parseutils.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/avassert.h"
42 #include "libswscale/swscale.h"
44 static const char *const var_names[] = {
72 struct SwsContext *sws; ///< software scaler context
73 struct SwsContext *isws[2]; ///< software scaler context for interlaced material
76 * New dimensions. Special values are:
77 * 0 = original width/height
78 * -1 = keep original aspect
81 char *flags_str; ///sws flags string
83 unsigned int flags; ///sws flags
85 int hsub, vsub; ///< chroma subsampling
86 int slice_y; ///< top of current output slice
87 int input_is_pal; ///< set to 1 if the input format is paletted
88 int output_is_pal; ///< set to 1 if the output format is paletted
91 char *w_expr; ///< width expression string
92 char *h_expr; ///< height expression string
95 #define OFFSET(x) offsetof(ScaleContext, x)
96 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
98 static const AVOption scale_options[] = {
99 { "w", "set width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
100 { "width", "set width expression", OFFSET(w_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
101 { "h", "set height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
102 { "height", "set height expression", OFFSET(h_expr), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
103 { "flags", "set libswscale flags", OFFSET(flags_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, INT_MAX, FLAGS },
104 { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, 1, FLAGS },
105 { "size", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
106 { "s", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
110 AVFILTER_DEFINE_CLASS(scale);
112 static av_cold int init(AVFilterContext *ctx, const char *args)
114 ScaleContext *scale = ctx->priv;
115 static const char *shorthand[] = { "w", "h", NULL };
117 const char *args0 = args;
119 scale->class = &scale_class;
120 av_opt_set_defaults(scale);
122 if (args && (scale->size_str = av_get_token(&args, ":"))) {
123 if (av_parse_video_size(&scale->w, &scale->h, scale->size_str) < 0) {
124 av_freep(&scale->size_str);
130 if ((ret = av_opt_set_from_string(scale, args, shorthand, "=", ":")) < 0)
133 if (scale->size_str && (scale->w_expr || scale->h_expr)) {
134 av_log(ctx, AV_LOG_ERROR,
135 "Size and width/height expressions cannot be set at the same time.\n");
136 return AVERROR(EINVAL);
139 if (scale->size_str) {
141 if ((ret = av_parse_video_size(&scale->w, &scale->h, scale->size_str)) < 0) {
142 av_log(ctx, AV_LOG_ERROR,
143 "Invalid size '%s'\n", scale->size_str);
146 snprintf(buf, sizeof(buf)-1, "%d", scale->w);
147 av_opt_set(scale, "w", buf, 0);
148 snprintf(buf, sizeof(buf)-1, "%d", scale->h);
149 av_opt_set(scale, "h", buf, 0);
152 av_opt_set(scale, "w", "iw", 0);
154 av_opt_set(scale, "h", "ih", 0);
156 av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:'%s' interl:%d\n",
157 scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);
159 scale->flags = SWS_BILINEAR;
160 if (scale->flags_str) {
161 const AVClass *class = sws_get_class();
162 const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
163 AV_OPT_SEARCH_FAKE_OBJ);
164 int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
172 static av_cold void uninit(AVFilterContext *ctx)
174 ScaleContext *scale = ctx->priv;
175 sws_freeContext(scale->sws);
176 sws_freeContext(scale->isws[0]);
177 sws_freeContext(scale->isws[1]);
182 static int query_formats(AVFilterContext *ctx)
184 AVFilterFormats *formats;
185 enum AVPixelFormat pix_fmt;
188 if (ctx->inputs[0]) {
190 for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
191 if ( sws_isSupportedInput(pix_fmt)
192 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
193 ff_formats_unref(&formats);
196 ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
198 if (ctx->outputs[0]) {
200 for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
201 if ( (sws_isSupportedOutput(pix_fmt) || pix_fmt == AV_PIX_FMT_PAL8)
202 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
203 ff_formats_unref(&formats);
206 ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
212 static int config_props(AVFilterLink *outlink)
214 AVFilterContext *ctx = outlink->src;
215 AVFilterLink *inlink = outlink->src->inputs[0];
216 enum AVPixelFormat outfmt = outlink->format;
217 ScaleContext *scale = ctx->priv;
218 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
220 double var_values[VARS_NB], res;
224 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
225 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
226 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
227 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
228 var_values[VAR_A] = (double) inlink->w / inlink->h;
229 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
230 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
231 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
232 var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
233 var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
235 /* evaluate width and height */
236 av_expr_parse_and_eval(&res, (expr = scale->w_expr),
237 var_names, var_values,
238 NULL, NULL, NULL, NULL, NULL, 0, ctx);
239 scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
240 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
241 var_names, var_values,
242 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
244 scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
245 /* evaluate again the width, as it may depend on the output height */
246 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
247 var_names, var_values,
248 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
255 /* sanity check params */
256 if (w < -1 || h < -1) {
257 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
258 return AVERROR(EINVAL);
260 if (w == -1 && h == -1)
261 scale->w = scale->h = 0;
268 w = av_rescale(h, inlink->w, inlink->h);
270 h = av_rescale(w, inlink->h, inlink->w);
272 if (w > INT_MAX || h > INT_MAX ||
273 (h * inlink->w) > INT_MAX ||
274 (w * inlink->h) > INT_MAX)
275 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
280 /* TODO: make algorithm configurable */
282 scale->input_is_pal = desc->flags & PIX_FMT_PAL ||
283 desc->flags & PIX_FMT_PSEUDOPAL;
284 if (outfmt == AV_PIX_FMT_PAL8) outfmt = AV_PIX_FMT_BGR8;
285 scale->output_is_pal = av_pix_fmt_desc_get(outfmt)->flags & PIX_FMT_PAL ||
286 av_pix_fmt_desc_get(outfmt)->flags & PIX_FMT_PSEUDOPAL;
289 sws_freeContext(scale->sws);
290 if (inlink->w == outlink->w && inlink->h == outlink->h &&
291 inlink->format == outlink->format)
294 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
295 outlink->w, outlink->h, outfmt,
296 scale->flags, NULL, NULL, NULL);
298 sws_freeContext(scale->isws[0]);
299 scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
300 outlink->w, outlink->h/2, outfmt,
301 scale->flags, NULL, NULL, NULL);
303 sws_freeContext(scale->isws[1]);
304 scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
305 outlink->w, outlink->h/2, outfmt,
306 scale->flags, NULL, NULL, NULL);
307 if (!scale->sws || !scale->isws[0] || !scale->isws[1])
308 return AVERROR(EINVAL);
311 if (inlink->sample_aspect_ratio.num){
312 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
314 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
316 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s sar:%d/%d -> w:%d h:%d fmt:%s sar:%d/%d flags:0x%0x\n",
317 inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),
318 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
319 outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
320 outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
325 av_log(NULL, AV_LOG_ERROR,
326 "Error when evaluating the expression '%s'.\n"
327 "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
328 expr, scale->w_expr, scale->h_expr);
332 static int scale_slice(AVFilterLink *link, AVFrame *out_buf, AVFrame *cur_pic, struct SwsContext *sws, int y, int h, int mul, int field)
334 ScaleContext *scale = link->dst->priv;
335 const uint8_t *in[4];
337 int in_stride[4],out_stride[4];
341 int vsub= ((i+1)&2) ? scale->vsub : 0;
342 in_stride[i] = cur_pic->linesize[i] * mul;
343 out_stride[i] = out_buf->linesize[i] * mul;
344 in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
345 out[i] = out_buf->data[i] + field * out_buf->linesize[i];
347 if(scale->input_is_pal)
348 in[1] = cur_pic->data[1];
349 if(scale->output_is_pal)
350 out[1] = out_buf->data[1];
352 return sws_scale(sws, in, in_stride, y/mul, h,
356 static int filter_frame(AVFilterLink *link, AVFrame *in)
358 ScaleContext *scale = link->dst->priv;
359 AVFilterLink *outlink = link->dst->outputs[0];
361 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
364 if( in->width != link->w
365 || in->height != link->h
366 || in->format != link->format) {
368 snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
369 av_opt_set(scale, "w", buf, 0);
370 snprintf(buf, sizeof(buf)-1, "%d", outlink->h);
371 av_opt_set(scale, "h", buf, 0);
373 link->dst->inputs[0]->format = in->format;
374 link->dst->inputs[0]->w = in->width;
375 link->dst->inputs[0]->h = in->height;
377 if ((ret = config_props(outlink)) < 0)
382 return ff_filter_frame(outlink, in);
384 scale->hsub = desc->log2_chroma_w;
385 scale->vsub = desc->log2_chroma_h;
387 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
390 return AVERROR(ENOMEM);
393 av_frame_copy_props(out, in);
394 out->width = outlink->w;
395 out->height = outlink->h;
397 if(scale->output_is_pal)
398 avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);
400 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
401 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
402 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
405 if(scale->interlaced>0 || (scale->interlaced<0 && in->interlaced_frame)){
406 scale_slice(link, out, in, scale->isws[0], 0, (link->h+1)/2, 2, 0);
407 scale_slice(link, out, in, scale->isws[1], 0, link->h /2, 2, 1);
409 scale_slice(link, out, in, scale->sws, 0, link->h, 1, 0);
413 return ff_filter_frame(outlink, out);
416 static const AVFilterPad avfilter_vf_scale_inputs[] = {
419 .type = AVMEDIA_TYPE_VIDEO,
420 .filter_frame = filter_frame,
425 static const AVFilterPad avfilter_vf_scale_outputs[] = {
428 .type = AVMEDIA_TYPE_VIDEO,
429 .config_props = config_props,
434 AVFilter avfilter_vf_scale = {
436 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
441 .query_formats = query_formats,
443 .priv_size = sizeof(ScaleContext),
445 .inputs = avfilter_vf_scale_inputs,
446 .outputs = avfilter_vf_scale_outputs,
447 .priv_class = &scale_class,