00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "avfilter.h"
00027 #include "internal.h"
00028 #include "avcodec.h"
00029 #include "buffersrc.h"
00030 #include "vsrc_buffer.h"
00031 #include "libavutil/imgutils.h"
00032
00033 typedef struct {
00034 AVFilterBufferRef *picref;
00035 int h, w;
00036 enum PixelFormat pix_fmt;
00037 AVRational time_base;
00038 AVRational sample_aspect_ratio;
00039 char sws_param[256];
00040 } BufferSourceContext;
00041
00042 #define CHECK_PARAM_CHANGE(s, c, width, height, format)\
00043 if (c->w != width || c->h != height || c->pix_fmt != format) {\
00044 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
00045 return AVERROR(EINVAL);\
00046 }
00047
00048 int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
00049 AVFilterBufferRef *picref, int flags)
00050 {
00051 BufferSourceContext *c = buffer_filter->priv;
00052 AVFilterLink *outlink = buffer_filter->outputs[0];
00053 int ret;
00054
00055 if (c->picref) {
00056 if (flags & AV_VSRC_BUF_FLAG_OVERWRITE) {
00057 avfilter_unref_buffer(c->picref);
00058 c->picref = NULL;
00059 } else {
00060 av_log(buffer_filter, AV_LOG_ERROR,
00061 "Buffering several frames is not supported. "
00062 "Please consume all available frames before adding a new one.\n");
00063 return AVERROR(EINVAL);
00064 }
00065 }
00066
00067 if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) {
00068 AVFilterContext *scale = buffer_filter->outputs[0]->dst;
00069 AVFilterLink *link;
00070 char scale_param[1024];
00071
00072 av_log(buffer_filter, AV_LOG_INFO,
00073 "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
00074 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
00075 picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name);
00076
00077 if (!scale || strcmp(scale->filter->name, "scale")) {
00078 AVFilter *f = avfilter_get_by_name("scale");
00079
00080 av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
00081 if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0)
00082 return ret;
00083
00084 snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", c->w, c->h, c->sws_param);
00085 if ((ret = avfilter_init_filter(scale, scale_param, NULL)) < 0) {
00086 avfilter_free(scale);
00087 return ret;
00088 }
00089
00090 if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) {
00091 avfilter_free(scale);
00092 return ret;
00093 }
00094 scale->outputs[0]->time_base = scale->inputs[0]->time_base;
00095
00096 scale->outputs[0]->format= c->pix_fmt;
00097 } else if (!strcmp(scale->filter->name, "scale")) {
00098 snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s",
00099 scale->outputs[0]->w, scale->outputs[0]->h, c->sws_param);
00100 scale->filter->init(scale, scale_param, NULL);
00101 }
00102
00103 c->pix_fmt = scale->inputs[0]->format = picref->format;
00104 c->w = scale->inputs[0]->w = picref->video->w;
00105 c->h = scale->inputs[0]->h = picref->video->h;
00106
00107 link = scale->outputs[0];
00108 if ((ret = link->srcpad->config_props(link)) < 0)
00109 return ret;
00110 }
00111
00112 c->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
00113 picref->video->w, picref->video->h);
00114 av_image_copy(c->picref->data, c->picref->linesize,
00115 (void*)picref->data, picref->linesize,
00116 picref->format, picref->video->w, picref->video->h);
00117 avfilter_copy_buffer_ref_props(c->picref, picref);
00118
00119 return 0;
00120 }
00121
00122 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
00123 {
00124 BufferSourceContext *c = s->priv;
00125
00126 if (c->picref) {
00127 av_log(s, AV_LOG_ERROR,
00128 "Buffering several frames is not supported. "
00129 "Please consume all available frames before adding a new one.\n"
00130 );
00131 return AVERROR(EINVAL);
00132 }
00133
00134
00135
00136 c->picref = buf;
00137
00138 return 0;
00139 }
00140
00141 #if CONFIG_AVCODEC
00142 #include "avcodec.h"
00143
00144 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
00145 const AVFrame *frame, int flags)
00146 {
00147 int ret;
00148 AVFilterBufferRef *picref =
00149 avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE);
00150 if (!picref)
00151 return AVERROR(ENOMEM);
00152 ret = av_vsrc_buffer_add_video_buffer_ref(buffer_src, picref, flags);
00153 picref->buf->data[0] = NULL;
00154 avfilter_unref_buffer(picref);
00155
00156 return ret;
00157 }
00158 #endif
00159
00160 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00161 {
00162 BufferSourceContext *c = ctx->priv;
00163 char pix_fmt_str[128];
00164 int ret, n = 0;
00165 *c->sws_param = 0;
00166
00167 if (!args ||
00168 (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
00169 &c->time_base.num, &c->time_base.den,
00170 &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) {
00171 av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
00172 return AVERROR(EINVAL);
00173 }
00174
00175 if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
00176 return ret;
00177
00178 av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
00179 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
00180 c->time_base.num, c->time_base.den,
00181 c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param);
00182 return 0;
00183 }
00184
00185 static av_cold void uninit(AVFilterContext *ctx)
00186 {
00187 BufferSourceContext *s = ctx->priv;
00188 if (s->picref)
00189 avfilter_unref_buffer(s->picref);
00190 s->picref = NULL;
00191 }
00192
00193 static int query_formats(AVFilterContext *ctx)
00194 {
00195 BufferSourceContext *c = ctx->priv;
00196 enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
00197
00198 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00199 return 0;
00200 }
00201
00202 static int config_props(AVFilterLink *link)
00203 {
00204 BufferSourceContext *c = link->src->priv;
00205
00206 link->w = c->w;
00207 link->h = c->h;
00208 link->sample_aspect_ratio = c->sample_aspect_ratio;
00209 link->time_base = c->time_base;
00210
00211 return 0;
00212 }
00213
00214 static int request_frame(AVFilterLink *link)
00215 {
00216 BufferSourceContext *c = link->src->priv;
00217
00218 if (!c->picref) {
00219 av_log(link->src, AV_LOG_WARNING,
00220 "request_frame() called with no available frame!\n");
00221 return AVERROR(EINVAL);
00222 }
00223
00224 avfilter_start_frame(link, avfilter_ref_buffer(c->picref, ~0));
00225 avfilter_draw_slice(link, 0, link->h, 1);
00226 avfilter_end_frame(link);
00227 avfilter_unref_buffer(c->picref);
00228 c->picref = NULL;
00229
00230 return 0;
00231 }
00232
00233 static int poll_frame(AVFilterLink *link)
00234 {
00235 BufferSourceContext *c = link->src->priv;
00236 return !!c->picref;
00237 }
00238
00239 AVFilter avfilter_vsrc_buffer = {
00240 .name = "buffer",
00241 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
00242 .priv_size = sizeof(BufferSourceContext),
00243 .query_formats = query_formats,
00244
00245 .init = init,
00246 .uninit = uninit,
00247
00248 .inputs = (const AVFilterPad[]) {{ .name = NULL }},
00249 .outputs = (const AVFilterPad[]) {{ .name = "default",
00250 .type = AVMEDIA_TYPE_VIDEO,
00251 .request_frame = request_frame,
00252 .poll_frame = poll_frame,
00253 .config_props = config_props, },
00254 { .name = NULL}},
00255 };