Home | History | Annotate | Download | only in spdif
      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_FRAME_SCANNER_H
     18 #define ANDROID_AUDIO_FRAME_SCANNER_H
     19 
     20 #include <stdint.h>
     21 
     22 namespace android {
     23 
     24 
     25 /**
     26  * Scan a byte stream looking for the start of an encoded frame.
     27  * Parse the sample rate and the size of the encoded frame.
     28  * Buffer the sync header so it can be prepended to the remaining data.
     29  *
     30  * This is used directly by the SPDIFEncoder. External clients will
     31  * generally not call this class.
     32  */
     33 class FrameScanner {
     34 public:
     35     FrameScanner(int dataType,
     36             const uint8_t *syncBytes,
     37             uint32_t syncLength,
     38             uint32_t headerLength
     39             );
     40     virtual ~FrameScanner();
     41 
     42     /**
     43      * Pass each byte of the encoded stream to this scanner.
     44      * @return true if a complete and valid header was detected
     45      */
     46     virtual bool scan(uint8_t byte);
     47 
     48     /**
     49      * @return address of where the sync header was stored by scan()
     50      */
     51     const uint8_t *getHeaderAddress() const { return mHeaderBuffer; }
     52 
     53     /**
     54      * @return number of bytes in sync header stored by scan()
     55      */
     56     size_t getHeaderSizeBytes() const { return mHeaderLength; }
     57 
     58     /**
     59      * @return sample rate of the encoded audio
     60      */
     61     uint32_t getSampleRate()   const { return mSampleRate; }
     62 
     63     /**
     64      * Some formats, for example EAC3, are wrapped in data bursts that have
     65      * a sample rate that is a multiple of the encoded sample rate.
     66      * The default multiplier is 1.
     67      * @return sample rate multiplier for the SP/DIF PCM data bursts
     68      */
     69     uint32_t getRateMultiplier()   const { return mRateMultiplier; }
     70 
     71     size_t getFrameSizeBytes()     const { return mFrameSizeBytes; }
     72 
     73     /**
     74      * dataType is defined by the SPDIF standard for each format
     75      */
     76     int getDataType()      const { return mDataType; }
     77     int getDataTypeInfo()  const { return mDataTypeInfo; }
     78 
     79     virtual int getMaxChannels() const = 0;
     80 
     81     virtual void resetBurst() = 0;
     82 
     83     /**
     84      * @return the number of pcm frames that correspond to one encoded frame
     85      */
     86     virtual int getMaxSampleFramesPerSyncFrame() const = 0;
     87     virtual int getSampleFramesPerSyncFrame()    const = 0;
     88 
     89     /**
     90      * @return true if this parsed frame must be the first frame in a data burst.
     91      */
     92     virtual bool isFirstInBurst() = 0;
     93 
     94     /**
     95      * If this returns false then the previous frame may or may not be the last frame.
     96      * @return true if this parsed frame is definitely the last frame in a data burst.
     97      */
     98     virtual bool isLastInBurst()  = 0;
     99 
    100     /**
    101      * Most compression types use a lengthCode expressed in bits.
    102      */
    103     virtual uint16_t convertBytesToLengthCode(uint16_t numBytes) const { return numBytes * 8; }
    104 
    105 protected:
    106     uint32_t  mBytesSkipped;     // how many bytes were skipped looking for the start of a frame
    107     const uint8_t *mSyncBytes;   // pointer to the sync word specific to a format
    108     uint32_t  mSyncLength;       // number of bytes in sync word
    109     uint8_t   mHeaderBuffer[32]; // a place to gather the relevant header bytes for parsing
    110     uint32_t  mHeaderLength;     // the number of bytes we need to parse
    111     uint32_t  mCursor;           // position in the mHeaderBuffer
    112     uint32_t  mFormatDumpCount;  // used to thin out the debug dumps
    113     uint32_t  mSampleRate;       // encoded sample rate
    114     uint32_t  mRateMultiplier;   // SPDIF output data burst rate = msampleRate * mRateMultiplier
    115     size_t    mFrameSizeBytes;   // encoded frame size
    116     int       mDataType;         // as defined in IEC61937-2 paragraph 4.2
    117     int       mDataTypeInfo;     // as defined in IEC61937-2 paragraph 4.1
    118 
    119     /**
    120      * Parse data in mHeaderBuffer.
    121      * Sets mDataType, mFrameSizeBytes, mSampleRate, mRateMultiplier.
    122      * @return true if the header is valid.
    123      */
    124     virtual bool parseHeader() = 0;
    125 
    126 };
    127 
    128 
    129 }  // namespace android
    130 #endif  // ANDROID_AUDIO_FRAME_SCANNER_H
    131