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(audio_format_t format)95 AC3FrameScanner::AC3FrameScanner(audio_format_t format)
96  : FrameScanner(SPDIF_DATA_TYPE_AC3,
97         AC3FrameScanner::kSyncBytes,
98         sizeof(AC3FrameScanner::kSyncBytes), 6)
99  , mStreamType(0)
100  , mSubstreamID(0)
101  , mFormat(format)
102 {
103     mAudioBlocksPerSyncFrame = 6;
104     memset(mSubstreamBlockCounts, 0, sizeof(mSubstreamBlockCounts));
105 }
106 
~AC3FrameScanner()107 AC3FrameScanner::~AC3FrameScanner()
108 {
109 }
110 
getSampleFramesPerSyncFrame() const111 int AC3FrameScanner::getSampleFramesPerSyncFrame() const
112 {
113     return mRateMultiplier
114             * AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK * AC3_PCM_FRAMES_PER_BLOCK;
115 }
116 
resetBurst()117 void AC3FrameScanner::resetBurst()
118 {
119     for (int i = 0; i < EAC3_MAX_SUBSTREAMS; i++) {
120         if (mSubstreamBlockCounts[i] >= AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK) {
121             mSubstreamBlockCounts[i] -= AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK;
122         } else if (mSubstreamBlockCounts[i] > 0) {
123             ALOGW("EAC3 substream[%d] has only %d audio blocks!",
124                 i, mSubstreamBlockCounts[i]);
125             mSubstreamBlockCounts[i] = 0;
126         }
127     }
128 }
129 
130 // Per IEC 61973-3:5.3.3, for E-AC3 burst-length shall be in bytes.
convertBytesToLengthCode(uint16_t numBytes) const131 uint16_t AC3FrameScanner::convertBytesToLengthCode(uint16_t numBytes) const
132 {
133     return (mDataType == SPDIF_DATA_TYPE_E_AC3) ? numBytes : numBytes * 8;
134 }
135 
136 // per IEC 61973-3 Paragraph 5.3.3
137 // We have to send 6 audio blocks on all active substreams.
138 // Substream zero must be the first.
139 // We don't know if we have all the blocks we need until we see
140 // the 7th block of substream#0.
isFirstInBurst()141 bool AC3FrameScanner::isFirstInBurst()
142 {
143     if (mDataType == SPDIF_DATA_TYPE_E_AC3) {
144         if (((mStreamType == AC3_STREAM_TYPE_0)
145                 || (mStreamType == AC3_STREAM_TYPE_2))
146                 && (mSubstreamID == 0)
147                 // The ">" is intentional. We have to see the beginning
148                 // of the block in the next burst before we can send
149                 // the current burst.
150                 && (mSubstreamBlockCounts[0] > AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK)) {
151             return true;
152         }
153     }
154     return false;
155 }
156 
isLastInBurst()157 bool AC3FrameScanner::isLastInBurst()
158 {
159     // For EAC3 we don't know if we are the end until we see a
160     // frame that must be at the beginning. See isFirstInBurst().
161     return (mDataType != SPDIF_DATA_TYPE_E_AC3); // Just one AC3 frame per burst.
162 }
163 
164 // TODO Use BitFieldParser
165 
166 // Parse AC3 header.
167 // Detect whether the stream is AC3 or EAC3. Extract data depending on type.
168 //
169 // @return true if valid
parseHeader()170 bool AC3FrameScanner::parseHeader()
171 {
172     // Interpret bsid based on paragraph E2.3.1.6 of EAC3 spec.
173     uint32_t bsid = mHeaderBuffer[5] >> 3; // bitstream ID
174     // Check BSID to see if this is EAC3 or regular AC3.
175     // These arbitrary BSID numbers do not have any names in the spec.
176     if ((bsid > 10) && (bsid <= 16)) {
177         mDataType = SPDIF_DATA_TYPE_E_AC3;
178     } else if (bsid <= 8) {
179         mDataType = SPDIF_DATA_TYPE_AC3;
180     } else {
181         ALOGW("AC3 bsid = %d not supported", bsid);
182         return false;
183     }
184 
185     // bitstream mode, main, commentary, etc.
186     uint32_t bsmod = mHeaderBuffer[5] & 7;
187     mDataTypeInfo = bsmod; // as per IEC61937-3, table 3.
188 
189     // The names fscod, frmsiz are from the AC3 spec.
190     uint32_t fscod = mHeaderBuffer[4] >> 6;
191     if (mDataType == SPDIF_DATA_TYPE_E_AC3) {
192         mStreamType = mHeaderBuffer[2] >> 6; // strmtyp in spec
193         mSubstreamID = (mHeaderBuffer[2] >> 3) & 0x07;
194 
195         // Frame size is explicit in EAC3. Paragraph E2.3.1.3
196         uint32_t frmsiz = ((mHeaderBuffer[2] & 0x07) << 8) + mHeaderBuffer[3];
197         mFrameSizeBytes = (frmsiz + 1) * sizeof(int16_t);
198 
199         uint32_t numblkscod = 3; // 6 blocks default
200         if (fscod == 3) {
201             uint32_t fscod2 = (mHeaderBuffer[4] >> 4) & 0x03;
202             if (fscod2 >= AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES) {
203                 ALOGW("Invalid EAC3 fscod2 = %d", fscod2);
204                 return false;
205             } else {
206                 mSampleRate = kEAC3ReducedSampleRateTable[fscod2];
207             }
208         } else {
209             mSampleRate = kAC3SampleRateTable[fscod];
210             numblkscod = (mHeaderBuffer[4] >> 4) & 0x03;
211         }
212         mRateMultiplier = EAC3_RATE_MULTIPLIER; // per IEC 61973-3 Paragraph 5.3.3
213         // Don't send data burst until we have 6 blocks per substream.
214         mAudioBlocksPerSyncFrame = kEAC3BlocksPerFrameTable[numblkscod];
215         // Keep track of how many audio blocks we have for each substream.
216         // This should be safe because mSubstreamID is ANDed with 0x07 above.
217         // And the array is allocated as [8].
218         if ((mStreamType == AC3_STREAM_TYPE_0)
219                 || (mStreamType == AC3_STREAM_TYPE_2)) {
220             mSubstreamBlockCounts[mSubstreamID] += mAudioBlocksPerSyncFrame;
221         }
222 
223         // Print enough so we can see all the substreams.
224         ALOGD_IF((mFormatDumpCount < 3*8 ),
225                 "EAC3 mStreamType = %d, mSubstreamID = %d",
226                 mStreamType, mSubstreamID);
227     } else { // regular AC3
228         // Extract sample rate and frame size from codes.
229         uint32_t frmsizcod = mHeaderBuffer[4] & 0x3F; // frame size code
230 
231         if (fscod >= AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES) {
232             ALOGW("Invalid AC3 sampleRateCode = %d", fscod);
233             return false;
234         } else if (frmsizcod >= AC3_NUM_FRAME_SIZE_TABLE_ENTRIES) {
235             ALOGW("Invalid AC3 frameSizeCode = %d", frmsizcod);
236             return false;
237         } else {
238             mSampleRate = kAC3SampleRateTable[fscod];
239             mRateMultiplier = 1;
240             mFrameSizeBytes = sizeof(uint16_t)
241                     * kAC3FrameSizeTable[frmsizcod][fscod];
242         }
243         mAudioBlocksPerSyncFrame = 6;
244         if (mFormat == AUDIO_FORMAT_E_AC3) {
245             ALOGV("Its a Ac3 substream in EAC3 stream");
246             mStreamType = 2;
247             mSubstreamID = 0;
248             mSubstreamBlockCounts[0] += mAudioBlocksPerSyncFrame;
249             mDataType = SPDIF_DATA_TYPE_E_AC3;
250             mRateMultiplier = EAC3_RATE_MULTIPLIER;
251         }
252     }
253     ALOGI_IF((mFormatDumpCount == 0),
254             "AC3 frame rate = %d * %d, size = %zu, audioBlocksPerSyncFrame = %d",
255             mSampleRate, mRateMultiplier, mFrameSizeBytes, mAudioBlocksPerSyncFrame);
256     mFormatDumpCount++;
257     return true;
258 }
259 
260 }  // namespace android
261