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