1 /* 2 ** 3 ** Copyright 2014, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 #ifndef ANDROID_AUDIO_FRAME_SCANNER_H 19 #define ANDROID_AUDIO_FRAME_SCANNER_H 20 21 #include <stdint.h> 22 23 namespace android { 24 25 26 /** 27 * Scan a byte stream looking for the start of an encoded frame. 28 * Parse the sample rate and the size of the encoded frame. 29 * Buffer the sync header so it can be prepended to the remaining data. 30 * 31 * This is used directly by the SPDIFEncoder. External clients will 32 * generally not call this class. 33 */ 34 class FrameScanner { 35 public: 36 37 FrameScanner(int dataType); 38 virtual ~FrameScanner(); 39 40 /** 41 * Pass each byte of the encoded stream to this scanner. 42 * @return true if a complete and valid header was detected 43 */ 44 virtual bool scan(uint8_t) = 0; 45 46 /** 47 * @return address of where the sync header was stored by scan() 48 */ 49 virtual const uint8_t *getHeaderAddress() const = 0; 50 51 /** 52 * @return number of bytes in sync header stored by scan() 53 */ 54 virtual size_t getHeaderSizeBytes() const = 0; 55 56 /** 57 * @return sample rate of the encoded audio 58 */ 59 uint32_t getSampleRate() const { return mSampleRate; } 60 61 /** 62 * Some formats, for example EAC3, are wrapped in data bursts that have 63 * a sample rate that is a multiple of the encoded sample rate. 64 * The default multiplier is 1. 65 * @return sample rate multiplier for the SP/DIF PCM data bursts 66 */ 67 uint32_t getRateMultiplier() const { return mRateMultiplier; } 68 69 size_t getFrameSizeBytes() const { return mFrameSizeBytes; } 70 71 /** 72 * dataType is defined by the SPDIF standard for each format 73 */ 74 virtual int getDataType() const { return mDataType; } 75 virtual int getDataTypeInfo() const { return mDataTypeInfo; } 76 77 virtual int getMaxChannels() const = 0; 78 79 virtual void resetBurst() = 0; 80 81 /** 82 * @return the number of pcm frames that correspond to one encoded frame 83 */ 84 virtual int getMaxSampleFramesPerSyncFrame() const = 0; 85 virtual int getSampleFramesPerSyncFrame() const = 0; 86 87 /** 88 * @return true if this parsed frame must be the first frame in a data burst. 89 */ 90 virtual bool isFirstInBurst() = 0; 91 92 /** 93 * If this returns false then the previous frame may or may not be the last frame. 94 * @return true if this parsed frame is definitely the last frame in a data burst. 95 */ 96 virtual bool isLastInBurst() = 0; 97 98 protected: 99 uint32_t mSampleRate; 100 uint32_t mRateMultiplier; 101 size_t mFrameSizeBytes; 102 int mDataType; 103 int mDataTypeInfo; 104 }; 105 106 #define AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES 3 107 #define AC3_NUM_FRAME_SIZE_TABLE_ENTRIES 38 108 #define AC3_PCM_FRAMES_PER_BLOCK 256 109 #define AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK 6 110 #define EAC3_RATE_MULTIPLIER 4 111 #define EAC3_NUM_SAMPLE_RATE_TABLE_ENTRIES 3 112 #define EAC3_NUM_BLOCKS_PER_FRAME_TABLE_ENTRIES 38 113 #define EAC3_MAX_SUBSTREAMS 8 114 115 class AC3FrameScanner : public FrameScanner 116 { 117 public: 118 AC3FrameScanner(); 119 virtual ~AC3FrameScanner(); 120 121 virtual bool scan(uint8_t); 122 123 virtual const uint8_t *getHeaderAddress() const { return mHeaderBuffer; } 124 virtual size_t getHeaderSizeBytes() const { return sizeof(mHeaderBuffer); } 125 126 virtual int getDataType() const { return mDataType; } 127 virtual int getDataTypeInfo() const { return 0; } 128 virtual int getMaxChannels() const { return 5 + 1; } 129 130 virtual int getMaxSampleFramesPerSyncFrame() const { return EAC3_RATE_MULTIPLIER 131 * AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK * AC3_PCM_FRAMES_PER_BLOCK; } 132 virtual int getSampleFramesPerSyncFrame() const; 133 134 virtual bool isFirstInBurst(); 135 virtual bool isLastInBurst(); 136 virtual void resetBurst(); 137 138 protected: 139 140 // Preamble state machine states. 141 enum State { 142 STATE_EXPECTING_SYNC_1, 143 STATE_EXPECTING_SYNC_2, 144 STATE_GATHERING, 145 STATE_GOT_HEADER, 146 }; 147 148 State parseHeader(void); 149 150 State mState; 151 uint32_t mBytesSkipped; 152 uint8_t mHeaderBuffer[6]; 153 uint8_t mSubstreamBlockCounts[EAC3_MAX_SUBSTREAMS]; 154 int mAudioBlocksPerSyncFrame; 155 uint mCursor; 156 uint mStreamType; 157 uint mSubstreamID; 158 uint mFormatDumpCount; 159 160 static const uint8_t kAC3SyncByte1; 161 static const uint8_t kAC3SyncByte2; 162 static const uint16_t kAC3SampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES]; 163 static const uint16_t kAC3FrameSizeTable[AC3_NUM_FRAME_SIZE_TABLE_ENTRIES] 164 [AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES]; 165 166 static const uint16_t kEAC3ReducedSampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES]; 167 static const uint16_t kEAC3BlocksPerFrameTable[EAC3_NUM_BLOCKS_PER_FRAME_TABLE_ENTRIES]; 168 169 }; 170 171 } // namespace android 172 #endif // ANDROID_AUDIO_FRAME_SCANNER_H 173