]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_buffer.c
Add asink_anullsink - null audio sink.
[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 "vsrc_buffer.h"
28 #include "libavcore/imgutils.h"
29
30 typedef struct {
31     int64_t           pts;
32     AVFrame           frame;
33     int               has_frame;
34     int               h, w;
35     enum PixelFormat  pix_fmt;
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->has_frame) {
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     memcpy(c->frame.data    , frame->data    , sizeof(frame->data));
53     memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
54     c->frame.interlaced_frame= frame->interlaced_frame;
55     c->frame.top_field_first = frame->top_field_first;
56     c->pts = pts;
57     c->pixel_aspect = pixel_aspect;
58     c->has_frame = 1;
59
60     return 0;
61 }
62
63 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
64 {
65     BufferSourceContext *c = ctx->priv;
66     char pix_fmt_str[128];
67     int n = 0;
68
69     if (!args || (n = sscanf(args, "%d:%d:%127s", &c->w, &c->h, pix_fmt_str)) != 3) {
70         av_log(ctx, AV_LOG_ERROR, "Expected 3 arguments, but only %d found in '%s'\n", n, args ? args : "");
71         return AVERROR(EINVAL);
72     }
73     if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
74         char *tail;
75         c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
76         if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
77             av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
78             return AVERROR(EINVAL);
79         }
80     }
81
82     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);
83     return 0;
84 }
85
86 static int query_formats(AVFilterContext *ctx)
87 {
88     BufferSourceContext *c = ctx->priv;
89     enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
90
91     avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
92     return 0;
93 }
94
95 static int config_props(AVFilterLink *link)
96 {
97     BufferSourceContext *c = link->src->priv;
98
99     link->w = c->w;
100     link->h = c->h;
101
102     return 0;
103 }
104
105 static int request_frame(AVFilterLink *link)
106 {
107     BufferSourceContext *c = link->src->priv;
108     AVFilterBufferRef *picref;
109
110     if (!c->has_frame) {
111         av_log(link->src, AV_LOG_ERROR,
112                "request_frame() called with no available frame!\n");
113         //return -1;
114     }
115
116     /* This picture will be needed unmodified later for decoding the next
117      * frame */
118     picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
119                                        AV_PERM_REUSE2,
120                                        link->w, link->h);
121
122     av_image_copy(picref->data, picref->linesize,
123                   c->frame.data, c->frame.linesize,
124                   picref->format, link->w, link->h);
125
126     picref->pts                    = c->pts;
127     picref->video->pixel_aspect    = c->pixel_aspect;
128     picref->video->interlaced      = c->frame.interlaced_frame;
129     picref->video->top_field_first = c->frame.top_field_first;
130     avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
131     avfilter_draw_slice(link, 0, link->h, 1);
132     avfilter_end_frame(link);
133     avfilter_unref_buffer(picref);
134
135     c->has_frame = 0;
136
137     return 0;
138 }
139
140 static int poll_frame(AVFilterLink *link)
141 {
142     BufferSourceContext *c = link->src->priv;
143     return !!(c->has_frame);
144 }
145
146 AVFilter avfilter_vsrc_buffer = {
147     .name      = "buffer",
148     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
149     .priv_size = sizeof(BufferSourceContext),
150     .query_formats = query_formats,
151
152     .init      = init,
153
154     .inputs    = (AVFilterPad[]) {{ .name = NULL }},
155     .outputs   = (AVFilterPad[]) {{ .name            = "default",
156                                     .type            = AVMEDIA_TYPE_VIDEO,
157                                     .request_frame   = request_frame,
158                                     .poll_frame      = poll_frame,
159                                     .config_props    = config_props, },
160                                   { .name = NULL}},
161 };