1 /*****************************************************************************
2 * bits.h
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
5 * $Id$
6 *
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 *
10 * Copyright (C) 2008 Lin YANG <oxcsnicho@gmail.com>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
26
27 #ifndef __BITS_H__
28 #define __BITS_H__
29
30 #include <glib.h>
31
32 G_BEGIN_DECLS
33 typedef struct bits_buffer_s
34 {
35 gint i_size;
36
37 gint i_data;
38 guint8 i_mask;
39 guint8* p_data;
40
41 } bits_buffer_t;
42
bits_initwrite(bits_buffer_t * p_buffer,gint i_size,void * p_data)43 static inline gint bits_initwrite( bits_buffer_t *p_buffer,
44 gint i_size, void *p_data )
45 {
46 p_buffer->i_size = i_size;
47 p_buffer->i_data = 0;
48 p_buffer->i_mask = 0x80;
49 p_buffer->p_data = p_data;
50 if( !p_buffer->p_data )
51 {
52 if( !( p_buffer->p_data = g_slice_alloc0( i_size ) ) )
53 return -1;
54 }
55 p_buffer->p_data[0] = 0;
56 return 0;
57 }
58
bits_align(bits_buffer_t * p_buffer)59 static inline void bits_align( bits_buffer_t *p_buffer )
60 {
61 if( p_buffer->i_mask != 0x80 && p_buffer->i_data < p_buffer->i_size )
62 {
63 p_buffer->i_mask = 0x80;
64 p_buffer->i_data++;
65 p_buffer->p_data[p_buffer->i_data] = 0x00;
66 }
67 }
68
bits_write(bits_buffer_t * p_buffer,gint i_count,guint64 i_bits)69 static inline void bits_write( bits_buffer_t *p_buffer,
70 gint i_count, guint64 i_bits )
71 {
72 while( i_count > 0 )
73 {
74 i_count--;
75
76 if( ( i_bits >> i_count )&0x01 )
77 {
78 p_buffer->p_data[p_buffer->i_data] |= p_buffer->i_mask;
79 }
80 else
81 {
82 p_buffer->p_data[p_buffer->i_data] &= ~p_buffer->i_mask;
83 }
84 p_buffer->i_mask >>= 1;
85 if( p_buffer->i_mask == 0 )
86 {
87 p_buffer->i_data++;
88 p_buffer->i_mask = 0x80;
89 }
90 }
91 }
92
93 G_END_DECLS
94
95 #endif
96