2 * Copyright (c) 2008 Vitor Sessak
4 * This file is part of FFmpeg.
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.
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.
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
23 * memory buffer source filter
27 #include "vsrc_buffer.h"
28 #include "libavutil/imgutils.h"
35 enum PixelFormat pix_fmt;
36 AVRational time_base; ///< time_base to set in the output link
37 AVRational pixel_aspect;
39 } BufferSourceContext;
41 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)
46 BufferSourceContext *c = buffer_filter->priv;
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"
58 snprintf(c->sws_param, 255, "%d:%d:%s", c->w, c->h, sws_param);
61 if(width != c->w || height != c->h || pix_fmt != c->pix_fmt){
62 AVFilterContext *scale= buffer_filter->outputs[0]->dst;
65 av_log(buffer_filter, AV_LOG_INFO, "Changing filter graph input to accept %dx%d %d (%d %d)\n",
66 width,height,pix_fmt, c->pix_fmt, scale && scale->outputs ? scale->outputs[0]->format : -123);
68 if(!scale || strcmp(scale->filter->name,"scale")){
69 AVFilter *f= avfilter_get_by_name("scale");
71 av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
72 if(avfilter_open(&scale, f, "Input equalizer") < 0)
75 if((ret=avfilter_init_filter(scale, c->sws_param, NULL))<0){
80 if((ret=avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0))<0){
84 scale->outputs[0]->time_base = scale->inputs[0]->time_base;
86 scale->outputs[0]->format= c->pix_fmt;
89 c->pix_fmt= scale->inputs[0]->format= pix_fmt;
90 c->w= scale->inputs[0]->w= width;
91 c->h= scale->inputs[0]->h= height;
93 link= scale->outputs[0];
94 if ((ret = link->srcpad->config_props(link)) < 0)
98 memcpy(c->frame.data , frame->data , sizeof(frame->data));
99 memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
100 c->frame.interlaced_frame= frame->interlaced_frame;
101 c->frame.top_field_first = frame->top_field_first;
102 c->frame.key_frame = frame->key_frame;
103 c->frame.pict_type = frame->pict_type;
105 c->pixel_aspect = pixel_aspect;
111 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
112 int64_t pts, AVRational pixel_aspect)
114 BufferSourceContext *c = buffer_filter->priv;
116 return av_vsrc_buffer_add_frame2(buffer_filter, frame,
117 pts, pixel_aspect, c->w,
118 c->h, c->pix_fmt, "");
121 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
123 BufferSourceContext *c = ctx->priv;
124 char pix_fmt_str[128];
128 (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
129 &c->time_base.num, &c->time_base.den,
130 &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
131 av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
132 return AVERROR(EINVAL);
134 if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
136 c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
137 if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
138 av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
139 return AVERROR(EINVAL);
143 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);
147 static int query_formats(AVFilterContext *ctx)
149 BufferSourceContext *c = ctx->priv;
150 enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
152 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
156 static int config_props(AVFilterLink *link)
158 BufferSourceContext *c = link->src->priv;
162 link->sample_aspect_ratio = c->pixel_aspect;
163 link->time_base = c->time_base;
168 static int request_frame(AVFilterLink *link)
170 BufferSourceContext *c = link->src->priv;
171 AVFilterBufferRef *picref;
174 av_log(link->src, AV_LOG_ERROR,
175 "request_frame() called with no available frame!\n");
179 /* This picture will be needed unmodified later for decoding the next
181 picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
185 av_image_copy(picref->data, picref->linesize,
186 c->frame.data, c->frame.linesize,
187 picref->format, link->w, link->h);
189 picref->pts = c->pts;
190 picref->video->pixel_aspect = c->pixel_aspect;
191 picref->video->interlaced = c->frame.interlaced_frame;
192 picref->video->top_field_first = c->frame.top_field_first;
193 picref->video->key_frame = c->frame.key_frame;
194 picref->video->pict_type = c->frame.pict_type;
195 avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
196 avfilter_draw_slice(link, 0, link->h, 1);
197 avfilter_end_frame(link);
198 avfilter_unref_buffer(picref);
205 static int poll_frame(AVFilterLink *link)
207 BufferSourceContext *c = link->src->priv;
208 return !!(c->has_frame);
211 AVFilter avfilter_vsrc_buffer = {
213 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
214 .priv_size = sizeof(BufferSourceContext),
215 .query_formats = query_formats,
219 .inputs = (AVFilterPad[]) {{ .name = NULL }},
220 .outputs = (AVFilterPad[]) {{ .name = "default",
221 .type = AVMEDIA_TYPE_VIDEO,
222 .request_frame = request_frame,
223 .poll_frame = poll_frame,
224 .config_props = config_props, },