alsa-audio-common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 FFmpeg.
  7. *
  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.
  12. *
  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.
  17. *
  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
  21. */
  22. /**
  23. * @file
  24. * ALSA input and output: common code
  25. * @author Luca Abeni ( lucabe72 email it )
  26. * @author Benoit Fouet ( benoit fouet free fr )
  27. * @author Nicolas George ( nicolas george normalesup org )
  28. */
  29. #include <alsa/asoundlib.h>
  30. #include "avdevice.h"
  31. #include "libavutil/avassert.h"
  32. #include "libavutil/channel_layout.h"
  33. #include "alsa-audio.h"
  34. static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
  35. {
  36. switch(codec_id) {
  37. case AV_CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
  38. case AV_CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
  39. case AV_CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
  40. case AV_CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
  41. case AV_CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
  42. case AV_CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
  43. case AV_CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
  44. case AV_CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
  45. case AV_CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
  46. case AV_CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
  47. case AV_CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
  48. case AV_CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
  49. case AV_CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
  50. case AV_CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
  51. case AV_CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
  52. case AV_CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
  53. case AV_CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
  54. case AV_CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8;
  55. case AV_CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
  56. case AV_CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW;
  57. default: return SND_PCM_FORMAT_UNKNOWN;
  58. }
  59. }
  60. #define MAKE_REORDER_FUNC(NAME, TYPE, CHANNELS, LAYOUT, MAP) \
  61. static void alsa_reorder_ ## NAME ## _ ## LAYOUT(const void *in_v, \
  62. void *out_v, \
  63. int n) \
  64. { \
  65. const TYPE *in = in_v; \
  66. TYPE *out = out_v; \
  67. \
  68. while (n-- > 0) { \
  69. MAP \
  70. in += CHANNELS; \
  71. out += CHANNELS; \
  72. } \
  73. }
  74. #define MAKE_REORDER_FUNCS(CHANNELS, LAYOUT, MAP) \
  75. MAKE_REORDER_FUNC(int8, int8_t, CHANNELS, LAYOUT, MAP) \
  76. MAKE_REORDER_FUNC(int16, int16_t, CHANNELS, LAYOUT, MAP) \
  77. MAKE_REORDER_FUNC(int32, int32_t, CHANNELS, LAYOUT, MAP) \
  78. MAKE_REORDER_FUNC(f32, float, CHANNELS, LAYOUT, MAP)
  79. MAKE_REORDER_FUNCS(5, out_50, \
  80. out[0] = in[0]; \
  81. out[1] = in[1]; \
  82. out[2] = in[3]; \
  83. out[3] = in[4]; \
  84. out[4] = in[2]; \
  85. );
  86. MAKE_REORDER_FUNCS(6, out_51, \
  87. out[0] = in[0]; \
  88. out[1] = in[1]; \
  89. out[2] = in[4]; \
  90. out[3] = in[5]; \
  91. out[4] = in[2]; \
  92. out[5] = in[3]; \
  93. );
  94. MAKE_REORDER_FUNCS(8, out_71, \
  95. out[0] = in[0]; \
  96. out[1] = in[1]; \
  97. out[2] = in[4]; \
  98. out[3] = in[5]; \
  99. out[4] = in[2]; \
  100. out[5] = in[3]; \
  101. out[6] = in[6]; \
  102. out[7] = in[7]; \
  103. );
  104. #define FORMAT_I8 0
  105. #define FORMAT_I16 1
  106. #define FORMAT_I32 2
  107. #define FORMAT_F32 3
  108. #define PICK_REORDER(layout)\
  109. switch(format) {\
  110. case FORMAT_I8: s->reorder_func = alsa_reorder_int8_out_ ##layout; break;\
  111. case FORMAT_I16: s->reorder_func = alsa_reorder_int16_out_ ##layout; break;\
  112. case FORMAT_I32: s->reorder_func = alsa_reorder_int32_out_ ##layout; break;\
  113. case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout; break;\
  114. }
  115. static av_cold int find_reorder_func(AlsaData *s, int codec_id, uint64_t layout, int out)
  116. {
  117. int format;
  118. /* reordering input is not currently supported */
  119. if (!out)
  120. return AVERROR(ENOSYS);
  121. /* reordering is not needed for QUAD or 2_2 layout */
  122. if (layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2)
  123. return 0;
  124. switch (codec_id) {
  125. case AV_CODEC_ID_PCM_S8:
  126. case AV_CODEC_ID_PCM_U8:
  127. case AV_CODEC_ID_PCM_ALAW:
  128. case AV_CODEC_ID_PCM_MULAW: format = FORMAT_I8; break;
  129. case AV_CODEC_ID_PCM_S16LE:
  130. case AV_CODEC_ID_PCM_S16BE:
  131. case AV_CODEC_ID_PCM_U16LE:
  132. case AV_CODEC_ID_PCM_U16BE: format = FORMAT_I16; break;
  133. case AV_CODEC_ID_PCM_S32LE:
  134. case AV_CODEC_ID_PCM_S32BE:
  135. case AV_CODEC_ID_PCM_U32LE:
  136. case AV_CODEC_ID_PCM_U32BE: format = FORMAT_I32; break;
  137. case AV_CODEC_ID_PCM_F32LE:
  138. case AV_CODEC_ID_PCM_F32BE: format = FORMAT_F32; break;
  139. default: return AVERROR(ENOSYS);
  140. }
  141. if (layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0)
  142. PICK_REORDER(50)
  143. else if (layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1)
  144. PICK_REORDER(51)
  145. else if (layout == AV_CH_LAYOUT_7POINT1)
  146. PICK_REORDER(71)
  147. return s->reorder_func ? 0 : AVERROR(ENOSYS);
  148. }
  149. av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
  150. unsigned int *sample_rate,
  151. int channels, enum AVCodecID *codec_id)
  152. {
  153. AlsaData *s = ctx->priv_data;
  154. const char *audio_device;
  155. int res, flags = 0;
  156. snd_pcm_format_t format;
  157. snd_pcm_t *h;
  158. snd_pcm_hw_params_t *hw_params;
  159. snd_pcm_uframes_t buffer_size, period_size;
  160. uint64_t layout = ctx->streams[0]->codec->channel_layout;
  161. if (ctx->filename[0] == 0) audio_device = "default";
  162. else audio_device = ctx->filename;
  163. if (*codec_id == AV_CODEC_ID_NONE)
  164. *codec_id = DEFAULT_CODEC_ID;
  165. format = codec_id_to_pcm_format(*codec_id);
  166. if (format == SND_PCM_FORMAT_UNKNOWN) {
  167. av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
  168. return AVERROR(ENOSYS);
  169. }
  170. s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
  171. if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
  172. flags = SND_PCM_NONBLOCK;
  173. }
  174. res = snd_pcm_open(&h, audio_device, mode, flags);
  175. if (res < 0) {
  176. av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
  177. audio_device, snd_strerror(res));
  178. return AVERROR(EIO);
  179. }
  180. res = snd_pcm_hw_params_malloc(&hw_params);
  181. if (res < 0) {
  182. av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
  183. snd_strerror(res));
  184. goto fail1;
  185. }
  186. res = snd_pcm_hw_params_any(h, hw_params);
  187. if (res < 0) {
  188. av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
  189. snd_strerror(res));
  190. goto fail;
  191. }
  192. res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
  193. if (res < 0) {
  194. av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
  195. snd_strerror(res));
  196. goto fail;
  197. }
  198. res = snd_pcm_hw_params_set_format(h, hw_params, format);
  199. if (res < 0) {
  200. av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
  201. *codec_id, format, snd_strerror(res));
  202. goto fail;
  203. }
  204. res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
  205. if (res < 0) {
  206. av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
  207. snd_strerror(res));
  208. goto fail;
  209. }
  210. res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
  211. if (res < 0) {
  212. av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
  213. channels, snd_strerror(res));
  214. goto fail;
  215. }
  216. snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
  217. buffer_size = FFMIN(buffer_size, ALSA_BUFFER_SIZE_MAX);
  218. /* TODO: maybe use ctx->max_picture_buffer somehow */
  219. res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
  220. if (res < 0) {
  221. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
  222. snd_strerror(res));
  223. goto fail;
  224. }
  225. snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
  226. if (!period_size)
  227. period_size = buffer_size / 4;
  228. res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
  229. if (res < 0) {
  230. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
  231. snd_strerror(res));
  232. goto fail;
  233. }
  234. s->period_size = period_size;
  235. res = snd_pcm_hw_params(h, hw_params);
  236. if (res < 0) {
  237. av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
  238. snd_strerror(res));
  239. goto fail;
  240. }
  241. snd_pcm_hw_params_free(hw_params);
  242. if (channels > 2 && layout) {
  243. if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) {
  244. char name[128];
  245. av_get_channel_layout_string(name, sizeof(name), channels, layout);
  246. av_log(ctx, AV_LOG_WARNING, "ALSA channel layout unknown or unimplemented for %s %s.\n",
  247. name, mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
  248. }
  249. if (s->reorder_func) {
  250. s->reorder_buf_size = buffer_size;
  251. s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
  252. if (!s->reorder_buf)
  253. goto fail1;
  254. }
  255. }
  256. s->h = h;
  257. return 0;
  258. fail:
  259. snd_pcm_hw_params_free(hw_params);
  260. fail1:
  261. snd_pcm_close(h);
  262. return AVERROR(EIO);
  263. }
  264. av_cold int ff_alsa_close(AVFormatContext *s1)
  265. {
  266. AlsaData *s = s1->priv_data;
  267. av_freep(&s->reorder_buf);
  268. if (CONFIG_ALSA_INDEV)
  269. ff_timefilter_destroy(s->timefilter);
  270. snd_pcm_close(s->h);
  271. return 0;
  272. }
  273. int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
  274. {
  275. AlsaData *s = s1->priv_data;
  276. snd_pcm_t *handle = s->h;
  277. av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
  278. if (err == -EPIPE) {
  279. err = snd_pcm_prepare(handle);
  280. if (err < 0) {
  281. av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
  282. return AVERROR(EIO);
  283. }
  284. } else if (err == -ESTRPIPE) {
  285. av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
  286. return -1;
  287. }
  288. return err;
  289. }
  290. int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
  291. {
  292. int size = s->reorder_buf_size;
  293. void *r;
  294. av_assert0(size != 0);
  295. while (size < min_size)
  296. size *= 2;
  297. r = av_realloc(s->reorder_buf, size * s->frame_size);
  298. if (!r)
  299. return AVERROR(ENOMEM);
  300. s->reorder_buf = r;
  301. s->reorder_buf_size = size;
  302. return 0;
  303. }