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 <audio_utils/fifo.h>
23 #include <audio_utils/roundup.h>
24 #include <cutils/atomic.h>
25 #include <cutils/log.h>
26 
audio_utils_fifo_init(struct audio_utils_fifo * fifo,size_t frameCount,size_t frameSize,void * buffer)27 void audio_utils_fifo_init(struct audio_utils_fifo *fifo, size_t frameCount, size_t frameSize,
28         void *buffer)
29 {
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;
37     fifo->mFront = 0;
38     fifo->mRear = 0;
39 }
40 
audio_utils_fifo_deinit(struct audio_utils_fifo * fifo __unused)41 void audio_utils_fifo_deinit(struct audio_utils_fifo *fifo __unused)
42 {
43 }
44 
45 // Return a new index as the sum of an old index (either mFront or mRear) and a specified increment.
audio_utils_fifo_sum(struct audio_utils_fifo * fifo,int32_t index,uint32_t increment)46 static inline int32_t audio_utils_fifo_sum(struct audio_utils_fifo *fifo, int32_t index,
47         uint32_t increment)
48 {
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;
55         }
56         index += increment;
57         ALOG_ASSERT((index & mask) < fifo->mFrameCount);
58         return index;
59     } else {
60         return index + increment;
61     }
62 }
63 
64 // Return the difference between two indices: rear - front, where 0 <= difference <= mFrameCount.
audio_utils_fifo_diff(struct audio_utils_fifo * fifo,int32_t rear,int32_t front)65 static inline size_t audio_utils_fifo_diff(struct audio_utils_fifo *fifo, int32_t rear,
66         int32_t front)
67 {
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);
72         if (genDiff != 0) {
73             ALOG_ASSERT(genDiff == (int32_t) fifo->mFrameCountP2);
74             diff -= fifo->mFudgeFactor;
75         }
76     }
77     // FIFO should not be overfull
78     ALOG_ASSERT(0 <= diff && diff <= (int32_t) fifo->mFrameCount);
79     return (size_t) diff;
80 }
81 
audio_utils_fifo_write(struct audio_utils_fifo * fifo,const void * buffer,size_t count)82 ssize_t audio_utils_fifo_write(struct audio_utils_fifo *fifo, const void *buffer, size_t count)
83 {
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) {
88         availToWrite = count;
89     }
90     rear &= fifo->mFrameCountP2 - 1;
91     size_t part1 = fifo->mFrameCount - rear;
92     if (part1 > availToWrite) {
93         part1 = availToWrite;
94     }
95     if (part1 > 0) {
96         memcpy((char *) fifo->mBuffer + (rear * fifo->mFrameSize), buffer,
97                 part1 * fifo->mFrameSize);
98         size_t part2 = availToWrite - part1;
99         if (part2 > 0) {
100             memcpy(fifo->mBuffer, (char *) buffer + (part1 * fifo->mFrameSize),
101                     part2 * fifo->mFrameSize);
102         }
103         android_atomic_release_store(audio_utils_fifo_sum(fifo, fifo->mRear, availToWrite),
104                 &fifo->mRear);
105     }
106     return availToWrite;
107 }
108 
audio_utils_fifo_read(struct audio_utils_fifo * fifo,void * buffer,size_t count)109 ssize_t audio_utils_fifo_read(struct audio_utils_fifo *fifo, void *buffer, size_t count)
110 {
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) {
115         availToRead = count;
116     }
117     front &= fifo->mFrameCountP2 - 1;
118     size_t part1 = fifo->mFrameCount - front;
119     if (part1 > availToRead) {
120         part1 = availToRead;
121     }
122     if (part1 > 0) {
123         memcpy(buffer, (char *) fifo->mBuffer + (front * fifo->mFrameSize),
124                 part1 * fifo->mFrameSize);
125         size_t part2 = availToRead - part1;
126         if (part2 > 0) {
127             memcpy((char *) buffer + (part1 * fifo->mFrameSize), fifo->mBuffer,
128                     part2 * fifo->mFrameSize);
129         }
130         android_atomic_release_store(audio_utils_fifo_sum(fifo, fifo->mFront, availToRead),
131                 &fifo->mFront);
132     }
133     return availToRead;
134 }
135