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
82 unsigned int flags; ///sws flags
84 int hsub, vsub; ///< chroma subsampling
85 int slice_y; ///< top of current output slice
86 int input_is_pal; ///< set to 1 if the input format is paletted
87 int output_is_pal; ///< set to 1 if the output format is paletted
90 char *w_expr; ///< width expression string
91 char *h_expr; ///< height expression string
95 static av_cold int init(AVFilterContext *ctx)
97 ScaleContext *scale = ctx->priv;
100 if (scale->size_str && (scale->w_expr || scale->h_expr)) {
101 av_log(ctx, AV_LOG_ERROR,
102 "Size and width/height expressions cannot be set at the same time.\n");
103 return AVERROR(EINVAL);
106 if (scale->w_expr && !scale->h_expr)
107 FFSWAP(char *, scale->w_expr, scale->size_str);
109 if (scale->size_str) {
111 if ((ret = av_parse_video_size(&scale->w, &scale->h, scale->size_str)) < 0) {
112 av_log(ctx, AV_LOG_ERROR,
113 "Invalid size '%s'\n", scale->size_str);
116 snprintf(buf, sizeof(buf)-1, "%d", scale->w);
117 av_opt_set(scale, "w", buf, 0);
118 snprintf(buf, sizeof(buf)-1, "%d", scale->h);
119 av_opt_set(scale, "h", buf, 0);
122 av_opt_set(scale, "w", "iw", 0);
124 av_opt_set(scale, "h", "ih", 0);
126 av_log(ctx, AV_LOG_VERBOSE, "w:%s h:%s flags:'%s' interl:%d\n",
127 scale->w_expr, scale->h_expr, (char *)av_x_if_null(scale->flags_str, ""), scale->interlaced);
129 scale->flags = SWS_BILINEAR;
131 if (scale->flags_str) {
132 const AVClass *class = sws_get_class();
133 const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
134 AV_OPT_SEARCH_FAKE_OBJ);
135 int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
143 static av_cold void uninit(AVFilterContext *ctx)
145 ScaleContext *scale = ctx->priv;
146 sws_freeContext(scale->sws);
147 sws_freeContext(scale->isws[0]);
148 sws_freeContext(scale->isws[1]);
153 static int query_formats(AVFilterContext *ctx)
155 AVFilterFormats *formats;
156 enum AVPixelFormat pix_fmt;
159 if (ctx->inputs[0]) {
161 for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
162 if ( sws_isSupportedInput(pix_fmt)
163 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
164 ff_formats_unref(&formats);
167 ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
169 if (ctx->outputs[0]) {
171 for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
172 if ( (sws_isSupportedOutput(pix_fmt) || pix_fmt == AV_PIX_FMT_PAL8)
173 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
174 ff_formats_unref(&formats);
177 ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
183 static int config_props(AVFilterLink *outlink)
185 AVFilterContext *ctx = outlink->src;
186 AVFilterLink *inlink = outlink->src->inputs[0];
187 enum AVPixelFormat outfmt = outlink->format;
188 ScaleContext *scale = ctx->priv;
189 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
191 double var_values[VARS_NB], res;
195 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
196 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
197 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
198 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
199 var_values[VAR_A] = (double) inlink->w / inlink->h;
200 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
201 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
202 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
203 var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
204 var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
206 /* evaluate width and height */
207 av_expr_parse_and_eval(&res, (expr = scale->w_expr),
208 var_names, var_values,
209 NULL, NULL, NULL, NULL, NULL, 0, ctx);
210 scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
211 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
212 var_names, var_values,
213 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
215 scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
216 /* evaluate again the width, as it may depend on the output height */
217 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
218 var_names, var_values,
219 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
226 /* sanity check params */
227 if (w < -1 || h < -1) {
228 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
229 return AVERROR(EINVAL);
231 if (w == -1 && h == -1)
232 scale->w = scale->h = 0;
239 w = av_rescale(h, inlink->w, inlink->h);
241 h = av_rescale(w, inlink->h, inlink->w);
243 if (w > INT_MAX || h > INT_MAX ||
244 (h * inlink->w) > INT_MAX ||
245 (w * inlink->h) > INT_MAX)
246 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
251 /* TODO: make algorithm configurable */
253 scale->input_is_pal = desc->flags & PIX_FMT_PAL ||
254 desc->flags & PIX_FMT_PSEUDOPAL;
255 if (outfmt == AV_PIX_FMT_PAL8) outfmt = AV_PIX_FMT_BGR8;
256 scale->output_is_pal = av_pix_fmt_desc_get(outfmt)->flags & PIX_FMT_PAL ||
257 av_pix_fmt_desc_get(outfmt)->flags & PIX_FMT_PSEUDOPAL;
260 sws_freeContext(scale->sws);
261 if (inlink->w == outlink->w && inlink->h == outlink->h &&
262 inlink->format == outlink->format)
265 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
266 outlink->w, outlink->h, outfmt,
267 scale->flags, NULL, NULL, NULL);
269 sws_freeContext(scale->isws[0]);
270 scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
271 outlink->w, outlink->h/2, outfmt,
272 scale->flags, NULL, NULL, NULL);
274 sws_freeContext(scale->isws[1]);
275 scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
276 outlink->w, outlink->h/2, outfmt,
277 scale->flags, NULL, NULL, NULL);
278 if (!scale->sws || !scale->isws[0] || !scale->isws[1])
279 return AVERROR(EINVAL);
282 if (inlink->sample_aspect_ratio.num){
283 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
285 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
287 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",
288 inlink ->w, inlink ->h, av_get_pix_fmt_name( inlink->format),
289 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
290 outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
291 outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
296 av_log(NULL, AV_LOG_ERROR,
297 "Error when evaluating the expression '%s'.\n"
298 "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
299 expr, scale->w_expr, scale->h_expr);
303 static int scale_slice(AVFilterLink *link, AVFrame *out_buf, AVFrame *cur_pic, struct SwsContext *sws, int y, int h, int mul, int field)
305 ScaleContext *scale = link->dst->priv;
306 const uint8_t *in[4];
308 int in_stride[4],out_stride[4];
312 int vsub= ((i+1)&2) ? scale->vsub : 0;
313 in_stride[i] = cur_pic->linesize[i] * mul;
314 out_stride[i] = out_buf->linesize[i] * mul;
315 in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
316 out[i] = out_buf->data[i] + field * out_buf->linesize[i];
318 if(scale->input_is_pal)
319 in[1] = cur_pic->data[1];
320 if(scale->output_is_pal)
321 out[1] = out_buf->data[1];
323 return sws_scale(sws, in, in_stride, y/mul, h,
327 static int filter_frame(AVFilterLink *link, AVFrame *in)
329 ScaleContext *scale = link->dst->priv;
330 AVFilterLink *outlink = link->dst->outputs[0];
332 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
335 if( in->width != link->w
336 || in->height != link->h
337 || in->format != link->format) {
339 snprintf(buf, sizeof(buf)-1, "%d", outlink->w);
340 av_opt_set(scale, "w", buf, 0);
341 snprintf(buf, sizeof(buf)-1, "%d", outlink->h);
342 av_opt_set(scale, "h", buf, 0);
344 link->dst->inputs[0]->format = in->format;
345 link->dst->inputs[0]->w = in->width;
346 link->dst->inputs[0]->h = in->height;
348 if ((ret = config_props(outlink)) < 0)
353 return ff_filter_frame(outlink, in);
355 scale->hsub = desc->log2_chroma_w;
356 scale->vsub = desc->log2_chroma_h;
358 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
361 return AVERROR(ENOMEM);
364 av_frame_copy_props(out, in);
365 out->width = outlink->w;
366 out->height = outlink->h;
368 if(scale->output_is_pal)
369 avpriv_set_systematic_pal2((uint32_t*)out->data[1], outlink->format == AV_PIX_FMT_PAL8 ? AV_PIX_FMT_BGR8 : outlink->format);
371 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
372 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
373 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
376 if(scale->interlaced>0 || (scale->interlaced<0 && in->interlaced_frame)){
377 scale_slice(link, out, in, scale->isws[0], 0, (link->h+1)/2, 2, 0);
378 scale_slice(link, out, in, scale->isws[1], 0, link->h /2, 2, 1);
380 scale_slice(link, out, in, scale->sws, 0, link->h, 1, 0);
384 return ff_filter_frame(outlink, out);
387 #define OFFSET(x) offsetof(ScaleContext, x)
388 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
390 static const AVOption scale_options[] = {
391 { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
392 { "width", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
393 { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
394 { "height","Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, .flags = FLAGS },
395 { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
396 { "interl", "set interlacing", OFFSET(interlaced), AV_OPT_TYPE_INT, {.i64 = 0 }, -1, 1, FLAGS },
397 { "size", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
398 { "s", "set video size", OFFSET(size_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, FLAGS },
402 AVFILTER_DEFINE_CLASS(scale);
404 static const AVFilterPad avfilter_vf_scale_inputs[] = {
407 .type = AVMEDIA_TYPE_VIDEO,
408 .filter_frame = filter_frame,
413 static const AVFilterPad avfilter_vf_scale_outputs[] = {
416 .type = AVMEDIA_TYPE_VIDEO,
417 .config_props = config_props,
422 AVFilter avfilter_vf_scale = {
424 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
429 .query_formats = query_formats,
431 .priv_size = sizeof(ScaleContext),
432 .priv_class = &scale_class,
434 .inputs = avfilter_vf_scale_inputs,
435 .outputs = avfilter_vf_scale_outputs,