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