Phil Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 1 | /* |
Phil Burk | a8bbc96 | 2015-04-01 17:16:31 -0700 | [diff] [blame] | 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 | */ |
Phil Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 16 | |
Phil Burk | fe521b1 | 2014-11-12 16:56:25 -0800 | [diff] [blame] | 17 | #define LOG_TAG "AudioSPDIF" |
Phil Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | 1f38e5b | 2015-01-31 00:37:28 -0800 | [diff] [blame] | 19 | #include <string.h> |
Phil Burk | a8bbc96 | 2015-04-01 17:16:31 -0700 | [diff] [blame] | 20 | #include <assert.h> |
Elliott Hughes | 1f38e5b | 2015-01-31 00:37:28 -0800 | [diff] [blame] | 21 | |
Phil Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | #include <audio_utils/spdif/FrameScanner.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
Phil Burk | a8bbc96 | 2015-04-01 17:16:31 -0700 | [diff] [blame] | 27 | FrameScanner::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 Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 38 | , mRateMultiplier(1) |
| 39 | , mFrameSizeBytes(0) |
| 40 | , mDataType(dataType) |
| 41 | , mDataTypeInfo(0) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | FrameScanner::~FrameScanner() |
| 46 | { |
| 47 | } |
| 48 | |
Phil Burk | a8bbc96 | 2015-04-01 17:16:31 -0700 | [diff] [blame] | 49 | // State machine that scans for headers in a byte stream. |
Phil Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 50 | // @return true if we have detected a complete and valid header. |
Phil Burk | a8bbc96 | 2015-04-01 17:16:31 -0700 | [diff] [blame] | 51 | bool FrameScanner::scan(uint8_t byte) |
Phil Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 52 | { |
Phil Burk | a8bbc96 | 2015-04-01 17:16:31 -0700 | [diff] [blame] | 53 | bool result = false; |
Glenn Kasten | 391c442 | 2016-03-24 11:51:39 -0700 | [diff] [blame] | 54 | ALOGV("FrameScanner: byte = 0x%02X, mCursor = %d", byte, mCursor); |
Phil Burk | a8bbc96 | 2015-04-01 17:16:31 -0700 | [diff] [blame] | 55 | assert(mCursor < sizeof(mHeaderBuffer)); |
| 56 | if (mCursor < mSyncLength) { |
| 57 | // match sync word |
| 58 | if (byte == mSyncBytes[mCursor]) { |
| 59 | mHeaderBuffer[mCursor++] = byte; |
Phil Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 60 | } else { |
| 61 | mBytesSkipped += 1; // skip unsynchronized data |
Phil Burk | a8bbc96 | 2015-04-01 17:16:31 -0700 | [diff] [blame] | 62 | mCursor = 0; |
Phil Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 63 | } |
Phil Burk | a8bbc96 | 2015-04-01 17:16:31 -0700 | [diff] [blame] | 64 | } 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 Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 72 | } |
Phil Burk | a8bbc96 | 2015-04-01 17:16:31 -0700 | [diff] [blame] | 73 | mCursor = 0; |
Phil Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 74 | } |
Phil Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 75 | } |
Phil Burk | a8bbc96 | 2015-04-01 17:16:31 -0700 | [diff] [blame] | 76 | return result; |
Phil Burk | 4171478 | 2014-08-05 15:15:29 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | } // namespace android |