00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00031 #include <alsa/asoundlib.h>
00032 #include "avdevice.h"
00033 #include "libavutil/avassert.h"
00034 #include "libavutil/audioconvert.h"
00035
00036 #include "alsa-audio.h"
00037
00038 static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
00039 {
00040 switch(codec_id) {
00041 case AV_CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
00042 case AV_CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
00043 case AV_CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
00044 case AV_CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
00045 case AV_CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
00046 case AV_CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
00047 case AV_CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
00048 case AV_CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
00049 case AV_CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
00050 case AV_CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
00051 case AV_CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
00052 case AV_CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
00053 case AV_CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
00054 case AV_CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
00055 case AV_CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
00056 case AV_CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
00057 case AV_CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
00058 case AV_CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8;
00059 case AV_CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
00060 case AV_CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW;
00061 default: return SND_PCM_FORMAT_UNKNOWN;
00062 }
00063 }
00064
00065 #define REORDER_OUT_50(NAME, TYPE) \
00066 static void alsa_reorder_ ## NAME ## _out_50(const void *in_v, void *out_v, int n) \
00067 { \
00068 const TYPE *in = in_v; \
00069 TYPE *out = out_v; \
00070 \
00071 while (n-- > 0) { \
00072 out[0] = in[0]; \
00073 out[1] = in[1]; \
00074 out[2] = in[3]; \
00075 out[3] = in[4]; \
00076 out[4] = in[2]; \
00077 in += 5; \
00078 out += 5; \
00079 } \
00080 }
00081
00082 #define REORDER_OUT_51(NAME, TYPE) \
00083 static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \
00084 { \
00085 const TYPE *in = in_v; \
00086 TYPE *out = out_v; \
00087 \
00088 while (n-- > 0) { \
00089 out[0] = in[0]; \
00090 out[1] = in[1]; \
00091 out[2] = in[4]; \
00092 out[3] = in[5]; \
00093 out[4] = in[2]; \
00094 out[5] = in[3]; \
00095 in += 6; \
00096 out += 6; \
00097 } \
00098 }
00099
00100 #define REORDER_OUT_71(NAME, TYPE) \
00101 static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \
00102 { \
00103 const TYPE *in = in_v; \
00104 TYPE *out = out_v; \
00105 \
00106 while (n-- > 0) { \
00107 out[0] = in[0]; \
00108 out[1] = in[1]; \
00109 out[2] = in[4]; \
00110 out[3] = in[5]; \
00111 out[4] = in[2]; \
00112 out[5] = in[3]; \
00113 out[6] = in[6]; \
00114 out[7] = in[7]; \
00115 in += 8; \
00116 out += 8; \
00117 } \
00118 }
00119
00120 REORDER_OUT_50(int8, int8_t)
00121 REORDER_OUT_51(int8, int8_t)
00122 REORDER_OUT_71(int8, int8_t)
00123 REORDER_OUT_50(int16, int16_t)
00124 REORDER_OUT_51(int16, int16_t)
00125 REORDER_OUT_71(int16, int16_t)
00126 REORDER_OUT_50(int32, int32_t)
00127 REORDER_OUT_51(int32, int32_t)
00128 REORDER_OUT_71(int32, int32_t)
00129 REORDER_OUT_50(f32, float)
00130 REORDER_OUT_51(f32, float)
00131 REORDER_OUT_71(f32, float)
00132
00133 #define FORMAT_I8 0
00134 #define FORMAT_I16 1
00135 #define FORMAT_I32 2
00136 #define FORMAT_F32 3
00137
00138 #define PICK_REORDER(layout)\
00139 switch(format) {\
00140 case FORMAT_I8: s->reorder_func = alsa_reorder_int8_out_ ##layout; break;\
00141 case FORMAT_I16: s->reorder_func = alsa_reorder_int16_out_ ##layout; break;\
00142 case FORMAT_I32: s->reorder_func = alsa_reorder_int32_out_ ##layout; break;\
00143 case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout; break;\
00144 }
00145
00146 static av_cold int find_reorder_func(AlsaData *s, int codec_id, uint64_t layout, int out)
00147 {
00148 int format;
00149
00150
00151 if (!out)
00152 return AVERROR(ENOSYS);
00153
00154
00155 if (layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2)
00156 return 0;
00157
00158 switch (codec_id) {
00159 case AV_CODEC_ID_PCM_S8:
00160 case AV_CODEC_ID_PCM_U8:
00161 case AV_CODEC_ID_PCM_ALAW:
00162 case AV_CODEC_ID_PCM_MULAW: format = FORMAT_I8; break;
00163 case AV_CODEC_ID_PCM_S16LE:
00164 case AV_CODEC_ID_PCM_S16BE:
00165 case AV_CODEC_ID_PCM_U16LE:
00166 case AV_CODEC_ID_PCM_U16BE: format = FORMAT_I16; break;
00167 case AV_CODEC_ID_PCM_S32LE:
00168 case AV_CODEC_ID_PCM_S32BE:
00169 case AV_CODEC_ID_PCM_U32LE:
00170 case AV_CODEC_ID_PCM_U32BE: format = FORMAT_I32; break;
00171 case AV_CODEC_ID_PCM_F32LE:
00172 case AV_CODEC_ID_PCM_F32BE: format = FORMAT_F32; break;
00173 default: return AVERROR(ENOSYS);
00174 }
00175
00176 if (layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0)
00177 PICK_REORDER(50)
00178 else if (layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1)
00179 PICK_REORDER(51)
00180 else if (layout == AV_CH_LAYOUT_7POINT1)
00181 PICK_REORDER(71)
00182
00183 return s->reorder_func ? 0 : AVERROR(ENOSYS);
00184 }
00185
00186 av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
00187 unsigned int *sample_rate,
00188 int channels, enum AVCodecID *codec_id)
00189 {
00190 AlsaData *s = ctx->priv_data;
00191 const char *audio_device;
00192 int res, flags = 0;
00193 snd_pcm_format_t format;
00194 snd_pcm_t *h;
00195 snd_pcm_hw_params_t *hw_params;
00196 snd_pcm_uframes_t buffer_size, period_size;
00197 uint64_t layout = ctx->streams[0]->codec->channel_layout;
00198
00199 if (ctx->filename[0] == 0) audio_device = "default";
00200 else audio_device = ctx->filename;
00201
00202 if (*codec_id == AV_CODEC_ID_NONE)
00203 *codec_id = DEFAULT_CODEC_ID;
00204 format = codec_id_to_pcm_format(*codec_id);
00205 if (format == SND_PCM_FORMAT_UNKNOWN) {
00206 av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
00207 return AVERROR(ENOSYS);
00208 }
00209 s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
00210
00211 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
00212 flags = SND_PCM_NONBLOCK;
00213 }
00214 res = snd_pcm_open(&h, audio_device, mode, flags);
00215 if (res < 0) {
00216 av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
00217 audio_device, snd_strerror(res));
00218 return AVERROR(EIO);
00219 }
00220
00221 res = snd_pcm_hw_params_malloc(&hw_params);
00222 if (res < 0) {
00223 av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
00224 snd_strerror(res));
00225 goto fail1;
00226 }
00227
00228 res = snd_pcm_hw_params_any(h, hw_params);
00229 if (res < 0) {
00230 av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
00231 snd_strerror(res));
00232 goto fail;
00233 }
00234
00235 res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
00236 if (res < 0) {
00237 av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
00238 snd_strerror(res));
00239 goto fail;
00240 }
00241
00242 res = snd_pcm_hw_params_set_format(h, hw_params, format);
00243 if (res < 0) {
00244 av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
00245 *codec_id, format, snd_strerror(res));
00246 goto fail;
00247 }
00248
00249 res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
00250 if (res < 0) {
00251 av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
00252 snd_strerror(res));
00253 goto fail;
00254 }
00255
00256 res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
00257 if (res < 0) {
00258 av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
00259 channels, snd_strerror(res));
00260 goto fail;
00261 }
00262
00263 snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
00264 buffer_size = FFMIN(buffer_size, ALSA_BUFFER_SIZE_MAX);
00265
00266 res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
00267 if (res < 0) {
00268 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
00269 snd_strerror(res));
00270 goto fail;
00271 }
00272
00273 snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
00274 if (!period_size)
00275 period_size = buffer_size / 4;
00276 res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
00277 if (res < 0) {
00278 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
00279 snd_strerror(res));
00280 goto fail;
00281 }
00282 s->period_size = period_size;
00283
00284 res = snd_pcm_hw_params(h, hw_params);
00285 if (res < 0) {
00286 av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
00287 snd_strerror(res));
00288 goto fail;
00289 }
00290
00291 snd_pcm_hw_params_free(hw_params);
00292
00293 if (channels > 2 && layout) {
00294 if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) {
00295 char name[128];
00296 av_get_channel_layout_string(name, sizeof(name), channels, layout);
00297 av_log(ctx, AV_LOG_WARNING, "ALSA channel layout unknown or unimplemented for %s %s.\n",
00298 name, mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
00299 }
00300 if (s->reorder_func) {
00301 s->reorder_buf_size = buffer_size;
00302 s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
00303 if (!s->reorder_buf)
00304 goto fail1;
00305 }
00306 }
00307
00308 s->h = h;
00309 return 0;
00310
00311 fail:
00312 snd_pcm_hw_params_free(hw_params);
00313 fail1:
00314 snd_pcm_close(h);
00315 return AVERROR(EIO);
00316 }
00317
00318 av_cold int ff_alsa_close(AVFormatContext *s1)
00319 {
00320 AlsaData *s = s1->priv_data;
00321
00322 av_freep(&s->reorder_buf);
00323 if (CONFIG_ALSA_INDEV)
00324 ff_timefilter_destroy(s->timefilter);
00325 snd_pcm_close(s->h);
00326 return 0;
00327 }
00328
00329 int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
00330 {
00331 AlsaData *s = s1->priv_data;
00332 snd_pcm_t *handle = s->h;
00333
00334 av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
00335 if (err == -EPIPE) {
00336 err = snd_pcm_prepare(handle);
00337 if (err < 0) {
00338 av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
00339
00340 return AVERROR(EIO);
00341 }
00342 } else if (err == -ESTRPIPE) {
00343 av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
00344
00345 return -1;
00346 }
00347 return err;
00348 }
00349
00350 int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
00351 {
00352 int size = s->reorder_buf_size;
00353 void *r;
00354
00355 av_assert0(size != 0);
00356 while (size < min_size)
00357 size *= 2;
00358 r = av_realloc(s->reorder_buf, size * s->frame_size);
00359 if (!r)
00360 return AVERROR(ENOMEM);
00361 s->reorder_buf = r;
00362 s->reorder_buf_size = size;
00363 return 0;
00364 }