]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_scale.c
lavd/lavfi: do not set the channel layout list.
[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 "formats.h"
28 #include "internal.h"
29 #include "video.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/eval.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/avassert.h"
37 #include "libswscale/swscale.h"
38
39 static const char *const var_names[] = {
40     "in_w",   "iw",
41     "in_h",   "ih",
42     "out_w",  "ow",
43     "out_h",  "oh",
44     "a",
45     "sar",
46     "dar",
47     "hsub",
48     "vsub",
49     NULL
50 };
51
52 enum var_name {
53     VAR_IN_W,   VAR_IW,
54     VAR_IN_H,   VAR_IH,
55     VAR_OUT_W,  VAR_OW,
56     VAR_OUT_H,  VAR_OH,
57     VAR_A,
58     VAR_SAR,
59     VAR_DAR,
60     VAR_HSUB,
61     VAR_VSUB,
62     VARS_NB
63 };
64
65 typedef struct {
66     struct SwsContext *sws;     ///< software scaler context
67     struct SwsContext *isws[2]; ///< software scaler context for interlaced material
68
69     /**
70      * New dimensions. Special values are:
71      *   0 = original width/height
72      *  -1 = keep original aspect
73      */
74     int w, h;
75     unsigned int flags;         ///sws flags
76
77     int hsub, vsub;             ///< chroma subsampling
78     int slice_y;                ///< top of current output slice
79     int input_is_pal;           ///< set to 1 if the input format is paletted
80     int output_is_pal;          ///< set to 1 if the output format is paletted
81     int interlaced;
82
83     char w_expr[256];           ///< width  expression string
84     char h_expr[256];           ///< height expression string
85 } ScaleContext;
86
87 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
88 {
89     ScaleContext *scale = ctx->priv;
90     const char *p;
91
92     av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
93     av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
94
95     scale->flags = SWS_BILINEAR;
96     if (args) {
97         sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
98         p = strstr(args,"flags=");
99         if (p) {
100             const AVClass *class = sws_get_class();
101             const AVOption    *o = av_opt_find(&class, "sws_flags", NULL, 0,
102                                                AV_OPT_SEARCH_FAKE_OBJ);
103             int ret = av_opt_eval_flags(&class, o, p + 6, &scale->flags);
104
105             if (ret < 0)
106                 return ret;
107         }
108         if(strstr(args,"interl=1")){
109             scale->interlaced=1;
110         }else if(strstr(args,"interl=-1"))
111             scale->interlaced=-1;
112     }
113
114     return 0;
115 }
116
117 static av_cold void uninit(AVFilterContext *ctx)
118 {
119     ScaleContext *scale = ctx->priv;
120     sws_freeContext(scale->sws);
121     sws_freeContext(scale->isws[0]);
122     sws_freeContext(scale->isws[1]);
123     scale->sws = NULL;
124 }
125
126 static int query_formats(AVFilterContext *ctx)
127 {
128     AVFilterFormats *formats;
129     enum PixelFormat pix_fmt;
130     int ret;
131
132     if (ctx->inputs[0]) {
133         formats = NULL;
134         for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
135             if (   sws_isSupportedInput(pix_fmt)
136                 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
137                 ff_formats_unref(&formats);
138                 return ret;
139             }
140         ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
141     }
142     if (ctx->outputs[0]) {
143         formats = NULL;
144         for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
145             if (   (sws_isSupportedOutput(pix_fmt) || pix_fmt == PIX_FMT_PAL8)
146                 && (ret = ff_add_format(&formats, pix_fmt)) < 0) {
147                 ff_formats_unref(&formats);
148                 return ret;
149             }
150         ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
151     }
152
153     return 0;
154 }
155
156 static int config_props(AVFilterLink *outlink)
157 {
158     AVFilterContext *ctx = outlink->src;
159     AVFilterLink *inlink = outlink->src->inputs[0];
160     enum PixelFormat outfmt = outlink->format;
161     ScaleContext *scale = ctx->priv;
162     int64_t w, h;
163     double var_values[VARS_NB], res;
164     char *expr;
165     int ret;
166
167     var_values[VAR_IN_W]  = var_values[VAR_IW] = inlink->w;
168     var_values[VAR_IN_H]  = var_values[VAR_IH] = inlink->h;
169     var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
170     var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
171     var_values[VAR_A]     = (float) inlink->w / inlink->h;
172     var_values[VAR_SAR]   = inlink->sample_aspect_ratio.num ?
173         (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
174     var_values[VAR_DAR]   = var_values[VAR_A] * var_values[VAR_SAR];
175     var_values[VAR_HSUB]  = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
176     var_values[VAR_VSUB]  = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
177
178     /* evaluate width and height */
179     av_expr_parse_and_eval(&res, (expr = scale->w_expr),
180                            var_names, var_values,
181                            NULL, NULL, NULL, NULL, NULL, 0, ctx);
182     scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
183     if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
184                                       var_names, var_values,
185                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
186         goto fail;
187     scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
188     /* evaluate again the width, as it may depend on the output height */
189     if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
190                                       var_names, var_values,
191                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
192         goto fail;
193     scale->w = res;
194
195     w = scale->w;
196     h = scale->h;
197
198     /* sanity check params */
199     if (w <  -1 || h <  -1) {
200         av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
201         return AVERROR(EINVAL);
202     }
203     if (w == -1 && h == -1)
204         scale->w = scale->h = 0;
205
206     if (!(w = scale->w))
207         w = inlink->w;
208     if (!(h = scale->h))
209         h = inlink->h;
210     if (w == -1)
211         w = av_rescale(h, inlink->w, inlink->h);
212     if (h == -1)
213         h = av_rescale(w, inlink->h, inlink->w);
214
215     if (w > INT_MAX || h > INT_MAX ||
216         (h * inlink->w) > INT_MAX  ||
217         (w * inlink->h) > INT_MAX)
218         av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
219
220     outlink->w = w;
221     outlink->h = h;
222
223     /* TODO: make algorithm configurable */
224
225     scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL ||
226                           av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PSEUDOPAL;
227     if (outfmt == PIX_FMT_PAL8) outfmt = PIX_FMT_BGR8;
228     scale->output_is_pal = av_pix_fmt_descriptors[outfmt].flags & PIX_FMT_PAL ||
229                            av_pix_fmt_descriptors[outfmt].flags & PIX_FMT_PSEUDOPAL;
230
231     if (scale->sws)
232         sws_freeContext(scale->sws);
233     if (inlink->w == outlink->w && inlink->h == outlink->h &&
234         inlink->format == outlink->format)
235         scale->sws = NULL;
236     else {
237         scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
238                                     outlink->w, outlink->h, outfmt,
239                                     scale->flags, NULL, NULL, NULL);
240         if (scale->isws[0])
241             sws_freeContext(scale->isws[0]);
242         scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
243                                         outlink->w, outlink->h/2, outfmt,
244                                         scale->flags, NULL, NULL, NULL);
245         if (scale->isws[1])
246             sws_freeContext(scale->isws[1]);
247         scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
248                                         outlink->w, outlink->h/2, outfmt,
249                                         scale->flags, NULL, NULL, NULL);
250         if (!scale->sws || !scale->isws[0] || !scale->isws[1])
251             return AVERROR(EINVAL);
252     }
253
254     if (inlink->sample_aspect_ratio.num){
255         outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
256     } else
257         outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
258
259     av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s sar:%d/%d -> w:%d h:%d fmt:%s sar:%d/%d flags:0x%0x\n",
260            inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
261            inlink->sample_aspect_ratio.num, inlink->sample_aspect_ratio.den,
262            outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
263            outlink->sample_aspect_ratio.num, outlink->sample_aspect_ratio.den,
264            scale->flags);
265     return 0;
266
267 fail:
268     av_log(NULL, AV_LOG_ERROR,
269            "Error when evaluating the expression '%s'.\n"
270            "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
271            expr, scale->w_expr, scale->h_expr);
272     return ret;
273 }
274
275 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
276 {
277     ScaleContext *scale = link->dst->priv;
278     AVFilterLink *outlink = link->dst->outputs[0];
279     AVFilterBufferRef *outpicref;
280
281     if(   picref->video->w != link->w
282        || picref->video->h != link->h
283        || picref->format   != link->format) {
284         int ret;
285         snprintf(scale->w_expr, sizeof(scale->w_expr)-1, "%d", outlink->w);
286         snprintf(scale->h_expr, sizeof(scale->h_expr)-1, "%d", outlink->h);
287
288         link->dst->inputs[0]->format = picref->format;
289         link->dst->inputs[0]->w      = picref->video->w;
290         link->dst->inputs[0]->h      = picref->video->h;
291
292         if ((ret = config_props(outlink)) < 0)
293             av_assert0(0); //what to do here ?
294     }
295
296
297     if (!scale->sws) {
298         ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
299         return;
300     }
301
302     scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
303     scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
304
305     outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
306     avfilter_copy_buffer_ref_props(outpicref, picref);
307     outpicref->video->w = outlink->w;
308     outpicref->video->h = outlink->h;
309
310     outlink->out_buf = outpicref;
311     if(scale->output_is_pal)
312         ff_set_systematic_pal2(outpicref->data[1], outlink->format == PIX_FMT_PAL8 ? PIX_FMT_BGR8 : outlink->format);
313
314     av_reduce(&outpicref->video->sample_aspect_ratio.num, &outpicref->video->sample_aspect_ratio.den,
315               (int64_t)picref->video->sample_aspect_ratio.num * outlink->h * link->w,
316               (int64_t)picref->video->sample_aspect_ratio.den * outlink->w * link->h,
317               INT_MAX);
318
319     scale->slice_y = 0;
320     ff_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
321 }
322
323 static int scale_slice(AVFilterLink *link, struct SwsContext *sws, int y, int h, int mul, int field)
324 {
325     ScaleContext *scale = link->dst->priv;
326     AVFilterBufferRef *cur_pic = link->cur_buf;
327     AVFilterBufferRef *out_buf = link->dst->outputs[0]->out_buf;
328     const uint8_t *in[4];
329     uint8_t *out[4];
330     int in_stride[4],out_stride[4];
331     int i;
332
333     for(i=0; i<4; i++){
334         int vsub= ((i+1)&2) ? scale->vsub : 0;
335          in_stride[i] = cur_pic->linesize[i] * mul;
336         out_stride[i] = out_buf->linesize[i] * mul;
337          in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
338         out[i] = out_buf->data[i] +            field  * out_buf->linesize[i];
339     }
340     if(scale->input_is_pal)
341          in[1] = cur_pic->data[1];
342     if(scale->output_is_pal)
343         out[1] = out_buf->data[1];
344
345     return sws_scale(sws, in, in_stride, y/mul, h,
346                          out,out_stride);
347 }
348
349 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
350 {
351     ScaleContext *scale = link->dst->priv;
352     int out_h;
353
354     if (!scale->sws) {
355         ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
356         return;
357     }
358
359     if (scale->slice_y == 0 && slice_dir == -1)
360         scale->slice_y = link->dst->outputs[0]->h;
361
362     if(scale->interlaced>0 || (scale->interlaced<0 && link->cur_buf->video->interlaced)){
363         av_assert0(y%(2<<scale->vsub) == 0);
364         out_h = scale_slice(link, scale->isws[0], y, (h+1)/2, 2, 0);
365         out_h+= scale_slice(link, scale->isws[1], y,  h   /2, 2, 1);
366     }else{
367         out_h = scale_slice(link, scale->sws, y, h, 1, 0);
368     }
369
370     if (slice_dir == -1)
371         scale->slice_y -= out_h;
372     ff_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
373     if (slice_dir == 1)
374         scale->slice_y += out_h;
375 }
376
377 AVFilter avfilter_vf_scale = {
378     .name      = "scale",
379     .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
380
381     .init      = init,
382     .uninit    = uninit,
383
384     .query_formats = query_formats,
385
386     .priv_size = sizeof(ScaleContext),
387
388     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
389                                     .type             = AVMEDIA_TYPE_VIDEO,
390                                     .start_frame      = start_frame,
391                                     .draw_slice       = draw_slice,
392                                     .min_perms        = AV_PERM_READ, },
393                                   { .name = NULL}},
394     .outputs   = (const AVFilterPad[]) {{ .name       = "default",
395                                     .type             = AVMEDIA_TYPE_VIDEO,
396                                     .config_props     = config_props, },
397                                   { .name = NULL}},
398 };