OSDN Git Service

Introduce bswap.h for portable definitions of byte swap macros.
[android-x86/external-alsa-lib.git] / src / pcm / pcm_misc.c
index dddeff4..7d2b05d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  PCM Interface - misc routines
- *  Copyright (c) 1998 by Jaroslav Kysela <perex@suse.cz>
+ *  Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
  *
  *
  *   This library is free software; you can redistribute it and/or modify
@@ -23,7 +23,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
-#include <byteswap.h>
+#include "bswap.h"
 #include "pcm_local.h"
 
 
@@ -62,6 +62,11 @@ int snd_pcm_format_signed(snd_pcm_format_t format)
        case SNDRV_PCM_FORMAT_U20_3BE:
        case SNDRV_PCM_FORMAT_U18_3LE:
        case SNDRV_PCM_FORMAT_U18_3BE:
+       case SNDRV_PCM_FORMAT_DSD_U8:
+       case SNDRV_PCM_FORMAT_DSD_U16_LE:
+       case SNDRV_PCM_FORMAT_DSD_U32_LE:
+       case SNDRV_PCM_FORMAT_DSD_U16_BE:
+       case SNDRV_PCM_FORMAT_DSD_U32_BE:
                return 0;
        default:
                return -EINVAL;
@@ -134,6 +139,8 @@ int snd_pcm_format_little_endian(snd_pcm_format_t format)
        case SNDRV_PCM_FORMAT_U24_3LE:
        case SNDRV_PCM_FORMAT_U20_3LE:
        case SNDRV_PCM_FORMAT_U18_3LE:
+       case SNDRV_PCM_FORMAT_DSD_U16_LE:
+       case SNDRV_PCM_FORMAT_DSD_U32_LE:
                return 1;
        case SNDRV_PCM_FORMAT_S16_BE:
        case SNDRV_PCM_FORMAT_U16_BE:
@@ -150,6 +157,8 @@ int snd_pcm_format_little_endian(snd_pcm_format_t format)
        case SNDRV_PCM_FORMAT_U24_3BE:
        case SNDRV_PCM_FORMAT_U20_3BE:
        case SNDRV_PCM_FORMAT_U18_3BE:
+       case SNDRV_PCM_FORMAT_DSD_U16_BE:
+       case SNDRV_PCM_FORMAT_DSD_U32_BE:
                return 0;
        default:
                return -EINVAL;
