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