Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vf_scale.c @ bbd1c43f

History | View | Annotate | Download (8.73 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/pixdesc.h"
28
#include "libavutil/avassert.h"
29
#include "libswscale/swscale.h"
30

    
31
typedef struct {
32
    struct SwsContext *sws;     ///< software scaler context
33
    struct SwsContext *isws[2]; ///< software scaler context for interlaced material
34

    
35
    /**
36
     * New dimensions. Special values are:
37
     *   0 = original width/height
38
     *  -1 = keep original aspect
39
     */
40
    int w, h;
41
    unsigned int flags;         ///sws flags
42

    
43
    int hsub, vsub;             ///< chroma subsampling
44
    int slice_y;                ///< top of current output slice
45
    int input_is_pal;           ///< set to 1 if the input format is paletted
46
    int interlaced;
47
} ScaleContext;
48

    
49
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
50
{
51
    ScaleContext *scale = ctx->priv;
52
    const char *p;
53

    
54
    scale->flags = SWS_BILINEAR;
55
    if (args) {
56
        sscanf(args, "%d:%d", &scale->w, &scale->h);
57
        p = strstr(args,"flags=");
58
        if (p) scale->flags = strtoul(p+6, NULL, 0);
59
        if(strstr(args,"interl=1")){
60
            scale->interlaced=1;
61
        }else if(strstr(args,"interl=-1"))
62
            scale->interlaced=-1;
63
    }
64

    
65
    /* sanity check params */
66
    if (scale->w <  -1 || scale->h <  -1) {
67
        av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
68
        return AVERROR(EINVAL);
69
    }
70
    if (scale->w == -1 && scale->h == -1)
71
        scale->w = scale->h = 0;
72

    
73
    return 0;
74
}
75

    
76
static av_cold void uninit(AVFilterContext *ctx)
77
{
78
    ScaleContext *scale = ctx->priv;
79
    sws_freeContext(scale->sws);
80
    sws_freeContext(scale->isws[0]);
81
    sws_freeContext(scale->isws[1]);
82
    scale->sws = NULL;
83
}
84

    
85
static int query_formats(AVFilterContext *ctx)
86
{
87
    AVFilterFormats *formats;
88
    enum PixelFormat pix_fmt;
89
    int ret;
90

    
91
    if (ctx->inputs[0]) {
92
        formats = NULL;
93
        for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
94
            if (   sws_isSupportedInput(pix_fmt)
95
                && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
96
                avfilter_formats_unref(&formats);
97
                return ret;
98
            }
99
        avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
100
    }
101
    if (ctx->outputs[0]) {
102
        formats = NULL;
103
        for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
104
            if (    sws_isSupportedOutput(pix_fmt)
105
                && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
106
                avfilter_formats_unref(&formats);
107
                return ret;
108
            }
109
        avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
110
    }
111

    
112
    return 0;
113
}
114

    
115
static int config_props(AVFilterLink *outlink)
116
{
117
    AVFilterContext *ctx = outlink->src;
118
    AVFilterLink *inlink = outlink->src->inputs[0];
119
    ScaleContext *scale = ctx->priv;
120
    int64_t w, h;
121

    
122
    if (!(w = scale->w))
123
        w = inlink->w;
124
    if (!(h = scale->h))
125
        h = inlink->h;
126
    if (w == -1)
127
        w = av_rescale(h, inlink->w, inlink->h);
128
    if (h == -1)
129
        h = av_rescale(w, inlink->h, inlink->w);
130

    
131
    if (w > INT_MAX || h > INT_MAX ||
132
        (h * inlink->w) > INT_MAX  ||
133
        (w * inlink->h) > INT_MAX)
134
        av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
135

    
136
    outlink->w = w;
137
    outlink->h = h;
138

    
139
    /* TODO: make algorithm configurable */
140
    av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
141
           inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
142
           outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
143
           scale->flags);
144

    
145
    scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL;
146

    
147
    if(scale->sws)
148
        sws_freeContext(scale->sws);
149
    scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
150
                                outlink->w, outlink->h, outlink->format,
151
                                scale->flags, NULL, NULL, NULL);
