2
* TwoLAME: an optimized MPEG Audio Layer Two encoder
4
* Copyright (C) 2001-2004 Michael Cheng
5
* Copyright (C) 2004-2005 The TwoLAME Project
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
#include "bitbuffer.h"
34
bit_stream* buffer_init( unsigned char *buffer, int buffer_size )
36
bit_stream* bs = (bit_stream *)twolame_malloc(sizeof(bit_stream),"bit_stream");
39
bs->buf_size = buffer_size;
49
/* Dellocate bit buffer */
50
void buffer_deinit( bit_stream ** bs )
52
twolame_free( (void**)bs );
56
/*write 1 bit from the bit stream */
57
void buffer_put1bit (bit_stream * bs, int bit)
61
bs->buf[bs->buf_byte_idx] |= (bit & 0x1) << (bs->buf_bit_idx - 1);
63
if (!bs->buf_bit_idx) {
66
if (bs->buf_byte_idx >= bs->buf_size) {
67
//empty_buffer (bs, minimum);
68
fprintf(stdout,"buffer_put1bit: error. bit_stream buffer needs to be bigger\n");
71
bs->buf[bs->buf_byte_idx] = 0;
75
/*write N bits into the bit stream */
76
inline void buffer_putbits (bit_stream * bs, unsigned int val, int N)
78
static const int putmask[9] = { 0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff };
84
k = MIN (j, bs->buf_bit_idx);
86
bs->buf[bs->buf_byte_idx] |= (tmp & putmask[k]) << (bs->buf_bit_idx - k);
88
if (!bs->buf_bit_idx) {
91
if (bs->buf_byte_idx >= bs->buf_size) {
92
//empty_buffer (bs, minimum);
93
fprintf(stdout,"buffer_putbits: error. bit_stream buffer needs to be bigger\n");
96
bs->buf[bs->buf_byte_idx] = 0;
102
/*return the current bit stream length (in bits)*/
103
unsigned long buffer_sstell (bit_stream * bs)