comparison spandsp-0.0.6pre17/src/bitstream.c @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents
children
comparison
equal deleted inserted replaced
3:c6c5a16ce2f2 4:26cd8f1ef0b1
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 Lesser General Public License version 2.1,
14 * as 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 Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License 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.18.4.1 2009/12/28 12:20:46 steveu Exp $
26 */
27
28 /*! \file */
29
30 #if defined(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/bitstream.h"
41
42 #include "spandsp/private/bitstream.h"
43
44 SPAN_DECLARE(void) bitstream_put(bitstream_state_t *s, uint8_t **c, uint32_t value, int bits)
45 {
46 value &= ((1 << bits) - 1);
47 if (s->lsb_first)
48 {
49 if (s->residue + bits <= 32)
50 {
51 s->bitstream |= (value << s->residue);
52 s->residue += bits;
53 }
54 while (s->residue >= 8)
55 {
56 s->residue -= 8;
57 *(*c)++ = (uint8_t) (s->bitstream & 0xFF);
58 s->bitstream >>= 8;
59 }
60 }
61 else
62 {
63 if (s->residue + bits <= 32)
64 {
65 s->bitstream = (s->bitstream << bits) | value;
66 s->residue += bits;
67 }
68 while (s->residue >= 8)
69 {
70 s->residue -= 8;
71 *(*c)++ = (uint8_t) ((s->bitstream >> s->residue) & 0xFF);
72 }
73 }
74 }
75 /*- End of function --------------------------------------------------------*/
76
77 SPAN_DECLARE(void) bitstream_flush(bitstream_state_t *s, uint8_t **c)
78 {
79 if (s->residue > 0)
80 {
81 s->bitstream &= ((1 << s->residue) - 1);
82 if (s->lsb_first)
83 *(*c)++ = (uint8_t) s->bitstream;
84 else
85 *(*c)++ = (uint8_t) (s->bitstream << (8 - s->residue));
86 s->residue = 0;
87 }
88 s->bitstream = 0;
89 }
90 /*- End of function --------------------------------------------------------*/
91
92 SPAN_DECLARE(uint32_t) bitstream_get(bitstream_state_t *s, const uint8_t **c, int bits)
93 {
94 uint32_t x;
95
96 if (s->lsb_first)
97 {
98 while (s->residue < bits)
99 {
100 s->bitstream |= (((uint32_t) *(*c)++) << s->residue);
101 s->residue += 8;
102 }
103 s->residue -= bits;
104 x = s->bitstream & ((1 << bits) - 1);
105 s->bitstream >>= bits;
106 }
107 else
108 {
109 while (s->residue < bits)
110 {
111 s->bitstream = (s->bitstream << 8) | ((uint32_t) *(*c)++);
112 s->residue += 8;
113 }
114 s->residue -= bits;
115 x = (s->bitstream >> s->residue) & ((1 << bits) - 1);
116 }
117 return x;
118 }
119 /*- End of function --------------------------------------------------------*/
120
121 SPAN_DECLARE(bitstream_state_t *) bitstream_init(bitstream_state_t *s, int lsb_first)
122 {
123 if (s == NULL)
124 {
125 if ((s = (bitstream_state_t *) malloc(sizeof(*s))) == NULL)
126 return NULL;
127 }
128 s->bitstream = 0;
129 s->residue = 0;
130 s->lsb_first = lsb_first;
131 return s;
132 }
133 /*- End of function --------------------------------------------------------*/
134
135 SPAN_DECLARE(int) bitstream_release(bitstream_state_t *s)
136 {
137 return 0;
138 }
139 /*- End of function --------------------------------------------------------*/
140
141 SPAN_DECLARE(int) bitstream_free(bitstream_state_t *s)
142 {
143 if (s)
144 free(s);
145 return 0;
146 }
147 /*- End of function --------------------------------------------------------*/
148 /*- End of file ------------------------------------------------------------*/

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