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