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 
audio_utils_fifo_init(struct audio_utils_fifo * fifo,size_t frameCount,size_t frameSize,void * buffer)28 void audio_utils_fifo_init(struct audio_utils_fifo *fifo, size_t frameCount, size_t frameSize,
29         void *buffer)
30 {
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 
audio_utils_fifo_deinit(struct audio_utils_fifo * fifo __unused)42 void audio_utils_fifo_deinit(struct audio_utils_fifo *fifo __unused)
43 {
44 }
45 
46 // 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)47 static inline int32_t audio_utils_fifo_sum(struct audio_utils_fifo *fifo, int32_t index,
48         uint32_t increment)
49 {
50     if (fifo->mFudgeFactor) {
51         uint32_t mask = fifo->mFrameCountP2 - 1;
52         ALOG_ASSERT((index & mask) < fifo->mFrameCount);
53         ALOG_ASSERT(/*0 <= increment &&*/ increment <= fifo->mFrameCountP2);
54         if ((index & mask) + increment >= fifo->mFrameCount) {
55             increment += fifo->mFudgeFactor;
56         }
57         index += increment;
58         ALOG_ASSERT((index & mask) < fifo->mFrameCount);
59         return index;
60     } else {
61         return index + increment;
62     }
63 }
64 
65 // 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)66 static inline size_t audio_utils_fifo_diff(struct audio_utils_fifo *fifo, int32_t rear,
67         int32_t front)
68 {
69     int32_t diff = rear - front;
70     if (fifo->mFudgeFactor) {
71         uint32_t mask = ~(fifo->mFrameCountP2 - 1);
72         int32_t genDiff = (rear & mask) - (front & mask);
73         if (genDiff != 0) {
74             ALOG_ASSERT(genDiff == (int32_t) fifo->mFrameCountP2);
75             diff -= fifo->mFudgeFactor;
76         }
77     }
78     // FIFO should not be overfull
79     ALOG_ASSERT(0 <= diff && diff <= (int32_t) fifo->mFrameCount);
80     return (size_t) diff;
81 }
82 
audio_utils_fifo_write(struct audio_utils_fifo * fifo,const void * buffer,size_t count)83 ssize_t audio_utils_fifo_write(struct audio_utils_fifo *fifo, const void *buffer, size_t count)
84 {
85     int32_t front = android_atomic_acquire_load(&fifo->mFront);
86     int32_t rear = fifo->mRear;
87     size_t availToWrite = fifo->mFrameCount - audio_utils_fifo_diff(fifo, rear, front);
88     if (availToWrite > count) {
89         availToWrite = count;
90     }
91     rear &= fifo->mFrameCountP2 - 1;
92     size_t part1 = fifo->mFrameCount - rear;
93     if (part1 > availToWrite) {
94         part1 = availToWrite;
95     }
96     if (part1 > 0) {
97         memcpy((char *) fifo->mBuffer + (rear * fifo->mFrameSize), buffer,
98                 part1 * fifo->mFrameSize);
99         size_t part2 = availToWrite - part1;
100         if (part2 > 0) {
101             memcpy(fifo->mBuffer, (char *) buffer + (part1 * fifo->mFrameSize),
102                     part2 * fifo->mFrameSize);
103         }
104         android_atomic_release_store(audio_utils_fifo_sum(fifo, fifo->mRear, availToWrite),
105                 &fifo->mRear);
106     }
107     return availToWrite;
108 }
109 
audio_utils_fifo_read(struct audio_utils_fifo * fifo,void * buffer,size_t count)110 ssize_t audio_utils_fifo_read(struct audio_utils_fifo *fifo, void *buffer, size_t count)
111 {
112     int32_t rear = android_atomic_acquire_load(&fifo->mRear);
113     int32_t front = fifo->mFront;
114     size_t availToRead = audio_utils_fifo_diff(fifo, rear, front);
115     if (availToRead > count) {
116         availToRead = count;
117     }
118     front &= fifo->mFrameCountP2 - 1;
119     size_t part1 = fifo->mFrameCount - front;
120     if (part1 > availToRead) {
121         part1 = availToRead;
122     }
123     if (part1 > 0) {
124         memcpy(buffer, (char *) fifo->mBuffer + (front * fifo->mFrameSize),
125                 part1 * fifo->mFrameSize);
126         size_t part2 = availToRead - part1;
127         if (part2 > 0) {
128             memcpy((char *) buffer + (part1 * fifo->mFrameSize), fifo->mBuffer,
129                     part2 * fifo->mFrameSize);
130         }
131         android_atomic_release_store(audio_utils_fifo_sum(fifo, fifo->mFront, availToRead),
132                 &fifo->mFront);
133     }
134     return availToRead;
135 }
136