OSDN Git Service

Camera: Prevent data size overflow am: 97a9cd6748 am: 5ccb5b43ec am: 4d15f88da3 am...
[android-x86/system-media.git] / audio_utils / format.c
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 <cutils/log.h>
21 #include <audio_utils/primitives.h>
22 #include <audio_utils/format.h>
23
24 void memcpy_by_audio_format(void *dst, audio_format_t dst_format,
25         const void *src, audio_format_t src_format, size_t count)
26 {
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:
32         case AUDIO_FORMAT_PCM_24_BIT_PACKED:
33         case AUDIO_FORMAT_PCM_32_BIT:
34         case AUDIO_FORMAT_PCM_8_24_BIT:
35             memcpy(dst, src, count * audio_bytes_per_sample(dst_format));
36             return;
37         default:
38             break;
39         }
40     }
41     switch (dst_format) {
42     case AUDIO_FORMAT_PCM_16_BIT:
43         switch (src_format) {
44         case AUDIO_FORMAT_PCM_FLOAT:
45             memcpy_to_i16_from_float((int16_t*)dst, (float*)src, count);
46             return;
47         case AUDIO_FORMAT_PCM_24_BIT_PACKED:
48             memcpy_to_i16_from_p24((int16_t*)dst, (uint8_t*)src, count);
49             return;
50         case AUDIO_FORMAT_PCM_32_BIT:
51             memcpy_to_i16_from_i32((int16_t*)dst, (int32_t*)src, count);
52             return;
53         case AUDIO_FORMAT_PCM_8_24_BIT:
54             memcpy_to_i16_from_q8_23((int16_t*)dst, (int32_t*)src, count);
55             return;
56         default:
57             break;
58         }
59         break;
60     case AUDIO_FORMAT_PCM_FLOAT:
61         switch (src_format) {
62         case AUDIO_FORMAT_PCM_16_BIT:
63             memcpy_to_float_from_i16((float*)dst, (int16_t*)src, count);
64             return;
65         case AUDIO_FORMAT_PCM_24_BIT_PACKED:
66             memcpy_to_float_from_p24((float*)dst, (uint8_t*)src, count);
67             return;
68         case AUDIO_FORMAT_PCM_32_BIT:
69             memcpy_to_float_from_i32((float*)dst, (int32_t*)src, count);
70             return;
71         case AUDIO_FORMAT_PCM_8_24_BIT:
72             memcpy_to_float_from_q8_23((float*)dst, (int32_t*)src, count);
73             return;
74         default:
75             break;
76         }
77         break;
78     case AUDIO_FORMAT_PCM_24_BIT_PACKED:
79         switch (src_format) {
80         case AUDIO_FORMAT_PCM_16_BIT:
81             memcpy_to_p24_from_i16((uint8_t*)dst, (int16_t*)src, count);
82             return;
83         case AUDIO_FORMAT_PCM_FLOAT:
84             memcpy_to_p24_from_float((uint8_t*)dst, (float*)src, count);
85             return;
86         default:
87             break;
88         }
89         break;
90     case AUDIO_FORMAT_PCM_32_BIT:
91         switch (src_format) {
92         case AUDIO_FORMAT_PCM_16_BIT:
93             memcpy_to_i32_from_i16((int32_t*)dst, (int16_t*)src, count);
94             return;
95         case AUDIO_FORMAT_PCM_FLOAT:
96             memcpy_to_i32_from_float((int32_t*)dst, (float*)src, count);
97             return;
98         default:
99             break;
100         }
101         break;
102     case AUDIO_FORMAT_PCM_8_24_BIT:
103         switch (src_format) {
104         case AUDIO_FORMAT_PCM_16_BIT:
105             memcpy_to_q8_23_from_i16((int32_t*)dst, (int16_t*)src, count);
106             return;
107         case AUDIO_FORMAT_PCM_FLOAT:
108             memcpy_to_q8_23_from_float_with_clamp((int32_t*)dst, (float*)src, count);
109             return;
110         default:
111             break;
112         }
113         break;
114     default:
115         break;
116     }
117     LOG_ALWAYS_FATAL("invalid src format %#x for dst format %#x",
118             src_format, dst_format);
119 }