2 * Copyright (c) Stefano Sabatini | stefasab at gmail.com
3 * Copyright (c) S.N. Hemanth Meenakshisundaram | smeenaks at ucsd.edu
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/avassert.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/common.h"
30 int avfilter_ref_get_channels(AVFilterBufferRef *ref)
32 return ref->audio ? ref->audio->channels : 0;
35 AVFilterBufferRef *ff_null_get_audio_buffer(AVFilterLink *link, int perms,
38 return ff_get_audio_buffer(link->dst->outputs[0], perms, nb_samples);
41 AVFilterBufferRef *ff_default_get_audio_buffer(AVFilterLink *link, int perms,
44 AVFilterBufferRef *samplesref = NULL;
46 int planar = av_sample_fmt_is_planar(link->format);
47 int nb_channels = link->channels;
48 int planes = planar ? nb_channels : 1;
50 int full_perms = AV_PERM_READ | AV_PERM_WRITE | AV_PERM_PRESERVE |
51 AV_PERM_REUSE | AV_PERM_REUSE2 | AV_PERM_ALIGN;
53 av_assert1(!(perms & ~(full_perms | AV_PERM_NEG_LINESIZES)));
55 if (!(data = av_mallocz(sizeof(*data) * planes)))
58 if (av_samples_alloc(data, &linesize, nb_channels, nb_samples, link->format, 0) < 0)
61 samplesref = avfilter_get_audio_buffer_ref_from_arrays_channels(
62 data, linesize, full_perms, nb_samples, link->format,
63 link->channels, link->channel_layout);
67 samplesref->audio->sample_rate = link->sample_rate;
78 AVFilterBufferRef *ff_get_audio_buffer(AVFilterLink *link, int perms,
81 AVFilterBufferRef *ret = NULL;
83 if (link->dstpad->get_audio_buffer)
84 ret = link->dstpad->get_audio_buffer(link, perms, nb_samples);
87 ret = ff_default_get_audio_buffer(link, perms, nb_samples);
90 ret->type = AVMEDIA_TYPE_AUDIO;
95 AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays_channels(uint8_t **data,
99 enum AVSampleFormat sample_fmt,
101 uint64_t channel_layout)
104 AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
105 AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
107 if (!samples || !samplesref)
110 av_assert0(channels);
111 av_assert0(channel_layout == 0 ||
112 channels == av_get_channel_layout_nb_channels(channel_layout));
114 samplesref->buf = samples;
115 samplesref->buf->free = ff_avfilter_default_free_buffer;
116 if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
119 samplesref->audio->nb_samples = nb_samples;
120 samplesref->audio->channel_layout = channel_layout;
121 samplesref->audio->channels = channels;
123 planes = av_sample_fmt_is_planar(sample_fmt) ? channels : 1;
125 /* make sure the buffer gets read permission or it's useless for output */
126 samplesref->perms = perms | AV_PERM_READ;
128 samples->refcount = 1;
129 samplesref->type = AVMEDIA_TYPE_AUDIO;
130 samplesref->format = sample_fmt;
132 memcpy(samples->data, data,
133 FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
134 memcpy(samplesref->data, samples->data, sizeof(samples->data));
136 samples->linesize[0] = samplesref->linesize[0] = linesize;
138 if (planes > FF_ARRAY_ELEMS(samples->data)) {
139 samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
141 samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
144 if (!samples->extended_data || !samplesref->extended_data)
147 memcpy(samples-> extended_data, data, sizeof(*data)*planes);
148 memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
150 samples->extended_data = samples->data;
151 samplesref->extended_data = samplesref->data;
154 samplesref->pts = AV_NOPTS_VALUE;
159 if (samples && samples->extended_data != samples->data)
160 av_freep(&samples->extended_data);
162 av_freep(&samplesref->audio);
163 if (samplesref->extended_data != samplesref->data)
164 av_freep(&samplesref->extended_data);
166 av_freep(&samplesref);
171 AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
172 int linesize,int perms,
174 enum AVSampleFormat sample_fmt,
175 uint64_t channel_layout)
177 int channels = av_get_channel_layout_nb_channels(channel_layout);
178 return avfilter_get_audio_buffer_ref_from_arrays_channels(data, linesize, perms,
179 nb_samples, sample_fmt,
180 channels, channel_layout);