OSDN Git Service

Add PCM_32_BIT and PCM_8_24_BIT to format conversion
authorAndy Hung <hunga@google.com>
Tue, 18 Mar 2014 20:13:45 +0000 (13:13 -0700)
committerAndy Hung <hunga@google.com>
Tue, 18 Mar 2014 21:30:17 +0000 (14:30 -0700)
Change-Id: I533f0be84410117d1c31229bb80b8cabda430f99
Signed-off-by: Andy Hung <hunga@google.com>
audio_utils/format.c
audio_utils/include/audio_utils/format.h

index fd02fb9..caa354a 100644 (file)
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-//#define LOG_NDEBUG 0
+/* #define LOG_NDEBUG 0 */
 #define LOG_TAG "audio_utils_format"
 
 #include <cutils/log.h>
 #include <audio_utils/format.h>
 
 void memcpy_by_audio_format(void *dst, audio_format_t dst_format,
-        void *src, audio_format_t src_format, size_t count) {
-    if (dst_format == src_format
-            && (dst_format == AUDIO_FORMAT_PCM_16_BIT
-                    || dst_format == AUDIO_FORMAT_PCM_FLOAT
-                    || dst_format == AUDIO_FORMAT_PCM_24_BIT_PACKED)) {
-        memcpy(dst, src, count * audio_bytes_per_sample(dst_format));
-        return;
+        void *src, audio_format_t src_format, size_t count)
+{
+    /* default cases for error falls through to fatal log below. */
+    if (dst_format == src_format) {
+        switch (dst_format) {
+        case AUDIO_FORMAT_PCM_16_BIT:
+        case AUDIO_FORMAT_PCM_FLOAT:
+        case AUDIO_FORMAT_PCM_24_BIT_PACKED:
+        case AUDIO_FORMAT_PCM_32_BIT:
+        case AUDIO_FORMAT_PCM_8_24_BIT:
+            memcpy(dst, src, count * audio_bytes_per_sample(dst_format));
+            return;
+        default:
+            break;
+        }
     }
     switch (dst_format) {
     case AUDIO_FORMAT_PCM_16_BIT:
@@ -39,6 +47,12 @@ void memcpy_by_audio_format(void *dst, audio_format_t dst_format,
         case AUDIO_FORMAT_PCM_24_BIT_PACKED:
             memcpy_to_i16_from_p24((int16_t*)dst, (uint8_t*)src, count);
             return;
+        case AUDIO_FORMAT_PCM_32_BIT:
+            memcpy_to_i16_from_i32((int16_t*)dst, (int32_t*)src, count);
+            return;
+        case AUDIO_FORMAT_PCM_8_24_BIT:
+            memcpy_to_i16_from_q8_23((int16_t*)dst, (int32_t*)src, count);
+            return;
         default:
             break;
         }
@@ -51,6 +65,12 @@ void memcpy_by_audio_format(void *dst, audio_format_t dst_format,
         case AUDIO_FORMAT_PCM_24_BIT_PACKED:
             memcpy_to_float_from_p24((float*)dst, (uint8_t*)src, count);
             return;
+        case AUDIO_FORMAT_PCM_32_BIT:
+            memcpy_to_float_from_i32((float*)dst, (int32_t*)src, count);
+            return;
+        case AUDIO_FORMAT_PCM_8_24_BIT:
+            memcpy_to_float_from_q8_23((float*)dst, (int32_t*)src, count);
+            return;
         default:
             break;
         }
@@ -67,6 +87,30 @@ void memcpy_by_audio_format(void *dst, audio_format_t dst_format,
             break;
         }
         break;
+    case AUDIO_FORMAT_PCM_32_BIT:
+        switch (src_format) {
+        case AUDIO_FORMAT_PCM_16_BIT:
+            memcpy_to_i32_from_i16((int32_t*)dst, (int16_t*)src, count);
+            return;
+        case AUDIO_FORMAT_PCM_FLOAT:
+            memcpy_to_i32_from_float((int32_t*)dst, (float*)src, count);
+            return;
+        default:
+            break;
+        }
+        break;
+    case AUDIO_FORMAT_PCM_8_24_BIT:
+        switch (src_format) {
+        case AUDIO_FORMAT_PCM_16_BIT:
+            memcpy_to_q8_23_from_i16((int32_t*)dst, (int16_t*)src, count);
+            return;
+        case AUDIO_FORMAT_PCM_FLOAT:
+            memcpy_to_q8_23_from_float_with_clamp((int32_t*)dst, (float*)src, count);
+            return;
+        default:
+            break;
+        }
+        break;
     default:
         break;
     }
index 7ae6226..9a68fd2 100644 (file)
@@ -31,16 +31,25 @@ __BEGIN_DECLS
  *  src_format Source buffer format
  *  count      Number of samples to copy
  *
- * Permitted format types for dst_format and src_format are as follows:
+ * Allowed format conversions are given by either case 1 or 2 below:
+ *
+ * 1) One of src_format or dst_format is AUDIO_FORMAT_PCM_16_BIT or
+ * AUDIO_FORMAT_PCM_FLOAT, and the other format type is one of:
+ *
  * AUDIO_FORMAT_PCM_16_BIT
- * AUDIO_FORMAT_PCM_24_BIT_PACKED
  * AUDIO_FORMAT_PCM_FLOAT
+ * AUDIO_FORMAT_PCM_24_BIT_PACKED
+ * AUDIO_FORMAT_PCM_32_BIT
+ * AUDIO_FORMAT_PCM_8_24_BIT
+ *
+ * 2) Both dst_format and src_format are identical and of the list given
+ * in (1). This is a straight copy.
  *
  * The destination and source buffers must be completely separate if the destination
  * format size is larger than the source format size. These routines call functions
  * in primitives.h, so descriptions of detailed behavior can be reviewed there.
  *
- * Logs a fatal error if dst or src format is not one of the permitted types.
+ * Logs a fatal error if dst or src format is not allowed by the conversion rules above.
  */
 void memcpy_by_audio_format(void *dst, audio_format_t dst_format,
         void *src, audio_format_t src_format, size_t count);