Merge commit '36ef5369ee9b336febc2c270f8718cec4476cb85'
[ffmpeg.git] / 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/audioconvert.h"
24
25 #include "audio.h"
26 #include "avfilter.h"
27 #include "internal.h"
28
29 AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
30                                             int nb_samples)
31 {
32     return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
33 }
34
35 AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
36                                                int nb_samples)
37 {
38     AVFilterBufferRef *samplesref = NULL;
39     uint8_t **data;
40     int planar      = av_sample_fmt_is_planar(link->format);
41     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
42     int planes      = planar ? nb_channels : 1;
43     int linesize;
44
45     if (!(data = av_mallocz(sizeof(*data) * planes)))
46         goto fail;
47
48     if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
49         goto fail;
50
51     samplesref = avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
52                                                            nb_samples, link->format,
53                                                            link->channel_layout);
54     if (!samplesref)
55         goto fail;
56
57     av_freep(&data);
58
59 fail:
60     if (data)
61         av_freep(&data[0]);
62     av_freep(&data);
63     return samplesref;
64 }
65
66 AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
67                                        int nb_samples)
68 {
69     AVFilterBufferRef *ret = NULL;
70
71     if (link->dstpad->get_audio_buffer)
72         ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
73
74     if (!ret)
75         ret = ff_default_get_audio_buffer(link, perms, nb_samples);
76
77     if (ret)
78         ret->type = AVMEDIA_TYPE_AUDIO;
79
80     return ret;
81 }
82
83 AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
84                                                              int linesize,int perms,
85                                                              int nb_samples,
86                                                              enum AVSampleFormat sample_fmt,
87                                                              uint64_t channel_layout)
88 {
89     int planes;
90     AVFilterBuffer    *samples    = av_mallocz(sizeof(*samples));
91     AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
92
93     if (!samples || !samplesref)
94         goto fail;
95
96     samplesref->buf         = samples;
97     samplesref->buf->free   = ff_avfilter_default_free_buffer;
98     if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
99         goto fail;
100
101     samplesref->audio->nb_samples     = nb_samples;
102     samplesref->audio->channel_layout = channel_layout;
103
104     planes = av_sample_fmt_is_planar(sample_fmt) ?
105         av_get_channel_layout_nb_channels(channel_layout) : 1;
106
107     /* make sure the buffer gets read permission or it's useless for output */
108     samplesref->perms = perms | AV_PERM_READ;
109
110     samples->refcount  = 1;
111     samplesref->type   = AVMEDIA_TYPE_AUDIO;
112     samplesref->format = sample_fmt;
113
114     memcpy(samples->data, data,
115            FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
116     memcpy(samplesref->data, samples->data, sizeof(samples->data));
117
118     samples->linesize[0] = samplesref->linesize[0] = linesize;
119
120     if (planes > FF_ARRAY_ELEMS(samples->data)) {
121         samples->   extended_data = av_mallocz(sizeof(*samples->extended_data) *
122                                                planes);
123         samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
124                                                planes);
125
126         if (!samples->extended_data || !samplesref->extended_data)
127             goto fail;
128
129         memcpy(samples->   extended_data, data, sizeof(*data)*planes);
130         memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
131     } else {
132         samples->extended_data    = samples->data;
133         samplesref->extended_data = samplesref->data;
134     }
135
136     samplesref->pts = AV_NOPTS_VALUE;
137
138     return samplesref;
139
140 fail:
141     if (samples && samples->extended_data != samples->data)
142         av_freep(&samples->extended_data);
143     if (samplesref) {
144         av_freep(&samplesref->audio);
145         if (samplesref->extended_data != samplesref->data)
146             av_freep(&samplesref->extended_data);
147     }
148     av_freep(&samplesref);
149     av_freep(&samples);
150     return NULL;
151 }
152
153 static int default_filter_samples(AVFilterLink *link,
154                                   AVFilterBufferRef *samplesref)
155 {
156     return ff_filter_samples(link->dst->outputs[0], samplesref);
157 }
158
159 int ff_filter_samples_framed(AVFilterLink *link, AVFilterBufferRef *samplesref)
160 {
161     int (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
162     AVFilterPad *dst = link->dstpad;
163     int64_t pts;
164     AVFilterBufferRef *buf_out;
165     int ret;
166
167     FF_TPRINTF_START(NULL, filter_samples); ff_tlog_link(NULL, link, 1);
168
169     if (!(filter_samples = dst->filter_samples))
170         filter_samples = default_filter_samples;
171
172     /* prepare to copy the samples if the buffer has insufficient permissions */
173     if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
174         dst->rej_perms & samplesref->perms) {
175         av_log(link->dst, AV_LOG_DEBUG,
176                "Copying audio data in avfilter (have perms %x, need %x, reject %x)\n",
177                samplesref->perms, link->dstpad->min_perms, link->dstpad->rej_perms);
178
179         buf_out = ff_default_get_audio_buffer(link, dst->min_perms,
180                                               samplesref->audio->nb_samples);
181         if (!buf_out) {
182             avfilter_unref_buffer(samplesref);
183             return AVERROR(ENOMEM);
184         }
185         buf_out->pts                = samplesref->pts;
186         buf_out->audio->sample_rate = samplesref->audio->sample_rate;
187
188         /* Copy actual data into new samples buffer */
189         av_samples_copy(buf_out->extended_data, samplesref->extended_data,
190                         0, 0, samplesref->audio->nb_samples,
191                         av_get_channel_layout_nb_channels(link->channel_layout),
192                         link->format);
193
194         avfilter_unref_buffer(samplesref);
195     } else
196         buf_out = samplesref;
197
198     link->cur_buf = buf_out;
199     pts = buf_out->pts;
200     ret = filter_samples(link, buf_out);
201     ff_update_link_current_pts(link, pts);
202     return ret;
203 }
204
205 int ff_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
206 {
207     int insamples = samplesref->audio->nb_samples, inpos = 0, nb_samples;
208     AVFilterBufferRef *pbuf = link->partial_buf;
209     int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
210     int ret = 0;
211
212     if (!link->min_samples ||
213         (!pbuf &&
214          insamples >= link->min_samples && insamples <= link->max_samples)) {
215         return ff_filter_samples_framed(link, samplesref);
216     }
217     /* Handle framing (min_samples, max_samples) */
218     while (insamples) {
219         if (!pbuf) {
220             AVRational samples_tb = { 1, link->sample_rate };
221             int perms = link->dstpad->min_perms | AV_PERM_WRITE;
222             pbuf = ff_get_audio_buffer(link, perms, link->partial_buf_size);
223             if (!pbuf) {
224                 av_log(link->dst, AV_LOG_WARNING,
225                        "Samples dropped due to memory allocation failure.\n");
226                 return 0;
227             }
228             avfilter_copy_buffer_ref_props(pbuf, samplesref);
229             pbuf->pts = samplesref->pts +
230                         av_rescale_q(inpos, samples_tb, link->time_base);
231             pbuf->audio->nb_samples = 0;
232         }
233         nb_samples = FFMIN(insamples,
234                            link->partial_buf_size - pbuf->audio->nb_samples);
235         av_samples_copy(pbuf->extended_data, samplesref->extended_data,
236                         pbuf->audio->nb_samples, inpos,
237                         nb_samples, nb_channels, link->format);
238         inpos                   += nb_samples;
239         insamples               -= nb_samples;
240         pbuf->audio->nb_samples += nb_samples;
241         if (pbuf->audio->nb_samples >= link->min_samples) {
242             ret = ff_filter_samples_framed(link, pbuf);
243             pbuf = NULL;
244         }
245     }
246     avfilter_unref_buffer(samplesref);
247     link->partial_buf = pbuf;
248     return ret;
249 }