Tizen 2.0 Release
[framework/osp/media.git] / src / FMedia_BitWriter.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <FBaseObject.h>
19 #include <FMedia_BitWriter.h>
20
21 using namespace Tizen::Base;
22
23 namespace Tizen { namespace Media
24 {
25
26 static const unsigned char _LOG_2_TAB[256] = {
27         0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
28         5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
29         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
30         6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
31         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
32         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
33         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
34         7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
35         8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
36         8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
37         8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
38         8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
39         8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
40         8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
41         8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
42         8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
43 };
44
45
46 _BitWriter::_BitWriter(unsigned char* dest, int size)
47 {
48         __num = 0;
49         __curr = 0;
50         __mask = 0x80;
51
52         __pBuf = dest;
53         __size = size;
54         __pos = 0;
55 }
56
57 _BitWriter::~_BitWriter(void)
58 {
59
60 }
61
62 int
63 _BitWriter::GetPosition(void)
64 {
65         return __pos;
66 }
67
68 void
69 _BitWriter::AlignByte(void)
70 {
71         if (__mask != 0x80)
72         {
73                 ++__num;
74                 WriteByte(__curr);
75                 __curr = 0;
76                 __mask = 0x80;
77         }
78 }
79
80 void
81 _BitWriter::PutBits(int count, unsigned long bits)
82 {
83         while (count > 0)
84         {
85                 PutBit((bool)((bits >> (count-1)) & 0x01));
86                 count--;
87         }
88 }
89
90 void
91 _BitWriter::PutBit(bool bit)
92 {
93         if (bit)
94         {
95                 __curr |= __mask;
96         }
97         __mask >>= 1;
98         if (__mask == 0)
99         {
100                 ++__num;
101                 WriteByte(__curr);
102                 __curr = 0;
103                 __mask = 0x80;
104         }
105 }
106
107 void
108 _BitWriter::PutByte(unsigned char v)
109 {
110         ++__num;
111         if (__mask == 0x80)
112         {
113                 WriteByte(v);
114                 return;
115         }
116
117         unsigned char ml = _LOG_2_TAB[__mask];
118         __curr |= v >> (8 - ml);
119         WriteByte(__curr);
120         __curr = v << ml;
121 }
122
123 void
124 _BitWriter::PutWord(unsigned short v)
125 {
126         PutByte((v >> 8) & 0x00ff);
127         PutByte((v     ) & 0x00ff);
128 }
129
130 void
131 _BitWriter::PutDword(unsigned long v)
132 {
133         PutByte((unsigned char) ((v >> 24) & 0x00ff));
134         PutByte((unsigned char) ((v >> 16) & 0x00ff));
135         PutByte((unsigned char) ((v >> 8 ) & 0x00ff));
136         PutByte((unsigned char) ((v      ) & 0x00ff));
137 }
138
139 void
140 _BitWriter::WriteByte(unsigned char v)
141 {
142         if (__pos >= __size)
143         {
144                 return ;
145         }
146
147         __pBuf[__pos] = v;
148         __pos++;
149 }
150
151 }} // Tizen::Media