• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 namespace OHOS {
20 namespace Sharing {
21 
DumpAdtsHeader(const AdtsHeader & hed,uint8_t * out,size_t outSize)22 void AdtsHeader::DumpAdtsHeader(const AdtsHeader &hed, uint8_t *out, size_t outSize)
23 {
24     RETURN_IF_NULL(out);
25     if (outSize < ADTS_HEADER_LEN) {
26         return;
27     }
28     out[0] = ((hed.syncword_ >> 4) & 0xFF);                      // 8bit, 4:byte offset
29     out[1] = ((hed.syncword_ << 4) & 0xF0);                      // 4 bit, 4:byte offset
30     out[1] |= ((hed.id_ << 3) & 0x08);                           // 1 bit, 3:byte offset
31     out[1] |= ((hed.layer_ << 1) & 0x06);                        // 2bit
32     out[1] |= ((hed.protectionAbsent_) & 0x01);                  // 1 bit
33     out[2] = ((hed.profile_ << 6) & 0xC0);                       // 2 bit, 6:byte offset, 2:byte offset
34     out[2] |= ((hed.sfIndex_ << 2) & 0x3C);                      // 4bit, 2:byte offset
35     out[2] |= ((hed.privateBit_ << 1) & 0x02);                   // 1 bit, 2:byte offset
36     out[2] |= ((hed.channelConfiguration_ >> 2) & 0x03);         // 1 bit, 2:byte offset
37     out[3] = ((hed.channelConfiguration_ << 6) & 0xC0);          // 2 bit, 6:byte offset, 3:byte offset
38     out[3] |= ((hed.original_ << 5) & 0x20);                     // 1 bit, 5:byte offset, 3:byte offset
39     out[3] |= ((hed.home_ << 4) & 0x10);                         // 1 bit, 4:byte offset, 3:byte offset
40     out[3] |= ((hed.copyrightIdentificationBit_ << 3) & 0x08);   // 1 bit, 3:byte offset
41     out[3] |= ((hed.copyrightIdentificationStart_ << 2) & 0x04); // 1 bit, 2:byte offset, 3:byte offset
42     out[3] |= ((hed.aacFrameLength_ >> 11) & 0x03);              // 2 bit, 11:byte offset, 3:byte offset
43     out[4] = ((hed.aacFrameLength_ >> 3) & 0xFF);                // 8 bit, 3:byte offset, 4:byte offset
44     out[5] = ((hed.aacFrameLength_ << 5) & 0xE0);                // 3 bit, 5:byte offset
45     out[5] |= ((hed.adtsBufferFullness_ >> 6) & 0x1F);           // 5 bit, 6:byte offset, 5:byte offset
46     out[6] = ((hed.adtsBufferFullness_ << 2) & 0xFC);            // 6 bit, 2:byte offset, 6:byte offset
47     out[6] |= (hed.numberOfRawDataBlocksInFrame_ & 0x03);        // 2 bit, 6:byte offset
48 }
49 
ParseAacConfig(const std::string & config,AdtsHeader & adts)50 void AdtsHeader::ParseAacConfig(const std::string &config, AdtsHeader &adts)
51 {
52     uint8_t cfg1 = config[0];
53     uint8_t cfg2 = config[1];
54 
55     int32_t audioObjectType;
56     int32_t samplingFrequencyIndex;
57     int32_t channelConfiguration;
58 
59     audioObjectType = cfg1 >> 3;                                 // 3:byte offset
60     samplingFrequencyIndex = ((cfg1 & 0x07) << 1) | (cfg2 >> 7); // 7:byte offset
61     channelConfiguration = (cfg2 & 0x7F) >> 3;                   // 3:byte offset
62 
63     adts.syncword_ = 0x0FFF;
64     adts.id_ = 0;
65     adts.layer_ = 0;
66     adts.protectionAbsent_ = 1;
67     adts.profile_ = (uint32_t)(audioObjectType - 1);
68     adts.sfIndex_ = (uint32_t)samplingFrequencyIndex;
69     adts.privateBit_ = 0;
70     adts.channelConfiguration_ = (uint32_t)channelConfiguration;
71     adts.original_ = 0;
72     adts.home_ = 0;
73     adts.copyrightIdentificationBit_ = 0;
74     adts.copyrightIdentificationStart_ = 0;
75     adts.aacFrameLength_ = 7;        // 7:aac header length
76     adts.adtsBufferFullness_ = 2047; // 2047:aac fixed value
77     adts.numberOfRawDataBlocksInFrame_ = 0;
78 }
79 
DumpAacConfig(const std::string & config,size_t length,uint8_t * out,size_t outSize)80 int32_t AdtsHeader::DumpAacConfig(const std::string &config, size_t length, uint8_t *out, size_t outSize)
81 {
82     RETURN_INVALID_IF_NULL(out);
83     AdtsHeader header;
84     ParseAacConfig(config, header);
85     header.aacFrameLength_ = (decltype(header.aacFrameLength_))(ADTS_HEADER_LEN + length);
86     DumpAdtsHeader(header, out, outSize);
87     return ADTS_HEADER_LEN;
88 }
89 
GetAacFrameLength(const uint8_t * data,size_t bytes)90 int32_t AdtsHeader::GetAacFrameLength(const uint8_t *data, size_t bytes)
91 {
92     RETURN_INVALID_IF_NULL(data);
93     uint16_t len;
94     if (bytes < 7) // 7:aac header length
95         return -1;
96     if (0xFF != data[0] || 0xF0 != (data[1] & 0xF0)) {
97         return -1;
98     }
99     len = ((uint16_t)(data[3] & 0x03) << 11) | // 3:byte offset, 11:byte length
100           ((uint16_t)data[4] << 3) |           // 4:byte offset, 3:byte length
101           ((uint16_t)(data[5] >> 5) & 0x07);   // 5:byte offset, 7:byte length
102     return len;
103 }
104 } // namespace Sharing
105 } // namespace OHOS