blob: a6b2033b11d5eafc20da998b2f134f15bb6bd09d [file] [log] [blame]
Andy Hungfcad7e42014-03-06 17:36:40 -08001/*
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
Andy Hunge46f5532014-03-18 13:13:45 -070017/* #define LOG_NDEBUG 0 */
Andy Hungfcad7e42014-03-06 17:36:40 -080018#define LOG_TAG "audio_utils_format"
19
20#include <cutils/log.h>
21#include <audio_utils/primitives.h>
22#include <audio_utils/format.h>
23
24void memcpy_by_audio_format(void *dst, audio_format_t dst_format,
Andy Hung1bc2b262014-07-10 14:21:42 -070025 const void *src, audio_format_t src_format, size_t count)
Andy Hunge46f5532014-03-18 13:13:45 -070026{
27 /* default cases for error falls through to fatal log below. */
28 if (dst_format == src_format) {
29 switch (dst_format) {
30 case AUDIO_FORMAT_PCM_16_BIT:
31 case AUDIO_FORMAT_PCM_FLOAT:
Andy Hung23ef1b32015-01-13 13:56:37 -080032 case AUDIO_FORMAT_PCM_8_BIT:
Andy Hunge46f5532014-03-18 13:13:45 -070033 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
34 case AUDIO_FORMAT_PCM_32_BIT:
35 case AUDIO_FORMAT_PCM_8_24_BIT:
36 memcpy(dst, src, count * audio_bytes_per_sample(dst_format));
37 return;
38 default:
39 break;
40 }
Andy Hungfcad7e42014-03-06 17:36:40 -080041 }
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 Hung23ef1b32015-01-13 13:56:37 -080048 case AUDIO_FORMAT_PCM_8_BIT:
49 memcpy_to_i16_from_u8((int16_t*)dst, (uint8_t*)src, count);
50 return;
Andy Hungfcad7e42014-03-06 17:36:40 -080051 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
52 memcpy_to_i16_from_p24((int16_t*)dst, (uint8_t*)src, count);
53 return;
Andy Hunge46f5532014-03-18 13:13:45 -070054 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 Hungfcad7e42014-03-06 17:36:40 -080060 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 Hung23ef1b32015-01-13 13:56:37 -080069 case AUDIO_FORMAT_PCM_8_BIT:
70 memcpy_to_float_from_u8((float*)dst, (uint8_t*)src, count);
71 return;
Andy Hungfcad7e42014-03-06 17:36:40 -080072 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
73 memcpy_to_float_from_p24((float*)dst, (uint8_t*)src, count);
74 return;
Andy Hunge46f5532014-03-18 13:13:45 -070075 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 Hungfcad7e42014-03-06 17:36:40 -080081 default:
82 break;
83 }
84 break;
Andy Hung23ef1b32015-01-13 13:56:37 -080085 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;
93 default:
94 break;
95 }
96 break;
Andy Hungfcad7e42014-03-06 17:36:40 -080097 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
98 switch (src_format) {
99 case AUDIO_FORMAT_PCM_16_BIT:
100 memcpy_to_p24_from_i16((uint8_t*)dst, (int16_t*)src, count);
101 return;
102 case AUDIO_FORMAT_PCM_FLOAT:
103 memcpy_to_p24_from_float((uint8_t*)dst, (float*)src, count);
104 return;
105 default:
106 break;
107 }
108 break;
Andy Hunge46f5532014-03-18 13:13:45 -0700109 case AUDIO_FORMAT_PCM_32_BIT:
110 switch (src_format) {
111 case AUDIO_FORMAT_PCM_16_BIT:
112 memcpy_to_i32_from_i16((int32_t*)dst, (int16_t*)src, count);
113 return;
114 case AUDIO_FORMAT_PCM_FLOAT:
115 memcpy_to_i32_from_float((int32_t*)dst, (float*)src, count);
116 return;
117 default:
118 break;
119 }
120 break;
121 case AUDIO_FORMAT_PCM_8_24_BIT:
122 switch (src_format) {
123 case AUDIO_FORMAT_PCM_16_BIT:
124 memcpy_to_q8_23_from_i16((int32_t*)dst, (int16_t*)src, count);
125 return;
126 case AUDIO_FORMAT_PCM_FLOAT:
127 memcpy_to_q8_23_from_float_with_clamp((int32_t*)dst, (float*)src, count);
128 return;
Haynes Mathew George78ac9f82015-04-09 13:50:13 -0700129 case AUDIO_FORMAT_PCM_24_BIT_PACKED: {
130 memcpy_to_q8_23_from_p24((int32_t *)dst, (uint8_t *)src, count);
131 return;
132 }
Andy Hunge46f5532014-03-18 13:13:45 -0700133 default:
134 break;
135 }
136 break;
Andy Hungfcad7e42014-03-06 17:36:40 -0800137 default:
138 break;
139 }
140 LOG_ALWAYS_FATAL("invalid src format %#x for dst format %#x",
141 src_format, dst_format);
142}