]> git.sesse.net Git - vlc/blob - modules/mux/mpeg/bits.h
* pes.c, ps.c: a52 stream should now work.
[vlc] / modules / mux / mpeg / bits.h
1 /*****************************************************************************
2  * bits.h
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: bits.h,v 1.3 2003/01/08 10:34:58 fenrir Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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         {        
47             return( -1 );
48         }
49         else
50         {
51             return( 0 );
52         }
53     }
54     else
55     {
56         return( 0 );
57     }
58 }
59
60 static inline void bits_align( bits_buffer_t *p_buffer )
61 {
62     if( p_buffer->i_mask != 0x80 && p_buffer->i_data < p_buffer->i_size )
63     {
64         p_buffer->i_mask = 0x80;
65         p_buffer->i_data++;
66         p_buffer->p_data[p_buffer->i_data] = 0x00;
67     }
68 }
69
70 static inline void bits_write( bits_buffer_t *p_buffer, 
71                                int i_count, uint64_t i_bits )
72 {
73     while( i_count > 0 )
74     {
75         if( ( i_bits >> ( i_count - 1 ) )&0x01 )
76         {
77             p_buffer->p_data[p_buffer->i_data] |= p_buffer->i_mask;
78         }
79         else
80         {
81             p_buffer->p_data[p_buffer->i_data] &= ~p_buffer->i_mask;
82         }
83         p_buffer->i_mask >>= 1;
84         if( p_buffer->i_mask == 0 )
85         {
86             p_buffer->i_data++;
87             if( p_buffer->i_data < p_buffer->i_size )
88             {
89 //                p_buffer->p_data[p_buffer->i_data] = 0;
90                 p_buffer->i_mask = 0x80;
91             }
92         }
93         i_count--;
94     }
95 }
96
97