Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vf_scale.c @ 35fe66ab

History | View | Annotate | Download (11.1 KB)

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
    scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
223
                                    outlink->w, outlink->h/2, outlink->format,
224
                                    scale->flags, NULL, NULL, NULL);
225
    scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
226
                                    outlink->w, outlink->h/2, outlink->format,
227
                                    scale->flags, NULL, NULL, NULL);
228
    if (!scale->sws)
229
        return AVERROR(EINVAL);
230

    
231
    return 0;
232

    
233
fail:
234
    av_log(NULL, AV_LOG_ERROR,
235
           "Error when evaluating the expression '%s'\n", expr);
236
    return ret;
237
}
238

    
239
static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
240
{
241
    ScaleContext *scale = link->dst->priv;
242
    AVFilterLink *outlink = link->dst->outputs[0];
243
    AVFilterBufferRef *outpicref;
244

    
245
    scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
246
    scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
247

    
248
    outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
249
    avfilter_copy_buffer_ref_props(outpicref, picref);
250
    outpicref->video->w = outlink->w;
251
    outpicref->video->h = outlink->h;
252

    
253
    outlink->out_buf = outpicref;
254

    
255
    av_reduce(&outpicref->video->sample_aspect_ratio.num, &outpicref->video->sample_aspect_ratio.den,
256
              (int64_t)picref->video->sample_aspect_ratio.num * outlink->h * link->w,
257
              (int64_t)picref->video->sample_aspect_ratio.den * outlink->w * link->h,
258
              INT_MAX);
259

    
260
    scale->slice_y = 0;
261
    avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
262
}
263

    
264
static int scale_slice(AVFilterLink *link, struct SwsContext *sws, int y, int h, int mul, int field)
265
{
266
    ScaleContext *scale = link->dst->priv;
267
    AVFilterBufferRef *cur_pic = link->cur_buf;
268
    AVFilterBufferRef *out_buf = link->dst->outputs[0]->out_buf;
269
    const uint8_t *in[4], *out[4];
270
    int in_stride[4],out_stride[4];
271
    int i;
272

    
273
    for(i=0; i<4; i++){
274
        int vsub= ((i+1)&2) ? scale->vsub : 0;
275
         in_stride[i] = cur_pic->linesize[i] * mul;
276
        out_stride[i] = out_buf->linesize[i] * mul;
277
         in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
278
        out[i] = out_buf->data[i] +            field  * out_buf->linesize[i];
279
    }
280
    if(scale->input_is_pal){
281
         in[1] = cur_pic->data[1];
282
        out[1] = out_buf->data[1];
283
    }
284

    
285
    return sws_scale(sws, in, in_stride, y/mul, h,
286
                         out,out_stride);
287
}
288

    
289
static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
290
{
291
    ScaleContext *scale = link->dst->priv;
292
    int out_h;
293

    
294
    if (scale->slice_y == 0 && slice_dir == -1)
295
        scale->slice_y = link->dst->outputs[0]->h;
296

    
297
    if(scale->interlaced>0 || (scale->interlaced<0 && link->cur_buf->video->interlaced)){
298
        av_assert0(y%4 == 0);
299
        out_h = scale_slice(link, scale->isws[0], y, (h+1)/2, 2, 0);
300
        out_h+= scale_slice(link, scale->isws[1], y,  h   /2, 2, 1);
301
    }else{
302
        out_h = scale_slice(link, scale->sws, y, h, 1, 0);
303
    }
304

    
305
    if (slice_dir == -1)
306
        scale->slice_y -= out_h;
307
    avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
308
    if (slice_dir == 1)
309
        scale->slice_y += out_h;
310
}
311

    
312
AVFilter avfilter_vf_scale = {
313
    .name      = "scale",
314
    .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
315

    
316
    .init      = init,
317
    .uninit    = uninit,
318

    
319
    .query_formats = query_formats,
320

    
321
    .priv_size = sizeof(ScaleContext),
322

    
323
    .inputs    = (AVFilterPad[]) {{ .name             = "default",
324
                                    .type             = AVMEDIA_TYPE_VIDEO,
325
                                    .start_frame      = start_frame,
326
                                    .draw_slice       = draw_slice,
327
                                    .min_perms        = AV_PERM_READ, },
328
                                  { .name = NULL}},
329
    .outputs   = (AVFilterPad[]) {{ .name             = "default",
330
                                    .type             = AVMEDIA_TYPE_VIDEO,
331
                                    .config_props     = config_props, },
332
                                  { .name = NULL}},
333
};