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/fifo.h"
00032 #include "libavutil/imgutils.h"
00033
00034 typedef struct {
00035 AVFilterContext *scale;
00036 AVFifoBuffer *fifo;
00037 int h, w;
00038 enum PixelFormat pix_fmt;
00039 AVRational time_base;
00040 AVRational sample_aspect_ratio;
00041 char sws_param[256];
00042 int eof;
00043 unsigned nb_failed_requests;
00044 } BufferSourceContext;
00045
00046 #define CHECK_PARAM_CHANGE(s, c, width, height, format)\
00047 if (c->w != width || c->h != height || c->pix_fmt != format) {\
00048 av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
00049 return AVERROR(EINVAL);\
00050 }
00051
00052 int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
00053 AVFilterBufferRef *picref, int flags)
00054 {
00055 BufferSourceContext *c = buffer_filter->priv;
00056 AVFilterLink *outlink = buffer_filter->outputs[0];
00057 AVFilterBufferRef *buf;
00058 int ret;
00059
00060 if (!picref) {
00061 c->eof = 1;
00062 return 0;
00063 } else if (c->eof)
00064 return AVERROR(EINVAL);
00065
00066 if (!av_fifo_space(c->fifo) &&
00067 (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
00068 sizeof(buf))) < 0)
00069 return ret;
00070
00071 if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) {
00072 AVFilterContext *scale = buffer_filter->outputs[0]->dst;
00073 AVFilterLink *link;
00074 char scale_param[1024];
00075
00076 av_log(buffer_filter, AV_LOG_INFO,
00077 "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
00078 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
00079 picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name);
00080
00081 if (!scale || strcmp(scale->filter->name, "scale")) {
00082 AVFilter *f = avfilter_get_by_name("scale");
00083
00084 av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
00085 if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0)
00086 return ret;
00087
00088 c->scale = scale;
00089
00090 snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", c->w, c->h, c->sws_param);
00091 if ((ret = avfilter_init_filter(scale, scale_param, NULL)) < 0) {
00092 return ret;
00093 }
00094
00095 if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) {
00096 return ret;
00097 }
00098 scale->outputs[0]->time_base = scale->inputs[0]->time_base;
00099
00100 scale->outputs[0]->format= c->pix_fmt;
00101 } else if (!strcmp(scale->filter->name, "scale")) {
00102 snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s",
00103 scale->outputs[0]->w, scale->outputs[0]->h, c->sws_param);
00104 scale->filter->init(scale, scale_param, NULL);
00105 }
00106
00107 c->pix_fmt = scale->inputs[0]->format = picref->format;
00108 c->w = scale->inputs[0]->w = picref->video->w;
00109 c->h = scale->inputs[0]->h = picref->video->h;
00110
00111 link = scale->outputs[0];
00112 if ((ret = link->srcpad->config_props(link)) < 0)
00113 return ret;
00114 }
00115
00116 buf = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
00117 picref->video->w, picref->video->h);
00118 av_image_copy(buf->data, buf->linesize,
00119 (void*)picref->data, picref->linesize,
00120 picref->format, picref->video->w, picref->video->h);
00121 avfilter_copy_buffer_ref_props(buf, picref);
00122
00123 if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
00124 avfilter_unref_buffer(buf);
00125 return ret;
00126 }
00127 c->nb_failed_requests = 0;
00128
00129 return 0;
00130 }
00131
00132 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
00133 {
00134 BufferSourceContext *c = s->priv;
00135 int ret;
00136
00137 if (!buf) {
00138 c->eof = 1;
00139 return 0;
00140 } else if (c->eof)
00141 return AVERROR(EINVAL);
00142
00143 if (!av_fifo_space(c->fifo) &&
00144 (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
00145 sizeof(buf))) < 0)
00146 return ret;
00147
00148
00149
00150 if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0)
00151 return ret;
00152 c->nb_failed_requests = 0;
00153
00154 return 0;
00155 }
00156
00157 #if CONFIG_AVCODEC
00158 #include "avcodec.h"
00159
00160 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
00161 const AVFrame *frame, int flags)
00162 {
00163 BufferSourceContext *c = buffer_src->priv;
00164 AVFilterBufferRef *picref;
00165 int ret;
00166
00167 if (!frame) {
00168 c->eof = 1;
00169 return 0;
00170 } else if (c->eof)
00171 return AVERROR(EINVAL);
00172
00173 picref = avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE);
00174 if (!picref)
00175 return AVERROR(ENOMEM);
00176 ret = av_vsrc_buffer_add_video_buffer_ref(buffer_src, picref, flags);
00177 picref->buf->data[0] = NULL;
00178 avfilter_unref_buffer(picref);
00179
00180 return ret;
00181 }
00182 #endif
00183
00184 unsigned av_vsrc_buffer_get_nb_failed_requests(AVFilterContext *buffer_src)
00185 {
00186 return ((BufferSourceContext *)buffer_src->priv)->nb_failed_requests;
00187 }
00188
00189 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00190 {
00191 BufferSourceContext *c = ctx->priv;
00192 char pix_fmt_str[128];
00193 int ret, n = 0;
00194 *c->sws_param = 0;
00195
00196 if (!args ||
00197 (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
00198 &c->time_base.num, &c->time_base.den,
00199 &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) {
00200 av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
00201 return AVERROR(EINVAL);
00202 }
00203
00204 if ((ret = ff_parse_pixel_format(&c->pix_fmt, pix_fmt_str, ctx)) < 0)
00205 return ret;
00206
00207 if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*))))
00208 return AVERROR(ENOMEM);
00209
00210 av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
00211 c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
00212 c->time_base.num, c->time_base.den,
00213 c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param);
00214 return 0;
00215 }
00216
00217 static av_cold void uninit(AVFilterContext *ctx)
00218 {
00219 BufferSourceContext *s = ctx->priv;
00220 while (s->fifo && av_fifo_size(s->fifo)) {
00221 AVFilterBufferRef *buf;
00222 av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
00223 avfilter_unref_buffer(buf);
00224 }
00225 av_fifo_free(s->fifo);
00226 s->fifo = NULL;
00227 avfilter_free(s->scale);
00228 s->scale = NULL;
00229 }
00230
00231 static int query_formats(AVFilterContext *ctx)
00232 {
00233 BufferSourceContext *c = ctx->priv;
00234 enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
00235
00236 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00237 return 0;
00238 }
00239
00240 static int config_props(AVFilterLink *link)
00241 {
00242 BufferSourceContext *c = link->src->priv;
00243
00244 link->w = c->w;
00245 link->h = c->h;
00246 link->sample_aspect_ratio = c->sample_aspect_ratio;
00247 link->time_base = c->time_base;
00248
00249 return 0;
00250 }
00251
00252 static int request_frame(AVFilterLink *link)
00253 {
00254 BufferSourceContext *c = link->src->priv;
00255 AVFilterBufferRef *buf;
00256
00257 if (!av_fifo_size(c->fifo)) {
00258 if (c->eof)
00259 return AVERROR_EOF;
00260 c->nb_failed_requests++;
00261 return AVERROR(EAGAIN);
00262 }
00263 av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
00264
00265 avfilter_start_frame(link, avfilter_ref_buffer(buf, ~0));
00266 avfilter_draw_slice(link, 0, link->h, 1);
00267 avfilter_end_frame(link);
00268 avfilter_unref_buffer(buf);
00269
00270 return 0;
00271 }
00272
00273 static int poll_frame(AVFilterLink *link)
00274 {
00275 BufferSourceContext *c = link->src->priv;
00276 int size = av_fifo_size(c->fifo);
00277 if (!size && c->eof)
00278 return AVERROR_EOF;
00279 return size/sizeof(AVFilterBufferRef*);
00280 }
00281
00282 AVFilter avfilter_vsrc_buffer = {
00283 .name = "buffer",
00284 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
00285 .priv_size = sizeof(BufferSourceContext),
00286 .query_formats = query_formats,
00287
00288 .init = init,
00289 .uninit = uninit,
00290
00291 .inputs = (const AVFilterPad[]) {{ .name = NULL }},
00292 .outputs = (const AVFilterPad[]) {{ .name = "default",
00293 .type = AVMEDIA_TYPE_VIDEO,
00294 .request_frame = request_frame,
00295 .poll_frame = poll_frame,
00296 .config_props = config_props, },
00297 { .name = NULL}},
00298 };