1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
10 * Copyright (C) 2008 Lin YANG <oxcsnicho@gmail.com>
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.
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.
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 *****************************************************************************/
33 typedef struct bits_buffer_s
43 static inline gint bits_initwrite( bits_buffer_t *p_buffer,
44 gint i_size, void *p_data )
46 p_buffer->i_size = i_size;
48 p_buffer->i_mask = 0x80;
49 p_buffer->p_data = p_data;
50 if( !p_buffer->p_data )
52 if( !( p_buffer->p_data = g_slice_alloc0( i_size ) ) )
55 p_buffer->p_data[0] = 0;
59 static inline void bits_align( bits_buffer_t *p_buffer )
61 if( p_buffer->i_mask != 0x80 && p_buffer->i_data < p_buffer->i_size )
63 p_buffer->i_mask = 0x80;
65 p_buffer->p_data[p_buffer->i_data] = 0x00;
69 static inline void bits_write( bits_buffer_t *p_buffer,
70 gint i_count, guint64 i_bits )
76 if( ( i_bits >> i_count )&0x01 )
78 p_buffer->p_data[p_buffer->i_data] |= p_buffer->i_mask;
82 p_buffer->p_data[p_buffer->i_data] &= ~p_buffer->i_mask;
84 p_buffer->i_mask >>= 1;
85 if( p_buffer->i_mask == 0 )
88 p_buffer->i_mask = 0x80;