1/*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "adts.h"
17#include "common/common_macro.h"
18
19namespace OHOS {
20namespace Sharing {
21
22void AdtsHeader::DumpAdtsHeader(const AdtsHeader &hed, uint8_t *out)
23{
24    RETURN_IF_NULL(out);
25    out[0] = ((hed.syncword_ >> 4) & 0xFF);                      // 8bit, 4:byte offset
26    out[1] = ((hed.syncword_ << 4) & 0xF0);                      // 4 bit, 4:byte offset
27    out[1] |= ((hed.id_ << 3) & 0x08);                           // 1 bit, 3:byte offset
28    out[1] |= ((hed.layer_ << 1) & 0x06);                        // 2bit
29    out[1] |= ((hed.protectionAbsent_) & 0x01);                  // 1 bit
30    out[2] = ((hed.profile_ << 6) & 0xC0);                       // 2 bit, 6:byte offset, 2:byte offset
31    out[2] |= ((hed.sfIndex_ << 2) & 0x3C);                      // 4bit, 2:byte offset
32    out[2] |= ((hed.privateBit_ << 1) & 0x02);                   // 1 bit, 2:byte offset
33    out[2] |= ((hed.channelConfiguration_ >> 2) & 0x03);         // 1 bit, 2:byte offset
34    out[3] = ((hed.channelConfiguration_ << 6) & 0xC0);          // 2 bit, 6:byte offset, 3:byte offset
35    out[3] |= ((hed.original_ << 5) & 0x20);                     // 1 bit, 5:byte offset, 3:byte offset
36    out[3] |= ((hed.home_ << 4) & 0x10);                         // 1 bit, 4:byte offset, 3:byte offset
37    out[3] |= ((hed.copyrightIdentificationBit_ << 3) & 0x08);   // 1 bit, 3:byte offset
38    out[3] |= ((hed.copyrightIdentificationStart_ << 2) & 0x04); // 1 bit, 2:byte offset, 3:byte offset
39    out[3] |= ((hed.aacFrameLength_ >> 11) & 0x03);              // 2 bit, 11:byte offset, 3:byte offset
40    out[4] = ((hed.aacFrameLength_ >> 3) & 0xFF);                // 8 bit, 3:byte offset, 4:byte offset
41    out[5] = ((hed.aacFrameLength_ << 5) & 0xE0);                // 3 bit, 5:byte offset
42    out[5] |= ((hed.adtsBufferFullness_ >> 6) & 0x1F);           // 5 bit, 6:byte offset, 5:byte offset
43    out[6] = ((hed.adtsBufferFullness_ << 2) & 0xFC);            // 6 bit, 2:byte offset, 6:byte offset
44    out[6] |= (hed.numberOfRawDataBlocksInFrame_ & 0x03);        // 2 bit, 6:byte offset
45}
46
47void AdtsHeader::ParseAacConfig(const std::string &config, AdtsHeader &adts)
48{
49    uint8_t cfg1 = config[0];
50    uint8_t cfg2 = config[1];
51
52    int32_t audioObjectType;
53    int32_t samplingFrequencyIndex;
54    int32_t channelConfiguration;
55
56    audioObjectType = cfg1 >> 3;                                 // 3:byte offset
57    samplingFrequencyIndex = ((cfg1 & 0x07) << 1) | (cfg2 >> 7); // 7:byte offset
58    channelConfiguration = (cfg2 & 0x7F) >> 3;                   // 3:byte offset
59
60    adts.syncword_ = 0x0FFF;
61    adts.id_ = 0;
62    adts.layer_ = 0;
63    adts.protectionAbsent_ = 1;
64    adts.profile_ = audioObjectType - 1;
65    adts.sfIndex_ = samplingFrequencyIndex;
66    adts.privateBit_ = 0;
67    adts.channelConfiguration_ = channelConfiguration;
68    adts.original_ = 0;
69    adts.home_ = 0;
70    adts.copyrightIdentificationBit_ = 0;
71    adts.copyrightIdentificationStart_ = 0;
72    adts.aacFrameLength_ = 7;        // 7:aac header length
73    adts.adtsBufferFullness_ = 2047; // 2047:aac fixed value
74    adts.numberOfRawDataBlocksInFrame_ = 0;
75}
76
77int32_t AdtsHeader::DumpAacConfig(const std::string &config, size_t length, uint8_t *out, size_t outSize)
78{
79    RETURN_INVALID_IF_NULL(out);
80    AdtsHeader header;
81    ParseAacConfig(config, header);
82    header.aacFrameLength_ = (decltype(header.aacFrameLength_))(ADTS_HEADER_LEN + length);
83    DumpAdtsHeader(header, out);
84    return ADTS_HEADER_LEN;
85}
86
87int32_t AdtsHeader::GetAacFrameLength(const uint8_t *data, size_t bytes)
88{
89    RETURN_INVALID_IF_NULL(data);
90    uint16_t len;
91    if (bytes < 7) // 7:aac header length
92        return -1;
93    if (0xFF != data[0] || 0xF0 != (data[1] & 0xF0)) {
94        return -1;
95    }
96    len = ((uint16_t)(data[3] & 0x03) << 11) | // 3:byte offset, 11:byte length
97          ((uint16_t)data[4] << 3) |           // 4:byte offset, 3:byte length
98          ((uint16_t)(data[5] >> 5) & 0x07);   // 5:byte offset, 7:byte length
99    return len;
100}
101} // namespace Sharing
102} // namespace OHOS