OSDN Git Service

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