2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* #define LOG_NDEBUG 0 */
18 #define LOG_TAG "audio_utils_format"
20 #include <cutils/log.h>
21 #include <audio_utils/primitives.h>
22 #include <audio_utils/format.h>
24 void memcpy_by_audio_format(void *dst, audio_format_t dst_format,
25 const void *src, audio_format_t src_format, size_t count)
27 /* default cases for error falls through to fatal log below. */
28 if (dst_format == src_format) {
30 case AUDIO_FORMAT_PCM_16_BIT:
31 case AUDIO_FORMAT_PCM_FLOAT:
32 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
33 case AUDIO_FORMAT_PCM_32_BIT:
34 case AUDIO_FORMAT_PCM_8_24_BIT:
35 memcpy(dst, src, count * audio_bytes_per_sample(dst_format));
42 case AUDIO_FORMAT_PCM_16_BIT:
44 case AUDIO_FORMAT_PCM_FLOAT:
45 memcpy_to_i16_from_float((int16_t*)dst, (float*)src, count);
47 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
48 memcpy_to_i16_from_p24((int16_t*)dst, (uint8_t*)src, count);
50 case AUDIO_FORMAT_PCM_32_BIT:
51 memcpy_to_i16_from_i32((int16_t*)dst, (int32_t*)src, count);
53 case AUDIO_FORMAT_PCM_8_24_BIT:
54 memcpy_to_i16_from_q8_23((int16_t*)dst, (int32_t*)src, count);
60 case AUDIO_FORMAT_PCM_FLOAT:
62 case AUDIO_FORMAT_PCM_16_BIT:
63 memcpy_to_float_from_i16((float*)dst, (int16_t*)src, count);
65 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
66 memcpy_to_float_from_p24((float*)dst, (uint8_t*)src, count);
68 case AUDIO_FORMAT_PCM_32_BIT:
69 memcpy_to_float_from_i32((float*)dst, (int32_t*)src, count);
71 case AUDIO_FORMAT_PCM_8_24_BIT:
72 memcpy_to_float_from_q8_23((float*)dst, (int32_t*)src, count);
78 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
80 case AUDIO_FORMAT_PCM_16_BIT:
81 memcpy_to_p24_from_i16((uint8_t*)dst, (int16_t*)src, count);
83 case AUDIO_FORMAT_PCM_FLOAT:
84 memcpy_to_p24_from_float((uint8_t*)dst, (float*)src, count);
90 case AUDIO_FORMAT_PCM_32_BIT:
92 case AUDIO_FORMAT_PCM_16_BIT:
93 memcpy_to_i32_from_i16((int32_t*)dst, (int16_t*)src, count);
95 case AUDIO_FORMAT_PCM_FLOAT:
96 memcpy_to_i32_from_float((int32_t*)dst, (float*)src, count);
102 case AUDIO_FORMAT_PCM_8_24_BIT:
103 switch (src_format) {
104 case AUDIO_FORMAT_PCM_16_BIT:
105 memcpy_to_q8_23_from_i16((int32_t*)dst, (int16_t*)src, count);
107 case AUDIO_FORMAT_PCM_FLOAT:
108 memcpy_to_q8_23_from_float_with_clamp((int32_t*)dst, (float*)src, count);
117 LOG_ALWAYS_FATAL("invalid src format %#x for dst format %#x",
118 src_format, dst_format);