[MPlayer-cvslog] r25343 - in trunk: DOCS/man/en/mplayer.1 libaf/Makefile libaf/reorder_ch.c libaf/reorder_ch.h libao2/ao_pcm.c libmpcodecs/ad_dmo.c libmpcodecs/ad_faad.c libmpcodecs/ad_ffmpeg.c libmpcodecs/ad_pcm.c libmpcodecs/ae_faac.c libmpcodecs/ae_lavc.c libmpcodecs/ae_pcm.c

ulion subversion at mplayerhq.hu
Mon Dec 10 17:53:30 CET 2007


Author: ulion
Date: Mon Dec 10 17:53:30 2007
New Revision: 25343

Log:
Fix all current known multi-channel wrong order problems by adding
common functions for channel reordering.
This fixes these modules by adding channel reordering code for 5.0/5.1 audio:
ao: pcm
ad: dmo, faad, ffmpeg(ac3, dca, libfaad, liba52), pcm
ae: faac, lavc(ac3, libfaac), pcm


Added:
   trunk/libaf/reorder_ch.c
   trunk/libaf/reorder_ch.h
Modified:
   trunk/libaf/Makefile
   trunk/libao2/ao_pcm.c
   trunk/libmpcodecs/ad_dmo.c
   trunk/libmpcodecs/ad_faad.c
   trunk/libmpcodecs/ad_ffmpeg.c
   trunk/libmpcodecs/ad_pcm.c
   trunk/libmpcodecs/ae_faac.c
   trunk/libmpcodecs/ae_lavc.c
   trunk/libmpcodecs/ae_pcm.c

Changes in other areas also in this revision:
Modified:
   trunk/DOCS/man/en/mplayer.1

Modified: trunk/libaf/Makefile
==============================================================================
--- trunk/libaf/Makefile	(original)
+++ trunk/libaf/Makefile	Mon Dec 10 17:53:30 2007
@@ -26,6 +26,7 @@ SRCS_COMMON = af.c \
               af_volume.c \
               filter.c \
               format.c \
+              reorder_ch.c \
               window.c \
               $(AF_SRCS) \
 

