audio endianness API changes (malc)


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@2042 c046a42c-6fe2-441c-8c8c-71466251a162
diff --git a/audio/alsaaudio.c b/audio/alsaaudio.c
index 2cac396..71e5235 100644
--- a/audio/alsaaudio.c
+++ b/audio/alsaaudio.c
@@ -662,12 +662,9 @@
     obt_as.freq = obt.freq;
     obt_as.nchannels = obt.nchannels;
     obt_as.fmt = effective_fmt;
+    obt_as.endianness = endianness;
 
-    audio_pcm_init_info (
-        &hw->info,
-        &obt_as,
-        audio_need_to_swap_endian (endianness)
-        );
+    audio_pcm_init_info (&hw->info, &obt_as);
     hw->samples = obt.samples;
 
     alsa->pcm_buf = audio_calloc (AUDIO_FUNC, obt.samples, 1 << hw->info.shift);
@@ -751,12 +748,9 @@
     obt_as.freq = obt.freq;
     obt_as.nchannels = obt.nchannels;
     obt_as.fmt = effective_fmt;
+    obt_as.endianness = endianness;
 
-    audio_pcm_init_info (
-        &hw->info,
-        &obt_as,
-        audio_need_to_swap_endian (endianness)
-        );
+    audio_pcm_init_info (&hw->info, &obt_as);
     hw->samples = obt.samples;
 
     alsa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
diff --git a/audio/audio.c b/audio/audio.c
index 0de728c..dd86c4d 100644
--- a/audio/audio.c
+++ b/audio/audio.c
@@ -510,6 +510,18 @@
         AUD_log (NULL, "invalid(%d)", as->fmt);
         break;
     }
+    AUD_log (NULL, "endianness=");
+    switch (as->endianness) {
+    case 0:
+        AUD_log (NULL, "little");
+        break;
+    case 1:
+        AUD_log (NULL, "big");
+        break;
+    default:
+        AUD_log (NULL, "invalid");
+        break;
+    }
     AUD_log (NULL, "\n");
 }
 
@@ -518,6 +530,7 @@
     int invalid;
 
     invalid = as->nchannels != 1 && as->nchannels != 2;
+    invalid |= as->endianness != 0 && as->endianness != 1;
 
     switch (as->fmt) {
     case AUD_FMT_S8:
@@ -531,11 +544,7 @@
     }
 
     invalid |= as->freq <= 0;
-
-    if (invalid) {
-        return -1;
-    }
-    return 0;
+    return invalid ? -1 : 0;
 }
 
 static int audio_pcm_info_eq (struct audio_pcm_info *info, audsettings_t *as)
@@ -557,14 +566,11 @@
     return info->freq == as->freq
         && info->nchannels == as->nchannels
         && info->sign == sign
-        && info->bits == bits;
+        && info->bits == bits
+        && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
 }
 
-void audio_pcm_init_info (
-    struct audio_pcm_info *info,
-    audsettings_t *as,
-    int swap_endian
-    )
+void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as)
 {
     int bits = 8, sign = 0;
 
@@ -588,7 +594,7 @@
     info->shift = (as->nchannels == 2) + (bits == 16);
     info->align = (1 << info->shift) - 1;
     info->bytes_per_second = info->freq << info->shift;
-    info->swap_endian = swap_endian;
+    info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
 }
 
 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
@@ -610,7 +616,7 @@
             int shift = info->nchannels - 1;
             short s = INT16_MAX;
 
