1 /**
2  * Copyright (C) 2022 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  */
16 
17 #include <ImsMediaBitWriter.h>
18 #include <ImsMediaTrace.h>
19 #include <string.h>
20 
ImsMediaBitWriter()21 ImsMediaBitWriter::ImsMediaBitWriter()
22 {
23     mBuffer = nullptr;
24     mMaxBufferSize = 0;
25     mBytePos = 0;
26     mBitPos = 0;
27     mBitBuffer = 0;
28     mBufferFull = false;
29 }
30 
~ImsMediaBitWriter()31 ImsMediaBitWriter::~ImsMediaBitWriter() {}
32 
SetBuffer(uint8_t * pbBuffer,uint32_t nBufferSize)33 void ImsMediaBitWriter::SetBuffer(uint8_t* pbBuffer, uint32_t nBufferSize)
34 {
35     mBytePos = 0;
36     mBitPos = 0;
37     mBitBuffer = 0;
38     mBufferFull = false;
39     mBuffer = pbBuffer;
40     mMaxBufferSize = nBufferSize;
41 }
42 
Write(uint32_t nValue,uint32_t nSize)43 bool ImsMediaBitWriter::Write(uint32_t nValue, uint32_t nSize)
44 {
45     if (nSize == 0)
46     {
47         return false;
48     }
49 
50     if (mBuffer == nullptr || nSize > 24 || mBufferFull)
51     {
52         IMLOGE2("[Write] nSize[%d], BufferFull[%d]", nSize, mBufferFull);
53         return false;
54     }
55 
56     // write to bit buffer
57     mBitBuffer += (nValue << (32 - nSize) >> mBitPos);
58     mBitPos += nSize;
59 
60     // write to byte buffer
61     while (mBitPos >= 8)
62     {
63         mBuffer[mBytePos++] = (uint8_t)(mBitBuffer >> 24);
64         mBitBuffer <<= 8;
65         mBitPos -= 8;
66     }
67 
68     if (mBytePos >= mMaxBufferSize)
69     {
70         mBufferFull = true;
71     }
72 
73     return true;
74 }
75 
WriteByteBuffer(uint8_t * pbSrc,uint32_t nBitSize)76 bool ImsMediaBitWriter::WriteByteBuffer(uint8_t* pbSrc, uint32_t nBitSize)
77 {
78     uint32_t nByteSize;
79     uint32_t nRemainBitSize;
80     nByteSize = nBitSize >> 3;
81     nRemainBitSize = nBitSize & 0x07;
82 
83     if (mBitPos == 0)
84     {
85         memcpy(mBuffer + mBytePos, pbSrc, nByteSize);
86         mBytePos += nByteSize;
87     }
88     else
89     {
90         uint32_t i;
91 
92         for (i = 0; i < nByteSize; i++)
93         {
94             if (!Write(pbSrc[i], 8))
95             {
96                 return false;
97             }
98         }
99     }
100 
101     if (nRemainBitSize > 0)
102     {
103         uint32_t v = pbSrc[nByteSize];
104         v >>= (8 - nRemainBitSize);
105 
106         if (!Write(v, nRemainBitSize))
107         {
108             return false;
109         }
110     }
111 
112     return true;
113 }
114 
WriteByteBuffer(uint32_t value)115 bool ImsMediaBitWriter::WriteByteBuffer(uint32_t value)
116 {
117     uint32_t nRemainBitSize = 32;
118 
119     for (int32_t i = 0; i < 4; i++)
120     {
121         nRemainBitSize -= 8;
122         uint8_t v = (value >> nRemainBitSize) & 0x00ff;
123 
124         if (!Write(v, 8))
125         {
126             return false;
127         }
128     }
129 
130     return true;
131 }
132 
Seek(uint32_t nSize)133 void ImsMediaBitWriter::Seek(uint32_t nSize)
134 {
135     Flush();
136     mBitPos += nSize;
137 
138     while (mBitPos >= 8)
139     {
140         mBytePos++;
141         mBitPos -= 8;
142     }
143 }
144 
AddPadding()145 void ImsMediaBitWriter::AddPadding()
146 {
147     if (mBitPos > 0)
148     {
149         Write(0, 8 - mBitPos);
150     }
151 }
152 
GetBufferSize()153 uint32_t ImsMediaBitWriter::GetBufferSize()
154 {
155     uint32_t nSize;
156     nSize = (mBitPos + 7) >> 3;
157     nSize += mBytePos;
158     return nSize;
159 }
160 
Flush()161 void ImsMediaBitWriter::Flush()
162 {
163     if (mBitPos > 0)
164     {
165         mBuffer[mBytePos] += (uint8_t)(mBitBuffer >> 24);
166         mBitBuffer = 0;
167     }
168 }