lavfi: add a new function av_buffersrc_buffer().
[libav.git] / libavfilter / vsrc_buffer.c
1 /*
2 * Copyright (c) 2008 Vitor Sessak
3 *
4 * This file is part of Libav.
5 *
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * memory buffer source filter
24 */
25
26 #include "avfilter.h"
27 #include "buffersrc.h"
28 #include "vsrc_buffer.h"
29 #include "libavutil/imgutils.h"
30
31 typedef struct {
32 AVFilterBufferRef *buf;
33 int h, w;
34 enum PixelFormat pix_fmt;
35 AVRational time_base; ///< time_base to set in the output link
36 AVRational pixel_aspect;
37 } BufferSourceContext;
38
39 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
40 int64_t pts, AVRational pixel_aspect)
41 {
42 BufferSourceContext *c = buffer_filter->priv;
43
44 if (c->buf) {
45 av_log(buffer_filter, AV_LOG_ERROR,
46 "Buffering several frames is not supported. "
47 "Please consume all available frames before adding a new one.\n"
48 );
49 //return -1;
50 }
51
52 c->buf = avfilter_get_video_buffer(buffer_filter->outputs[0], AV_PERM_WRITE,
53 c->w, c->h);
54 av_image_copy(c->buf->data, c->buf->linesize, frame->data, frame->linesize,
55 c->pix_fmt, c->w, c->h);
56
57 avfilter_copy_frame_props(c->buf, frame);
58 c->buf->pts = pts;
59 c->buf->video->pixel_aspect = pixel_aspect;
60
61 return 0;
62 }
63
64 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
65 {
66 BufferSourceContext *c = s->priv;
67
68 if (c->buf) {
69 av_log(s, AV_LOG_ERROR,
70 "Buffering several frames is not supported. "
71 "Please consume all available frames before adding a new one.\n"
72 );
73 return AVERROR(EINVAL);
74 }
75
76 c->buf = buf;
77
78 return 0;
79 }
80
81 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
82 {
83 BufferSourceContext *c = ctx->priv;
84 char pix_fmt_str[128];
85 int n = 0;
86
87 if (!args ||
88 (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
89 &c->time_base.num, &c->time_base.den,
90 &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
91 av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
92 return AVERROR(EINVAL);
93 }
94 if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
95 char *tail;
96 c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
97 if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
98 av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
99 return AVERROR(EINVAL);
100 }
101 }
102
103 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);
104 return 0;
105 }
106
107 static int query_formats(AVFilterContext *ctx)
108 {
109 BufferSourceContext *c = ctx->priv;
110 enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
111
112 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
113 return 0;
114 }
115
116 static int config_props(AVFilterLink *link)
117 {
118 BufferSourceContext *c = link->src->priv;
119
120 link->w = c->w;
121 link->h = c->h;
122 link->sample_aspect_ratio = c->pixel_aspect;
123 link->time_base = c->time_base;
124
125 return 0;
126 }
127
128 static int request_frame(AVFilterLink *link)
129 {
130 BufferSourceContext *c = link->src->priv;
131
132 if (!c->buf) {
133 av_log(link->src, AV_LOG_ERROR,
134 "request_frame() called with no available frame!\n");
135 //return -1;
136 }
137
138 avfilter_start_frame(link, avfilter_ref_buffer(c->buf, ~0));
139 avfilter_draw_slice(link, 0, link->h, 1);
140 avfilter_end_frame(link);
141 avfilter_unref_buffer(c->buf);
142 c->buf = NULL;
143
144 return 0;
145 }
146
147 static int poll_frame(AVFilterLink *link)
148 {
149 BufferSourceContext *c = link->src->priv;
150 return !!(c->buf);
151 }
152
153 AVFilter avfilter_vsrc_buffer = {
154 .name = "buffer",
155 .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
156 .priv_size = sizeof(BufferSourceContext),
157 .query_formats = query_formats,
158
159 .init = init,
160
161 .inputs = (AVFilterPad[]) {{ .name = NULL }},
162 .outputs = (AVFilterPad[]) {{ .name = "default",
163 .type = AVMEDIA_TYPE_VIDEO,
164 .request_frame = request_frame,
165 .poll_frame = poll_frame,
166 .config_props = config_props, },
167 { .name = NULL}},
168 };