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