1632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten/*
2632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten * Copyright (C) 2011 The Android Open Source Project
3632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten *
4632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten * Licensed under the Apache License, Version 2.0 (the "License");
5632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten * you may not use this file except in compliance with the License.
6632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten * You may obtain a copy of the License at
7632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten *
8632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten *      http://www.apache.org/licenses/LICENSE-2.0
9632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten *
10632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten * Unless required by applicable law or agreed to in writing, software
11632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten * distributed under the License is distributed on an "AS IS" BASIS,
12632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten * See the License for the specific language governing permissions and
14632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten * limitations under the License.
15632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten */
16632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten
17632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten#include <audio_utils/primitives.h>
18632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten
19632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kastenvoid ditherAndClamp(int32_t* out, int32_t const *sums, size_t c)
20632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten{
21632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten    size_t i;
22632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten    for (i=0 ; i<c ; i++) {
23632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten        int32_t l = *sums++;
24632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten        int32_t r = *sums++;
25632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten        int32_t nl = l >> 12;
26632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten        int32_t nr = r >> 12;
27632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten        l = clamp16(nl);
28632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten        r = clamp16(nr);
29632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten        *out++ = (r<<16) | (l & 0xFFFF);
30632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten    }
31632e0c016c9a518b36f09988b740b3bc1199c3e4Glenn Kasten}
32ddb2e930bef3d1be1627c613fb8f3df848166ac7Glenn Kasten
33ddb2e930bef3d1be1627c613fb8f3df848166ac7Glenn Kastenvoid memcpy_to_i16_from_u8(int16_t *dst, const uint8_t *src, size_t count)
34ddb2e930bef3d1be1627c613fb8f3df848166ac7Glenn Kasten{
35ddb2e930bef3d1be1627c613fb8f3df848166ac7Glenn Kasten    dst += count;
36ddb2e930bef3d1be1627c613fb8f3df848166ac7Glenn Kasten    src += count;
37ddb2e930bef3d1be1627c613fb8f3df848166ac7Glenn Kasten    while (count--) {
38ddb2e930bef3d1be1627c613fb8f3df848166ac7Glenn Kasten        *--dst = (int16_t)(*--src - 0x80) << 8;
39ddb2e930bef3d1be1627c613fb8f3df848166ac7Glenn Kasten    }
40ddb2e930bef3d1be1627c613fb8f3df848166ac7Glenn Kasten}
417a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten
427a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kastenvoid downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count)
437a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten{
447a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten    while (count--) {
457a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten        *dst++ = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
467a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten        src += 2;
477a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten    }
487a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten}
497a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten
507a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kastenvoid upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count)
517a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten{
527a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten    while (count--) {
537a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten        int32_t temp = *src++;
547a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten        dst[0] = temp;
557a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten        dst[1] = temp;
567a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten        dst += 2;
577a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten    }
587a0bacaf9634d6bf0abbea9608fb15e94a675358Glenn Kasten}
59