3 * This file is part of Libav.
5 * Libav is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * Libav is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with Libav; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * sample format and channel layout conversion audio filter
25 #include "libavutil/avassert.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/common.h"
28 #include "libavutil/mathematics.h"
29 #include "libavutil/opt.h"
31 #include "libavresample/avresample.h"
38 typedef struct ResampleContext
{
39 AVAudioResampleContext
*avr
;
43 /* set by filter_frame() to signal an output frame to request_frame() */
47 static av_cold
void uninit(AVFilterContext
*ctx
)
49 ResampleContext
*s
= ctx
->priv
;
52 avresample_close(s
->avr
);
53 avresample_free(&s
->avr
);
57 static int query_formats(AVFilterContext
*ctx
)
59 AVFilterLink
*inlink
= ctx
->inputs
[0];
60 AVFilterLink
*outlink
= ctx
->outputs
[0];
62 AVFilterFormats
*in_formats
= ff_all_formats(AVMEDIA_TYPE_AUDIO
);
63 AVFilterFormats
*out_formats
= ff_all_formats(AVMEDIA_TYPE_AUDIO
);
64 AVFilterFormats
*in_samplerates
= ff_all_samplerates();
65 AVFilterFormats
*out_samplerates
= ff_all_samplerates();
66 AVFilterChannelLayouts
*in_layouts
= ff_all_channel_layouts();
67 AVFilterChannelLayouts
*out_layouts
= ff_all_channel_layouts();
69 ff_formats_ref(in_formats
, &inlink
->out_formats
);
70 ff_formats_ref(out_formats
, &outlink
->in_formats
);
72 ff_formats_ref(in_samplerates
, &inlink
->out_samplerates
);
73 ff_formats_ref(out_samplerates
, &outlink
->in_samplerates
);
75 ff_channel_layouts_ref(in_layouts
, &inlink
->out_channel_layouts
);
76 ff_channel_layouts_ref(out_layouts
, &outlink
->in_channel_layouts
);
81 static int config_output(AVFilterLink
*outlink
)
83 AVFilterContext
*ctx
= outlink
->src
;
84 AVFilterLink
*inlink
= ctx
->inputs
[0];
85 ResampleContext
*s
= ctx
->priv
;
86 char buf1
[64], buf2
[64];
90 avresample_close(s
->avr
);
91 avresample_free(&s
->avr
);
94 if (inlink
->channel_layout
== outlink
->channel_layout
&&
95 inlink
->sample_rate
== outlink
->sample_rate
&&
96 (inlink
->format
== outlink
->format
||
97 (av_get_channel_layout_nb_channels(inlink
->channel_layout
) == 1 &&
98 av_get_channel_layout_nb_channels(outlink
->channel_layout
) == 1 &&
99 av_get_planar_sample_fmt(inlink
->format
) ==
100 av_get_planar_sample_fmt(outlink
->format
))))
103 if (!(s
->avr
= avresample_alloc_context()))
104 return AVERROR(ENOMEM
);
106 av_opt_set_int(s
->avr
, "in_channel_layout", inlink
->channel_layout
, 0);
107 av_opt_set_int(s
->avr
, "out_channel_layout", outlink
->channel_layout
, 0);
108 av_opt_set_int(s
->avr
, "in_sample_fmt", inlink
->format
, 0);
109 av_opt_set_int(s
->avr
, "out_sample_fmt", outlink
->format
, 0);
110 av_opt_set_int(s
->avr
, "in_sample_rate", inlink
->sample_rate
, 0);
111 av_opt_set_int(s
->avr
, "out_sample_rate", outlink
->sample_rate
, 0);
113 if ((ret
= avresample_open(s
->avr
)) < 0)
116 outlink
->time_base
= (AVRational
){ 1, outlink
->sample_rate
};
117 s
->next_pts
= AV_NOPTS_VALUE
;
119 av_get_channel_layout_string(buf1
, sizeof(buf1
),
120 -1, inlink
->channel_layout
);
121 av_get_channel_layout_string(buf2
, sizeof(buf2
),
122 -1, outlink
->channel_layout
);
123 av_log(ctx
, AV_LOG_VERBOSE
,
124 "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
125 av_get_sample_fmt_name(inlink
->format
), inlink
->sample_rate
, buf1
,
126 av_get_sample_fmt_name(outlink
->format
), outlink
->sample_rate
, buf2
);
131 static int request_frame(AVFilterLink
*outlink
)
133 AVFilterContext
*ctx
= outlink
->src
;
134 ResampleContext
*s
= ctx
->priv
;
138 while (ret
>= 0 && !s
->got_output
)
139 ret
= ff_request_frame(ctx
->inputs
[0]);
141 /* flush the lavr delay buffer */
142 if (ret
== AVERROR_EOF
&& s
->avr
) {
143 AVFilterBufferRef
*buf
;
144 int nb_samples
= av_rescale_rnd(avresample_get_delay(s
->avr
),
145 outlink
->sample_rate
,
146 ctx
->inputs
[0]->sample_rate
,
152 buf
= ff_get_audio_buffer(outlink
, AV_PERM_WRITE
, nb_samples
);
154 return AVERROR(ENOMEM
);
156 ret
= avresample_convert(s
->avr
, buf
->extended_data
,
157 buf
->linesize
[0], nb_samples
,
160 avfilter_unref_buffer(buf
);
161 return (ret
== 0) ? AVERROR_EOF
: ret
;
164 buf
->pts
= s
->next_pts
;
165 return ff_filter_frame(outlink
, buf
);
170 static int filter_frame(AVFilterLink
*inlink
, AVFilterBufferRef
*buf
)
172 AVFilterContext
*ctx
= inlink
->dst
;
173 ResampleContext
*s
= ctx
->priv
;
174 AVFilterLink
*outlink
= ctx
->outputs
[0];
178 AVFilterBufferRef
*buf_out
;
179 int delay
, nb_samples
;
181 /* maximum possible samples lavr can output */
182 delay
= avresample_get_delay(s
->avr
);
183 nb_samples
= av_rescale_rnd(buf
->audio
->nb_samples
+ delay
,
184 outlink
->sample_rate
, inlink
->sample_rate
,
187 buf_out
= ff_get_audio_buffer(outlink
, AV_PERM_WRITE
, nb_samples
);
189 ret
= AVERROR(ENOMEM
);
193 ret
= avresample_convert(s
->avr
, buf_out
->extended_data
,
194 buf_out
->linesize
[0], nb_samples
,
195 buf
->extended_data
, buf
->linesize
[0],
196 buf
->audio
->nb_samples
);
198 avfilter_unref_buffer(buf_out
);
203 av_assert0(!avresample_available(s
->avr
));
205 if (s
->next_pts
== AV_NOPTS_VALUE
) {
206 if (buf
->pts
== AV_NOPTS_VALUE
) {
207 av_log(ctx
, AV_LOG_WARNING
, "First timestamp is missing, "
211 s
->next_pts
= av_rescale_q(buf
->pts
, inlink
->time_base
,
216 buf_out
->audio
->nb_samples
= ret
;
217 if (buf
->pts
!= AV_NOPTS_VALUE
) {
218 buf_out
->pts
= av_rescale_q(buf
->pts
, inlink
->time_base
,
219 outlink
->time_base
) -
220 av_rescale(delay
, outlink
->sample_rate
,
221 inlink
->sample_rate
);
223 buf_out
->pts
= s
->next_pts
;
225 s
->next_pts
= buf_out
->pts
+ buf_out
->audio
->nb_samples
;
227 ret
= ff_filter_frame(outlink
, buf_out
);
232 avfilter_unref_buffer(buf
);
234 buf
->format
= outlink
->format
;
235 ret
= ff_filter_frame(outlink
, buf
);
242 static const AVFilterPad avfilter_af_resample_inputs
[] = {
245 .type
= AVMEDIA_TYPE_AUDIO
,
246 .filter_frame
= filter_frame
,
247 .min_perms
= AV_PERM_READ
252 static const AVFilterPad avfilter_af_resample_outputs
[] = {
255 .type
= AVMEDIA_TYPE_AUDIO
,
256 .config_props
= config_output
,
257 .request_frame
= request_frame
262 AVFilter avfilter_af_resample
= {
264 .description
= NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
265 .priv_size
= sizeof(ResampleContext
),
268 .query_formats
= query_formats
,
270 .inputs
= avfilter_af_resample_inputs
,
271 .outputs
= avfilter_af_resample_outputs
,