2 * This file is part of Libav.
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.
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.
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
19 #include "libavutil/channel_layout.h"
20 #include "libavutil/common.h"
26 AVFrame *ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
28 return ff_get_audio_buffer(link->dst->outputs[0], nb_samples);
31 AVFrame *ff_default_get_audio_buffer(AVFilterLink *link, int nb_samples)
33 AVFrame *frame = av_frame_alloc();
34 int channels = av_get_channel_layout_nb_channels(link->channel_layout);
40 frame->nb_samples = nb_samples;
41 frame->format = link->format;
42 frame->channel_layout = link->channel_layout;
43 frame->sample_rate = link->sample_rate;
44 ret = av_frame_get_buffer(frame, 0);
46 av_frame_free(&frame);
50 av_samples_set_silence(frame->extended_data, 0, nb_samples, channels,
57 AVFrame *ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
61 if (link->dstpad->get_audio_buffer)
62 ret = link->dstpad->get_audio_buffer(link, nb_samples);
65 ret = ff_default_get_audio_buffer(link, nb_samples);
70 #if FF_API_AVFILTERBUFFER
71 AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
72 int linesize,int perms,
74 enum AVSampleFormat sample_fmt,
75 uint64_t channel_layout)
78 AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
79 AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
81 if (!samples || !samplesref)
84 samplesref->buf = samples;
85 samplesref->buf->free = ff_avfilter_default_free_buffer;
86 if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
89 samplesref->audio->nb_samples = nb_samples;
90 samplesref->audio->channel_layout = channel_layout;
91 samplesref->audio->planar = av_sample_fmt_is_planar(sample_fmt);
93 planes = samplesref->audio->planar ? av_get_channel_layout_nb_channels(channel_layout) : 1;
95 /* make sure the buffer gets read permission or it's useless for output */
96 samplesref->perms = perms | AV_PERM_READ;
98 samples->refcount = 1;
99 samplesref->type = AVMEDIA_TYPE_AUDIO;
100 samplesref->format = sample_fmt;
102 memcpy(samples->data, data,
103 FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
104 memcpy(samplesref->data, samples->data, sizeof(samples->data));
106 samples->linesize[0] = samplesref->linesize[0] = linesize;
108 if (planes > FF_ARRAY_ELEMS(samples->data)) {
109 samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
111 samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
114 if (!samples->extended_data || !samplesref->extended_data)
117 memcpy(samples-> extended_data, data, sizeof(*data)*planes);
118 memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
120 samples->extended_data = samples->data;
121 samplesref->extended_data = samplesref->data;
124 samplesref->pts = AV_NOPTS_VALUE;
129 if (samples && samples->extended_data != samples->data)
130 av_freep(&samples->extended_data);
132 av_freep(&samplesref->audio);
133 if (samplesref->extended_data != samplesref->data)
134 av_freep(&samplesref->extended_data);
136 av_freep(&samplesref);