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 #define LOG_TAG "AudioSPDIF"
18 
19 #include <string.h>
20 
21 #include <utils/Log.h>
22 #include <audio_utils/spdif/FrameScanner.h>
23 
24 #include "AC3FrameScanner.h"
25 
26 namespace android {
27 
28 // These values are from the AC3 spec. Do not change them.
29 
30 const uint8_t AC3FrameScanner::kSyncBytes[] = { 0x0B, 0x77 };
31 
32 const uint16_t AC3FrameScanner::kAC3SampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES]
33     = { 48000, 44100, 32000 };
34 
35 // Table contains number of 16-bit words in an AC3 frame.
36 // From AC3 spec table 5.13
37 const uint16_t AC3FrameScanner::kAC3FrameSizeTable[AC3_NUM_FRAME_SIZE_TABLE_ENTRIES]
38         [AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES] = {
39     { 64, 69, 96 },
40     { 64, 70, 96 },
41     { 80, 87, 120 },
42     { 80, 88, 120 },
43     { 96, 104, 144 },
44     { 96, 105, 144 },
45     { 112, 121, 168 },
46     { 112, 122, 168 },
47     { 128, 139, 192 },
48     { 128, 140, 192 },
49     { 160, 174, 240 },
50     { 160, 175, 240 },
51     { 192, 208, 288 },
52     { 192, 209, 288 },
53     { 224, 243, 336 },
54     { 224, 244, 336 },
55     { 256, 278, 384 },
56     { 256, 279, 384 },
57     { 320, 348, 480 },
58     { 320, 349, 480 },
59     { 384, 417, 576 },
60     { 384, 418, 576 },
61     { 448, 487, 672 },
62     { 448, 488, 672 },
63     { 512, 557, 768 },
64     { 512, 558, 768 },
65     { 640, 696, 960 },
66     { 640, 697, 960 },
67     { 768, 835, 1152 },
68     { 768, 836, 1152 },
69     { 896, 975, 1344 },
70     { 896, 976, 1344 },
71     { 1024, 1114, 1536 },
72     { 1024, 1115, 1536 },
73     { 1152, 1253, 1728 },
74     { 1152, 1254, 1728 },
75     { 1280, 1393, 1920 },
76     { 1280, 1394, 1920 }
77 };
78 
79 const uint16_t AC3FrameScanner::kEAC3ReducedSampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES]
80         = { 24000, 22050, 16000 };
81 
82 const uint16_t
83         AC3FrameScanner::kEAC3BlocksPerFrameTable[EAC3_NUM_BLOCKS_PER_FRAME_TABLE_ENTRIES]
84         = { 1, 2, 3, 6 };
85 
86 // Defined in IEC61937-2
87 #define SPDIF_DATA_TYPE_AC3     1
88 #define SPDIF_DATA_TYPE_E_AC3  21
89 #define AC3_STREAM_TYPE_0       0
90 #define AC3_STREAM_TYPE_1       1
91 #define AC3_STREAM_TYPE_2       2
92 // -----------------------------------------------------------------------------
93 
94 // Scanner for AC3 byte streams.
AC3FrameScanner()95 AC3FrameScanner::AC3FrameScanner()
96  : FrameScanner(SPDIF_DATA_TYPE_AC3,
97         AC3FrameScanner::kSyncBytes,
98         sizeof(AC3FrameScanner::kSyncBytes), 6)
99  , mStreamType(0)
100  , mSubstreamID(0)
101 {
102     mAudioBlocksPerSyncFrame = 6;
103     memset(mSubstreamBlockCounts, 0, sizeof(mSubstreamBlockCounts));
104 }
105 
~AC3FrameScanner()106 AC3FrameScanner::~AC3FrameScanner()
107 {
108 }
109 
getSampleFramesPerSyncFrame() const110 int AC3FrameScanner::getSampleFramesPerSyncFrame() const
111 {
112     return mRateMultiplier
113             * AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK * AC3_PCM_FRAMES_PER_BLOCK;
114 }
115 
resetBurst()116 void AC3FrameScanner::resetBurst()
117 {
118     for (int i = 0; i < EAC3_MAX_SUBSTREAMS; i++) {
119         if (mSubstreamBlockCounts[i] >= AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK) {
120             mSubstreamBlockCounts[i] -= AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK;
121         } else if (mSubstreamBlockCounts[i] > 0) {
122             ALOGW("EAC3 substream[%d] has only %d audio blocks!",
123                 i, mSubstreamBlockCounts[i]);
124             mSubstreamBlockCounts[i] = 0;
125         }
126     }
127 }
128 
129 // Per IEC 61973-3:5.3.3, for E-AC3 burst-length shall be in bytes.
convertBytesToLengthCode(uint16_t numBytes) const130 uint16_t AC3FrameScanner::convertBytesToLengthCode(uint16_t numBytes) const
131 {
132     return (mDataType == SPDIF_DATA_TYPE_E_AC3) ? numBytes : numBytes * 8;
133 }
134 
135 // per IEC 61973-3 Paragraph 5.3.3
136 // We have to send 6 audio blocks on all active substreams.
137 // Substream zero must be the first.
138 // We don't know if we have all the blocks we need until we see
139 // the 7th block of substream#0.
isFirstInBurst()140 bool AC3FrameScanner::isFirstInBurst()
141 {
142     if (mDataType == SPDIF_DATA_TYPE_E_AC3) {
143         if (((mStreamType == AC3_STREAM_TYPE_0)
144                 || (mStreamType == AC3_STREAM_TYPE_2))
145                 && (mSubstreamID == 0)
146                 // The ">" is intentional. We have to see the beginning
147                 // of the block in the next burst before we can send
148                 // the current burst.
149                 && (mSubstreamBlockCounts[0] > AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK)) {
150             return true;
151         }
152     }
153     return false;
154 }
155 
isLastInBurst()156 bool AC3FrameScanner::isLastInBurst()
157 {
158     // For EAC3 we don't know if we are the end until we see a
159     // frame that must be at the beginning. See isFirstInBurst().
160     return (mDataType != SPDIF_DATA_TYPE_E_AC3); // Just one AC3 frame per burst.
161 }
162 
163 // TODO Use BitFieldParser
164 
165 // Parse AC3 header.
166 // Detect whether the stream is AC3 or EAC3. Extract data depending on type.
167 //
168 // @return true if valid
parseHeader()169 bool AC3FrameScanner::parseHeader()
170 {
171     // Interpret bsid based on paragraph E2.3.1.6 of EAC3 spec.
172     uint32_t bsid = mHeaderBuffer[5] >> 3; // bitstream ID
173     // Check BSID to see if this is EAC3 or regular AC3.
174     // These arbitrary BSID numbers do not have any names in the spec.
175     if ((bsid > 10) && (bsid <= 16)) {
176         mDataType = SPDIF_DATA_TYPE_E_AC3;
177     } else if (bsid <= 8) {
178         mDataType = SPDIF_DATA_TYPE_AC3;
179     } else {
180         ALOGW("AC3 bsid = %d not supported", bsid);
181         return false;
182     }
183 
184     // bitstream mode, main, commentary, etc.
185     uint32_t bsmod = mHeaderBuffer[5] & 7;
186     mDataTypeInfo = bsmod; // as per IEC61937-3, table 3.
187 
188     // The names fscod, frmsiz are from the AC3 spec.
189     uint32_t fscod = mHeaderBuffer[4] >> 6;
190     if (mDataType == SPDIF_DATA_TYPE_E_AC3) {
191         mStreamType = mHeaderBuffer[2] >> 6; // strmtyp in spec
192         mSubstreamID = (mHeaderBuffer[2] >> 3) & 0x07;
193 
194         // Frame size is explicit in EAC3. Paragraph E2.3.1.3
195         uint32_t frmsiz = ((mHeaderBuffer[2] & 0x07) << 8) + mHeaderBuffer[3];
196         mFrameSizeBytes = (frmsiz + 1) * sizeof(int16_t);
197 
198         uint32_t numblkscod = 3; // 6 blocks default
199         if (fscod == 3) {
200             uint32_t fscod2 = (mHeaderBuffer[4] >> 4) & 0x03;
201             if (fscod2 >= AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES) {
202                 ALOGW("Invalid EAC3 fscod2 = %d", fscod2);
203                 return false;
204             } else {
205                 mSampleRate = kEAC3ReducedSampleRateTable[fscod2];
206             }
207         } else {
208             mSampleRate = kAC3SampleRateTable[fscod];
209             numblkscod = (mHeaderBuffer[4] >> 4) & 0x03;
210         }
211         mRateMultiplier = EAC3_RATE_MULTIPLIER; // per IEC 61973-3 Paragraph 5.3.3
212         // Don't send data burst until we have 6 blocks per substream.
213         mAudioBlocksPerSyncFrame = kEAC3BlocksPerFrameTable[numblkscod];
214         // Keep track of how many audio blocks we have for each substream.
215         // This should be safe because mSubstreamID is ANDed with 0x07 above.
216         // And the array is allocated as [8].
217         if ((mStreamType == AC3_STREAM_TYPE_0)
218                 || (mStreamType == AC3_STREAM_TYPE_2)) {
219             mSubstreamBlockCounts[mSubstreamID] += mAudioBlocksPerSyncFrame;
220         }
221 
222         // Print enough so we can see all the substreams.
223         ALOGD_IF((mFormatDumpCount < 3*8 ),
224                 "EAC3 mStreamType = %d, mSubstreamID = %d",
225                 mStreamType, mSubstreamID);
226     } else { // regular AC3
227         // Extract sample rate and frame size from codes.
228         uint32_t frmsizcod = mHeaderBuffer[4] & 0x3F; // frame size code
229 
230         if (fscod >= AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES) {
231             ALOGW("Invalid AC3 sampleRateCode = %d", fscod);
232             return false;
233         } else if (frmsizcod >= AC3_NUM_FRAME_SIZE_TABLE_ENTRIES) {
234             ALOGW("Invalid AC3 frameSizeCode = %d", frmsizcod);
235             return false;
236         } else {
237             mSampleRate = kAC3SampleRateTable[fscod];
238             mRateMultiplier = 1;
239             mFrameSizeBytes = sizeof(uint16_t)
240                     * kAC3FrameSizeTable[frmsizcod][fscod];
241         }
242         mAudioBlocksPerSyncFrame = 6;
243     }
244     ALOGI_IF((mFormatDumpCount == 0),
245             "AC3 frame rate = %d * %d, size = %zu, audioBlocksPerSyncFrame = %d",
246             mSampleRate, mRateMultiplier, mFrameSizeBytes, mAudioBlocksPerSyncFrame);
247     mFormatDumpCount++;
248     return true;
249 }
250 
251 }  // namespace android
252