3 ** Copyright 2014, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define LOG_TAG "AudioHardwareTungsten"
20 #include <utils/Log.h>
21 #include <audio_utils/spdif/FrameScanner.h>
25 #define SPDIF_DATA_TYPE_AC3 1
26 #define SPDIF_DATA_TYPE_E_AC3 21
28 #define AC3_SYNCWORD_SIZE 2
30 FrameScanner::FrameScanner(int dataType)
39 FrameScanner::~FrameScanner()
43 // ------------------- AC3 -----------------------------------------------------
44 // These values are from the AC3 spec. Do not change them.
45 const uint8_t AC3FrameScanner::kAC3SyncByte1 = 0x0B;
46 const uint8_t AC3FrameScanner::kAC3SyncByte2 = 0x77;
48 const uint16_t AC3FrameScanner::kAC3SampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES]
49 = { 48000, 44100, 32000 };
51 // Table contains number of 16-bit words in an AC3 frame.
52 const uint16_t AC3FrameScanner::kAC3FrameSizeTable[AC3_NUM_FRAME_SIZE_TABLE_ENTRIES]
53 [AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES] = {
94 const uint16_t AC3FrameScanner::kEAC3ReducedSampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES]
95 = { 24000, 22050, 16000 };
98 AC3FrameScanner::kEAC3BlocksPerFrameTable[EAC3_NUM_BLOCKS_PER_FRAME_TABLE_ENTRIES]
100 // -----------------------------------------------------------------------------
102 // Scanner for AC3 byte streams.
103 AC3FrameScanner::AC3FrameScanner()
104 : FrameScanner(SPDIF_DATA_TYPE_AC3)
105 , mState(STATE_EXPECTING_SYNC_1)
108 , mAudioBlocksPerSyncFrame(6)
109 , mCursor(AC3_SYNCWORD_SIZE) // past sync word
112 , mFormatDumpCount(0)
114 // Define beginning of syncinfo for getSyncAddress()
115 mHeaderBuffer[0] = kAC3SyncByte1;
116 mHeaderBuffer[1] = kAC3SyncByte2;
119 AC3FrameScanner::~AC3FrameScanner()
123 int AC3FrameScanner::getSampleFramesPerSyncFrame() const
125 return mRateMultiplier * AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK * AC3_PCM_FRAMES_PER_BLOCK;
128 // per IEC 61973-3 Paragraph 5.3.3
129 bool AC3FrameScanner::isFirstInBurst()
131 if (mDataType == SPDIF_DATA_TYPE_E_AC3) {
132 return (((mStreamType == 0) || (mStreamType == 2)) && (mSubstreamID == 0));
134 return false; // For AC3 just flush at end.
138 bool AC3FrameScanner::isLastInBurst()
140 // For EAC3 we don't know if we are the end until we see a
141 // frame that must be at the beginning. See isFirstInBurst().
142 return (mDataType != SPDIF_DATA_TYPE_E_AC3); // Just one AC3 frame per burst.
146 // Detect whether the stream is AC3 or EAC3. Extract data depending on type.
147 // Sets mDataType, mFrameSizeBytes, mAudioBlocksPerSyncFrame,
148 // mSampleRate, mRateMultiplier, mLengthCode.
150 // @return next state for scanner
151 AC3FrameScanner::State AC3FrameScanner::parseHeader()
153 // Interpret bsid based on paragraph E2.3.1.6 of EAC3 spec.
154 int bsid = mHeaderBuffer[5] >> 3; // bitstream ID
155 // Check BSID to see if this is EAC3 or regular AC3
156 if ((bsid >= 10) && (bsid <= 16)) {
157 mDataType = SPDIF_DATA_TYPE_E_AC3;
158 } else if ((bsid >= 0) && (bsid <= 8)) {
159 mDataType = SPDIF_DATA_TYPE_AC3;
161 ALOGW("AC3 bsid = %d not supported", bsid);
162 return STATE_EXPECTING_SYNC_1;
165 // The names fscod, frmsiz are from the AC3 spec.
166 int fscod = mHeaderBuffer[4] >> 6;
167 if (mDataType == SPDIF_DATA_TYPE_E_AC3) {
168 mStreamType = mHeaderBuffer[2] >> 6;
169 mSubstreamID = (mHeaderBuffer[2] >> 3) & 0x07;
171 // Print enough so we can see all the substreams.
172 ALOGD_IF((mFormatDumpCount < 3*8 ),
173 "EAC3 strmtyp = %d, substreamid = %d",
174 mStreamType, mSubstreamID);
176 // Frame size is explicit in EAC3. Paragraph E2.3.1.3
177 int frmsiz = ((mHeaderBuffer[2] & 0x07) << 8) + mHeaderBuffer[3];
178 mFrameSizeBytes = (frmsiz + 1) * sizeof(int16_t);
180 int numblkscod = 3; // 6 blocks default
182 int fscod2 = (mHeaderBuffer[4] >> 4) & 0x03;
183 if (fscod2 >= AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES) {
184 ALOGW("Invalid EAC3 fscod2 = %d\n", fscod2);
185 return STATE_EXPECTING_SYNC_1;
187 mSampleRate = kEAC3ReducedSampleRateTable[fscod2];
190 mSampleRate = kAC3SampleRateTable[fscod];
191 numblkscod = (mHeaderBuffer[4] >> 4) & 0x03;
193 mRateMultiplier = EAC3_RATE_MULTIPLIER; // per IEC 61973-3 Paragraph 5.3.3
194 // TODO Don't send data burst until we have 6 blocks per substream.
195 mAudioBlocksPerSyncFrame = kEAC3BlocksPerFrameTable[numblkscod];
196 } else { // regular AC3
197 // Extract sample rate and frame size from codes.
198 unsigned int frmsizcod = mHeaderBuffer[4] & 0x3F; // frame size code
200 if (fscod >= AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES) {
201 ALOGW("Invalid AC3 sampleRateCode = %d\n", fscod);
202 return STATE_EXPECTING_SYNC_1;
203 } else if (frmsizcod >= AC3_NUM_FRAME_SIZE_TABLE_ENTRIES) {
204 ALOGW("Invalid AC3 frameSizeCode = %d\n", frmsizcod);
205 return STATE_EXPECTING_SYNC_1;
207 mSampleRate = kAC3SampleRateTable[fscod];
209 mFrameSizeBytes = sizeof(uint16_t)
210 * kAC3FrameSizeTable[frmsizcod][fscod];
212 mAudioBlocksPerSyncFrame = 6;
214 mLengthCode = 8 * mFrameSizeBytes; // size in bits
215 ALOGI_IF((mFormatDumpCount == 0),
216 "AC3 frame rate = %d * %d, size = %d, audioBlocksPerSyncFrame = %d\n",
217 mSampleRate, mRateMultiplier, mFrameSizeBytes, mAudioBlocksPerSyncFrame);
219 return STATE_GOT_HEADER;
222 // State machine that scans for AC3 headers in a byte stream.
223 // @return true if we have detected a complete and valid header.
224 bool AC3FrameScanner::scan(uint8_t byte)
226 State nextState = mState;
228 case STATE_GOT_HEADER:
229 nextState = STATE_EXPECTING_SYNC_1;
230 // deliberately fall through
231 case STATE_EXPECTING_SYNC_1:
232 if (byte == kAC3SyncByte1) {
233 nextState = STATE_EXPECTING_SYNC_2; // advance
235 mBytesSkipped += 1; // skip unsynchronized data
239 case STATE_EXPECTING_SYNC_2:
240 if (byte == kAC3SyncByte2) {
241 if (mBytesSkipped > 0) {
242 ALOGI("WARNING AC3 skipped %u bytes looking for a valid header.\n", mBytesSkipped);
245 mCursor = AC3_SYNCWORD_SIZE;
246 nextState = STATE_GATHERING; // advance
247 } else if (byte == kAC3SyncByte1) {
248 nextState = STATE_EXPECTING_SYNC_2; // resync
250 nextState = STATE_EXPECTING_SYNC_1; // restart
254 case STATE_GATHERING:
255 mHeaderBuffer[mCursor++] = byte; // save for getSyncAddress()
256 if (mCursor >= sizeof(mHeaderBuffer)) {
257 nextState = parseHeader();
262 ALOGE("AC3FrameScanner: invalid state = %d\n", mState);
263 nextState = STATE_EXPECTING_SYNC_1; // restart
267 return mState == STATE_GOT_HEADER;
270 } // namespace android