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
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 "libavutil/imgutils.h"
36 #include "libavutil/avassert.h"
37 #include "libswscale/swscale.h"
39 static const char *const var_names[] = {
66 struct SwsContext *sws; ///< software scaler context
67 struct SwsContext *isws[2]; ///< software scaler context for interlaced material
70 * New dimensions. Special values are:
71 * 0 = original width/height
72 * -1 = keep original aspect
75 unsigned int flags; ///sws flags
77 int hsub, vsub; ///< chroma subsampling
78 int slice_y; ///< top of current output slice
79 int input_is_pal; ///< set to 1 if the input format is paletted
80 int output_is_pal; ///< set to 1 if the output format is paletted
83 char w_expr[256]; ///< width expression string
84 char h_expr[256]; ///< height expression string
87 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
89 ScaleContext *scale = ctx->priv;
92 av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
93 av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
95 scale->flags = SWS_BILINEAR;
97 sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
98 p = strstr(args,"flags=");
100 const AVClass *class = sws_get_class();
101 const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
102 AV_OPT_SEARCH_FAKE_OBJ);
103 int ret = av_opt_eval_flags(&class, o, p + 6, &scale->flags);
108 if(strstr(args,"interl=1")){
110 }else if(strstr(args,"interl=-1"))
111 scale->interlaced=-1;
117 static av_cold void uninit(AVFilterContext *ctx)
119 ScaleContext *scale = ctx->priv;
120 sws_freeContext(scale->sws);
121 sws_freeContext(scale->isws[0]);
122 sws_freeContext(scale->isws[1]);
126 static int query_formats(AVFilterContext *ctx)
128 AVFilterFormats *formats;
129 enum PixelFormat pix_fmt;
132 if (ctx->inputs[0]) {
134 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
135 if ( sws_isSupportedInput(pix_fmt)
136 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
137 ff_formats_unref(&formats);
140 ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
142 if (ctx->outputs[0]) {
144 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
145 if ( (sws_isSupportedOutput(pix_fmt) || pix_fmt == PIX_FMT_PAL8)
146 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
147 ff_formats_unref(&formats);
150 ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
156 static int config_props(AVFilterLink *outlink)
158 AVFilterContext *ctx = outlink->src;
159 AVFilterLink *inlink = outlink->src->inputs[0];
160 enum PixelFormat outfmt = outlink->format;
161 ScaleContext *scale = ctx->priv;
163 double var_values[VARS_NB], res;
167 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
168 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
169 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
170 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
171 var_values[VAR_A] = (float) inlink->w / inlink->h;
172 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
173 (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
174 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
175 var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
176 var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
178 /* evaluate width and height */
179 av_expr_parse_and_eval(&res, (expr = scale->w_expr),
180 var_names, var_values,
181 NULL, NULL, NULL, NULL, NULL, 0, ctx);
182 scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
183 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
184 var_names, var_values,
185 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
187 scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
188 /* evaluate again the width, as it may depend on the output height */
189 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
190 var_names, var_values,
191 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
198 /* sanity check params */
199 if (w < -1 || h < -1) {
200 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
201 return AVERROR(EINVAL);
203 if (w == -1 && h == -1)
204 scale->w = scale->h = 0;
211 w = av_rescale(h, inlink->w, inlink->h);
213 h = av_rescale(w, inlink->h, inlink->w);
215 if (w > INT_MAX || h > INT_MAX ||
216 (h * inlink->w) > INT_MAX ||
217 (w * inlink->h) > INT_MAX)
218 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
223 /* TODO: make algorithm configurable */
225 scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
226 av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PSEUDOPAL;
227 if (outfmt == PIX_FMT_PAL8) outfmt = PIX_FMT_BGR8;
228 scale->output_is_pal = av_pix_fmt_descriptors[outfmt].flags & PIX_FMT_PAL ||
229 av_pix_fmt_descriptors[outfmt].flags & PIX_FMT_PSEUDOPAL;
232 sws_freeContext(scale->sws);
233 if (inlink->w == outlink->w && inlink->h == outlink->h &&
234 inlink->format == outlink->format)
237 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
238 outlink->w, outlink->h, outfmt,
239 scale->flags, NULL, NULL, NULL);
241 sws_freeContext(scale->isws[0]);
242 scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
243 outlink->w, outlink->h/2, outfmt,
244 scale->flags, NULL, NULL, NULL);
246 sws_freeContext(scale->isws[1]);
247 scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
248 outlink->w, outlink->h/2, outfmt,
249 scale->flags, NULL, NULL, NULL);
250 if (!scale->sws || !scale->isws[0] || !scale->isws[1])
251 return AVERROR(EINVAL);
254 if (inlink->sample_aspect_ratio.num){
255 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
257 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
259 av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s sar:%d/%d -> w:%d h:%d fmt:%s sar:%d/%d flags:0x%0x\n",
260 inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
261 inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
262 outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
263 outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
268 av_log(NULL, AV_LOG_ERROR,
269 "Error when evaluating the expression '%s'.\n"
270 "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
271 expr, scale->w_expr, scale->h_expr);
275 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
277 ScaleContext *scale = link->dst->priv;
278 AVFilterLink *outlink = link->dst->outputs[0];
279 AVFilterBufferRef *outpicref;
281 if( picref->video->w != link->w
282 || picref->video->h != link->h
283 || picref->format != link->format) {
285 snprintf(scale->w_expr, sizeof(scale->w_expr)-1, "%d", outlink->w);
286 snprintf(scale->h_expr, sizeof(scale->h_expr)-1, "%d", outlink->h);
288 link->dst->inputs[0]->format = picref->format;
289 link->dst->inputs[0]->w = picref->video->w;
290 link->dst->inputs[0]->h = picref->video->h;
292 if ((ret = config_props(outlink)) < 0)
293 av_assert0(0); //what to do here ?
298 ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
302 scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
303 scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
305 outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
306 avfilter_copy_buffer_ref_props(outpicref, picref);
307 outpicref->video->w = outlink->w;
308 outpicref->video->h = outlink->h;
310 outlink->out_buf = outpicref;
311 if(scale->output_is_pal)
312 ff_set_systematic_pal2(outpicref->data[1], outlink->format == PIX_FMT_PAL8 ? PIX_FMT_BGR8 : outlink->format);
314 av_reduce(&outpicref->video->sample_aspect_ratio.num, &outpicref->video->sample_aspect_ratio.den,
315 (int64_t)picref->video->sample_aspect_ratio.num * outlink->h * link->w,
316 (int64_t)picref->video->sample_aspect_ratio.den * outlink->w * link->h,
320 ff_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
323 static int scale_slice(AVFilterLink *link, struct SwsContext *sws, int y, int h, int mul, int field)
325 ScaleContext *scale = link->dst->priv;
326 AVFilterBufferRef *cur_pic = link->cur_buf;
327 AVFilterBufferRef *out_buf = link->dst->outputs[0]->out_buf;
328 const uint8_t *in[4];
330 int in_stride[4],out_stride[4];
334 int vsub= ((i+1)&2) ? scale->vsub : 0;
335 in_stride[i] = cur_pic->linesize[i] * mul;
336 out_stride[i] = out_buf->linesize[i] * mul;
337 in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
338 out[i] = out_buf->data[i] + field * out_buf->linesize[i];
340 if(scale->input_is_pal)
341 in[1] = cur_pic->data[1];
342 if(scale->output_is_pal)
343 out[1] = out_buf->data[1];
345 return sws_scale(sws, in, in_stride, y/mul, h,
349 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
351 ScaleContext *scale = link->dst->priv;
355 ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
359 if (scale->slice_y == 0 && slice_dir == -1)
360 scale->slice_y = link->dst->outputs[0]->h;
362 if(scale->interlaced>0 || (scale->interlaced<0 && link->cur_buf->video->interlaced)){
363 av_assert0(y%(2<<scale->vsub) == 0);
364 out_h = scale_slice(link, scale->isws[0], y, (h+1)/2, 2, 0);
365 out_h+= scale_slice(link, scale->isws[1], y, h /2, 2, 1);
367 out_h = scale_slice(link, scale->sws, y, h, 1, 0);
371 scale->slice_y -= out_h;
372 ff_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
374 scale->slice_y += out_h;
377 AVFilter avfilter_vf_scale = {
379 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
384 .query_formats = query_formats,
386 .priv_size = sizeof(ScaleContext),
388 .inputs = (const AVFilterPad[]) {{ .name = "default",
389 .type = AVMEDIA_TYPE_VIDEO,
390 .start_frame = start_frame,
391 .draw_slice = draw_slice,
392 .min_perms = AV_PERM_READ, },
394 .outputs = (const AVFilterPad[]) {{ .name = "default",
395 .type = AVMEDIA_TYPE_VIDEO,
396 .config_props = config_props, },