]> git.sesse.net Git - vlc/blob - modules/mux/mpeg/bits.h
FSF address change.
[vlc] / modules / mux / mpeg / bits.h
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  * 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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 typedef struct bits_buffer_s
26 {
27     int     i_size;
28
29     int     i_data;
30     uint8_t i_mask;
31     uint8_t *p_data;
32
33 } bits_buffer_t;
34
35 static inline int bits_initwrite( bits_buffer_t *p_buffer,
36                                   int i_size, void *p_data )
37 {
38     p_buffer->i_size = i_size;
39     p_buffer->i_data = 0;
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 )
44     {
45         if( !( p_buffer->p_data = malloc( i_size ) ) )
46             return -1;
47     }
48     return 0;
49 }
50
51 static inline void bits_align( bits_buffer_t *p_buffer )
52 {
53     if( p_buffer->i_mask != 0x80 && p_buffer->i_data < p_buffer->i_size )
54     {
55         p_buffer->i_mask = 0x80;
56         p_buffer->i_data++;
57         p_buffer->p_data[p_buffer->i_data] = 0x00;
58     }
59 }
60
61 static inline void bits_write( bits_buffer_t *p_buffer,
62                                int i_count, uint64_t i_bits )
63 {
64     while( i_count > 0 )
65     {
66         i_count--;
67
68         if( ( i_bits >> i_count )&0x01 )
69         {
70             p_buffer->p_data[p_buffer->i_data] |= p_buffer->i_mask;
71         }
72         else
73         {
74             p_buffer->p_data[p_buffer->i_data] &= ~p_buffer->i_mask;
75         }
76         p_buffer->i_mask >>= 1;
77         if( p_buffer->i_mask == 0 )
78         {
79             p_buffer->i_data++;
80             p_buffer->i_mask = 0x80;
81         }
82     }
83 }
84
85