Move libnbaio out of AudioFlinger

libnbaio is now a separate shared library from AudioFlinger, rather
than a static library used only by AudioFlinger.

AudioBufferProvider interface is now also independent of AudioFlinger,
moved to include/media/

Change-Id: I9bb62ffbc38d42a38b0af76e66da5e9ab1e0e21b
diff --git a/include/media/AudioBufferProvider.h b/include/media/AudioBufferProvider.h
new file mode 100644
index 0000000..43e4de7
--- /dev/null
+++ b/include/media/AudioBufferProvider.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2007 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_BUFFER_PROVIDER_H
+#define ANDROID_AUDIO_BUFFER_PROVIDER_H
+
+#include <utils/Errors.h>
+
+namespace android {
+// ----------------------------------------------------------------------------
+
+class AudioBufferProvider
+{
+public:
+
+    struct Buffer {
+        Buffer() : raw(NULL), frameCount(0) { }
+        union {
+            void*       raw;
+            short*      i16;
+            int8_t*     i8;
+        };
+        size_t frameCount;
+    };
+
+    virtual ~AudioBufferProvider() {}
+
+    // value representing an invalid presentation timestamp
+    static const int64_t kInvalidPTS = 0x7FFFFFFFFFFFFFFFLL;    // <stdint.h> is too painful
+
+    // pts is the local time when the next sample yielded by getNextBuffer
+    // will be rendered.
+    // Pass kInvalidPTS if the PTS is unknown or not applicable.
+    virtual status_t getNextBuffer(Buffer* buffer, int64_t pts = kInvalidPTS) = 0;
+
+    virtual void releaseBuffer(Buffer* buffer) = 0;
+};
+
+// ----------------------------------------------------------------------------
+}; // namespace android
+
+#endif // ANDROID_AUDIO_BUFFER_PROVIDER_H
diff --git a/include/media/ExtendedAudioBufferProvider.h b/include/media/ExtendedAudioBufferProvider.h
new file mode 100644
index 0000000..00c4444
--- /dev/null
+++ b/include/media/ExtendedAudioBufferProvider.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_EXTENDED_AUDIO_BUFFER_PROVIDER_H
+#define ANDROID_EXTENDED_AUDIO_BUFFER_PROVIDER_H
+
+#include <media/AudioBufferProvider.h>
+
+namespace android {
+
+class ExtendedAudioBufferProvider : public AudioBufferProvider {
+public:
+    virtual size_t  framesReady() const = 0;  // see description at AudioFlinger.h
+};
+
+}   // namespace android
+
+#endif  // ANDROID_EXTENDED_AUDIO_BUFFER_PROVIDER_H
diff --git a/include/media/nbaio/AudioBufferProviderSource.h b/include/media/nbaio/AudioBufferProviderSource.h
new file mode 100644
index 0000000..2c4aaff
--- /dev/null
+++ b/include/media/nbaio/AudioBufferProviderSource.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Implementation of NBAIO_Source that wraps an AudioBufferProvider
+
+#ifndef ANDROID_AUDIO_BUFFER_PROVIDER_SOURCE_H
+#define ANDROID_AUDIO_BUFFER_PROVIDER_SOURCE_H
+
+#include "NBAIO.h"
+#include <media/AudioBufferProvider.h>
+
+namespace android {
+
+class AudioBufferProviderSource : public NBAIO_Source {
+
+public:
+    AudioBufferProviderSource(AudioBufferProvider *provider, NBAIO_Format format);
+    virtual ~AudioBufferProviderSource();
+
+    // NBAIO_Port interface
+
+    //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
+    //                          NBAIO_Format counterOffers[], size_t& numCounterOffers);
+    //virtual NBAIO_Format format();
+
+    // NBAIO_Source interface
+
+    //virtual size_t framesRead() const;
+    //virtual size_t framesOverrun();
+    //virtual size_t overruns();
+    virtual ssize_t availableToRead();
+    virtual ssize_t read(void *buffer, size_t count, int64_t readPTS);
+    virtual ssize_t readVia(readVia_t via, size_t total, void *user,
+                            int64_t readPTS, size_t block);
+
+private:
+    AudioBufferProvider * const mProvider;
+    AudioBufferProvider::Buffer mBuffer;    // current buffer
+    size_t                      mConsumed;  // number of frames consumed so far from current buffer
+};
+
+}   // namespace android
+
+#endif  // ANDROID_AUDIO_BUFFER_PROVIDER_SOURCE_H
diff --git a/include/media/nbaio/AudioStreamInSource.h b/include/media/nbaio/AudioStreamInSource.h
new file mode 100644
index 0000000..07d8c89
--- /dev/null
+++ b/include/media/nbaio/AudioStreamInSource.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_STREAM_IN_SOURCE_H
+#define ANDROID_AUDIO_STREAM_IN_SOURCE_H
+
+#include <hardware/audio.h>
+#include "NBAIO.h"
+
+namespace android {
+
+// not multi-thread safe
+class AudioStreamInSource : public NBAIO_Source {
+
+public:
+    AudioStreamInSource(audio_stream_in *stream);
+    virtual ~AudioStreamInSource();
+
+    // NBAIO_Port interface
+
+    virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
+                              NBAIO_Format counterOffers[], size_t& numCounterOffers);
+    //virtual NBAIO_Format format() const;
+
+    // NBAIO_Sink interface
+
+    //virtual size_t framesRead() const;
+    virtual size_t framesOverrun();
+    virtual size_t overruns() { (void) framesOverrun(); return mOverruns; }
+
+    // This is an over-estimate, and could dupe the caller into making a blocking read()
+    // FIXME Use an audio HAL API to query the buffer filling status when it's available.
+    virtual ssize_t availableToRead() { return mStreamBufferSizeBytes >> mBitShift; }
+
+    virtual ssize_t read(void *buffer, size_t count);
+
+    // NBAIO_Sink end
+
+#if 0   // until necessary
+    audio_stream_in *stream() const { return mStream; }
+#endif
+
+private:
+    audio_stream_in * const mStream;
+    size_t              mStreamBufferSizeBytes; // as reported by get_buffer_size()
+    size_t              mFramesOverrun;
+    size_t              mOverruns;
+};
+
+}   // namespace android
+
+#endif  // ANDROID_AUDIO_STREAM_IN_SOURCE_H
diff --git a/include/media/nbaio/AudioStreamOutSink.h b/include/media/nbaio/AudioStreamOutSink.h
new file mode 100644
index 0000000..5976b18
--- /dev/null
+++ b/include/media/nbaio/AudioStreamOutSink.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_STREAM_OUT_SINK_H
+#define ANDROID_AUDIO_STREAM_OUT_SINK_H
+
+#include <hardware/audio.h>
+#include "NBAIO.h"
+
+namespace android {
+
+// not multi-thread safe
+class AudioStreamOutSink : public NBAIO_Sink {
+
+public:
+    AudioStreamOutSink(audio_stream_out *stream);
+    virtual ~AudioStreamOutSink();
+
+    // NBAIO_Port interface
+
+    virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
+                              NBAIO_Format counterOffers[], size_t& numCounterOffers);
+    //virtual NBAIO_Format format();
+
+    // NBAIO_Sink interface
+
+    //virtual size_t framesWritten() const;
+    //virtual size_t framesUnderrun() const;
+    //virtual size_t underruns() const;
+
+    // This is an over-estimate, and could dupe the caller into making a blocking write()
+    // FIXME Use an audio HAL API to query the buffer emptying status when it's available.
+    virtual ssize_t availableToWrite() const { return mStreamBufferSizeBytes >> mBitShift; }
+
+    virtual ssize_t write(const void *buffer, size_t count);
+
+    // AudioStreamOutSink wraps a HAL's output stream.  Its
+    // getNextWriteTimestamp method is simply a passthru to the HAL's underlying
+    // implementation of GNWT (if any)
+    virtual status_t getNextWriteTimestamp(int64_t *timestamp);
+
+    // NBAIO_Sink end
+
+#if 0   // until necessary
+    audio_stream_out *stream() const { return mStream; }
+#endif
+
+private:
+    audio_stream_out * const mStream;
+    size_t              mStreamBufferSizeBytes; // as reported by get_buffer_size()
+};
+
+}   // namespace android
+
+#endif  // ANDROID_AUDIO_STREAM_OUT_SINK_H
diff --git a/include/media/nbaio/LibsndfileSink.h b/include/media/nbaio/LibsndfileSink.h
new file mode 100644
index 0000000..f5d53d5
--- /dev/null
+++ b/include/media/nbaio/LibsndfileSink.h
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_LIBSNDFILE_SINK_H
+#define ANDROID_AUDIO_LIBSNDFILE_SINK_H
+
+#include "NBAIO.h"
+#include "sndfile.h"
+
+// Implementation of NBAIO_Sink that wraps a libsndfile opened in SFM_WRITE mode
+
+namespace android {
+
+class LibsndfileSink : public NBAIO_Sink {
+
+public:
+    LibsndfileSink(SNDFILE *sndfile, const SF_INFO &sfinfo);
+    virtual ~LibsndfileSink();
+
+    // NBAIO_Port interface
+
+    //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
+    //                          NBAIO_Format counterOffers[], size_t& numCounterOffers);
+    //virtual NBAIO_Format format() const;
+
+    // NBAIO_Sink interface
+
+    //virtual size_t framesWritten() const;
+    //virtual size_t framesUnderrun() const;
+    //virtual size_t underruns() const;
+    //virtual ssize_t availableToWrite() const;
+    virtual ssize_t write(const void *buffer, size_t count);
+    //virtual ssize_t writeVia(writeVia_t via, size_t total, void *user, size_t block);
+
+private:
+    SNDFILE *                   mSndfile;
+};
+
+}   // namespace android
+
+#endif  // ANDROID_AUDIO_LIBSNDFILE_SINK_H
diff --git a/include/media/nbaio/LibsndfileSource.h b/include/media/nbaio/LibsndfileSource.h
new file mode 100644
index 0000000..4fbdb4b
--- /dev/null
+++ b/include/media/nbaio/LibsndfileSource.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_LIBSNDFILE_SOURCE_H
+#define ANDROID_AUDIO_LIBSNDFILE_SOURCE_H
+
+#include "NBAIO.h"
+#include "sndfile.h"
+
+// Implementation of NBAIO_Source that wraps a libsndfile opened in SFM_READ mode
+
+namespace android {
+
+class LibsndfileSource : public NBAIO_Source {
+
+public:
+    // If 'loop' is true and it permits seeking, then we'll act as an infinite source
+    LibsndfileSource(SNDFILE *sndfile, const SF_INFO &sfinfo, bool loop = false);
+    virtual ~LibsndfileSource();
+
+    // NBAIO_Port interface
+
+    //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
+    //                          NBAIO_Format counterOffers[], size_t& numCounterOffers);
+    //virtual NBAIO_Format format() const;
+
+    // NBAIO_Source interface
+
+    //virtual size_t framesRead() const;
+    //virtual size_t framesOverrun();
+    //virtual size_t overruns();
+    virtual ssize_t availableToRead();
+    virtual ssize_t read(void *buffer, size_t count);
+    //virtual ssize_t readVia(readVia_t via, size_t total, void *user, size_t block);
+
+private:
+    SNDFILE *   mSndfile;
+    sf_count_t  mEstimatedFramesUntilEOF;
+    bool        mLooping;
+    bool        mReadAnyFramesThisLoopCycle;
+};
+
+}   // namespace android
+
+#endif  // ANDROID_AUDIO_LIBSNDFILE_SOURCE_H
diff --git a/include/media/nbaio/MonoPipe.h b/include/media/nbaio/MonoPipe.h
new file mode 100644
index 0000000..c47bf6c
--- /dev/null
+++ b/include/media/nbaio/MonoPipe.h
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_MONO_PIPE_H
+#define ANDROID_AUDIO_MONO_PIPE_H
+
+#include <time.h>
+#include <utils/LinearTransform.h>
+#include "NBAIO.h"
+
+namespace android {
+
+// MonoPipe is similar to Pipe except:
+//  - supports only a single reader, called MonoPipeReader
+//  - write() cannot overrun; instead it will return a short actual count if insufficient space
+//  - write() can optionally block if the pipe is full
+// Like Pipe, it is not multi-thread safe for either writer or reader
+// but writer and reader can be different threads.
+class MonoPipe : public NBAIO_Sink {
+
+    friend class MonoPipeReader;
+
+public:
+    // reqFrames will be rounded up to a power of 2, and all slots are available. Must be >= 2.
+    // Note: whatever shares this object with another thread needs to do so in an SMP-safe way (like
+    // creating it the object before creating the other thread, or storing the object with a
+    // release_store). Otherwise the other thread could see a partially-constructed object.
+    MonoPipe(size_t reqFrames, NBAIO_Format format, bool writeCanBlock = false);
+    virtual ~MonoPipe();
+
+    // NBAIO_Port interface
+
+    //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
+    //                          NBAIO_Format counterOffers[], size_t& numCounterOffers);
+    //virtual NBAIO_Format format() const;
+
+    // NBAIO_Sink interface
+
+    //virtual size_t framesWritten() const;
+    //virtual size_t framesUnderrun() const;
+    //virtual size_t underruns() const;
+
+    virtual ssize_t availableToWrite() const;
+    virtual ssize_t write(const void *buffer, size_t count);
+    //virtual ssize_t writeVia(writeVia_t via, size_t total, void *user, size_t block);
+
+    // MonoPipe's implementation of getNextWriteTimestamp works in conjunction
+    // with MonoPipeReader.  Every time a MonoPipeReader reads from the pipe, it
+    // receives a "readPTS" indicating the point in time for which the reader
+    // would like to read data.  This "last read PTS" is offset by the amt of
+    // data the reader is currently mixing and then cached cached along with the
+    // updated read pointer.  This cached value is the local time for which the
+    // reader is going to request data next time it reads data (assuming we are
+    // in steady state and operating with no underflows).  Writers to the
+    // MonoPipe who would like to know when their next write operation will hit
+    // the speakers can call getNextWriteTimestamp which will return the value
+    // of the last read PTS plus the duration of the amt of data waiting to be
+    // read in the MonoPipe.
+    virtual status_t getNextWriteTimestamp(int64_t *timestamp);
+
+            // average number of frames present in the pipe under normal conditions.
+            // See throttling mechanism in MonoPipe::write()
+            size_t  getAvgFrames() const { return mSetpoint; }
+            void    setAvgFrames(size_t setpoint);
+            size_t  maxFrames() const { return mMaxFrames; }
+
+private:
+    // A pair of methods and a helper variable which allows the reader and the
+    // writer to update and observe the values of mFront and mNextRdPTS in an
+    // atomic lock-less fashion.
+    //
+    // :: Important ::
+    // Two assumptions must be true in order for this lock-less approach to
+    // function properly on all systems.  First, there may only be one updater
+    // thread in the system.  Second, the updater thread must be running at a
+    // strictly higher priority than the observer threads.  Currently, both of
+    // these assumptions are true.  The only updater is always a single
+    // FastMixer thread (which runs with SCHED_FIFO/RT priority while the only
+    // observer is always an AudioFlinger::PlaybackThread running with
+    // traditional (non-RT) audio priority.
+    void updateFrontAndNRPTS(int32_t newFront, int64_t newNextRdPTS);
+    void observeFrontAndNRPTS(int32_t *outFront, int64_t *outNextRdPTS);
+    volatile int32_t mUpdateSeq;
+
+    const size_t    mReqFrames;     // as requested in constructor, unrounded
+    const size_t    mMaxFrames;     // always a power of 2
+    void * const    mBuffer;
+    // mFront and mRear will never be separated by more than mMaxFrames.
+    // 32-bit overflow is possible if the pipe is active for a long time, but if that happens it's
+    // safe because we "&" with (mMaxFrames-1) at end of computations to calculate a buffer index.
+    volatile int32_t mFront;        // written by the reader with updateFrontAndNRPTS, observed by
+                                    // the writer with observeFrontAndNRPTS
+    volatile int32_t mRear;         // written by writer with android_atomic_release_store,
+                                    // read by reader with android_atomic_acquire_load
+    volatile int64_t mNextRdPTS;    // written by the reader with updateFrontAndNRPTS, observed by
+                                    // the writer with observeFrontAndNRPTS
+    bool            mWriteTsValid;  // whether mWriteTs is valid
+    struct timespec mWriteTs;       // time that the previous write() completed
+    size_t          mSetpoint;      // target value for pipe fill depth
+    const bool      mWriteCanBlock; // whether write() should block if the pipe is full
+
+    int64_t offsetTimestampByAudioFrames(int64_t ts, size_t audFrames);
+    LinearTransform mSamplesToLocalTime;
+};
+
+}   // namespace android
+
+#endif  // ANDROID_AUDIO_MONO_PIPE_H
diff --git a/include/media/nbaio/MonoPipeReader.h b/include/media/nbaio/MonoPipeReader.h
new file mode 100644
index 0000000..0e1c992
--- /dev/null
+++ b/include/media/nbaio/MonoPipeReader.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_MONO_PIPE_READER_H
+#define ANDROID_AUDIO_MONO_PIPE_READER_H
+
+#include "MonoPipe.h"
+
+namespace android {
+
+// MonoPipeReader is safe for only a single reader thread
+class MonoPipeReader : public NBAIO_Source {
+
+public:
+
+    // Construct a MonoPipeReader and associate it with a MonoPipe;
+    // any data already in the pipe is visible to this PipeReader.
+    // There can be only a single MonoPipeReader per MonoPipe.
+    // FIXME make this constructor a factory method of MonoPipe.
+    MonoPipeReader(MonoPipe* pipe);
+    virtual ~MonoPipeReader();
+
+    // NBAIO_Port interface
+
+    //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
+    //                          NBAIO_Format counterOffers[], size_t& numCounterOffers);
+    //virtual NBAIO_Format format() const;
+
+    // NBAIO_Source interface
+
+    //virtual size_t framesRead() const;
+    //virtual size_t framesOverrun();
+    //virtual size_t overruns();
+
+    virtual ssize_t availableToRead();
+
+    virtual ssize_t read(void *buffer, size_t count, int64_t readPTS);
+
+    // NBAIO_Source end
+
+#if 0   // until necessary
+    MonoPipe* pipe() const { return mPipe; }
+#endif
+
+private:
+    MonoPipe * const mPipe;
+};
+
+}   // namespace android
+
+#endif  // ANDROID_AUDIO_MONO_PIPE_READER_H
diff --git a/include/media/nbaio/NBAIO.h b/include/media/nbaio/NBAIO.h
new file mode 100644
index 0000000..81f42ed
--- /dev/null
+++ b/include/media/nbaio/NBAIO.h
@@ -0,0 +1,315 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_NBAIO_H
+#define ANDROID_AUDIO_NBAIO_H
+
+// Non-blocking audio I/O interface
+//
+// This header file has the abstract interfaces only.  Concrete implementation classes are declared
+// elsewhere.  Implementations _should_ be non-blocking for all methods, especially read() and
+// write(), but this is not enforced.  In general, implementations do not need to be multi-thread
+// safe, and any exceptions are noted in the particular implementation.
+
+#include <limits.h>
+#include <stdlib.h>
+#include <utils/Errors.h>
+#include <utils/RefBase.h>
+
+namespace android {
+
+// In addition to the usual status_t
+enum {
+    NEGOTIATE    = 0x80000010,  // Must (re-)negotiate format.  For negotiate() only, the offeree
+                                // doesn't accept offers, and proposes counter-offers
+    OVERRUN      = 0x80000011,  // availableToRead(), read(), or readVia() detected lost input due
+                                // to overrun; an event is counted and the caller should re-try
+    UNDERRUN     = 0x80000012,  // availableToWrite(), write(), or writeVia() detected a gap in
+                                // output due to underrun (not being called often enough, or with
+                                // enough data); an event is counted and the caller should re-try
+};
+
+// Negotiation of format is based on the data provider and data sink, or the data consumer and
+// data source, exchanging prioritized arrays of offers and counter-offers until a single offer is
+// mutually agreed upon.  Each offer is an NBAIO_Format.  For simplicity and performance,
+// NBAIO_Format is an enum that ties together the most important combinations of the various
+// attributes, rather than a struct with separate fields for format, sample rate, channel count,
+// interleave, packing, alignment, etc.  The reason is that NBAIO_Format tries to abstract out only
+// the combinations that are actually needed within AudioFligner.  If the list of combinations grows
+// too large, then this decision should be re-visited.
+enum NBAIO_Format {
+    Format_Invalid,
+    Format_SR44_1_C2_I16,   // 44.1 kHz PCM stereo interleaved 16-bit signed
+    Format_SR48_C2_I16,     // 48 kHz PCM stereo interleaved 16-bit signed
+    Format_SR44_1_C1_I16,   // 44.1 kHz PCM mono interleaved 16-bit signed
+    Format_SR48_C1_I16,     // 48 kHz PCM mono interleaved 16-bit signed
+};
+
+// Return the frame size of an NBAIO_Format in bytes
+size_t Format_frameSize(NBAIO_Format format);
+
+// Return the frame size of an NBAIO_Format as a bit shift
+size_t Format_frameBitShift(NBAIO_Format format);
+
+// Convert a sample rate in Hz and channel count to an NBAIO_Format
+NBAIO_Format Format_from_SR_C(unsigned sampleRate, unsigned channelCount);
+
+// Return the sample rate in Hz of an NBAIO_Format
+unsigned Format_sampleRate(NBAIO_Format format);
+
+// Return the channel count of an NBAIO_Format
+unsigned Format_channelCount(NBAIO_Format format);
+
+// Callbacks used by NBAIO_Sink::writeVia() and NBAIO_Source::readVia() below.
+typedef ssize_t (*writeVia_t)(void *user, void *buffer, size_t count);
+typedef ssize_t (*readVia_t)(void *user, const void *buffer,
+                             size_t count, int64_t readPTS);
+
+// Abstract class (interface) representing a data port.
+class NBAIO_Port : public RefBase {
+
+public:
+
+    // negotiate() must called first.  The purpose of negotiate() is to check compatibility of
+    // formats, not to automatically adapt if they are incompatible.  It's the responsibility of
+    // whoever sets up the graph connections to make sure formats are compatible, and this method
+    // just verifies that.  The edges are "dumb" and don't attempt to adapt to bad connections.
+    // How it works: offerer proposes an array of formats, in descending order of preference from
+    // offers[0] to offers[numOffers - 1].  If offeree accepts one of these formats, it returns
+    // the index of that offer.  Otherwise, offeree sets numCounterOffers to the number of
+    // counter-offers (up to a maximumum of the entry value of numCounterOffers), fills in the
+    // provided array counterOffers[] with its counter-offers, in descending order of preference
+    // from counterOffers[0] to counterOffers[numCounterOffers - 1], and returns NEGOTIATE.
+    // Note that since the offerer allocates space for counter-offers, but only the offeree knows
+    // how many counter-offers it has, there may be insufficient space for all counter-offers.
+    // In that case, the offeree sets numCounterOffers to the requested number of counter-offers
+    // (which is greater than the entry value of numCounterOffers), fills in as many of the most
+    // important counterOffers as will fit, and returns NEGOTIATE.  As this implies a re-allocation,
+    // it should be used as a last resort.  It is preferable for the offerer to simply allocate a
+    // larger space to begin with, and/or for the offeree to tolerate a smaller space than desired.
+    // Alternatively, the offerer can pass NULL for offers and counterOffers, and zero for
+    // numOffers. This indicates that it has not allocated space for any counter-offers yet.
+    // In this case, the offerree should set numCounterOffers appropriately and return NEGOTIATE.
+    // Then the offerer will allocate the correct amount of memory and retry.
+    // Format_Invalid is not allowed as either an offer or counter-offer.
+    // Returns:
+    //  >= 0        Offer accepted.
+    //  NEGOTIATE   No offer accepted, and counter-offer(s) optionally made. See above for details.
+    virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
+                              NBAIO_Format counterOffers[], size_t& numCounterOffers);
+
+    // Return the current negotiated format, or Format_Invalid if negotiation has not been done,
+    // or if re-negotiation is required.
+    virtual NBAIO_Format format() const { return mNegotiated ? mFormat : Format_Invalid; }
+
+protected:
+    NBAIO_Port(NBAIO_Format format) : mNegotiated(false), mFormat(format),
+                                      mBitShift(Format_frameBitShift(format)) { }
+    virtual ~NBAIO_Port() { }
+
+    // Implementations are free to ignore these if they don't need them
+
+    bool            mNegotiated;    // mNegotiated implies (mFormat != Format_Invalid)
+    NBAIO_Format    mFormat;        // (mFormat != Format_Invalid) does not imply mNegotiated
+    size_t          mBitShift;      // assign in parallel with any assignment to mFormat
+};
+
+// Abstract class (interface) representing a non-blocking data sink, for use by a data provider.
+class NBAIO_Sink : public NBAIO_Port {
+
+public:
+
+    // For the next two APIs:
+    // 32 bits rolls over after 27 hours at 44.1 kHz; if that concerns you then poll periodically.
+
+    // Return the number of frames written successfully since construction.
+    virtual size_t framesWritten() const { return mFramesWritten; }
+
+    // Number of frames lost due to underrun since construction.
+    virtual size_t framesUnderrun() const { return 0; }
+
+    // Number of underruns since construction, where a set of contiguous lost frames is one event.
+    virtual size_t underruns() const { return 0; }
+
+    // Estimate of number of frames that could be written successfully now without blocking.
+    // When a write() is actually attempted, the implementation is permitted to return a smaller or
+    // larger transfer count, however it will make a good faith effort to give an accurate estimate.
+    // Errors:
+    //  NEGOTIATE   (Re-)negotiation is needed.
+    //  UNDERRUN    write() has not been called frequently enough, or with enough frames to keep up.
+    //              An underrun event is counted, and the caller should re-try this operation.
+    //  WOULD_BLOCK Determining how many frames can be written without blocking would itself block.
+    virtual ssize_t availableToWrite() const { return SSIZE_MAX; }
+
+    // Transfer data to sink from single input buffer.  Implies a copy.
+    // Inputs:
+    //  buffer  Non-NULL buffer owned by provider.
+    //  count   Maximum number of frames to transfer.
+    // Return value:
+    //  > 0     Number of frames successfully transferred prior to first error.
+    //  = 0     Count was zero.
+    //  < 0     status_t error occurred prior to the first frame transfer.
+    // Errors:
+    //  NEGOTIATE   (Re-)negotiation is needed.
+    //  WOULD_BLOCK No frames can be transferred without blocking.
+    //  UNDERRUN    write() has not been called frequently enough, or with enough frames to keep up.
+    //              An underrun event is counted, and the caller should re-try this operation.
+    virtual ssize_t write(const void *buffer, size_t count) = 0;
+
+    // Transfer data to sink using a series of callbacks.  More suitable for zero-fill, synthesis,
+    // and non-contiguous transfers (e.g. circular buffer or writev).
+    // Inputs:
+    //  via     Callback function that the sink will call as many times as needed to consume data.
+    //  total   Estimate of the number of frames the provider has available.  This is an estimate,
+    //          and it can provide a different number of frames during the series of callbacks.
+    //  user    Arbitrary void * reserved for data provider.
+    //  block   Number of frames per block, that is a suggested value for 'count' in each callback.
+    //          Zero means no preference.  This parameter is a hint only, and may be ignored.
+    // Return value:
+    //  > 0     Total number of frames successfully transferred prior to first error.
+    //  = 0     Count was zero.
+    //  < 0     status_t error occurred prior to the first frame transfer.
+    // Errors:
+    //  NEGOTIATE   (Re-)negotiation is needed.
+    //  WOULD_BLOCK No frames can be transferred without blocking.
+    //  UNDERRUN    write() has not been called frequently enough, or with enough frames to keep up.
+    //              An underrun event is counted, and the caller should re-try this operation.
+    //
+    // The 'via' callback is called by the data sink as follows:
+    // Inputs:
+    //  user    Arbitrary void * reserved for data provider.
+    //  buffer  Non-NULL buffer owned by sink that callback should fill in with data,
+    //          up to a maximum of 'count' frames.
+    //  count   Maximum number of frames to transfer during this callback.
+    // Return value:
+    //  > 0     Number of frames successfully transferred during this callback prior to first error.
+    //  = 0     Count was zero.
+    //  < 0     status_t error occurred prior to the first frame transfer during this callback.
+    virtual ssize_t writeVia(writeVia_t via, size_t total, void *user, size_t block = 0);
+
+    // Get the time (on the LocalTime timeline) at which the first frame of audio of the next write
+    // operation to this sink will be eventually rendered by the HAL.
+    // Inputs:
+    //  ts      A pointer pointing to the int64_t which will hold the result.
+    // Return value:
+    //  OK      Everything went well, *ts holds the time at which the first audio frame of the next
+    //          write operation will be rendered, or AudioBufferProvider::kInvalidPTS if this sink
+    //          does not know the answer for some reason.  Sinks which eventually lead to a HAL
+    //          which implements get_next_write_timestamp may return Invalid temporarily if the DMA
+    //          output of the audio driver has not started yet.  Sinks which lead to a HAL which
+    //          does not implement get_next_write_timestamp, or which don't lead to a HAL at all,
+    //          will always return kInvalidPTS.
+    //  <other> Something unexpected happened internally.  Check the logs and start debugging.
+    virtual status_t getNextWriteTimestamp(int64_t *ts) { return INVALID_OPERATION; }
+
+protected:
+    NBAIO_Sink(NBAIO_Format format = Format_Invalid) : NBAIO_Port(format), mFramesWritten(0) { }
+    virtual ~NBAIO_Sink() { }
+
+    // Implementations are free to ignore these if they don't need them
+    size_t  mFramesWritten;
+};
+
+// Abstract class (interface) representing a non-blocking data source, for use by a data consumer.
+class NBAIO_Source : public NBAIO_Port {
+
+public:
+
+    // For the next two APIs:
+    // 32 bits rolls over after 27 hours at 44.1 kHz; if that concerns you then poll periodically.
+
+    // Number of frames read successfully since construction.
+    virtual size_t framesRead() const { return mFramesRead; }
+
+    // Number of frames lost due to overrun since construction.
+    // Not const because implementations may need to do I/O.
+    virtual size_t framesOverrun() /*const*/ { return 0; }
+
+    // Number of overruns since construction, where a set of contiguous lost frames is one event.
+    // Not const because implementations may need to do I/O.
+    virtual size_t overruns() /*const*/ { return 0; }
+
+    // Estimate of number of frames that could be read successfully now.
+    // When a read() is actually attempted, the implementation is permitted to return a smaller or
+    // larger transfer count, however it will make a good faith effort to give an accurate estimate.
+    // Errors:
+    //  NEGOTIATE   (Re-)negotiation is needed.
+    //  OVERRUN     One or more frames were lost due to overrun, try again to read more recent data.
+    //  WOULD_BLOCK Determining how many frames can be read without blocking would itself block.
+    virtual ssize_t availableToRead() { return SSIZE_MAX; }
+
+    // Transfer data from source into single destination buffer.  Implies a copy.
+    // Inputs:
+    //  buffer  Non-NULL destination buffer owned by consumer.
+    //  count   Maximum number of frames to transfer.
+    //  readPTS The presentation time (on the LocalTime timeline) for which data
+    //          is being requested, or kInvalidPTS if not known.
+    // Return value:
+    //  > 0     Number of frames successfully transferred prior to first error.
+    //  = 0     Count was zero.
+    //  < 0     status_t error occurred prior to the first frame transfer.
+    // Errors:
+    //  NEGOTIATE   (Re-)negotiation is needed.
+    //  WOULD_BLOCK No frames can be transferred without blocking.
+    //  OVERRUN     read() has not been called frequently enough, or with enough frames to keep up.
+    //              One or more frames were lost due to overrun, try again to read more recent data.
+    virtual ssize_t read(void *buffer, size_t count, int64_t readPTS) = 0;
+
+    // Transfer data from source using a series of callbacks.  More suitable for zero-fill,
+    // synthesis, and non-contiguous transfers (e.g. circular buffer or readv).
+    // Inputs:
+    //  via     Callback function that the source will call as many times as needed to provide data.
+    //  total   Estimate of the number of frames the consumer desires.  This is an estimate,
+    //          and it can consume a different number of frames during the series of callbacks.
+    //  user    Arbitrary void * reserved for data consumer.
+    //  readPTS The presentation time (on the LocalTime timeline) for which data
+    //          is being requested, or kInvalidPTS if not known.
+    //  block   Number of frames per block, that is a suggested value for 'count' in each callback.
+    //          Zero means no preference.  This parameter is a hint only, and may be ignored.
+    // Return value:
+    //  > 0     Total number of frames successfully transferred prior to first error.
+    //  = 0     Count was zero.
+    //  < 0     status_t error occurred prior to the first frame transfer.
+    // Errors:
+    //  NEGOTIATE   (Re-)negotiation is needed.
+    //  WOULD_BLOCK No frames can be transferred without blocking.
+    //  OVERRUN     read() has not been called frequently enough, or with enough frames to keep up.
+    //              One or more frames were lost due to overrun, try again to read more recent data.
+    //
+    // The 'via' callback is called by the data source as follows:
+    // Inputs:
+    //  user    Arbitrary void * reserved for data consumer.
+    //  dest    Non-NULL buffer owned by source that callback should consume data from,
+    //          up to a maximum of 'count' frames.
+    //  count   Maximum number of frames to transfer during this callback.
+    // Return value:
+    //  > 0     Number of frames successfully transferred during this callback prior to first error.
+    //  = 0     Count was zero.
+    //  < 0     status_t error occurred prior to the first frame transfer during this callback.
+    virtual ssize_t readVia(readVia_t via, size_t total, void *user,
+                            int64_t readPTS, size_t block = 0);
+
+protected:
+    NBAIO_Source(NBAIO_Format format = Format_Invalid) : NBAIO_Port(format), mFramesRead(0) { }
+    virtual ~NBAIO_Source() { }
+
+    // Implementations are free to ignore these if they don't need them
+    size_t  mFramesRead;
+};
+
+}   // namespace android
+
+#endif  // ANDROID_AUDIO_NBAIO_H
diff --git a/include/media/nbaio/Pipe.h b/include/media/nbaio/Pipe.h
new file mode 100644
index 0000000..79a4eee
--- /dev/null
+++ b/include/media/nbaio/Pipe.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_PIPE_H
+#define ANDROID_AUDIO_PIPE_H
+
+#include "NBAIO.h"
+
+namespace android {
+
+// Pipe is multi-thread safe for readers (see PipeReader), but safe for only a single writer thread.
+// It cannot UNDERRUN on write, unless we allow designation of a master reader that provides the
+// time-base. Readers can be added and removed dynamically, and it's OK to have no readers.
+class Pipe : public NBAIO_Sink {
+
+    friend class PipeReader;
+
+public:
+    // maxFrames will be rounded up to a power of 2, and all slots are available. Must be >= 2.
+    Pipe(size_t maxFrames, NBAIO_Format format);
+    virtual ~Pipe();
+
+    // NBAIO_Port interface
+
+    //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
+    //                          NBAIO_Format counterOffers[], size_t& numCounterOffers);
+    //virtual NBAIO_Format format() const;
+
+    // NBAIO_Sink interface
+
+    //virtual size_t framesWritten() const;
+    //virtual size_t framesUnderrun() const;
+    //virtual size_t underruns() const;
+
+    // The write side of a pipe permits overruns; flow control is the caller's responsibility.
+    // It doesn't return +infinity because that would guarantee an overrun.
+    virtual ssize_t availableToWrite() const { return mMaxFrames; }
+
+    virtual ssize_t write(const void *buffer, size_t count);
+    //virtual ssize_t writeVia(writeVia_t via, size_t total, void *user, size_t block);
+
+private:
+    const size_t    mMaxFrames;     // always a power of 2
+    void * const    mBuffer;
+    volatile int32_t mRear;         // written by android_atomic_release_store
+    volatile int32_t mReaders;      // number of PipeReader clients currently attached to this Pipe
+};
+
+}   // namespace android
+
+#endif  // ANDROID_AUDIO_PIPE_H
diff --git a/include/media/nbaio/PipeReader.h b/include/media/nbaio/PipeReader.h
new file mode 100644
index 0000000..350e6ab
--- /dev/null
+++ b/include/media/nbaio/PipeReader.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_PIPE_READER_H
+#define ANDROID_AUDIO_PIPE_READER_H
+
+#include "Pipe.h"
+
+namespace android {
+
+// PipeReader is safe for only a single thread
+class PipeReader : public NBAIO_Source {
+
+public:
+
+    // Construct a PipeReader and associate it with a Pipe
+    // FIXME make this constructor a factory method of Pipe.
+    PipeReader(Pipe& pipe);
+    virtual ~PipeReader();
+
+    // NBAIO_Port interface
+
+    //virtual ssize_t negotiate(const NBAIO_Format offers[], size_t numOffers,
+    //                          NBAIO_Format counterOffers[], size_t& numCounterOffers);
+    //virtual NBAIO_Format format() const;
+
+    // NBAIO_Source interface
+
+    //virtual size_t framesRead() const;
+    virtual size_t framesOverrun() { return mFramesOverrun; }
+    virtual size_t overruns()  { return mOverruns; }
+
+    virtual ssize_t availableToRead();
+
+    virtual ssize_t read(void *buffer, size_t count, int64_t readPTS);
+
+    // NBAIO_Source end
+
+#if 0   // until necessary
+    Pipe& pipe() const { return mPipe; }
+#endif
+
+private:
+    Pipe&       mPipe;
+    int32_t     mFront;         // follows behind mPipe.mRear
+    size_t      mFramesOverrun;
+    size_t      mOverruns;
+};
+
+}   // namespace android
+
+#endif  // ANDROID_AUDIO_PIPE_READER_H
diff --git a/include/media/nbaio/SourceAudioBufferProvider.h b/include/media/nbaio/SourceAudioBufferProvider.h
new file mode 100644
index 0000000..c08331b
--- /dev/null
+++ b/include/media/nbaio/SourceAudioBufferProvider.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Implementation of AudioBufferProvider that wraps an NBAIO_Source
+
+#ifndef ANDROID_SOURCE_AUDIO_BUFFER_PROVIDER_H
+#define ANDROID_SOURCE_AUDIO_BUFFER_PROVIDER_H
+
+#include "NBAIO.h"
+#include <media/ExtendedAudioBufferProvider.h>
+
+namespace android {
+
+class SourceAudioBufferProvider : public ExtendedAudioBufferProvider {
+
+public:
+    SourceAudioBufferProvider(const sp<NBAIO_Source>& source);
+    virtual ~SourceAudioBufferProvider();
+
+    // AudioBufferProvider interface
+    virtual status_t getNextBuffer(Buffer *buffer, int64_t pts);
+    virtual void     releaseBuffer(Buffer *buffer);
+
+    // ExtendedAudioBufferProvider interface
+    virtual size_t   framesReady() const;
+
+private:
+    const sp<NBAIO_Source> mSource;     // the wrapped source
+    /*const*/ size_t    mFrameBitShift; // log2(frame size in bytes)
+    void*               mAllocated; // pointer to base of allocated memory
+    size_t              mSize;      // size of mAllocated in frames
+    size_t              mOffset;    // frame offset within mAllocated of valid data
+    size_t              mRemaining; // frame count within mAllocated of valid data
+    size_t              mGetCount;  // buffer.frameCount of the most recent getNextBuffer
+};
+
+}   // namespace android
+
+#endif  // ANDROID_SOURCE_AUDIO_BUFFER_PROVIDER_H
diff --git a/include/media/nbaio/roundup.h b/include/media/nbaio/roundup.h
new file mode 100644
index 0000000..4c3cc25
--- /dev/null
+++ b/include/media/nbaio/roundup.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ROUNDUP_H
+#define ROUNDUP_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Round up to the next highest power of 2
+unsigned roundup(unsigned v);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // ROUNDUP_H