]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_scale.c
pixdesc: rename PIX_FMT_* flags to AV_PIX_FMT_FLAG_*
[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 <stdio.h>
27 #include <string.h>
28
29 #include "avfilter.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "video.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/eval.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/pixdesc.h"
39 #include "libswscale/swscale.h"
40
41 static const char *const var_names[] = {
42     "PI",
43     "PHI",
44     "E",
45     "in_w",   "iw",
46     "in_h",   "ih",
47     "out_w",  "ow",
48     "out_h",  "oh",
49     "a", "dar",
50     "sar",
51     "hsub",
52     "vsub",
53     NULL
54 };
55
56 enum 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,
64     VAR_A, VAR_DAR,
65     VAR_SAR,
66     VAR_HSUB,
67     VAR_VSUB,
68     VARS_NB
69 };
70
71 typedef struct {
72     const AVClass *class;
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;
81     unsigned int flags;         ///sws flags
82
83     int hsub, vsub;             ///< chroma subsampling
84     int slice_y;                ///< top of current output slice
85     int input_is_pal;           ///< set to 1 if the input format is paletted
86
87     char *w_expr;               ///< width  expression string
88     char *h_expr;               ///< height expression string
89     char *flags_str;
90 } ScaleContext;
91
92 static av_cold int init(AVFilterContext *ctx)
93 {
94     ScaleContext *scale = ctx->priv;
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;
104     }
105
106     return 0;
107 }
108
109 static av_cold void uninit(AVFilterContext *ctx)
110 {
111     ScaleContext *scale = ctx->priv;
112     sws_freeContext(scale->sws);
113     scale->sws = NULL;
114 }
115
116 static int query_formats(AVFilterContext *ctx)
117 {
118     AVFilterFormats *formats;
119     enum AVPixelFormat pix_fmt;
120     int ret;
121
122     if (ctx->inputs[0]) {
123         formats = NULL;
124         for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
125             if ((sws_isSupportedInput(pix_fmt) ||
126                  sws_isSupportedEndiannessConversion(pix_fmt))
127                 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
128                 ff_formats_unref(&formats);
129                 return ret;
130             }
131         ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
132     }
133     if (ctx->outputs[0]) {
134         formats = NULL;
135         for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++)
136             if ((sws_isSupportedOutput(pix_fmt) ||
137                  sws_isSupportedEndiannessConversion(pix_fmt))
138                 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
139                 ff_formats_unref(&formats);
140                 return ret;
141             }
142         ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
143     }
144
145     return 0;
146 }
147
148 static int config_props(AVFilterLink *outlink)
149 {
150     AVFilterContext *ctx = outlink->src;
151     AVFilterLink *inlink = outlink->src->inputs[0];
152     ScaleContext *scale = ctx->priv;
153     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
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]  = (double) inlink->w / inlink->h;
167     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
168         (double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
169     var_values[VAR_HSUB]  = 1 << desc->log2_chroma_w;
170     var_values[VAR_VSUB]  = 1 << desc->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_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
219            inlink ->w, inlink ->h, av_get_pix_fmt_name(inlink->format),
220            outlink->w, outlink->h, av_get_pix_fmt_name(outlink->format),
221            scale->flags);
222
223     scale->input_is_pal = desc->flags & AV_PIX_FMT_FLAG_PAL ||
224                           desc->flags & AV_PIX_FMT_FLAG_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 int filter_frame(AVFilterLink *link, AVFrame *in)
256 {
257     ScaleContext *scale = link->dst->priv;
258     AVFilterLink *outlink = link->dst->outputs[0];
259     AVFrame *out;
260     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
261
262     if (!scale->sws)
263         return ff_filter_frame(outlink, in);
264
265     scale->hsub = desc->log2_chroma_w;
266     scale->vsub = desc->log2_chroma_h;
267
268     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
269     if (!out) {
270         av_frame_free(&in);
271         return AVERROR(ENOMEM);
272     }
273
274     av_frame_copy_props(out, in);
275     out->width  = outlink->w;
276     out->height = outlink->h;
277
278     av_reduce(&out->sample_aspect_ratio.num, &out->sample_aspect_ratio.den,
279               (int64_t)in->sample_aspect_ratio.num * outlink->h * link->w,
280               (int64_t)in->sample_aspect_ratio.den * outlink->w * link->h,
281               INT_MAX);
282
283     sws_scale(scale->sws, in->data, in->linesize, 0, in->height,
284               out->data, out->linesize);
285
286     av_frame_free(&in);
287     return ff_filter_frame(outlink, out);
288 }
289
290 #define OFFSET(x) offsetof(ScaleContext, x)
291 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
292 static const AVOption options[] = {
293     { "w",     "Output video width",          OFFSET(w_expr),    AV_OPT_TYPE_STRING, { .str = "iw" },       .flags = FLAGS },
294     { "h",     "Output video height",         OFFSET(h_expr),    AV_OPT_TYPE_STRING, { .str = "ih" },       .flags = FLAGS },
295     { "flags", "Flags to pass to libswscale", OFFSET(flags_str), AV_OPT_TYPE_STRING, { .str = "bilinear" }, .flags = FLAGS },
296     { NULL },
297 };
298
299 static const AVClass scale_class = {
300     .class_name = "scale",
301     .item_name  = av_default_item_name,
302     .option     = options,
303     .version    = LIBAVUTIL_VERSION_INT,
304 };
305
306 static const AVFilterPad avfilter_vf_scale_inputs[] = {
307     {
308         .name        = "default",
309         .type        = AVMEDIA_TYPE_VIDEO,
310         .filter_frame = filter_frame,
311     },
312     { NULL }
313 };
314
315 static const AVFilterPad avfilter_vf_scale_outputs[] = {
316     {
317         .name         = "default",
318         .type         = AVMEDIA_TYPE_VIDEO,
319         .config_props = config_props,
320     },
321     { NULL }
322 };
323
324 AVFilter avfilter_vf_scale = {
325     .name      = "scale",
326     .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
327
328     .init      = init,
329     .uninit    = uninit,
330
331     .query_formats = query_formats,
332
333     .priv_size = sizeof(ScaleContext),
334     .priv_class = &scale_class,
335
336     .inputs    = avfilter_vf_scale_inputs,
337     .outputs   = avfilter_vf_scale_outputs,
338 };