blob: fd02fb9ce2d3be6709617a5e24010c01950ee572 [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
17//#define LOG_NDEBUG 0
18#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,
25 void *src, audio_format_t src_format, size_t count) {
26 if (dst_format == src_format
27 && (dst_format == AUDIO_FORMAT_PCM_16_BIT
28 || dst_format == AUDIO_FORMAT_PCM_FLOAT
29 || dst_format == AUDIO_FORMAT_PCM_24_BIT_PACKED)) {
30 memcpy(dst, src, count * audio_bytes_per_sample(dst_format));
31 return;
32 }
33 switch (dst_format) {
34 case AUDIO_FORMAT_PCM_16_BIT:
35 switch (src_format) {
36 case AUDIO_FORMAT_PCM_FLOAT:
37 memcpy_to_i16_from_float((int16_t*)dst, (float*)src, count);
38 return;
39 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
40 memcpy_to_i16_from_p24((int16_t*)dst, (uint8_t*)src, count);
41 return;
42 default:
43 break;
44 }
45 break;
46 case AUDIO_FORMAT_PCM_FLOAT:
47 switch (src_format) {
48 case AUDIO_FORMAT_PCM_16_BIT:
49 memcpy_to_float_from_i16((float*)dst, (int16_t*)src, count);
50 return;
51 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
52 memcpy_to_float_from_p24((float*)dst, (uint8_t*)src, count);
53 return;
54 default:
55 break;
56 }
57 break;
58 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
59 switch (src_format) {
60 case AUDIO_FORMAT_PCM_16_BIT:
61 memcpy_to_p24_from_i16((uint8_t*)dst, (int16_t*)src, count);
62 return;
63 case AUDIO_FORMAT_PCM_FLOAT:
64 memcpy_to_p24_from_float((uint8_t*)dst, (float*)src, count);
65 return;
66 default:
67 break;
68 }
69 break;
70 default:
71 break;
72 }
73 LOG_ALWAYS_FATAL("invalid src format %#x for dst format %#x",
74 src_format, dst_format);
75}