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