OSDN Git Service

Add memcpy based on audio_format to audio_utils
authorAndy Hung <hunga@google.com>
Fri, 7 Mar 2014 01:36:40 +0000 (17:36 -0800)
committerAndy Hung <hunga@google.com>
Fri, 7 Mar 2014 02:01:55 +0000 (18:01 -0800)
Use audio_utils/format.h to include them.

Change-Id: I5b9fb0ba3fa09858e0b5af1ffefa3c69d943720f
Signed-off-by: Andy Hung <hunga@google.com>
audio_utils/Android.mk
audio_utils/format.c [new file with mode: 0644]
audio_utils/include/audio_utils/format.h [new file with mode: 0644]

index 9a9d5e8..8c80e46 100644 (file)
@@ -7,6 +7,7 @@ LOCAL_MODULE_TAGS := optional
 
 LOCAL_SRC_FILES:= \
        fixedfft.cpp.arm \
+       format.c \
        primitives.c \
        resampler.c \
        echo_reference.c
@@ -27,6 +28,7 @@ include $(CLEAR_VARS)
 LOCAL_MODULE := libaudioutils
 LOCAL_MODULE_TAGS := optional
 LOCAL_SRC_FILES := \
+       format.c \
        primitives.c
 LOCAL_C_INCLUDES += \
        $(call include-path-for, audio-utils)
diff --git a/audio_utils/format.c b/audio_utils/format.c
new file mode 100644 (file)
index 0000000..fd02fb9
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//#define LOG_NDEBUG 0
+#define LOG_TAG "audio_utils_format"
+
+#include <cutils/log.h>
+#include <audio_utils/primitives.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;
+    }
+    switch (dst_format) {
+    case AUDIO_FORMAT_PCM_16_BIT:
+        switch (src_format) {
+        case AUDIO_FORMAT_PCM_FLOAT:
+            memcpy_to_i16_from_float((int16_t*)dst, (float*)src, count);
+            return;
+        case AUDIO_FORMAT_PCM_24_BIT_PACKED:
+            memcpy_to_i16_from_p24((int16_t*)dst, (uint8_t*)src, count);
+            return;
+        default:
+            break;
+        }
+        break;
+    case AUDIO_FORMAT_PCM_FLOAT:
+        switch (src_format) {
+        case AUDIO_FORMAT_PCM_16_BIT:
+            memcpy_to_float_from_i16((float*)dst, (int16_t*)src, count);
+            return;
+        case AUDIO_FORMAT_PCM_24_BIT_PACKED:
+            memcpy_to_float_from_p24((float*)dst, (uint8_t*)src, count);
+            return;
+        default:
+            break;
+        }
+        break;
+    case AUDIO_FORMAT_PCM_24_BIT_PACKED:
+        switch (src_format) {
+        case AUDIO_FORMAT_PCM_16_BIT:
+            memcpy_to_p24_from_i16((uint8_t*)dst, (int16_t*)src, count);
+            return;
+        case AUDIO_FORMAT_PCM_FLOAT:
+            memcpy_to_p24_from_float((uint8_t*)dst, (float*)src, count);
+            return;
+        default:
+            break;
+        }
+        break;
+    default:
+        break;
+    }
+    LOG_ALWAYS_FATAL("invalid src format %#x for dst format %#x",
+            src_format, dst_format);
+}
diff --git a/audio_utils/include/audio_utils/format.h b/audio_utils/include/audio_utils/format.h
new file mode 100644 (file)
index 0000000..7ae6226
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_FORMAT_H
+#define ANDROID_AUDIO_FORMAT_H
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <system/audio.h>
+
+__BEGIN_DECLS
+
+/* Copy buffers with conversion between buffer sample formats.
+ *
+ *  dst        Destination buffer
+ *  dst_format Destination buffer format
+ *  src        Source buffer
+ *  src_format Source buffer format
+ *  count      Number of samples to copy
+ *
+ * Permitted format types for dst_format and src_format are as follows:
+ * AUDIO_FORMAT_PCM_16_BIT
+ * AUDIO_FORMAT_PCM_24_BIT_PACKED
+ * AUDIO_FORMAT_PCM_FLOAT
+ *
+ * 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.
+ */
+void memcpy_by_audio_format(void *dst, audio_format_t dst_format,
+        void *src, audio_format_t src_format, size_t count);
+
+__END_DECLS
+
+#endif  // ANDROID_AUDIO_FORMAT_H