You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
9.1KB

  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 "libavformat/avformat.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_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
  36. case CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
  37. case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
  38. case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
  39. case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
  40. case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
  41. case CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
  42. default: return SND_PCM_FORMAT_UNKNOWN;
  43. }
  44. }
  45. #define REORDER_OUT_51(NAME, TYPE) \
  46. static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \
  47. { \
  48. const TYPE *in = in_v; \
  49. TYPE * out = out_v; \
  50. \
  51. while (n-- > 0) { \
  52. out[0] = in[0]; \
  53. out[1] = in[1]; \
  54. out[2] = in[4]; \
  55. out[3] = in[5]; \
  56. out[4] = in[2]; \
  57. out[5] = in[3]; \
  58. in += 6; \
  59. out += 6; \
  60. } \
  61. }
  62. #define REORDER_OUT_71(NAME, TYPE) \
  63. static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \
  64. { \
  65. const TYPE *in = in_v; \
  66. TYPE * out = out_v; \
  67. \
  68. while (n-- > 0) { \
  69. out[0] = in[0]; \
  70. out[1] = in[1]; \
  71. out[2] = in[4]; \
  72. out[3] = in[5]; \
  73. out[4] = in[2]; \
  74. out[5] = in[3]; \
  75. out[6] = in[6]; \
  76. out[7] = in[7]; \
  77. in += 8; \
  78. out += 8; \
  79. } \
  80. }
  81. REORDER_OUT_51(s16, int16_t)
  82. REORDER_OUT_71(s16, int16_t)
  83. REORDER_OUT_51(s32, int32_t)
  84. REORDER_OUT_71(s32, int32_t)
  85. #define REORDER_DUMMY ((void *)1)
  86. static av_cold ff_reorder_func find_reorder_func(int codec_id,
  87. int64_t layout,
  88. int out)
  89. {
  90. return
  91. codec_id == CODEC_ID_PCM_S16LE || codec_id == CODEC_ID_PCM_S16BE ?
  92. layout == AV_CH_LAYOUT_QUAD ? REORDER_DUMMY :
  93. layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
  94. out ? alsa_reorder_s16_out_51 : NULL :
  95. layout == AV_CH_LAYOUT_7POINT1 ?
  96. out ? alsa_reorder_s16_out_71 : NULL :
  97. NULL :
  98. codec_id == CODEC_ID_PCM_S32LE || codec_id == CODEC_ID_PCM_S32BE ?
  99. layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
  100. out ? alsa_reorder_s32_out_51 : NULL :
  101. layout == AV_CH_LAYOUT_7POINT1 ?
  102. out ? alsa_reorder_s32_out_71 : NULL :
  103. NULL :
  104. NULL;
  105. }
  106. av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
  107. unsigned int *sample_rate,
  108. int channels, enum CodecID *codec_id)
  109. {
  110. AlsaData *s = ctx->priv_data;
  111. const char *audio_device;
  112. int res, flags = 0;
  113. snd_pcm_format_t format;
  114. snd_pcm_t *h;
  115. snd_pcm_hw_params_t *hw_params;
  116. snd_pcm_uframes_t buffer_size, period_size;
  117. int64_t layout = ctx->streams[0]->codec->channel_layout;
  118. if (ctx->filename[0] == 0) audio_device = "default";
  119. else audio_device = ctx->filename;
  120. if (*codec_id == CODEC_ID_NONE)
  121. *codec_id = DEFAULT_CODEC_ID;
  122. format = codec_id_to_pcm_format(*codec_id);
  123. if (format == SND_PCM_FORMAT_UNKNOWN) {
  124. av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
  125. return AVERROR(ENOSYS);
  126. }
  127. s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
  128. if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
  129. flags = SND_PCM_NONBLOCK;
  130. }
  131. res = snd_pcm_open(&h, audio_device, mode, flags);
  132. if (res < 0) {
  133. av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
  134. audio_device, snd_strerror(res));
  135. return AVERROR(EIO);
  136. }
  137. res = snd_pcm_hw_params_malloc(&hw_params);
  138. if (res < 0) {
  139. av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
  140. snd_strerror(res));
  141. goto fail1;
  142. }
  143. res = snd_pcm_hw_params_any(h, hw_params);
  144. if (res < 0) {
  145. av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
  146. snd_strerror(res));
  147. goto fail;
  148. }
  149. res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
  150. if (res < 0) {
  151. av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
  152. snd_strerror(res));
  153. goto fail;
  154. }
  155. res = snd_pcm_hw_params_set_format(h, hw_params, format);
  156. if (res < 0) {
  157. av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
  158. *codec_id, format, snd_strerror(res));
  159. goto fail;
  160. }
  161. res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
  162. if (res < 0) {
  163. av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
  164. snd_strerror(res));
  165. goto fail;
  166. }
  167. res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
  168. if (res < 0) {
  169. av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
  170. channels, snd_strerror(res));
  171. goto fail;
  172. }
  173. snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
  174. /* TODO: maybe use ctx->max_picture_buffer somehow */
  175. res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
  176. if (res < 0) {
  177. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
  178. snd_strerror(res));
  179. goto fail;
  180. }
  181. snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
  182. res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
  183. if (res < 0) {
  184. av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
  185. snd_strerror(res));
  186. goto fail;
  187. }
  188. s->period_size = period_size;
  189. res = snd_pcm_hw_params(h, hw_params);
  190. if (res < 0) {
  191. av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
  192. snd_strerror(res));
  193. goto fail;
  194. }
  195. snd_pcm_hw_params_free(hw_params);
  196. if (channels > 2 && layout) {
  197. s->reorder_func = find_reorder_func(*codec_id, layout,
  198. mode == SND_PCM_STREAM_PLAYBACK);
  199. if (s->reorder_func == REORDER_DUMMY) {
  200. s->reorder_func = NULL;
  201. } else if (s->reorder_func) {
  202. s->reorder_buf_size = buffer_size;
  203. s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
  204. if (!s->reorder_buf)
  205. goto fail1;
  206. } else {
  207. char name[16];
  208. av_get_channel_layout_string(name, sizeof(name), channels, layout);
  209. av_log(ctx, AV_LOG_WARNING,
  210. "ALSA channel layout unknown or unimplemented for %s %s.\n",
  211. name,
  212. mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
  213. }
  214. }
  215. s->h = h;
  216. return 0;
  217. fail:
  218. snd_pcm_hw_params_free(hw_params);
  219. fail1:
  220. snd_pcm_close(h);
  221. return AVERROR(EIO);
  222. }
  223. av_cold int ff_alsa_close(AVFormatContext *s1)
  224. {
  225. AlsaData *s = s1->priv_data;
  226. av_freep(&s->reorder_buf);
  227. snd_pcm_close(s->h);
  228. return 0;
  229. }
  230. int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
  231. {
  232. AlsaData *s = s1->priv_data;
  233. snd_pcm_t *handle = s->h;
  234. av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
  235. if (err == -EPIPE) {
  236. err = snd_pcm_prepare(handle);
  237. if (err < 0) {
  238. av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
  239. return AVERROR(EIO);
  240. }
  241. } else if (err == -ESTRPIPE) {
  242. av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
  243. return -1;
  244. }
  245. return err;
  246. }
  247. int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
  248. {
  249. int size = s->reorder_buf_size;
  250. void *r;
  251. while (size < min_size)
  252. size *= 2;
  253. r = av_realloc(s->reorder_buf, size * s->frame_size);
  254. if (!r)
  255. return AVERROR(ENOMEM);
  256. s->reorder_buf = r;
  257. s->reorder_buf_size = size;
  258. return 0;
  259. }