alsa-audio-common.c 11 KB

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