Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vsrc_buffer.c @ 18ded93a

History | View | Annotate | Download (8 KB)

1
/*
2
 * Copyright (c) 2008 Vitor Sessak
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
 * memory buffer source filter
24
 */
25

    
26
#include "avfilter.h"
27
#include "vsrc_buffer.h"
28
#include "libavutil/imgutils.h"
29

    
30
typedef struct {
31
    int64_t           pts;
32
    AVFrame           frame;
33
    int               has_frame;
34
    int               h, w;
35
    enum PixelFormat  pix_fmt;
36
    AVRational        time_base;     ///< time_base to set in the output link
37
    AVRational        sample_aspect_ratio;
38
    char              sws_param[256];
39
} BufferSourceContext;
40

    
41
int av_vsrc_buffer_add_frame2(AVFilterContext *buffer_filter, AVFrame *frame,
42
                              int64_t pts, int width,
43
                              int height, enum PixelFormat  pix_fmt,
44
                              const char *sws_param)
45
{
46
    BufferSourceContext *c = buffer_filter->priv;
47
    int ret;
48

    
49
    if (c->has_frame) {
50
        av_log(buffer_filter, AV_LOG_ERROR,
51
               "Buffering several frames is not supported. "
52
               "Please consume all available frames before adding a new one.\n"
53
            );
54
        //return -1;
55
    }
56

    
57
    if(!c->sws_param[0]){
58
        snprintf(c->sws_param, 255, "%d:%d:%s", c->w, c->h, sws_param);
59
    }
60

    
61
    if(width != c->w || height != c->h || pix_fmt != c->pix_fmt){
62
        AVFilterContext *scale= buffer_filter->outputs[0]->dst;
63
        AVFilterLink *link;
64

    
65
        av_log(buffer_filter, AV_LOG_INFO,
66
               "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
67
               c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
68
               width, height, av_pix_fmt_descriptors[pix_fmt].name);
69

    
70
        if(!scale || strcmp(scale->filter->name,"scale")){
71
            AVFilter *f= avfilter_get_by_name("scale");
72

    
73
            av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
74
            if(avfilter_open(&scale, f, "Input equalizer") < 0)
75
                return -1;
76

    
77
            if((ret=avfilter_init_filter(scale, c->sws_param, NULL))<0){
78
                avfilter_free(scale);
79
                return ret;
80
            }
81

    
82
            if((ret=avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0))<0){
83
                avfilter_free(scale);
84
                return ret;
85
            }
86
            scale->outputs[0]->time_base = scale->inputs[0]->time_base;
87

    
88
            scale->outputs[0]->format= c->pix_fmt;
89
        } else if(!strcmp(scale->filter->name, "scale")) {
90
            snprintf(c->sws_param, 255, "%d:%d:%s", scale->outputs[0]->w, scale->outputs[0]->h, sws_param);
91
            scale->filter->init(scale, c->sws_param, NULL);
92
        }
93

    
94
        c->pix_fmt= scale->inputs[0]->format= pix_fmt;
95
        c->w= scale->inputs[0]->w= width;
96
        c->h= scale->inputs[0]->h= height;
97

    
98
        link= scale->outputs[0];
99
        if ((ret =  link->srcpad->config_props(link)) < 0)
100
            return ret;
101
    }
102

    
103
    memcpy(c->frame.data    , frame->data    , sizeof(frame->data));
104
    memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
105
    c->frame.width  = frame->width;
106
    c->frame.height = frame->height;
107
    c->frame.format = frame->format;
108
    c->frame.interlaced_frame= frame->interlaced_frame;
109
    c->frame.top_field_first = frame->top_field_first;
110
    c->frame.key_frame = frame->key_frame;
111
    c->frame.pict_type = frame->pict_type;
112
    c->frame.sample_aspect_ratio = frame->sample_aspect_ratio;
113
    c->pts = pts;
114
    c->has_frame = 1;
115

    
116
    return 0;
117
}
118

    
119
int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
120
                             int64_t pts)
121
{
122
    BufferSourceContext *c = buffer_filter->priv;
123

    
124
    return av_vsrc_buffer_add_frame2(buffer_filter, frame,
125
                              pts, c->w,
126
                              c->h, c->pix_fmt, "");
127
}
128

    
129
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
130
{
131
    BufferSourceContext *c = ctx->priv;
132
    char pix_fmt_str[128];
133
    int n = 0;
134

    
135
    if (!args ||
136
        (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
137
                    &c->time_base.num, &c->time_base.den,
138
                    &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den)) != 7) {
139
        av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but only %d found in '%s'\n", n, args);
140
        return AVERROR(EINVAL);
141
    }
142
    if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
143
        char *tail;
144
        c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
145
        if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
146
            av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
147
            return AVERROR(EINVAL);
148
        }
149
    }
150

    
151
    av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s\n", c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name);
152
    return 0;
153
}
154

    
155
static int query_formats(AVFilterContext *ctx)
156
{
157
    BufferSourceContext *c = ctx->priv;
158
    enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
159

    
160
    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
161
    return 0;
162
}
163

    
164
static int config_props(AVFilterLink *link)
165
{
166
    BufferSourceContext *c = link->src->priv;
167

    
168
    link->w = c->w;
169
    link->h = c->h;
170
    link->sample_aspect_ratio = c->sample_aspect_ratio;
171
    link->time_base = c->time_base;
172

    
173
    return 0;
174
}
175

    
176
static int request_frame(AVFilterLink *link)
177
{
178
    BufferSourceContext *c = link->src->priv;
179
    AVFilterBufferRef *picref;
180

    
181
    if (!c->has_frame) {
182
        av_log(link->src, AV_LOG_ERROR,
183
               "request_frame() called with no available frame!\n");
184
        //return -1;
185
    }
186

    
187
    /* This picture will be needed unmodified later for decoding the next
188
     * frame */
189
    picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
190
                                       AV_PERM_REUSE2,
191
                                       link->w, link->h);
192

    
193
    av_image_copy(picref->data, picref->linesize,
194
                  c->frame.data, c->frame.linesize,
195
                  picref->format, link->w, link->h);
196

    
197
    picref->pts                    = c->pts;
198
    picref->video->sample_aspect_ratio = c->frame.sample_aspect_ratio;
199
    picref->video->interlaced      = c->frame.interlaced_frame;
200
    picref->video->top_field_first = c->frame.top_field_first;
201
    picref->video->key_frame       = c->frame.key_frame;
202
    picref->video->pict_type       = c->frame.pict_type;
203
    avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
204
    avfilter_draw_slice(link, 0, link->h, 1);
205
    avfilter_end_frame(link);
206
    avfilter_unref_buffer(picref);
207

    
208
    c->has_frame = 0;
209

    
210
    return 0;
211
}
212

    
213
static int poll_frame(AVFilterLink *link)
214
{
215
    BufferSourceContext *c = link->src->priv;
216
    return !!(c->has_frame);
217
}
218

    
219
AVFilter avfilter_vsrc_buffer = {
220
    .name      = "buffer",
221
    .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
222
    .priv_size = sizeof(BufferSourceContext),
223
    .query_formats = query_formats,
224

    
225
    .init      = init,
226

    
227
    .inputs    = (AVFilterPad[]) {{ .name = NULL }},
228
    .outputs   = (AVFilterPad[]) {{ .name            = "default",
229
                                    .type            = AVMEDIA_TYPE_VIDEO,
230
                                    .request_frame   = request_frame,
231
                                    .poll_frame      = poll_frame,
232
                                    .config_props    = config_props, },
233
                                  { .name = NULL}},
234
};