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#define LOG_TAG "AudioHardwareTungsten" 19 20#include <utils/Log.h> 21#include <audio_utils/spdif/FrameScanner.h> 22 23namespace android { 24 25#define SPDIF_DATA_TYPE_AC3 1 26#define SPDIF_DATA_TYPE_E_AC3 21 27 28#define AC3_SYNCWORD_SIZE 2 29 30FrameScanner::FrameScanner(int dataType) 31 : mSampleRate(0) 32 , mRateMultiplier(1) 33 , mFrameSizeBytes(0) 34 , mDataType(dataType) 35 , mDataTypeInfo(0) 36{ 37} 38 39FrameScanner::~FrameScanner() 40{ 41} 42 43// ------------------- AC3 ----------------------------------------------------- 44// These values are from the AC3 spec. Do not change them. 45const uint8_t AC3FrameScanner::kAC3SyncByte1 = 0x0B; 46const uint8_t AC3FrameScanner::kAC3SyncByte2 = 0x77; 47 48const uint16_t AC3FrameScanner::kAC3SampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES] 49 = { 48000, 44100, 32000 }; 50 51// Table contains number of 16-bit words in an AC3 frame. 52const uint16_t AC3FrameScanner::kAC3FrameSizeTable[AC3_NUM_FRAME_SIZE_TABLE_ENTRIES] 53 [AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES] = { 54 { 64, 69, 96 }, 55 { 64, 70, 96 }, 56 { 80, 87, 120 }, 57 { 80, 88, 120 }, 58 { 96, 104, 144 }, 59 { 96, 105, 144 }, 60 { 112, 121, 168 }, 61 { 112, 122, 168 }, 62 { 128, 139, 192 }, 63 { 128, 140, 192 }, 64 { 160, 174, 240 }, 65 { 160, 175, 240 }, 66 { 192, 208, 288 }, 67 { 192, 209, 288 }, 68 { 224, 243, 336 }, 69 { 224, 244, 336 }, 70 { 256, 278, 384 }, 71 { 256, 279, 384 }, 72 { 320, 348, 480 }, 73 { 320, 349, 480 }, 74 { 384, 417, 576 }, 75 { 384, 418, 576 }, 76 { 448, 487, 672 }, 77 { 448, 488, 672 }, 78 { 512, 557, 768 }, 79 { 512, 558, 768 }, 80 { 640, 696, 960 }, 81 { 640, 697, 960 }, 82 { 768, 835, 1152 }, 83 { 768, 836, 1152 }, 84 { 896, 975, 1344 }, 85 { 896, 976, 1344 }, 86 { 1024, 1114, 1536 }, 87 { 1024, 1115, 1536 }, 88 { 1152, 1253, 1728 }, 89 { 1152, 1254, 1728 }, 90 { 1280, 1393, 1920 }, 91 { 1280, 1394, 1920 } 92}; 93 94const uint16_t AC3FrameScanner::kEAC3ReducedSampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES] 95 = { 24000, 22050, 16000 }; 96 97const uint16_t 98 AC3FrameScanner::kEAC3BlocksPerFrameTable[EAC3_NUM_BLOCKS_PER_FRAME_TABLE_ENTRIES] 99 = { 1, 2, 3, 6 }; 100// ----------------------------------------------------------------------------- 101 102// Scanner for AC3 byte streams. 103AC3FrameScanner::AC3FrameScanner() 104 : FrameScanner(SPDIF_DATA_TYPE_AC3) 105 , mState(STATE_EXPECTING_SYNC_1) 106 , mBytesSkipped(0) 107 , mLengthCode(0) 108 , mAudioBlocksPerSyncFrame(6) 109 , mCursor(AC3_SYNCWORD_SIZE) // past sync word 110 , mStreamType(0) 111 , mSubstreamID(0) 112 , mFormatDumpCount(0) 113{ 114 // Define beginning of syncinfo for getSyncAddress() 115 mHeaderBuffer[0] = kAC3SyncByte1; 116 mHeaderBuffer[1] = kAC3SyncByte2; 117} 118 119AC3FrameScanner::~AC3FrameScanner() 120{ 121} 122 123int AC3FrameScanner::getSampleFramesPerSyncFrame() const 124{ 125 return mRateMultiplier * AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK * AC3_PCM_FRAMES_PER_BLOCK; 126} 127 128// per IEC 61973-3 Paragraph 5.3.3 129bool AC3FrameScanner::isFirstInBurst() 130{ 131 if (mDataType == SPDIF_DATA_TYPE_E_AC3) { 132 return (((mStreamType == 0) || (mStreamType == 2)) && (mSubstreamID == 0)); 133 } else { 134 return false; // For AC3 just flush at end. 135 } 136} 137 138bool AC3FrameScanner::isLastInBurst() 139{ 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. 143} 144 145// Parse AC3 header. 146// Detect whether the stream is AC3 or EAC3. Extract data depending on type. 147// Sets mDataType, mFrameSizeBytes, mAudioBlocksPerSyncFrame, 148// mSampleRate, mRateMultiplier, mLengthCode. 149// 150// @return next state for scanner 151AC3FrameScanner::State AC3FrameScanner::parseHeader() 152{ 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; 160 } else { 161 ALOGW("AC3 bsid = %d not supported", bsid); 162 return STATE_EXPECTING_SYNC_1; 163 } 164 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; 170 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); 175 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); 179 180 int numblkscod = 3; // 6 blocks default 181 if (fscod == 3) { 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; 186 } else { 187 mSampleRate = kEAC3ReducedSampleRateTable[fscod2]; 188 } 189 } else { 190 mSampleRate = kAC3SampleRateTable[fscod]; 191 numblkscod = (mHeaderBuffer[4] >> 4) & 0x03; 192 } 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 199 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; 206 } else { 207 mSampleRate = kAC3SampleRateTable[fscod]; 208 mRateMultiplier = 1; 209 mFrameSizeBytes = sizeof(uint16_t) 210 * kAC3FrameSizeTable[frmsizcod][fscod]; 211 } 212 mAudioBlocksPerSyncFrame = 6; 213 } 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); 218 mFormatDumpCount++; 219 return STATE_GOT_HEADER; 220} 221 222// State machine that scans for AC3 headers in a byte stream. 223// @return true if we have detected a complete and valid header. 224bool AC3FrameScanner::scan(uint8_t byte) 225{ 226 State nextState = mState; 227 switch (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 234 } else { 235 mBytesSkipped += 1; // skip unsynchronized data 236 } 237 break; 238 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); 243 mBytesSkipped = 0; 244 } 245 mCursor = AC3_SYNCWORD_SIZE; 246 nextState = STATE_GATHERING; // advance 247 } else if (byte == kAC3SyncByte1) { 248 nextState = STATE_EXPECTING_SYNC_2; // resync 249 } else { 250 nextState = STATE_EXPECTING_SYNC_1; // restart 251 } 252 break; 253 254 case STATE_GATHERING: 255 mHeaderBuffer[mCursor++] = byte; // save for getSyncAddress() 256 if (mCursor >= sizeof(mHeaderBuffer)) { 257 nextState = parseHeader(); 258 } 259 break; 260 261 default: 262 ALOGE("AC3FrameScanner: invalid state = %d\n", mState); 263 nextState = STATE_EXPECTING_SYNC_1; // restart 264 break; 265 } 266 mState = nextState; 267 return mState == STATE_GOT_HEADER; 268} 269 270} // namespace android 271