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