]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_buffer.c
avconv: only set SAR once on the decoded frame.
[ffmpeg] / 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/fifo.h"
30 #include "libavutil/imgutils.h"
31
32 typedef struct {
33     AVFifoBuffer     *fifo;
34     int               h, w;
35     enum PixelFormat  pix_fmt;
36     AVRational        time_base;     ///< time_base to set in the output link
37     AVRational        pixel_aspect;
38 } BufferSourceContext;
39
40 #define CHECK_PARAM_CHANGE(s, c, width, height, format)\
41     if (c->w != width || c->h != height || c->pix_fmt != format) {\
42         av_log(s, AV_LOG_ERROR, "Changing frame properties on the fly is not supported.\n");\
43         return AVERROR(EINVAL);\
44     }
45
46 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
47                              int64_t pts, AVRational pixel_aspect)
48 {
49     BufferSourceContext *c = buffer_filter->priv;
50     AVFilterBufferRef *buf;
51     int ret;
52
53     if (!av_fifo_space(c->fifo) &&
54         (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
55                                          sizeof(buf))) < 0)
56         return ret;
57
58     CHECK_PARAM_CHANGE(buffer_filter, c, frame->width, frame->height, frame->format);
59
60     buf = avfilter_get_video_buffer(buffer_filter->outputs[0], AV_PERM_WRITE,
61                                     c->w, c->h);
62     av_image_copy(buf->data, buf->linesize, frame->data, frame->linesize,
63                   c->pix_fmt, c->w, c->h);
64
65     avfilter_copy_frame_props(buf, frame);
66     buf->pts                    = pts;
67     buf->video->pixel_aspect    = pixel_aspect;
68
69     if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0) {
70         avfilter_unref_buffer(buf);
71         return ret;
72     }
73
74     return 0;
75 }
76
77 int av_buffersrc_buffer(AVFilterContext *s, AVFilterBufferRef *buf)
78 {
79     BufferSourceContext *c = s->priv;
80     int ret;
81
82     if (!av_fifo_space(c->fifo) &&
83         (ret = av_fifo_realloc2(c->fifo, av_fifo_size(c->fifo) +
84                                          sizeof(buf))) < 0)
85         return ret;
86
87     CHECK_PARAM_CHANGE(s, c, buf->video->w, buf->video->h, buf->format);
88
89     if ((ret = av_fifo_generic_write(c->fifo, &buf, sizeof(buf), NULL)) < 0)
90         return ret;
91
92     return 0;
93 }
94
95 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
96 {
97     BufferSourceContext *c = ctx->priv;
98     char pix_fmt_str[128];
99     int n = 0;
100
101     if (!args ||
102         (n = sscanf(args, "%d:%d:%127[^:]:%d:%d:%d:%d", &c->w, &c->h, pix_fmt_str,
103                     &c->time_base.num, &c->time_base.den,
104                     &c->pixel_aspect.num, &c->pixel_aspect.den)) != 7) {
105         av_log(ctx, AV_LOG_ERROR, "Expected 7 arguments, but %d found in '%s'\n", n, args);
106         return AVERROR(EINVAL);
107     }
108     if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
109         char *tail;
110         c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
111         if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
112             av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
113             return AVERROR(EINVAL);
114         }
115     }
116
117     if (!(c->fifo = av_fifo_alloc(sizeof(AVFilterBufferRef*))))
118         return AVERROR(ENOMEM);
119
120     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);
121     return 0;
122 }
123
124 static av_cold void uninit(AVFilterContext *ctx)
125 {
126     BufferSourceContext *s = ctx->priv;
127     while (av_fifo_size(s->fifo)) {
128         AVFilterBufferRef *buf;
129         av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL);
130         avfilter_unref_buffer(buf);
131     }
132     av_fifo_free(s->fifo);
133     s->fifo = NULL;
134 }
135
136 static int query_formats(AVFilterContext *ctx)
137 {
138     BufferSourceContext *c = ctx->priv;
139     enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
140
141     avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
142     return 0;
143 }
144
145 static int config_props(AVFilterLink *link)
146 {
147     BufferSourceContext *c = link->src->priv;
148
149     link->w = c->w;
150     link->h = c->h;
151     link->sample_aspect_ratio = c->pixel_aspect;
152     link->time_base = c->time_base;
153
154     return 0;
155 }
156
157 static int request_frame(AVFilterLink *link)
158 {
159     BufferSourceContext *c = link->src->priv;
160     AVFilterBufferRef *buf;
161
162     if (!av_fifo_size(c->fifo)) {
163         av_log(link->src, AV_LOG_ERROR,
164                "request_frame() called with no available frame!\n");
165         return AVERROR(EINVAL);
166     }
167     av_fifo_generic_read(c->fifo, &buf, sizeof(buf), NULL);
168
169     avfilter_start_frame(link, avfilter_ref_buffer(buf, ~0));
170     avfilter_draw_slice(link, 0, link->h, 1);
171     avfilter_end_frame(link);
172     avfilter_unref_buffer(buf);
173
174     return 0;
175 }
176
177 static int poll_frame(AVFilterLink *link)
178 {
179     BufferSourceContext *c = link->src->priv;
180     return !!av_fifo_size(c->fifo);
181 }
182
183 AVFilter avfilter_vsrc_buffer = {
184     .name      = "buffer",
185     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
186     .priv_size = sizeof(BufferSourceContext),
187     .query_formats = query_formats,
188
189     .init      = init,
190     .uninit    = uninit,
191
192     .inputs    = (AVFilterPad[]) {{ .name = NULL }},
193     .outputs   = (AVFilterPad[]) {{ .name            = "default",
194                                     .type            = AVMEDIA_TYPE_VIDEO,
195                                     .request_frame   = request_frame,
196                                     .poll_frame      = poll_frame,
197                                     .config_props    = config_props, },
198                                   { .name = NULL}},
199 };