]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_buffer.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vsrc_buffer.c
1 /*
2  * Copyright (c) 2008 Vitor Sessak
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "avcodec.h"
28 #include "vsrc_buffer.h"
29 #include "libavutil/imgutils.h"
30
31 typedef struct {
32     AVFilterBufferRef *picref;
33     int               h, w;
34     enum PixelFormat  pix_fmt;
35     AVRational        time_base;     ///< time_base to set in the output link
36     AVRational        sample_aspect_ratio;
37     char              sws_param[256];
38 } BufferSourceContext;
39
40 int av_vsrc_buffer_add_video_buffer_ref(AVFilterContext *buffer_filter,
41                                         AVFilterBufferRef *picref, int flags)
42 {
43     BufferSourceContext *c = buffer_filter->priv;
44     AVFilterLink *outlink = buffer_filter->outputs[0];
45     int ret;
46
47     if (c->picref) {
48         if (flags & AV_VSRC_BUF_FLAG_OVERWRITE) {
49             avfilter_unref_buffer(c->picref);
50             c->picref = NULL;
51         } else {
52             av_log(buffer_filter, AV_LOG_ERROR,
53                    "Buffering several frames is not supported. "
54                    "Please consume all available frames before adding a new one.\n");
55             return AVERROR(EINVAL);
56         }
57     }
58
59     if (picref->video->w != c->w || picref->video->h != c->h || picref->format != c->pix_fmt) {
60         AVFilterContext *scale = buffer_filter->outputs[0]->dst;
61         AVFilterLink *link;
62         char scale_param[1024];
63
64         av_log(buffer_filter, AV_LOG_INFO,
65                "Buffer video input changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
66                c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
67                picref->video->w, picref->video->h, av_pix_fmt_descriptors[picref->format].name);
68
69         if (!scale || strcmp(scale->filter->name, "scale")) {
70             AVFilter *f = avfilter_get_by_name("scale");
71
72             av_log(buffer_filter, AV_LOG_INFO, "Inserting scaler filter\n");
73             if ((ret = avfilter_open(&scale, f, "Input equalizer")) < 0)
74                 return ret;
75
76             snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s", c->w, c->h, c->sws_param);
77             if ((ret = avfilter_init_filter(scale, scale_param, NULL)) < 0) {
78                 avfilter_free(scale);
79                 return ret;
80             }
81
82             if ((ret = avfilter_insert_filter(buffer_filter->outputs[0], scale, 0, 0)) < 0) {
83                 avfilter_free(scale);
84                 return ret;
85             }
86             scale->outputs[0]->time_base = scale->inputs[0]->time_base;
87
88             scale->outputs[0]->format= c->pix_fmt;
89         } else if (!strcmp(scale->filter->name, "scale")) {
90             snprintf(scale_param, sizeof(scale_param)-1, "%d:%d:%s",
91                      scale->outputs[0]->w, scale->outputs[0]->h, c->sws_param);
92             scale->filter->init(scale, scale_param, NULL);
93         }
94
95         c->pix_fmt = scale->inputs[0]->format = picref->format;
96         c->w       = scale->inputs[0]->w      = picref->video->w;
97         c->h       = scale->inputs[0]->h      = picref->video->h;
98
99         link = scale->outputs[0];
100         if ((ret =  link->srcpad->config_props(link)) < 0)
101             return ret;
102     }
103
104     c->picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
105                                           picref->video->w, picref->video->h);
106     av_image_copy(c->picref->data, c->picref->linesize,
107                   picref->data, picref->linesize,
108                   picref->format, picref->video->w, picref->video->h);
109     avfilter_copy_buffer_ref_props(c->picref, picref);
110
111     return 0;
112 }
113
114 #if CONFIG_AVCODEC
115 #include "avcodec.h"
116
117 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_src,
118                              const AVFrame *frame, int flags)
119 {
120     int ret;
121     AVFilterBufferRef *picref =
122         avfilter_get_video_buffer_ref_from_frame(frame, AV_PERM_WRITE);
123     if (!picref)
124         return AVERROR(ENOMEM);
125     ret = av_vsrc_buffer_add_video_buffer_ref(buffer_src, picref, flags);
126     picref->buf->data[0] = NULL;
127     avfilter_unref_buffer(picref);
128
129     return ret;
130 }
131 #endif
132
133 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
134 {
135     BufferSourceContext *c = ctx->priv;
136     char pix_fmt_str[128];
137     int n = 0;
138     *c->sws_param = 0;
139
140     if (!args ||
141         (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d:%255c", &c->w, &c->h, pix_fmt_str,
142                     &c->time_base.num, &c->time_base.den,
143                     &c->sample_aspect_ratio.num, &c->sample_aspect_ratio.den, c->sws_param)) < 7) {
144         av_log(ctx, AV_LOG_ERROR, "Expected at least 7 arguments, but only %d found in '%s'\n", n, args);
145         return AVERROR(EINVAL);
146     }
147
148     if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
149         char *tail;
150         c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
151         if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
152             av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
153             return AVERROR(EINVAL);
154         }
155     }
156
157     av_log(ctx, AV_LOG_INFO, "w:%d h:%d pixfmt:%s tb:%d/%d sar:%d/%d sws_param:%s\n",
158            c->w, c->h, av_pix_fmt_descriptors[c->pix_fmt].name,
159            c->time_base.num, c->time_base.den,
160            c->sample_aspect_ratio.num, c->sample_aspect_ratio.den, c->sws_param);
161     return 0;
162 }
163
164 static int query_formats(AVFilterContext *ctx)
165 {
166     BufferSourceContext *c = ctx->priv;
167     enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
168
169     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
170     return 0;
171 }
172
173 static int config_props(AVFilterLink *link)
174 {
175     BufferSourceContext *c = link->src->priv;
176
177     link->w = c->w;
178     link->h = c->h;
179     link->sample_aspect_ratio = c->sample_aspect_ratio;
180     link->time_base = c->time_base;
181
182     return 0;
183 }
184
185 static int request_frame(AVFilterLink *link)
186 {
187     BufferSourceContext *c = link->src->priv;
188
189     if (!c->picref) {
190         av_log(link->src, AV_LOG_WARNING,
191                "request_frame() called with no available frame!\n");
192         return AVERROR(EINVAL);
193     }
194
195     avfilter_start_frame(link, avfilter_ref_buffer(c->picref, ~0));
196     avfilter_draw_slice(link, 0, link->h, 1);
197     avfilter_end_frame(link);
198     avfilter_unref_buffer(c->picref);
199     c->picref = NULL;
200
201     return 0;
202 }
203
204 static int poll_frame(AVFilterLink *link)
205 {
206     BufferSourceContext *c = link->src->priv;
207     return !!(c->picref);
208 }
209
210 AVFilter avfilter_vsrc_buffer = {
211     .name      = "buffer",
212     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
213     .priv_size = sizeof(BufferSourceContext),
214     .query_formats = query_formats,
215
216     .init      = init,
217
218     .inputs    = (AVFilterPad[]) {{ .name = NULL }},
219     .outputs   = (AVFilterPad[]) {{ .name            = "default",
220                                     .type            = AVMEDIA_TYPE_VIDEO,
221                                     .request_frame   = request_frame,
222                                     .poll_frame      = poll_frame,
223                                     .config_props    = config_props, },
224                                   { .name = NULL}},
225 };