blob: 7c37eb65d17a7b37292db78d838706a820bc45cd [file] [log] [blame]
Phil Burk41714782014-08-05 15:15:29 -07001/*
Phil Burka8bbc962015-04-01 17:16:31 -07002 * 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 */
Phil Burk41714782014-08-05 15:15:29 -070016
Phil Burkfe521b12014-11-12 16:56:25 -080017#define LOG_TAG "AudioSPDIF"
Phil Burk41714782014-08-05 15:15:29 -070018
Elliott Hughes1f38e5b2015-01-31 00:37:28 -080019#include <string.h>
Phil Burka8bbc962015-04-01 17:16:31 -070020#include <assert.h>
Elliott Hughes1f38e5b2015-01-31 00:37:28 -080021
Phil Burk41714782014-08-05 15:15:29 -070022#include <utils/Log.h>
23#include <audio_utils/spdif/FrameScanner.h>
24
25namespace android {
26
Phil Burka8bbc962015-04-01 17:16:31 -070027FrameScanner::FrameScanner(int dataType,
28 const uint8_t *syncBytes,
29 uint32_t syncLength,
30 uint32_t headerLength)
31 : mBytesSkipped(0)
32 , mSyncBytes(syncBytes)
33 , mSyncLength(syncLength)
34 , mHeaderLength(headerLength)
35 , mCursor(0)
36 , mFormatDumpCount(0)
37 , mSampleRate(0)
Phil Burk41714782014-08-05 15:15:29 -070038 , mRateMultiplier(1)
Phil Burke2bba7f2020-03-03 12:51:15 -080039 , mFrameSizeBytes(headerLength) // minimum
Phil Burk41714782014-08-05 15:15:29 -070040 , mDataType(dataType)
41 , mDataTypeInfo(0)
42{
43}
44
45FrameScanner::~FrameScanner()
46{
47}
48
Phil Burka8bbc962015-04-01 17:16:31 -070049// State machine that scans for headers in a byte stream.
Phil Burk41714782014-08-05 15:15:29 -070050// @return true if we have detected a complete and valid header.
Phil Burka8bbc962015-04-01 17:16:31 -070051bool FrameScanner::scan(uint8_t byte)
Phil Burk41714782014-08-05 15:15:29 -070052{
Phil Burka8bbc962015-04-01 17:16:31 -070053 bool result = false;
Glenn Kasten391c4422016-03-24 11:51:39 -070054 ALOGV("FrameScanner: byte = 0x%02X, mCursor = %d", byte, mCursor);
Phil Burka8bbc962015-04-01 17:16:31 -070055 assert(mCursor < sizeof(mHeaderBuffer));
56 if (mCursor < mSyncLength) {
57 // match sync word
58 if (byte == mSyncBytes[mCursor]) {
59 mHeaderBuffer[mCursor++] = byte;
Phil Burk41714782014-08-05 15:15:29 -070060 } else {
61 mBytesSkipped += 1; // skip unsynchronized data
Phil Burka8bbc962015-04-01 17:16:31 -070062 mCursor = 0;
Phil Burk41714782014-08-05 15:15:29 -070063 }
Phil Burka8bbc962015-04-01 17:16:31 -070064 } else if (mCursor < mHeaderLength) {
65 // gather header for parsing
66 mHeaderBuffer[mCursor++] = byte;
67 if (mCursor >= mHeaderLength) {
68 if (parseHeader()) {
69 result = true;
70 } else {
71 ALOGE("FrameScanner: ERROR - parseHeader() failed.");
Phil Burk41714782014-08-05 15:15:29 -070072 }
Phil Burka8bbc962015-04-01 17:16:31 -070073 mCursor = 0;
Phil Burk41714782014-08-05 15:15:29 -070074 }
Phil Burk41714782014-08-05 15:15:29 -070075 }
Phil Burka8bbc962015-04-01 17:16:31 -070076 return result;
Phil Burk41714782014-08-05 15:15:29 -070077}
78
79} // namespace android