2
* Copyright (c) 2007 Bobby Bingham
4
* This file is part of Libav.
6
* Libav is free software; you can redistribute it and/or
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.
11
* Libav is distributed in the hope that it will be useful,
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.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with Libav; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27
#include "libavutil/avstring.h"
28
#include "libavutil/eval.h"
29
#include "libavutil/pixdesc.h"
30
#include "libswscale/swscale.h"
32
static const char *var_names[] = {
61
struct SwsContext *sws; ///< software scaler context
64
* New dimensions. Special values are:
65
* 0 = original width/height
66
* -1 = keep original aspect
69
unsigned int flags; ///sws flags
71
int hsub, vsub; ///< chroma subsampling
72
int slice_y; ///< top of current output slice
73
int input_is_pal; ///< set to 1 if the input format is paletted
75
char w_expr[256]; ///< width expression string
76
char h_expr[256]; ///< height expression string
79
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
81
ScaleContext *scale = ctx->priv;
84
av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
85
av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
87
scale->flags = SWS_BILINEAR;
89
sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
90
p = strstr(args,"flags=");
91
if (p) scale->flags = strtoul(p+6, NULL, 0);
97
static av_cold void uninit(AVFilterContext *ctx)
99
ScaleContext *scale = ctx->priv;
100
sws_freeContext(scale->sws);
104
static int query_formats(AVFilterContext *ctx)
106
AVFilterFormats *formats;
107
enum PixelFormat pix_fmt;
110
if (ctx->inputs[0]) {
112
for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
113
if ( sws_isSupportedInput(pix_fmt)
114
&& (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
115
avfilter_formats_unref(&formats);
118
avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
120
if (ctx->outputs[0]) {
122
for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
123
if ( sws_isSupportedOutput(pix_fmt)
124
&& (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
125
avfilter_formats_unref(&formats);
128
avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
134
static int config_props(AVFilterLink *outlink)
136
AVFilterContext *ctx = outlink->src;
137
AVFilterLink *inlink = outlink->src->inputs[0];
138
ScaleContext *scale = ctx->priv;
140
double var_values[VARS_NB], res;
144
var_values[VAR_PI] = M_PI;
145
var_values[VAR_PHI] = M_PHI;
146
var_values[VAR_E] = M_E;
147
var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
148
var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
149
var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
150
var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
151
var_values[VAR_A] = (float) inlink->w / inlink->h;
152
var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
153
var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
155
/* evaluate width and height */
156
av_expr_parse_and_eval(&res, (expr = scale->w_expr),
157
var_names, var_values,
158
NULL, NULL, NULL, NULL, NULL, 0, ctx);
159
scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
160
if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
161
var_names, var_values,
162
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
164
scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
165
/* evaluate again the width, as it may depend on the output height */
166
if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
167
var_names, var_values,
168
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
175
/* sanity check params */
176
if (w < -1 || h < -1) {
177
av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
178
return AVERROR(EINVAL);
180
if (w == -1 && h == -1)
181
scale->w = scale->h = 0;
188
w = av_rescale(h, inlink->w, inlink->h);
190
h = av_rescale(w, inlink->h, inlink->w);
192
if (w > INT_MAX || h > INT_MAX ||
193
(h * inlink->w) > INT_MAX ||
194
(w * inlink->h) > INT_MAX)
195
av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
200
/* TODO: make algorithm configurable */
201
av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
202
inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
203
outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
206
scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL;
208
scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
209
outlink->w, outlink->h, outlink->format,
210
scale->flags, NULL, NULL, NULL);
212
return AVERROR(EINVAL);
217
av_log(NULL, AV_LOG_ERROR,
218
"Error when evaluating the expression '%s'\n", expr);
222
static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
224
ScaleContext *scale = link->dst->priv;
225
AVFilterLink *outlink = link->dst->outputs[0];
226
AVFilterBufferRef *outpicref;
228
scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
229
scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
231
outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
232
avfilter_copy_buffer_ref_props(outpicref, picref);
233
outpicref->video->w = outlink->w;
234
outpicref->video->h = outlink->h;
236
outlink->out_buf = outpicref;
238
av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
239
(int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
240
(int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
244
avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
247
static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
249
ScaleContext *scale = link->dst->priv;
251
AVFilterBufferRef *cur_pic = link->cur_buf;
252
const uint8_t *data[4];
254
if (scale->slice_y == 0 && slice_dir == -1)
255
scale->slice_y = link->dst->outputs[0]->h;
257
data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
258
data[1] = scale->input_is_pal ?
260
cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
261
data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
262
data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
264
out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
265
link->dst->outputs[0]->out_buf->data,
266
link->dst->outputs[0]->out_buf->linesize);
269
scale->slice_y -= out_h;
270
avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
272
scale->slice_y += out_h;
275
AVFilter avfilter_vf_scale = {
277
.description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
282
.query_formats = query_formats,
284
.priv_size = sizeof(ScaleContext),
286
.inputs = (AVFilterPad[]) {{ .name = "default",
287
.type = AVMEDIA_TYPE_VIDEO,
288
.start_frame = start_frame,
289
.draw_slice = draw_slice,
290
.min_perms = AV_PERM_READ, },
292
.outputs = (AVFilterPad[]) {{ .name = "default",
293
.type = AVMEDIA_TYPE_VIDEO,
294
.config_props = config_props, },