]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_scale.c
lavfi: make formats API private on next bump.
[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 "libavutil/avstring.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "libswscale/swscale.h"
34
35 static const char *const var_names[] = {
36     "PI",
37     "PHI",
38     "E",
39     "in_w",   "iw",
40     "in_h",   "ih",
41     "out_w",  "ow",
42     "out_h",  "oh",
43     "a", "dar",
44     "sar",
45     "hsub",
46     "vsub",
47     NULL
48 };
49
50 enum var_name {
51     VAR_PI,
52     VAR_PHI,
53     VAR_E,
54     VAR_IN_W,   VAR_IW,
55     VAR_IN_H,   VAR_IH,
56     VAR_OUT_W,  VAR_OW,
57     VAR_OUT_H,  VAR_OH,
58     VAR_A, VAR_DAR,
59     VAR_SAR,
60     VAR_HSUB,
61     VAR_VSUB,
62     VARS_NB
63 };
64
65 typedef struct {
66     struct SwsContext *sws;     ///< software scaler context
67
68     /**
69      * New dimensions. Special values are:
70      *   0 = original width/height
71      *  -1 = keep original aspect
72      */
73     int w, h;
74     unsigned int flags;         ///sws flags
75
76     int hsub, vsub;             ///< chroma subsampling
77     int slice_y;                ///< top of current output slice
78     int input_is_pal;           ///< set to 1 if the input format is paletted
79
80     char w_expr[256];           ///< width  expression string
81     char h_expr[256];           ///< height expression string
82 } ScaleContext;
83
84 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
85 {
86     ScaleContext *scale = ctx->priv;
87     const char *p;
88
89     av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
90     av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
91
92     scale->flags = SWS_BILINEAR;
93     if (args) {
94         sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
95         p = strstr(args,"flags=");
96         if (p) {
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, p + 6, &scale->flags);
101
102             if (ret < 0)
103                 return ret;
104         }
105     }
106
107     return 0;
108 }
109
110 static av_cold void uninit(AVFilterContext *ctx)
111 {
112     ScaleContext *scale = ctx->priv;
113     sws_freeContext(scale->sws);
114     scale->sws = NULL;
115 }
116
117 static int query_formats(AVFilterContext *ctx)
118 {
119     AVFilterFormats *formats;
120     enum PixelFormat pix_fmt;
121     int ret;
122
123     if (ctx->inputs[0]) {
124         formats = NULL;
125         for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
126             if (   sws_isSupportedInput(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 < PIX_FMT_NB; pix_fmt++)
136             if (    sws_isSupportedOutput(pix_fmt)
137                 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
138                 ff_formats_unref(&formats);
139                 return ret;
140             }
141         ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
142     }
143
144     return 0;
145 }
146
147 static int config_props(AVFilterLink *outlink)
148 {
149     AVFilterContext *ctx = outlink->src;
150     AVFilterLink *inlink = outlink->src->inputs[0];
151     ScaleContext *scale = ctx->priv;
152     int64_t w, h;
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;
164     var_values[VAR_DAR]   = var_values[VAR_A]  = (float) inlink->w / inlink->h;
165     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
166         (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
167     var_values[VAR_HSUB]  = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
168     var_values[VAR_VSUB]  = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
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;
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 */
216     av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
217            inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
218            outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
219            scale->flags);
220
221     scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
222                           av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PSEUDOPAL;
223
224     if (scale->sws)
225         sws_freeContext(scale->sws);
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     }
236
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
245     return 0;
246
247 fail:
248     av_log(NULL, AV_LOG_ERROR,
249            "Error when evaluating the expression '%s'\n", expr);
250     return ret;
251 }
252
253 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
254 {
255     ScaleContext *scale = link->dst->priv;
256     AVFilterLink *outlink = link->dst->outputs[0];
257     AVFilterBufferRef *outpicref;
258
259     if (!scale->sws) {
260         avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
261         return;
262     }
263
264     scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
265     scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
266
267     outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
268     avfilter_copy_buffer_ref_props(outpicref, picref);
269     outpicref->video->w = outlink->w;
270     outpicref->video->h = outlink->h;
271
272     outlink->out_buf = outpicref;
273
274     av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
275               (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
276               (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
277               INT_MAX);
278
279     scale->slice_y = 0;
280     avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
281 }
282
283 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
284 {
285     ScaleContext *scale = link->dst->priv;
286     int out_h;
287     AVFilterBufferRef *cur_pic = link->cur_buf;
288     const uint8_t *data[4];
289
290     if (!scale->sws) {
291         avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
292         return;
293     }
294
295     if (scale->slice_y == 0 && slice_dir == -1)
296         scale->slice_y = link->dst->outputs[0]->h;
297
298     data[0] = cur_pic->data[0] +  y               * cur_pic->linesize[0];
299     data[1] = scale->input_is_pal ?
300               cur_pic->data[1] :
301               cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
302     data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
303     data[3] = cur_pic->data[3] +  y               * cur_pic->linesize[3];
304
305     out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
306                       link->dst->outputs[0]->out_buf->data,
307                       link->dst->outputs[0]->out_buf->linesize);
308
309     if (slice_dir == -1)
310         scale->slice_y -= out_h;
311     avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
312     if (slice_dir == 1)
313         scale->slice_y += out_h;
314 }
315
316 AVFilter avfilter_vf_scale = {
317     .name      = "scale",
318     .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
319
320     .init      = init,
321     .uninit    = uninit,
322
323     .query_formats = query_formats,
324
325     .priv_size = sizeof(ScaleContext),
326
327     .inputs    = (AVFilterPad[]) {{ .name             = "default",
328                                     .type             = AVMEDIA_TYPE_VIDEO,
329                                     .start_frame      = start_frame,
330                                     .draw_slice       = draw_slice,
331                                     .min_perms        = AV_PERM_READ, },
332                                   { .name = NULL}},
333     .outputs   = (AVFilterPad[]) {{ .name             = "default",
334                                     .type             = AVMEDIA_TYPE_VIDEO,
335                                     .config_props     = config_props, },
336                                   { .name = NULL}},
337 };