1/*
2 * Copyright (C) 2015 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_fifo"
19
20#include <stdlib.h>
21#include <string.h>
22#include "fifo.h"
23#include "roundup.h"
24#include "atomic.h"
25//#include <cutils/log.h>
26#define ALOG_ASSERT(exp)
27
28
29void audio_utils_fifo_init(struct audio_utils_fifo *fifo, size_t frameCount, size_t frameSize,
30        void *buffer) {
31    // We would need a 64-bit roundup to support larger frameCount.
32    ALOG_ASSERT(fifo != NULL && frameCount > 0 && frameSize > 0 && buffer != NULL);
33    fifo->mFrameCount = frameCount;
34    fifo->mFrameCountP2 = roundup(frameCount);
35    fifo->mFudgeFactor = fifo->mFrameCountP2 - fifo->mFrameCount;
36    fifo->mFrameSize = frameSize;
37    fifo->mBuffer = buffer;
38    fifo->mFront = 0;
39    fifo->mRear = 0;
40}
41
42
43void audio_utils_fifo_deinit(struct audio_utils_fifo *fifo __unused)
44{
45}
46
47
48// Return a new index as the sum of an old index (either mFront or mRear) and a specified increment.
49static inline int32_t audio_utils_fifo_sum(struct audio_utils_fifo *fifo, int32_t index,
50        uint32_t increment) {
51    if (fifo->mFudgeFactor) {
52        uint32_t mask = fifo->mFrameCountP2 - 1;
53        ALOG_ASSERT((index & mask) < fifo->mFrameCount);
54        ALOG_ASSERT(/*0 <= increment &&*/ increment <= fifo->mFrameCountP2);
55        if ((index & mask) + increment >= fifo->mFrameCount) {
56            increment += fifo->mFudgeFactor;
57        }
58
59        index += increment;
60        ALOG_ASSERT((index & mask) < fifo->mFrameCount);
61        return index;
62    } else {
63        return index + increment;
64    }
65}
66
67
68// Return the difference between two indices: rear - front, where 0 <= difference <= mFrameCount.
69static inline size_t audio_utils_fifo_diff(struct audio_utils_fifo *fifo, int32_t rear,
70        int32_t front) {
71    int32_t diff = rear - front;
72
73    if (fifo->mFudgeFactor) {
74        uint32_t mask = ~(fifo->mFrameCountP2 - 1);
75        int32_t genDiff = (rear & mask) - (front & mask);
76
77        if (genDiff != 0) {
78            ALOG_ASSERT(genDiff == (int32_t) fifo->mFrameCountP2);
79            diff -= fifo->mFudgeFactor;
80        }
81    }
82
83    // FIFO should not be overfull
84    ALOG_ASSERT(0 <= diff && diff <= (int32_t) fifo->mFrameCount);
85    return (size_t) diff;
86}
87
88
89ssize_t audio_utils_fifo_write(struct audio_utils_fifo *fifo, const void *buffer, size_t count) {
90    int32_t front = android_atomic_acquire_load(&fifo->mFront);
91    int32_t rear = fifo->mRear;
92    size_t availToWrite = fifo->mFrameCount - audio_utils_fifo_diff(fifo, rear, front);
93
94    if (availToWrite > count) {
95        availToWrite = count;
96    }
97
98    rear &= fifo->mFrameCountP2 - 1;
99    size_t part1 = fifo->mFrameCount - rear;
100    if (part1 > availToWrite) {
101        part1 = availToWrite;
102    }
103
104    if (part1 > 0) {
105        memcpy((char *) fifo->mBuffer + (rear * fifo->mFrameSize), buffer,
106                part1 * fifo->mFrameSize);
107        size_t part2 = availToWrite - part1;
108
109        if (part2 > 0) {
110            memcpy(fifo->mBuffer, (char *) buffer + (part1 * fifo->mFrameSize),
111                    part2 * fifo->mFrameSize);
112        }
113
114        android_atomic_release_store(audio_utils_fifo_sum(fifo, fifo->mRear, availToWrite),
115                &fifo->mRear);
116    }
117    return availToWrite;
118}
119
120
121ssize_t audio_utils_fifo_read(struct audio_utils_fifo *fifo, void *buffer, size_t count) {
122    int32_t rear = android_atomic_acquire_load(&fifo->mRear);
123    int32_t front = fifo->mFront;
124    size_t availToRead = audio_utils_fifo_diff(fifo, rear, front);
125    if (availToRead > count) {
126        availToRead = count;
127    }
128
129    front &= fifo->mFrameCountP2 - 1;
130    size_t part1 = fifo->mFrameCount - front;
131    if (part1 > availToRead) {
132        part1 = availToRead;
133    }
134
135    if (part1 > 0) {
136        memcpy(buffer, (char *) fifo->mBuffer + (front * fifo->mFrameSize),
137               part1 * fifo->mFrameSize);
138        size_t part2 = availToRead - part1;
139        if (part2 > 0) {
140            memcpy((char *) buffer + (part1 * fifo->mFrameSize), fifo->mBuffer,
141                   part2 * fifo->mFrameSize);
142        }
143        android_atomic_release_store(audio_utils_fifo_sum(fifo, fifo->mFront, availToRead),
144                                     &fifo->mFront);
145    }
146    return availToRead;
147}
148
149size_t audio_utils_fifo_availToRead(struct audio_utils_fifo *fifo) {
150    int32_t rear = android_atomic_acquire_load(&fifo->mRear);
151    int32_t front = fifo->mFront;
152    size_t availToRead = audio_utils_fifo_diff(fifo, rear, front);
153    return availToRead;
154}
155