Added: trunk/libaf/reorder_ch.c
==============================================================================
--- (empty file)
+++ trunk/libaf/reorder_ch.c	Mon Dec 10 17:53:30 2007
@@ -0,0 +1,1197 @@
+/*
+ * common functions for reordering audio channels
+ *
+ * Copyright (C) 2007 Ulion <ulion A gmail P com>
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MPlayer; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include "libvo/fastmemcpy.h"
+
+#include "reorder_ch.h"
+
+#ifdef TEST
+#define mp_msg(mod,lev, fmt, args... )  printf( fmt, ## args )
+#else
+#include "mp_msg.h"
+#endif
+
+
+#define REORDER_COPY_5(DEST,SRC,SAMPLES,S0,S1,S2,S3,S4) \
+for (i = 0; i < SAMPLES; i += 5) {\
+    DEST[i]   = SRC[i+S0];\
+    DEST[i+1] = SRC[i+S1];\
+    DEST[i+2] = SRC[i+S2];\
+    DEST[i+3] = SRC[i+S3];\
+    DEST[i+4] = SRC[i+S4];\
+}
+
+static int reorder_copy_5ch(void *dest, const void *src,
+                            unsigned int samples, unsigned int samplesize,
+                            int s0, int s1, int s2, int s3, int s4)
+{
+    int i;
+    switch (samplesize) {
+    case 1:
+    {
+        int8_t *dest_8 = dest;
+        const int8_t *src_8 = src;
+        REORDER_COPY_5(dest_8,src_8,samples,s0,s1,s2,s3,s4);
+        break;
+    }
+    case 2:
+    {
+        int16_t *dest_16 = dest;
+        const int16_t *src_16 = src;
+        REORDER_COPY_5(dest_16,src_16,samples,s0,s1,s2,s3,s4);
+        break;
+    }
+    case 3:
+    {
+        int8_t *dest_8 = dest;
+        const int8_t *src_8 = src;
+        for (i = 0; i < samples; i += 15) {
+            dest_8[i]    = src_8[i+s0*3];
+            dest_8[i+1]  = src_8[i+s0*3+1];
+            dest_8[i+2]  = src_8[i+s0*3+2];
+            dest_8[i+3]  = src_8[i+s1*3];
+            dest_8[i+4]  = src_8[i+s1*3+1];
+            dest_8[i+5]  = src_8[i+s1*3+2];
+            dest_8[i+6]  = src_8[i+s2*3];
+            dest_8[i+7]  = src_8[i+s2*3+1];
+            dest_8[i+8]  = src_8[i+s2*3+2];
+            dest_8[i+9]  = src_8[i+s3*3];
+            dest_8[i+10] = src_8[i+s3*3+1];
+            dest_8[i+11] = src_8[i+s3*3+2];
+            dest_8[i+12] = src_8[i+s4*3];
+            dest_8[i+13] = src_8[i+s4*3+1];
+            dest_8[i+14] = src_8[i+s4*3+2];
+        }
+    }
+    case 4:
+    {
+        int32_t *dest_32 = dest;
+        const int32_t *src_32 = src;
+        REORDER_COPY_5(dest_32,src_32,samples,s0,s1,s2,s3,s4);
+        break;
+    }
+    case 8:
+    {
+        int64_t *dest_64 = dest;
+        const int64_t *src_64 = src;
+        REORDER_COPY_5(dest_64,src_64,samples,s0,s1,s2,s3,s4);
+        break;
+    }
+    default:
+        mp_msg(MSGT_GLOBAL, MSGL_WARN,
+               "[reorder_ch] Unsupported sample size: %d, please "
+               "report this error on the MPlayer mailing list.\n",samplesize);
+        return 0;
+    }
+    return 1;
+}
+
+#define REORDER_COPY_6(DEST,SRC,SAMPLES,S0,S1,S2,S3,S4,S5) \
+for (i = 0; i < SAMPLES; i += 6) {\
+    DEST[i]   = SRC[i+S0];\
+    DEST[i+1] = SRC[i+S1];\
+    DEST[i+2] = SRC[i+S2];\
+    DEST[i+3] = SRC[i+S3];\
+    DEST[i+4] = SRC[i+S4];\
+    DEST[i+5] = SRC[i+S5];\
+}
+
+static int reorder_copy_6ch(void *dest, const void *src,
+                            unsigned int samples, uint8_t samplesize,
+                            int s0, int s1, int s2, int s3, int s4, int s5)
+{
+    int i;
+    switch (samplesize) {
+    case 1:
+    {
+        int8_t *dest_8 = dest;
+        const int8_t *src_8 = src;
+        REORDER_COPY_6(dest_8,src_8,samples,s0,s1,s2,s3,s4,s5);
+        break;
+    }
+    case 2:
+    {
+        int16_t *dest_16 = dest;
+        const int16_t *src_16 = src;
+        REORDER_COPY_6(dest_16,src_16,samples,s0,s1,s2,s3,s4,s5);
+        break;
+    }
+    case 3:
+    {
+        int8_t *dest_8 = dest;
+        const int8_t *src_8 = src;
+        for (i = 0; i < samples; i += 18) {
+            dest_8[i]    = src_8[i+s0*3];
+            dest_8[i+1]  = src_8[i+s0*3+1];
+            dest_8[i+2]  = src_8[i+s0*3+2];
+            dest_8[i+3]  = src_8[i+s1*3];
+            dest_8[i+4]  = src_8[i+s1*3+1];
+            dest_8[i+5]  = src_8[i+s1*3+2];
+            dest_8[i+6]  = src_8[i+s2*3];
+            dest_8[i+7]  = src_8[i+s2*3+1];
+            dest_8[i+8]  = src_8[i+s2*3+2];
+            dest_8[i+9]  = src_8[i+s3*3];
+            dest_8[i+10] = src_8[i+s3*3+1];
+            dest_8[i+11] = src_8[i+s3*3+2];
+            dest_8[i+12] = src_8[i+s4*3];
+            dest_8[i+13] = src_8[i+s4*3+1];
+            dest_8[i+14] = src_8[i+s4*3+2];
+            dest_8[i+15] = src_8[i+s5*3];
+            dest_8[i+16] = src_8[i+s5*3+1];
+            dest_8[i+17] = src_8[i+s5*3+2];
+        }
+    }
+    case 4:
+    {
+        int32_t *dest_32 = dest;
+        const int32_t *src_32 = src;
+        REORDER_COPY_6(dest_32,src_32,samples,s0,s1,s2,s3,s4,s5);
+        break;
+    }
+    case 8:
+    {
+        int64_t *dest_64 = dest;
+        const int64_t *src_64 = src;
+        REORDER_COPY_6(dest_64,src_64,samples,s0,s1,s2,s3,s4,s5);
+        break;
+    }
+    default:
+        mp_msg(MSGT_GLOBAL, MSGL_WARN,
+               "[reorder_ch] Unsupported sample size: %d, please "
+               "report this error on the MPlayer mailing list.\n",samplesize);
+        return 0;
+    }
+    return 1;
+}
+
+void reorder_channel_copy(void *src,
+                          int src_layout,
+                          void *dest,
+                          int dest_layout,
+                          int samples,
+                          int samplesize)
+{
+    if (dest_layout==src_layout) {
+        fast_memcpy(dest, src, samples*samplesize);
+        return;
+    }
+    if (!AF_IS_SAME_CH_NUM(dest_layout,src_layout)) {
+        mp_msg(MSGT_GLOBAL, MSGL_WARN, "[reorder_ch] different channel count "
+               "between src and dest: %x, %x\n",
+               AF_GET_CH_NUM_WITH_LFE(src_layout),
+               AF_GET_CH_NUM_WITH_LFE(dest_layout));
+        return;
+    }
+    switch ((src_layout<<16)|dest_layout) {
+    // AF_CHANNEL_LAYOUT_5_0_A   L R C Ls Rs
+    // AF_CHANNEL_LAYOUT_5_0_B   L R Ls Rs C
+    // AF_CHANNEL_LAYOUT_5_0_C   L C R Ls Rs
+    // AF_CHANNEL_LAYOUT_5_0_D   C L R Ls Rs
+    case AF_CHANNEL_LAYOUT_5_0_A << 16 | AF_CHANNEL_LAYOUT_5_0_B:
+        reorder_copy_5ch(dest, src, samples, samplesize, 0, 1, 3, 4, 2);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_A << 16 | AF_CHANNEL_LAYOUT_5_0_C:
+        reorder_copy_5ch(dest, src, samples, samplesize, 0, 2, 1, 3, 4);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_A << 16 | AF_CHANNEL_LAYOUT_5_0_D:
+        reorder_copy_5ch(dest, src, samples, samplesize, 2, 0, 1, 3, 4);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_B << 16 | AF_CHANNEL_LAYOUT_5_0_A:
+        reorder_copy_5ch(dest, src, samples, samplesize, 0, 1, 4, 2, 3);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_B << 16 | AF_CHANNEL_LAYOUT_5_0_C:
+        reorder_copy_5ch(dest, src, samples, samplesize, 0, 4, 1, 2, 3);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_B << 16 | AF_CHANNEL_LAYOUT_5_0_D:
+        reorder_copy_5ch(dest, src, samples, samplesize, 4, 0, 1, 2, 3);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_C << 16 | AF_CHANNEL_LAYOUT_5_0_A:
+        reorder_copy_5ch(dest, src, samples, samplesize, 0, 2, 1, 3, 4);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_C << 16 | AF_CHANNEL_LAYOUT_5_0_B:
+        reorder_copy_5ch(dest, src, samples, samplesize, 0, 2, 3, 4, 1);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_C << 16 | AF_CHANNEL_LAYOUT_5_0_D:
+        reorder_copy_5ch(dest, src, samples, samplesize, 1, 0, 2, 3, 4);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_D << 16 | AF_CHANNEL_LAYOUT_5_0_A:
+        reorder_copy_5ch(dest, src, samples, samplesize, 1, 2, 0, 3, 4);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_D << 16 | AF_CHANNEL_LAYOUT_5_0_B:
+        reorder_copy_5ch(dest, src, samples, samplesize, 1, 2, 3, 4, 0);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_D << 16 | AF_CHANNEL_LAYOUT_5_0_C:
+        reorder_copy_5ch(dest, src, samples, samplesize, 1, 0, 2, 3, 4);
+        break;
+    // AF_CHANNEL_LAYOUT_5_1_A   L R C LFE Ls Rs
+    // AF_CHANNEL_LAYOUT_5_1_B   L R Ls Rs C LFE
+    // AF_CHANNEL_LAYOUT_5_1_C   L C R Ls Rs LFE
+    // AF_CHANNEL_LAYOUT_5_1_D   C L R Ls Rs LFE
+    // AF_CHANNEL_LAYOUT_5_1_E   LFE L C R Ls Rs
+    case AF_CHANNEL_LAYOUT_5_1_A << 16 | AF_CHANNEL_LAYOUT_5_1_B:
+        reorder_copy_6ch(dest, src, samples, samplesize, 0, 1, 4, 5, 2, 3);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_A << 16 | AF_CHANNEL_LAYOUT_5_1_C:
+        reorder_copy_6ch(dest, src, samples, samplesize, 0, 2, 1, 4, 5, 3);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_A << 16 | AF_CHANNEL_LAYOUT_5_1_D:
+        reorder_copy_6ch(dest, src, samples, samplesize, 2, 0, 1, 4, 5, 3);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_B << 16 | AF_CHANNEL_LAYOUT_5_1_A:
+        reorder_copy_6ch(dest, src, samples, samplesize, 0, 1, 4, 5, 2, 3);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_B << 16 | AF_CHANNEL_LAYOUT_5_1_C:
+        reorder_copy_6ch(dest, src, samples, samplesize, 0, 4, 1, 2, 3, 5);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_B << 16 | AF_CHANNEL_LAYOUT_5_1_D:
+        reorder_copy_6ch(dest, src, samples, samplesize, 4, 0, 1, 2, 3, 5);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_B << 16 | AF_CHANNEL_LAYOUT_5_1_E:
+        reorder_copy_6ch(dest, src, samples, samplesize, 5, 0, 4, 1, 2, 3);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_C << 16 | AF_CHANNEL_LAYOUT_5_1_A:
+        reorder_copy_6ch(dest, src, samples, samplesize, 0, 2, 1, 5, 3, 4);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_C << 16 | AF_CHANNEL_LAYOUT_5_1_B:
+        reorder_copy_6ch(dest, src, samples, samplesize, 0, 2, 3, 4, 1, 5);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_C << 16 | AF_CHANNEL_LAYOUT_5_1_D:
+        reorder_copy_6ch(dest, src, samples, samplesize, 1, 0, 2, 3, 4, 5);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_D << 16 | AF_CHANNEL_LAYOUT_5_1_A:
+        reorder_copy_6ch(dest, src, samples, samplesize, 1, 2, 0, 5, 3, 4);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_D << 16 | AF_CHANNEL_LAYOUT_5_1_B:
+        reorder_copy_6ch(dest, src, samples, samplesize, 1, 2, 3, 4, 0, 5);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_D << 16 | AF_CHANNEL_LAYOUT_5_1_C:
+        reorder_copy_6ch(dest, src, samples, samplesize, 1, 0, 2, 3, 4, 5);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_E << 16 | AF_CHANNEL_LAYOUT_5_1_B:
+        reorder_copy_6ch(dest, src, samples, samplesize, 1, 3, 4, 5, 2, 0);
+        break;
+    default:
+        mp_msg(MSGT_GLOBAL, MSGL_WARN, "[reorder_channel_copy] unsupport "
+               "from %x to %x, %d * %d\n", src_layout, dest_layout,
+               samples, samplesize);
+        fast_memcpy(dest, src, samples*samplesize);
+    }
+}
+
+
+#define REORDER_SELF_SWAP_2(SRC,TMP,SAMPLES,CHNUM,S0,S1) \
+for (i = 0; i < SAMPLES; i += CHNUM) {\
+    TMP = SRC[i+S0];\
+    SRC[i+S0] = SRC[i+S1];\
+    SRC[i+S1] = TMP;\
+}
+
+static int reorder_self_2(void *src, unsigned int samples,
+                          unsigned int samplesize, unsigned int chnum,
+                          int s0, int s1)
+{
+    int i;
+    switch (samplesize) {
+    case 1:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_2(src_8,tmp,samples,6,s0,s1);
+        }
+        else {
+            REORDER_SELF_SWAP_2(src_8,tmp,samples,5,s0,s1);
+        }
+        break;
+    }
+    case 2:
+    {
+        int16_t *src_16 = src;
+        int16_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_2(src_16,tmp,samples,6,s0,s1);
+        }
+        else if (chnum==3) {
+            REORDER_SELF_SWAP_2(src_16,tmp,samples,3,s0,s1);
+        }
+        else {
+            REORDER_SELF_SWAP_2(src_16,tmp,samples,5,s0,s1);
+        }
+        break;
+    }
+    case 3:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp0, tmp1, tmp2;
+        for (i = 0; i < samples; i += chnum*3) {
+            tmp0 = src_8[i+s0*3];
+            tmp1 = src_8[i+s0*3+1];
+            tmp2 = src_8[i+s0*3+2];
+            src_8[i+s0*3]   = src_8[i+s1*3];
+            src_8[i+s0*3+1] = src_8[i+s1*3+1];
+            src_8[i+s0*3+2] = src_8[i+s1*3+2];
+            src_8[i+s1*3]   = tmp0;
+            src_8[i+s1*3+1] = tmp1;
+            src_8[i+s1*3+2] = tmp2;
+        }
+    }
+    case 4:
+    {
+        int32_t *src_32 = src;
+        int32_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_2(src_32,tmp,samples,6,s0,s1);
+        }
+        else if (chnum==3) {
+            REORDER_SELF_SWAP_2(src_32,tmp,samples,3,s0,s1);
+        }
+        else {
+            REORDER_SELF_SWAP_2(src_32,tmp,samples,5,s0,s1);
+        }
+        break;
+    }
+    case 8:
+    {
+        int64_t *src_64 = src;
+        int64_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_2(src_64,tmp,samples,6,s0,s1);
+        }
+        else if (chnum==3) {
+            REORDER_SELF_SWAP_2(src_64,tmp,samples,3,s0,s1);
+        }
+        else {
+            REORDER_SELF_SWAP_2(src_64,tmp,samples,5,s0,s1);
+        }
+        break;
+    }
+    default:
+        mp_msg(MSGT_GLOBAL, MSGL_WARN,
+               "[reorder_ch] Unsupported sample size: %d, please "
+               "report this error on the MPlayer mailing list.\n",samplesize);
+        return 0;
+    }
+    return 1;
+}
+
+#define REORDER_SELF_SWAP_3(SRC,TMP,SAMPLES,CHNUM,S0,S1,S2) \
+for (i = 0; i < SAMPLES; i += CHNUM) {\
+    TMP = SRC[i+S0];\
+    SRC[i+S0] = SRC[i+S1];\
+    SRC[i+S1] = SRC[i+S2];\
+    SRC[i+S2] = TMP;\
+}
+
+static int reorder_self_3(void *src, unsigned int samples,
+                          unsigned int samplesize, unsigned int chnum,
+                          int s0, int s1, int s2)
+{
+    int i;
+    switch (samplesize) {
+    case 1:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_3(src_8,tmp,samples,6,s0,s1,s2);
+        }
+        else {
+            REORDER_SELF_SWAP_3(src_8,tmp,samples,5,s0,s1,s2);
+        }
+        break;
+    }
+    case 2:
+    {
+        int16_t *src_16 = src;
+        int16_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_3(src_16,tmp,samples,6,s0,s1,s2);
+        }
+        else {
+            REORDER_SELF_SWAP_3(src_16,tmp,samples,5,s0,s1,s2);
+        }
+        break;
+    }
+    case 3:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp0, tmp1, tmp2;
+        for (i = 0; i < samples; i += chnum*3) {
+            tmp0 = src_8[i+s0*3];
+            tmp1 = src_8[i+s0*3+1];
+            tmp2 = src_8[i+s0*3+2];
+            src_8[i+s0*3]   = src_8[i+s1*3];
+            src_8[i+s0*3+1] = src_8[i+s1*3+1];
+            src_8[i+s0*3+2] = src_8[i+s1*3+2];
+            src_8[i+s1*3]   = src_8[i+s2*3];
+            src_8[i+s1*3+1] = src_8[i+s2*3+1];
+            src_8[i+s1*3+2] = src_8[i+s2*3+2];
+            src_8[i+s2*3]   = tmp0;
+            src_8[i+s2*3+1] = tmp1;
+            src_8[i+s2*3+2] = tmp2;
+        }
+    }
+    case 4:
+    {
+        int32_t *src_32 = src;
+        int32_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_3(src_32,tmp,samples,6,s0,s1,s2);
+        }
+        else {
+            REORDER_SELF_SWAP_3(src_32,tmp,samples,5,s0,s1,s2);
+        }
+        break;
+    }
+    case 8:
+    {
+        int64_t *src_64 = src;
+        int64_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_3(src_64,tmp,samples,6,s0,s1,s2);
+        }
+        else {
+            REORDER_SELF_SWAP_3(src_64,tmp,samples,5,s0,s1,s2);
+        }
+        break;
+    }
+    default:
+        mp_msg(MSGT_GLOBAL, MSGL_WARN,
+               "[reorder_ch] Unsupported sample size: %d, please "
+               "report this error on the MPlayer mailing list.\n",samplesize);
+        return 0;
+    }
+    return 1;
+}
+
+#define REORDER_SELF_SWAP_4_STEP_1(SRC,TMP,SAMPLES,CHNUM,S0,S1,S2,S3) \
+for (i = 0; i < SAMPLES; i += CHNUM) {\
+    TMP = SRC[i+S0];\
+    SRC[i+S0] = SRC[i+S1];\
+    SRC[i+S1] = SRC[i+S2];\
+    SRC[i+S2] = SRC[i+S3];\
+    SRC[i+S3] = TMP;\
+}
+
+static int reorder_self_4_step_1(void *src, unsigned int samples,
+                                 unsigned int samplesize, unsigned int chnum,
+                                 int s0, int s1, int s2, int s3)
+{
+    int i;
+    switch (samplesize) {
+    case 1:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_4_STEP_1(src_8,tmp,samples,6,s0,s1,s2,s3);
+        }
+        else {
+            REORDER_SELF_SWAP_4_STEP_1(src_8,tmp,samples,5,s0,s1,s2,s3);
+        }
+        break;
+    }
+    case 2:
+    {
+        int16_t *src_16 = src;
+        int16_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_4_STEP_1(src_16,tmp,samples,6,s0,s1,s2,s3);
+        }
+        else {
+            REORDER_SELF_SWAP_4_STEP_1(src_16,tmp,samples,5,s0,s1,s2,s3);
+        }
+        break;
+    }
+    case 3:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp0, tmp1, tmp2;
+        for (i = 0; i < samples; i += chnum*3) {
+            tmp0 = src_8[i+s0*3];
+            tmp1 = src_8[i+s0*3+1];
+            tmp2 = src_8[i+s0*3+2];
+            src_8[i+s0*3]   = src_8[i+s1*3];
+            src_8[i+s0*3+1] = src_8[i+s1*3+1];
+            src_8[i+s0*3+2] = src_8[i+s1*3+2];
+            src_8[i+s1*3]   = src_8[i+s2*3];
+            src_8[i+s1*3+1] = src_8[i+s2*3+1];
+            src_8[i+s1*3+2] = src_8[i+s2*3+2];
+            src_8[i+s2*3]   = src_8[i+s3*3];
+            src_8[i+s2*3+1] = src_8[i+s3*3+1];
+            src_8[i+s2*3+2] = src_8[i+s3*3+2];
+            src_8[i+s3*3]   = tmp0;
+            src_8[i+s3*3+1] = tmp1;
+            src_8[i+s3*3+2] = tmp2;
+        }
+    }
+    case 4:
+    {
+        int32_t *src_32 = src;
+        int32_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_4_STEP_1(src_32,tmp,samples,6,s0,s1,s2,s3);
+        }
+        else {
+            REORDER_SELF_SWAP_4_STEP_1(src_32,tmp,samples,5,s0,s1,s2,s3);
+        }
+        break;
+    }
+    case 8:
+    {
+        int64_t *src_64 = src;
+        int64_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_4_STEP_1(src_64,tmp,samples,6,s0,s1,s2,s3);
+        }
+        else {
+            REORDER_SELF_SWAP_4_STEP_1(src_64,tmp,samples,5,s0,s1,s2,s3);
+        }
+        break;
+    }
+    default:
+        mp_msg(MSGT_GLOBAL, MSGL_WARN,
+               "[reorder_ch] Unsupported sample size: %d, please "
+               "report this error on the MPlayer mailing list.\n",samplesize);
+        return 0;
+    }
+    return 1;
+}
+
+#define REORDER_SELF_SWAP_4_STEP_2(SRC,TMP,SAMPLES,CHNUM,S0,S1,S2,S3) \
+for (i = 0; i < SAMPLES; i += CHNUM) {\
+    TMP = SRC[i+S0];\
+    SRC[i+S0] = SRC[i+S2];\
+    SRC[i+S2] = TMP;\
+    TMP = SRC[i+S1];\
+    SRC[i+S1] = SRC[i+S3];\
+    SRC[i+S3] = TMP;\
+}
+
+static int reorder_self_4_step_2(void *src, unsigned int samples,
+                                 unsigned int samplesize, unsigned int chnum,
+                                 int s0, int s1, int s2, int s3)
+{
+    int i;
+    switch (samplesize) {
+    case 3:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp0, tmp1, tmp2;
+        for (i = 0; i < samples; i += chnum*3) {
+            tmp0 = src_8[i+s0*3];
+            tmp1 = src_8[i+s0*3+1];
+            tmp2 = src_8[i+s0*3+2];
+            src_8[i+s0*3]   = src_8[i+s2*3];
+            src_8[i+s0*3+1] = src_8[i+s2*3+1];
+            src_8[i+s0*3+2] = src_8[i+s2*3+2];
+            src_8[i+s2*3]   = tmp0;
+            src_8[i+s2*3+1] = tmp1;
+            src_8[i+s2*3+2] = tmp2;
+            tmp0 = src_8[i+s1*3];
+            tmp1 = src_8[i+s1*3+1];
+            tmp2 = src_8[i+s1*3+2];
+            src_8[i+s1*3]   = src_8[i+s3*3];
+            src_8[i+s1*3+1] = src_8[i+s3*3+1];
+            src_8[i+s1*3+2] = src_8[i+s3*3+2];
+            src_8[i+s3*3]   = tmp0;
+            src_8[i+s3*3+1] = tmp1;
+            src_8[i+s3*3+2] = tmp2;
+        }
+    }
+    default:
+        mp_msg(MSGT_GLOBAL, MSGL_WARN,
+               "[reorder_ch] Unsupported sample size: %d, please "
+               "report this error on the MPlayer mailing list.\n",samplesize);
+        return 0;
+    }
+    return 1;
+}
+
+#define REORDER_SELF_SWAP_5_STEP_1(SRC,TMP,SAMPLES,CHNUM,S0,S1,S2,S3,S4) \
+for (i = 0; i < SAMPLES; i += CHNUM) {\
+    TMP = SRC[i+S0];\
+    SRC[i+S0] = SRC[i+S1];\
+    SRC[i+S1] = SRC[i+S2];\
+    SRC[i+S2] = SRC[i+S3];\
+    SRC[i+S3] = SRC[i+S4];\
+    SRC[i+S4] = TMP;\
+}
+
+static int reorder_self_5_step_1(void *src, unsigned int samples,
+                                 unsigned int samplesize, unsigned int chnum,
+                                 int s0, int s1, int s2, int s3, int s4)
+{
+    int i;
+    switch (samplesize) {
+    case 1:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_5_STEP_1(src_8,tmp,samples,6,s0,s1,s2,s3,s4);
+        }
+        else {
+            REORDER_SELF_SWAP_5_STEP_1(src_8,tmp,samples,5,s0,s1,s2,s3,s4);
+        }
+        break;
+    }
+    case 2:
+    {
+        int16_t *src_16 = src;
+        int16_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_5_STEP_1(src_16,tmp,samples,6,s0,s1,s2,s3,s4);
+        }
+        else {
+            REORDER_SELF_SWAP_5_STEP_1(src_16,tmp,samples,5,s0,s1,s2,s3,s4);
+        }
+        break;
+    }
+    case 3:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp0, tmp1, tmp2;
+        for (i = 0; i < samples; i += chnum*3) {
+            tmp0 = src_8[i+s0*3];
+            tmp1 = src_8[i+s0*3+1];
+            tmp2 = src_8[i+s0*3+2];
+            src_8[i+s0*3]   = src_8[i+s1*3];
+            src_8[i+s0*3+1] = src_8[i+s1*3+1];
+            src_8[i+s0*3+2] = src_8[i+s1*3+2];
+            src_8[i+s1*3]   = src_8[i+s2*3];
+            src_8[i+s1*3+1] = src_8[i+s2*3+1];
+            src_8[i+s1*3+2] = src_8[i+s2*3+2];
+            src_8[i+s2*3]   = src_8[i+s3*3];
+            src_8[i+s2*3+1] = src_8[i+s3*3+1];
+            src_8[i+s2*3+2] = src_8[i+s3*3+2];
+            src_8[i+s3*3]   = src_8[i+s4*3];
+            src_8[i+s3*3+1] = src_8[i+s4*3+1];
+            src_8[i+s3*3+2] = src_8[i+s4*3+2];
+            src_8[i+s4*3]   = tmp0;
+            src_8[i+s4*3+1] = tmp1;
+            src_8[i+s4*3+2] = tmp2;
+        }
+    }
+    case 4:
+    {
+        int32_t *src_32 = src;
+        int32_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_5_STEP_1(src_32,tmp,samples,6,s0,s1,s2,s3,s4);
+        }
+        else {
+            REORDER_SELF_SWAP_5_STEP_1(src_32,tmp,samples,5,s0,s1,s2,s3,s4);
+        }
+        break;
+    }
+    case 8:
+    {
+        int64_t *src_64 = src;
+        int64_t tmp;
+        if (chnum==6) {
+            REORDER_SELF_SWAP_5_STEP_1(src_64,tmp,samples,6,s0,s1,s2,s3,s4);
+        }
+        else {
+            REORDER_SELF_SWAP_5_STEP_1(src_64,tmp,samples,5,s0,s1,s2,s3,s4);
+        }
+        break;
+    }
+    default:
+        mp_msg(MSGT_GLOBAL, MSGL_WARN,
+               "[reorder_ch] Unsupported sample size: %d, please "
+               "report this error on the MPlayer mailing list.\n",samplesize);
+        return 0;
+    }
+    return 1;
+}
+
+#define REORDER_SELF_SWAP_2_3(SRC,TMP,SAMPLES,CHNUM,S0,S1,S2,S3,S4) \
+for (i = 0; i < SAMPLES; i += CHNUM) {\
+    TMP = SRC[i+S0];\
+    SRC[i+S0] = SRC[i+S1];\
+    SRC[i+S1] = TMP;\
+    TMP = SRC[i+S2];\
+    SRC[i+S2] = SRC[i+S3];\
+    SRC[i+S3] = SRC[i+S4];\
+    SRC[i+S4] = TMP;\
+}
+
+static int reorder_self_2_3(void *src, unsigned int samples,
+                            unsigned int samplesize,
+                            int s0, int s1, int s2, int s3, int s4)
+{
+    int i;
+    switch (samplesize) {
+    case 1:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp;
+        REORDER_SELF_SWAP_2_3(src_8,tmp,samples,6,s0,s1,s2,s3,s4);
+        break;
+    }
+    case 2:
+    {
+        int16_t *src_16 = src;
+        int16_t tmp;
+        REORDER_SELF_SWAP_2_3(src_16,tmp,samples,6,s0,s1,s2,s3,s4);
+        break;
+    }
+    case 3:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp0, tmp1, tmp2;
+        for (i = 0; i < samples; i += 18) {
+            tmp0 = src_8[i+s0*3];
+            tmp1 = src_8[i+s0*3+1];
+            tmp2 = src_8[i+s0*3+2];
+            src_8[i+s0*3]   = src_8[i+s1*3];
+            src_8[i+s0*3+1] = src_8[i+s1*3+1];
+            src_8[i+s0*3+2] = src_8[i+s1*3+2];
+            src_8[i+s1*3]   = tmp0;
+            src_8[i+s1*3+1] = tmp1;
+            src_8[i+s1*3+2] = tmp2;
+            tmp0 = src_8[i+s2*3];
+            tmp1 = src_8[i+s2*3+1];
+            tmp2 = src_8[i+s2*3+2];
+            src_8[i+s2*3]   = src_8[i+s3*3];
+            src_8[i+s2*3+1] = src_8[i+s3*3+1];
+            src_8[i+s2*3+2] = src_8[i+s3*3+2];
+            src_8[i+s3*3]   = src_8[i+s4*3];
+            src_8[i+s3*3+1] = src_8[i+s4*3+1];
+            src_8[i+s3*3+2] = src_8[i+s4*3+2];
+            src_8[i+s4*3]   = tmp0;
+            src_8[i+s4*3+1] = tmp1;
+            src_8[i+s4*3+2] = tmp2;
+        }
+    }
+    case 4:
+    {
+        int32_t *src_32 = src;
+        int32_t tmp;
+        REORDER_SELF_SWAP_2_3(src_32,tmp,samples,6,s0,s1,s2,s3,s4);
+        break;
+    }
+    case 8:
+    {
+        int64_t *src_64 = src;
+        int64_t tmp;
+        REORDER_SELF_SWAP_2_3(src_64,tmp,samples,6,s0,s1,s2,s3,s4);
+        break;
+    }
+    default:
+        mp_msg(MSGT_GLOBAL, MSGL_WARN,
+               "[reorder_ch] Unsupported sample size: %d, please "
+               "report this error on the MPlayer mailing list.\n",samplesize);
+        return 0;
+    }
+    return 1;
+}
+
+#define REORDER_SELF_SWAP_3_3(SRC,TMP,SAMPLES,CHNUM,S0,S1,S2,S3,S4,S5) \
+for (i = 0; i < SAMPLES; i += CHNUM) {\
+    TMP = SRC[i+S0];\
+    SRC[i+S0] = SRC[i+S1];\
+    SRC[i+S1] = SRC[i+S2];\
+    SRC[i+S2] = TMP;\
+    TMP = SRC[i+S3];\
+    SRC[i+S3] = SRC[i+S4];\
+    SRC[i+S4] = SRC[i+S5];\
+    SRC[i+S5] = TMP;\
+}
+
+static int reorder_self_3_3(void *src, unsigned int samples,
+                            unsigned int samplesize,
+                            int s0, int s1, int s2, int s3, int s4, int s5)
+{
+    int i;
+    switch (samplesize) {
+    case 1:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp;
+        REORDER_SELF_SWAP_3_3(src_8,tmp,samples,6,s0,s1,s2,s3,s4,s5);
+        break;
+    }
+    case 2:
+    {
+        int16_t *src_16 = src;
+        int16_t tmp;
+        REORDER_SELF_SWAP_3_3(src_16,tmp,samples,6,s0,s1,s2,s3,s4,s5);
+        break;
+    }
+    case 3:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp0, tmp1, tmp2;
+        for (i = 0; i < samples; i += 18) {
+            tmp0 = src_8[i+s0*3];
+            tmp1 = src_8[i+s0*3+1];
+            tmp2 = src_8[i+s0*3+2];
+            src_8[i+s0*3]   = src_8[i+s1*3];
+            src_8[i+s0*3+1] = src_8[i+s1*3+1];
+            src_8[i+s0*3+2] = src_8[i+s1*3+2];
+            src_8[i+s1*3]   = src_8[i+s2*3];
+            src_8[i+s1*3+1] = src_8[i+s2*3+1];
+            src_8[i+s1*3+2] = src_8[i+s2*3+2];
+            src_8[i+s2*3]   = tmp0;
+            src_8[i+s2*3+1] = tmp1;
+            src_8[i+s2*3+2] = tmp2;
+            tmp0 = src_8[i+s3*3];
+            tmp1 = src_8[i+s3*3+1];
+            tmp2 = src_8[i+s3*3+2];
+            src_8[i+s3*3]   = src_8[i+s4*3];
+            src_8[i+s3*3+1] = src_8[i+s4*3+1];
+            src_8[i+s3*3+2] = src_8[i+s4*3+2];
+            src_8[i+s4*3]   = src_8[i+s5*3];
+            src_8[i+s4*3+1] = src_8[i+s5*3+1];
+            src_8[i+s4*3+2] = src_8[i+s5*3+2];
+            src_8[i+s5*3]   = tmp0;
+            src_8[i+s5*3+1] = tmp1;
+            src_8[i+s5*3+2] = tmp2;
+        }
+    }
+    case 4:
+    {
+        int32_t *src_32 = src;
+        int32_t tmp;
+        REORDER_SELF_SWAP_3_3(src_32,tmp,samples,6,s0,s1,s2,s3,s4,s5);
+        break;
+    }
+    case 8:
+    {
+        int64_t *src_64 = src;
+        int64_t tmp;
+        REORDER_SELF_SWAP_3_3(src_64,tmp,samples,6,s0,s1,s2,s3,s4,s5);
+        break;
+    }
+    default:
+        mp_msg(MSGT_GLOBAL, MSGL_WARN,
+               "[reorder_ch] Unsupported sample size: %d, please "
+               "report this error on the MPlayer mailing list.\n",samplesize);
+        return 0;
+    }
+    return 1;
+}
+
+#define REORDER_SELF_SWAP_2_4(SRC,TMP,SAMPLES,CHNUM,S0,S1,S2,S3,S4,S5) \
+for (i = 0; i < SAMPLES; i += CHNUM) {\
+    TMP = SRC[i+S0];\
+    SRC[i+S0] = SRC[i+S1];\
+    SRC[i+S1] = TMP;\
+    TMP = SRC[i+S2];\
+    SRC[i+S2] = SRC[i+S3];\
+    SRC[i+S3] = SRC[i+S4];\
+    SRC[i+S4] = SRC[i+S5];\
+    SRC[i+S5] = TMP;\
+}
+
+static int reorder_self_2_4(void *src, unsigned int samples,
+                            unsigned int samplesize,
+                            int s0, int s1, int s2, int s3, int s4, int s5)
+{
+    int i;
+    switch (samplesize) {
+    case 1:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp;
+        REORDER_SELF_SWAP_2_4(src_8,tmp,samples,6,s0,s1,s2,s3,s4,s5);
+        break;
+    }
+    case 2:
+    {
+        int16_t *src_16 = src;
+        int16_t tmp;
+        REORDER_SELF_SWAP_2_4(src_16,tmp,samples,6,s0,s1,s2,s3,s4,s5);
+        break;
+    }
+    case 3:
+    {
+        int8_t *src_8 = src;
+        int8_t tmp0, tmp1, tmp2;
+        for (i = 0; i < samples; i += 18) {
+            tmp0 = src_8[i+s0*3];
+            tmp1 = src_8[i+s0*3+1];
+            tmp2 = src_8[i+s0*3+2];
+            src_8[i+s0*3]   = src_8[i+s1*3];
+            src_8[i+s0*3+1] = src_8[i+s1*3+1];
+            src_8[i+s0*3+2] = src_8[i+s1*3+2];
+            src_8[i+s1*3]   = tmp0;
+            src_8[i+s1*3+1] = tmp1;
+            src_8[i+s1*3+2] = tmp2;
+            tmp0 = src_8[i+s2*3];
+            tmp1 = src_8[i+s2*3+1];
+            tmp2 = src_8[i+s2*3+2];
+            src_8[i+s2*3]   = src_8[i+s3*3];
+            src_8[i+s2*3+1] = src_8[i+s3*3+1];
+            src_8[i+s2*3+2] = src_8[i+s3*3+2];
+            src_8[i+s3*3]   = src_8[i+s4*3];
+            src_8[i+s3*3+1] = src_8[i+s4*3+1];
+            src_8[i+s3*3+2] = src_8[i+s4*3+2];
+            src_8[i+s4*3]   = src_8[i+s5*3];
+            src_8[i+s4*3+1] = src_8[i+s5*3+1];
+            src_8[i+s4*3+2] = src_8[i+s5*3+2];
+            src_8[i+s5*3]   = tmp0;
+            src_8[i+s5*3+1] = tmp1;
+            src_8[i+s5*3+2] = tmp2;
+        }
+    }
+    case 4:
+    {
+        int32_t *src_32 = src;
+        int32_t tmp;
+        REORDER_SELF_SWAP_2_4(src_32,tmp,samples,6,s0,s1,s2,s3,s4,s5);
+        break;
+    }
+    case 8:
+    {
+        int64_t *src_64 = src;
+        int64_t tmp;
+        REORDER_SELF_SWAP_2_4(src_64,tmp,samples,6,s0,s1,s2,s3,s4,s5);
+        break;
+    }
+    default:
+        mp_msg(MSGT_GLOBAL, MSGL_WARN,
+               "[reorder_ch] Unsupported sample size: %d, please "
+               "report this error on the MPlayer mailing list.\n",samplesize);
+        return 0;
+    }
+    return 1;
+}
+
+void reorder_channel(void *src,
+                     int src_layout,
+                     int dest_layout,
+                     int samples,
+                     int samplesize)
+{
+    if (dest_layout==src_layout)
+        return;
+    if (!AF_IS_SAME_CH_NUM(dest_layout,src_layout)) {
+        mp_msg(MSGT_GLOBAL, MSGL_WARN,
+               "[reorder_channel] different channel count "
+               "between current and target: %x, %x\n",
+               AF_GET_CH_NUM_WITH_LFE(src_layout),
+               AF_GET_CH_NUM_WITH_LFE(dest_layout));
+        return;
+    }
+    switch ((src_layout<<16)|dest_layout) {
+    // AF_CHANNEL_LAYOUT_5_0_A   L R C Ls Rs
+    // AF_CHANNEL_LAYOUT_5_0_B   L R Ls Rs C
+    // AF_CHANNEL_LAYOUT_5_0_C   L C R Ls Rs
+    // AF_CHANNEL_LAYOUT_5_0_D   C L R Ls Rs
+    case AF_CHANNEL_LAYOUT_5_0_A << 16 | AF_CHANNEL_LAYOUT_5_0_B:
+        reorder_self_3(src, samples, samplesize, 5, 2, 3, 4);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_A << 16 | AF_CHANNEL_LAYOUT_5_0_C:
+        reorder_self_2(src, samples, samplesize, 5, 1, 2);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_A << 16 | AF_CHANNEL_LAYOUT_5_0_D:
+        reorder_self_3(src, samples, samplesize, 5, 2, 1, 0);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_B << 16 | AF_CHANNEL_LAYOUT_5_0_A:
+        reorder_self_3(src, samples, samplesize, 5, 4, 3, 2);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_B << 16 | AF_CHANNEL_LAYOUT_5_0_C:
+        reorder_self_4_step_1(src, samples, samplesize, 5, 4, 3, 2, 1);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_B << 16 | AF_CHANNEL_LAYOUT_5_0_D:
+        reorder_self_5_step_1(src, samples, samplesize, 5, 4, 3, 2, 1, 0);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_C << 16 | AF_CHANNEL_LAYOUT_5_0_A:
+        reorder_self_2(src, samples, samplesize, 5, 1, 2);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_C << 16 | AF_CHANNEL_LAYOUT_5_0_B:
+        reorder_self_4_step_1(src, samples, samplesize, 5, 1, 2, 3, 4);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_C << 16 | AF_CHANNEL_LAYOUT_5_0_D:
+        reorder_self_2(src, samples, samplesize, 5, 0, 1);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_D << 16 | AF_CHANNEL_LAYOUT_5_0_A:
+        reorder_self_3(src, samples, samplesize, 5, 0, 1, 2);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_D << 16 | AF_CHANNEL_LAYOUT_5_0_B:
+        reorder_self_5_step_1(src, samples, samplesize, 5, 0, 1, 2, 3, 4);
+        break;
+    case AF_CHANNEL_LAYOUT_5_0_D << 16 | AF_CHANNEL_LAYOUT_5_0_C:
+        reorder_self_2(src, samples, samplesize, 5, 0, 1);
+        break;
+    // AF_CHANNEL_LAYOUT_5_1_A   L R C LFE Ls Rs
+    // AF_CHANNEL_LAYOUT_5_1_B   L R Ls Rs C LFE
+    // AF_CHANNEL_LAYOUT_5_1_C   L C R Ls Rs LFE
+    // AF_CHANNEL_LAYOUT_5_1_D   C L R Ls Rs LFE
+    // AF_CHANNEL_LAYOUT_5_1_E   LFE L C R Ls Rs
+    case AF_CHANNEL_LAYOUT_5_1_A << 16 | AF_CHANNEL_LAYOUT_5_1_B:
+        if (samplesize != 3)
+            reorder_self_2(src, samples/2, samplesize*2, 3, 1, 2);
+        else
+            reorder_self_4_step_2(src, samples, samplesize, 6, 2, 3, 4, 5);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_A << 16 | AF_CHANNEL_LAYOUT_5_1_C:
+        reorder_self_2_3(src, samples, samplesize, 1, 2, 3, 4, 5);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_A << 16 | AF_CHANNEL_LAYOUT_5_1_D:
+        reorder_self_3_3(src, samples, samplesize, 2, 1, 0, 3, 4, 5);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_B << 16 | AF_CHANNEL_LAYOUT_5_1_A:
+        if (samplesize != 3)
+            reorder_self_2(src, samples/2, samplesize*2, 3, 1, 2);
+        else
+            reorder_self_4_step_2(src, samples, samplesize, 6, 2, 3, 4, 5);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_B << 16 | AF_CHANNEL_LAYOUT_5_1_C:
+        reorder_self_4_step_1(src, samples, samplesize, 6, 4, 3, 2, 1);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_B << 16 | AF_CHANNEL_LAYOUT_5_1_D:
+        reorder_self_5_step_1(src, samples, samplesize, 6, 4, 3, 2, 1, 0);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_B << 16 | AF_CHANNEL_LAYOUT_5_1_E:
+        reorder_self_2_4(src, samples, samplesize, 2, 4, 5, 3, 1, 0);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_C << 16 | AF_CHANNEL_LAYOUT_5_1_A:
+        reorder_self_2_3(src, samples, samplesize, 1, 2, 5, 4, 3);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_C << 16 | AF_CHANNEL_LAYOUT_5_1_B:
+        reorder_self_4_step_1(src, samples, samplesize, 6, 1, 2, 3, 4);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_C << 16 | AF_CHANNEL_LAYOUT_5_1_D:
+        reorder_self_2(src, samples, samplesize, 6, 0, 1);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_D << 16 | AF_CHANNEL_LAYOUT_5_1_A:
+        reorder_self_3_3(src, samples, samplesize, 0, 1, 2, 5, 4, 3);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_D << 16 | AF_CHANNEL_LAYOUT_5_1_B:
+        reorder_self_5_step_1(src, samples, samplesize, 6, 0, 1, 2, 3, 4);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_D << 16 | AF_CHANNEL_LAYOUT_5_1_C:
+        reorder_self_2(src, samples, samplesize, 6, 0, 1);
+        break;
+    case AF_CHANNEL_LAYOUT_5_1_E << 16 | AF_CHANNEL_LAYOUT_5_1_B:
+        reorder_self_2_4(src, samples, samplesize, 2, 4, 0, 1, 3, 5);
+        break;
+    default:
+        mp_msg(MSGT_GLOBAL, MSGL_WARN,
+               "[reorder_channel] unsupported from %x to %x, %d * %d\n",
+               src_layout, dest_layout, samples, samplesize);
+    }
+}
+
+
+static int channel_layout_mapping_5ch[AF_CHANNEL_LAYOUT_SOURCE_NUM] = {
+    AF_CHANNEL_LAYOUT_ALSA_5CH_DEFAULT,
+    AF_CHANNEL_LAYOUT_AAC_5CH_DEFAULT,
+    AF_CHANNEL_LAYOUT_WAVEEX_5CH_DEFAULT,
+    AF_CHANNEL_LAYOUT_LAVC_AC3_5CH_DEFAULT,
+    AF_CHANNEL_LAYOUT_LAVC_LIBA52_5CH_DEFAULT,
+    AF_CHANNEL_LAYOUT_LAVC_DCA_5CH_DEFAULT,
+};
+
+static int channel_layout_mapping_6ch[AF_CHANNEL_LAYOUT_SOURCE_NUM] = {
+    AF_CHANNEL_LAYOUT_ALSA_6CH_DEFAULT,
+    AF_CHANNEL_LAYOUT_AAC_6CH_DEFAULT,
+    AF_CHANNEL_LAYOUT_WAVEEX_6CH_DEFAULT,
+    AF_CHANNEL_LAYOUT_LAVC_AC3_6CH_DEFAULT,
+    AF_CHANNEL_LAYOUT_LAVC_LIBA52_6CH_DEFAULT,
+    AF_CHANNEL_LAYOUT_LAVC_DCA_6CH_DEFAULT,
+};
+
+void reorder_channel_copy_nch(void *src,
+                              int src_layout,
+                              void *dest,
+                              int dest_layout,
+                              int chnum,
+                              int samples,
+                              int samplesize)
+{
+    if (chnum < 5 || chnum > 6 || src_layout < 0 || dest_layout < 0 ||
+            src_layout >= AF_CHANNEL_LAYOUT_SOURCE_NUM ||
+            dest_layout >= AF_CHANNEL_LAYOUT_SOURCE_NUM)
+        fast_memcpy(dest, src, samples*samplesize);
+    else if (chnum == 6)
+        reorder_channel_copy(src, channel_layout_mapping_6ch[src_layout],
+                             dest, channel_layout_mapping_6ch[dest_layout],
+                             samples, samplesize);
+    else
+        reorder_channel_copy(src, channel_layout_mapping_5ch[src_layout],
+                             dest, channel_layout_mapping_5ch[dest_layout],
+                             samples, samplesize);
+}
+
+void reorder_channel_nch(void *buf,
+                         int src_layout,
+                         int dest_layout,
+                         int chnum,
+                         int samples,
+                         int samplesize)
+{
+    if (src_layout == dest_layout || chnum < 5 || chnum > 6 ||
+            src_layout < 0 || dest_layout < 0 ||
+            src_layout >= AF_CHANNEL_LAYOUT_SOURCE_NUM ||
+            dest_layout >= AF_CHANNEL_LAYOUT_SOURCE_NUM ||
+            src_layout == dest_layout)
+        return;
+    if (chnum == 6)
+        reorder_channel(buf, channel_layout_mapping_6ch[src_layout],
+                        channel_layout_mapping_6ch[dest_layout],
+                        samples, samplesize);
+    else
+        reorder_channel(buf, channel_layout_mapping_5ch[src_layout],
+                        channel_layout_mapping_5ch[dest_layout],
+                        samples, samplesize);
+}
+
+
+#ifdef TEST
+
+static void test_copy(int channels) {
+    int samples = 12*1024*1024;
+    int samplesize = 2;
+    int i;
+    unsigned char *bufin = malloc((samples+100)*samplesize);
+    unsigned char *bufout = malloc((samples+100)*samplesize);
+    memset(bufin, 0xFF, samples*samplesize);
+    for (i = 0;i < 100; ++i)
+        reorder_channel_copy(bufin, AF_CHANNEL_LAYOUT_5_1_A,
+                             bufout, AF_CHANNEL_LAYOUT_5_1_B,
+                             samples, samplesize);
+//    reorder_channel(bufin, AF_CHANNEL_LAYOUT_5_1_B,
+//                         AF_CHANNEL_LAYOUT_5_1_D,
+//                         samples, samplesize);
+    free(bufin);
+    free(bufout);
+}
+
+int main(int argc, char *argv[]) {
+    int channels = 6;
+    if (argc > 1)
+        channels = atoi(argv[1]);
+    test_copy(channels);
+    return 0;
+}
+
+#endif
+

Added: trunk/libaf/reorder_ch.h
==============================================================================
--- (empty file)
+++ trunk/libaf/reorder_ch.h	Mon Dec 10 17:53:30 2007
@@ -0,0 +1,125 @@
+/*
+ * common functions for reordering audio channels
+ *
+ * Copyright (C) 2007 Ulion <ulion A gmail P com>
+ *
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with MPlayer; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef REORDER_CH_H
+#define REORDER_CH_H
+
+// L   - Left
+// R   - Right
+// C   - Center
+// Ls  - Left Surround
+// Rs  - Right Surround
+// Cs  - Center Surround
+// Rls - Rear Left Surround
+// Rrs - Rear Right Surround
+
+#define AF_LFE   (1<<7)
+
+#define AF_CHANNEL_LAYOUT_MONO   ((100<<8)|1)
+#define AF_CHANNEL_LAYOUT_STEREO ((101<<8)|2)
+#define AF_CHANNEL_LAYOUT_1_0 AF_CHANNEL_LAYOUT_MONO   // C
+#define AF_CHANNEL_LAYOUT_2_0 AF_CHANNEL_LAYOUT_STEREO // L R
+#define AF_CHANNEL_LAYOUT_2_1   ((102<<8)|3)           // L R LFE
+#define AF_CHANNEL_LAYOUT_3_0_A ((103<<8)|3)           // L R C
+#define AF_CHANNEL_LAYOUT_3_0_B ((104<<8)|3)           // C L R
+#define AF_CHANNEL_LAYOUT_4_0_A ((105<<8)|4)           // L R C Cs
+#define AF_CHANNEL_LAYOUT_4_0_B ((106<<8)|4)           // C L R Cs
+#define AF_CHANNEL_LAYOUT_4_0_C ((107<<8)|4)           // L R Ls Rs
+#define AF_CHANNEL_LAYOUT_5_0_A ((108<<8)|5)           // L R C Ls Rs
+#define AF_CHANNEL_LAYOUT_5_0_B ((109<<8)|5)           // L R Ls Rs C
+#define AF_CHANNEL_LAYOUT_5_0_C ((110<<8)|5)           // L C R Ls Rs
+#define AF_CHANNEL_LAYOUT_5_0_D ((111<<8)|5)           // C L R Ls Rs
+#define AF_CHANNEL_LAYOUT_5_1_A ((112<<8)|6|AF_LFE)    // L R C LFE Ls Rs
+#define AF_CHANNEL_LAYOUT_5_1_B ((113<<8)|6|AF_LFE)    // L R Ls Rs C LFE
+#define AF_CHANNEL_LAYOUT_5_1_C ((114<<8)|6|AF_LFE)    // L C R Ls Rs LFE
+#define AF_CHANNEL_LAYOUT_5_1_D ((115<<8)|6|AF_LFE)    // C L R Ls Rs LFE
+#define AF_CHANNEL_LAYOUT_5_1_E ((116<<8)|6|AF_LFE)    // LFE L C R Ls Rs
+#define AF_CHANNEL_LAYOUT_6_1_A ((117<<8)|7|AF_LFE)    // L R C LFE Ls Rs Cs
+#define AF_CHANNEL_LAYOUT_7_1_A ((118<<8)|8|AF_LFE)   // L R C LFE Ls Rs Rls Rrs
+
+
+#define AF_CHANNEL_LAYOUT_ALSA_5CH_DEFAULT AF_CHANNEL_LAYOUT_5_0_B
+#define AF_CHANNEL_LAYOUT_ALSA_6CH_DEFAULT AF_CHANNEL_LAYOUT_5_1_B
+#define AF_CHANNEL_LAYOUT_MPLAYER_5CH_DEFAULT AF_CHANNEL_LAYOUT_ALSA_5CH_DEFAULT
+#define AF_CHANNEL_LAYOUT_MPLAYER_6CH_DEFAULT AF_CHANNEL_LAYOUT_ALSA_6CH_DEFAULT
+#define AF_CHANNEL_LAYOUT_AAC_5CH_DEFAULT AF_CHANNEL_LAYOUT_5_0_D
+#define AF_CHANNEL_LAYOUT_AAC_6CH_DEFAULT AF_CHANNEL_LAYOUT_5_1_D
+#define AF_CHANNEL_LAYOUT_WAVEEX_5CH_DEFAULT AF_CHANNEL_LAYOUT_5_0_A
+#define AF_CHANNEL_LAYOUT_WAVEEX_6CH_DEFAULT AF_CHANNEL_LAYOUT_5_1_A
+#define AF_CHANNEL_LAYOUT_LAVC_AC3_5CH_DEFAULT AF_CHANNEL_LAYOUT_5_0_C
+#define AF_CHANNEL_LAYOUT_LAVC_AC3_6CH_DEFAULT AF_CHANNEL_LAYOUT_5_1_C
+#define AF_CHANNEL_LAYOUT_LAVC_LIBA52_5CH_DEFAULT AF_CHANNEL_LAYOUT_5_0_C
+#define AF_CHANNEL_LAYOUT_LAVC_LIBA52_6CH_DEFAULT AF_CHANNEL_LAYOUT_5_1_E
+#define AF_CHANNEL_LAYOUT_LAVC_DCA_5CH_DEFAULT AF_CHANNEL_LAYOUT_5_0_D
+#define AF_CHANNEL_LAYOUT_LAVC_DCA_6CH_DEFAULT AF_CHANNEL_LAYOUT_5_1_D
+
+#define AF_CHANNEL_MASK  0xFF
+#define AF_GET_CH_NUM(A) ((A)&0x7F)
+#define AF_GET_CH_NUM_WITH_LFE(A) ((A)&0xFF)
+#define AF_IS_SAME_CH_NUM(A,B) (((A)&0xFF)==((B)&0xFF))
+#define AF_IS_LAYOUT_SPECIFIED(A) ((A)&0xFFFFF800)
+#define AF_IS_LAYOUT_UNSPECIFIED(A) (!AF_IS_LAYOUT_SPECIFIED(A))
+
+/// Optimized channel reorder between channel layouts with same channel number.
+void reorder_channel_copy(void *src,
+                          int src_layout,
+                          void *dest,
+                          int dest_layout,
+                          int samples,
+                          int samplesize);
+
+/// Same with reorder_channel_copy, but works on single buffer.
+void reorder_channel(void *buf,
+                     int src_layout,
+                     int dest_layout,
+                     int samples,
+                     int samplesize);
+
+// Channel layout definitions for different audio sources or targets
+// When specified channel number, they will be map to the specific layouts.
+#define AF_CHANNEL_LAYOUT_ALSA_DEFAULT        0
+#define AF_CHANNEL_LAYOUT_AAC_DEFAULT         1
+#define AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT      2
+#define AF_CHANNEL_LAYOUT_LAVC_AC3_DEFAULT    3
+#define AF_CHANNEL_LAYOUT_LAVC_LIBA52_DEFAULT 4
+#define AF_CHANNEL_LAYOUT_LAVC_DCA_DEFAULT    5
+#define AF_CHANNEL_LAYOUT_SOURCE_NUM          6
+#define AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT AF_CHANNEL_LAYOUT_ALSA_DEFAULT
+
+/// Optimized channel reorder between different audio sources and targets.
+void reorder_channel_copy_nch(void *src,
+                              int src_layout,
+                              void *dest,
+                              int dest_layout,
+                              int chnum,
+                              int samples,
+                              int samplesize);
+
+/// Same with reorder_channel_copy_nch, but works on single buffer.
+void reorder_channel_nch(void *buf,
+                         int src_layout,
+                         int dest_layout,
+                         int chnum,
+                         int samples,
+                         int samplesize);
+
+#endif /* REORDER_CH_H */