-            if (info->swap_endian) {
+            if (info->swap_endianness) {
                 s = bswap16 (s);
             }
 
@@ -635,16 +641,13 @@
 
 static CaptureVoiceOut *audio_pcm_capture_find_specific (
     AudioState *s,
-    audsettings_t *as,
-    int endian
+    audsettings_t *as
     )
 {
     CaptureVoiceOut *cap;
-    int swap_endian = audio_need_to_swap_endian (endian);
 
     for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
-        if ((cap->hw.info.swap_endian == swap_endian)
-            && audio_pcm_info_eq (&cap->hw.info, as)) {
+        if (audio_pcm_info_eq (&cap->hw.info, as)) {
             return cap;
         }
     }
@@ -1697,7 +1700,6 @@
 int AUD_add_capture (
     AudioState *s,
     audsettings_t *as,
-    int endian,
     struct audio_capture_ops *ops,
     void *cb_opaque
     )
@@ -1725,7 +1727,7 @@
     cb->ops = *ops;
     cb->opaque = cb_opaque;
 
-    cap = audio_pcm_capture_find_specific (s, as, endian);
+    cap = audio_pcm_capture_find_specific (s, as);
     if (cap) {
         LIST_INSERT_HEAD (&cap->cb_head, cb, entries);
         return 0;
@@ -1755,7 +1757,7 @@
             goto err2;
         }
 
-        audio_pcm_init_info (&hw->info, as, endian);
+        audio_pcm_init_info (&hw->info, as);
 
         cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
         if (!cap->buf) {
@@ -1768,7 +1770,7 @@
         hw->clip = mixeng_clip
             [hw->info.nchannels == 2]
             [hw->info.sign]
-            [hw->info.swap_endian]
+            [hw->info.swap_endianness]
             [hw->info.bits == 16];
 
         LIST_INSERT_HEAD (&s->cap_head, cap, entries);
diff --git a/audio/audio.h b/audio/audio.h
index 4e1a694..14fa3bc 100644
--- a/audio/audio.h
+++ b/audio/audio.h
@@ -24,6 +24,7 @@
 #ifndef QEMU_AUDIO_H
 #define QEMU_AUDIO_H
 
+#include "config.h"
 #include "sys-queue.h"
 
 typedef void (*audio_callback_fn_t) (void *opaque, int avail);
@@ -35,10 +36,17 @@
     AUD_FMT_S16
 } audfmt_e;
 
+#ifdef WORDS_BIGENDIAN
+#define AUDIO_HOST_ENDIANNESS 1
+#else
+#define AUDIO_HOST_ENDIANNESS 0
+#endif
+
 typedef struct {
     int freq;
     int nchannels;
     audfmt_e fmt;
+    int endianness;
 } audsettings_t;
 
 struct audio_capture_ops {
@@ -74,7 +82,6 @@
 int AUD_add_capture (
     AudioState *s,
     audsettings_t *as,
-    int endian,
     struct audio_capture_ops *ops,
     void *opaque
     );
@@ -85,8 +92,7 @@
     const char *name,
     void *callback_opaque,
     audio_callback_fn_t callback_fn,
-    audsettings_t *settings,
-    int sw_endian
+    audsettings_t *settings
     );
 
 void AUD_close_out (QEMUSoundCard *card, SWVoiceOut *sw);
@@ -104,8 +110,7 @@
     const char *name,
     void *callback_opaque,
     audio_callback_fn_t callback_fn,
-    audsettings_t *settings,
-    int sw_endian
+    audsettings_t *settings
     );
 
 void AUD_close_in (QEMUSoundCard *card, SWVoiceIn *sw);
diff --git a/audio/audio_int.h b/audio/audio_int.h
index c01c16a..f5dcb2c 100644
--- a/audio/audio_int.h
+++ b/audio/audio_int.h
@@ -61,7 +61,7 @@
     int align;
     int shift;
     int bytes_per_second;
-    int swap_endian;
+    int swap_endianness;
 };
 
 typedef struct HWVoiceOut {
@@ -198,8 +198,7 @@
 extern struct audio_driver dsound_audio_driver;
 extern volume_t nominal_volume;
 
-void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as,
-                          int swap_endian);
+void audio_pcm_init_info (struct audio_pcm_info *info, audsettings_t *as);
 void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len);
 
 int  audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int len);
@@ -220,15 +219,6 @@
     return (dst >= src) ? (dst - src) : (len - src + dst);
 }
 
-static inline int audio_need_to_swap_endian (int endianness)
-{
-#ifdef WORDS_BIGENDIAN
-    return endianness != 1;
-#else
-    return endianness != 0;
-#endif
-}
-
 #if defined __GNUC__
 #define GCC_ATTR __attribute__ ((__unused__, __format__ (__printf__, 1, 2)))
 #define INIT_FIELD(f) . f
diff --git a/audio/audio_template.h b/audio/audio_template.h
index 04b3023..04b47be 100644
--- a/audio/audio_template.h
+++ b/audio/audio_template.h
@@ -140,13 +140,12 @@
     SW *sw,
     HW *hw,
     const char *name,
