h264_metadata: Add option to delete filler data
[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
58400ac1 71typedef struct ScaleContext {
c334c113 72 const AVClass *class;
c3eabb7d
SS
73 struct SwsContext *sws; ///< software scaler context
74
75 /**
76 * New dimensions. Special values are:
77 * 0 = original width/height
78 * -1 = keep original aspect
79 */
80 int w, h;
8155bbc9 81 unsigned int flags; ///sws flags
b1abd2aa 82 double param[2]; // sws params
c3eabb7d
SS
83
84 int hsub, vsub; ///< chroma subsampling
85 int slice_y; ///< top of current output slice
c3eabb7d 86 int input_is_pal; ///< set to 1 if the input format is paletted
68e23c08 87
c334c113
AK
88 char *w_expr; ///< width expression string
89 char *h_expr; ///< height expression string
90 char *flags_str;
c3eabb7d
SS
91} ScaleContext;
92
c43a7eca 93static av_cold int init(AVFilterContext *ctx)
c3eabb7d
SS
94{
95 ScaleContext *scale = ctx->priv;
c334c113
AK
96
97 if (scale->flags_str) {
98 const AVClass *class = sws_get_class();
99 const AVOption *o = av_opt_find(&class, "sws_flags", NULL, 0,
100 AV_OPT_SEARCH_FAKE_OBJ);
101 int ret = av_opt_eval_flags(&class, o, scale->flags_str, &scale->flags);
102
103 if (ret < 0)
104 return ret;
8155bbc9 105 }
c3eabb7d 106
c3eabb7d
SS
107 return 0;
108}
109
110static av_cold void uninit(AVFilterContext *ctx)
111{
112 ScaleContext *scale = ctx->priv;
113 sws_freeContext(scale->sws);
114 scale->sws = NULL;
115}
116
117static int query_formats(AVFilterContext *ctx)
118{
119 AVFilterFormats *formats;
716d413c 120 enum AVPixelFormat pix_fmt;
27d8f6b6 121 int ret;
c3eabb7d
SS
122
123 if (ctx->inputs[0]) {
862f33c1 124 const AVPixFmtDescriptor *desc = NULL;
27d8f6b6 125 formats = NULL;
862f33c1
AK
126 while ((desc = av_pix_fmt_desc_next(desc))) {
127 pix_fmt = av_pix_fmt_desc_get_id(desc);
5f87c277
JG
128 if ((sws_isSupportedInput(pix_fmt) ||
129 sws_isSupportedEndiannessConversion(pix_fmt))
b74a1da4
AK
130 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
131 ff_formats_unref(&formats);
27d8f6b6
SS
132 return ret;
133 }
862f33c1 134 }
b74a1da4 135 ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
c3eabb7d
SS
136 }
137 if (ctx->outputs[0]) {
862f33c1 138 const AVPixFmtDescriptor *desc = NULL;
27d8f6b6 139 formats = NULL;
862f33c1
AK
140 while ((desc = av_pix_fmt_desc_next(desc))) {
141 pix_fmt = av_pix_fmt_desc_get_id(desc);
5f87c277
JG
142 if ((sws_isSupportedOutput(pix_fmt) ||
143 sws_isSupportedEndiannessConversion(pix_fmt))
b74a1da4
AK
144 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
145 ff_formats_unref(&formats);
27d8f6b6
SS
146 return ret;
147 }
862f33c1 148 }
b74a1da4 149 ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
c3eabb7d
SS
150 }
151
152 return 0;
153}
154
155static int config_props(AVFilterLink *outlink)
156{
157 AVFilterContext *ctx = outlink->src;
158 AVFilterLink *inlink = outlink->src->inputs[0];
159 ScaleContext *scale = ctx->priv;
59ee9f78 160 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
c3eabb7d 161 int64_t w, h;
68e23c08
SS
162 double var_values[VARS_NB], res;
163 char *expr;
164 int ret;
165
166 var_values[VAR_PI] = M_PI;
167 var_values[VAR_PHI] = M_PHI;
168 var_values[VAR_E] = M_E;
169 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
170 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
171 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
172 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
3a14b160 173 var_values[VAR_A] = (double) inlink->w / inlink->h;
46b29397 174 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
ba04177e 175 (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
3a14b160 176 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
59ee9f78
AK
177 var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
178 var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
68e23c08
SS
179
180 /* evaluate width and height */
3735b5c6
AK
181 av_expr_parse_and_eval(&res, (expr = scale->w_expr),
182 var_names, var_values,
183 NULL, NULL, NULL, NULL, NULL, 0, ctx);
68e23c08
SS
184 scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
185 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
186 var_names, var_values,
187 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
188 goto fail;
189 scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
190 /* evaluate again the width, as it may depend on the output height */
191 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
192 var_names, var_values,
193 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
194 goto fail;
195 scale->w = res;
196
197 w = scale->w;
198 h = scale->h;
199
200 /* sanity check params */
201 if (w < -1 || h < -1) {
202 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
203 return AVERROR(EINVAL);
204 }
205 if (w == -1 && h == -1)
206 scale->w = scale->h = 0;
c3eabb7d
SS
207
208 if (!(w = scale->w))
209 w = inlink->w;
210 if (!(h = scale->h))
211 h = inlink->h;
212 if (w == -1)
213 w = av_rescale(h, inlink->w, inlink->h);
214 if (h == -1)
215 h = av_rescale(w, inlink->h, inlink->w);
216
217 if (w > INT_MAX || h > INT_MAX ||
218 (h * inlink->w) > INT_MAX ||
219 (w * inlink->h) > INT_MAX)
220 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
221
222 outlink->w = w;
223 outlink->h = h;
224
225 /* TODO: make algorithm configurable */
1a49a169 226 av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
59ee9f78
AK
227 inlink ->w, inlink ->h, av_get_pix_fmt_name(inlink->format),
228 outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
38efdb2c 229 scale->flags);
c3eabb7d 230
e6c4ac7b
AK
231 scale->input_is_pal = desc->flags & AV_PIX_FMT_FLAG_PAL ||
232 desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL;
c3eabb7d 233
52982dbe
MN
234 if (scale->sws)
235 sws_freeContext(scale->sws);
416fd90e
AK
236 if (inlink->w == outlink->w && inlink->h == outlink->h &&
237 inlink->format == outlink->format)
238 scale->sws = NULL;
239 else {
240 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
241 outlink->w, outlink->h, outlink->format,
b1abd2aa 242 scale->flags, NULL, NULL, scale->param);
416fd90e
AK
243 if (!scale->sws)
244 return AVERROR(EINVAL);
245 }
d760a316 246
d33e0c6b
MN
247
248 if (inlink->sample_aspect_ratio.num)
249 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
250 outlink->w*inlink->h},
251 inlink->sample_aspect_ratio);
252 else
253 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
254
dccdd93e 255 return 0;
68e23c08
SS
256
257fail:
258 av_log(NULL, AV_LOG_ERROR,
259 "Error when evaluating the expression '%s'\n", expr);
260 return ret;
c3eabb7d
SS
261}
262
7e350379 263static int filter_frame(AVFilterLink *link, AVFrame *in)
c3eabb7d
SS
264{
265 ScaleContext *scale = link->dst->priv;
266 AVFilterLink *outlink = link->dst->outputs[0];
7e350379 267 AVFrame *out;
59ee9f78 268 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
c3eabb7d 269
565e4993
AK
270 if (!scale->sws)
271 return ff_filter_frame(outlink, in);
416fd90e 272
59ee9f78
AK
273 scale->hsub = desc->log2_chroma_w;
274 scale->vsub = desc->log2_chroma_h;
b37aa4de 275
7e350379 276 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
565e4993 277 if (!out) {
7e350379 278 av_frame_free(&in);
ebc8d974 279 return AVERROR(ENOMEM);
ebc8d974
AK
280 }
281
7e350379
AK
282 av_frame_copy_props(out, in);
283 out->width = outlink->w;
284 out->height = outlink->h;
c3eabb7d 285
7e350379
AK
286 av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
287 (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
288 (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
565e4993 289 INT_MAX);
c3eabb7d 290
7e350379 291 sws_scale(scale->sws, in->data, in->linesize, 0, in->height,
565e4993 292 out->data, out->linesize);
416fd90e 293
7e350379 294 av_frame_free(&in);
565e4993 295 return ff_filter_frame(outlink, out);
c3eabb7d
SS
296}
297
c334c113
AK
298#define OFFSET(x) offsetof(ScaleContext, x)
299#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
300static const AVOption options[] = {
301 { "w", "Output video width", OFFSET(w_expr), AV_OPT_TYPE_STRING, { .str = "iw" }, .flags = FLAGS },
302 { "h", "Output video height", OFFSET(h_expr), AV_OPT_TYPE_STRING, { .str = "ih" }, .flags = FLAGS },
303 { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
b1abd2aa
LB
304 { "param0", "Scaler param 0", OFFSET(param[0]), AV_OPT_TYPE_DOUBLE, { .dbl = SWS_PARAM_DEFAULT }, INT_MIN, INT_MAX, FLAGS },
305 { "param1", "Scaler param 1", OFFSET(param[1]), AV_OPT_TYPE_DOUBLE, { .dbl = SWS_PARAM_DEFAULT }, INT_MIN, INT_MAX, FLAGS },
c334c113
AK
306 { NULL },
307};
308
309static const AVClass scale_class = {
310 .class_name = "scale",
311 .item_name = av_default_item_name,
312 .option = options,
313 .version = LIBAVUTIL_VERSION_INT,
314};
315
568c70e7
MR
316static const AVFilterPad avfilter_vf_scale_inputs[] = {
317 {
318 .name = "default",
319 .type = AVMEDIA_TYPE_VIDEO,
565e4993 320 .filter_frame = filter_frame,
568c70e7
MR
321 },
322 { NULL }
323};
324
325static const AVFilterPad avfilter_vf_scale_outputs[] = {
326 {
327 .name = "default",
328 .type = AVMEDIA_TYPE_VIDEO,
329 .config_props = config_props,
330 },
331 { NULL }
332};
333
cd43ca04 334AVFilter ff_vf_scale = {
c3eabb7d 335 .name = "scale",
a6881c6a 336 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
c3eabb7d
SS
337
338 .init = init,
339 .uninit = uninit,
340
341 .query_formats = query_formats,
342
343 .priv_size = sizeof(ScaleContext),
c334c113 344 .priv_class = &scale_class,
c3eabb7d 345
568c70e7
MR
346 .inputs = avfilter_vf_scale_inputs,
347 .outputs = avfilter_vf_scale_outputs,
c3eabb7d 348};