[FFmpeg-devel] [PATCH 2/3] lavd/alsa: use the channels reordering API.

Nicolas George nicolas.george at normalesup.org
Thu Nov 22 19:13:58 CET 2012


Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 libavdevice/alsa-audio-common.c |  108 ++++-----------------------------------
 libavdevice/alsa-audio-enc.c    |    3 +-
 libavdevice/alsa-audio.h        |    5 +-
 3 files changed, 15 insertions(+), 101 deletions(-)

diff --git a/libavdevice/alsa-audio-common.c b/libavdevice/alsa-audio-common.c
index afe6751..9331f75 100644
--- a/libavdevice/alsa-audio-common.c
+++ b/libavdevice/alsa-audio-common.c
@@ -62,125 +62,37 @@ 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(int8, int8_t)
-REORDER_OUT_51(int8, int8_t)
-REORDER_OUT_71(int8, int8_t)
-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 FORMAT_I8  0
-#define FORMAT_I16 1
-#define FORMAT_I32 2
-#define FORMAT_F32 3
-
-#define PICK_REORDER(layout)\
-switch(format) {\
-    case FORMAT_I8:  s->reorder_func = alsa_reorder_int8_out_ ##layout;  break;\
-    case FORMAT_I16: s->reorder_func = alsa_reorder_int16_out_ ##layout; break;\
-    case FORMAT_I32: s->reorder_func = alsa_reorder_int32_out_ ##layout; break;\
-    case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout;   break;\
-}
-
 static av_cold int find_reorder_func(AlsaData *s, int codec_id, uint64_t layout, int out)
 {
-    int format;
+    int channels = av_get_channel_layout_nb_channels(layout);
+    enum AVSampleFormat format;
 
     /* reordering input is not currently supported */
     if (!out)
         return AVERROR(ENOSYS);
 
-    /* reordering is not needed for QUAD or 2_2 layout */
-    if (layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2)
-        return 0;
-
     switch (codec_id) {
     case AV_CODEC_ID_PCM_S8:
     case AV_CODEC_ID_PCM_U8:
     case AV_CODEC_ID_PCM_ALAW:
-    case AV_CODEC_ID_PCM_MULAW: format = FORMAT_I8;  break;
+    case AV_CODEC_ID_PCM_MULAW: format = AV_SAMPLE_FMT_U8;  break;
     case AV_CODEC_ID_PCM_S16LE:
     case AV_CODEC_ID_PCM_S16BE:
     case AV_CODEC_ID_PCM_U16LE:
-    case AV_CODEC_ID_PCM_U16BE: format = FORMAT_I16; break;
+    case AV_CODEC_ID_PCM_U16BE: format = AV_SAMPLE_FMT_S16; break;
     case AV_CODEC_ID_PCM_S32LE:
     case AV_CODEC_ID_PCM_S32BE:
     case AV_CODEC_ID_PCM_U32LE:
-    case AV_CODEC_ID_PCM_U32BE: format = FORMAT_I32; break;
+    case AV_CODEC_ID_PCM_U32BE: format = AV_SAMPLE_FMT_S32; break;
     case AV_CODEC_ID_PCM_F32LE:
-    case AV_CODEC_ID_PCM_F32BE: format = FORMAT_F32; break;
+    case AV_CODEC_ID_PCM_F32BE: format = AV_SAMPLE_FMT_FLT; break;
     default:                 return AVERROR(ENOSYS);
     }
 
-    if      (layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0)
-        PICK_REORDER(50)
-    else if (layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1)
-        PICK_REORDER(51)
-    else if (layout == AV_CH_LAYOUT_7POINT1)
-        PICK_REORDER(71)
-
-    return s->reorder_func ? 0 : AVERROR(ENOSYS);
+    return av_get_channels_reorder_func(&s->reorder_func,
+                                        format, channels, layout,
+                                        AV_CH_ORDER_INTERNAL, AV_CH_ORDER_ALSA,
+                                        0);
 }
 
 av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
diff --git a/libavdevice/alsa-audio-enc.c b/libavdevice/alsa-audio-enc.c
index 403a44f..86d5e16 100644
--- a/libavdevice/alsa-audio-enc.c
+++ b/libavdevice/alsa-audio-enc.c
@@ -39,6 +39,7 @@
 
 #include <alsa/asoundlib.h>
 
+#include "libavutil/channels_reorder.h"
 #include "libavformat/internal.h"
 #include "avdevice.h"
 #include "alsa-audio.h"
@@ -83,7 +84,7 @@ static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
         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);
+        s->reorder_func(&s->reorder_buf, &buf, size);
         buf = s->reorder_buf;
     }
     while ((res = snd_pcm_writei(s->h, buf, size)) < 0) {
diff --git a/libavdevice/alsa-audio.h b/libavdevice/alsa-audio.h
index 44b7c72..87df547 100644
--- a/libavdevice/alsa-audio.h
+++ b/libavdevice/alsa-audio.h
@@ -32,6 +32,7 @@
 
 #include <alsa/asoundlib.h>
 #include "config.h"
+#include "libavutil/channels_reorder.h"
 #include "libavutil/log.h"
 #include "timefilter.h"
 #include "avdevice.h"
@@ -54,8 +55,8 @@ typedef struct AlsaData {
     int channels;    ///< number of channels set by user
     int last_period;
     TimeFilter *timefilter;
-    void (*reorder_func)(const void *, void *, int);
-    void *reorder_buf;
+    AVChannelsReorderFunc reorder_func;
+    uint8_t *reorder_buf;
     int reorder_buf_size; ///< in frames
 } AlsaData;
 
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list