1a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk/*
2a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * Copyright 2015, The Android Open Source Project
3a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk *
4a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * Licensed under the Apache License, Version 2.0 (the "License");
5a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * you may not use this file except in compliance with the License.
6a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * You may obtain a copy of the License at
7a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk *
8a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk *      http://www.apache.org/licenses/LICENSE-2.0
9a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk *
10a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * Unless required by applicable law or agreed to in writing, software
11a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * distributed under the License is distributed on an "AS IS" BASIS,
12a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * See the License for the specific language governing permissions and
14a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk * limitations under the License.
15a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk */
16a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
17a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#define LOG_TAG "AudioSPDIF"
18a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk//#define LOG_NDEBUG 0
19a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
20a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#include <assert.h>
21a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#include <string.h>
22a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
23a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#include <utils/Log.h>
24a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#include <audio_utils/spdif/FrameScanner.h>
25a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
26a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#include "BitFieldParser.h"
27a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#include "DTSFrameScanner.h"
28a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
29a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burknamespace android {
30a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
31a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk// TODO Handle termination frames.
32a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk// TODO assert if parse past end of header buffer
33a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk// TODO Handle DTS_HD
34a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
35a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burkconst uint8_t DTSFrameScanner::kSyncBytes[] =
36a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        { 0x7F, 0xFE, 0x80, 0x01 };
37a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
38a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burkconst int32_t DTSFrameScanner::kDTSSampleRateTable[DTS_NUM_SAMPLE_RATE_TABLE_ENTRIES]
39a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        = { -1, 8000, 16000, 32000, -1, -1,
40a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        11025, 22050, 44100, -1, -1, 12000, 24000, 48000, -1, -1 };
41a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
42a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk// Defined in IEC61937-2
43a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#define IEC61937_DATA_TYPE_DTS_I        11
44a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#define IEC61937_DATA_TYPE_DTS_II       12
45a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#define IEC61937_DATA_TYPE_DTS_III      13
46a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#define IEC61937_DATA_TYPE_DTS_IV       17
47a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
48a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#define IEC61937_MAX_SAMPLES_TYPE_I    512
49a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#define IEC61937_MAX_SAMPLES_TYPE_II  1024
50a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#define IEC61937_MAX_SAMPLES_TYPE_III 2048
51a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
52a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk// Limits defined in DTS spec paragraph 5.3.1
53a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#define DTS_MINIMUM_NBLKS                5
54a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#define DTS_MINIMUM_FSIZE               95
55a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
56a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk#define DTS_HEADER_BYTES_NEEDED         12
57a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
58a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk// Scanner for DTS byte streams.
59a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil BurkDTSFrameScanner::DTSFrameScanner()
60a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk : FrameScanner(IEC61937_DATA_TYPE_DTS_I,
61a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    DTSFrameScanner::kSyncBytes,
62a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    sizeof(DTSFrameScanner::kSyncBytes),
63a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    DTS_HEADER_BYTES_NEEDED)
64a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk , mSampleFramesPerSyncFrame(0)
65a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk{
66a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk}
67a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
68a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil BurkDTSFrameScanner::~DTSFrameScanner()
69a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk{
70a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk}
71a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
72a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk// Parse DTS header.
73a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk// Detect whether the stream is DTS or DTS_HD. Extract data depending on type.
74a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk// Sets mDataType, mFrameSizeBytes,
75a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk//      mSampleRate, mRateMultiplier, mLengthCode.
76a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk//
77a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk// @return true if valid
78a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burkbool DTSFrameScanner::parseHeader()
79a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk{
80a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    BitFieldParser parser(&mHeaderBuffer[mSyncLength]);
81a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
82a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    // These variables are named after the fields in the DTS spec 5.3.1
83a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    // Extract field in order.
84391c442d517b69cd1d0d4b8dd01aa2d8ef36501dGlenn Kasten    (void) /* uint32_t ftype = */ parser.readBits(1);
85391c442d517b69cd1d0d4b8dd01aa2d8ef36501dGlenn Kasten    (void) /* uint32_t deficit = */ parser.readBits(5); // "short"
86a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    uint32_t cpf = parser.readBits(1);
87a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    uint32_t nblks = parser.readBits(7);
88a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    uint32_t fsize = parser.readBits(14);
89391c442d517b69cd1d0d4b8dd01aa2d8ef36501dGlenn Kasten    (void) /* uint32_t amode = */ parser.readBits(6);
90a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    uint32_t sfreq = parser.readBits(4);
91a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    // make sure we did not read past collected data
92a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    ALOG_ASSERT((mSyncLength + ((parser.getBitCursor() + 7) >> 3))
93a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk            <= mHeaderLength);
94a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
95a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    // Validate fields.
96a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    if (cpf != 0) {
97a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        ALOGE("DTSFrameScanner: ERROR - CPF not zero!");
98a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        return false;
99a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    }
100a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    if (nblks < DTS_MINIMUM_NBLKS) {
101a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        ALOGE("DTSFrameScanner: ERROR - nblks = %u", nblks);
102a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        return false;
103a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    }
104a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    if (fsize < DTS_MINIMUM_FSIZE) {
105a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        ALOGE("DTSFrameScanner: ERROR - fsize = %u", fsize);
106a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        return false;
107a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    }
108a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
109b9cc914d10e31266b43bcefee4cfdd72cd81ec10Phil Burk    int32_t sampleRate = kDTSSampleRateTable[sfreq];
110a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    if (sampleRate < 0) {
111b9cc914d10e31266b43bcefee4cfdd72cd81ec10Phil Burk        ALOGE("DTSFrameScanner: ERROR - invalid sampleRate[%u] = %d", sfreq, sampleRate);
112a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        return false;
113a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    }
114a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    mSampleRate = (uint32_t) sampleRate;
115a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
116a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    mSampleFramesPerSyncFrame = (nblks + 1) * DTS_PCM_FRAMES_PER_BLOCK;
117a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    if (mSampleFramesPerSyncFrame <= IEC61937_MAX_SAMPLES_TYPE_I) {
118a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        mDataType = IEC61937_DATA_TYPE_DTS_I;
119a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    } else if (mSampleFramesPerSyncFrame <= IEC61937_MAX_SAMPLES_TYPE_II) {
120a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        mDataType = IEC61937_DATA_TYPE_DTS_II;
121a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    } else if (mSampleFramesPerSyncFrame <= IEC61937_MAX_SAMPLES_TYPE_III) {
122a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        mDataType = IEC61937_DATA_TYPE_DTS_III;
123a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    } else {
124a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        mDataType = IEC61937_DATA_TYPE_DTS_IV;
125a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk        // TODO set bits 8,10
126a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    }
127a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
128a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    mFrameSizeBytes = fsize + 1;
129a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
130a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    mRateMultiplier = 1; // TODO what about "frequency extension"?
131a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    ALOGI_IF((mFormatDumpCount == 0),
132391c442d517b69cd1d0d4b8dd01aa2d8ef36501dGlenn Kasten            "DTS frame rate = %d * %d, size = %zu",
133a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk            mSampleRate, mRateMultiplier, mFrameSizeBytes);
134a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    mFormatDumpCount++;
135a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk    return true;
136a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk}
137a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
138a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk
139a8bbc96f21452a5dea03be9f3b92f8959005aa87Phil Burk}  // namespace android
140