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 AVFilterBufferRef
*ff_null_get_audio_buffer(AVFilterLink
*link
, int perms
,
29 return ff_get_audio_buffer(link
->dst
->outputs
[0], perms
, nb_samples
);
32 AVFilterBufferRef
*ff_default_get_audio_buffer(AVFilterLink
*link
, int perms
,
35 AVFilterBufferRef
*samplesref
= NULL
;
37 int planar
= av_sample_fmt_is_planar(link
->format
);
38 int nb_channels
= av_get_channel_layout_nb_channels(link
->channel_layout
);
39 int planes
= planar
? nb_channels
: 1;
42 if (!(data
= av_mallocz(sizeof(*data
) * planes
)))
45 if (av_samples_alloc(data
, &linesize
, nb_channels
, nb_samples
, link
->format
, 0) < 0)
48 samplesref
= avfilter_get_audio_buffer_ref_from_arrays(data
, linesize
, perms
,
49 nb_samples
, link
->format
,
50 link
->channel_layout
);
63 AVFilterBufferRef
*ff_get_audio_buffer(AVFilterLink
*link
, int perms
,
66 AVFilterBufferRef
*ret
= NULL
;
68 if (link
->dstpad
->get_audio_buffer
)
69 ret
= link
->dstpad
->get_audio_buffer(link
, perms
, nb_samples
);
72 ret
= ff_default_get_audio_buffer(link
, perms
, nb_samples
);
75 ret
->type
= AVMEDIA_TYPE_AUDIO
;
80 AVFilterBufferRef
* avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data
,
81 int linesize
,int perms
,
83 enum AVSampleFormat sample_fmt
,
84 uint64_t channel_layout
)
87 AVFilterBuffer
*samples
= av_mallocz(sizeof(*samples
));
88 AVFilterBufferRef
*samplesref
= av_mallocz(sizeof(*samplesref
));
90 if (!samples
|| !samplesref
)
93 samplesref
->buf
= samples
;
94 samplesref
->buf
->free
= ff_avfilter_default_free_buffer
;
95 if (!(samplesref
->audio
= av_mallocz(sizeof(*samplesref
->audio
))))
98 samplesref
->audio
->nb_samples
= nb_samples
;
99 samplesref
->audio
->channel_layout
= channel_layout
;
100 samplesref
->audio
->planar
= av_sample_fmt_is_planar(sample_fmt
);
102 planes
= samplesref
->audio
->planar
? av_get_channel_layout_nb_channels(channel_layout
) : 1;
104 /* make sure the buffer gets read permission or it's useless for output */
105 samplesref
->perms
= perms
| AV_PERM_READ
;
107 samples
->refcount
= 1;
108 samplesref
->type
= AVMEDIA_TYPE_AUDIO
;
109 samplesref
->format
= sample_fmt
;
111 memcpy(samples
->data
, data
,
112 FFMIN(FF_ARRAY_ELEMS(samples
->data
), planes
)*sizeof(samples
->data
[0]));
113 memcpy(samplesref
->data
, samples
->data
, sizeof(samples
->data
));
115 samples
->linesize
[0] = samplesref
->linesize
[0] = linesize
;
117 if (planes
> FF_ARRAY_ELEMS(samples
->data
)) {
118 samples
-> extended_data
= av_mallocz(sizeof(*samples
->extended_data
) *
120 samplesref
->extended_data
= av_mallocz(sizeof(*samplesref
->extended_data
) *
123 if (!samples
->extended_data
|| !samplesref
->extended_data
)
126 memcpy(samples
-> extended_data
, data
, sizeof(*data
)*planes
);
127 memcpy(samplesref
->extended_data
, data
, sizeof(*data
)*planes
);
129 samples
->extended_data
= samples
->data
;
130 samplesref
->extended_data
= samplesref
->data
;
133 samplesref
->pts
= AV_NOPTS_VALUE
;
138 if (samples
&& samples
->extended_data
!= samples
->data
)
139 av_freep(&samples
->extended_data
);
141 av_freep(&samplesref
->audio
);
142 if (samplesref
->extended_data
!= samplesref
->data
)
143 av_freep(&samplesref
->extended_data
);
145 av_freep(&samplesref
);