vsrc_buffer: simplify av_vsrc_buffer_add_frame*() interface
[ffmpeg.git] / libavfilter / vsrc_buffer.c
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 "avcodec.h"
28 #include "vsrc_buffer.h"
29 #include "libavutil/imgutils.h"
30
31 typedef struct {
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                               const char *sws_param)
43 {
44     BufferSourceContext *c = buffer_filter->priv;
45     int ret;
46
47     if (c->has_frame) {
48         av_log(buffer_filter, AV_LOG_ERROR,
49                "Buffering several frames is not supported. "
50                "Please consume all available frames before adding a new one.\n"
51             );
52         //return -1;
53     }
54
55     if(!c->sws_param[0]){
56         snprintf(c->sws_param, 255, "%d:%d:%s", c->w, c->h, sws_param);
57     }
58
59     if (frame->width != c->w || frame->height != c->h || frame->format != c->pix_fmt) {
60         AVFilterContext *scale= buffer_filter->outputs[0]->dst;
61         AVFilterLink *link;
62
63         av_log(buffer_filter, AV_LOG_INFO,
64                "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
65                c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
66                frame->width, frame->height, av_pix_fmt_descriptors[frame->format].name);
67
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             if((ret=avfilter_init_filter(scale, c->sws_param, NULL))<0){
76                 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             scale->outputs[0]->time_base = scale->inputs[0]->time_base;
85
86             scale->outputs[0]->format= c->pix_fmt;
87         } else if(!strcmp(scale->filter->name, "scale")) {
88             snprintf(c->sws_param, 255, "%d:%d:%s", scale->outputs[0]->w, scale->outputs[0]->h, sws_param);
89             scale->filter->init(scale, c->sws_param, NULL);
90         }
91
92         c->pix_fmt = scale->inputs[0]->format = frame->format;
93         c->w       = scale->inputs[0]->w      = frame->width;
94         c->h       = scale->inputs[0]->h      = frame->height;
95
96         link= scale->outputs[0];
97         if ((ret =  link->srcpad->config_props(link)) < 0)
98             return ret;
99     }
100
101     c->frame = *frame;
102     memcpy(c->frame.data    , frame->data    , sizeof(frame->data));
103     memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
104     c->has_frame = 1;
105
106     return 0;
107 }
108
109 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame)
110 {
111     return av_vsrc_buffer_add_frame2(buffer_filter, frame, "");
112 }
113
114 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
115 {
116     BufferSourceContext *c = ctx->priv;
117     char pix_fmt_str[128];
118     int n = 0;
119
120     if (!args ||
121         (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
122                     &c->time_base.num, &c->time_base.den,
123                     &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den)) != 7) {
124         av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but only %d found in '%s'\n", n, args);
125         return AVERROR(EINVAL);
126     }
127     if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
128         char *tail;
129         c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
130         if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
131             av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
132             return AVERROR(EINVAL);
133         }
134     }
135
136     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);
137     return 0;
138 }
139
140 static int query_formats(AVFilterContext *ctx)
141 {
142     BufferSourceContext *c = ctx->priv;
143     enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
144
145     avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
146     return 0;
147 }
148
149 static int config_props(AVFilterLink *link)
150 {
151     BufferSourceContext *c = link->src->priv;
152
153     link->w = c->w;
154     link->h = c->h;
155     link->sample_aspect_ratio = c->sample_aspect_ratio;
156     link->time_base = c->time_base;
157
158     return 0;
159 }
160
161 static int request_frame(AVFilterLink *link)
162 {
163     BufferSourceContext *c = link->src->priv;
164     AVFilterBufferRef *picref;
165
166     if (!c->has_frame) {
167         av_log(link->src, AV_LOG_ERROR,
168                "request_frame() called with no available frame!\n");
169         //return -1;
170     }
171
172     /* This picture will be needed unmodified later for decoding the next
173      * frame */
174     picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
175                                        AV_PERM_REUSE2,
176                                        link->w, link->h);
177
178     av_image_copy(picref->data, picref->linesize,
179                   c->frame.data, c->frame.linesize,
180                   picref->format, link->w, link->h);
181     avfilter_copy_frame_props(picref, &c->frame);
182
183     avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
184     avfilter_draw_slice(link, 0, link->h, 1);
185     avfilter_end_frame(link);
186     avfilter_unref_buffer(picref);
187
188     c->has_frame = 0;
189
190     return 0;
191 }
192
193 static int poll_frame(AVFilterLink *link)
194 {
195     BufferSourceContext *c = link->src->priv;
196     return !!(c->has_frame);
197 }
198
199 AVFilter avfilter_vsrc_buffer = {
200     .name      = "buffer",
201     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
202     .priv_size = sizeof(BufferSourceContext),
203     .query_formats = query_formats,
204
205     .init      = init,
206
207     .inputs    = (AVFilterPad[]) {{ .name = NULL }},
208     .outputs   = (AVFilterPad[]) {{ .name            = "default",
209                                     .type            = AVMEDIA_TYPE_VIDEO,
210                                     .request_frame   = request_frame,
211                                     .poll_frame      = poll_frame,
212                                     .config_props    = config_props, },
213                                   { .name = NULL}},
214 };