bits.h 2.56 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * bits.h
 *****************************************************************************
 * Copyright (C) 2001, 2002 VideoLAN
Laurent Aimar's avatar
Laurent Aimar committed
5
 * $Id: bits.h,v 1.4 2003/05/05 15:52:13 fenrir Exp $
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Eric Petit <titer@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

typedef struct bits_buffer_s
{
    int     i_size;

    int     i_data;
    uint8_t i_mask;
    uint8_t *p_data;

} bits_buffer_t;
Laurent Aimar's avatar
Laurent Aimar committed
34
35

static inline int bits_initwrite( bits_buffer_t *p_buffer,
36
37
38
39
40
41
42
43
                                  int i_size, void *p_data )
{
    p_buffer->i_size = i_size;
    p_buffer->i_data = 0;
    p_buffer->i_mask = 0x80;
    p_buffer->p_data = p_data;
    p_buffer->p_data[0] = 0;
    if( !p_buffer->p_data )
Laurent Aimar's avatar
Laurent Aimar committed
44
    {
45
        if( !( p_buffer->p_data = malloc( i_size ) ) )
Laurent Aimar's avatar
Laurent Aimar committed
46
        {
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
            return( -1 );
        }
        else
        {
            return( 0 );
        }
    }
    else
    {
        return( 0 );
    }
}

static inline void bits_align( bits_buffer_t *p_buffer )
{
    if( p_buffer->i_mask != 0x80 && p_buffer->i_data < p_buffer->i_size )
    {
        p_buffer->i_mask = 0x80;
        p_buffer->i_data++;
        p_buffer->p_data[p_buffer->i_data] = 0x00;
    }
}

Laurent Aimar's avatar
Laurent Aimar committed
70
static inline void bits_write( bits_buffer_t *p_buffer,
71
72
73
74
                               int i_count, uint64_t i_bits )
{
    while( i_count > 0 )
    {
Laurent Aimar's avatar
Laurent Aimar committed
75
76
77
        i_count--;

        if( ( i_bits >> i_count )&0x01 )
78
79
80
        {
            p_buffer->p_data[p_buffer->i_data] |= p_buffer->i_mask;
        }
81
82
83
84
        else
        {
            p_buffer->p_data[p_buffer->i_data] &= ~p_buffer->i_mask;
        }
85
86
87
88
        p_buffer->i_mask >>= 1;
        if( p_buffer->i_mask == 0 )
        {
            p_buffer->i_data++;
Laurent Aimar's avatar
Laurent Aimar committed
89
            p_buffer->i_mask = 0x80;
90
91
92
93
94
        }
    }
}