]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_scale.c
vf_scale: avoid a pointless memcpy in no-op conversion.
[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 "libavutil/avstring.h"
28 #include "libavutil/eval.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/pixdesc.h"
31 #include "libswscale/swscale.h"
32
33 static const char *const var_names[] = {
34     "PI",
35     "PHI",
36     "E",
37     "in_w",   "iw",
38     "in_h",   "ih",
39     "out_w",  "ow",
40     "out_h",  "oh",
41     "a", "dar",
42     "sar",
43     "hsub",
44     "vsub",
45     NULL
46 };
47
48 enum var_name {
49     VAR_PI,
50     VAR_PHI,
51     VAR_E,
52     VAR_IN_W,   VAR_IW,
53     VAR_IN_H,   VAR_IH,
54     VAR_OUT_W,  VAR_OW,
55     VAR_OUT_H,  VAR_OH,
56     VAR_A, VAR_DAR,
57     VAR_SAR,
58     VAR_HSUB,
59     VAR_VSUB,
60     VARS_NB
61 };
62
63 typedef struct {
64     struct SwsContext *sws;     ///< software scaler context
65
66     /**
67      * New dimensions. Special values are:
68      *   0 = original width/height
69      *  -1 = keep original aspect
70      */
71     int w, h;
72     unsigned int flags;         ///sws flags
73
74     int hsub, vsub;             ///< chroma subsampling
75     int slice_y;                ///< top of current output slice
76     int input_is_pal;           ///< set to 1 if the input format is paletted
77
78     char w_expr[256];           ///< width  expression string
79     char h_expr[256];           ///< height expression string
80 } ScaleContext;
81
82 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
83 {
84     ScaleContext *scale = ctx->priv;
85     const char *p;
86
87     av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
88     av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
89
90     scale->flags = SWS_BILINEAR;
91     if (args) {
92         sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
93         p = strstr(args,"flags=");
94         if (p) scale->flags = strtoul(p+6, NULL, 0);
95     }
96
97     return 0;
98 }
99
100 static av_cold void uninit(AVFilterContext *ctx)
101 {
102     ScaleContext *scale = ctx->priv;
103     sws_freeContext(scale->sws);
104     scale->sws = NULL;
105 }
106
107 static int query_formats(AVFilterContext *ctx)
108 {
109     AVFilterFormats *formats;
110     enum PixelFormat pix_fmt;
111     int ret;
112
113     if (ctx->inputs[0]) {
114         formats = NULL;
115         for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
116             if (   sws_isSupportedInput(pix_fmt)
117                 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
118                 avfilter_formats_unref(&formats);
119                 return ret;
120             }
121         avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
122     }
123     if (ctx->outputs[0]) {
124         formats = NULL;
125         for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
126             if (    sws_isSupportedOutput(pix_fmt)
127                 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
128                 avfilter_formats_unref(&formats);
129                 return ret;
130             }
131         avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
132     }
133
134     return 0;
135 }
136
137 static int config_props(AVFilterLink *outlink)
138 {
139     AVFilterContext *ctx = outlink->src;
140     AVFilterLink *inlink = outlink->src->inputs[0];
141     ScaleContext *scale = ctx->priv;
142     int64_t w, h;
143     double var_values[VARS_NB], res;
144     char *expr;
145     int ret;
146
147     var_values[VAR_PI]    = M_PI;
148     var_values[VAR_PHI]   = M_PHI;
149     var_values[VAR_E]     = M_E;
150     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
151     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
152     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
153     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
154     var_values[VAR_DAR]   = var_values[VAR_A]  = (float) inlink->w / inlink->h;
155     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
156         (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
157     var_values[VAR_HSUB]  = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
158     var_values[VAR_VSUB]  = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
159
160     /* evaluate width and height */
161     av_expr_parse_and_eval(&res, (expr = scale->w_expr),
162                            var_names, var_values,
163                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
164     scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
165     if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
166                                       var_names, var_values,
167                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
168         goto fail;
169     scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
170     /* evaluate again the width, as it may depend on the output height */
171     if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
172                                       var_names, var_values,
173                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
174         goto fail;
175     scale->w = res;
176
177     w = scale->w;
178     h = scale->h;
179
180     /* sanity check params */
181     if (w <  -1 || h <  -1) {
182         av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
183         return AVERROR(EINVAL);
184     }
185     if (w == -1 && h == -1)
186         scale->w = scale->h = 0;
187
188     if (!(w = scale->w))
189         w = inlink->w;
190     if (!(h = scale->h))
191         h = inlink->h;
192     if (w == -1)
193         w = av_rescale(h, inlink->w, inlink->h);
194     if (h == -1)
195         h = av_rescale(w, inlink->h, inlink->w);
196
197     if (w > INT_MAX || h > INT_MAX ||
198         (h * inlink->w) > INT_MAX  ||
199         (w * inlink->h) > INT_MAX)
200         av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
201
202     outlink->w = w;
203     outlink->h = h;
204
205     /* TODO: make algorithm configurable */
206     av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
207            inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
208            outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
209            scale->flags);
210
211     scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
212                           av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PSEUDOPAL;
213
214     if (scale->sws)
215         sws_freeContext(scale->sws);
216     if (inlink->w == outlink->w && inlink->h == outlink->h &&
217         inlink->format == outlink->format)
218         scale->sws = NULL;
219     else {
220         scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
221                                     outlink->w, outlink->h, outlink->format,
222                                     scale->flags, NULL, NULL, NULL);
223         if (!scale->sws)
224             return AVERROR(EINVAL);
225     }
226
227
228     if (inlink->sample_aspect_ratio.num)
229         outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
230                                                              outlink->w*inlink->h},
231                                                 inlink->sample_aspect_ratio);
232     else
233         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
234
235     return 0;
236
237 fail:
238     av_log(NULL, AV_LOG_ERROR,
239            "Error when evaluating the expression '%s'\n", expr);
240     return ret;
241 }
242
243 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
244 {
245     ScaleContext *scale = link->dst->priv;
246     AVFilterLink *outlink = link->dst->outputs[0];
247     AVFilterBufferRef *outpicref;
248
249     if (!scale->sws) {
250         avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
251         return;
252     }
253
254     scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
255     scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
256
257     outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
258     avfilter_copy_buffer_ref_props(outpicref, picref);
259     outpicref->video->w = outlink->w;
260     outpicref->video->h = outlink->h;
261
262     outlink->out_buf = outpicref;
263
264     av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
265               (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
266               (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
267               INT_MAX);
268
269     scale->slice_y = 0;
270     avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
271 }
272
273 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
274 {
275     ScaleContext *scale = link->dst->priv;
276     int out_h;
277     AVFilterBufferRef *cur_pic = link->cur_buf;
278     const uint8_t *data[4];
279
280     if (!scale->sws) {
281         avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
282         return;
283     }
284
285     if (scale->slice_y == 0 && slice_dir == -1)
286         scale->slice_y = link->dst->outputs[0]->h;
287
288     data[0] = cur_pic->data[0] +  y               * cur_pic->linesize[0];
289     data[1] = scale->input_is_pal ?
290               cur_pic->data[1] :
291               cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
292     data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
293     data[3] = cur_pic->data[3] +  y               * cur_pic->linesize[3];
294
295     out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
296                       link->dst->outputs[0]->out_buf->data,
297                       link->dst->outputs[0]->out_buf->linesize);
298
299     if (slice_dir == -1)
300         scale->slice_y -= out_h;
301     avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
302     if (slice_dir == 1)
303         scale->slice_y += out_h;
304 }
305
306 AVFilter avfilter_vf_scale = {
307     .name      = "scale",
308     .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
309
310     .init      = init,
311     .uninit    = uninit,
312
313     .query_formats = query_formats,
314
315     .priv_size = sizeof(ScaleContext),
316
317     .inputs    = (AVFilterPad[]) {{ .name             = "default",
318                                     .type             = AVMEDIA_TYPE_VIDEO,
319                                     .start_frame      = start_frame,
320                                     .draw_slice       = draw_slice,
321                                     .min_perms        = AV_PERM_READ, },
322                                   { .name = NULL}},
323     .outputs   = (AVFilterPad[]) {{ .name             = "default",
324                                     .type             = AVMEDIA_TYPE_VIDEO,
325                                     .config_props     = config_props, },
326                                   { .name = NULL}},
327 };