Libav
alsa-audio-common.c
Go to the documentation of this file.
1 /*
2  * ALSA input and output
3  * Copyright (c) 2007 Luca Abeni ( lucabe72 email it )
4  * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr )
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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.
12  *
13  * Libav 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.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
31 #include <alsa/asoundlib.h>
32 #include "libavformat/avformat.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/channel_layout.h"
35 
36 #include "alsa-audio.h"
37 
38 static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
39 {
40  switch(codec_id) {
41  case AV_CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
42  case AV_CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
43  case AV_CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
44  case AV_CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
45  case AV_CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
46  case AV_CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
47  case AV_CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
48  case AV_CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
49  case AV_CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
50  case AV_CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
51  case AV_CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
52  case AV_CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
53  case AV_CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
54  case AV_CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
55  case AV_CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
56  case AV_CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
57  case AV_CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
58  case AV_CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8;
59  case AV_CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
60  case AV_CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW;
61  default: return SND_PCM_FORMAT_UNKNOWN;
62  }
63 }
64 
65 #define REORDER_OUT_50(NAME, TYPE) \
66 static void alsa_reorder_ ## NAME ## _out_50(const void *in_v, void *out_v, int n) \
67 { \
68  const TYPE *in = in_v; \
69  TYPE *out = out_v; \
70 \
71  while (n-- > 0) { \
72  out[0] = in[0]; \
73  out[1] = in[1]; \
74  out[2] = in[3]; \
75  out[3] = in[4]; \
76  out[4] = in[2]; \
77  in += 5; \
78  out += 5; \
79  } \
80 }
81 
82 #define REORDER_OUT_51(NAME, TYPE) \
83 static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \
84 { \
85  const TYPE *in = in_v; \
86  TYPE *out = out_v; \
87 \
88  while (n-- > 0) { \
89  out[0] = in[0]; \
90  out[1] = in[1]; \
91  out[2] = in[4]; \
92  out[3] = in[5]; \
93  out[4] = in[2]; \
94  out[5] = in[3]; \
95  in += 6; \
96  out += 6; \
97  } \
98 }
99 
100 #define REORDER_OUT_71(NAME, TYPE) \
101 static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \
102 { \
103  const TYPE *in = in_v; \
104  TYPE *out = out_v; \
105 \
106  while (n-- > 0) { \
107  out[0] = in[0]; \
108  out[1] = in[1]; \
109  out[2] = in[4]; \
110  out[3] = in[5]; \
111  out[4] = in[2]; \
112  out[5] = in[3]; \
113  out[6] = in[6]; \
114  out[7] = in[7]; \
115  in += 8; \
116  out += 8; \
117  } \
118 }
119 
120 REORDER_OUT_50(int8, int8_t)
121 REORDER_OUT_51(int8, int8_t)
122 REORDER_OUT_71(int8, int8_t)
123 REORDER_OUT_50(int16, int16_t)
124 REORDER_OUT_51(int16, int16_t)
125 REORDER_OUT_71(int16, int16_t)
126 REORDER_OUT_50(int32, int32_t)
127 REORDER_OUT_51(int32, int32_t)
128 REORDER_OUT_71(int32, int32_t)
129 REORDER_OUT_50(f32, float)
130 REORDER_OUT_51(f32, float)
131 REORDER_OUT_71(f32, float)
132 
133 #define FORMAT_I8 0
134 #define FORMAT_I16 1
135 #define FORMAT_I32 2
136 #define FORMAT_F32 3
137 
138 #define PICK_REORDER(layout)\
139 switch(format) {\
140  case FORMAT_I8: s->reorder_func = alsa_reorder_int8_out_ ##layout; break;\
141  case FORMAT_I16: s->reorder_func = alsa_reorder_int16_out_ ##layout; break;\
142  case FORMAT_I32: s->reorder_func = alsa_reorder_int32_out_ ##layout; break;\
143  case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout; break;\
144 }
145 
146 static av_cold int find_reorder_func(AlsaData *s, int codec_id, uint64_t layout, int out)
147 {
148  int format;
149 
150  /* reordering input is not currently supported */
151  if (!out)
152  return AVERROR(ENOSYS);
153 
154  /* reordering is not needed for QUAD or 2_2 layout */
155  if (layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2)
156  return 0;
157 
158  switch (codec_id) {
159  case AV_CODEC_ID_PCM_S8:
160  case AV_CODEC_ID_PCM_U8:
162  case AV_CODEC_ID_PCM_MULAW: format = FORMAT_I8; break;
166  case AV_CODEC_ID_PCM_U16BE: format = FORMAT_I16; break;
170  case AV_CODEC_ID_PCM_U32BE: format = FORMAT_I32; break;
172  case AV_CODEC_ID_PCM_F32BE: format = FORMAT_F32; break;
173  default: return AVERROR(ENOSYS);
174  }
175 
176  if (layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0)
177  PICK_REORDER(50)
178  else if (layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1)
179  PICK_REORDER(51)
180  else if (layout == AV_CH_LAYOUT_7POINT1)
181  PICK_REORDER(71)
182 
183  return s->reorder_func ? 0 : AVERROR(ENOSYS);
184 }
185 
186 av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
187  unsigned int *sample_rate,
188  int channels, enum AVCodecID *codec_id)
189 {
190  AlsaData *s = ctx->priv_data;
191  const char *audio_device;
192  int res, flags = 0;
193  snd_pcm_format_t format;
194  snd_pcm_t *h;
195  snd_pcm_hw_params_t *hw_params;
196  snd_pcm_uframes_t buffer_size, period_size;
197  uint64_t layout = ctx->streams[0]->codec->channel_layout;
198 
199  if (ctx->filename[0] == 0) audio_device = "default";
200  else audio_device = ctx->filename;
201 
202  if (*codec_id == AV_CODEC_ID_NONE)
203  *codec_id = DEFAULT_CODEC_ID;
204  format = codec_id_to_pcm_format(*codec_id);
205  if (format == SND_PCM_FORMAT_UNKNOWN) {
206  av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
207  return AVERROR(ENOSYS);
208  }
209  s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
210 
211  if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
212  flags = SND_PCM_NONBLOCK;
213  }
214  res = snd_pcm_open(&h, audio_device, mode, flags);
215  if (res < 0) {
216  av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
217  audio_device, snd_strerror(res));
218  return AVERROR(EIO);
219  }
220 
221  res = snd_pcm_hw_params_malloc(&hw_params);
222  if (res < 0) {
223  av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
224  snd_strerror(res));
225  goto fail1;
226  }
227 
228  res = snd_pcm_hw_params_any(h, hw_params);
229  if (res < 0) {
230  av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
231  snd_strerror(res));
232  goto fail;
233  }
234 
235  res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
236  if (res < 0) {
237  av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
238  snd_strerror(res));
239  goto fail;
240  }
241 
242  res = snd_pcm_hw_params_set_format(h, hw_params, format);
243  if (res < 0) {
244  av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
245  *codec_id, format, snd_strerror(res));
246  goto fail;
247  }
248 
249  res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
250  if (res < 0) {
251  av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
252  snd_strerror(res));
253  goto fail;
254  }
255 
256  res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
257  if (res < 0) {
258  av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
259  channels, snd_strerror(res));
260  goto fail;
261  }
262 
263  snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
264  buffer_size = FFMIN(buffer_size, ALSA_BUFFER_SIZE_MAX);
265  /* TODO: maybe use ctx->max_picture_buffer somehow */
266  res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
267  if (res < 0) {
268  av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
269  snd_strerror(res));
270  goto fail;
271  }
272 
273  snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
274  if (!period_size)
275  period_size = buffer_size / 4;
276  res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
277  if (res < 0) {
278  av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
279  snd_strerror(res));
280  goto fail;
281  }
282  s->period_size = period_size;
283 
284  res = snd_pcm_hw_params(h, hw_params);
285  if (res < 0) {
286  av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
287  snd_strerror(res));
288  goto fail;
289  }
290 
291  snd_pcm_hw_params_free(hw_params);
292 
293  if (channels > 2 && layout) {
294  if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) {
295  char name[128];
296  av_get_channel_layout_string(name, sizeof(name), channels, layout);
297  av_log(ctx, AV_LOG_WARNING, "ALSA channel layout unknown or unimplemented for %s %s.\n",
298  name, mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
299  }
300  if (s->reorder_func) {
301  s->reorder_buf_size = buffer_size;
303  if (!s->reorder_buf)
304  goto fail1;
305  }
306  }
307 
308  s->h = h;
309  return 0;
310 
311 fail:
312  snd_pcm_hw_params_free(hw_params);
313 fail1:
314  snd_pcm_close(h);
315  return AVERROR(EIO);
316 }
317 
319 {
320  AlsaData *s = s1->priv_data;
321 
322  av_freep(&s->reorder_buf);
323  snd_pcm_close(s->h);
324  return 0;
325 }
326 
328 {
329  AlsaData *s = s1->priv_data;
330  snd_pcm_t *handle = s->h;
331 
332  av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
333  if (err == -EPIPE) {
334  err = snd_pcm_prepare(handle);
335  if (err < 0) {
336  av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
337 
338  return AVERROR(EIO);
339  }
340  } else if (err == -ESTRPIPE) {
341  av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
342 
343  return -1;
344  }
345  return err;
346 }
347 
349 {
350  int size = s->reorder_buf_size;
351  void *r;
352 
353  av_assert0(size != 0);
354  while (size < min_size)
355  size *= 2;
356  r = av_realloc(s->reorder_buf, size * s->frame_size);
357  if (!r)
358  return AVERROR(ENOMEM);
359  s->reorder_buf = r;
360  s->reorder_buf_size = size;
361  return 0;
362 }
#define AVFMT_FLAG_NONBLOCK
Do not block when reading packets from input.
Definition: avformat.h:1036
#define AV_CH_LAYOUT_7POINT1
int size
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
#define AV_CH_LAYOUT_5POINT0
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Format I/O context.
Definition: avformat.h:922
const char * name
av_cold int ff_alsa_close(AVFormatContext *s1)
Close the ALSA PCM.
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1033
static int flags
Definition: log.c:44
int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
#define FORMAT_I8
#define r
Definition: input.c:51
#define AV_CH_LAYOUT_5POINT1
#define FORMAT_I32
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2043
void * priv_data
Format private data.
Definition: avformat.h:950
char filename[1024]
input or output filename
Definition: avformat.h:998
#define AVERROR(e)
Definition: error.h:43
#define AV_CH_LAYOUT_QUAD
enum AVCodecID codec_id
Definition: mov_chan.c:432
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1852
#define AV_CH_LAYOUT_2_2
void(* reorder_func)(const void *, void *, int)
Definition: alsa-audio.h:52
void * av_realloc(void *ptr, size_t size) 1(2)
Allocate or reallocate a block of memory.
Definition: mem.c:117
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
int32_t
static av_cold int find_reorder_func(AlsaData *s, int codec_id, uint64_t layout, int out)
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode, unsigned int *sample_rate, int channels, enum AVCodecID *codec_id)
Open an ALSA PCM.
#define AV_CH_LAYOUT_5POINT1_BACK
NULL
Definition: eval.c:55
void * reorder_buf
Definition: alsa-audio.h:53
#define FORMAT_I16
#define av_cold
Definition: attributes.h:66
#define FORMAT_F32
#define AV_CH_LAYOUT_5POINT0_BACK
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
Try to recover from ALSA buffer underrun.
#define PICK_REORDER(layout)
int period_size
bytes per sample * channels
Definition: alsa-audio.h:49
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
#define DEFAULT_CODEC_ID
Definition: alsa-audio.h:41
#define ALSA_BUFFER_SIZE_MAX
Definition: alsa-audio.h:43
snd_pcm_t * h
Definition: alsa-audio.h:47
uint64_t layout
#define REORDER_OUT_71(NAME, TYPE)
int frame_size
preferred size for reads and writes
Definition: alsa-audio.h:48
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
ALSA input and output: definitions and structures.
static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
#define REORDER_OUT_51(NAME, TYPE)
int reorder_buf_size
in frames
Definition: alsa-audio.h:54
#define REORDER_OUT_50(NAME, TYPE)
#define FFMIN(a, b)
Definition: common.h:57
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.