1a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk/*
2a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * Copyright 2015, The Android Open Source Project
3a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk *
4a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * Licensed under the Apache License, Version 2.0 (the "License");
5a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * you may not use this file except in compliance with the License.
6a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * You may obtain a copy of the License at
7a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk *
8a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk *      http://www.apache.org/licenses/LICENSE-2.0
9a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk *
10a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * Unless required by applicable law or agreed to in writing, software
11a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * distributed under the License is distributed on an "AS IS" BASIS,
12a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * See the License for the specific language governing permissions and
14a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * limitations under the License.
15a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk */
16a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
17a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#ifndef ANDROID_AUDIO_BIT_FIELD_PARSER_H
18a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#define ANDROID_AUDIO_BIT_FIELD_PARSER_H
19a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
20a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#include <stdint.h>
21a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
22a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burknamespace android {
23a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
24a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk/**
25a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * Extract bit fields from a byte array.
26a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk */
27a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burkclass BitFieldParser {
28a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burkpublic:
29a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
30a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    BitFieldParser(uint8_t *data);
31a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    virtual ~BitFieldParser();
32a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
33a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    /**
34a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk     * Read numBits bits from the data array.
35a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk     * Fields may span byte boundaries but may not exceed 32-bits.
36a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk     * Note that the caller must ensure that there is suffcient data.
37a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk     * Assume data is organized as BigEndian format.
38a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk     */
39a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    uint32_t readBits(uint32_t numBits);
40a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
41a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    /*
42a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk     * When the cursor is zero it points to a position right before
43a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk     * the most significant bit.
44a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk     * When the cursor is seven it points to a position right before
45a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk     * the least significant bit.
46a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk     */
47a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    uint32_t getBitCursor() const { return mBitCursor; }
48a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
49a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burkprivate:
50a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    uint8_t *mData;
51a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    uint32_t mBitCursor;
52a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk};
53a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
54a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
55a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk}  // namespace android
56a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
57a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#endif  // ANDROID_AUDIO_BIT_FIELD_PARSER_H
58