source: roaraudio/roard/codecfilter_celt.c @ 4957:45ba4cf2abe3

Last change on this file since 4957:45ba4cf2abe3 was 4957:45ba4cf2abe3, checked in by phi, 13 years ago

use roar_mm_*() where possible

File size: 8.9 KB
Line 
1//codecfilter_celt.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27#ifdef ROAR_HAVE_LIBCELT
28
29#ifdef ROAR_HAVE_CELT_VERSION_0_7_1
30typedef celt_int16 celt_int16_t;
31#endif
32
33int cf_celt_open(CODECFILTER_USERDATA_T * inst, int codec,
34                                            struct roar_stream_server * info,
35                                            struct roar_codecfilter   * filter) {
36 struct codecfilter_celt_inst * self = roar_mm_malloc(sizeof(struct codecfilter_celt_inst));
37 struct roar_stream * s = ROAR_STREAM(info);
38
39 if ( !self )
40  return -1;
41
42/*
43 CELTMode * mode;
44 CELTEncoder * encoder;
45 CELTDecoder * decoder;
46 int frame_size;
47 int lookahead;
48 int out_size;
49 char * ibuf;
50 char * obuf;
51 char * rest;
52 int s_buf;
53 int f_rest; /-* how much is in rest? *-/
54*/
55
56 self->stream               = info;
57 self->frame_size           = 256;
58 self->lookahead            = self->frame_size;
59 self->encoder              = NULL;
60 self->decoder              = NULL;
61 self->opened_encoder       = 0;
62 self->opened_decoder       = 0;
63 self->s_buf                = s->info.channels * self->frame_size * 2;
64 self->ibuf                 = roar_mm_malloc(self->s_buf);
65 self->obuf                 = roar_mm_malloc(self->s_buf);
66 self->i_rest               = roar_mm_malloc(self->s_buf);
67 self->o_rest               = roar_mm_malloc(self->s_buf);
68 self->fi_rest              = 0;
69 self->fo_rest              = 0;
70
71 if ( !(self->ibuf && self->obuf && self->i_rest && self->o_rest) ) {
72  if ( self->ibuf != NULL )
73   roar_mm_free(self->ibuf);
74
75  if ( self->obuf != NULL )
76   roar_mm_free(self->obuf);
77
78  if ( self->i_rest != NULL )
79   roar_mm_free(self->o_rest);
80
81  if ( self->o_rest != NULL )
82   roar_mm_free(self->o_rest);
83
84  roar_mm_free(self);
85  return -1;
86 }
87
88#ifdef ROAR_HAVE_CELT_VERSION_0_7_1
89 self->mode                 = celt_mode_create(s->info.rate, self->frame_size, NULL);
90#else
91 self->mode                 = celt_mode_create(s->info.rate, s->info.channels, self->frame_size, NULL);
92#endif
93
94 if ( !self->mode ) {
95  roar_mm_free(self);
96  return -1;
97 }
98
99 if ( s->dir == ROAR_DIR_PLAY ) {
100#ifdef ROAR_HAVE_CELT_VERSION_0_7_1
101   self->decoder = celt_decoder_create(self->mode, s->info.channels, NULL);
102#else
103   self->decoder = celt_decoder_create(self->mode);
104#endif
105 } else if ( s->dir == ROAR_DIR_MONITOR || s->dir == ROAR_DIR_OUTPUT ) {
106#ifdef ROAR_HAVE_CELT_VERSION_0_7_1
107   self->encoder = celt_encoder_create(self->mode, s->info.channels, NULL);
108#else
109   self->encoder = celt_encoder_create(self->mode);
110#endif
111 } else if ( s->dir == ROAR_DIR_BIDIR ) {
112#ifdef ROAR_HAVE_CELT_VERSION_0_7_1
113   self->decoder = celt_decoder_create(self->mode, s->info.channels, NULL);
114   self->encoder = celt_encoder_create(self->mode, s->info.channels, NULL);
115#else
116   self->decoder = celt_decoder_create(self->mode);
117   self->encoder = celt_encoder_create(self->mode);
118#endif
119 } else {
120  celt_mode_destroy(self->mode);
121  roar_mm_free(self);
122  return -1;
123 }
124
125 *inst = (CODECFILTER_USERDATA_T) self;
126
127 s->info.codec = ROAR_CODEC_DEFAULT;
128 s->info.bits  = 16; // CELT hardcoded
129
130 return 0;
131}
132
133int cf_celt_close(CODECFILTER_USERDATA_T   inst) {
134 struct codecfilter_celt_inst * self = (struct codecfilter_celt_inst *) inst;
135
136 if ( !inst )
137  return -1;
138
139 if ( self->encoder != NULL )
140  celt_encoder_destroy(self->encoder);
141
142 if ( self->decoder != NULL )
143  celt_decoder_destroy(self->decoder);
144
145 if ( self->mode != NULL )
146  celt_mode_destroy(self->mode);
147
148 if ( self->ibuf != NULL )
149  roar_mm_free(self->ibuf);
150
151 if ( self->obuf != NULL )
152  roar_mm_free(self->obuf);
153
154 if ( self->i_rest != NULL )
155  roar_mm_free(self->i_rest);
156
157 if ( self->o_rest != NULL )
158  roar_mm_free(self->o_rest);
159
160 roar_mm_free(inst);
161 return 0;
162}
163
164int cf_celt_read(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
165 struct codecfilter_celt_inst * self = (struct codecfilter_celt_inst *) inst;
166 int r = 0;
167 uint16_t fs;
168 char * cbuf;
169 char magic[ROAR_CELT_MAGIC_LEN];
170
171// printf("buf=%p, len=%i\n", buf, len);
172
173 if ( !self->opened_decoder ) {
174  errno = ENOSYS;
175  if ( stream_vio_s_read(self->stream, magic, ROAR_CELT_MAGIC_LEN) != ROAR_CELT_MAGIC_LEN )
176   return -1;
177  if ( memcmp(magic, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) != 0 )
178   return -1;
179
180  errno = 0;
181  self->opened_decoder = 1;
182 }
183
184 if ( self->fi_rest ) {
185  memcpy(buf, self->i_rest, self->fi_rest);
186  r += self->fi_rest;
187  self->fi_rest = 0;
188 }
189
190 while ( r <= (len - self->s_buf) ) {
191  if ( stream_vio_s_read(self->stream, &fs, 2) != 2 )
192   break;
193
194  fs = ROAR_NET2HOST16(fs);
195
196//  ROAR_WARN("0:fs=%i", fs);
197
198  if ( fs > self->s_buf )
199   return -1;
200
201  if ( stream_vio_s_read(self->stream, self->ibuf, fs) != fs )
202   return -1;
203
204  cbuf = buf + r;
205
206//  printf("buf=%p, r=%i // cbuf=%p\n", buf, r, cbuf);
207  if ( celt_decode(self->decoder, (unsigned char *) self->ibuf, fs, (celt_int16_t *) cbuf) < 0 )
208   return -1;
209
210  r += self->s_buf;
211 }
212
213 if ( r < len ) {
214//  printf("r < len!\n");
215  if ( stream_vio_s_read(self->stream, &fs, 2) == 2 ) {
216   fs = ROAR_NET2HOST16(fs);
217//  ROAR_WARN("1:fs=%i", fs);
218//   printf("next: fs=%i\n", fs);
219   if ( fs > self->s_buf )
220    return -1;
221   if ( stream_vio_s_read(self->stream, self->ibuf, fs) == fs ) {
222//    printf("got data!\n");
223    if ( celt_decode(self->decoder, (unsigned char *) self->ibuf, fs, (celt_int16_t *) self->obuf) >= 0 ) {
224//     printf("{ // decode rest\n");
225//     printf(" r=%i // need %i Bytes\n", r, len - r);
226//     printf(" memcpy(buf+%i, self->obuf, %i) = ?\n", r, len - r);
227     memcpy(buf+r, self->obuf, len - r);
228     self->fi_rest = self->s_buf + r - len;
229     memcpy(self->i_rest, self->obuf + len - r, self->fi_rest);
230//     printf(" len=%i, r=%i, fi_rest=%i, s_buf=%i\n", len, r, self->fi_rest, self->s_buf);
231     r = len;
232//     printf("}\n");
233    }
234   }
235  }
236 }
237
238 ROAR_DBG("cf_celt_read(inst=%p, buf=%p, len=%i) = %i", inst, buf, len, r);
239 return r;
240}
241
242#define BS (ROAR_STREAM(self->stream)->info.channels * 32)
243int cf_celt_write(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
244 struct codecfilter_celt_inst * self = (struct codecfilter_celt_inst *) inst;
245 int have = 0;
246 int org_len = len;
247 int diff;
248 int fs2 = self->frame_size * 2 * ROAR_STREAM(self->stream)->info.channels;
249 int sid;
250 uint16_t pkglen_net, pkglen;
251 unsigned char cbits[BS+2];
252 void * prethru;
253
254 if ( !self->opened_encoder ) {
255  sid = ROAR_STREAM(self->stream)->id;
256
257  if ( stream_prethru_destroy(sid) == -1 ) {
258   return -1;
259  }
260
261  if ( stream_prethru_add_data(sid, &prethru, ROAR_CELT_MAGIC_LEN) == -1 ) {
262   return -1;
263  }
264
265  memcpy(prethru, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN);
266
267  if ( stream_vio_s_write(self->stream, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) != ROAR_CELT_MAGIC_LEN )
268   return -1;
269  self->opened_encoder = 1;
270 }
271
272 if ( (self->fo_rest + len) > fs2 ) {
273  if ( self->fo_rest ) {
274   memcpy(self->obuf, self->o_rest, self->fo_rest);
275   have = self->fo_rest;
276   self->fo_rest = 0;
277  }
278
279  memcpy(self->obuf+have, buf, (diff=fs2-have));
280  buf += diff;
281  len -= diff;
282
283  pkglen     = celt_encode(self->encoder, (celt_int16_t *) self->obuf, NULL, cbits+2, BS);
284  pkglen_net = ROAR_HOST2NET16(pkglen);
285  *(uint16_t*)cbits = pkglen_net;
286
287  if ( stream_vio_s_write(self->stream, cbits, pkglen+2) == -1 )
288   return -1;
289
290  while (len >= fs2) {
291   pkglen     = celt_encode(self->encoder, (celt_int16_t *) buf, NULL, cbits+2, BS);
292   pkglen_net = ROAR_HOST2NET16(pkglen);
293   *(uint16_t*)cbits = pkglen_net;
294
295   if ( stream_vio_s_write(self->stream, cbits, pkglen+2) == -1 )
296    return -1;
297   len -= fs2;
298   buf += fs2;
299  }
300 }
301
302 if ( len ) {
303  memcpy(self->o_rest + self->fo_rest, buf, len);
304  self->fo_rest += len;
305  len            = 0;
306 }
307
308 return org_len;
309}
310
311int cf_celt_delay(CODECFILTER_USERDATA_T   inst, uint_least32_t * delay) {
312 struct codecfilter_celt_inst * self = (struct codecfilter_celt_inst *) inst;
313
314 ROAR_DBG("cf_celt_delay(*) = ?");
315
316 if ( self == NULL ) {
317  *delay = (1000000 * 256) / ROAR_RATE_DEFAULT;
318  return 0;
319 } else {
320  *delay = (1000000 * self->frame_size) / ROAR_STREAM(self->stream)->info.rate;
321  ROAR_DBG("cf_celt_delay(*): frame_size=%i, rate=%i, *delay=%i",
322                  self->frame_size, ROAR_STREAM(self->stream)->info.rate, *delay);
323  return 0;
324 }
325
326 return -1;
327}
328
329#endif
330//ll
Note: See TracBrowser for help on using the repository browser.