-    audsettings_t *as,
-    int endian
+    audsettings_t *as
     )
 {
     int err;
 
-    audio_pcm_init_info (&sw->info, as, audio_need_to_swap_endian (endian));
+    audio_pcm_init_info (&sw->info, as);
     sw->hw = hw;
     sw->active = 0;
 #ifdef DAC
@@ -164,7 +163,7 @@
 #endif
         [sw->info.nchannels == 2]
         [sw->info.sign]
-        [sw->info.swap_endian]
+        [sw->info.swap_endianness]
         [sw->info.bits == 16];
 
     sw->name = qemu_strdup (name);
@@ -288,7 +287,7 @@
 #endif
         [hw->info.nchannels == 2]
         [hw->info.sign]
-        [hw->info.swap_endian]
+        [hw->info.swap_endianness]
         [hw->info.bits == 16];
 
     if (glue (audio_pcm_hw_alloc_resources_, TYPE) (hw)) {
@@ -336,8 +335,7 @@
 static SW *glue (audio_pcm_create_voice_pair_, TYPE) (
     AudioState *s,
     const char *sw_name,
-    audsettings_t *as,
-    int sw_endian
+    audsettings_t *as
     )
 {
     SW *sw;
@@ -365,7 +363,7 @@
 
     glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw);
 
-    if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as, sw_endian)) {
+    if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) {
         goto err3;
     }
 
@@ -407,8 +405,7 @@
     const char *name,
     void *callback_opaque ,
     audio_callback_fn_t callback_fn,
-    audsettings_t *as,
-    int sw_endian
+    audsettings_t *as
     )
 {
     AudioState *s;
@@ -481,12 +478,12 @@
         }
 
         glue (audio_pcm_sw_fini_, TYPE) (sw);
