]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_scale.c
avfilter: x86: Use more precise compile template names
[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     struct SwsContext *sws;     ///< software scaler context
73
74     /**
75      * New dimensions. Special values are:
76      *   0 = original width/height
77      *  -1 = keep original aspect
78      */
79     int w, h;
80     unsigned int flags;         ///sws flags
81
82     int hsub, vsub;             ///< chroma subsampling
83     int slice_y;                ///< top of current output slice
84     int input_is_pal;           ///< set to 1 if the input format is paletted
85
86     char w_expr[256];           ///< width  expression string
87     char h_expr[256];           ///< height expression string
88 } ScaleContext;
89
90 static av_cold int init(AVFilterContext *ctx, const char *args)
91 {
92     ScaleContext *scale = ctx->priv;
93     const char *p;
94
95     av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
96     av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
97
98     scale->flags = SWS_BILINEAR;
99     if (args) {
100         sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
101         p = strstr(args,"flags=");
102         if (p) {
103             const AVClass *class = sws_get_class();
104             const AVOption    *o = av_opt_find(&class, "sws_flags", NULL, 0,
105                                                AV_OPT_SEARCH_FAKE_OBJ);
106             int ret = av_opt_eval_flags(&class, o, p + 6, &scale->flags);
107
108             if (ret < 0)
109                 return ret;
110         }
111     }
112
113     return 0;
114 }
115
116 static av_cold void uninit(AVFilterContext *ctx)
117 {
118     ScaleContext *scale = ctx->priv;
119     sws_freeContext(scale->sws);
120     scale->sws = NULL;
121 }
122
123 static int query_formats(AVFilterContext *ctx)
124 {
125     AVFilterFormats *formats;
126     enum PixelFormat pix_fmt;
127     int ret;
128
129     if (ctx->inputs[0]) {
130         formats = NULL;
131         for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
132             if (   sws_isSupportedInput(pix_fmt)
133                 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
134                 ff_formats_unref(&formats);
135                 return ret;
136             }
137         ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
138     }
139     if (ctx->outputs[0]) {
140         formats = NULL;
141         for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
142             if (    sws_isSupportedOutput(pix_fmt)
143                 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
144                 ff_formats_unref(&formats);
145                 return ret;
146             }
147         ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
148     }
149
150     return 0;
151 }
152
153 static int config_props(AVFilterLink *outlink)
154 {
155     AVFilterContext *ctx = outlink->src;
156     AVFilterLink *inlink = outlink->src->inputs[0];
157     ScaleContext *scale = ctx->priv;
158     int64_t w, h;
159     double var_values[VARS_NB], res;
160     char *expr;
161     int ret;
162
163     var_values[VAR_PI]    = M_PI;
164     var_values[VAR_PHI]   = M_PHI;
165     var_values[VAR_E]     = M_E;
166     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
167     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
168     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
169     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
170     var_values[VAR_DAR]   = var_values[VAR_A]  = (float) inlink->w / inlink->h;
171     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
172         (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
173     var_values[VAR_HSUB]  = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
174     var_values[VAR_VSUB]  = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
175
176     /* evaluate width and height */
177     av_expr_parse_and_eval(&res, (expr = scale->w_expr),
178                            var_names, var_values,
179                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
180     scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
181     if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
182                                       var_names, var_values,
183                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
184         goto fail;
185     scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
186     /* evaluate again the width, as it may depend on the output height */
187     if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
188                                       var_names, var_values,
189                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
190         goto fail;
191     scale->w = res;
192
193     w = scale->w;
194     h = scale->h;
195
196     /* sanity check params */
197     if (w <  -1 || h <  -1) {
198         av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
199         return AVERROR(EINVAL);
200     }
201     if (w == -1 && h == -1)
202         scale->w = scale->h = 0;
203
204     if (!(w = scale->w))
205         w = inlink->w;
206     if (!(h = scale->h))
207         h = inlink->h;
208     if (w == -1)
209         w = av_rescale(h, inlink->w, inlink->h);
210     if (h == -1)
211         h = av_rescale(w, inlink->h, inlink->w);
212
213     if (w > INT_MAX || h > INT_MAX ||
214         (h * inlink->w) > INT_MAX  ||
215         (w * inlink->h) > INT_MAX)
216         av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
217
218     outlink->w = w;
219     outlink->h = h;
220
221     /* TODO: make algorithm configurable */
222     av_log(ctx, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
223            inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
224            outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
225            scale->flags);
226
227     scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
228                           av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PSEUDOPAL;
229
230     if (scale->sws)
231         sws_freeContext(scale->sws);
232     if (inlink->w == outlink->w && inlink->h == outlink->h &&
233         inlink->format == outlink->format)
234         scale->sws = NULL;
235     else {
236         scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
237                                     outlink->w, outlink->h, outlink->format,
238                                     scale->flags, NULL, NULL, NULL);
239         if (!scale->sws)
240             return AVERROR(EINVAL);
241     }
242
243
244     if (inlink->sample_aspect_ratio.num)
245         outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
246                                                              outlink->w*inlink->h},
247                                                 inlink->sample_aspect_ratio);
248     else
249         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
250
251     return 0;
252
253 fail:
254     av_log(NULL, AV_LOG_ERROR,
255            "Error when evaluating the expression '%s'\n", expr);
256     return ret;
257 }
258
259 static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
260 {
261     ScaleContext *scale = link->dst->priv;
262     AVFilterLink *outlink = link->dst->outputs[0];
263     AVFilterBufferRef *outpicref, *for_next_filter;
264     int ret = 0;
265
266     if (!scale->sws) {
267         outpicref = avfilter_ref_buffer(picref, ~0);
268         if (!outpicref)
269             return AVERROR(ENOMEM);
270         return ff_start_frame(outlink, outpicref);
271     }
272
273     scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
274     scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
275
276     outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
277     if (!outpicref)
278         return AVERROR(ENOMEM);
279
280     avfilter_copy_buffer_ref_props(outpicref, picref);
281     outpicref->video->w = outlink->w;
282     outpicref->video->h = outlink->h;
283
284
285     av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
286               (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
287               (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
288               INT_MAX);
289
290     scale->slice_y = 0;
291     for_next_filter = avfilter_ref_buffer(outpicref, ~0);
292     if (for_next_filter)
293         ret = ff_start_frame(outlink, for_next_filter);
294     else
295         ret = AVERROR(ENOMEM);
296
297     if (ret < 0) {
298         avfilter_unref_bufferp(&outpicref);
299         return ret;
300     }
301
302     outlink->out_buf = outpicref;
303     return 0;
304 }
305
306 static int draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
307 {
308     ScaleContext *scale = link->dst->priv;
309     int out_h, ret;
310     AVFilterBufferRef *cur_pic = link->cur_buf;
311     const uint8_t *data[4];
312
313     if (!scale->sws) {
314         return ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
315     }
316
317     if (scale->slice_y == 0 && slice_dir == -1)
318         scale->slice_y = link->dst->outputs[0]->h;
319
320     data[0] = cur_pic->data[0] +  y               * cur_pic->linesize[0];
321     data[1] = scale->input_is_pal ?
322               cur_pic->data[1] :
323               cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
324     data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
325     data[3] = cur_pic->data[3] +  y               * cur_pic->linesize[3];
326
327     out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
328                       link->dst->outputs[0]->out_buf->data,
329                       link->dst->outputs[0]->out_buf->linesize);
330
331     if (slice_dir == -1)
332         scale->slice_y -= out_h;
333     ret = ff_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
334     if (slice_dir == 1)
335         scale->slice_y += out_h;
336     return ret;
337 }
338
339 AVFilter avfilter_vf_scale = {
340     .name      = "scale",
341     .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
342
343     .init      = init,
344     .uninit    = uninit,
345
346     .query_formats = query_formats,
347
348     .priv_size = sizeof(ScaleContext),
349
350     .inputs    = (const AVFilterPad[]) {{ .name             = "default",
351                                           .type             = AVMEDIA_TYPE_VIDEO,
352                                           .start_frame      = start_frame,
353                                           .draw_slice       = draw_slice,
354                                           .min_perms        = AV_PERM_READ, },
355                                         { .name = NULL}},
356     .outputs   = (const AVFilterPad[]) {{ .name             = "default",
357                                           .type             = AVMEDIA_TYPE_VIDEO,
358                                           .config_props     = config_props, },
359                                         { .name = NULL}},
360 };