Home | History | Annotate | Download | only in spdif
      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 "AudioSPDIF"
     19 
     20 #include <utils/Log.h>
     21 #include <audio_utils/spdif/FrameScanner.h>
     22 
     23 namespace 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 
     30 FrameScanner::FrameScanner(int dataType)
     31  : mSampleRate(0)
     32  , mRateMultiplier(1)
     33  , mFrameSizeBytes(0)
     34  , mDataType(dataType)
     35  , mDataTypeInfo(0)
     36 {
     37 }
     38 
     39 FrameScanner::~FrameScanner()
     40 {
     41 }
     42 
     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;
     47 
     48 const 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.
     52 const 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 
     94 const uint16_t AC3FrameScanner::kEAC3ReducedSampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES]
     95         = { 24000, 22050, 16000 };
     96 
     97 const 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.
    103 AC3FrameScanner::AC3FrameScanner()
    104  : FrameScanner(SPDIF_DATA_TYPE_AC3)
    105  , mState(STATE_EXPECTING_SYNC_1)
    106  , mBytesSkipped(0)
    107  , mAudioBlocksPerSyncFrame(6)
    108  , mCursor(AC3_SYNCWORD_SIZE) // past sync word
    109  , mStreamType(0)
    110  , mSubstreamID(0)
    111  , mFormatDumpCount(0)
    112 {
    113     memset(mSubstreamBlockCounts, 0, sizeof(mSubstreamBlockCounts));
    114     // Define beginning of syncinfo for getSyncAddress()
    115     mHeaderBuffer[0] = kAC3SyncByte1;
    116     mHeaderBuffer[1] = kAC3SyncByte2;
    117 }
    118 
    119 AC3FrameScanner::~AC3FrameScanner()
    120 {
    121 }
    122 
    123 int AC3FrameScanner::getSampleFramesPerSyncFrame() const
    124 {
    125     return mRateMultiplier * AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK * AC3_PCM_FRAMES_PER_BLOCK;
    126 }
    127 
    128 void AC3FrameScanner::resetBurst()
    129 {
    130     for (int i = 0; i < EAC3_MAX_SUBSTREAMS; i++) {
    131         if (mSubstreamBlockCounts[i] >= AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK) {
    132             mSubstreamBlockCounts[i] -= AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK;
    133         } else if (mSubstreamBlockCounts[i] > 0) {
    134             ALOGW("EAC3 substream[%d] has only %d audio blocks!",
    135                 i, mSubstreamBlockCounts[i]);
    136             mSubstreamBlockCounts[i] = 0;
    137         }
    138     }
    139 }
    140 
    141 // per IEC 61973-3 Paragraph 5.3.3
    142 // We have to send 6 audio blocks on all active substreams.
    143 // Substream zero must be the first.
    144 // We don't know if we have all the blocks we need until we see
    145 // the 7th block of substream#0.
    146 bool AC3FrameScanner::isFirstInBurst()
    147 {
    148     if (mDataType == SPDIF_DATA_TYPE_E_AC3) {
    149         if (((mStreamType == 0) || (mStreamType == 2))
    150             && (mSubstreamID == 0)
    151             // The ">" is intentional. We have to see the beginning of the block
    152             // in the next burst before we can send the current burst.
    153             && (mSubstreamBlockCounts[0] > AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK)) {
    154             return true;
    155         }
    156     }
    157     return false;
    158 }
    159 
    160 bool AC3FrameScanner::isLastInBurst()
    161 {
    162     // For EAC3 we don't know if we are the end until we see a
    163     // frame that must be at the beginning. See isFirstInBurst().
    164     return (mDataType != SPDIF_DATA_TYPE_E_AC3); // Just one AC3 frame per burst.
    165 }
    166 
    167 // Parse AC3 header.
    168 // Detect whether the stream is AC3 or EAC3. Extract data depending on type.
    169 // Sets mDataType, mFrameSizeBytes, mAudioBlocksPerSyncFrame,
    170 //      mSampleRate, mRateMultiplier, mLengthCode.
    171 //
    172 // @return next state for scanner
    173 AC3FrameScanner::State AC3FrameScanner::parseHeader()
    174 {
    175     // Interpret bsid based on paragraph E2.3.1.6 of EAC3 spec.
    176     int bsid = mHeaderBuffer[5] >> 3; // bitstream ID
    177     // Check BSID to see if this is EAC3 or regular AC3
    178     if ((bsid >= 10) && (bsid <= 16)) {
    179         mDataType = SPDIF_DATA_TYPE_E_AC3;
    180     } else if ((bsid >= 0) && (bsid <= 8)) {
    181         mDataType = SPDIF_DATA_TYPE_AC3;
    182     } else {
    183         ALOGW("AC3 bsid = %d not supported", bsid);
    184         return STATE_EXPECTING_SYNC_1;
    185     }
    186 
    187     // The names fscod, frmsiz are from the AC3 spec.
    188     int fscod = mHeaderBuffer[4] >> 6;
    189     if (mDataType == SPDIF_DATA_TYPE_E_AC3) {
    190         mStreamType = mHeaderBuffer[2] >> 6; // strmtyp in spec
    191         mSubstreamID = (mHeaderBuffer[2] >> 3) & 0x07;
    192 
    193         // Frame size is explicit in EAC3. Paragraph E2.3.1.3
    194         int frmsiz = ((mHeaderBuffer[2] & 0x07) << 8) + mHeaderBuffer[3];
    195         mFrameSizeBytes = (frmsiz + 1) * sizeof(int16_t);
    196 
    197         int numblkscod = 3; // 6 blocks default
    198         if (fscod == 3) {
    199             int fscod2 = (mHeaderBuffer[4] >> 4) & 0x03;
    200             if (fscod2 >= AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES) {
    201                 ALOGW("Invalid EAC3 fscod2 = %d\n", fscod2);
    202                 return STATE_EXPECTING_SYNC_1;
    203             } else {
    204                 mSampleRate = kEAC3ReducedSampleRateTable[fscod2];
    205             }
    206         } else {
    207             mSampleRate = kAC3SampleRateTable[fscod];
    208             numblkscod = (mHeaderBuffer[4] >> 4) & 0x03;
    209         }
    210         mRateMultiplier = EAC3_RATE_MULTIPLIER; // per IEC 61973-3 Paragraph 5.3.3
    211         // Don't send data burst until we have 6 blocks per substream.
    212         mAudioBlocksPerSyncFrame = kEAC3BlocksPerFrameTable[numblkscod];
    213         // Keep track of how many audio blocks we have for each substream.
    214         // This should be safe because mSubstreamID is ANDed with 0x07 above.
    215         // And the array is allocated as [8].
    216         mSubstreamBlockCounts[mSubstreamID] += mAudioBlocksPerSyncFrame;
    217 
    218         // Print enough so we can see all the substreams.
    219         ALOGD_IF((mFormatDumpCount < 3*8 ),
    220                 "EAC3 mStreamType = %d, mSubstreamID = %d",
    221                 mStreamType, mSubstreamID);
    222     } else { // regular AC3
    223         // Extract sample rate and frame size from codes.
    224         unsigned int frmsizcod = mHeaderBuffer[4] & 0x3F; // frame size code
    225 
    226         if (fscod >= AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES) {
    227             ALOGW("Invalid AC3 sampleRateCode = %d\n", fscod);
    228             return STATE_EXPECTING_SYNC_1;
    229         } else if (frmsizcod >= AC3_NUM_FRAME_SIZE_TABLE_ENTRIES) {
    230             ALOGW("Invalid AC3 frameSizeCode = %d\n", frmsizcod);
    231             return STATE_EXPECTING_SYNC_1;
    232         } else {
    233             mSampleRate = kAC3SampleRateTable[fscod];
    234             mRateMultiplier = 1;
    235             mFrameSizeBytes = sizeof(uint16_t)
    236                     * kAC3FrameSizeTable[frmsizcod][fscod];
    237         }
    238         mAudioBlocksPerSyncFrame = 6;
    239     }
    240     ALOGI_IF((mFormatDumpCount == 0),
    241             "AC3 frame rate = %d * %d, size = %d, audioBlocksPerSyncFrame = %d\n",
    242             mSampleRate, mRateMultiplier, mFrameSizeBytes, mAudioBlocksPerSyncFrame);
    243     mFormatDumpCount++;
    244     return STATE_GOT_HEADER;
    245 }
    246 
    247 // State machine that scans for AC3 headers in a byte stream.
    248 // @return true if we have detected a complete and valid header.
    249 bool AC3FrameScanner::scan(uint8_t byte)
    250 {
    251     State nextState = mState;
    252     switch (mState) {
    253     case STATE_GOT_HEADER:
    254         nextState = STATE_EXPECTING_SYNC_1;
    255         // deliberately fall through
    256     case STATE_EXPECTING_SYNC_1:
    257         if (byte == kAC3SyncByte1) {
    258             nextState = STATE_EXPECTING_SYNC_2; // advance
    259         } else {
    260             mBytesSkipped += 1; // skip unsynchronized data
    261         }
    262         break;
    263 
    264     case STATE_EXPECTING_SYNC_2:
    265         if (byte == kAC3SyncByte2) {
    266             if (mBytesSkipped > 0) {
    267                 ALOGI("WARNING AC3 skipped %u bytes looking for a valid header.\n", mBytesSkipped);
    268                 mBytesSkipped = 0;
    269             }
    270             mCursor = AC3_SYNCWORD_SIZE;
    271             nextState = STATE_GATHERING; // advance
    272         } else if (byte == kAC3SyncByte1) {
    273             nextState = STATE_EXPECTING_SYNC_2; // resync
    274         } else {
    275             nextState = STATE_EXPECTING_SYNC_1; // restart
    276         }
    277         break;
    278 
    279     case STATE_GATHERING:
    280         mHeaderBuffer[mCursor++] = byte; // save for getSyncAddress()
    281         if (mCursor >= sizeof(mHeaderBuffer)) {
    282             nextState = parseHeader();
    283         }
    284         break;
    285 
    286     default:
    287         ALOGE("AC3FrameScanner: invalid state = %d\n", mState);
    288         nextState = STATE_EXPECTING_SYNC_1; // restart
    289         break;
    290     }
    291     mState = nextState;
    292     return mState == STATE_GOT_HEADER;
    293 }
    294 
    295 }  // namespace android
    296