1 /* Copyright (C) 2005 Jean-Marc Valin */
2 /**
3   @file pcm_wrapper.c
4   @brief PCM encoding wrapped as a Speex mode
5  */
6 /*
7    Redistribution and use in source and binary forms, with or without
8    modification, are permitted provided that the following conditions
9    are met:
10 
11    - Redistributions of source code must retain the above copyright
12    notice, this list of conditions and the following disclaimer.
13 
14    - Redistributions in binary form must reproduce the above copyright
15    notice, this list of conditions and the following disclaimer in the
16    documentation and/or other materials provided with the distribution.
17 
18    - Neither the name of the Xiph.org Foundation nor the names of its
19    contributors may be used to endorse or promote products derived from
20    this software without specific prior written permission.
21 
22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
26    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 
40 #include "pcm_wrapper.h"
41 #include "misc.h"
42 
43 
44 typedef struct {
45    const SpeexMode *mode;
46    int frame_size;
47    int format;
48 } PCMState;
49 
50 /** Initializes encoder state*/
pcm_encoder_init(const SpeexMode * m)51 static void *pcm_encoder_init(const SpeexMode *m)
52 {
53    PCMState *st = (PCMState*)speex_alloc(sizeof(PCMState));
54    st->mode = m;
55    st->frame_size = 64;
56 
57 }
58 
59 /** De-allocates encoder state resources*/
pcm_encoder_destroy(void * state)60 static void pcm_encoder_destroy(void *state)
61 {
62    speex_free(state);
63 }
64 
65 /** Encodes one frame*/
pcm_encode(void * state,void * vin,SpeexBits * bits)66 static int pcm_encode(void *state, void *vin, SpeexBits *bits)
67 {
68    int i;
69    PCMState *st = (PCMState*)state;
70    spx_word16_t *in = vin;
71    for (i=0;i<st->frame_size;i++)
72    {
73       spx_int16_t x;
74       x = in[i];
75       speex_bits_pack(bits, x, 16);
76    }
77 }
78 
79 /** Initializes decoder state*/
pcm_decoder_init(const SpeexMode * m)80 static void *pcm_decoder_init(const SpeexMode *m)
81 {
82    PCMState *st = (PCMState*)speex_alloc(sizeof(PCMState));
83    st->mode = m;
84    st->frame_size = 64;
85 }
86 
87 /** De-allocates decoder state resources*/
pcm_decoder_destroy(void * state)88 static void pcm_decoder_destroy(void *state)
89 {
90    speex_free(state);
91 }
92 
93 /** Decodes one frame*/
pcm_decode(void * state,SpeexBits * bits,void * vout)94 static int pcm_decode(void *state, SpeexBits *bits, void *vout)
95 {
96    int i;
97    PCMState *st = (PCMState*)state;
98    spx_word16_t *out = vout;
99    for (i=0;i<st->frame_size;i++)
100    {
101       spx_int16_t x;
102       x = speex_bits_unpack_signed(bits, 16);
103       out[i] = x;
104    }
105 }
106 
107 /** ioctl-like function for controlling a narrowband encoder */
pcm_encoder_ctl(void * state,int request,void * ptr)108 static int pcm_encoder_ctl(void *state, int request, void *ptr)
109 {
110    PCMState *st;
111    st=(PCMState*)state;
112    switch(request)
113    {
114       case PCM_SET_FRAME_SIZE:
115          st->frame_size = (*(int*)ptr);
116          break;
117       case PCM_GET_FRAME_SIZE:
118          (*(int*)ptr) = st->frame_size;
119          break;
120       default:
121          speex_warning_int("Unknown nb_ctl request: ", request);
122          return -1;
123    }
124 }
125 
126 /** ioctl-like function for controlling a narrowband decoder */
pcm_decoder_ctl(void * state,int request,void * ptr)127 static int pcm_decoder_ctl(void *state, int request, void *ptr)
128 {
129    PCMState *st;
130    st=(PCMState*)state;
131    switch(request)
132    {
133       case PCM_SET_FRAME_SIZE:
134          st->frame_size = (*(int*)ptr);
135          break;
136       case PCM_GET_FRAME_SIZE:
137          (*(int*)ptr) = st->frame_size;
138          break;
139       default:
140          speex_warning_int("Unknown nb_ctl request: ", request);
141          return -1;
142 
143    }
144 }
145 
146 typedef struct {
147  int dummy;
148 } PCMMode;
149 
150 static PCMMode pcmmode;
151 
pcm_mode_query(const void * mode,int request,void * ptr)152 int pcm_mode_query(const void *mode, int request, void *ptr)
153 {
154    const PCMMode *m = (const PCMMode*)mode;
155 
156    switch (request)
157    {
158       default:
159          speex_warning_int("Unknown nb_mode_query request: ", request);
160          return -1;
161    }
162    return 0;
163 }
164 /* Default mode for narrowband */
165 const SpeexMode pcm_wrapper_mode = {
166    &pcmmode,
167    pcm_mode_query,
168    "PCM",
169    0,
170    4,
171    &pcm_encoder_init,
172    &pcm_encoder_destroy,
173    &pcm_encode,
174    &pcm_decoder_init,
175    &pcm_decoder_destroy,
176    &pcm_decode,
177    &pcm_encoder_ctl,
178    &pcm_decoder_ctl,
179 };
180 
181 const SpeexMode *speex_pcm_wrapper = &pcm_wrapper_mode;
182