Modified: trunk/libao2/ao_pcm.c
==============================================================================
--- trunk/libao2/ao_pcm.c	(original)
+++ trunk/libao2/ao_pcm.c	Mon Dec 10 17:53:30 2007
@@ -8,6 +8,7 @@
 #include "mpbswap.h"
 #include "subopt-helper.h"
 #include "libaf/af_format.h"
+#include "libaf/reorder_ch.h"
 #include "audio_out.h"
 #include "audio_out_internal.h"
 #include "mp_msg.h"
@@ -200,6 +201,15 @@ static int play(void* data,int len,int f
 	}
 #endif 
 
+	if (ao_data.channels == 6 || ao_data.channels == 5) {
+		int frame_size = le2me_16(wavhdr.bits) / 8;
+		len -= len % (frame_size * ao_data.channels);
+		reorder_channel_nch(data, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
+		                    AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT,
+		                    ao_data.channels,
+		                    len / frame_size, frame_size);
+	}
+
 	//printf("PCM: Writing chunk!\n");
 	fwrite(data,len,1,fp);
 

Modified: trunk/libmpcodecs/ad_dmo.c
==============================================================================
--- trunk/libmpcodecs/ad_dmo.c	(original)
+++ trunk/libmpcodecs/ad_dmo.c	Mon Dec 10 17:53:30 2007
@@ -7,6 +7,7 @@
 #include "help_mp.h"
 
 #include "ad_internal.h"
