AC3FrameScanner.cpp revision a8bbc96f21452a5dea03be9f3b92f8959005aa87
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
26namespace android {
27
28// These values are from the AC3 spec. Do not change them.
29
30const uint8_t AC3FrameScanner::kSyncBytes[] = { 0x0B, 0x77 };
31
32const 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
37const 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
79const uint16_t AC3FrameScanner::kEAC3ReducedSampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES]
80        = { 24000, 22050, 16000 };
81
82const 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.
95AC3FrameScanner::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
106AC3FrameScanner::~AC3FrameScanner()
107{
108}
109
110int AC3FrameScanner::getSampleFramesPerSyncFrame() const
111{
112    return mRateMultiplier
113            * AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK * AC3_PCM_FRAMES_PER_BLOCK;
114}
115
116void 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 Paragraph 5.3.3
130// We have to send 6 audio blocks on all active substreams.
131// Substream zero must be the first.
132// We don't know if we have all the blocks we need until we see
133// the 7th block of substream#0.
134bool AC3FrameScanner::isFirstInBurst()
135{
136    if (mDataType == SPDIF_DATA_TYPE_E_AC3) {
137        if (((mStreamType == AC3_STREAM_TYPE_0)
138                || (mStreamType == AC3_STREAM_TYPE_2))
139                && (mSubstreamID == 0)
140                // The ">" is intentional. We have to see the beginning
141                // of the block in the next burst before we can send
142                // the current burst.
143                && (mSubstreamBlockCounts[0] > AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK)) {
144            return true;
145        }
146    }
147    return false;
148}
149
150bool AC3FrameScanner::isLastInBurst()
151{
152    // For EAC3 we don't know if we are the end until we see a
153    // frame that must be at the beginning. See isFirstInBurst().
154    return (mDataType != SPDIF_DATA_TYPE_E_AC3); // Just one AC3 frame per burst.
155}
156
157// TODO Use BitFieldParser
158
159// Parse AC3 header.
160// Detect whether the stream is AC3 or EAC3. Extract data depending on type.
161//
162// @return true if valid
163bool AC3FrameScanner::parseHeader()
164{
165    // Interpret bsid based on paragraph E2.3.1.6 of EAC3 spec.
166    uint32_t bsid = mHeaderBuffer[5] >> 3; // bitstream ID
167    // Check BSID to see if this is EAC3 or regular AC3.
168    // These arbitrary BSID numbers do not have any names in the spec.
169    if ((bsid > 10) && (bsid <= 16)) {
170        mDataType = SPDIF_DATA_TYPE_E_AC3;
171    } else if (bsid <= 8) {
172        mDataType = SPDIF_DATA_TYPE_AC3;
173    } else {
174        ALOGW("AC3 bsid = %d not supported", bsid);
175        return false;
176    }
177
178    // The names fscod, frmsiz are from the AC3 spec.
179    uint32_t fscod = mHeaderBuffer[4] >> 6;
180    if (mDataType == SPDIF_DATA_TYPE_E_AC3) {
181        mStreamType = mHeaderBuffer[2] >> 6; // strmtyp in spec
182        mSubstreamID = (mHeaderBuffer[2] >> 3) & 0x07;
183
184        // Frame size is explicit in EAC3. Paragraph E2.3.1.3
185        uint32_t frmsiz = ((mHeaderBuffer[2] & 0x07) << 8) + mHeaderBuffer[3];
186        mFrameSizeBytes = (frmsiz + 1) * sizeof(int16_t);
187
188        uint32_t numblkscod = 3; // 6 blocks default
189        if (fscod == 3) {
190            uint32_t fscod2 = (mHeaderBuffer[4] >> 4) & 0x03;
191            if (fscod2 >= AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES) {
192                ALOGW("Invalid EAC3 fscod2 = %d\n", fscod2);
193                return false;
194            } else {
195                mSampleRate = kEAC3ReducedSampleRateTable[fscod2];
196            }
197        } else {
198            mSampleRate = kAC3SampleRateTable[fscod];
199            numblkscod = (mHeaderBuffer[4] >> 4) & 0x03;
200        }
201        mRateMultiplier = EAC3_RATE_MULTIPLIER; // per IEC 61973-3 Paragraph 5.3.3
202        // Don't send data burst until we have 6 blocks per substream.
203        mAudioBlocksPerSyncFrame = kEAC3BlocksPerFrameTable[numblkscod];
204        // Keep track of how many audio blocks we have for each substream.
205        // This should be safe because mSubstreamID is ANDed with 0x07 above.
206        // And the array is allocated as [8].
207        mSubstreamBlockCounts[mSubstreamID] += mAudioBlocksPerSyncFrame;
208
209        // Print enough so we can see all the substreams.
210        ALOGD_IF((mFormatDumpCount < 3*8 ),
211                "EAC3 mStreamType = %d, mSubstreamID = %d",
212                mStreamType, mSubstreamID);
213    } else { // regular AC3
214        // Extract sample rate and frame size from codes.
215        uint32_t frmsizcod = mHeaderBuffer[4] & 0x3F; // frame size code
216
217        if (fscod >= AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES) {
218            ALOGW("Invalid AC3 sampleRateCode = %d\n", fscod);
219            return false;
220        } else if (frmsizcod >= AC3_NUM_FRAME_SIZE_TABLE_ENTRIES) {
221            ALOGW("Invalid AC3 frameSizeCode = %d\n", frmsizcod);
222            return false;
223        } else {
224            mSampleRate = kAC3SampleRateTable[fscod];
225            mRateMultiplier = 1;
226            mFrameSizeBytes = sizeof(uint16_t)
227                    * kAC3FrameSizeTable[frmsizcod][fscod];
228        }
229        mAudioBlocksPerSyncFrame = 6;
230    }
231    ALOGI_IF((mFormatDumpCount == 0),
232            "AC3 frame rate = %d * %d, size = %d, audioBlocksPerSyncFrame = %d\n",
233            mSampleRate, mRateMultiplier, mFrameSizeBytes, mAudioBlocksPerSyncFrame);
234    mFormatDumpCount++;
235    return true;
236}
237
238}  // namespace android
239