2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "audio_utils_fifo"
22 #include <audio_utils/fifo.h>
23 #include <audio_utils/roundup.h>
24 #include <cutils/atomic.h>
25 #include <cutils/log.h>
27 void audio_utils_fifo_init(struct audio_utils_fifo *fifo, size_t frameCount, size_t frameSize,
30 // We would need a 64-bit roundup to support larger frameCount.
31 ALOG_ASSERT(fifo != NULL && frameCount > 0 && frameSize > 0 && buffer != NULL);
32 fifo->mFrameCount = frameCount;
33 fifo->mFrameCountP2 = roundup(frameCount);
34 fifo->mFudgeFactor = fifo->mFrameCountP2 - fifo->mFrameCount;
35 fifo->mFrameSize = frameSize;
36 fifo->mBuffer = buffer;
41 void audio_utils_fifo_deinit(struct audio_utils_fifo *fifo __unused)
45 // Return a new index as the sum of an old index (either mFront or mRear) and a specified increment.
46 static inline int32_t audio_utils_fifo_sum(struct audio_utils_fifo *fifo, int32_t index,
49 if (fifo->mFudgeFactor) {
50 uint32_t mask = fifo->mFrameCountP2 - 1;
51 ALOG_ASSERT((index & mask) < fifo->mFrameCount);
52 ALOG_ASSERT(/*0 <= increment &&*/ increment <= fifo->mFrameCountP2);
53 if ((index & mask) + increment >= fifo->mFrameCount) {
54 increment += fifo->mFudgeFactor;
57 ALOG_ASSERT((index & mask) < fifo->mFrameCount);
60 return index + increment;
64 // Return the difference between two indices: rear - front, where 0 <= difference <= mFrameCount.
65 static inline size_t audio_utils_fifo_diff(struct audio_utils_fifo *fifo, int32_t rear,
68 int32_t diff = rear - front;
69 if (fifo->mFudgeFactor) {
70 uint32_t mask = ~(fifo->mFrameCountP2 - 1);
71 int32_t genDiff = (rear & mask) - (front & mask);
73 ALOG_ASSERT(genDiff == (int32_t) fifo->mFrameCountP2);
74 diff -= fifo->mFudgeFactor;
77 // FIFO should not be overfull
78 ALOG_ASSERT(0 <= diff && diff <= (int32_t) fifo->mFrameCount);
82 ssize_t audio_utils_fifo_write(struct audio_utils_fifo *fifo, const void *buffer, size_t count)
84 int32_t front = android_atomic_acquire_load(&fifo->mFront);
85 int32_t rear = fifo->mRear;
86 size_t availToWrite = fifo->mFrameCount - audio_utils_fifo_diff(fifo, rear, front);
87 if (availToWrite > count) {
90 rear &= fifo->mFrameCountP2 - 1;
91 size_t part1 = fifo->mFrameCount - rear;
92 if (part1 > availToWrite) {
96 memcpy((char *) fifo->mBuffer + (rear * fifo->mFrameSize), buffer,
97 part1 * fifo->mFrameSize);
98 size_t part2 = availToWrite - part1;
100 memcpy(fifo->mBuffer, (char *) buffer + (part1 * fifo->mFrameSize),
101 part2 * fifo->mFrameSize);
103 android_atomic_release_store(audio_utils_fifo_sum(fifo, fifo->mRear, availToWrite),
109 ssize_t audio_utils_fifo_read(struct audio_utils_fifo *fifo, void *buffer, size_t count)
111 int32_t rear = android_atomic_acquire_load(&fifo->mRear);
112 int32_t front = fifo->mFront;
113 size_t availToRead = audio_utils_fifo_diff(fifo, rear, front);
114 if (availToRead > count) {
117 front &= fifo->mFrameCountP2 - 1;
118 size_t part1 = fifo->mFrameCount - front;
119 if (part1 > availToRead) {
123 memcpy(buffer, (char *) fifo->mBuffer + (front * fifo->mFrameSize),
124 part1 * fifo->mFrameSize);
125 size_t part2 = availToRead - part1;
127 memcpy((char *) buffer + (part1 * fifo->mFrameSize), fifo->mBuffer,
128 part2 * fifo->mFrameSize);
130 android_atomic_release_store(audio_utils_fifo_sum(fifo, fifo->mFront, availToRead),