@@ -195,11 +204,14 @@ int snd_pcm_format_width(snd_pcm_format_t format)
        switch (format) {
        case SNDRV_PCM_FORMAT_S8:
        case SNDRV_PCM_FORMAT_U8:
+       case SNDRV_PCM_FORMAT_DSD_U8:
                return 8;
        case SNDRV_PCM_FORMAT_S16_LE:
        case SNDRV_PCM_FORMAT_S16_BE:
        case SNDRV_PCM_FORMAT_U16_LE:
        case SNDRV_PCM_FORMAT_U16_BE:
+       case SNDRV_PCM_FORMAT_DSD_U16_LE:
+       case SNDRV_PCM_FORMAT_DSD_U16_BE:
                return 16;
        case SNDRV_PCM_FORMAT_S18_3LE:
        case SNDRV_PCM_FORMAT_S18_3BE:
@@ -226,13 +238,15 @@ int snd_pcm_format_width(snd_pcm_format_t format)
        case SNDRV_PCM_FORMAT_U32_BE:
        case SNDRV_PCM_FORMAT_FLOAT_LE:
        case SNDRV_PCM_FORMAT_FLOAT_BE:
+       case SNDRV_PCM_FORMAT_DSD_U32_LE:
+       case SNDRV_PCM_FORMAT_DSD_U32_BE:
                return 32;
        case SNDRV_PCM_FORMAT_FLOAT64_LE:
        case SNDRV_PCM_FORMAT_FLOAT64_BE:
                return 64;
        case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE:
        case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE:
-               return 24;
+               return 32;
        case SNDRV_PCM_FORMAT_MU_LAW:
        case SNDRV_PCM_FORMAT_A_LAW:
                return 8;
@@ -253,11 +267,14 @@ int snd_pcm_format_physical_width(snd_pcm_format_t format)
        switch (format) {
        case SNDRV_PCM_FORMAT_S8:
        case SNDRV_PCM_FORMAT_U8:
+       case SNDRV_PCM_FORMAT_DSD_U8:
                return 8;
        case SNDRV_PCM_FORMAT_S16_LE:
        case SNDRV_PCM_FORMAT_S16_BE:
        case SNDRV_PCM_FORMAT_U16_LE:
        case SNDRV_PCM_FORMAT_U16_BE:
+       case SNDRV_PCM_FORMAT_DSD_U16_LE:
+       case SNDRV_PCM_FORMAT_DSD_U16_BE:
                return 16;
        case SNDRV_PCM_FORMAT_S18_3LE:
        case SNDRV_PCM_FORMAT_S18_3BE:
@@ -284,6 +301,8 @@ int snd_pcm_format_physical_width(snd_pcm_format_t format)
        case SNDRV_PCM_FORMAT_FLOAT_BE:
        case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE:
        case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE:
+       case SNDRV_PCM_FORMAT_DSD_U32_LE:
+       case SNDRV_PCM_FORMAT_DSD_U32_BE:
                return 32;
        case SNDRV_PCM_FORMAT_FLOAT64_LE:
        case SNDRV_PCM_FORMAT_FLOAT64_BE:
@@ -309,11 +328,14 @@ ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
        switch (format) {
        case SNDRV_PCM_FORMAT_S8:
        case SNDRV_PCM_FORMAT_U8:
+       case SNDRV_PCM_FORMAT_DSD_U8:
                return samples;
        case SNDRV_PCM_FORMAT_S16_LE:
        case SNDRV_PCM_FORMAT_S16_BE:
        case SNDRV_PCM_FORMAT_U16_LE:
        case SNDRV_PCM_FORMAT_U16_BE:
+       case SNDRV_PCM_FORMAT_DSD_U16_LE:
+       case SNDRV_PCM_FORMAT_DSD_U16_BE:
                return samples * 2;
        case SNDRV_PCM_FORMAT_S18_3LE:
        case SNDRV_PCM_FORMAT_S18_3BE:
@@ -338,6 +360,8 @@ ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples)
        case SNDRV_PCM_FORMAT_U32_BE:
        case SNDRV_PCM_FORMAT_FLOAT_LE:
        case SNDRV_PCM_FORMAT_FLOAT_BE:
+       case SNDRV_PCM_FORMAT_DSD_U32_LE:
+       case SNDRV_PCM_FORMAT_DSD_U32_BE:
                return samples * 4;
        case SNDRV_PCM_FORMAT_FLOAT64_LE:
        case SNDRV_PCM_FORMAT_FLOAT64_BE:
@@ -382,6 +406,12 @@ u_int64_t snd_pcm_format_silence_64(snd_pcm_format_t format)
                return 0;
        case SNDRV_PCM_FORMAT_U8:
                return 0x8080808080808080ULL;
+       case SNDRV_PCM_FORMAT_DSD_U8:
+       case SNDRV_PCM_FORMAT_DSD_U16_LE:
+       case SNDRV_PCM_FORMAT_DSD_U32_LE:
+       case SNDRV_PCM_FORMAT_DSD_U16_BE:
+       case SNDRV_PCM_FORMAT_DSD_U32_BE:
+               return 0x6969696969696969ULL;
 #ifdef SNDRV_LITTLE_ENDIAN
        case SNDRV_PCM_FORMAT_U16_LE:
                return 0x8000800080008000ULL;
@@ -541,7 +571,8 @@ u_int8_t snd_pcm_format_silence(snd_pcm_format_t format)
  * \brief Silence a PCM samples buffer
  * \param format Sample format
  * \param data Buffer
- * \return samples Samples count
+ * \param samples Samples count
+ * \return 0 if successful or a negative error code
  */
 int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int samples)
 {
@@ -564,28 +595,30 @@ int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int
        }
        case 16: {
                u_int16_t silence = snd_pcm_format_silence_64(format);
+               u_int16_t *pdata = (u_int16_t *)data;
                if (! silence)
                        memset(data, 0, samples * 2);
                else {
                        while (samples-- > 0)
-                               *((u_int16_t *)data)++ = silence;
+                               *pdata++ = silence;
                }
                break;
        }
        case 24: {
                u_int32_t silence = snd_pcm_format_silence_64(format);
+               u_int8_t *pdata = (u_int8_t *)data;
                if (! silence)
                        memset(data, 0, samples * 3);
                else {
                        while (samples-- > 0) {
 #ifdef SNDRV_LITTLE_ENDIAN
-                               *((u_int8_t *)data)++ = silence >> 0;
-                               *((u_int8_t *)data)++ = silence >> 8;
-                               *((u_int8_t *)data)++ = silence >> 16;
+                               *pdata++ = silence >> 0;
+                               *pdata++ = silence >> 8;
+                               *pdata++ = silence >> 16;
 #else
-                               *((u_int8_t *)data)++ = silence >> 16;
-                               *((u_int8_t *)data)++ = silence >> 8;
-                               *((u_int8_t *)data)++ = silence >> 0;
+                               *pdata++ = silence >> 16;
+                               *pdata++ = silence >> 8;
+                               *pdata++ = silence >> 0;
 #endif
                        }
                }
@@ -593,21 +626,23 @@ int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int
        }
        case 32: {
                u_int32_t silence = snd_pcm_format_silence_64(format);
+               u_int32_t *pdata = (u_int32_t *)data;
                if (! silence)
                        memset(data, 0, samples * 4);
                else {
                        while (samples-- > 0)
-                               *((u_int32_t *)data)++ = silence;
+                               *pdata++ = silence;
                }
                break;
        }
        case 64: {
                u_int64_t silence = snd_pcm_format_silence_64(format);
+               u_int64_t *pdata = (u_int64_t *)data;
                if (! silence)
                        memset(data, 0, samples * 8);
                else {
                        while (samples-- > 0)
-                               *((u_int64_t *)data)++ = silence;
+                               *pdata++ = silence;
                }
                break;
        }
@@ -618,38 +653,24 @@ int snd_pcm_format_set_silence(snd_pcm_format_t format, void *data, unsigned int
        return 0;
 }
 
-static int linear_formats[4*2*2] = {
-       SNDRV_PCM_FORMAT_S8,
-       SNDRV_PCM_FORMAT_S8,
-       SNDRV_PCM_FORMAT_U8,
-       SNDRV_PCM_FORMAT_U8,
-       SNDRV_PCM_FORMAT_S16_LE,
-       SNDRV_PCM_FORMAT_S16_BE,
-       SNDRV_PCM_FORMAT_U16_LE,
-       SNDRV_PCM_FORMAT_U16_BE,
-       SNDRV_PCM_FORMAT_S24_LE,
-       SNDRV_PCM_FORMAT_S24_BE,
-       SNDRV_PCM_FORMAT_U24_LE,
-       SNDRV_PCM_FORMAT_U24_BE,
-       SNDRV_PCM_FORMAT_S32_LE,
-       SNDRV_PCM_FORMAT_S32_BE,
-       SNDRV_PCM_FORMAT_U32_LE,
-       SNDRV_PCM_FORMAT_U32_BE
+static const int linear_formats[4][2][2] = {
+       { { SNDRV_PCM_FORMAT_S8, SNDRV_PCM_FORMAT_S8 },
+         { SNDRV_PCM_FORMAT_U8, SNDRV_PCM_FORMAT_U8 } },
+       { { SNDRV_PCM_FORMAT_S16_LE, SNDRV_PCM_FORMAT_S16_BE },
+         { SNDRV_PCM_FORMAT_U16_LE, SNDRV_PCM_FORMAT_U16_BE } },
+       { { SNDRV_PCM_FORMAT_S24_LE, SNDRV_PCM_FORMAT_S24_BE },
+         { SNDRV_PCM_FORMAT_U24_LE, SNDRV_PCM_FORMAT_U24_BE } },
+       { { SNDRV_PCM_FORMAT_S32_LE, SNDRV_PCM_FORMAT_S32_BE },
+         { SNDRV_PCM_FORMAT_U32_LE, SNDRV_PCM_FORMAT_U32_BE } }
 };
 
-static int linear24_formats[3*2*2] = {
-       SNDRV_PCM_FORMAT_S24_3LE,
-       SNDRV_PCM_FORMAT_S24_3BE,
-       SNDRV_PCM_FORMAT_U24_3LE,
-       SNDRV_PCM_FORMAT_U24_3BE,
-       SNDRV_PCM_FORMAT_S20_3LE,
-       SNDRV_PCM_FORMAT_S20_3BE,
-       SNDRV_PCM_FORMAT_U20_3LE,
-       SNDRV_PCM_FORMAT_U20_3BE,
-       SNDRV_PCM_FORMAT_S18_3LE,
-       SNDRV_PCM_FORMAT_S18_3BE,
-       SNDRV_PCM_FORMAT_U18_3LE,
-       SNDRV_PCM_FORMAT_U18_3BE,
+static const int linear24_formats[3][2][2] = {
+       { { SNDRV_PCM_FORMAT_S24_3LE, SNDRV_PCM_FORMAT_S24_3BE },
+         { SNDRV_PCM_FORMAT_U24_3LE, SNDRV_PCM_FORMAT_U24_3BE } },
+       { { SNDRV_PCM_FORMAT_S20_3LE, SNDRV_PCM_FORMAT_S20_3BE },
+         { SNDRV_PCM_FORMAT_U20_3LE, SNDRV_PCM_FORMAT_U20_3BE } },
+       { { SNDRV_PCM_FORMAT_S18_3LE, SNDRV_PCM_FORMAT_S18_3BE },
+         { SNDRV_PCM_FORMAT_U18_3LE, SNDRV_PCM_FORMAT_U18_3BE } },
 };
 
 /**
@@ -657,7 +678,8 @@ static int linear24_formats[3*2*2] = {
  * \param width Nominal bits per sample
  * \param pwidth Physical bit width of the format
  * \param unsignd Sign: 0 signed, 1 unsigned
- * \return big_endian Endian: 0 little endian, 1 big endian
+ * \param big_endian Endian: 0 little endian, 1 big endian
+ * \return The matching format type, or #SND_PCM_FORMAT_UNKNOWN if no match
  */
 snd_pcm_format_t snd_pcm_build_linear_format(int width, int pwidth, int unsignd, int big_endian)
 {
@@ -675,7 +697,7 @@ snd_pcm_format_t snd_pcm_build_linear_format(int width, int pwidth, int unsignd,
                default:
                        return SND_PCM_FORMAT_UNKNOWN;
                }
-               return ((int(*)[2][2])linear24_formats)[width][!!unsignd][!!big_endian];
+               return linear24_formats[width][!!unsignd][!!big_endian];
        } else {
                switch (width) {
                case 8:
@@ -693,6 +715,149 @@ snd_pcm_format_t snd_pcm_build_linear_format(int width, int pwidth, int unsignd,
                default:
                        return SND_PCM_FORMAT_UNKNOWN;
                }
-               return ((int(*)[2][2])linear_formats)[width][!!unsignd][!!big_endian];
+               return linear_formats[width][!!unsignd][!!big_endian];
+       }
+}
+
+/**
+ * \brief Parse control element id from the config
+ * \param conf the config tree to parse
+ * \param ctl_id the pointer to store the resultant control element id
+ * \param cardp the pointer to store the card index
+ * \param cchannelsp the pointer to store the number of channels (optional)
+ * \param hwctlp the pointer to store the h/w control flag (optional)
+ * \return 0 if successful, or a negative error code
+ *
+ * This function parses the given config tree to retrieve the control element id
+ * and the card index.  It's used by softvol.  External PCM plugins can use this
+ * function for creating or assigining their controls.
+ *
+ * cchannelsp and hwctlp arguments are optional.  Set NULL if not necessary.
+ */
+int snd_pcm_parse_control_id(snd_config_t *conf, snd_ctl_elem_id_t *ctl_id, int *cardp,
+                            int *cchannelsp, int *hwctlp)
+{
+       snd_config_iterator_t i, next;
+       int iface = SND_CTL_ELEM_IFACE_MIXER;
+       const char *name = NULL;
+       long index = 0;
+       long device = -1;
+       long subdevice = -1;
+       int err;
+
+       assert(ctl_id && cardp);
+
+       *cardp = -1;
+       if (cchannelsp)
+               *cchannelsp = 2;
+       snd_config_for_each(i, next, conf) {
+               snd_config_t *n = snd_config_iterator_entry(i);
+               const char *id;
+               if (snd_config_get_id(n, &id) < 0)
+                       continue;
+               if (strcmp(id, "comment") == 0)
+                       continue;
+               if (strcmp(id, "card") == 0) {
+                       const char *str;
+                       long v;
+                       if ((err = snd_config_get_integer(n, &v)) < 0) {
+                               if ((err = snd_config_get_string(n, &str)) < 0) {
+                                       SNDERR("Invalid field %s", id);
+                                       goto _err;
+                               }
+                               *cardp = snd_card_get_index(str);
+                               if (*cardp < 0) {
+                                       SNDERR("Cannot get index for %s", str);
+                                       err = *cardp;
+                                       goto _err;
+                               }
+                       } else
+                               *cardp = v;
+                       continue;
+               }
+               if (strcmp(id, "iface") == 0 || strcmp(id, "interface") == 0) {
+                       const char *ptr;
+                       if ((err = snd_config_get_string(n, &ptr)) < 0) {
+                               SNDERR("field %s is not a string", id);
+                               goto _err;
+                       }
+                       if ((err = snd_config_get_ctl_iface_ascii(ptr)) < 0) {
+                               SNDERR("Invalid value for '%s'", id);
+                               goto _err;
+                       }
+                       iface = err;
+                       continue;
+               }
+               if (strcmp(id, "name") == 0) {
+                       if ((err = snd_config_get_string(n, &name)) < 0) {
+                               SNDERR("field %s is not a string", id);
+                               goto _err;
+                       }
+                       continue;
+               }
+               if (strcmp(id, "index") == 0) {
+                       if ((err = snd_config_get_integer(n, &index)) < 0) {
+                               SNDERR("field %s is not an integer", id);
+                               goto _err;
+                       }
+                       continue;
+               }
+               if (strcmp(id, "device") == 0) {
+                       if ((err = snd_config_get_integer(n, &device)) < 0) {
+                               SNDERR("field %s is not an integer", id);
+                               goto _err;
+                       }
+                       continue;
+               }
+               if (strcmp(id, "subdevice") == 0) {
+                       if ((err = snd_config_get_integer(n, &subdevice)) < 0) {
+                               SNDERR("field %s is not an integer", id);
+                               goto _err;
+                       }
+                       continue;
+               }
+               if (cchannelsp && strcmp(id, "count") == 0) {
+                       long v;
+                       if ((err = snd_config_get_integer(n, &v)) < 0) {
+                               SNDERR("field %s is not an integer", id);
+                               goto _err;
+                       }
+                       if (v < 1 || v > 2) {
+                               SNDERR("Invalid count %ld", v);
+                               goto _err;
+                       }
+                       *cchannelsp = v;
+                       continue;
+               }
+               if (hwctlp && strcmp(id, "hwctl") == 0) {
+                       if ((err = snd_config_get_bool(n)) < 0) {
+                               SNDERR("The field %s must be a boolean type", id);
+                               return err;
+                       }
+                       *hwctlp = err;
+                       continue;
+               }
+               SNDERR("Unknown field %s", id);
+               return -EINVAL;
+       }
+       if (name == NULL) {
+               SNDERR("Missing control name");
+               err = -EINVAL;
+               goto _err;
        }
+       if (device < 0)
+               device = 0;
+       if (subdevice < 0)
+               subdevice = 0;
+
+       snd_ctl_elem_id_set_interface(ctl_id, iface);
+       snd_ctl_elem_id_set_name(ctl_id, name);
+       snd_ctl_elem_id_set_index(ctl_id, index);
+       snd_ctl_elem_id_set_device(ctl_id, device);
+       snd_ctl_elem_id_set_subdevice(ctl_id, subdevice);
+
+       return 0;
+
+ _err:
+       return err;
 }