Statistics
| Branch: | Revision:

ffmpeg / libavfilter / vsrc_buffer.c @ a38cdfde

History | View | Annotate | Download (7.53 KB)

1 cf13f204 Michael Niedermayer
/*
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 b5634e45 Stefano Sabatini
/**
22
 * @file
23
 * memory buffer source filter
24
 */
25
26 cf13f204 Michael Niedermayer
#include "avfilter.h"
27
#include "vsrc_buffer.h"
28 7ffe76e5 Reinhard Tartler
#include "libavutil/imgutils.h"
29 cf13f204 Michael Niedermayer
30
typedef struct {
31
    int64_t           pts;
32
    AVFrame           frame;
33
    int               has_frame;
34 9164afcb Carl Eugen Hoyos
    int               h, w;
35
    enum PixelFormat  pix_fmt;
36 94498ec9 Stefano Sabatini
    AVRational        time_base;     ///< time_base to set in the output link
37 cf13f204 Michael Niedermayer
    AVRational        pixel_aspect;
38 a38cdfde Michael Niedermayer
    char              sws_param[256];
39 cf13f204 Michael Niedermayer
} BufferSourceContext;
40
41 7d948dc4 Michael Niedermayer
int av_vsrc_buffer_add_frame2(AVFilterContext *buffer_filter, AVFrame *frame,
42
                              int64_t pts, AVRational pixel_aspect, int width,
43
                              int height, enum PixelFormat  pix_fmt,
44
                              const char *sws_param)
45 cf13f204 Michael Niedermayer
{
46
    BufferSourceContext *c = buffer_filter->priv;
47 7d948dc4 Michael Niedermayer
    int ret;
48 cf13f204 Michael Niedermayer
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 a38cdfde Michael Niedermayer
    if(!c->sws_param[0]){
58
        snprintf(c->sws_param, 255, "%d:%d:%s", c->w, c->h, sws_param);
59
    }
60
61 7d948dc4 Michael Niedermayer
    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, "Changing filter graph input to accept %dx%d %d (%d %d)\n",
66 ad0193bf Michael Niedermayer
               width,height,pix_fmt, c->pix_fmt, scale && scale->outputs ? scale->outputs[0]->format : -123);
67 7d948dc4 Michael Niedermayer
68
        if(!scale || strcmp(scale->filter->name,"scale")){
69
            AVFilter *f= avfilter_get_by_name("scale");
70
71
            av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
72
            if(avfilter_open(&scale, f, "Input equalizer") < 0)
73
                return -1;
74
75 a38cdfde Michael Niedermayer
            if((ret=avfilter_init_filter(scale, c->sws_param, NULL))<0){
76 7d948dc4 Michael Niedermayer
                avfilter_free(scale);
77
                return ret;
78
            }
79
80
            if((ret=avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0))<0){
81
                avfilter_free(scale);
82
                return ret;
83
            }
84
85
            scale->outputs[0]->format= c->pix_fmt;
86
        }
87
88
        c->pix_fmt= scale->inputs[0]->format= pix_fmt;
89
        c->w= scale->inputs[0]->w= width;
90
        c->h= scale->inputs[0]->h= height;
91
92
        link= scale->outputs[0];
93
        if ((ret =  link->srcpad->config_props(link)) < 0)
94
            return ret;
95
    }
96
97 cf13f204 Michael Niedermayer
    memcpy(c->frame.data    , frame->data    , sizeof(frame->data));
98
    memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
99 efdc74ef Michael Niedermayer
    c->frame.interlaced_frame= frame->interlaced_frame;
100
    c->frame.top_field_first = frame->top_field_first;
101 09a1416d Roger Pau Monné
    c->frame.key_frame = frame->key_frame;
102
    c->frame.pict_type = frame->pict_type;
103 cf13f204 Michael Niedermayer
    c->pts = pts;
104
    c->pixel_aspect = pixel_aspect;
105
    c->has_frame = 1;
106
107
    return 0;
108
}
109
110 7d948dc4 Michael Niedermayer
int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
111
                             int64_t pts, AVRational pixel_aspect)
