Debian Codegraph @

rate-lav/rate_lavrate.c

raw symbols SCIP
1 /*
2 * Rate converter plugin using libswresample
3 * Copyright (c) 2014 by Anton Khirnov
4 *
5 * This library 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 * This library 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
16 #include <stdio.h>
17 #include <alsa/asoundlib.h>
18 #include <alsa/pcm_rate.h>
19
20 #include <libswresample/swresample.h>
21 #include <libavcodec/avcodec.h>
22 #include <libavutil/avutil.h>
23 #include <libavutil/channel_layout.h>
24 #include <libavutil/opt.h>
25 #include <libavutil/mathematics.h>
26 #include <libavutil/samplefmt.h>
27
28 /* some compatibility wrappers */
29 #ifndef AV_VERSION_INT
30 #define AV_VERSION_INT(a, b, c) (((a) << 16) | ((b) << 8) | (c))
31 #endif
32 #ifndef LIBAVUTIL_VERSION_INT
33 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
34 LIBAVUTIL_VERSION_MINOR, \
35 LIBAVUTIL_VERSION_MICRO)
36 #endif
37
38 #define HAVE_CH_LAYOUT (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
39
40 static unsigned int filter_size = 16;
41
42 struct rate_src {
43 SwrContext *avr;
44
45 unsigned int in_rate;
46 unsigned int out_rate;
47 unsigned int channels;
48
49 unsigned int version;
50 };
51
52 static snd_pcm_uframes_t input_frames(void *obj ATTRIBUTE_UNUSED,
53 snd_pcm_uframes_t frames)
54 {
55 return frames;
56 }
57
58 static snd_pcm_uframes_t output_frames(void *obj ATTRIBUTE_UNUSED,
59 snd_pcm_uframes_t frames)
60 {
61 return frames;
62 }
63
64 static void pcm_src_free(void *obj)
65 {
66 struct rate_src *rate = obj;
67 swr_free(&rate->avr);
68 }
69
70 static int to_av_format(snd_pcm_format_t f)
71 {
72 switch (f) {
73 case SND_PCM_FORMAT_FLOAT:
74 return AV_SAMPLE_FMT_FLT;
75 case SND_PCM_FORMAT_U8:
76 return AV_SAMPLE_FMT_U8;
77 case SND_PCM_FORMAT_S16:
78 return AV_SAMPLE_FMT_S16;
79 case SND_PCM_FORMAT_S32:
80 default:
81 return AV_SAMPLE_FMT_S32;
82 }
83 }
84
85 static int support_multi_format(struct rate_src *rate)
86 {
87 #if SND_PCM_RATE_PLUGIN_VERSION >= 0x010003
88 return rate->version >= 0x010003;
89 #else
90 return 0;
91 #endif
92 }
93
94 static int pcm_src_init(void *obj, snd_pcm_rate_info_t *info)
95 {
96 struct rate_src *rate = obj;
97 int fmt;
98
99 if (!rate->avr || rate->channels != info->channels) {
100 int ret;
101
102 pcm_src_free(rate);
103 rate->channels = info->channels;
104 rate->in_rate = info->in.rate;
105 rate->out_rate = info->out.rate;
106
107 rate->avr = swr_alloc();
108 if (!rate->avr)
109 return -ENOMEM;
110
111 #if HAVE_CH_LAYOUT
112 AVChannelLayout layout;
113 av_channel_layout_default(&layout, rate->channels);
114 av_opt_set_chlayout(rate->avr, "in_chlayout", &layout, 0);
115 av_opt_set_chlayout(rate->avr, "out_chlayout", &layout, 0);
116 #else
117 av_opt_set_channel_layout(rate->avr, "in_channel_layout",
118 av_get_default_channel_layout(rate->channels), 0);
119 av_opt_set_channel_layout(rate->avr, "out_channel_layout",
120 av_get_default_channel_layout(rate->channels), 0);
121 #endif
122 av_opt_set_int(rate->avr, "in_sample_rate", rate->in_rate, 0);
123 av_opt_set_int(rate->avr, "out_sample_rate", rate->out_rate, 0);
124 fmt = support_multi_format(rate) ? info->in.format : SND_PCM_FORMAT_S16;
125 av_opt_set_sample_fmt(rate->avr, "in_sample_fmt",
126 to_av_format(fmt), 0);
127 fmt = support_multi_format(rate) ? info->out.format : SND_PCM_FORMAT_S16;
128 av_opt_set_sample_fmt(rate->avr, "out_sample_fmt",
129 to_av_format(fmt), 0);
130
131 ret = swr_init(rate->avr);
132 if (ret < 0) {
133 SNDERR("sw_init() error %d\n", ret);
134 swr_free(&rate->avr);
135 return -EINVAL;
136 }
137 }
138
139 return 0;
140 }
141
142 static int pcm_src_adjust_pitch(void *obj, snd_pcm_rate_info_t *info)
143 {
144 struct rate_src *rate = obj;
145
146 if (info->out.rate != rate->out_rate || info->in.rate != rate->in_rate)
147 pcm_src_init(obj, info);
148 return 0;
149 }
150
151 static void pcm_src_reset(void *obj)
152 {
153 struct rate_src *rate = obj;
154
155 if (rate->avr) {
156 #if 0
157 swr_free(rate->avr);
158 swr_init(rate->avr);
159 #endif
160 }
161 }
162
163 static void do_convert(struct rate_src *rate,
164 void *dst, unsigned int dst_frames,
165 const void *src, unsigned int src_frames)
166 {
167 unsigned int total_in = swr_get_delay(rate->avr, rate->in_rate) + src_frames;
168
169 swr_convert(rate->avr, (uint8_t **)&dst, dst_frames,
170 (const uint8_t **)&src, src_frames);
171
172 swr_set_compensation(rate->avr,
173 total_in - src_frames > filter_size ? 0 : 1,
174 src_frames);
175 }
176
177 #if SND_PCM_RATE_PLUGIN_VERSION >= 0x010003
178 static void pcm_src_convert(void *obj,
179 const snd_pcm_channel_area_t *dst_areas,
180 snd_pcm_uframes_t dst_offset,
181 unsigned int dst_frames,
182 const snd_pcm_channel_area_t *src_areas,
183 snd_pcm_uframes_t src_offset,
184 unsigned int src_frames)
185 {
186 struct rate_src *rate = obj;
187 const void *src = snd_pcm_channel_area_addr(src_areas, src_offset);
188 void *dst = snd_pcm_channel_area_addr(dst_areas, dst_offset);
189
190 do_convert(rate, dst, dst_frames, src, src_frames);
191 }
192 #endif
193
194 static void pcm_src_convert_s16(void *obj, int16_t *dst,
195 unsigned int dst_frames,
196 const int16_t *src,
197 unsigned int src_frames)
198 {
199 struct rate_src *rate = obj;
200
201 do_convert(rate, dst, dst_frames, src, src_frames);
202 }
203
204 static void pcm_src_close(void *obj)
205 {
206 pcm_src_free(obj);
207 }
208
209 #if SND_PCM_RATE_PLUGIN_VERSION >= 0x010002
210 static int get_supported_rates(void *obj ATTRIBUTE_UNUSED,
211 unsigned int *rate_min,
212 unsigned int *rate_max)
213 {
214 *rate_min = *rate_max = 0; /* both unlimited */
215 return 0;
216 }
217
218 static void dump(void *obj ATTRIBUTE_UNUSED, snd_output_t *out)
219 {
220 snd_output_printf(out, "Converter: libswresample\n");
221 }
222 #endif
223
224 #if SND_PCM_RATE_PLUGIN_VERSION >= 0x010003
225 static int get_supported_formats(void *obj, uint64_t *in_formats,
226 uint64_t *out_formats,
227 unsigned int *flags)
228 {
229 *in_formats = *out_formats =
230 (1ULL << SND_PCM_FORMAT_U8) |
231 (1ULL << SND_PCM_FORMAT_S16) |
232 (1ULL << SND_PCM_FORMAT_S32);
233 *flags = SND_PCM_RATE_FLAG_INTERLEAVED;
234 return 0;
235 }
236 #endif
237
238 static snd_pcm_rate_ops_t pcm_src_ops = {
239 .close = pcm_src_close,
240 .init = pcm_src_init,
241 .free = pcm_src_free,
242 .reset = pcm_src_reset,
243 .adjust_pitch = pcm_src_adjust_pitch,
244 #if SND_PCM_RATE_PLUGIN_VERSION >= 0x010003
245 .convert = pcm_src_convert,
246 #endif
247 .convert_s16 = pcm_src_convert_s16,
248 .input_frames = input_frames,
249 .output_frames = output_frames,
250 #if SND_PCM_RATE_PLUGIN_VERSION >= 0x010002
251 .version = SND_PCM_RATE_PLUGIN_VERSION,
252 .get_supported_rates = get_supported_rates,
253 .dump = dump,
254 #endif
255 #if SND_PCM_RATE_PLUGIN_VERSION >= 0x010003
256 .get_supported_formats = get_supported_formats,
257 #endif
258 };
259
260 int pcm_src_open(unsigned int version, void **objp, snd_pcm_rate_ops_t *ops)
261 {
262 struct rate_src *rate;
263
264 rate = calloc(1, sizeof(*rate));
265 if (!rate)
266 return -ENOMEM;
267
268 *objp = rate;
269 rate->avr = NULL;
270 rate->version = version;
271 #if SND_PCM_RATE_PLUGIN_VERSION >= 0x010002
272 if (version == 0x010001) {
273 memcpy(ops, &pcm_src_ops, sizeof(snd_pcm_rate_old_ops_t));
274 return 0;
275 }
276 #endif
277 #if SND_PCM_RATE_PLUGIN_VERSION >= 0x010003
278 if (version == 0x010002) {
279 memcpy(ops, &pcm_src_ops, sizeof(snd_pcm_rate_v2_ops_t));
280 return 0;
281 }
282 #endif
283 *ops = pcm_src_ops;
284 return 0;
285 }
286
287 int SND_PCM_RATE_PLUGIN_ENTRY(lavrate)(unsigned int version, void **objp,
288 snd_pcm_rate_ops_t *ops)
289 {
290 return pcm_src_open(version, objp, ops);
291 }
292 int SND_PCM_RATE_PLUGIN_ENTRY(lavrate_higher)(unsigned int version,
293 void **objp, snd_pcm_rate_ops_t *ops)
294 {
295 filter_size = 64;
296 return pcm_src_open(version, objp, ops);
297 }
298 int SND_PCM_RATE_PLUGIN_ENTRY(lavrate_high)(unsigned int version,
299 void **objp, snd_pcm_rate_ops_t *ops)
300 {
301 filter_size = 32;
302 return pcm_src_open(version, objp, ops);
303 }
304 int SND_PCM_RATE_PLUGIN_ENTRY(lavrate_fast)(unsigned int version,
305 void **objp, snd_pcm_rate_ops_t *ops)
306 {
307 filter_size = 8;
308 return pcm_src_open(version, objp, ops);
309 }
310 int SND_PCM_RATE_PLUGIN_ENTRY(lavrate_faster)(unsigned int version,
311 void **objp, snd_pcm_rate_ops_t *ops)
312 {
313 filter_size = 4;
314 return pcm_src_open(version, objp, ops);
315 }
316
317