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