Project

General

Profile

v42bis.h

dexter, 08/01/2016 04:45 PM

 
1
/*
2
 * SpanDSP - a series of DSP components for telephony
3
 *
4
 * v42bis.h
5
 *
6
 * Written by Steve Underwood <steveu@coppice.org>
7
 *
8
 * Copyright (C) 2005 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: v42bis.h,v 1.27 2009/04/11 18:11:19 steveu Exp $
26
 */
27

    
28
/*! \page v42bis_page V.42bis modem data compression
29
\section v42bis_page_sec_1 What does it do?
30
The v.42bis specification defines a data compression scheme, to work in
31
conjunction with the error correction scheme defined in V.42.
32

33
\section v42bis_page_sec_2 How does it work?
34
*/
35

    
36
#ifndef V42BIS_H
37
#define V42BIS_H
38

    
39
#define V42BIS_MAX_BITS         12
40
#define V42BIS_MAX_CODEWORDS    4096    /* 2^V42BIS_MAX_BITS */
41
#define V42BIS_TABLE_SIZE       5021    /* This should be a prime >(2^V42BIS_MAX_BITS) */
42
#define V42BIS_MAX_STRING_SIZE  250
43

    
44
#define TRUE 1
45
#define FALSE 0
46

    
47
enum
48
{
49
    V42BIS_P0_NEITHER_DIRECTION = 0,
50
    V42BIS_P0_INITIATOR_RESPONDER,
51
    V42BIS_P0_RESPONDER_INITIATOR,
52
    V42BIS_P0_BOTH_DIRECTIONS
53
};
54

    
55
enum
56
{
57
    V42BIS_COMPRESSION_MODE_DYNAMIC = 0,
58
    V42BIS_COMPRESSION_MODE_ALWAYS,
59
    V42BIS_COMPRESSION_MODE_NEVER
60
};
61

    
62
typedef void (*v42bis_frame_handler_t)(void *user_data, const uint8_t *pkt, int len);
63
typedef void (*v42bis_data_handler_t)(void *user_data, const uint8_t *buf, int len);
64

    
65
/*!
66
    V.42bis dictionary node.
67
*/
68
typedef struct
69
{
70
    /*! \brief The prior code for each defined code. */
71
    uint16_t parent_code;
72
    /*! \brief The number of leaf nodes this node has */
73
    int16_t leaves;
74
    /*! \brief This leaf octet for each defined code. */
75
    uint8_t node_octet;
76
    /*! \brief Bit map of the children which exist */
77
    uint32_t children[8];
78
} v42bis_dict_node_t;
79

    
80
/*!
81
    V.42bis compression. This defines the working state for a single instance
82
    of V.42bis compression.
83
*/
84
typedef struct
85
{
86
    /*! \brief Compression mode. */
87
    int compression_mode;
88
    /*! \brief Callback function to handle received frames. */
89
    v42bis_frame_handler_t handler;
90
    /*! \brief An opaque pointer passed in calls to frame_handler. */
91
    void *user_data;
92
    /*! \brief The maximum frame length allowed */
93
    int max_len;
94

    
95
    uint32_t string_code;
96
    uint32_t latest_code;
97
    int string_length;
98
    uint32_t output_bit_buffer;
99
    int output_bit_count;
100
    int output_octet_count;
101
    uint8_t output_buf[1024];
102
    v42bis_dict_node_t dict[V42BIS_MAX_CODEWORDS];
103
    /*! \brief TRUE if we are in transparent (i.e. uncompressable) mode */
104
    int transparent;
105
    int change_transparency;
106
    /*! \brief IIR filter state, used in assessing compressibility. */
107
    int compressibility_filter;
108
    int compressibility_persistence;
109
    
110
    /*! \brief Next empty dictionary entry */
111
    uint32_t v42bis_parm_c1;
112
    /*! \brief Current codeword size */
113
    int v42bis_parm_c2;
114
    /*! \brief Threshold for codeword size change */
115
    uint32_t v42bis_parm_c3;
116

    
117
    /*! \brief Mark that this is the first octet/code to be processed */
118
    int first;
119
    uint8_t escape_code;
120
} v42bis_compress_state_t;
121

    
122
/*!
123
    V.42bis decompression. This defines the working state for a single instance
124
    of V.42bis decompression.
125
*/
126
typedef struct
127
{
128
    /*! \brief Callback function to handle decompressed data. */
129
    v42bis_data_handler_t handler;
130
    /*! \brief An opaque pointer passed in calls to data_handler. */
131
    void *user_data;
132
    /*! \brief The maximum decompressed data block length allowed */
133
    int max_len;
134

    
135
    uint32_t old_code;
136
    uint32_t last_old_code;
137
    uint32_t input_bit_buffer;
138
    int input_bit_count;
139
    int octet;
140
    int last_length;
141
    int output_octet_count;
142
    uint8_t output_buf[1024];
143
    v42bis_dict_node_t dict[V42BIS_MAX_CODEWORDS];
144
    /*! \brief TRUE if we are in transparent (i.e. uncompressable) mode */
145
    int transparent;
146

    
147
    int last_extra_octet;
148

    
149
    /*! \brief Next empty dictionary entry */
150
    uint32_t v42bis_parm_c1;
151
    /*! \brief Current codeword size */
152
    int v42bis_parm_c2;
153
    /*! \brief Threshold for codeword size change */
154
    uint32_t v42bis_parm_c3;
155
        
156
    /*! \brief Mark that this is the first octet/code to be processed */
157
    int first;
158
    uint8_t escape_code;
159
    int escaped;
160
} v42bis_decompress_state_t;
161

    
162
/*!
163
    V.42bis compression/decompression descriptor. This defines the working state for a
164
    single instance of V.42bis compress/decompression.
165
*/
166
struct v42bis_state_s
167
{
168
    /*! \brief V.42bis data compression directions. */
169
    int v42bis_parm_p0;
170

    
171
    /*! \brief Compression state. */
172
    v42bis_compress_state_t compress;
173
    /*! \brief Decompression state. */
174
    v42bis_decompress_state_t decompress;
175
    
176
    /*! \brief Maximum codeword size (bits) */
177
    int v42bis_parm_n1;
178
    /*! \brief Total number of codewords */
179
    uint32_t v42bis_parm_n2;
180
    /*! \brief Maximum string length */
181
    int v42bis_parm_n7;
182
};
183

    
184

    
185
/*!
186
    V.42bis compression/decompression descriptor. This defines the working state for a
187
    single instance of V.42bis compress/decompression.
188
*/
189
typedef struct v42bis_state_s v42bis_state_t;
190

    
191

    
192

    
193
/*! Compress a block of octets.
194
    \param s The V.42bis context.
195
    \param buf The data to be compressed.
196
    \param len The length of the data buffer.
197
    \return 0 */
