lavfi: move audio-related functions to a separate file.
[libav.git] / libavfilter / audio.c
CommitLineData
0b45334a
AK
1/*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "libavutil/audioconvert.h"
20
21#include "audio.h"
22#include "avfilter.h"
23#include "internal.h"
24
25AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
26 int nb_samples)
27{
28 return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
29}
30
31AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
32 int nb_samples)
33{
34 AVFilterBufferRef *samplesref = NULL;
35 uint8_t **data;
36 int planar = av_sample_fmt_is_planar(link->format);
37 int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
38 int planes = planar ? nb_channels : 1;
39 int linesize;
40
41 if (!(data = av_mallocz(sizeof(*data) * planes)))
42 goto fail;
43
44 if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
45 goto fail;
46
47 samplesref = avfilter_get_audio_buffer_ref_from_arrays(data, linesize, perms,
48 nb_samples, link->format,
49 link->channel_layout);
50 if (!samplesref)
51 goto fail;
52
53 av_freep(&data);
54
55fail:
56 if (data)
57 av_freep(&data[0]);
58 av_freep(&data);
59 return samplesref;
60}
61
62AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
63 int nb_samples)
64{
65 AVFilterBufferRef *ret = NULL;
66
67 if (link->dstpad->get_audio_buffer)
68 ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
69
70 if (!ret)
71 ret = ff_default_get_audio_buffer(link, perms, nb_samples);
72
73 if (ret)
74 ret->type = AVMEDIA_TYPE_AUDIO;
75
76 return ret;
77}
78
79AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
80 int linesize,int perms,
81 int nb_samples,
82 enum AVSampleFormat sample_fmt,
83 uint64_t channel_layout)
84{
85 int planes;
86 AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
87 AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
88
89 if (!samples || !samplesref)
90 goto fail;
91
92 samplesref->buf = samples;
93 samplesref->buf->free = ff_avfilter_default_free_buffer;
94 if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
95 goto fail;
96
97 samplesref->audio->nb_samples = nb_samples;
98 samplesref->audio->channel_layout = channel_layout;
99 samplesref->audio->planar = av_sample_fmt_is_planar(sample_fmt);
100
101 planes = samplesref->audio->planar ? av_get_channel_layout_nb_channels(channel_layout) : 1;
102
103 /* make sure the buffer gets read permission or it's useless for output */
104 samplesref->perms = perms | AV_PERM_READ;
105
106 samples->refcount = 1;
107 samplesref->type = AVMEDIA_TYPE_AUDIO;
108 samplesref->format = sample_fmt;
109
110 memcpy(samples->data, data,
111 FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
112 memcpy(samplesref->data, samples->data, sizeof(samples->data));
113
114 samples->linesize[0] = samplesref->linesize[0] = linesize;
115
116 if (planes > FF_ARRAY_ELEMS(samples->data)) {
117 samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
118 planes);
119 samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
120 planes);
121
122 if (!samples->extended_data || !samplesref->extended_data)
123 goto fail;
124
125 memcpy(samples-> extended_data, data, sizeof(*data)*planes);
126 memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
127 } else {
128 samples->extended_data = samples->data;
129 samplesref->extended_data = samplesref->data;
130 }
131
132 return samplesref;
133
134fail:
135 if (samples && samples->extended_data != samples->data)
136 av_freep(&samples->extended_data);
137 if (samplesref) {
138 av_freep(&samplesref->audio);
139 if (samplesref->extended_data != samplesref->data)
140 av_freep(&samplesref->extended_data);
141 }
142 av_freep(&samplesref);
143 av_freep(&samples);
144 return NULL;
145}
146
147void ff_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
148{
149 ff_filter_samples(link->dst->outputs[0], samplesref);
150}
151
152/* FIXME: samplesref is same as link->cur_buf. Need to consider removing the redundant parameter. */
153void ff_default_filter_samples(AVFilterLink *inlink, AVFilterBufferRef *samplesref)
154{
155 AVFilterLink *outlink = NULL;
156
157 if (inlink->dst->output_count)
158 outlink = inlink->dst->outputs[0];
159
160 if (outlink) {
161 outlink->out_buf = ff_default_get_audio_buffer(inlink, AV_PERM_WRITE,
162 samplesref->audio->nb_samples);
163 outlink->out_buf->pts = samplesref->pts;
164 outlink->out_buf->audio->sample_rate = samplesref->audio->sample_rate;
165 ff_filter_samples(outlink, avfilter_ref_buffer(outlink->out_buf, ~0));
166 avfilter_unref_buffer(outlink->out_buf);
167 outlink->out_buf = NULL;
168 }
169 avfilter_unref_buffer(samplesref);
170 inlink->cur_buf = NULL;
171}
172
173void ff_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref)
174{
175 void (*filter_samples)(AVFilterLink *, AVFilterBufferRef *);
176 AVFilterPad *dst = link->dstpad;
177
178 FF_DPRINTF_START(NULL, filter_samples); ff_dlog_link(NULL, link, 1);
179
180 if (!(filter_samples = dst->filter_samples))
181 filter_samples = ff_default_filter_samples;
182
183 /* prepare to copy the samples if the buffer has insufficient permissions */
184 if ((dst->min_perms & samplesref->perms) != dst->min_perms ||
185 dst->rej_perms & samplesref->perms) {
186 int i, planar = av_sample_fmt_is_planar(samplesref->format);
187 int planes = !planar ? 1:
188 av_get_channel_layout_nb_channels(samplesref->audio->channel_layout);
189
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 link->cur_buf = ff_default_get_audio_buffer(link, dst->min_perms,
195 samplesref->audio->nb_samples);
196 link->cur_buf->pts = samplesref->pts;
197 link->cur_buf->audio->sample_rate = samplesref->audio->sample_rate;
198
199 /* Copy actual data into new samples buffer */
200 for (i = 0; i < planes; i++)
201 memcpy(link->cur_buf->extended_data[i], samplesref->extended_data[i], samplesref->linesize[0]);
202
203 avfilter_unref_buffer(samplesref);
204 } else
205 link->cur_buf = samplesref;
206
207 filter_samples(link, link->cur_buf);
208}
209