1 /*
2 * Copyright (c) Stefano Sabatini | stefasab at gmail.com
3 * Copyright (c) S.N. Hemanth Meenakshisundaram | smeenaks at ucsd.edu
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "libavutil/avassert.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/common.h"
25 #include "libavutil/cpu.h"
26
27 #include "audio.h"
28 #include "avfilter.h"
29 #include "framepool.h"
30 #include "internal.h"
31
ff_null_get_audio_buffer(AVFilterLink * link,int nb_samples)32 AVFrame *ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
33 {
34 return ff_get_audio_buffer(link->dst->outputs[0], nb_samples);
35 }
36
ff_default_get_audio_buffer(AVFilterLink * link,int nb_samples)37 AVFrame *ff_default_get_audio_buffer(AVFilterLink *link, int nb_samples)
38 {
39 AVFrame *frame = NULL;
40 int channels = link->ch_layout.nb_channels;
41 #if FF_API_OLD_CHANNEL_LAYOUT
42 FF_DISABLE_DEPRECATION_WARNINGS
43 int channel_layout_nb_channels = av_get_channel_layout_nb_channels(link->channel_layout);
44 int align = av_cpu_max_align();
45
46 av_assert0(channels == channel_layout_nb_channels || !channel_layout_nb_channels);
47 FF_ENABLE_DEPRECATION_WARNINGS
48 #endif
49
50 if (!link->frame_pool) {
51 link->frame_pool = ff_frame_pool_audio_init(av_buffer_allocz, channels,
52 nb_samples, link->format, align);
53 if (!link->frame_pool)
54 return NULL;
55 } else {
56 int pool_channels = 0;
57 int pool_nb_samples = 0;
58 int pool_align = 0;
59 enum AVSampleFormat pool_format = AV_SAMPLE_FMT_NONE;
60
61 if (ff_frame_pool_get_audio_config(link->frame_pool,
62 &pool_channels, &pool_nb_samples,
63 &pool_format, &pool_align) < 0) {
64 return NULL;
65 }
66
67 if (pool_channels != channels || pool_nb_samples < nb_samples ||
68 pool_format != link->format || pool_align != align) {
69
70 ff_frame_pool_uninit((FFFramePool **)&link->frame_pool);
71 link->frame_pool = ff_frame_pool_audio_init(av_buffer_allocz, channels,
72 nb_samples, link->format, align);
73 if (!link->frame_pool)
74 return NULL;
75 }
76 }
77
78 frame = ff_frame_pool_get(link->frame_pool);
79 if (!frame)
80 return NULL;
81
82 frame->nb_samples = nb_samples;
83 #if FF_API_OLD_CHANNEL_LAYOUT
84 FF_DISABLE_DEPRECATION_WARNINGS
85 frame->channel_layout = link->channel_layout;
86 FF_ENABLE_DEPRECATION_WARNINGS
87 #endif
88 if (link->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC &&
89 av_channel_layout_copy(&frame->ch_layout, &link->ch_layout) < 0) {
90 av_frame_free(&frame);
91 return NULL;
92 }
93 frame->sample_rate = link->sample_rate;
94
95 av_samples_set_silence(frame->extended_data, 0, nb_samples, channels, link->format);
96
97 return frame;
98 }
99
ff_get_audio_buffer(AVFilterLink * link,int nb_samples)100 AVFrame *ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
101 {
102 AVFrame *ret = NULL;
103
104 if (link->dstpad->get_buffer.audio)
105 ret = link->dstpad->get_buffer.audio(link, nb_samples);
106
107 if (!ret)
108 ret = ff_default_get_audio_buffer(link, nb_samples);
109
110 return ret;
111 }
112