alsa-audio-common.c 12 KB

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