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
28 #include "vsrc_buffer.h"
29 #include "libavutil/imgutils.h"
36 enum PixelFormat pix_fmt;
37 AVRational time_base; ///< time_base to set in the output link
38 AVRational sample_aspect_ratio;
40 } BufferSourceContext;
42 int av_vsrc_buffer_add_frame2(AVFilterContext *buffer_filter, AVFrame *frame,
43 int64_t pts, int width,
44 int height, enum PixelFormat pix_fmt,
45 const char *sws_param)
47 BufferSourceContext *c = buffer_filter->priv;
51 av_log(buffer_filter, AV_LOG_ERROR,
52 "Buffering several frames is not supported. "
53 "Please consume all available frames before adding a new one.\n"
59 snprintf(c->sws_param, 255, "%d:%d:%s", c->w, c->h, sws_param);
62 if(width != c->w || height != c->h || pix_fmt != c->pix_fmt){
63 AVFilterContext *scale= buffer_filter->outputs[0]->dst;
66 av_log(buffer_filter, AV_LOG_INFO,
67 "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
68 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
69 width, height, av_pix_fmt_descriptors[pix_fmt].name);
71 if(!scale || strcmp(scale->filter->name,"scale")){
72 AVFilter *f= avfilter_get_by_name("scale");
74 av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
75 if(avfilter_open(&scale, f, "Input equalizer") < 0)
78 if((ret=avfilter_init_filter(scale, c->sws_param, NULL))<0){
83 if((ret=avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0))<0){
87 scale->outputs[0]->time_base = scale->inputs[0]->time_base;
89 scale->outputs[0]->format= c->pix_fmt;
90 } else if(!strcmp(scale->filter->name, "scale")) {
91 snprintf(c->sws_param, 255, "%d:%d:%s", scale->outputs[0]->w, scale->outputs[0]->h, sws_param);
92 scale->filter->init(scale, c->sws_param, NULL);
95 c->pix_fmt= scale->inputs[0]->format= pix_fmt;
96 c->w= scale->inputs[0]->w= width;
97 c->h= scale->inputs[0]->h= height;
99 link= scale->outputs[0];
100 if ((ret = link->srcpad->config_props(link)) < 0)
104 memcpy(c->frame.data , frame->data , sizeof(frame->data));
105 memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
106 c->frame.width = frame->width;
107 c->frame.height = frame->height;
108 c->frame.format = frame->format;
109 c->frame.interlaced_frame= frame->interlaced_frame;
110 c->frame.top_field_first = frame->top_field_first;
111 c->frame.key_frame = frame->key_frame;
112 c->frame.pict_type = frame->pict_type;
113 c->frame.sample_aspect_ratio = frame->sample_aspect_ratio;
120 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
123 BufferSourceContext *c = buffer_filter->priv;
125 return av_vsrc_buffer_add_frame2(buffer_filter, frame,
127 c->h, c->pix_fmt, "");
130 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
132 BufferSourceContext *c = ctx->priv;
133 char pix_fmt_str[128];
137 (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
138 &c->time_base.num, &c->time_base.den,
139 &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den)) != 7) {
140 av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but only %d found in '%s'\n", n, args);
141 return AVERROR(EINVAL);
143 if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
145 c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
146 if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
147 av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
148 return AVERROR(EINVAL);
152 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);
156 static int query_formats(AVFilterContext *ctx)
158 BufferSourceContext *c = ctx->priv;
159 enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
161 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
165 static int config_props(AVFilterLink *link)
167 BufferSourceContext *c = link->src->priv;
171 link->sample_aspect_ratio = c->sample_aspect_ratio;
172 link->time_base = c->time_base;
177 static int request_frame(AVFilterLink *link)
179 BufferSourceContext *c = link->src->priv;
180 AVFilterBufferRef *picref;
183 av_log(link->src, AV_LOG_ERROR,
184 "request_frame() called with no available frame!\n");
188 /* This picture will be needed unmodified later for decoding the next
190 picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
194 av_image_copy(picref->data, picref->linesize,
195 c->frame.data, c->frame.linesize,
196 picref->format, link->w, link->h);
197 avfilter_copy_frame_props(picref, &c->frame);
198 picref->pts = c->pts;
200 avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
201 avfilter_draw_slice(link, 0, link->h, 1);
202 avfilter_end_frame(link);
203 avfilter_unref_buffer(picref);
210 static int poll_frame(AVFilterLink *link)
212 BufferSourceContext *c = link->src->priv;
213 return !!(c->has_frame);
216 AVFilter avfilter_vsrc_buffer = {
218 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
219 .priv_size = sizeof(BufferSourceContext),
220 .query_formats = query_formats,
224 .inputs = (AVFilterPad[]) {{ .name = NULL }},
225 .outputs = (AVFilterPad[]) {{ .name = "default",
226 .type = AVMEDIA_TYPE_VIDEO,
227 .request_frame = request_frame,
228 .poll_frame = poll_frame,
229 .config_props = config_props, },