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 "vsrc_buffer.h"
00028 #include "libavutil/imgutils.h"
00029
00030 typedef struct {
00031 int64_t pts;
00032 AVFrame frame;
00033 int has_frame;
00034 int h, w;
00035 enum PixelFormat pix_fmt;
00036 AVRational time_base;
00037 AVRational pixel_aspect;
00038 } BufferSourceContext;
00039
00040 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
00041 int64_t pts, AVRational pixel_aspect)
00042 {
00043 BufferSourceContext *c = buffer_filter->priv;
00044
00045 if (c->has_frame) {
00046 av_log(buffer_filter, AV_LOG_ERROR,
00047 "Buffering several frames is not supported. "
00048 "Please consume all available frames before adding a new one.\n"
00049 );
00050
00051 }
00052
00053 memcpy(c->frame.data , frame->data , sizeof(frame->data));
00054 memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
00055 c->frame.interlaced_frame= frame->interlaced_frame;
00056 c->frame.top_field_first = frame->top_field_first;
00057 c->pts = pts;
00058 c->pixel_aspect = pixel_aspect;
00059 c->has_frame = 1;
00060
00061 return 0;
00062 }
00063
00064 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00065 {
00066 BufferSourceContext *c = ctx->priv;
00067 char pix_fmt_str[128];
00068 int n = 0;
00069
00070 if (!args ||
00071 (n = sscanf(args, "%d:%d:%127[^:]:%d:%d", &c->w, &c->h, pix_fmt_str, &c->time_base.num, &c->time_base.den)) != 5) {
00072 av_log(ctx, AV_LOG_ERROR, "Expected 5 arguments, but only %d found in '%s'\n", n, args);
00073 return AVERROR(EINVAL);
00074 }
00075 if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
00076 char *tail;
00077 c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
00078 if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
00079 av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
00080 return AVERROR(EINVAL);
00081 }
00082 }
00083
00084 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);
00085 return 0;
00086 }
00087
00088 static int query_formats(AVFilterContext *ctx)
00089 {
00090 BufferSourceContext *c = ctx->priv;
00091 enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
00092
00093 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
00094 return 0;
00095 }
00096
00097 static int config_props(AVFilterLink *link)
00098 {
00099 BufferSourceContext *c = link->src->priv;
00100
00101 link->w = c->w;
00102 link->h = c->h;
00103 link->time_base = c->time_base;
00104
00105 return 0;
00106 }
00107
00108 static int request_frame(AVFilterLink *link)
00109 {
00110 BufferSourceContext *c = link->src->priv;
00111 AVFilterBufferRef *picref;
00112
00113 if (!c->has_frame) {
00114 av_log(link->src, AV_LOG_ERROR,
00115 "request_frame() called with no available frame!\n");
00116
00117 }
00118
00119
00120
00121 picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
00122 AV_PERM_REUSE2,
00123 link->w, link->h);
00124
00125 av_image_copy(picref->data, picref->linesize,
00126 c->frame.data, c->frame.linesize,
00127 picref->format, link->w, link->h);
00128
00129 picref->pts = c->pts;
00130 picref->video->pixel_aspect = c->pixel_aspect;
00131 picref->video->interlaced = c->frame.interlaced_frame;
00132 picref->video->top_field_first = c->frame.top_field_first;
00133 avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
00134 avfilter_draw_slice(link, 0, link->h, 1);
00135 avfilter_end_frame(link);
00136 avfilter_unref_buffer(picref);
00137
00138 c->has_frame = 0;
00139
00140 return 0;
00141 }
00142
00143 static int poll_frame(AVFilterLink *link)
00144 {
00145 BufferSourceContext *c = link->src->priv;
00146 return !!(c->has_frame);
00147 }
00148
00149 AVFilter avfilter_vsrc_buffer = {
00150 .name = "buffer",
00151 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
00152 .priv_size = sizeof(BufferSourceContext),
00153 .query_formats = query_formats,
00154
00155 .init = init,
00156
00157 .inputs = (AVFilterPad[]) {{ .name = NULL }},
00158 .outputs = (AVFilterPad[]) {{ .name = "default",
00159 .type = AVMEDIA_TYPE_VIDEO,
00160 .request_frame = request_frame,
00161 .poll_frame = poll_frame,
00162 .config_props = config_props, },
00163 { .name = NULL}},
00164 };