-        if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as, sw_endian)) {
+        if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) {
             goto fail;
         }
     }
     else {
-        sw = glue (audio_pcm_create_voice_pair_, TYPE) (s, name, as, sw_endian);
+        sw = glue (audio_pcm_create_voice_pair_, TYPE) (s, name, as);
         if (!sw) {
             dolog ("Failed to create voice `%s'\n", name);
             return NULL;
diff --git a/audio/coreaudio.c b/audio/coreaudio.c
index 34e416d..8512f12 100644
--- a/audio/coreaudio.c
+++ b/audio/coreaudio.c
@@ -295,7 +295,6 @@
     UInt32 propertySize;
     int err;
     int bits = 8;
-    int endianess = 0;
     const char *typ = "playback";
     AudioValueRange frameRange;
 
@@ -308,16 +307,9 @@
 
     if (as->fmt == AUD_FMT_S16 || as->fmt == AUD_FMT_U16) {
         bits = 16;
-        endianess = 1;
     }
 
-    audio_pcm_init_info (
-        &hw->info,
-        as,
-        /* Following is irrelevant actually since we do not use
-           mixengs clipping routines */
-        audio_need_to_swap_endian (endianess)
-        );
+    audio_pcm_init_info (&hw->info, as);
 
     /* open default output device */
     propertySize = sizeof(core->outputDeviceID);
diff --git a/audio/dsound_template.h b/audio/dsound_template.h
index 96f7cc7..0896b04 100644
--- a/audio/dsound_template.h
+++ b/audio/dsound_template.h
@@ -250,8 +250,8 @@
     }
 
     ds->first_time = 1;
-
-    audio_pcm_init_info (&hw->info, &obt_as, audio_need_to_swap_endian (0));
+    obt_as.endianness = 0;
+    audio_pcm_init_info (&hw->info, &obt_as);
 
     if (bc.dwBufferBytes & hw->info.align) {
         dolog (
diff --git a/audio/fmodaudio.c b/audio/fmodaudio.c
index 23f2677..5875ba1 100644
--- a/audio/fmodaudio.c
+++ b/audio/fmodaudio.c
@@ -358,6 +358,7 @@
 {
     int bits16, mode, channel;
     FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
+    audsettings_t obt_as = *as;
 
     mode = aud_to_fmodfmt (as->fmt, as->nchannels == 2 ? 1 : 0);
     fmd->fmod_sample = FSOUND_Sample_Alloc (
@@ -384,7 +385,8 @@
     fmd->channel = channel;
 
     /* FMOD always operates on little endian frames? */
-    audio_pcm_init_info (&hw->info, as, audio_need_to_swap_endian (0));
+    obt_as.endianness = 0;
+    audio_pcm_init_info (&hw->info, &obt_as);
     bits16 = (mode & FSOUND_16BITS) != 0;
     hw->samples = conf.nb_samples;
     return 0;
@@ -418,6 +420,7 @@
 {
     int bits16, mode;
     FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
+    audsettings_t obt_as = *as;
 
     if (conf.broken_adc) {
         return -1;
@@ -440,7 +443,8 @@
     }
 
     /* FMOD always operates on little endian frames? */
-    audio_pcm_init_info (&hw->info, as, audio_need_to_swap_endian (0));
+    obt_as.endianness = 0;
+    audio_pcm_init_info (&hw->info, &obt_as);
     bits16 = (mode & FSOUND_16BITS) != 0;
     hw->samples = conf.nb_samples;
     return 0;
diff --git a/audio/noaudio.c b/audio/noaudio.c
index 314f617..8fb15a2 100644
--- a/audio/noaudio.c
+++ b/audio/noaudio.c
@@ -68,7 +68,7 @@
 
 static int no_init_out (HWVoiceOut *hw, audsettings_t *as)
 {
-    audio_pcm_init_info (&hw->info, as, 0);
+    audio_pcm_init_info (&hw->info, as);
     hw->samples = 1024;
     return 0;
 }
@@ -87,7 +87,7 @@
 
 static int no_init_in (HWVoiceIn *hw, audsettings_t *as)
 {
-    audio_pcm_init_info (&hw->info, as, 0);
+    audio_pcm_init_info (&hw->info, as);
     hw->samples = 1024;
     return 0;
 }
diff --git a/audio/ossaudio.c b/audio/ossaudio.c
index 0bdc8ea..125e4c8 100644
--- a/audio/ossaudio.c
+++ b/audio/ossaudio.c
@@ -453,12 +453,9 @@
     obt_as.freq = obt.freq;
     obt_as.nchannels = obt.nchannels;
     obt_as.fmt = effective_fmt;
+    obt_as.endianness = endianness;
 
-    audio_pcm_init_info (
-        &hw->info,
-        &obt_as,
-        audio_need_to_swap_endian (endianness)
-        );
+    audio_pcm_init_info (&hw->info, &obt_as);
     oss->nfrags = obt.nfrags;
     oss->fragsize = obt.fragsize;
 
@@ -597,12 +594,9 @@
     obt_as.freq = obt.freq;
     obt_as.nchannels = obt.nchannels;
     obt_as.fmt = effective_fmt;
+    obt_as.endianness = endianness;
 
-    audio_pcm_init_info (
-        &hw->info,
-        &obt_as,
-        audio_need_to_swap_endian (endianness)
-        );
+    audio_pcm_init_info (&hw->info, &obt_as);
     oss->nfrags = obt.nfrags;
     oss->fragsize = obt.fragsize;
 
diff --git a/audio/sdlaudio.c b/audio/sdlaudio.c
index 9fe2128..f2a6896 100644
--- a/audio/sdlaudio.c
+++ b/audio/sdlaudio.c
@@ -335,12 +335,9 @@
     obt_as.freq = obt.freq;
     obt_as.nchannels = obt.channels;
     obt_as.fmt = effective_fmt;
+    obt_as.endianness = endianess;
 
-    audio_pcm_init_info (
-        &hw->info,
-        &obt_as,
-        audio_need_to_swap_endian (endianess)
-        );
+    audio_pcm_init_info (&hw->info, &obt_as);
     hw->samples = obt.samples;
 
     s->initialized = 1;
diff --git a/audio/wavaudio.c b/audio/wavaudio.c
index ca1e99f..c359fc4 100644
--- a/audio/wavaudio.c
+++ b/audio/wavaudio.c
@@ -135,7 +135,8 @@
 
     hdr[34] = bits16 ? 0x10 : 0x08;
 
-    audio_pcm_init_info (&hw->info, &wav_as, audio_need_to_swap_endian (0));
+    wav_as.endianness = 0;
+    audio_pcm_init_info (&hw->info, &wav_as);
 
     hw->samples = 1024;
     wav->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
diff --git a/audio/wavcapture.c b/audio/wavcapture.c
index 33f04c5..7458a5e 100644
--- a/audio/wavcapture.c
+++ b/audio/wavcapture.c
@@ -70,6 +70,7 @@
     as.freq = freq;
     as.nchannels = 1 << stereo;
     as.fmt = bits16 ? AUD_FMT_S16 : AUD_FMT_U8;
+    as.endianness = 0;
 
     ops.state = wav_state_cb;
     ops.capture = wav_capture_cb;
@@ -97,5 +98,5 @@
     }
 
     qemu_put_buffer (wav->f, hdr, sizeof (hdr));
-    AUD_add_capture (NULL, &as, 0, &ops, wav);
+    AUD_add_capture (NULL, &as, &ops, wav);
 }