| 1 |
/* |
| 2 |
* TwoLAME: an optimized MPEG Audio Layer Two encoder |
| 3 |
* |
| 4 |
* Copyright (C) 2001-2004 Michael Cheng |
| 5 |
* Copyright (C) 2004-2018 The TwoLAME Project |
| 6 |
* |
| 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. |
| 11 |
* |
| 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. |
| 16 |
* |
| 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 |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
/* write 1 bit from the bit stream */ |
| 25 |
static inline void buffer_put1bit(bit_stream * bs, int bit) |
| 26 |
{ |
| 27 |
if (bs->buf_byte_idx < bs->buf_size) { |
| 28 |
bs->totbit++; |
| 29 |
|
| 30 |
bs->buf[bs->buf_byte_idx] |= (bit & 0x1) << (bs->buf_bit_idx - 1); |
| 31 |
bs->buf_bit_idx--; |
| 32 |
if (!bs->buf_bit_idx) { |
| 33 |
bs->buf_bit_idx = 8; |
| 34 |
bs->buf_byte_idx++; |
| 35 |
if (bs->buf_byte_idx < bs->buf_size) { |
| 36 |
bs->buf[bs->buf_byte_idx] = 0; |
| 37 |
} |
| 38 |
else { |
| 39 |
fprintf(stderr, "buffer_put1bit: error. bit_stream buffer full\n"); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
else |
| 44 |
fprintf(stderr, "buffer_put1bit: error. bit_stream buffer needs to be bigger\n"); |
| 45 |
} |
| 46 |
|
| 47 |
/* write N bits into the bit stream */ |
| 48 |
static inline void buffer_putbits(bit_stream * bs, unsigned int val, int N) |
| 49 |
{ |
| 50 |
static const int putmask[9] = { 0x0, 0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff }; |
| 51 |
register int j = N; |
| 52 |
register int k, tmp; |
| 53 |
|
| 54 |
if (bs->buf_byte_idx < bs->buf_size) { |
| 55 |
while (j > 0) { |
| 56 |
k = MIN(j, bs->buf_bit_idx); |
| 57 |
tmp = val >> (j - k); |
| 58 |
bs->buf[bs->buf_byte_idx] |= (tmp & putmask[k]) << (bs->buf_bit_idx - k); |
| 59 |
bs->buf_bit_idx -= k; |
| 60 |
bs->totbit += k; |
| 61 |
if (!bs->buf_bit_idx) { |
| 62 |
bs->buf_bit_idx = 8; |
| 63 |
bs->buf_byte_idx++; |
| 64 |
if (bs->buf_byte_idx < bs->buf_size) { |
| 65 |
bs->buf[bs->buf_byte_idx] = 0; |
| 66 |
} |
| 67 |
else { |
| 68 |
fprintf(stderr, "buffer_putbits: error. bit_stream buffer full\n"); |
| 69 |
break; |
| 70 |
} |
| 71 |
} |
| 72 |
j -= k; |
| 73 |
} |
| 74 |
} |
| 75 |
else |
| 76 |
fprintf(stderr, "buffer_putbits: error. bit_stream buffer needs to be bigger\n"); |
| 77 |
} |
| 78 |
|
| 79 |
// vim:ts=4:sw=4:nowrap: |