]> git.sesse.net Git - ffmpeg/blob - libavfilter/buffersink.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / buffersink.c
1 /*
2  * Copyright (c) 2011 Stefano Sabatini
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  * buffer sink
24  */
25
26 #include "libavutil/audio_fifo.h"
27 #include "libavutil/audioconvert.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/common.h"
30 #include "libavutil/mathematics.h"
31
32 #include "audio.h"
33 #include "avfilter.h"
34 #include "buffersink.h"
35 #include "internal.h"
36
37 typedef struct {
38     AVFilterBufferRef *cur_buf;  ///< last buffer delivered on the sink
39     AVAudioFifo  *audio_fifo;    ///< FIFO for audio samples
40     int64_t next_pts;            ///< interpolating audio pts
41 } BufferSinkContext;
42
43 static av_cold void uninit(AVFilterContext *ctx)
44 {
45     BufferSinkContext *sink = ctx->priv;
46
47     if (sink->audio_fifo)
48         av_audio_fifo_free(sink->audio_fifo);
49 }
50
51 static int start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
52 {
53     BufferSinkContext *s = link->dst->priv;
54
55 //     av_assert0(!s->cur_buf);
56     s->cur_buf    = buf;
57     link->cur_buf = NULL;
58
59     return 0;
60 }
61
62 int ff_buffersink_read_compat(AVFilterContext *ctx, AVFilterBufferRef **buf)
63 {
64     BufferSinkContext *s    = ctx->priv;
65     AVFilterLink      *link = ctx->inputs[0];
66     int ret;
67
68     if (!buf)
69         return ff_poll_frame(ctx->inputs[0]);
70
71     if ((ret = ff_request_frame(link)) < 0)
72         return ret;
73
74     if (!s->cur_buf)
75         return AVERROR(EINVAL);
76
77     *buf       = s->cur_buf;
78     s->cur_buf = NULL;
79
80     return 0;
81 }
82
83 static int read_from_fifo(AVFilterContext *ctx, AVFilterBufferRef **pbuf,
84                           int nb_samples)
85 {
86     BufferSinkContext *s = ctx->priv;
87     AVFilterLink   *link = ctx->inputs[0];
88     AVFilterBufferRef *buf;
89
90     if (!(buf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples)))
91         return AVERROR(ENOMEM);
92     av_audio_fifo_read(s->audio_fifo, (void**)buf->extended_data, nb_samples);
93
94     buf->pts = s->next_pts;
95     s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate},
96                                 link->time_base);
97
98     *pbuf = buf;
99     return 0;
100
101 }
102
103 int ff_buffersink_read_samples_compat(AVFilterContext *ctx, AVFilterBufferRef **pbuf,
104                                       int nb_samples)
105 {
106     BufferSinkContext *s = ctx->priv;
107     AVFilterLink   *link = ctx->inputs[0];
108     int ret = 0;
109
110     if (!s->audio_fifo) {
111         int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
112         if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples)))
113             return AVERROR(ENOMEM);
114     }
115
116     while (ret >= 0) {
117         AVFilterBufferRef *buf;
118
119         if (av_audio_fifo_size(s->audio_fifo) >= nb_samples)
120             return read_from_fifo(ctx, pbuf, nb_samples);
121
122         ret = av_buffersink_read(ctx, &buf);
123         if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo))
124             return read_from_fifo(ctx, pbuf, av_audio_fifo_size(s->audio_fifo));
125         else if (ret < 0)
126             return ret;
127
128         if (buf->pts != AV_NOPTS_VALUE) {
129             s->next_pts = buf->pts -
130                           av_rescale_q(av_audio_fifo_size(s->audio_fifo),
131                                        (AVRational){ 1, link->sample_rate },
132                                        link->time_base);
133         }
134
135         ret = av_audio_fifo_write(s->audio_fifo, (void**)buf->extended_data,
136                                   buf->audio->nb_samples);
137         avfilter_unref_buffer(buf);
138     }
139
140     return ret;
141 }
142
143 static const AVFilterPad avfilter_vsink_buffer_inputs[] = {
144     {
145         .name        = "default",
146         .type        = AVMEDIA_TYPE_VIDEO,
147         .start_frame = start_frame,
148         .min_perms   = AV_PERM_READ,
149         .needs_fifo  = 1
150     },
151     { NULL }
152 };
153
154 AVFilter avfilter_vsink_buffer = {
155 #if AV_HAVE_INCOMPATIBLE_FORK_ABI
156     .name      = "buffersink",
157 #else
158     .name      = "buffersink_old",
159 #endif
160     .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
161     .priv_size = sizeof(BufferSinkContext),
162     .uninit    = uninit,
163
164     .inputs    = avfilter_vsink_buffer_inputs,
165     .outputs   = NULL,
166 };
167
168 static const AVFilterPad avfilter_asink_abuffer_inputs[] = {
169     {
170         .name           = "default",
171         .type           = AVMEDIA_TYPE_AUDIO,
172         .filter_samples = start_frame,
173         .min_perms      = AV_PERM_READ,
174         .needs_fifo     = 1
175     },
176     { NULL }
177 };
178
179 AVFilter avfilter_asink_abuffer = {
180 #if AV_HAVE_INCOMPATIBLE_FORK_ABI
181     .name      = "abuffersink",
182 #else
183     .name      = "abuffersink_old",
184 #endif
185     .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
186     .priv_size = sizeof(BufferSinkContext),
187     .uninit    = uninit,
188
189     .inputs    = avfilter_asink_abuffer_inputs,
190     .outputs   = NULL,
191 };