summaryrefslogtreecommitdiffstats
tag nameLA.BR.1.2.9-05310-8x09.0 (14c176cce3dab850a921ce203ae540032fae6a88)
tag date2018-08-10 03:37:53 -0700
tagged byLinux Build Service Account <lnxbuild@localhost>
tagged objectcommit 5b4837720d...
"LA.BR.1.2.9-05310-8x09.0"
a> 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
/*
**
** Copyright 2014, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/


#include <stdint.h>

#define LOG_TAG "AudioHardwareTungsten"
#include <utils/Log.h>
#include <audio_utils/spdif/SPDIFEncoder.h>

namespace android {

// Burst Preamble defined in IEC61937-1
const unsigned short SPDIFEncoder::kSPDIFSync1 = 0xF872; // Pa
const unsigned short SPDIFEncoder::kSPDIFSync2 = 0x4E1F; // Pb

static int32_t sEndianDetector = 1;
#define isLittleEndian()  (*((uint8_t *)&sEndianDetector))

SPDIFEncoder::SPDIFEncoder()
  : mState(STATE_IDLE)
  , mSampleRate(48000)
  , mBurstBuffer(NULL)
  , mBurstBufferSizeBytes(0)
  , mRateMultiplier(1)
  , mBurstFrames(0)
  , mByteCursor(0)
  , mBitstreamNumber(0)
  , mPayloadBytesPending(0)
{
    // TODO support other compressed audio formats
    mFramer = new AC3FrameScanner();

    mBurstBufferSizeBytes = sizeof(uint16_t)
            * SPDIF_ENCODED_CHANNEL_COUNT
            * mFramer->getMaxSampleFramesPerSyncFrame();
    ALOGI("SPDIFEncoder: mBurstBufferSizeBytes = %d, littleEndian = %d",
        mBurstBufferSizeBytes, isLittleEndian());
    mBurstBuffer = new uint16_t[mBurstBufferSizeBytes >> 1];
    clearBurstBuffer();
}

SPDIFEncoder::~SPDIFEncoder()
{
    delete[] mBurstBuffer;
}

int SPDIFEncoder::getBytesPerOutputFrame()
{
    return SPDIF_ENCODED_CHANNEL_COUNT * sizeof(int16_t);
}

void SPDIFEncoder::writeBurstBufferShorts(const uint16_t *buffer, size_t numShorts)
{
    mByteCursor = (mByteCursor + 1) & ~1; // round up to even byte
    size_t bytesToWrite = numShorts * sizeof(uint16_t);
    if ((mByteCursor + bytesToWrite) > mBurstBufferSizeBytes) {
        ALOGE("SPDIFEncoder: Burst buffer overflow!\n");
        clearBurstBuffer();
        return;
    }
    memcpy(&mBurstBuffer[mByteCursor >> 1], buffer, bytesToWrite);
    mByteCursor += bytesToWrite;
}

// Pack the bytes into the short buffer in the order:
//   byte[0] -> short[0] MSB
//   byte[1] -> short[0] LSB
//   byte[2] -> short[1] MSB
//   byte[3] -> short[1] LSB
//   etcetera
// This way they should come out in the correct order for SPDIF on both
// Big and Little Endian CPUs.
void SPDIFEncoder::writeBurstBufferBytes(const uint8_t *buffer, size_t numBytes)
{
    size_t bytesToWrite = numBytes;
    if ((mByteCursor + bytesToWrite) > mBurstBufferSizeBytes) {
        ALOGE("SPDIFEncoder: Burst buffer overflow!\n");
        clearBurstBuffer();
        return;
    }
    uint16_t pad = mBurstBuffer[mByteCursor >> 1];
    for (size_t i = 0; i < bytesToWrite; i++) {
        if (mByteCursor & 1 ) {
            pad |= *buffer++; // put second byte in LSB
            mBurstBuffer[mByteCursor >> 1] = pad;
            pad = 0;
        } else {
            pad |= (*buffer++) << 8; // put first byte in MSB
        }
        mByteCursor++;
    }
    // Save partially filled short.
    if (mByteCursor & 1 ){
        mBurstBuffer[mByteCursor >> 1] = pad;
    }
}

void SPDIFEncoder::sendZeroPad()
{
    // Pad remainder of burst with zeros.
    size_t burstSize = mFramer->getSampleFramesPerSyncFrame() * sizeof(uint16_t)
            * SPDIF_ENCODED_CHANNEL_COUNT;
    if (mByteCursor > burstSize) {
        ALOGE("SPDIFEncoder: Burst buffer, contents too large!\n");
        clearBurstBuffer();
    } else {
        // We don't have to write zeros because buffer already set to zero
        // by clearBurstBuffer().
        mByteCursor = burstSize;
    }
}

void SPDIFEncoder::flushBurstBuffer()
{
    if (mByteCursor > 0) {
        sendZeroPad();
        writeOutput(mBurstBuffer, mByteCursor);
        clearBurstBuffer();
    }
}

void SPDIFEncoder::clearBurstBuffer()
{
    if (mBurstBuffer) {
        memset(mBurstBuffer, 0, mBurstBufferSizeBytes);
    }
    mByteCursor = 0;
}

size_t SPDIFEncoder::startDataBurst()
{
    // Encode IEC61937-1 Burst Preamble
    uint16_t preamble[4];

    uint16_t burstInfo = (mBitstreamNumber << 13)
        | (mFramer->getDataTypeInfo() << 8)
        | mFramer->getDataType();

    mRateMultiplier = mFramer->getRateMultiplier();

    preamble[0] = kSPDIFSync1;
    preamble[1] = kSPDIFSync2;
    preamble[2] = burstInfo;
    preamble[3] = mFramer->getLengthCode();
    writeBurstBufferShorts(preamble, 4);

    // Write start of encoded frame that was buffered in frame detector.
    size_t syncSize = mFramer->getHeaderSizeBytes();
    writeBurstBufferBytes(mFramer->getHeaderAddress(), syncSize);
    ALOGV("SPDIFEncoder: startDataBurst, syncSize = %u, lengthCode = %d",
        syncSize, mFramer->getLengthCode());
    return mFramer->getFrameSizeBytes() - syncSize;
}

// Wraps raw encoded data into a data burst.
ssize_t SPDIFEncoder::write( const void *buffer, size_t numBytes )
{
    size_t bytesLeft = numBytes;
    const uint8_t *data = (const uint8_t *)buffer;
    ALOGV("SPDIFEncoder: state = %d, write(buffer[0] = 0x%02X, numBytes = %u)",
        mState, (uint) *data, numBytes);
    while (bytesLeft > 0) {
        State nextState = mState;
        switch (mState) {
        // Look for beginning of encoded frame.
        case STATE_IDLE:
            if (mFramer->scan(*data)) {
                if (mFramer->isFirstInBurst()) {
                    // Make sure that this frame is at the beginning of the data burst.
                    flushBurstBuffer();
                }
                mPayloadBytesPending = startDataBurst();
                nextState = STATE_BURST;
            }
            data++;
            bytesLeft--;
            break;

        // Write payload until we hit end of frame.
        case STATE_BURST:
            {
                size_t bytesToWrite = bytesLeft;
                // Only write as many as we need to finish the frame.
                if (bytesToWrite > mPayloadBytesPending) {
                    bytesToWrite = mPayloadBytesPending;
                }
                writeBurstBufferBytes(data, bytesToWrite);

                data += bytesToWrite;
                bytesLeft -= bytesToWrite;
                mPayloadBytesPending -= bytesToWrite;

                // If we have all the payload then send a data burst.
                if (mPayloadBytesPending == 0) {
                    if (mFramer->isLastInBurst()) {
                        // Flush now if we know the burst is ready.
                        flushBurstBuffer();
                    }
                    nextState = STATE_IDLE;
                }
            }
            break;

        default:
            ALOGE("SPDIFEncoder: illegal state = %d\n", mState);
            nextState = STATE_IDLE;
            break;
        }
        mState = nextState;
    }
    return numBytes;
}

}  // namespace android