xref: /aosp_15_r20/system/media/audio_utils/spdif/SPDIFDecoder.cpp (revision b9df5ad1c9ac98a7fefaac271a55f7ae3db05414)
1*b9df5ad1SAndroid Build Coastguard Worker /*
2*b9df5ad1SAndroid Build Coastguard Worker  * Copyright 2023, The Android Open Source Project
3*b9df5ad1SAndroid Build Coastguard Worker  *
4*b9df5ad1SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*b9df5ad1SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*b9df5ad1SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*b9df5ad1SAndroid Build Coastguard Worker  *
8*b9df5ad1SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*b9df5ad1SAndroid Build Coastguard Worker  *
10*b9df5ad1SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*b9df5ad1SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*b9df5ad1SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*b9df5ad1SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*b9df5ad1SAndroid Build Coastguard Worker  * limitations under the License.
15*b9df5ad1SAndroid Build Coastguard Worker  */
16*b9df5ad1SAndroid Build Coastguard Worker 
17*b9df5ad1SAndroid Build Coastguard Worker #include <stdint.h>
18*b9df5ad1SAndroid Build Coastguard Worker #include <string.h>
19*b9df5ad1SAndroid Build Coastguard Worker 
20*b9df5ad1SAndroid Build Coastguard Worker #define LOG_TAG "AudioSPDIF"
21*b9df5ad1SAndroid Build Coastguard Worker // #define LOG_NDEBUG 0
22*b9df5ad1SAndroid Build Coastguard Worker #include <log/log.h>
23*b9df5ad1SAndroid Build Coastguard Worker #include <audio_utils/spdif/SPDIFDecoder.h>
24*b9df5ad1SAndroid Build Coastguard Worker 
25*b9df5ad1SAndroid Build Coastguard Worker #include "SPDIFFrameScanner.h"
26*b9df5ad1SAndroid Build Coastguard Worker 
27*b9df5ad1SAndroid Build Coastguard Worker namespace android {
28*b9df5ad1SAndroid Build Coastguard Worker 
SPDIFDecoder(audio_format_t format)29*b9df5ad1SAndroid Build Coastguard Worker SPDIFDecoder::SPDIFDecoder(audio_format_t format)
30*b9df5ad1SAndroid Build Coastguard Worker   : mFramer(std::make_unique<SPDIFFrameScanner>(format))
31*b9df5ad1SAndroid Build Coastguard Worker   , mBurstDataBuffer(sizeof(uint16_t) * kSpdifEncodedChannelCount
32*b9df5ad1SAndroid Build Coastguard Worker             * mFramer->getMaxSampleFramesPerSyncFrame())
33*b9df5ad1SAndroid Build Coastguard Worker   , mPayloadBytesPending(0)
34*b9df5ad1SAndroid Build Coastguard Worker   , mPayloadBytesRead(0)
35*b9df5ad1SAndroid Build Coastguard Worker   , mScanning(true) {
36*b9df5ad1SAndroid Build Coastguard Worker }
37*b9df5ad1SAndroid Build Coastguard Worker 
isFormatSupported(audio_format_t format)38*b9df5ad1SAndroid Build Coastguard Worker bool SPDIFDecoder::isFormatSupported(audio_format_t format) {
39*b9df5ad1SAndroid Build Coastguard Worker     switch (format) {
40*b9df5ad1SAndroid Build Coastguard Worker         case AUDIO_FORMAT_AC3:
41*b9df5ad1SAndroid Build Coastguard Worker         case AUDIO_FORMAT_E_AC3:
42*b9df5ad1SAndroid Build Coastguard Worker         case AUDIO_FORMAT_E_AC3_JOC:
43*b9df5ad1SAndroid Build Coastguard Worker             return true;
44*b9df5ad1SAndroid Build Coastguard Worker         default:
45*b9df5ad1SAndroid Build Coastguard Worker             return false;
46*b9df5ad1SAndroid Build Coastguard Worker     }
47*b9df5ad1SAndroid Build Coastguard Worker }
48*b9df5ad1SAndroid Build Coastguard Worker 
reset()49*b9df5ad1SAndroid Build Coastguard Worker void SPDIFDecoder::reset() {
50*b9df5ad1SAndroid Build Coastguard Worker     ALOGV("SPDIFDecoder: reset()");
51*b9df5ad1SAndroid Build Coastguard Worker     if (mFramer != NULL) {
52*b9df5ad1SAndroid Build Coastguard Worker         mFramer->resetBurst();
53*b9df5ad1SAndroid Build Coastguard Worker     }
54*b9df5ad1SAndroid Build Coastguard Worker     mPayloadBytesPending = 0;
55*b9df5ad1SAndroid Build Coastguard Worker     mPayloadBytesRead = 0;
56*b9df5ad1SAndroid Build Coastguard Worker     mScanning = true;
57*b9df5ad1SAndroid Build Coastguard Worker }
58*b9df5ad1SAndroid Build Coastguard Worker 
fillBurstDataBuffer()59*b9df5ad1SAndroid Build Coastguard Worker ssize_t SPDIFDecoder::fillBurstDataBuffer() {
60*b9df5ad1SAndroid Build Coastguard Worker     const auto bytesToFill = mBurstDataBuffer.availableToWrite();
61*b9df5ad1SAndroid Build Coastguard Worker     std::vector<uint8_t> tmpBuffer(bytesToFill);
62*b9df5ad1SAndroid Build Coastguard Worker     auto bytesRead = readInput(&tmpBuffer[0], bytesToFill);
63*b9df5ad1SAndroid Build Coastguard Worker     if (bytesRead > 0) {
64*b9df5ad1SAndroid Build Coastguard Worker         ALOGV("SPDIFDecoder: read %zd burst data bytes", bytesRead);
65*b9df5ad1SAndroid Build Coastguard Worker         auto written = mBurstDataBuffer.write(tmpBuffer.data(), bytesRead);
66*b9df5ad1SAndroid Build Coastguard Worker         LOG_ALWAYS_FATAL_IF(written != bytesRead);
67*b9df5ad1SAndroid Build Coastguard Worker     }
68*b9df5ad1SAndroid Build Coastguard Worker     return bytesRead;
69*b9df5ad1SAndroid Build Coastguard Worker }
70*b9df5ad1SAndroid Build Coastguard Worker 
71*b9df5ad1SAndroid Build Coastguard Worker // Read burst payload data.
read(void * buffer,size_t numBytes)72*b9df5ad1SAndroid Build Coastguard Worker ssize_t SPDIFDecoder::read(void *buffer, size_t numBytes) {
73*b9df5ad1SAndroid Build Coastguard Worker     size_t bytesRead = 0;
74*b9df5ad1SAndroid Build Coastguard Worker     uint16_t *buf = reinterpret_cast<uint16_t *>(buffer);
75*b9df5ad1SAndroid Build Coastguard Worker     ALOGV("SPDIFDecoder: mScanning = %d numBytes = %zu", mScanning, numBytes);
76*b9df5ad1SAndroid Build Coastguard Worker     while (bytesRead < numBytes) {
77*b9df5ad1SAndroid Build Coastguard Worker         if (mBurstDataBuffer.empty()) {
78*b9df5ad1SAndroid Build Coastguard Worker             if (fillBurstDataBuffer() <= 0) {
79*b9df5ad1SAndroid Build Coastguard Worker               return -1;
80*b9df5ad1SAndroid Build Coastguard Worker             }
81*b9df5ad1SAndroid Build Coastguard Worker             if (bytesRead > 0) {
82*b9df5ad1SAndroid Build Coastguard Worker                 return bytesRead;
83*b9df5ad1SAndroid Build Coastguard Worker             }
84*b9df5ad1SAndroid Build Coastguard Worker         }
85*b9df5ad1SAndroid Build Coastguard Worker         if (mScanning) {
86*b9df5ad1SAndroid Build Coastguard Worker             // Look for beginning of next IEC61937 frame.
87*b9df5ad1SAndroid Build Coastguard Worker             if (!mBurstDataBuffer.empty() && mFramer->scan(mBurstDataBuffer.readByte())) {
88*b9df5ad1SAndroid Build Coastguard Worker                 mPayloadBytesPending = mFramer->getFrameSizeBytes();
89*b9df5ad1SAndroid Build Coastguard Worker                 mPayloadBytesRead = 0;
90*b9df5ad1SAndroid Build Coastguard Worker                 mScanning = false;
91*b9df5ad1SAndroid Build Coastguard Worker             }
92*b9df5ad1SAndroid Build Coastguard Worker         } else {
93*b9df5ad1SAndroid Build Coastguard Worker             // Read until we hit end of burst payload.
94*b9df5ad1SAndroid Build Coastguard Worker             size_t bytesToRead = std::min(numBytes, mBurstDataBuffer.availableToRead());
95*b9df5ad1SAndroid Build Coastguard Worker             // Only read as many as we need to finish the frame.
96*b9df5ad1SAndroid Build Coastguard Worker             if (bytesToRead > mPayloadBytesPending) {
97*b9df5ad1SAndroid Build Coastguard Worker                 bytesToRead = mPayloadBytesPending;
98*b9df5ad1SAndroid Build Coastguard Worker             }
99*b9df5ad1SAndroid Build Coastguard Worker             // Unpack the bytes from short buffer into byte array in the order:
100*b9df5ad1SAndroid Build Coastguard Worker             //   short[0] MSB -> byte[0]
101*b9df5ad1SAndroid Build Coastguard Worker             //   short[0] LSB -> byte[1]
102*b9df5ad1SAndroid Build Coastguard Worker             //   short[1] MSB -> byte[2]
103*b9df5ad1SAndroid Build Coastguard Worker             //   short[1] LSB -> byte[3]
104*b9df5ad1SAndroid Build Coastguard Worker             //   ...
105*b9df5ad1SAndroid Build Coastguard Worker             // This way the payload should be extracted correctly on both
106*b9df5ad1SAndroid Build Coastguard Worker             // Big and Little Endian CPUs.
107*b9df5ad1SAndroid Build Coastguard Worker             uint16_t pad = 0;
108*b9df5ad1SAndroid Build Coastguard Worker             size_t actualBytesRead = 0;
109*b9df5ad1SAndroid Build Coastguard Worker             for (; !mBurstDataBuffer.empty() && actualBytesRead < bytesToRead; ++actualBytesRead) {
110*b9df5ad1SAndroid Build Coastguard Worker                 if (mPayloadBytesRead & 1) {
111*b9df5ad1SAndroid Build Coastguard Worker                     pad |= mBurstDataBuffer.readByte();  // Read second byte from LSB
112*b9df5ad1SAndroid Build Coastguard Worker                     buf[bytesRead >> 1] = pad;
113*b9df5ad1SAndroid Build Coastguard Worker                     pad = 0;
114*b9df5ad1SAndroid Build Coastguard Worker                 } else {
115*b9df5ad1SAndroid Build Coastguard Worker                     pad |= mBurstDataBuffer.readByte() << 8;  // Read first byte from MSB
116*b9df5ad1SAndroid Build Coastguard Worker                 }
117*b9df5ad1SAndroid Build Coastguard Worker                 mPayloadBytesRead++;
118*b9df5ad1SAndroid Build Coastguard Worker                 bytesRead++;
119*b9df5ad1SAndroid Build Coastguard Worker             }
120*b9df5ad1SAndroid Build Coastguard Worker             // Read out last byte from partially filled short.
121*b9df5ad1SAndroid Build Coastguard Worker             if (mPayloadBytesRead & 1) {
122*b9df5ad1SAndroid Build Coastguard Worker                 reinterpret_cast<uint8_t *>(buffer)[bytesRead] = pad >> 8;
123*b9df5ad1SAndroid Build Coastguard Worker             }
124*b9df5ad1SAndroid Build Coastguard Worker 
125*b9df5ad1SAndroid Build Coastguard Worker             buf += (bytesRead >> 1);
126*b9df5ad1SAndroid Build Coastguard Worker             mPayloadBytesPending -= actualBytesRead;
127*b9df5ad1SAndroid Build Coastguard Worker             mPayloadBytesRead += actualBytesRead;
128*b9df5ad1SAndroid Build Coastguard Worker 
129*b9df5ad1SAndroid Build Coastguard Worker             // If we have read the entire payload, prepare to look for the next IEC61937 frame.
130*b9df5ad1SAndroid Build Coastguard Worker             if (mPayloadBytesPending == 0) {
131*b9df5ad1SAndroid Build Coastguard Worker                 reset();
132*b9df5ad1SAndroid Build Coastguard Worker             }
133*b9df5ad1SAndroid Build Coastguard Worker         }
134*b9df5ad1SAndroid Build Coastguard Worker     }
135*b9df5ad1SAndroid Build Coastguard Worker     return numBytes;
136*b9df5ad1SAndroid Build Coastguard Worker }
137*b9df5ad1SAndroid Build Coastguard Worker 
138*b9df5ad1SAndroid Build Coastguard Worker }  // namespace android
139