lavfi: switch to AVFrame.
[FFMpeg-mirror/mplayer-patches.git] / libavfilter / audio.c
blob5295423e44f335426236802cab67daee7c8e762a
1 /*
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"
22 #include "audio.h"
23 #include "avfilter.h"
24 #include "internal.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);
35 int buf_size, ret;
37 if (!frame)
38 return NULL;
40 buf_size = av_samples_get_buffer_size(NULL, channels, nb_samples,
41 link->format, 0);
42 if (buf_size < 0)
43 goto fail;
45 frame->buf[0] = av_buffer_alloc(buf_size);
46 if (!frame->buf[0])
47 goto fail;
49 frame->nb_samples = nb_samples;
50 ret = avcodec_fill_audio_frame(frame, channels, link->format,
51 frame->buf[0]->data, buf_size, 0);
52 if (ret < 0)
53 goto fail;
55 av_samples_set_silence(frame->extended_data, 0, nb_samples, channels,
56 link->format);
58 frame->nb_samples = nb_samples;
59 frame->format = link->format;
60 frame->channel_layout = link->channel_layout;
61 frame->sample_rate = link->sample_rate;
63 return frame;
65 fail:
66 av_buffer_unref(&frame->buf[0]);
67 av_frame_free(&frame);
68 return NULL;
71 AVFrame *ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
73 AVFrame *ret = NULL;
75 if (link->dstpad->get_audio_buffer)
76 ret = link->dstpad->get_audio_buffer(link, nb_samples);
78 if (!ret)
79 ret = ff_default_get_audio_buffer(link, nb_samples);
81 return ret;
84 #if FF_API_AVFILTERBUFFER
85 AVFilterBufferRef* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
86 int linesize,int perms,
87 int nb_samples,
88 enum AVSampleFormat sample_fmt,
89 uint64_t channel_layout)
91 int planes;
92 AVFilterBuffer *samples = av_mallocz(sizeof(*samples));
93 AVFilterBufferRef *samplesref = av_mallocz(sizeof(*samplesref));
95 if (!samples || !samplesref)
96 goto fail;
98 samplesref->buf = samples;
99 samplesref->buf->free = ff_avfilter_default_free_buffer;
100 if (!(samplesref->audio = av_mallocz(sizeof(*samplesref->audio))))
101 goto fail;
103 samplesref->audio->nb_samples = nb_samples;
104 samplesref->audio->channel_layout = channel_layout;
105 samplesref->audio->planar = av_sample_fmt_is_planar(sample_fmt);
107 planes = samplesref->audio->planar ? av_get_channel_layout_nb_channels(channel_layout) : 1;
109 /* make sure the buffer gets read permission or it's useless for output */
110 samplesref->perms = perms | AV_PERM_READ;
112 samples->refcount = 1;
113 samplesref->type = AVMEDIA_TYPE_AUDIO;
114 samplesref->format = sample_fmt;
116 memcpy(samples->data, data,
117 FFMIN(FF_ARRAY_ELEMS(samples->data), planes)*sizeof(samples->data[0]));
118 memcpy(samplesref->data, samples->data, sizeof(samples->data));
120 samples->linesize[0] = samplesref->linesize[0] = linesize;
122 if (planes > FF_ARRAY_ELEMS(samples->data)) {
123 samples-> extended_data = av_mallocz(sizeof(*samples->extended_data) *
124 planes);
125 samplesref->extended_data = av_mallocz(sizeof(*samplesref->extended_data) *
126 planes);
128 if (!samples->extended_data || !samplesref->extended_data)
129 goto fail;
131 memcpy(samples-> extended_data, data, sizeof(*data)*planes);
132 memcpy(samplesref->extended_data, data, sizeof(*data)*planes);
133 } else {
134 samples->extended_data = samples->data;
135 samplesref->extended_data = samplesref->data;
138 samplesref->pts = AV_NOPTS_VALUE;
140 return samplesref;
142 fail:
143 if (samples && samples->extended_data != samples->data)
144 av_freep(&samples->extended_data);
145 if (samplesref) {
146 av_freep(&samplesref->audio);
147 if (samplesref->extended_data != samplesref->data)
148 av_freep(&samplesref->extended_data);
150 av_freep(&samplesref);
151 av_freep(&samples);
152 return NULL;
154 #endif