112
{
113
    BufferSourceContext *c = buffer_filter->priv;
114
115
    return av_vsrc_buffer_add_frame2(buffer_filter, frame,
116
                              pts, pixel_aspect, c->w,
117
                              c->h, c->pix_fmt, "");
118
}
119
120 cf13f204 Michael Niedermayer
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
121
{
122
    BufferSourceContext *c = ctx->priv;
123 59775b3c Stefano Sabatini
    char pix_fmt_str[128];
124 b8dddebf Stefano Sabatini
    int n = 0;
125 cf13f204 Michael Niedermayer
126 94498ec9 Stefano Sabatini
    if (!args ||
127 7b3ea550 Michael Niedermayer
        (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
128
                    &c->time_base.num, &c->time_base.den,
129
                    &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
130 f763a7c6 Stefano Sabatini
        av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
131 b8dddebf Stefano Sabatini
        return AVERROR(EINVAL);
132
    }
133 59775b3c Stefano Sabatini
    if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
134
        char *tail;
135
        c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
136
        if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
137
            av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
138
            return AVERROR(EINVAL);
139
        }
140
    }
141 cf13f204 Michael Niedermayer
142 e2f48c05 Stefano Sabatini
    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);
143 b8dddebf Stefano Sabatini
    return 0;
144 cf13f204 Michael Niedermayer
}
145
146
static int query_formats(AVFilterContext *ctx)
147
{
148
    BufferSourceContext *c = ctx->priv;
149
    enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
150
151
    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
152
    return 0;
153
}
154
155
static int config_props(AVFilterLink *link)
156
{
157
    BufferSourceContext *c = link->src->priv;
158
159
    link->w = c->w;
160
    link->h = c->h;
161 7b3ea550 Michael Niedermayer
    link->sample_aspect_ratio = c->pixel_aspect;
162 94498ec9 Stefano Sabatini
    link->time_base = c->time_base;
163 cf13f204 Michael Niedermayer
164
    return 0;
165
}
166
167
static int request_frame(AVFilterLink *link)
168
{
169
    BufferSourceContext *c = link->src->priv;
170 ecc8dada S.N. Hemanth Meenakshisundaram
    AVFilterBufferRef *picref;
171 cf13f204 Michael Niedermayer
172
    if (!c->has_frame) {
173
        av_log(link->src, AV_LOG_ERROR,
174
               "request_frame() called with no available frame!\n");
175
        //return -1;
176
    }
177
178
    /* This picture will be needed unmodified later for decoding the next
179
     * frame */
180
    picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
181
                                       AV_PERM_REUSE2,
182
                                       link->w, link->h);
183
184 34017fd9 Stefano Sabatini
    av_image_copy(picref->data, picref->linesize,
185 4afbcf46 Stefano Sabatini
                  c->frame.data, c->frame.linesize,
186
                  picref->format, link->w, link->h);
187 cf13f204 Michael Niedermayer
188 cc80caff S.N. Hemanth Meenakshisundaram
    picref->pts                    = c->pts;
189
    picref->video->pixel_aspect    = c->pixel_aspect;
190
    picref->video->interlaced      = c->frame.interlaced_frame;
191
    picref->video->top_field_first = c->frame.top_field_first;
192 09a1416d Roger Pau Monné
    picref->video->key_frame       = c->frame.key_frame;
193
    picref->video->pict_type       = c->frame.pict_type;
194 7fce481a S.N. Hemanth Meenakshisundaram
    avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
195 cf13f204 Michael Niedermayer
    avfilter_draw_slice(link, 0, link->h, 1);
196
    avfilter_end_frame(link);
197 7fce481a S.N. Hemanth Meenakshisundaram
    avfilter_unref_buffer(picref);
198 cf13f204 Michael Niedermayer
199
    c->has_frame = 0;
200
201
    return 0;
202
}
203
204
static int poll_frame(AVFilterLink *link)
205
{
206
    BufferSourceContext *c = link->src->priv;
207
    return !!(c->has_frame);
208
}
209
210 4055438b Stefano Sabatini
AVFilter avfilter_vsrc_buffer = {
211 cf13f204 Michael Niedermayer
    .name      = "buffer",
212 ce1f8536 Stefano Sabatini
    .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
213 cf13f204 Michael Niedermayer
    .priv_size = sizeof(BufferSourceContext),
214
    .query_formats = query_formats,
215
216
    .init      = init,
217
218
    .inputs    = (AVFilterPad[]) {{ .name = NULL }},
219
    .outputs   = (AVFilterPad[]) {{ .name            = "default",
220
                                    .type            = AVMEDIA_TYPE_VIDEO,
221
                                    .request_frame   = request_frame,
222
                                    .poll_frame      = poll_frame,
223
                                    .config_props    = config_props, },
224
                                  { .name = NULL}},
225
};