avplay: use buffersrc instead of custom input filter.
[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"
90f65dc6 30#include "libavutil/opt.h"
b4b66456 31#include "libavutil/pixdesc.h"
c3eabb7d
SS
32#include "libswscale/swscale.h"
33
b0f29db5 34static const char *const var_names[] = {
68e23c08
SS
35 "PI",
36 "PHI",
37 "E",
38 "in_w", "iw",
39 "in_h", "ih",
40 "out_w", "ow",
41 "out_h", "oh",
46b29397
SS
42 "a", "dar",
43 "sar",
68e23c08
SS
44 "hsub",
45 "vsub",
46 NULL
47};
48
49enum var_name {
50 VAR_PI,
51 VAR_PHI,
52 VAR_E,
53 VAR_IN_W, VAR_IW,
54 VAR_IN_H, VAR_IH,
55 VAR_OUT_W, VAR_OW,
56 VAR_OUT_H, VAR_OH,
46b29397
SS
57 VAR_A, VAR_DAR,
58 VAR_SAR,
68e23c08
SS
59 VAR_HSUB,
60 VAR_VSUB,
61 VARS_NB
62};
63
c3eabb7d
SS
64typedef struct {
65 struct SwsContext *sws; ///< software scaler context
66
67 /**
68 * New dimensions. Special values are:
69 * 0 = original width/height
70 * -1 = keep original aspect
71 */
72 int w, h;
8155bbc9 73 unsigned int flags; ///sws flags
c3eabb7d
SS
74
75 int hsub, vsub; ///< chroma subsampling
76 int slice_y; ///< top of current output slice
c3eabb7d 77 int input_is_pal; ///< set to 1 if the input format is paletted
68e23c08
SS
78
79 char w_expr[256]; ///< width expression string
80 char h_expr[256]; ///< height expression string
c3eabb7d
SS
81} ScaleContext;
82
83static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
84{
85 ScaleContext *scale = ctx->priv;
a0aa7354 86 const char *p;
c3eabb7d 87
68e23c08
SS
88 av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
89 av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
90
8155bbc9 91 scale->flags = SWS_BILINEAR;
46daee6e 92 if (args) {
68e23c08 93 sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
46daee6e 94 p = strstr(args,"flags=");
90f65dc6
AK
95 if (p) {
96 const AVClass *class = sws_get_class();
97 const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
98 AV_OPT_SEARCH_FAKE_OBJ);
99 int ret = av_opt_eval_flags(&class, o, p + 6, &scale->flags);
100
101 if (ret < 0)
102 return ret;
103 }
8155bbc9 104 }
c3eabb7d 105
c3eabb7d
SS
106 return 0;
107}
108
109static av_cold void uninit(AVFilterContext *ctx)
110{
111 ScaleContext *scale = ctx->priv;
112 sws_freeContext(scale->sws);
113 scale->sws = NULL;
114}
115
116static int query_formats(AVFilterContext *ctx)
117{
118 AVFilterFormats *formats;
27d8f6b6
SS
119 enum PixelFormat pix_fmt;
120 int ret;
c3eabb7d
SS
121
122 if (ctx->inputs[0]) {
27d8f6b6
SS
123 formats = NULL;
124 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
125 if ( sws_isSupportedInput(pix_fmt)
bdab614b 126 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
27d8f6b6
SS
127 avfilter_formats_unref(&formats);
128 return ret;
129 }
c3eabb7d
SS
130 avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
131 }
132 if (ctx->outputs[0]) {
27d8f6b6
SS
133 formats = NULL;
134 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
135 if ( sws_isSupportedOutput(pix_fmt)
bdab614b 136 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
27d8f6b6
SS
137 avfilter_formats_unref(&formats);
138 return ret;
139 }
c3eabb7d
SS
140 avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
141 }
142
143 return 0;
144}
145
146static int config_props(AVFilterLink *outlink)
147{
148 AVFilterContext *ctx = outlink->src;
149 AVFilterLink *inlink = outlink->src->inputs[0];
150 ScaleContext *scale = ctx->priv;
151 int64_t w, h;
68e23c08
SS
152 double var_values[VARS_NB], res;
153 char *expr;
154 int ret;
155
156 var_values[VAR_PI] = M_PI;
157 var_values[VAR_PHI] = M_PHI;
158 var_values[VAR_E] = M_E;
159 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
160 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
161 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
162 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
46b29397
SS
163 var_values[VAR_DAR] = var_values[VAR_A] = (float) inlink->w / inlink->h;
164 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
165 (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
68e23c08
SS
166 var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
167 var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
168
169 /* evaluate width and height */
170 av_expr_parse_and_eval(&res, (expr = scale->w_expr),
171 var_names, var_values,
172 NULL, NULL, NULL, NULL, NULL, 0, ctx);
173 scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
174 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
175 var_names, var_values,
176 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
177 goto fail;
178 scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
179 /* evaluate again the width, as it may depend on the output height */
180 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
181 var_names, var_values,
182 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
183 goto fail;
184 scale->w = res;
185
186 w = scale->w;
187 h = scale->h;
188
189 /* sanity check params */
190 if (w < -1 || h < -1) {
191 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
192 return AVERROR(EINVAL);
193 }
194 if (w == -1 && h == -1)
195 scale->w = scale->h = 0;
c3eabb7d
SS
196
197 if (!(w = scale->w))
198 w = inlink->w;
199 if (!(h = scale->h))
200 h = inlink->h;
201 if (w == -1)
202 w = av_rescale(h, inlink->w, inlink->h);
203 if (h == -1)
204 h = av_rescale(w, inlink->h, inlink->w);
205
206 if (w > INT_MAX || h > INT_MAX ||
207 (h * inlink->w) > INT_MAX ||
208 (w * inlink->h) > INT_MAX)
209 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
210
211 outlink->w = w;
212 outlink->h = h;
213
214 /* TODO: make algorithm configurable */
47f0704e 215 av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
38efdb2c
SS
216 inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
217 outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
218 scale->flags);
c3eabb7d 219
38d55332
AK
220 scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
221 av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PSEUDOPAL;
c3eabb7d 222
52982dbe
MN
223 if (scale->sws)
224 sws_freeContext(scale->sws);
416fd90e
AK
225 if (inlink->w == outlink->w && inlink->h == outlink->h &&
226 inlink->format == outlink->format)
227 scale->sws = NULL;
228 else {
229 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
230 outlink->w, outlink->h, outlink->format,
231 scale->flags, NULL, NULL, NULL);
232 if (!scale->sws)
233 return AVERROR(EINVAL);
234 }
d760a316 235
d33e0c6b
MN
236
237 if (inlink->sample_aspect_ratio.num)
238 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
239 outlink->w*inlink->h},
240 inlink->sample_aspect_ratio);
241 else
242 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
243
dccdd93e 244 return 0;
68e23c08
SS
245
246fail:
247 av_log(NULL, AV_LOG_ERROR,
248 "Error when evaluating the expression '%s'\n", expr);
249 return ret;
c3eabb7d
SS
250}
251
ecc8dada 252static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
c3eabb7d
SS
253{
254 ScaleContext *scale = link->dst->priv;
255 AVFilterLink *outlink = link->dst->outputs[0];
ecc8dada 256 AVFilterBufferRef *outpicref;
c3eabb7d 257
416fd90e
AK
258 if (!scale->sws) {
259 avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
260 return;
261 }
262
b4b66456
SS
263 scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
264 scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
b37aa4de 265
c3eabb7d 266 outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
7fce481a 267 avfilter_copy_buffer_ref_props(outpicref, picref);
e4cc9f20
BC
268 outpicref->video->w = outlink->w;
269 outpicref->video->h = outlink->h;
efdc74ef 270
5d4890d7 271 outlink->out_buf = outpicref;
c3eabb7d 272
cc80caff
HM
273 av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
274 (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
275 (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
c3eabb7d
SS
276 INT_MAX);
277
a13a5437 278 scale->slice_y = 0;
7fce481a 279 avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
c3eabb7d
SS
280}
281
a13a5437 282static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
c3eabb7d
SS
283{
284 ScaleContext *scale = link->dst->priv;
285 int out_h;
5d4890d7 286 AVFilterBufferRef *cur_pic = link->cur_buf;
93f7aeb5 287 const uint8_t *data[4];
c3eabb7d 288
416fd90e
AK
289 if (!scale->sws) {
290 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
291 return;
292 }
293
a13a5437
SS
294 if (scale->slice_y == 0 && slice_dir == -1)
295 scale->slice_y = link->dst->outputs[0]->h;
c3eabb7d
SS
296
297 data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
298 data[1] = scale->input_is_pal ?
299 cur_pic->data[1] :
300 cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
301 data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
302 data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
303
304 out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
5d4890d7
HM
305 link->dst->outputs[0]->out_buf->data,
306 link->dst->outputs[0]->out_buf->linesize);
c3eabb7d 307
a13a5437 308 if (slice_dir == -1)
c3eabb7d 309 scale->slice_y -= out_h;
a13a5437
SS
310 avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
311 if (slice_dir == 1)
c3eabb7d
SS
312 scale->slice_y += out_h;
313}
314
315AVFilter avfilter_vf_scale = {
316 .name = "scale",
a6881c6a 317 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
c3eabb7d
SS
318
319 .init = init,
320 .uninit = uninit,
321
322 .query_formats = query_formats,
323
324 .priv_size = sizeof(ScaleContext),
325
326 .inputs = (AVFilterPad[]) {{ .name = "default",
72415b2a 327 .type = AVMEDIA_TYPE_VIDEO,
c3eabb7d
SS
328 .start_frame = start_frame,
329 .draw_slice = draw_slice,
330 .min_perms = AV_PERM_READ, },
331 { .name = NULL}},
332 .outputs = (AVFilterPad[]) {{ .name = "default",
72415b2a 333 .type = AVMEDIA_TYPE_VIDEO,
c3eabb7d
SS
334 .config_props = config_props, },
335 { .name = NULL}},
336};