1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 /* #define LOG_NDEBUG 0 */
18 #define LOG_TAG "audio_utils_format"
19
20 #include <log/log.h>
21
22 #include <audio_utils/format.h>
23 #include <audio_utils/primitives.h>
24
memcpy_by_audio_format(void * dst,audio_format_t dst_format,const void * src,audio_format_t src_format,size_t count)25 void memcpy_by_audio_format(void *dst, audio_format_t dst_format,
26 const void *src, audio_format_t src_format, size_t count)
27 {
28 /* default cases for error falls through to fatal log below. */
29 if (dst_format == src_format) {
30 switch (dst_format) {
31 case AUDIO_FORMAT_PCM_16_BIT:
32 case AUDIO_FORMAT_PCM_FLOAT:
33 case AUDIO_FORMAT_PCM_8_BIT:
34 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
35 case AUDIO_FORMAT_PCM_32_BIT:
36 case AUDIO_FORMAT_PCM_8_24_BIT:
37 if (dst != src) {
38 // TODO: should assert if memory regions overlap.
39 memcpy(dst, src, count * audio_bytes_per_sample(dst_format));
40 }
41 return;
42 default:
43 break;
44 }
45 }
46 switch (dst_format) {
47 case AUDIO_FORMAT_PCM_16_BIT:
48 switch (src_format) {
49 case AUDIO_FORMAT_PCM_FLOAT:
50 memcpy_to_i16_from_float((int16_t*)dst, (float*)src, count);
51 return;
52 case AUDIO_FORMAT_PCM_8_BIT:
53 memcpy_to_i16_from_u8((int16_t*)dst, (uint8_t*)src, count);
54 return;
55 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
56 memcpy_to_i16_from_p24((int16_t*)dst, (uint8_t*)src, count);
57 return;
58 case AUDIO_FORMAT_PCM_32_BIT:
59 memcpy_to_i16_from_i32((int16_t*)dst, (int32_t*)src, count);
60 return;
61 case AUDIO_FORMAT_PCM_8_24_BIT:
62 memcpy_to_i16_from_q8_23((int16_t*)dst, (int32_t*)src, count);
63 return;
64 default:
65 break;
66 }
67 break;
68 case AUDIO_FORMAT_PCM_FLOAT:
69 switch (src_format) {
70 case AUDIO_FORMAT_PCM_16_BIT:
71 memcpy_to_float_from_i16((float*)dst, (int16_t*)src, count);
72 return;
73 case AUDIO_FORMAT_PCM_8_BIT:
74 memcpy_to_float_from_u8((float*)dst, (uint8_t*)src, count);
75 return;
76 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
77 memcpy_to_float_from_p24((float*)dst, (uint8_t*)src, count);
78 return;
79 case AUDIO_FORMAT_PCM_32_BIT:
80 memcpy_to_float_from_i32((float*)dst, (int32_t*)src, count);
81 return;
82 case AUDIO_FORMAT_PCM_8_24_BIT:
83 memcpy_to_float_from_q8_23((float*)dst, (int32_t*)src, count);
84 return;
85 default:
86 break;
87 }
88 break;
89 case AUDIO_FORMAT_PCM_8_BIT:
90 switch (src_format) {
91 case AUDIO_FORMAT_PCM_16_BIT:
92 memcpy_to_u8_from_i16((uint8_t*)dst, (int16_t*)src, count);
93 return;
94 case AUDIO_FORMAT_PCM_FLOAT:
95 memcpy_to_u8_from_float((uint8_t*)dst, (float*)src, count);
96 return;
97 // The following converts HAL to AudioRecord formats.
98 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
99 memcpy_to_u8_from_p24((uint8_t*)dst, (uint8_t*)src, count);
100 return;
101 case AUDIO_FORMAT_PCM_32_BIT:
102 memcpy_to_u8_from_i32((uint8_t*)dst, (int32_t*)src, count);
103 return;
104 case AUDIO_FORMAT_PCM_8_24_BIT:
105 memcpy_to_u8_from_q8_23((uint8_t*)dst, (int32_t*)src, count);
106 return;
107 default:
108 break;
109 }
110 break;
111 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
112 switch (src_format) {
113 case AUDIO_FORMAT_PCM_16_BIT:
114 memcpy_to_p24_from_i16((uint8_t*)dst, (int16_t*)src, count);
115 return;
116 case AUDIO_FORMAT_PCM_FLOAT:
117 memcpy_to_p24_from_float((uint8_t*)dst, (float*)src, count);
118 return;
119 case AUDIO_FORMAT_PCM_32_BIT:
120 memcpy_to_p24_from_i32((uint8_t*)dst, (int32_t*)src, count);
121 return;
122 case AUDIO_FORMAT_PCM_8_24_BIT:
123 memcpy_to_p24_from_q8_23((uint8_t*)dst, (int32_t*)src, count);
124 return;
125 default:
126 break;
127 }
128 break;
129 case AUDIO_FORMAT_PCM_32_BIT:
130 switch (src_format) {
131 case AUDIO_FORMAT_PCM_16_BIT:
132 memcpy_to_i32_from_i16((int32_t*)dst, (int16_t*)src, count);
133 return;
134 case AUDIO_FORMAT_PCM_FLOAT:
135 memcpy_to_i32_from_float((int32_t*)dst, (float*)src, count);
136 return;
137 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
138 memcpy_to_i32_from_p24((int32_t*)dst, (uint8_t *)src, count);
139 return;
140 default:
141 break;
142 }
143 break;
144 case AUDIO_FORMAT_PCM_8_24_BIT:
145 switch (src_format) {
146 case AUDIO_FORMAT_PCM_16_BIT:
147 memcpy_to_q8_23_from_i16((int32_t*)dst, (int16_t*)src, count);
148 return;
149 case AUDIO_FORMAT_PCM_FLOAT:
150 memcpy_to_q8_23_from_float_with_clamp((int32_t*)dst, (float*)src, count);
151 return;
152 case AUDIO_FORMAT_PCM_24_BIT_PACKED: {
153 memcpy_to_q8_23_from_p24((int32_t *)dst, (uint8_t *)src, count);
154 return;
155 }
156 default:
157 break;
158 }
159 break;
160 default:
161 break;
162 }
163 LOG_ALWAYS_FATAL("invalid src format %#x for dst format %#x",
164 src_format, dst_format);
165 }
166
memcpy_by_index_array_initialization_from_channel_mask(int8_t * idxary,size_t arysize,audio_channel_mask_t dst_channel_mask,audio_channel_mask_t src_channel_mask)167 size_t memcpy_by_index_array_initialization_from_channel_mask(int8_t *idxary, size_t arysize,
168 audio_channel_mask_t dst_channel_mask, audio_channel_mask_t src_channel_mask)
169 {
170 const audio_channel_representation_t src_representation =
171 audio_channel_mask_get_representation(src_channel_mask);
172 const audio_channel_representation_t dst_representation =
173 audio_channel_mask_get_representation(dst_channel_mask);
174 const uint32_t src_bits = audio_channel_mask_get_bits(src_channel_mask);
175 const uint32_t dst_bits = audio_channel_mask_get_bits(dst_channel_mask);
176
177 switch (src_representation) {
178 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
179 switch (dst_representation) {
180 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
181 return memcpy_by_index_array_initialization(idxary, arysize,
182 dst_bits, src_bits);
183 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
184 return memcpy_by_index_array_initialization_dst_index(idxary, arysize,
185 dst_bits, src_bits);
186 default:
187 return 0;
188 }
189 break;
190 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
191 switch (dst_representation) {
192 case AUDIO_CHANNEL_REPRESENTATION_POSITION:
193 return memcpy_by_index_array_initialization_src_index(idxary, arysize,
194 dst_bits, src_bits);
195 case AUDIO_CHANNEL_REPRESENTATION_INDEX:
196 return memcpy_by_index_array_initialization(idxary, arysize,
197 dst_bits, src_bits);
198 default:
199 return 0;
200 }
201 break;
202 default:
203 return 0;
204 }
205 }
206