From: Nicolas George <[email protected]> Currently quad, 5.0, 5.1 and 7.1 are implemented. Implementing support for other formats/layouts and capture should be straightforward.
5.0 and 7.1 support by Carl Eugen Hoyos. Signed-off-by: Anton Khirnov <[email protected]> --- libavdevice/alsa-audio-common.c | 156 +++++++++++++++++++++++++++++++++++++++ libavdevice/alsa-audio-enc.c | 10 ++- libavdevice/alsa-audio.h | 7 ++ 3 files changed, 172 insertions(+), 1 deletions(-) diff --git a/libavdevice/alsa-audio-common.c b/libavdevice/alsa-audio-common.c index baa6ac7..3612c59 100644 --- a/libavdevice/alsa-audio-common.c +++ b/libavdevice/alsa-audio-common.c @@ -60,6 +60,125 @@ static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id) } } +#define REORDER_OUT_50(NAME, TYPE) \ +static void alsa_reorder_ ## NAME ## _out_50(const void *in_v, void *out_v, int n) \ +{ \ + const TYPE *in = in_v; \ + TYPE *out = out_v; \ +\ + while (n-- > 0) { \ + out[0] = in[0]; \ + out[1] = in[1]; \ + out[2] = in[3]; \ + out[3] = in[4]; \ + out[4] = in[2]; \ + in += 5; \ + out += 5; \ + } \ +} + +#define REORDER_OUT_51(NAME, TYPE) \ +static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \ +{ \ + const TYPE *in = in_v; \ + TYPE *out = out_v; \ +\ + while (n-- > 0) { \ + out[0] = in[0]; \ + out[1] = in[1]; \ + out[2] = in[4]; \ + out[3] = in[5]; \ + out[4] = in[2]; \ + out[5] = in[3]; \ + in += 6; \ + out += 6; \ + } \ +} + +#define REORDER_OUT_71(NAME, TYPE) \ +static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \ +{ \ + const TYPE *in = in_v; \ + TYPE *out = out_v; \ +\ + while (n-- > 0) { \ + out[0] = in[0]; \ + out[1] = in[1]; \ + out[2] = in[4]; \ + out[3] = in[5]; \ + out[4] = in[2]; \ + out[5] = in[3]; \ + out[6] = in[6]; \ + out[7] = in[7]; \ + in += 8; \ + out += 8; \ + } \ +} + +REORDER_OUT_50(int16, int16_t) +REORDER_OUT_51(int16, int16_t) +REORDER_OUT_71(int16, int16_t) +REORDER_OUT_50(int32, int32_t) +REORDER_OUT_51(int32, int32_t) +REORDER_OUT_71(int32, int32_t) +REORDER_OUT_50(f32, float) +REORDER_OUT_51(f32, float) +REORDER_OUT_71(f32, float) + +#define REORDER_DUMMY ((void *)1) + +#define FORMAT_I16 0 +#define FORMAT_I32 1 +#define FORMAT_F32 2 +static av_cold ff_reorder_func find_reorder_func(int codec_id, + int64_t layout, + int out) +{ + int format; + + /* reordering input is not currently supported */ + if (!out) + return NULL; + + /* reordering is not needed for QUAD or 2_2 layout */ + if (layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2) + return REORDER_DUMMY; + + if ( codec_id == CODEC_ID_PCM_S16LE || codec_id == CODEC_ID_PCM_S16BE || + codec_id == CODEC_ID_PCM_U16LE || codec_id == CODEC_ID_PCM_U16BE) + format = FORMAT_I16; + else if (codec_id == CODEC_ID_PCM_S32LE || codec_id == CODEC_ID_PCM_S32BE || + codec_id == CODEC_ID_PCM_U32LE || codec_id == CODEC_ID_PCM_U32BE) + format = FORMAT_I32; + else if (codec_id == CODEC_ID_PCM_F32LE || codec_id == CODEC_ID_PCM_F32BE) + format = FORMAT_F32; + else + return NULL; + + if (layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1) + switch (format) { + case FORMAT_I16: return alsa_reorder_int16_out_51; + case FORMAT_I32: return alsa_reorder_int32_out_51; + case FORMAT_F32: return alsa_reorder_f32_out_51; + } + + if (layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0) + switch (format) { + case FORMAT_I16: return alsa_reorder_int16_out_50; + case FORMAT_I32: return alsa_reorder_int32_out_50; + case FORMAT_F32: return alsa_reorder_f32_out_50; + } + + if (layout == AV_CH_LAYOUT_7POINT1) + switch (format) { + case FORMAT_I16: return alsa_reorder_int16_out_71; + case FORMAT_I32: return alsa_reorder_int32_out_71; + case FORMAT_F32: return alsa_reorder_f32_out_71; + } + + return NULL; +} + av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode, unsigned int *sample_rate, int channels, enum CodecID *codec_id) @@ -71,6 +190,7 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode, snd_pcm_t *h; snd_pcm_hw_params_t *hw_params; snd_pcm_uframes_t buffer_size, period_size; + int64_t layout = ctx->streams[0]->codec->channel_layout; if (ctx->filename[0] == 0) audio_device = "default"; else audio_device = ctx->filename; @@ -163,6 +283,26 @@ av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode, snd_pcm_hw_params_free(hw_params); + if (channels > 2 && layout) { + s->reorder_func = find_reorder_func(*codec_id, layout, + mode == SND_PCM_STREAM_PLAYBACK); + if (s->reorder_func == REORDER_DUMMY) { + s->reorder_func = NULL; + } else if (s->reorder_func) { + s->reorder_buf_size = buffer_size; + s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size); + if (!s->reorder_buf) + goto fail1; + } else { + char name[32]; + av_get_channel_layout_string(name, sizeof(name), channels, layout); + av_log(ctx, AV_LOG_WARNING, + "ALSA channel layout unknown or unimplemented for %s %s.\n", + name, + mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture"); + } + } + s->h = h; return 0; @@ -177,6 +317,7 @@ av_cold int ff_alsa_close(AVFormatContext *s1) { AlsaData *s = s1->priv_data; + av_freep(&s->reorder_buf); snd_pcm_close(s->h); return 0; } @@ -201,3 +342,18 @@ int ff_alsa_xrun_recover(AVFormatContext *s1, int err) } return err; } + +int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size) +{ + int size = s->reorder_buf_size; + void *r; + + while (size < min_size) + size *= 2; + r = av_realloc(s->reorder_buf, size * s->frame_size); + if (!r) + return AVERROR(ENOMEM); + s->reorder_buf = r; + s->reorder_buf_size = size; + return 0; +} diff --git a/libavdevice/alsa-audio-enc.c b/libavdevice/alsa-audio-enc.c index ebe598c..f3782c5 100644 --- a/libavdevice/alsa-audio-enc.c +++ b/libavdevice/alsa-audio-enc.c @@ -76,7 +76,15 @@ static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt) int size = pkt->size; uint8_t *buf = pkt->data; - while((res = snd_pcm_writei(s->h, buf, size / s->frame_size)) < 0) { + size /= s->frame_size; + if (s->reorder_func) { + if (size > s->reorder_buf_size) + if (ff_alsa_extend_reorder_buf(s, size)) + return AVERROR(ENOMEM); + s->reorder_func(buf, s->reorder_buf, size); + buf = s->reorder_buf; + } + while ((res = snd_pcm_writei(s->h, buf, size)) < 0) { if (res == -EAGAIN) { return AVERROR(EAGAIN); diff --git a/libavdevice/alsa-audio.h b/libavdevice/alsa-audio.h index 32c0742..34f7fc9 100644 --- a/libavdevice/alsa-audio.h +++ b/libavdevice/alsa-audio.h @@ -40,6 +40,8 @@ other formats */ #define DEFAULT_CODEC_ID AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE) +typedef void (*ff_reorder_func)(const void *, void *, int); + typedef struct { AVClass *class; snd_pcm_t *h; @@ -47,6 +49,9 @@ typedef struct { int period_size; ///< bytes per sample * channels int sample_rate; ///< sample rate set by user int channels; ///< number of channels set by user + ff_reorder_func reorder_func; + void *reorder_buf; + int reorder_buf_size; ///< in frames } AlsaData; /** @@ -86,4 +91,6 @@ int ff_alsa_close(AVFormatContext *s1); */ int ff_alsa_xrun_recover(AVFormatContext *s1, int err); +int ff_alsa_extend_reorder_buf(AlsaData *s, int size); + #endif /* AVDEVICE_ALSA_AUDIO_H */ -- 1.7.5.3 _______________________________________________ libav-devel mailing list [email protected] https://lists.libav.org/mailman/listinfo/libav-devel
