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