2 * ALSA input and output
3 * Copyright (c) 2007 Luca Abeni ( lucabe72 email it )
4 * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr )
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * ALSA input and output: common code
26 * @author Luca Abeni ( lucabe72 email it )
27 * @author Benoit Fouet ( benoit fouet free fr )
28 * @author Nicolas George ( nicolas george normalesup org )
31 #include <alsa/asoundlib.h>
34 #include "alsa-audio.h"
36 static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
39 case CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
40 case CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
41 case CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
42 case CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
43 case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
44 case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
45 case CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
46 case CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
47 case CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
48 case CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
49 case CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
50 case CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
51 case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
52 case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
53 case CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
54 case CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
55 case CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
56 case CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8;
57 case CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
58 case CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW;
59 default: return SND_PCM_FORMAT_UNKNOWN;
63 #define REORDER_OUT_50(NAME, TYPE) \
64 static void alsa_reorder_ ## NAME ## _out_50(const void *in_v, void *out_v, int n) \
66 const TYPE *in = in_v; \
80 #define REORDER_OUT_51(NAME, TYPE) \
81 static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \
83 const TYPE *in = in_v; \
98 #define REORDER_OUT_71(NAME, TYPE) \
99 static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \
101 const TYPE *in = in_v; \
102 TYPE * out = out_v; \
118 REORDER_OUT_50(int8, int8_t)
119 REORDER_OUT_51(int8, int8_t)
120 REORDER_OUT_71(int8, int8_t)
121 REORDER_OUT_50(int16, int16_t)
122 REORDER_OUT_51(int16, int16_t)
123 REORDER_OUT_71(int16, int16_t)
124 REORDER_OUT_50(int32, int32_t)
125 REORDER_OUT_51(int32, int32_t)
126 REORDER_OUT_71(int32, int32_t)
127 REORDER_OUT_50(f32, float)
128 REORDER_OUT_51(f32, float)
129 REORDER_OUT_71(f32, float)
131 #define REORDER_DUMMY ((void *)1)
133 static av_cold ff_reorder_func find_reorder_func(int codec_id,
138 codec_id == CODEC_ID_PCM_U8 || codec_id == CODEC_ID_PCM_S8 ||
139 codec_id == CODEC_ID_PCM_ALAW || codec_id == CODEC_ID_PCM_MULAW ?
140 layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2 ?
142 layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0 ?
143 out ? alsa_reorder_int8_out_50 : NULL :
144 layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
145 out ? alsa_reorder_int8_out_51 : NULL :
146 layout == AV_CH_LAYOUT_7POINT1 ?
147 out ? alsa_reorder_int8_out_71 : NULL :
149 codec_id == CODEC_ID_PCM_U16LE || codec_id == CODEC_ID_PCM_U16BE ||
150 codec_id == CODEC_ID_PCM_S16LE || codec_id == CODEC_ID_PCM_S16BE ?
151 layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2 ?
153 layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0 ?
154 out ? alsa_reorder_int16_out_50 : NULL :
155 layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
156 out ? alsa_reorder_int16_out_51 : NULL :
157 layout == AV_CH_LAYOUT_7POINT1 ?
158 out ? alsa_reorder_int16_out_71 : NULL :
160 codec_id == CODEC_ID_PCM_U32LE || codec_id == CODEC_ID_PCM_U32BE ||
161 codec_id == CODEC_ID_PCM_S32LE || codec_id == CODEC_ID_PCM_S32BE ?
162 layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2 ?
164 layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0 ?
165 out ? alsa_reorder_int32_out_50 : NULL :
166 layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
167 out ? alsa_reorder_int32_out_51 : NULL :
168 layout == AV_CH_LAYOUT_7POINT1 ?
169 out ? alsa_reorder_int32_out_71 : NULL :
171 codec_id == CODEC_ID_PCM_F32LE || codec_id == CODEC_ID_PCM_F32BE ?
172 layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2 ?
174 layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0 ?
175 out ? alsa_reorder_f32_out_50 : NULL :
176 layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
177 out ? alsa_reorder_f32_out_51 : NULL :
178 layout == AV_CH_LAYOUT_7POINT1 ?
179 out ? alsa_reorder_f32_out_71 : NULL :
184 av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
185 unsigned int *sample_rate,
186 int channels, enum CodecID *codec_id)
188 AlsaData *s = ctx->priv_data;
189 const char *audio_device;
191 snd_pcm_format_t format;
193 snd_pcm_hw_params_t *hw_params;
194 snd_pcm_uframes_t buffer_size, period_size;
195 int64_t layout = ctx->streams[0]->codec->channel_layout;
197 if (ctx->filename[0] == 0) audio_device = "default";
198 else audio_device = ctx->filename;
200 if (*codec_id == CODEC_ID_NONE)
201 *codec_id = DEFAULT_CODEC_ID;
202 format = codec_id_to_pcm_format(*codec_id);
203 if (format == SND_PCM_FORMAT_UNKNOWN) {
204 av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
205 return AVERROR(ENOSYS);
207 s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
209 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
210 flags = SND_PCM_NONBLOCK;
212 res = snd_pcm_open(&h, audio_device, mode, flags);
214 av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
215 audio_device, snd_strerror(res));
219 res = snd_pcm_hw_params_malloc(&hw_params);
221 av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
226 res = snd_pcm_hw_params_any(h, hw_params);
228 av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
233 res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
235 av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
240 res = snd_pcm_hw_params_set_format(h, hw_params, format);
242 av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
243 *codec_id, format, snd_strerror(res));
247 res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
249 av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
254 res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
256 av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
257 channels, snd_strerror(res));
261 snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
262 /* TODO: maybe use ctx->max_picture_buffer somehow */
263 res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
265 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
270 snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
271 res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
273 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
277 s->period_size = period_size;
279 res = snd_pcm_hw_params(h, hw_params);
281 av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
286 snd_pcm_hw_params_free(hw_params);
288 if (channels > 2 && layout) {
289 s->reorder_func = find_reorder_func(*codec_id, layout,
290 mode == SND_PCM_STREAM_PLAYBACK);
291 if (s->reorder_func == REORDER_DUMMY) {
292 s->reorder_func = NULL;
293 } else if (s->reorder_func) {
294 s->reorder_buf_size = buffer_size;
295 s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
300 av_get_channel_layout_string(name, sizeof(name), channels, layout);
301 av_log(ctx, AV_LOG_WARNING,
302 "ALSA channel layout unknown or unimplemented for %s %s.\n",
304 mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
312 snd_pcm_hw_params_free(hw_params);
318 av_cold int ff_alsa_close(AVFormatContext *s1)
320 AlsaData *s = s1->priv_data;
322 av_freep(&s->reorder_buf);
327 int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
329 AlsaData *s = s1->priv_data;
330 snd_pcm_t *handle = s->h;
332 av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
334 err = snd_pcm_prepare(handle);
336 av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
340 } else if (err == -ESTRPIPE) {
341 av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
348 int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
350 int size = s->reorder_buf_size;
353 while (size < min_size)
355 r = av_realloc(s->reorder_buf, size * s->frame_size);
357 return AVERROR(ENOMEM);
359 s->reorder_buf_size = size;