ac3dec: validate channel output mode against channel count
[libav.git] / libavfilter / af_resample.c
CommitLineData
d371e7b9
AK
1/*
2 *
3 * This file is part of Libav.
4 *
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.
9 *
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.
14 *
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
18 */
19
20/**
21 * @file
22 * sample format and channel layout conversion audio filter
23 */
24
25#include "libavutil/avassert.h"
26#include "libavutil/avstring.h"
1d9c2dc8 27#include "libavutil/common.h"
d371e7b9
AK
28#include "libavutil/mathematics.h"
29#include "libavutil/opt.h"
30
31#include "libavresample/avresample.h"
32
33#include "audio.h"
34#include "avfilter.h"
ff1f51a8 35#include "formats.h"
d371e7b9
AK
36#include "internal.h"
37
38typedef struct ResampleContext {
39 AVAudioResampleContext *avr;
40
41 int64_t next_pts;
1ffb6456 42
565e4993 43 /* set by filter_frame() to signal an output frame to request_frame() */
1ffb6456 44 int got_output;
d371e7b9
AK
45} ResampleContext;
46
47static av_cold void uninit(AVFilterContext *ctx)
48{
49 ResampleContext *s = ctx->priv;
50
51 if (s->avr) {
52 avresample_close(s->avr);
53 avresample_free(&s->avr);
54 }
55}
56
57static int query_formats(AVFilterContext *ctx)
58{
59 AVFilterLink *inlink = ctx->inputs[0];
60 AVFilterLink *outlink = ctx->outputs[0];
61
b74a1da4
AK
62 AVFilterFormats *in_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
63 AVFilterFormats *out_formats = ff_all_formats(AVMEDIA_TYPE_AUDIO);
ff1f51a8
AK
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();
d371e7b9 68
b74a1da4
AK
69 ff_formats_ref(in_formats, &inlink->out_formats);
70 ff_formats_ref(out_formats, &outlink->in_formats);
d371e7b9 71
b74a1da4
AK
72 ff_formats_ref(in_samplerates, &inlink->out_samplerates);
73 ff_formats_ref(out_samplerates, &outlink->in_samplerates);
ff1f51a8
AK
74
75 ff_channel_layouts_ref(in_layouts, &inlink->out_channel_layouts);
76 ff_channel_layouts_ref(out_layouts, &outlink->in_channel_layouts);
77
d371e7b9
AK
78 return 0;
79}
80
81static int config_output(AVFilterLink *outlink)
82{
83 AVFilterContext *ctx = outlink->src;
84 AVFilterLink *inlink = ctx->inputs[0];
85 ResampleContext *s = ctx->priv;
86 char buf1[64], buf2[64];
87 int ret;
88
89 if (s->avr) {
90 avresample_close(s->avr);
91 avresample_free(&s->avr);
92 }
93
94 if (inlink->channel_layout == outlink->channel_layout &&
95 inlink->sample_rate == outlink->sample_rate &&
7b556be6
JR
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))))
d371e7b9
AK
101 return 0;
102
103 if (!(s->avr = avresample_alloc_context()))
104 return AVERROR(ENOMEM);
105
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);
112
d371e7b9
AK
113 if ((ret = avresample_open(s->avr)) < 0)
114 return ret;
115
116 outlink->time_base = (AVRational){ 1, outlink->sample_rate };
117 s->next_pts = AV_NOPTS_VALUE;
118
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,
e0d8427d 124 "fmt:%s srate:%d cl:%s -> fmt:%s srate:%d cl:%s\n",
d371e7b9
AK
125 av_get_sample_fmt_name(inlink ->format), inlink ->sample_rate, buf1,
126 av_get_sample_fmt_name(outlink->format), outlink->sample_rate, buf2);
127
128 return 0;
129}
130
131static int request_frame(AVFilterLink *outlink)
132{
133 AVFilterContext *ctx = outlink->src;
134 ResampleContext *s = ctx->priv;
1ffb6456
AK
135 int ret = 0;
136
137 s->got_output = 0;
138 while (ret >= 0 && !s->got_output)
139 ret = ff_request_frame(ctx->inputs[0]);
d371e7b9
AK
140
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,
147 AV_ROUND_UP);
148
149 if (!nb_samples)
150 return ret;
151
152 buf = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
153 if (!buf)
154 return AVERROR(ENOMEM);
155
e7ba5b1d 156 ret = avresample_convert(s->avr, buf->extended_data,
d371e7b9
AK
157 buf->linesize[0], nb_samples,
158 NULL, 0, 0);
159 if (ret <= 0) {
160 avfilter_unref_buffer(buf);
161 return (ret == 0) ? AVERROR_EOF : ret;
162 }
163
164 buf->pts = s->next_pts;
565e4993 165 return ff_filter_frame(outlink, buf);
d371e7b9
AK
166 }
167 return ret;
168}
169
565e4993 170static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
d371e7b9
AK
171{
172 AVFilterContext *ctx = inlink->dst;
173 ResampleContext *s = ctx->priv;
174 AVFilterLink *outlink = ctx->outputs[0];
cd991462 175 int ret;
d371e7b9
AK
176
177 if (s->avr) {
178 AVFilterBufferRef *buf_out;
cd991462 179 int delay, nb_samples;
d371e7b9
AK
180
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,
185 AV_ROUND_UP);
186
187 buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_samples);
cd991462
AK
188 if (!buf_out) {
189 ret = AVERROR(ENOMEM);
190 goto fail;
191 }
192
e7ba5b1d 193 ret = avresample_convert(s->avr, buf_out->extended_data,
d371e7b9 194 buf_out->linesize[0], nb_samples,
e7ba5b1d 195 buf->extended_data, buf->linesize[0],
d371e7b9 196 buf->audio->nb_samples);
ac9a8956 197 if (ret <= 0) {
cd991462 198 avfilter_unref_buffer(buf_out);
ac9a8956
JG
199 if (ret < 0)
200 goto fail;
cd991462 201 }
d371e7b9
AK
202
203 av_assert0(!avresample_available(s->avr));
204
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, "
208 "assuming 0.\n");
209 s->next_pts = 0;
210 } else
211 s->next_pts = av_rescale_q(buf->pts, inlink->time_base,
212 outlink->time_base);
213 }
214
215 if (ret > 0) {
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);
222 } else
223 buf_out->pts = s->next_pts;
224
225 s->next_pts = buf_out->pts + buf_out->audio->nb_samples;
226
565e4993 227 ret = ff_filter_frame(outlink, buf_out);
1ffb6456 228 s->got_output = 1;
d371e7b9 229 }
cd991462
AK
230
231fail:
d371e7b9 232 avfilter_unref_buffer(buf);
1ffb6456 233 } else {
7b556be6 234 buf->format = outlink->format;
565e4993 235 ret = ff_filter_frame(outlink, buf);
1ffb6456
AK
236 s->got_output = 1;
237 }
cd991462
AK
238
239 return ret;
d371e7b9
AK
240}
241
568c70e7
MR
242static const AVFilterPad avfilter_af_resample_inputs[] = {
243 {
244 .name = "default",
245 .type = AVMEDIA_TYPE_AUDIO,
565e4993 246 .filter_frame = filter_frame,
568c70e7
MR
247 .min_perms = AV_PERM_READ
248 },
249 { NULL }
250};
251
252static const AVFilterPad avfilter_af_resample_outputs[] = {
253 {
254 .name = "default",
255 .type = AVMEDIA_TYPE_AUDIO,
256 .config_props = config_output,
257 .request_frame = request_frame
258 },
259 { NULL }
260};
261
d371e7b9
AK
262AVFilter avfilter_af_resample = {
263 .name = "resample",
264 .description = NULL_IF_CONFIG_SMALL("Audio resampling and conversion."),
265 .priv_size = sizeof(ResampleContext),
266
267 .uninit = uninit,
268 .query_formats = query_formats,
269
568c70e7
MR
270 .inputs = avfilter_af_resample_inputs,
271 .outputs = avfilter_af_resample_outputs,
d371e7b9 272};