source: roaraudio/roard/codecfilter_speex.c @ 397:32efb0d12bb8

Last change on this file since 397:32efb0d12bb8 was 397:32efb0d12bb8, checked in by phi, 16 years ago

bytes vs. samples solved

File size: 4.5 KB
Line 
1//codecfilter_speex.c:
2
3#include "roard.h"
4#ifdef ROAR_HAVE_LIBSPEEX
5
6int cf_speex_open(CODECFILTER_USERDATA_T * inst, int codec,
7                                            struct roar_stream_server * info,
8                                            struct roar_codecfilter   * filter) {
9 struct codecfilter_speex_inst * self = malloc(sizeof(struct codecfilter_speex_inst));
10 struct roar_stream * s = (struct roar_stream *) info;
11
12 *inst = NULL;
13
14 if (!self)
15  return -1;
16
17 self->encoder = NULL;
18 self->decoder = NULL;
19
20 self->stream  = info;
21
22 self->cd      = NULL;
23
24 self->i_rest  = NULL;
25 self->fi_rest = 0;
26
27 speex_bits_init(&(self->bits));
28
29 s->info.codec    = ROAR_CODEC_DEFAULT;
30 s->info.bits     = 16; // speex hardcoded
31 s->info.channels = 1; // only mono support at the moment
32
33 *inst = (void*) self;
34
35 return 0;
36}
37
38int cf_speex_close(CODECFILTER_USERDATA_T   inst) {
39 struct codecfilter_speex_inst * self = (struct codecfilter_speex_inst *) inst;
40
41 if (!self)
42  return -1;
43
44 if ( self->encoder )
45  speex_encoder_destroy(self->decoder);
46
47 self->encoder = NULL;
48
49 if ( self->decoder )
50  speex_decoder_destroy(self->decoder);
51
52 self->decoder = NULL;
53
54 speex_bits_destroy(&(self->bits));
55
56 if ( self->cd )
57  free(self->cd);
58
59 if ( self->i_rest )
60  free(self->i_rest);
61
62 free((void*)self);
63
64 return 0;
65}
66
67int cf_speex_read(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
68 struct codecfilter_speex_inst * self = (struct codecfilter_speex_inst *) inst;
69 struct roar_stream * s = (struct roar_stream *) self->stream;
70 int mode;
71 uint16_t ui;
72 int tmp;
73 int still_todo = len / 2 /* 16 bit */;
74 int ret = 0;
75
76 ROAR_WARN("cf_speex_read(inst=%p, buf=%p, len=%i) = ?", inst, buf, len);
77
78 if ( ! self->decoder ) {
79  ROAR_DBG("cf_speex_read(*): no decoder, starting one!");
80  if ( read(s->fh, &ui, 2) != 2 )
81   return 0;
82
83  mode = ntohs(ui);
84
85  if ( mode == ROAR_SPEEX_MODE_NB ) {
86   self->decoder = speex_decoder_init(&speex_nb_mode);
87  } else if ( mode == ROAR_SPEEX_MODE_WB ) {
88   self->decoder = speex_decoder_init(&speex_wb_mode);
89  } else if ( mode == ROAR_SPEEX_MODE_UWB ) {
90   self->decoder = speex_decoder_init(&speex_uwb_mode);
91  } else {
92   return 0;
93  }
94
95  tmp=1;
96  speex_decoder_ctl(self->decoder, SPEEX_SET_ENH, &tmp);
97
98  speex_decoder_ctl(self->decoder, SPEEX_GET_FRAME_SIZE, &(self->frame_size));
99
100
101
102  ROAR_WARN("cf_speex_read(*): frame_size=%i (%i bytes)", self->frame_size, self->frame_size*2);
103
104  if ( !self->cd ) {
105   self->cd = malloc((self->frame_size)*2);
106   if ( !self->cd )
107    return 0;
108  }
109
110  if ( !self->i_rest ) {
111   self->i_rest = malloc((self->frame_size)*2);
112   if ( !self->i_rest )
113    return 0;
114  }
115 }
116
117 ROAR_DBG("cf_speex_read(*): Have a working decoder!");
118
119 ROAR_DBG("cf_speex_read(*): frame_size=%i (%i bytes)", self->frame_size, self->frame_size*2);
120 ROAR_DBG("cf_speex_read(*): i_rest is %i bytes after cd", ((void*)self->i_rest - (void*)self->cd));
121
122
123 if ( self->fi_rest ) {
124  if ( self->fi_rest > (still_todo*2) ) {
125   ROAR_WARN("cf_speex_read(*): discarding input rest data: buffer too long!");
126   self->fi_rest = 0;
127  } else {
128   ROAR_WARN("cf_speex_read(*): using data from input rest buffer: len=%i", self->fi_rest);
129   memcpy(buf, self->i_rest, self->fi_rest);
130   buf += self->fi_rest;
131   still_todo -= self->fi_rest/2;
132   ret += self->fi_rest;
133   self->fi_rest = 0;
134  }
135 }
136
137 while (still_todo) {
138  ROAR_WARN("cf_speex_read(*): we sill need %i frames", still_todo);
139  if ( read(s->fh, &ui, 2) != 2 )
140   return -1;
141
142  ui = ntohs(ui);
143
144  if ( ui > ROAR_SPEEX_MAX_CC )
145   return 0;
146
147  if ( read(s->fh, self->cc, ui) != ui )
148   break;
149
150  speex_bits_read_from(&(self->bits), self->cc, ui);
151
152  speex_decode_int(self->decoder, &(self->bits), self->cd);
153
154  if ( self->frame_size > still_todo ) {
155   memcpy(buf, self->cd, still_todo*2);
156   ret += still_todo*2;
157   self->fi_rest = (self->frame_size - still_todo)*2;
158   ROAR_WARN("cf_speex_read(*): self->fi_rest=%i, off=%i", self->fi_rest, still_todo*2);
159   memcpy(self->i_rest, (self->cd)+(still_todo*2), self->fi_rest);
160   still_todo = 0;
161  } else {
162   memcpy(buf, self->cd, self->frame_size*2);
163   buf        += self->frame_size*2;
164   ret        += self->frame_size*2;
165   still_todo -= self->frame_size;
166  }
167 }
168
169 if ( still_todo ) {
170  ROAR_WARN("cf_speex_read(*): could not read all reqquested data, returning %i byte less", still_todo*2);
171 }
172
173 ROAR_WARN("cf_speex_read(*) = %i", ret);
174
175 return ret;
176}
177
178int cf_speex_write(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
179 struct codecfilter_speex_inst * self = (struct codecfilter_speex_inst *) inst;
180
181 return -1;
182}
183
184#endif
185
186//ll
Note: See TracBrowser for help on using the repository browser.