format.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /* #define LOG_NDEBUG 0 */
  17. #define LOG_TAG "audio_utils_format"
  18. #include "audio/android/cutils/log.h"
  19. #include "audio/android/audio_utils/include/audio_utils/primitives.h"
  20. #include "audio/android/audio_utils/include/audio_utils/format.h"
  21. #include "audio/android/audio.h"
  22. void memcpy_by_audio_format(void *dst, audio_format_t dst_format,
  23. const void *src, audio_format_t src_format, size_t count)
  24. {
  25. /* default cases for error falls through to fatal log below. */
  26. if (dst_format == src_format) {
  27. switch (dst_format) {
  28. case AUDIO_FORMAT_PCM_16_BIT:
  29. case AUDIO_FORMAT_PCM_FLOAT:
  30. case AUDIO_FORMAT_PCM_8_BIT:
  31. case AUDIO_FORMAT_PCM_24_BIT_PACKED:
  32. case AUDIO_FORMAT_PCM_32_BIT:
  33. case AUDIO_FORMAT_PCM_8_24_BIT:
  34. memcpy(dst, src, count * audio_bytes_per_sample(dst_format));
  35. return;
  36. default:
  37. break;
  38. }
  39. }
  40. switch (dst_format) {
  41. case AUDIO_FORMAT_PCM_16_BIT:
  42. switch (src_format) {
  43. case AUDIO_FORMAT_PCM_FLOAT:
  44. memcpy_to_i16_from_float((int16_t*)dst, (float*)src, count);
  45. return;
  46. case AUDIO_FORMAT_PCM_8_BIT:
  47. memcpy_to_i16_from_u8((int16_t*)dst, (uint8_t*)src, count);
  48. return;
  49. case AUDIO_FORMAT_PCM_24_BIT_PACKED:
  50. memcpy_to_i16_from_p24((int16_t*)dst, (uint8_t*)src, count);
  51. return;
  52. case AUDIO_FORMAT_PCM_32_BIT:
  53. memcpy_to_i16_from_i32((int16_t*)dst, (int32_t*)src, count);
  54. return;
  55. case AUDIO_FORMAT_PCM_8_24_BIT:
  56. memcpy_to_i16_from_q8_23((int16_t*)dst, (int32_t*)src, count);
  57. return;
  58. default:
  59. break;
  60. }
  61. break;
  62. case AUDIO_FORMAT_PCM_FLOAT:
  63. switch (src_format) {
  64. case AUDIO_FORMAT_PCM_16_BIT:
  65. memcpy_to_float_from_i16((float*)dst, (int16_t*)src, count);
  66. return;
  67. case AUDIO_FORMAT_PCM_8_BIT:
  68. memcpy_to_float_from_u8((float*)dst, (uint8_t*)src, count);
  69. return;
  70. case AUDIO_FORMAT_PCM_24_BIT_PACKED:
  71. memcpy_to_float_from_p24((float*)dst, (uint8_t*)src, count);
  72. return;
  73. case AUDIO_FORMAT_PCM_32_BIT:
  74. memcpy_to_float_from_i32((float*)dst, (int32_t*)src, count);
  75. return;
  76. case AUDIO_FORMAT_PCM_8_24_BIT:
  77. memcpy_to_float_from_q8_23((float*)dst, (int32_t*)src, count);
  78. return;
  79. default:
  80. break;
  81. }
  82. break;
  83. case AUDIO_FORMAT_PCM_8_BIT:
  84. switch (src_format) {
  85. case AUDIO_FORMAT_PCM_16_BIT:
  86. memcpy_to_u8_from_i16((uint8_t*)dst, (int16_t*)src, count);
  87. return;
  88. case AUDIO_FORMAT_PCM_FLOAT:
  89. memcpy_to_u8_from_float((uint8_t*)dst, (float*)src, count);
  90. return;
  91. default:
  92. break;
  93. }
  94. break;
  95. case AUDIO_FORMAT_PCM_24_BIT_PACKED:
  96. switch (src_format) {
  97. case AUDIO_FORMAT_PCM_16_BIT:
  98. memcpy_to_p24_from_i16((uint8_t*)dst, (int16_t*)src, count);
  99. return;
  100. case AUDIO_FORMAT_PCM_FLOAT:
  101. memcpy_to_p24_from_float((uint8_t*)dst, (float*)src, count);
  102. return;
  103. default:
  104. break;
  105. }
  106. break;
  107. case AUDIO_FORMAT_PCM_32_BIT:
  108. switch (src_format) {
  109. case AUDIO_FORMAT_PCM_16_BIT:
  110. memcpy_to_i32_from_i16((int32_t*)dst, (int16_t*)src, count);
  111. return;
  112. case AUDIO_FORMAT_PCM_FLOAT:
  113. memcpy_to_i32_from_float((int32_t*)dst, (float*)src, count);
  114. return;
  115. default:
  116. break;
  117. }
  118. break;
  119. case AUDIO_FORMAT_PCM_8_24_BIT:
  120. switch (src_format) {
  121. case AUDIO_FORMAT_PCM_16_BIT:
  122. memcpy_to_q8_23_from_i16((int32_t*)dst, (int16_t*)src, count);
  123. return;
  124. case AUDIO_FORMAT_PCM_FLOAT:
  125. memcpy_to_q8_23_from_float_with_clamp((int32_t*)dst, (float*)src, count);
  126. return;
  127. case AUDIO_FORMAT_PCM_24_BIT_PACKED: {
  128. memcpy_to_q8_23_from_p24((int32_t *)dst, (uint8_t *)src, count);
  129. return;
  130. }
  131. default:
  132. break;
  133. }
  134. break;
  135. default:
  136. break;
  137. }
  138. LOG_ALWAYS_FATAL("invalid src format %#x for dst format %#x",
  139. src_format, dst_format);
  140. }
  141. size_t memcpy_by_index_array_initialization_from_channel_mask(int8_t *idxary, size_t arysize,
  142. audio_channel_mask_t dst_channel_mask, audio_channel_mask_t src_channel_mask)
  143. {
  144. const audio_channel_representation_t src_representation =
  145. audio_channel_mask_get_representation(src_channel_mask);
  146. const audio_channel_representation_t dst_representation =
  147. audio_channel_mask_get_representation(dst_channel_mask);
  148. const uint32_t src_bits = audio_channel_mask_get_bits(src_channel_mask);
  149. const uint32_t dst_bits = audio_channel_mask_get_bits(dst_channel_mask);
  150. switch (src_representation) {
  151. case AUDIO_CHANNEL_REPRESENTATION_POSITION:
  152. switch (dst_representation) {
  153. case AUDIO_CHANNEL_REPRESENTATION_POSITION:
  154. return memcpy_by_index_array_initialization(idxary, arysize,
  155. dst_bits, src_bits);
  156. case AUDIO_CHANNEL_REPRESENTATION_INDEX:
  157. return memcpy_by_index_array_initialization_dst_index(idxary, arysize,
  158. dst_bits, src_bits);
  159. default:
  160. return 0;
  161. }
  162. break;
  163. case AUDIO_CHANNEL_REPRESENTATION_INDEX:
  164. switch (dst_representation) {
  165. case AUDIO_CHANNEL_REPRESENTATION_POSITION:
  166. return memcpy_by_index_array_initialization_src_index(idxary, arysize,
  167. dst_bits, src_bits);
  168. case AUDIO_CHANNEL_REPRESENTATION_INDEX:
  169. return memcpy_by_index_array_initialization(idxary, arysize,
  170. dst_bits, src_bits);
  171. default:
  172. return 0;
  173. }
  174. break;
  175. default:
  176. return 0;
  177. }
  178. }