152
    scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
153
                                    outlink->w, outlink->h/2, outlink->format,
154
                                    scale->flags, NULL, NULL, NULL);
155
    scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
156
                                    outlink->w, outlink->h/2, outlink->format,
157
                                    scale->flags, NULL, NULL, NULL);
158
    if (!scale->sws)
159
        return AVERROR(EINVAL);
160

    
161
    return 0;
162
}
163

    
164
static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
165
{
166
    ScaleContext *scale = link->dst->priv;
167
    AVFilterLink *outlink = link->dst->outputs[0];
168
    AVFilterBufferRef *outpicref;
169

    
170
    scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
171
    scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
172

    
173
    outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
174
    avfilter_copy_buffer_ref_props(outpicref, picref);
175
    outpicref->video->w = outlink->w;
176
    outpicref->video->h = outlink->h;
177

    
178
    outlink->out_buf = outpicref;
179

    
180
    av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
181
              (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
182
              (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
183
              INT_MAX);
184

    
185
    scale->slice_y = 0;
186
    avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
187
}
188

    
189
static int scale_slice(AVFilterLink *link, struct SwsContext *sws, int y, int h, int mul, int field)
190
{
191
    ScaleContext *scale = link->dst->priv;
192
    AVFilterBufferRef *cur_pic = link->cur_buf;
193
    AVFilterBufferRef *out_buf = link->dst->outputs[0]->out_buf;
194
    const uint8_t *in[4], *out[4];
195
    int in_stride[4],out_stride[4];
196
    int i;
197

    
198
    for(i=0; i<4; i++){
199
        int vsub= ((i+1)&2) ? scale->vsub : 0;
200
         in_stride[i] = cur_pic->linesize[i] * mul;
201
        out_stride[i] = out_buf->linesize[i] * mul;
202
         in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
203
        out[i] = out_buf->data[i] +            field  * out_buf->linesize[i];
204
    }
205
    if(scale->input_is_pal){
206
         in[1] = cur_pic->data[1];
207
        out[1] = out_buf->data[1];
208
    }
209

    
210
    return sws_scale(sws, in, in_stride, y/mul, h,
211
                         out,out_stride);
212
}
213

    
214
static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
215
{
216
    ScaleContext *scale = link->dst->priv;
217
    int out_h;
218

    
219
    if (scale->slice_y == 0 && slice_dir == -1)
220
        scale->slice_y = link->dst->outputs[0]->h;
221

    
222
    if(scale->interlaced>0 || (scale->interlaced<0 && link->cur_buf->video->interlaced)){
223
        av_assert0(y%4 == 0);
224
        out_h = scale_slice(link, scale->isws[0], y, (h+1)/2, 2, 0);
225
        out_h+= scale_slice(link, scale->isws[1], y,  h   /2, 2, 1);
226
    }else{
227
        out_h = scale_slice(link, scale->sws, y, h, 1, 0);
228
    }
229

    
230
    if (slice_dir == -1)
231
        scale->slice_y -= out_h;
232
    avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
233
    if (slice_dir == 1)
234
        scale->slice_y += out_h;
235
}
236

    
237
AVFilter avfilter_vf_scale = {
238
    .name      = "scale",
239
    .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
240

    
241
    .init      = init,
242
    .uninit    = uninit,
243

    
244
    .query_formats = query_formats,
245

    
246
    .priv_size = sizeof(ScaleContext),
247

    
248
    .inputs    = (AVFilterPad[]) {{ .name             = "default",
249
                                    .type             = AVMEDIA_TYPE_VIDEO,
250
                                    .start_frame      = start_frame,
251
                                    .draw_slice       = draw_slice,
252
                                    .min_perms        = AV_PERM_READ, },
253
                                  { .name = NULL}},
254
    .outputs   = (AVFilterPad[]) {{ .name             = "default",
255
                                    .type             = AVMEDIA_TYPE_VIDEO,
256
                                    .config_props     = config_props, },
257
                                  { .name = NULL}},
258
};