1/*
2 * Copyright 2014, 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#ifndef ANDROID_AUDIO_AC3_FRAME_SCANNER_H
18#define ANDROID_AUDIO_AC3_FRAME_SCANNER_H
19
20#include <stdint.h>
21#include <system/audio.h>
22#include <audio_utils/spdif/FrameScanner.h>
23
24namespace android {
25
26#define AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES          3
27#define AC3_NUM_FRAME_SIZE_TABLE_ENTRIES          38
28#define AC3_PCM_FRAMES_PER_BLOCK                 256
29#define AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK        6
30#define EAC3_RATE_MULTIPLIER                       4
31#define EAC3_NUM_SAMPLE_RATE_TABLE_ENTRIES         3
32#define EAC3_NUM_BLOCKS_PER_FRAME_TABLE_ENTRIES   38
33#define EAC3_MAX_SUBSTREAMS                        8
34
35class AC3FrameScanner : public FrameScanner
36{
37public:
38    explicit AC3FrameScanner(audio_format_t format);
39    virtual ~AC3FrameScanner();
40
41    virtual int getMaxChannels()   const { return 5 + 1; } // 5.1 surround
42
43    virtual int getMaxSampleFramesPerSyncFrame() const { return EAC3_RATE_MULTIPLIER
44            * AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK * AC3_PCM_FRAMES_PER_BLOCK; }
45    virtual int getSampleFramesPerSyncFrame() const;
46
47    virtual bool isFirstInBurst();
48    virtual bool isLastInBurst();
49    virtual void resetBurst();
50
51    virtual uint16_t convertBytesToLengthCode(uint16_t numBytes) const;
52
53protected:
54    // Keep track of how many of each substream blocks have been accumulated.
55    // We need all of each substream before sending block data burst.
56    uint8_t   mSubstreamBlockCounts[EAC3_MAX_SUBSTREAMS];
57    int       mAudioBlocksPerSyncFrame;
58    // The type of EAC3 stream as per EAC3 spec paragraph 2.3.1.1
59    uint32_t  mStreamType;
60    // substream index
61    uint32_t  mSubstreamID;
62    audio_format_t mFormat;
63
64    // used to recognize the start of an AC3 sync frame
65    static const uint8_t  kSyncBytes[];
66    // sample rates from AC3 spec table 5.1
67    static const uint16_t kAC3SampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES];
68    // frame sizes from AC3 spec table 5.13
69    static const uint16_t kAC3FrameSizeTable[AC3_NUM_FRAME_SIZE_TABLE_ENTRIES]
70            [AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES];
71    // sample rates from EAC3 spec table E2.3
72    static const uint16_t kEAC3ReducedSampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES];
73    // audio blocks per frame from EAC3 spec table E2.4
74    static const uint16_t kEAC3BlocksPerFrameTable[EAC3_NUM_BLOCKS_PER_FRAME_TABLE_ENTRIES];
75
76    virtual bool parseHeader();
77};
78
79}  // namespace android
80
81#endif  // ANDROID_AUDIO_AC3_FRAME_SCANNER_H
82