vf_pad: fix "vsub" variable value computation
[libav.git] / libavfilter / vf_scale.c
CommitLineData
c3eabb7d 1/*
3fa77bde 2 * Copyright (c) 2007 Bobby Bingham
c3eabb7d 3 *
2912e87a 4 * This file is part of Libav.
c3eabb7d 5 *
2912e87a 6 * Libav is free software; you can redistribute it and/or
c3eabb7d
SS
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.
10 *
2912e87a 11 * Libav is distributed in the hope that it will be useful,
c3eabb7d
SS
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
2912e87a 17 * License along with Libav; if not, write to the Free Software
c3eabb7d
SS
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
ba87f080 22 * @file
c3eabb7d
SS
23 * scale video filter
24 */
25
26#include "avfilter.h"
68e23c08
SS
27#include "libavutil/avstring.h"
28#include "libavutil/eval.h"
0ebcdf5c 29#include "libavutil/mathematics.h"
b4b66456 30#include "libavutil/pixdesc.h"
c3eabb7d
SS
31#include "libswscale/swscale.h"
32
68e23c08
SS
33static const char *var_names[] = {
34 "PI",
35 "PHI",
36 "E",
37 "in_w", "iw",
38 "in_h", "ih",
39 "out_w", "ow",
40 "out_h", "oh",
46b29397
SS
41 "a", "dar",
42 "sar",
68e23c08
SS
43 "hsub",
44 "vsub",
45 NULL
46};
47
48enum var_name {
49 VAR_PI,
50 VAR_PHI,
51 VAR_E,
52 VAR_IN_W, VAR_IW,
53 VAR_IN_H, VAR_IH,
54 VAR_OUT_W, VAR_OW,
55 VAR_OUT_H, VAR_OH,
46b29397
SS
56 VAR_A, VAR_DAR,
57 VAR_SAR,
68e23c08
SS
58 VAR_HSUB,
59 VAR_VSUB,
60 VARS_NB
61};
62
c3eabb7d
SS
63typedef struct {
64 struct SwsContext *sws; ///< software scaler context
65
66 /**
67 * New dimensions. Special values are:
68 * 0 = original width/height
69 * -1 = keep original aspect
70 */
71 int w, h;
8155bbc9 72 unsigned int flags; ///sws flags
c3eabb7d
SS
73
74 int hsub, vsub; ///< chroma subsampling
75 int slice_y; ///< top of current output slice
c3eabb7d 76 int input_is_pal; ///< set to 1 if the input format is paletted
68e23c08
SS
77
78 char w_expr[256]; ///< width expression string
79 char h_expr[256]; ///< height expression string
c3eabb7d
SS
80} ScaleContext;
81
82static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
83{
84 ScaleContext *scale = ctx->priv;
a0aa7354 85 const char *p;
c3eabb7d 86
68e23c08
SS
87 av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
88 av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
89
8155bbc9 90 scale->flags = SWS_BILINEAR;
46daee6e 91 if (args) {
68e23c08 92 sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
46daee6e
SS
93 p = strstr(args,"flags=");
94 if (p) scale->flags = strtoul(p+6, NULL, 0);
8155bbc9 95 }
c3eabb7d 96
c3eabb7d
SS
97 return 0;
98}
99
100static av_cold void uninit(AVFilterContext *ctx)
101{
102 ScaleContext *scale = ctx->priv;
103 sws_freeContext(scale->sws);
104 scale->sws = NULL;
105}
106
107static int query_formats(AVFilterContext *ctx)
108{
109 AVFilterFormats *formats;
27d8f6b6
SS
110 enum PixelFormat pix_fmt;
111 int ret;
c3eabb7d
SS
112
113 if (ctx->inputs[0]) {
27d8f6b6
SS
114 formats = NULL;
115 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
116 if ( sws_isSupportedInput(pix_fmt)
bdab614b 117 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
27d8f6b6
SS
118 avfilter_formats_unref(&formats);
119 return ret;
120 }
c3eabb7d
SS
121 avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
122 }
123 if (ctx->outputs[0]) {
27d8f6b6
SS
124 formats = NULL;
125 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
126 if ( sws_isSupportedOutput(pix_fmt)
bdab614b 127 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
27d8f6b6
SS
128 avfilter_formats_unref(&formats);
129 return ret;
130 }
c3eabb7d
SS
131 avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
132 }
133
134 return 0;
135}
136
137static int config_props(AVFilterLink *outlink)
138{
139 AVFilterContext *ctx = outlink->src;
140 AVFilterLink *inlink = outlink->src->inputs[0];
141 ScaleContext *scale = ctx->priv;
142 int64_t w, h;
68e23c08
SS
143 double var_values[VARS_NB], res;
144 char *expr;
145 int ret;
146
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;
46b29397
SS
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;
68e23c08
SS
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;
159
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)
168 goto fail;
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)
174 goto fail;
175 scale->w = res;
176
177 w = scale->w;
178 h = scale->h;
179
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);
184 }
185 if (w == -1 && h == -1)
186 scale->w = scale->h = 0;
c3eabb7d
SS
187
188 if (!(w = scale->w))
189 w = inlink->w;
190 if (!(h = scale->h))
191 h = inlink->h;
192 if (w == -1)
193 w = av_rescale(h, inlink->w, inlink->h);
194 if (h == -1)
195 h = av_rescale(w, inlink->h, inlink->w);
196
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");
201
202 outlink->w = w;
203 outlink->h = h;
204
205 /* TODO: make algorithm configurable */
47f0704e 206 av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
38efdb2c
SS
207 inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
208 outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
209 scale->flags);
c3eabb7d 210
d2a2b08c 211 scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL;
c3eabb7d 212
52982dbe
MN
213 if (scale->sws)
214 sws_freeContext(scale->sws);
d760a316
SS
215 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
216 outlink->w, outlink->h, outlink->format,
217 scale->flags, NULL, NULL, NULL);
dccdd93e
SS
218 if (!scale->sws)
219 return AVERROR(EINVAL);
d760a316 220
dccdd93e 221 return 0;
68e23c08
SS
222
223fail:
224 av_log(NULL, AV_LOG_ERROR,
225 "Error when evaluating the expression '%s'\n", expr);
226 return ret;
c3eabb7d
SS
227}
228
ecc8dada 229static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
c3eabb7d
SS
230{
231 ScaleContext *scale = link->dst->priv;
232 AVFilterLink *outlink = link->dst->outputs[0];
ecc8dada 233 AVFilterBufferRef *outpicref;
c3eabb7d 234
b4b66456
SS
235 scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
236 scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
b37aa4de 237
c3eabb7d 238 outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
7fce481a 239 avfilter_copy_buffer_ref_props(outpicref, picref);
e4cc9f20
BC
240 outpicref->video->w = outlink->w;
241 outpicref->video->h = outlink->h;
efdc74ef 242
5d4890d7 243 outlink->out_buf = outpicref;
c3eabb7d 244
cc80caff
HM
245 av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
246 (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
247 (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
c3eabb7d
SS
248 INT_MAX);
249
a13a5437 250 scale->slice_y = 0;
7fce481a 251 avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
c3eabb7d
SS
252}
253
a13a5437 254static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
c3eabb7d
SS
255{
256 ScaleContext *scale = link->dst->priv;
257 int out_h;
5d4890d7 258 AVFilterBufferRef *cur_pic = link->cur_buf;
93f7aeb5 259 const uint8_t *data[4];
c3eabb7d 260
a13a5437
SS
261 if (scale->slice_y == 0 && slice_dir == -1)
262 scale->slice_y = link->dst->outputs[0]->h;
c3eabb7d
SS
263
264 data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
265 data[1] = scale->input_is_pal ?
266 cur_pic->data[1] :
267 cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
268 data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
269 data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
270
271 out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
5d4890d7
HM
272 link->dst->outputs[0]->out_buf->data,
273 link->dst->outputs[0]->out_buf->linesize);
c3eabb7d 274
a13a5437 275 if (slice_dir == -1)
c3eabb7d 276 scale->slice_y -= out_h;
a13a5437
SS
277 avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
278 if (slice_dir == 1)
c3eabb7d
SS
279 scale->slice_y += out_h;
280}
281
282AVFilter avfilter_vf_scale = {
283 .name = "scale",
a6881c6a 284 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
c3eabb7d
SS
285
286 .init = init,
287 .uninit = uninit,
288
289 .query_formats = query_formats,
290
291 .priv_size = sizeof(ScaleContext),
292
293 .inputs = (AVFilterPad[]) {{ .name = "default",
72415b2a 294 .type = AVMEDIA_TYPE_VIDEO,
c3eabb7d
SS
295 .start_frame = start_frame,
296 .draw_slice = draw_slice,
297 .min_perms = AV_PERM_READ, },
298 { .name = NULL}},
299 .outputs = (AVFilterPad[]) {{ .name = "default",
72415b2a 300 .type = AVMEDIA_TYPE_VIDEO,
c3eabb7d
SS
301 .config_props = config_props, },
302 { .name = NULL}},
303};