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