Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / cpp / Sample / Tizen C++ / MediaApp / MediaApp / project / src / Codec / BitWriter.h
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 #ifndef _BIT_WRITER_H_
19 #define _BIT_WRITER_H_
20
21 #include <FBase.h>
22
23 class BitWriter
24         : public Osp::Base::Object
25 {
26 public:
27         BitWriter(unsigned char* dest, int size)
28         {
29                 __num = 0;
30                 __curr = 0;
31                 __mask = 0x80;
32
33                 __pBuf = dest;
34                 __size = size;
35                 __pos = 0;
36         }
37
38         virtual ~BitWriter() {  }
39
40         int GetPosition() { return __pos; }
41
42         void AlignByte()
43         {
44                 if (__mask != 0x80)
45                 {
46                         ++__num;
47                         WriteByte(__curr);
48                         __curr = 0;
49                         __mask = 0x80;
50                 }
51         }
52
53         void PutBits(int count, unsigned long bits)
54         {
55                 while (count > 0)
56                 {
57                         PutBit((bool) ((bits >> (count - 1)) & 0x01));
58                         count--;
59                 }
60         }
61
62
63         void PutBit(bool bit)
64         {
65                 if (bit)
66                         __curr |= __mask;
67                 if (!(__mask >>= 1))
68                 {
69                         ++__num;
70                         WriteByte(__curr);
71                         __curr = 0;
72                         __mask = 0x80;
73                 }
74         }
75
76         void PutByte(unsigned char v)
77         {
78                 ++__num;
79                 if (__mask == 0x80)
80                 {
81                         WriteByte(v);
82                         return;
83                 }
84
85                 unsigned char ml = log2_tab[__mask];
86                 __curr |= v >> (8 - ml);
87                 WriteByte(__curr);
88                 __curr = v << ml;
89         }
90
91         void PutWord(unsigned short v)
92         {
93                 PutByte((v >> 8) & 0x00ff);
94                 PutByte((v) & 0x00ff);
95         }
96
97         void PutDword(unsigned long v)
98         {
99                 PutByte((unsigned char) ((v >> 24) & 0x00ff));
100                 PutByte((unsigned char) ((v >> 16) & 0x00ff));
101                 PutByte((unsigned char) ((v >> 8) & 0x00ff));
102                 PutByte((unsigned char) ((v) & 0x00ff));
103         }
104
105 protected:
106         void WriteByte(unsigned char v)
107         {
108                 if (__pos >= __size)
109                         return;
110
111                 __pBuf[__pos] = v;
112                 __pos++;
113         }
114
115
116 private:
117         int __num;
118         unsigned char __curr;
119         unsigned char __mask;
120         unsigned char* __pBuf;
121         int __size;
122         int __pos;
123
124         static const unsigned char log2_tab[256];
125 };
126
127 #endif // _BIT_WRITER_H_