alsa-audio-common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/audioconvert.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 CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
  38. case CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
  39. case CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
  40. case CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
  41. case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
  42. case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
  43. case CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
  44. case CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
  45. case CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
  46. case CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
  47. case CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
  48. case CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
  49. case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
  50. case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
  51. case CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
  52. case CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
  53. case CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
  54. case CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8;
  55. case CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
  56. case CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW;
  57. default: return SND_PCM_FORMAT_UNKNOWN;
  58. }
  59. }
  60. #define REORDER_OUT_50(NAME, TYPE) \
  61. static void alsa_reorder_ ## NAME ## _out_50(const void *in_v, void *out_v, int n) \
  62. { \
  63. const TYPE *in = in_v; \
  64. TYPE *out = out_v; \
  65. \
  66. while (n-- > 0) { \
  67. out[0] = in[0]; \
  68. out[1] = in[1]; \
  69. out[2] = in[3]; \
  70. out[3] = in[4]; \
  71. out[4] = in[2]; \
  72. in += 5; \
  73. out += 5; \
  74. } \
  75. }
  76. #define REORDER_OUT_51(NAME, TYPE) \
  77. static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \
  78. { \
  79. const TYPE *in = in_v; \
  80. TYPE *out = out_v; \
  81. \
  82. while (n-- > 0) { \
  83. out[0] = in[0]; \
  84. out[1] = in[1]; \
  85. out[2] = in[4]; \
  86. out[3] = in[5]; \
  87. out[4] = in[2]; \
  88. out[5] = in[3]; \
  89. in += 6; \
  90. out += 6; \
  91. } \
  92. }
  93. #define REORDER_OUT_71(NAME, TYPE) \
  94. static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \
  95. { \
  96. const TYPE *in = in_v; \
  97. TYPE *out = out_v; \
  98. \
  99. while (n-- > 0) { \
  100. out[0] = in[0]; \
  101. out[1] = in[1]; \
  102. out[2] = in[4]; \
  103. out[3] = in[5]; \
  104. out[4] = in[2]; \
  105. out[5] = in[3]; \
  106. out[6] = in[6]; \
  107. out[7] = in[7]; \
  108. in += 8; \
  109. out += 8; \
  110. } \
  111. }
  112. REORDER_OUT_50(int8, int8_t)
  113. REORDER_OUT_51(int8, int8_t)
  114. REORDER_OUT_71(int8, int8_t)
  115. REORDER_OUT_50(int16, int16_t)
  116. REORDER_OUT_51(int16, int16_t)
  117. REORDER_OUT_71(int16, int16_t)
  118. REORDER_OUT_50(int32, int32_t)
  119. REORDER_OUT_51(int32, int32_t)
  120. REORDER_OUT_71(int32, int32_t)
  121. REORDER_OUT_50(f32, float)
  122. REORDER_OUT_51(f32, float)
  123. REORDER_OUT_71(f32, float)
  124. #define FORMAT_I8 0
  125. #define FORMAT_I16 1
  126. #define FORMAT_I32 2
  127. #define FORMAT_F32 3
  128. #define PICK_REORDER(layout)\
  129. switch(format) {\
  130. case FORMAT_I8: s->reorder_func = alsa_reorder_int8_out_ ##layout; break;\
  131. case FORMAT_I16: s->reorder_func = alsa_reorder_int16_out_ ##layout; break;\
  132. case FORMAT_I32: s->reorder_func = alsa_reorder_int32_out_ ##layout; break;\
  133. case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout; break;\
  134. }
  135. static av_cold int find_reorder_func(AlsaData *s, int codec_id, uint64_t layout, int out)
  136. {
  137. int format;
  138. /* reordering input is not currently supported */
  139. if (!out)
  140. return AVERROR(ENOSYS);
  141. /* reordering is not needed for QUAD or 2_2 layout */
  142. if (layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2)
  143. return 0;
  144. switch (codec_id) {
  145. case CODEC_ID_PCM_S8:
  146. case CODEC_ID_PCM_U8:
  147. case CODEC_ID_PCM_ALAW:
  148. case CODEC_ID_PCM_MULAW: format = FORMAT_I8; break;
  149. case CODEC_ID_PCM_S16LE:
  150. case CODEC_ID_PCM_S16BE:
  151. case CODEC_ID_PCM_U16LE:
  152. case CODEC_ID_PCM_U16BE: format = FORMAT_I16; break;
  153. case CODEC_ID_PCM_S32LE:
  154. case CODEC_ID_PCM_S32BE:
  155. case CODEC_ID_PCM_U32LE:
  156. case CODEC_ID_PCM_U32BE: format = FORMAT_I32; break;
  157. case CODEC_ID_PCM_F32LE:
  158. case CODEC_ID_PCM_F32BE: format = FORMAT_F32; break;
  159. default: return AVERROR(ENOSYS);
  160. }
  161. if (layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0)
  162. PICK_REORDER(50)
  163. else if (layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1)
  164. PICK_REORDER(51)
  165. else if (layout == AV_CH_LAYOUT_7POINT1)
  166. PICK_REORDER(71)
  167. return s->reorder_func ? 0 : AVERROR(ENOSYS);
  168. }
  169. av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
  170. unsigned int *sample_rate,
  171. int channels, enum CodecID *codec_id)
  172. {
  173. AlsaData *s = ctx->priv_data;
  174. const char *audio_device;
  175. int res, flags = 0;
  176. snd_pcm_format_t format;
  177. snd_pcm_t *h;
  178. snd_pcm_hw_params_t *hw_params;
  179. snd_pcm_uframes_t buffer_size, period_size;
  180. uint64_t layout = ctx->streams[0]->codec->channel_layout;
  181. if (ctx->filename[0] == 0) audio_device = "default";
  182. else audio_device = ctx->filename;
  183. if (*codec_id == CODEC_ID_NONE)
  184. *codec_id = DEFAULT_CODEC_ID;
  185. format = codec_id_to_pcm_format(*codec_id);
  186. if (format == SND_PCM_FORMAT_UNKNOWN) {
  187. av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
  188. return AVERROR(ENOSYS);
  189. }
  190. s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
  191. if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
  192. flags = SND_PCM_NONBLOCK;
  193. }
  194. res = snd_pcm_open(&h, audio_device, mode, flags);
  195. if (res < 0) {
  196. av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
  197. audio_device, snd_strerror(res));
  198. return AVERROR(EIO);
  199. }
  200. res = snd_pcm_hw_params_malloc(&hw_params);
  201. if (res < 0) {
  202. av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
  203. snd_strerror(res));
  204. goto fail1;
  205. }
  206. res = snd_pcm_hw_params_any(h, hw_params);
  207. if (res < 0) {
  208. av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
  209. snd_strerror(res));
  210. goto fail;
  211. }
  212. res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
  213. if (res < 0) {
  214. av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
  215. snd_strerror(res));
  216. goto fail;
  217. }
  218. res = snd_pcm_hw_params_set_format(h, hw_params, format);
  219. if (res < 0) {
  220. av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
  221. *codec_id, format, snd_strerror(res));
  222. goto fail;
  223. }
  224. res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
  225. if (res < 0) {
  226. av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
  227. snd_strerror(res));
  228. goto fail;
  229. }
  230. res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
  231. if (res < 0) {
  232. av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
  233. channels, snd_strerror(res));
  234. goto fail;
  235. }
  236. snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
  237. buffer_size = FFMIN(buffer_size, ALSA_BUFFER_SIZE_MAX);
  238. /* TODO: maybe use ctx->max_picture_buffer somehow */
  239. res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
  240. if (res < 0) {
  241. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
  242. snd_strerror(res));
  243. goto fail;
  244. }
  245. snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
  246. if (!period_size)
  247. period_size = buffer_size / 4;
  248. res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
  249. if (res < 0) {
  250. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
  251. snd_strerror(res));
  252. goto fail;
  253. }
  254. s->period_size = period_size;
  255. res = snd_pcm_hw_params(h, hw_params);
  256. if (res < 0) {
  257. av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
  258. snd_strerror(res));
  259. goto fail;
  260. }
  261. snd_pcm_hw_params_free(hw_params);
  262. if (channels > 2 && layout) {
  263. if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) {
  264. char name[128];
  265. av_get_channel_layout_string(name, sizeof(name), channels, layout);
  266. av_log(ctx, AV_LOG_WARNING, "ALSA channel layout unknown or unimplemented for %s %s.\n",
  267. name, mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
  268. }
  269. if (s->reorder_func) {
  270. s->reorder_buf_size = buffer_size;
  271. s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
  272. if (!s->reorder_buf)
  273. goto fail1;
  274. }
  275. }
  276. s->h = h;
  277. return 0;
  278. fail:
  279. snd_pcm_hw_params_free(hw_params);
  280. fail1:
  281. snd_pcm_close(h);
  282. return AVERROR(EIO);
  283. }
  284. av_cold int ff_alsa_close(AVFormatContext *s1)
  285. {
  286. AlsaData *s = s1->priv_data;
  287. av_freep(&s->reorder_buf);
  288. if (CONFIG_ALSA_INDEV)
  289. ff_timefilter_destroy(s->timefilter);
  290. snd_pcm_close(s->h);
  291. return 0;
  292. }
  293. int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
  294. {
  295. AlsaData *s = s1->priv_data;
  296. snd_pcm_t *handle = s->h;
  297. av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
  298. if (err == -EPIPE) {
  299. err = snd_pcm_prepare(handle);
  300. if (err < 0) {
  301. av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
  302. return AVERROR(EIO);
  303. }
  304. } else if (err == -ESTRPIPE) {
  305. av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
  306. return -1;
  307. }
  308. return err;
  309. }
  310. int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
  311. {
  312. int size = s->reorder_buf_size;
  313. void *r;
  314. av_assert0(size != 0);
  315. while (size < min_size)
  316. size *= 2;
  317. r = av_realloc(s->reorder_buf, size * s->frame_size);
  318. if (!r)
  319. return AVERROR(ENOMEM);
  320. s->reorder_buf = r;
  321. s->reorder_buf_size = size;
  322. return 0;
  323. }