]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_buffer.c
rtsp: Parse RTP-Info headers
[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        time_base;     ///< time_base to set in the output link
37     AVRational        pixel_aspect;
38 } BufferSourceContext;
39
40 int av_vsrc_buffer_add_frame(AVFilterContext *buffer_filter, AVFrame *frame,
41                              int64_t pts, AVRational pixel_aspect)
42 {
43     BufferSourceContext *c = buffer_filter->priv;
44
45     if (c->has_frame) {
46         av_log(buffer_filter, AV_LOG_ERROR,
47                "Buffering several frames is not supported. "
48                "Please consume all available frames before adding a new one.\n"
49             );
50         //return -1;
51     }
52
53     memcpy(c->frame.data    , frame->data    , sizeof(frame->data));
54     memcpy(c->frame.linesize, frame->linesize, sizeof(frame->linesize));
55     c->frame.interlaced_frame= frame->interlaced_frame;
56     c->frame.top_field_first = frame->top_field_first;
57     c->pts = pts;
58     c->pixel_aspect = pixel_aspect;
59     c->has_frame = 1;
60
61     return 0;
62 }
63
64 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
65 {
66     BufferSourceContext *c = ctx->priv;
67     char pix_fmt_str[128];
68     int n = 0;
69
70     if (!args ||
71         (n = sscanf(args, "%d:%d:%127[^:]:%d:%d", &c->w, &c->h, pix_fmt_str, &c->time_base.num, &c->time_base.den)) != 5) {
72         av_log(ctx, AV_LOG_ERROR, "Expected 5 arguments, but only %d found in '%s'\n", n, args);
73         return AVERROR(EINVAL);
74     }
75     if ((c->pix_fmt = av_get_pix_fmt(pix_fmt_str)) == PIX_FMT_NONE) {
76         char *tail;
77         c->pix_fmt = strtol(pix_fmt_str, &tail, 10);
78         if (*tail || c->pix_fmt < 0 || c->pix_fmt >= PIX_FMT_NB) {
79             av_log(ctx, AV_LOG_ERROR, "Invalid pixel format string '%s'\n", pix_fmt_str);
80             return AVERROR(EINVAL);
81         }
82     }
83
84     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);
85     return 0;
86 }
87
88 static int query_formats(AVFilterContext *ctx)
89 {
90     BufferSourceContext *c = ctx->priv;
91     enum PixelFormat pix_fmts[] = { c->pix_fmt, PIX_FMT_NONE };
92
93     avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
94     return 0;
95 }
96
97 static int config_props(AVFilterLink *link)
98 {
99     BufferSourceContext *c = link->src->priv;
100
101     link->w = c->w;
102     link->h = c->h;
103     link->time_base = c->time_base;
104
105     return 0;
106 }
107
108 static int request_frame(AVFilterLink *link)
109 {
110     BufferSourceContext *c = link->src->priv;
111     AVFilterBufferRef *picref;
112
113     if (!c->has_frame) {
114         av_log(link->src, AV_LOG_ERROR,
115                "request_frame() called with no available frame!\n");
116         //return -1;
117     }
118
119     /* This picture will be needed unmodified later for decoding the next
120      * frame */
121     picref = avfilter_get_video_buffer(link, AV_PERM_WRITE | AV_PERM_PRESERVE |
122                                        AV_PERM_REUSE2,
123                                        link->w, link->h);
124
125     av_image_copy(picref->data, picref->linesize,
126                   c->frame.data, c->frame.linesize,
127                   picref->format, link->w, link->h);
128
129     picref->pts                    = c->pts;
130     picref->video->pixel_aspect    = c->pixel_aspect;
131     picref->video->interlaced      = c->frame.interlaced_frame;
132     picref->video->top_field_first = c->frame.top_field_first;
133     avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
134     avfilter_draw_slice(link, 0, link->h, 1);
135     avfilter_end_frame(link);
136     avfilter_unref_buffer(picref);
137
138     c->has_frame = 0;
139
140     return 0;
141 }
142
143 static int poll_frame(AVFilterLink *link)
144 {
145     BufferSourceContext *c = link->src->priv;
146     return !!(c->has_frame);
147 }
148
149 AVFilter avfilter_vsrc_buffer = {
150     .name      = "buffer",
151     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them accessible to the filterchain."),
152     .priv_size = sizeof(BufferSourceContext),
153     .query_formats = query_formats,
154
155     .init      = init,
156
157     .inputs    = (AVFilterPad[]) {{ .name = NULL }},
158     .outputs   = (AVFilterPad[]) {{ .name            = "default",
159                                     .type            = AVMEDIA_TYPE_VIDEO,
160                                     .request_frame   = request_frame,
161                                     .poll_frame      = poll_frame,
162                                     .config_props    = config_props, },
163                                   { .name = NULL}},
164 };