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:%d:%d", &c->w, &c->h, pix_fmt_str,
00072 &c->time_base.num, &c->time_base.den,
00073 &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
00074 av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but only %d found in '%s'\n", n, args);
00075 return AVERROR(EINVAL);
00076 }
00077 if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
00078 char *tail;
00079 c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
00080 if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
00081 av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
00082 return AVERROR(EINVAL);
00083 }
00084 }
00085
00086 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);
00087 return 0;
00088 }
00089
00090 static int query_formats(AVFilterContext *ctx)
00091 {
00092 BufferSourceContext *c = ctx->priv;
00093 enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
00094
00095 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
00096 return 0;
00097 }
00098
00099 static int config_props(AVFilterLink *link)
00100 {
00101 BufferSourceContext *c = link->src->priv;
00102
00103 link->w = c->w;
00104 link->h = c->h;
00105 link->sample_aspect_ratio = c->pixel_aspect;
00106 link->time_base = c->time_base;
00107
00108 return 0;
00109 }
00110
00111 static int request_frame(AVFilterLink *link)
00112 {
00113 BufferSourceContext *c = link->src->priv;
00114 AVFilterBufferRef *picref;
00115
00116 if (!c->has_frame) {
00117 av_log(link->src, AV_LOG_ERROR,
00118 "request_frame() called with no available frame!\n");
00119
00120 }
00121
00122
00123
00124 picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
00125 AV_PERM_REUSE2,
00126 link->w, link->h);
00127
00128 av_image_copy(picref->data, picref->linesize,
00129 c->frame.data, c->frame.linesize,
00130 picref->format, link->w, link->h);
00131
00132 picref->pts = c->pts;
00133 picref->video->pixel_aspect = c->pixel_aspect;
00134 picref->video->interlaced = c->frame.interlaced_frame;
00135 picref->video->top_field_first = c->frame.top_field_first;
00136 avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
00137 avfilter_draw_slice(link, 0, link->h, 1);
00138 avfilter_end_frame(link);
00139 avfilter_unref_buffer(picref);
00140
00141 c->has_frame = 0;
00142
00143 return 0;
00144 }
00145
00146 static int poll_frame(AVFilterLink *link)
00147 {
00148 BufferSourceContext *c = link->src->priv;
00149 return !!(c->has_frame);
00150 }
00151
00152 AVFilter avfilter_vsrc_buffer = {
00153 .name = "buffer",
00154 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
00155 .priv_size = sizeof(BufferSourceContext),
00156 .query_formats = query_formats,
00157
00158 .init = init,
00159
00160 .inputs = (AVFilterPad[]) {{ .name = NULL }},
00161 .outputs = (AVFilterPad[]) {{ .name = "default",
00162 .type = AVMEDIA_TYPE_VIDEO,
00163 .request_frame = request_frame,
00164 .poll_frame = poll_frame,
00165 .config_props = config_props, },
00166 { .name = NULL}},
00167 };