]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_scale.c
4e51b4f9f542882622601071280c4a1b9a391c82
[ffmpeg] / libavfilter / vf_scale.c
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * scale video filter
24  */
25
26 #include "avfilter.h"
27 #include "formats.h"
28 #include "internal.h"
29 #include "video.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/eval.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "libswscale/swscale.h"
36
37 static const char *const var_names[] = {
38     "PI",
39     "PHI",
40     "E",
41     "in_w",   "iw",
42     "in_h",   "ih",
43     "out_w",  "ow",
44     "out_h",  "oh",
45     "a", "dar",
46     "sar",
47     "hsub",
48     "vsub",
49     NULL
50 };
51
52 enum 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,
60     VAR_A, VAR_DAR,
61     VAR_SAR,
62     VAR_HSUB,
63     VAR_VSUB,
64     VARS_NB
65 };
66
67 typedef 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;
76     unsigned int flags;         ///sws flags
77
78     int hsub, vsub;             ///< chroma subsampling
79     int slice_y;                ///< top of current output slice
80     int input_is_pal;           ///< set to 1 if the input format is paletted
81
82     char w_expr[256];           ///< width  expression string
83     char h_expr[256];           ///< height expression string
84 } ScaleContext;
85
86 static av_cold int init(AVFilterContext *ctx, const char *args)
87 {
88     ScaleContext *scale = ctx->priv;
89     const char *p;
90
91     av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
92     av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
93
94     scale->flags = SWS_BILINEAR;
95     if (args) {
96         sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
97         p = strstr(args,"flags=");
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         }
107     }
108
109     return 0;
110 }
111
112 static av_cold void uninit(AVFilterContext *ctx)
113 {
114     ScaleContext *scale = ctx->priv;
115     sws_freeContext(scale->sws);
116     scale->sws = NULL;
117 }
118
119 static int query_formats(AVFilterContext *ctx)
120 {
121     AVFilterFormats *formats;
122     enum PixelFormat pix_fmt;
123     int ret;
124
125     if (ctx->inputs[0]) {
126         formats = NULL;
127         for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
128             if (   sws_isSupportedInput(pix_fmt)
129                 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
130                 ff_formats_unref(&formats);
131                 return ret;
132             }
133         ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
134     }
135     if (ctx->outputs[0]) {
136         formats = NULL;
137         for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
138             if (    sws_isSupportedOutput(pix_fmt)
139                 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
140                 ff_formats_unref(&formats);
141                 return ret;
142             }
143         ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
144     }
145
146     return 0;
147 }
148
149 static 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;
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;
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;
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;
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 */
218     av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
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);
222
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;
225
226     if (scale->sws)
227         sws_freeContext(scale->sws);
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     }
238
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
247     return 0;
248
249 fail:
250     av_log(NULL, AV_LOG_ERROR,
251            "Error when evaluating the expression '%s'\n", expr);
252     return ret;
253 }
254
255 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
256 {
257     ScaleContext *scale = link->dst->priv;
258     AVFilterLink *outlink = link->dst->outputs[0];
259     AVFilterBufferRef *outpicref;
260
261     if (!scale->sws) {
262         ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
263         return;
264     }
265
266     scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
267     scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
268
269     outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
270     avfilter_copy_buffer_ref_props(outpicref, picref);
271     outpicref->video->w = outlink->w;
272     outpicref->video->h = outlink->h;
273
274     outlink->out_buf = outpicref;
275
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,
279               INT_MAX);
280
281     scale->slice_y = 0;
282     ff_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
283 }
284
285 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
286 {
287     ScaleContext *scale = link->dst->priv;
288     int out_h;
289     AVFilterBufferRef *cur_pic = link->cur_buf;
290     const uint8_t *data[4];
291
292     if (!scale->sws) {
293         ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
294         return;
295     }
296
297     if (scale->slice_y == 0 && slice_dir == -1)
298         scale->slice_y = link->dst->outputs[0]->h;
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,
308                       link->dst->outputs[0]->out_buf->data,
309                       link->dst->outputs[0]->out_buf->linesize);
310
311     if (slice_dir == -1)
312         scale->slice_y -= out_h;
313     ff_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
314     if (slice_dir == 1)
315         scale->slice_y += out_h;
316 }
317
318 AVFilter avfilter_vf_scale = {
319     .name      = "scale",
320     .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
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",
330                                     .type             = AVMEDIA_TYPE_VIDEO,
331                                     .start_frame      = start_frame,
332                                     .draw_slice       = draw_slice,
333                                     .min_perms        = AV_PERM_READ, },
334                                   { .name = NULL}},
335     .outputs   = (AVFilterPad[]) {{ .name             = "default",
336                                     .type             = AVMEDIA_TYPE_VIDEO,
337                                     .config_props     = config_props, },
338                                   { .name = NULL}},
339 };