OSDN Git Service

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