198
int v42bis_compress(v42bis_state_t *s, const uint8_t *buf, int len);
199

    
200
/*! Flush out any data remaining in a compression buffer.
201
    \param s The V.42bis context.
202
    \return 0 */
203
int v42bis_compress_flush(v42bis_state_t *s);
204

    
205
/*! Decompress a block of octets.
206
    \param s The V.42bis context.
207
    \param buf The data to be decompressed.
208
    \param len The length of the data buffer.
209
    \return 0 */
210
int v42bis_decompress(v42bis_state_t *s, const uint8_t *buf, int len);
211
    
212
/*! Flush out any data remaining in the decompression buffer.
213
    \param s The V.42bis context.
214
    \return 0 */
215
int v42bis_decompress_flush(v42bis_state_t *s);
216

    
217
/*! Set the compression mode.
218
    \param s The V.42bis context.
219
    \param mode One of the V.42bis compression modes -
220
            V42BIS_COMPRESSION_MODE_DYNAMIC,
221
            V42BIS_COMPRESSION_MODE_ALWAYS,
222
            V42BIS_COMPRESSION_MODE_NEVER */
223
void v42bis_compression_control(v42bis_state_t *s, int mode);
224

    
225
/*! Initialise a V.42bis context.
226
    \param s The V.42bis context.
227
    \param negotiated_p0 The negotiated P0 parameter, from the V.42bis spec.
228
    \param negotiated_p1 The negotiated P1 parameter, from the V.42bis spec.
229
    \param negotiated_p2 The negotiated P2 parameter, from the V.42bis spec.
230
    \param frame_handler Frame callback handler.
231
    \param frame_user_data An opaque pointer passed to the frame callback handler.
232
    \param max_frame_len The maximum length that should be passed to the frame handler.
233
    \param data_handler data callback handler.
234
    \param data_user_data An opaque pointer passed to the data callback handler.
235
    \param max_data_len The maximum length that should be passed to the data handler.
236
    \return The V.42bis context. */
237
v42bis_state_t *v42bis_init(v42bis_state_t *s,
238
                                           int negotiated_p0,
239
                                           int negotiated_p1,
240
                                           int negotiated_p2,
241
                                           v42bis_frame_handler_t frame_handler,
242
                                           void *frame_user_data,
243
                                           int max_frame_len,
244
                                           v42bis_data_handler_t data_handler,
245
                                           void *data_user_data,
246
                                           int max_data_len);
247

    
248
/*! Release a V.42bis context.
249
    \param s The V.42bis context.
250
    \return 0 if OK */
251
int v42bis_release(v42bis_state_t *s);
252

    
253
/*! Free a V.42bis context.
254
    \param s The V.42bis context.
255
    \return 0 if OK */
256
int v42bis_free(v42bis_state_t *s);
257

    
258
#endif
259
/*- End of file ------------------------------------------------------------*/
Add picture from clipboard (Maximum size: 48.8 MB)