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 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 typedef struct bits_buffer_s
35 static inline int bits_initwrite( bits_buffer_t *p_buffer,
36 int i_size, void *p_data )
38 p_buffer->i_size = i_size;
40 p_buffer->i_mask = 0x80;
41 p_buffer->p_data = p_data;
42 p_buffer->p_data[0] = 0;
43 if( !p_buffer->p_data )
45 if( !( p_buffer->p_data = malloc( i_size ) ) )
51 static inline void bits_align( bits_buffer_t *p_buffer )
53 if( p_buffer->i_mask != 0x80 && p_buffer->i_data < p_buffer->i_size )
55 p_buffer->i_mask = 0x80;
57 p_buffer->p_data[p_buffer->i_data] = 0x00;
61 static inline void bits_write( bits_buffer_t *p_buffer,
62 int i_count, uint64_t i_bits )
68 if( ( i_bits >> i_count )&0x01 )
70 p_buffer->p_data[p_buffer->i_data] |= p_buffer->i_mask;
74 p_buffer->p_data[p_buffer->i_data] &= ~p_buffer->i_mask;
76 p_buffer->i_mask >>= 1;
77 if( p_buffer->i_mask == 0 )
80 p_buffer->i_mask = 0x80;