comparison spandsp-0.0.3/spandsp-0.0.3/src/bitstream.c @ 5:f762bf195c4b

import spandsp-0.0.3
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 16:00:21 +0200
parents
children
comparison
equal deleted inserted replaced
4:26cd8f1ef0b1 5:f762bf195c4b
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * bitstream.c - Bitstream composition and decomposition routines.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2006 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * $Id: bitstream.c,v 1.6 2006/11/30 15:41:47 steveu Exp $
26 */
27
28 /*! \file */
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <inttypes.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <assert.h>
38
39 #include "spandsp/telephony.h"
40 #include "spandsp/logging.h"
41 #include "spandsp/bitstream.h"
42
43 void bitstream_put(bitstream_state_t *s, uint8_t **c, unsigned int value, int bits)
44 {
45 value &= ((1 << bits) - 1);
46 if (s->residue + bits <= 32)
47 {
48 s->bitstream |= (value << s->residue);
49 s->residue += bits;
50 }
51 while (s->residue >= 8)
52 {
53 s->residue -= 8;
54 *(*c)++ = (uint8_t)(s->bitstream & 0xFF);
55 s->bitstream >>= 8;
56 }
57 }
58 /*- End of function --------------------------------------------------------*/
59
60 void bitstream_flush(bitstream_state_t *s, uint8_t **c)
61 {
62 if (s->residue > 0)
63 {
64 *(*c)++ = (uint8_t)((s->bitstream << (8 - s->residue)) & 0xFF);
65 s->residue = 0;
66 }
67 }
68 /*- End of function --------------------------------------------------------*/
69
70 unsigned int bitstream_get(bitstream_state_t *s, const uint8_t **c, int bits)
71 {
72 unsigned int x;
73
74 while (s->residue < bits)
75 {
76 x = (unsigned int) *(*c)++;
77 s->bitstream |= (x << s->residue);
78 s->residue += 8;
79 }
80 s->residue -= bits;
81 x = s->bitstream & ((1 << bits) - 1);
82 s->bitstream >>= bits;
83 return x;
84 }
85 /*- End of function --------------------------------------------------------*/
86
87 void bitstream_put2(bitstream_state_t *s, uint8_t **c, unsigned int value, int bits)
88 {
89 value &= ((1 << bits) - 1);
90 if (s->residue + bits <= 32)
91 {
92 s->bitstream = (s->bitstream << bits) | value;
93 s->residue += bits;
94 }
95 while (s->residue >= 8)
96 {
97 s->residue -= 8;
98 *(*c)++ = (uint8_t) ((s->bitstream >> s->residue) & 0xFF);
99 }
100 }
101 /*- End of function --------------------------------------------------------*/
102
103 void bitstream_flush2(bitstream_state_t *s, uint8_t **c)
104 {
105 if (s->residue > 0)
106 {
107 *(*c)++ = (uint8_t) ((s->bitstream << (8 - s->residue)) & 0xFF);
108 s->residue = 0;
109 }
110 }
111 /*- End of function --------------------------------------------------------*/
112
113 unsigned int bitstream_get2(bitstream_state_t *s, const uint8_t **c, int bits)
114 {
115 unsigned int x;
116
117 while (s->residue < bits)
118 {
119 x = (unsigned int) *(*c)++;
120 s->bitstream = (s->bitstream << 8) | x;
121 s->residue += 8;
122 }
123 s->residue -= bits;
124 x = (s->bitstream >> s->residue) & ((1 << bits) - 1);
125 return x;
126 }
127 /*- End of function --------------------------------------------------------*/
128
129 bitstream_state_t *bitstream_init(bitstream_state_t *s)
130 {
131 if (s)
132 memset(s, 0, sizeof(*s));
133 return s;
134 }
135 /*- End of function --------------------------------------------------------*/
136 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.