]> git.sesse.net Git - ffmpeg/blob - libavfilter/audio.c
Merge commit 'b047c68783aa4042b322af7af043b643d5daf09c'
[ffmpeg] / libavfilter / audio.c
1 /*
2  * Copyright (c) Stefano Sabatini | stefasab at gmail.com
3  * Copyright (c) S.N. Hemanth Meenakshisundaram | smeenaks at ucsd.edu
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/avassert.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/common.h"
25
26 #include "audio.h"
27 #include "avfilter.h"
28 #include "internal.h"
29
30 int avfilter_ref_get_channels(AVFilterBufferRef *ref)
31 {
32     return ref->audio ? ref->audio->channels : 0;
33 }
34
35 AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
36                                             int nb_samples)
37 {
38     return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
39 }
40
41 AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
42                                                int nb_samples)
43 {
44     AVFilterBufferRef *samplesref = NULL;
45     uint8_t **data;
46     int planar      = av_sample_fmt_is_planar(link->format);
47     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
48     int planes      = planar ? nb_channels : 1;
49     int linesize;
50     int full_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE |
51                      AV_PERM_REUSE | AV_PERM_REUSE2 | AV_PERM_ALIGN;
52
53     av_assert1(!(perms & ~(full_perms | AV_PERM_NEG_LINESIZES)));
54
55     if (!(data = av_mallocz(sizeof(*data) * planes)))
56         goto fail;
57
58     if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
59         goto fail;
60
61     samplesref = avfilter_get_audio_buffer_ref_from_arrays(data, linesize, full_perms,
62                                                            nb_samples, link->format,
63                                                            link->channel_layout);
64     if (!samplesref)
65         goto fail;
66
67     samplesref->audio->sample_rate = link->sample_rate;
68
69     av_freep(&data);
70
71 fail:
72     if (data)
73         av_freep(&data[0]);
74     av_freep(&data);
75     return samplesref;
76 }
77
78 AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
79                                        int nb_samples)
80 {
81     AVFilterBufferRef *ret = NULL;
82
83     if (link->dstpad->get_audio_buffer)
84         ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
85
86     if (!ret)
87         ret = ff_default_get_audio_buffer(link, perms, nb_samples);
88
89     if (ret)
90         ret->type = AVMEDIA_TYPE_AUDIO;
91
92     return ret;
93 }
94
95 AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
96                                                              int linesize,int perms,
97                                                              int nb_samples,
98                                                              enum AVSampleFormat sample_fmt,
99                                                              uint64_t channel_layout)
100 {
101     int channels = av_get_channel_layout_nb_channels(channel_layout);
102     int planes;
103     AVFilterBuffer    *samples    = av_mallocz(sizeof(*samples));
104     AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
105
106     if (!samples || !samplesref)
107         goto fail;
108
109     samplesref->buf         = samples;
110     samplesref->buf->free   = ff_avfilter_default_free_buffer;
111     if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
112         goto fail;
113
114     samplesref->audio->nb_samples     = nb_samples;
115     samplesref->audio->channel_layout = channel_layout;
116     samplesref->audio->channels       = channels;
117
118     planes = av_sample_fmt_is_planar(sample_fmt) ? channels : 1;
119
120     /* make sure the buffer gets read permission or it's useless for output */
121     samplesref->perms = perms | AV_PERM_READ;
122
123     samples->refcount  = 1;
124     samplesref->type   = AVMEDIA_TYPE_AUDIO;
125     samplesref->format = sample_fmt;
126
127     memcpy(samples->data, data,
128            FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
129     memcpy(samplesref->data, samples->data, sizeof(samples->data));
130
131     samples->linesize[0] = samplesref->linesize[0] = linesize;
132
133     if (planes > FF_ARRAY_ELEMS(samples->data)) {
134         samples->   extended_data = av_mallocz(sizeof(*samples->extended_data) *
135                                                planes);
136         samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
137                                                planes);
138
139         if (!samples->extended_data || !samplesref->extended_data)
140             goto fail;
141
142         memcpy(samples->   extended_data, data, sizeof(*data)*planes);
143         memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
144     } else {
145         samples->extended_data    = samples->data;
146         samplesref->extended_data = samplesref->data;
147     }
148
149     samplesref->pts = AV_NOPTS_VALUE;
150
151     return samplesref;
152
153 fail:
154     if (samples && samples->extended_data != samples->data)
155         av_freep(&samples->extended_data);
156     if (samplesref) {
157         av_freep(&samplesref->audio);
158         if (samplesref->extended_data != samplesref->data)
159             av_freep(&samplesref->extended_data);
160     }
161     av_freep(&samplesref);
162     av_freep(&samples);
163     return NULL;
164 }
165
166 static int default_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
167 {
168     return ff_filter_frame(link->dst->outputs[0], frame);
169 }
170
171 int ff_filter_samples_framed(AVFilterLink *link, AVFilterBufferRef *samplesref)
172 {
173     int (*filter_frame)(AVFilterLink *, AVFilterBufferRef *);
174     AVFilterPad *src = link->srcpad;
175     AVFilterPad *dst = link->dstpad;
176     int64_t pts;
177     AVFilterBufferRef *buf_out;
178     int ret;
179
180     FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1);
181
182     if (link->closed) {
183         avfilter_unref_buffer(samplesref);
184         return AVERROR_EOF;
185     }
186
187     if (!(filter_frame = dst->filter_frame))
188         filter_frame = default_filter_frame;
189
190     av_assert1((samplesref->perms & src->min_perms) == src->min_perms);
191     samplesref->perms &= ~ src->rej_perms;
192
193     /* prepare to copy the samples if the buffer has insufficient permissions */
194     if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
195         dst->rej_perms & samplesref->perms) {
196         av_log(link->dst, AV_LOG_DEBUG,
197                "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
198                samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
199
200         buf_out = ff_default_get_audio_buffer(link, dst->min_perms,
201                                               samplesref->audio->nb_samples);
202         if (!buf_out) {
203             avfilter_unref_buffer(samplesref);
204             return AVERROR(ENOMEM);
205         }
206         buf_out->pts                = samplesref->pts;
207         buf_out->audio->sample_rate = samplesref->audio->sample_rate;
208
209         /* Copy actual data into new samples buffer */
210         av_samples_copy(buf_out->extended_data, samplesref->extended_data,
211                         0, 0, samplesref->audio->nb_samples,
212                         av_get_channel_layout_nb_channels(link->channel_layout),
213                         link->format);
214
215         avfilter_unref_buffer(samplesref);
216     } else
217         buf_out = samplesref;
218
219     link->cur_buf = buf_out;
220     pts = buf_out->pts;
221     ret = filter_frame(link, buf_out);
222     ff_update_link_current_pts(link, pts);
223     return ret;
224 }
225
226 int ff_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
227 {
228     int insamples = samplesref->audio->nb_samples, inpos = 0, nb_samples;
229     AVFilterBufferRef *pbuf = link->partial_buf;
230     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
231     int ret = 0;
232
233     av_assert1(samplesref->format                == link->format);
234     av_assert1(samplesref->audio->channels       == link->channels);
235     av_assert1(samplesref->audio->channel_layout == link->channel_layout);
236     av_assert1(samplesref->audio->sample_rate    == link->sample_rate);
237
238     if (!link->min_samples ||
239         (!pbuf &&
240          insamples >= link->min_samples && insamples <= link->max_samples)) {
241         return ff_filter_samples_framed(link, samplesref);
242     }
243     /* Handle framing (min_samples, max_samples) */
244     while (insamples) {
245         if (!pbuf) {
246             AVRational samples_tb = { 1, link->sample_rate };
247             int perms = link->dstpad->min_perms | AV_PERM_WRITE;
248             pbuf = ff_get_audio_buffer(link, perms, link->partial_buf_size);
249             if (!pbuf) {
250                 av_log(link->dst, AV_LOG_WARNING,
251                        "Samples dropped due to memory allocation failure.\n");
252                 return 0;
253             }
254             avfilter_copy_buffer_ref_props(pbuf, samplesref);
255             pbuf->pts = samplesref->pts +
256                         av_rescale_q(inpos, samples_tb, link->time_base);
257             pbuf->audio->nb_samples = 0;
258         }
259         nb_samples = FFMIN(insamples,
260                            link->partial_buf_size - pbuf->audio->nb_samples);
261         av_samples_copy(pbuf->extended_data, samplesref->extended_data,
262                         pbuf->audio->nb_samples, inpos,
263                         nb_samples, nb_channels, link->format);
264         inpos                   += nb_samples;
265         insamples               -= nb_samples;
266         pbuf->audio->nb_samples += nb_samples;
267         if (pbuf->audio->nb_samples >= link->min_samples) {
268             ret = ff_filter_samples_framed(link, pbuf);
269             pbuf = NULL;
270         }
271     }
272     avfilter_unref_buffer(samplesref);
273     link->partial_buf = pbuf;
274     return ret;
275 }