+#include "libaf/reorder_ch.h"
 
 static ad_info_t info = 
 {
@@ -94,6 +95,13 @@ static int decode_audio(sh_audio_t *sh_a
           sh_audio->a_in_buffer_len-=size_in;
           memmove(sh_audio->a_in_buffer,&sh_audio->a_in_buffer[size_in],sh_audio->a_in_buffer_len);
         }
+        if (size_out > 0 && sh_audio->channels >= 5) {
+          reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT,
+                              AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
+                              sh_audio->channels,
+                              size_out / sh_audio->samplesize,
+                              sh_audio->samplesize);
+        }
 //        len=size_out;
   return size_out;
 }

Modified: trunk/libmpcodecs/ad_faad.c
==============================================================================
--- trunk/libmpcodecs/ad_faad.c	(original)
+++ trunk/libmpcodecs/ad_faad.c	Mon Dec 10 17:53:30 2007
@@ -10,6 +10,7 @@
 
 #include "config.h"
 #include "ad_internal.h"
+#include "libaf/reorder_ch.h"
 
 static ad_info_t info = 
 {
@@ -277,6 +278,14 @@ static int decode_audio(sh_audio_t *sh,u
       /* XXX: samples already multiplied by channels! */
       mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"FAAD: Successfully decoded frame (%ld Bytes)!\n",
       sh->samplesize*faac_finfo.samples);
+
+      if (sh->channels >= 5)
+        reorder_channel_copy_nch(faac_sample_buffer,
+                                 AF_CHANNEL_LAYOUT_AAC_DEFAULT,
+                                 buf+len, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
+                                 sh->channels,
+                                 faac_finfo.samples, sh->samplesize);
+      else
       memcpy(buf+len,faac_sample_buffer, sh->samplesize*faac_finfo.samples);
       last_dec_len = sh->samplesize*faac_finfo.samples;
       len += last_dec_len;

Modified: trunk/libmpcodecs/ad_ffmpeg.c
==============================================================================
--- trunk/libmpcodecs/ad_ffmpeg.c	(original)
+++ trunk/libmpcodecs/ad_ffmpeg.c	Mon Dec 10 17:53:30 2007
@@ -7,6 +7,7 @@
 #include "help_mp.h"
 
 #include "ad_internal.h"
+#include "libaf/reorder_ch.h"
 
 #include "mpbswap.h"
 
@@ -166,6 +167,25 @@ static int decode_audio(sh_audio_t *sh_a
 	if(y<0){ mp_msg(MSGT_DECAUDIO,MSGL_V,"lavc_audio: error\n");break; }
 	if(y<x) sh_audio->ds->buffer_pos+=y-x;  // put back data (HACK!)
 	if(len2>0){
+	  if (((AVCodecContext *)sh_audio->context)->channels >= 5) {
+            int src_ch_layout = AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT;
+            const char *codec=((AVCodecContext*)sh_audio->context)->codec->name;
+            if (!strcasecmp(codec, "ac3"))
+              src_ch_layout = AF_CHANNEL_LAYOUT_LAVC_AC3_DEFAULT;
+            else if (!strcasecmp(codec, "dca"))
+              src_ch_layout = AF_CHANNEL_LAYOUT_LAVC_DCA_DEFAULT;
+            else if (!strcasecmp(codec, "libfaad")
+                || !strcasecmp(codec, "mpeg4aac"))
+              src_ch_layout = AF_CHANNEL_LAYOUT_AAC_DEFAULT;
+            else if (!strcasecmp(codec, "liba52"))
+              src_ch_layout = AF_CHANNEL_LAYOUT_LAVC_LIBA52_DEFAULT;
+            else
+              src_ch_layout = AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT;
+            reorder_channel_nch(buf, src_ch_layout,
+                                AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
+                                ((AVCodecContext *)sh_audio->context)->channels,
+                                len2 / 2, 2);
+	  }
 	  //len=len2;break;
 	  if(len<0) len=len2; else len+=len2;
 	  buf+=len2;

Modified: trunk/libmpcodecs/ad_pcm.c
==============================================================================
--- trunk/libmpcodecs/ad_pcm.c	(original)
+++ trunk/libmpcodecs/ad_pcm.c	Mon Dec 10 17:53:30 2007
@@ -5,6 +5,7 @@
 #include "config.h"
 #include "ad_internal.h"
 #include "libaf/af_format.h"
+#include "libaf/reorder_ch.h"
 
 static ad_info_t info = 
 {
@@ -126,5 +127,11 @@ static int decode_audio(sh_audio_t *sh_a
       // based on channels in preinit()
       return -1;
   len=demux_read_data(sh_audio->ds,buf,len);
+  if (len > 0 && sh_audio->channels >= 5) {
+    reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT,
+                        AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
+                        sh_audio->channels,
+                        len / sh_audio->samplesize, sh_audio->samplesize);
+  }
   return len;
 }

Modified: trunk/libmpcodecs/ae_faac.c
==============================================================================
--- trunk/libmpcodecs/ae_faac.c	(original)
+++ trunk/libmpcodecs/ae_faac.c	Mon Dec 10 17:53:30 2007
@@ -8,6 +8,7 @@
 #include "mp_msg.h"
 #include "libmpdemux/aviheader.h"
 #include "libaf/af_format.h"
+#include "libaf/reorder_ch.h"
 #include "libmpdemux/ms_hdr.h"
 #include "stream/stream.h"
 #include "libmpdemux/muxer.h"
@@ -98,6 +99,12 @@ static int get_frame_size(audio_encoder_
 
 static int encode_faac(audio_encoder_t *encoder, uint8_t *dest, void *src, int len, int max_size)
 {
+	if (encoder->params.channels >= 5)
+		reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
+		                    AF_CHANNEL_LAYOUT_AAC_DEFAULT,
+		                    encoder->params.channels,
+		                    len / divisor, divisor);
+
 	// len is divided by the number of bytes per sample
 	enc_frame_size = faacEncEncode(faac,  (int32_t*) src,  len / divisor, dest, max_size);
 	

Modified: trunk/libmpcodecs/ae_lavc.c
==============================================================================
--- trunk/libmpcodecs/ae_lavc.c	(original)
+++ trunk/libmpcodecs/ae_lavc.c	Mon Dec 10 17:53:30 2007
@@ -14,6 +14,7 @@
 #include "help_mp.h"
 #include "config.h"
 #include "libaf/af_format.h"
+#include "libaf/reorder_ch.h"
 #ifdef USE_LIBAVCODEC_SO
 #include <ffmpeg/avcodec.h>
 #else
@@ -111,6 +112,16 @@ static int bind_lavc(audio_encoder_t *en
 static int encode_lavc(audio_encoder_t *encoder, uint8_t *dest, void *src, int size, int max_size)
 {
 	int n;
+	if ((encoder->params.channels == 6 || encoder->params.channels == 5) &&
+			(!strcmp(lavc_acodec->name,"ac3") ||
+			!strcmp(lavc_acodec->name,"libfaac"))) {
+		int isac3 = !strcmp(lavc_acodec->name,"ac3");
+		reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
+		                    isac3 ? AF_CHANNEL_LAYOUT_LAVC_AC3_DEFAULT
+		                          : AF_CHANNEL_LAYOUT_AAC_DEFAULT,
+		                    encoder->params.channels,
+		                    size / 2, 2);
+	}
 	n = avcodec_encode_audio(lavc_actx, dest, size, src);
         compressed_frame_size = n;
 	return n;

Modified: trunk/libmpcodecs/ae_pcm.c
==============================================================================
--- trunk/libmpcodecs/ae_pcm.c	(original)
+++ trunk/libmpcodecs/ae_pcm.c	Mon Dec 10 17:53:30 2007
@@ -8,6 +8,7 @@
 #include "mp_msg.h"
 #include "libmpdemux/aviheader.h"
 #include "libaf/af_format.h"
+#include "libaf/reorder_ch.h"
 #include "libmpdemux/ms_hdr.h"
 #include "stream/stream.h"
 #include "libmpdemux/muxer.h"
@@ -38,6 +39,14 @@ static int bind_pcm(audio_encoder_t *enc
 static int encode_pcm(audio_encoder_t *encoder, uint8_t *dest, void *src, int nsamples, int max_size)
 {
 	max_size = FFMIN(nsamples, max_size);
+	if (encoder->params.channels == 6 || encoder->params.channels == 5) {
+		max_size -= max_size % (encoder->params.channels * 2);
+		reorder_channel_copy_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT,
+		                         dest, AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT,
+		                         encoder->params.channels,
+		                         max_size / 2, 2);
+	}
+	else
 	memcpy(dest, src, max_size);
 	return max_size;
 }



More information about the MPlayer-cvslog mailing list