source: roaraudio/roard/codecfilter_celt.c @ 2120:7f67d164a05d

Last change on this file since 2120:7f67d164a05d was 2120:7f67d164a05d, checked in by phi, 15 years ago

added support to CELT cf for BIDIR streams

File size: 7.6 KB
Line 
1//codecfilter_celt.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "roard.h"
26#ifdef ROAR_HAVE_LIBCELT
27
28int cf_celt_open(CODECFILTER_USERDATA_T * inst, int codec,
29                                            struct roar_stream_server * info,
30                                            struct roar_codecfilter   * filter) {
31 struct codecfilter_celt_inst * self = malloc(sizeof(struct codecfilter_celt_inst));
32 struct roar_stream * s = ROAR_STREAM(info);
33
34 if ( !self )
35  return -1;
36
37/*
38 CELTMode * mode;
39 CELTEncoder * encoder;
40 CELTDecoder * decoder;
41 int frame_size;
42 int lookahead;
43 int out_size;
44 char * ibuf;
45 char * obuf;
46 char * rest;
47 int s_buf;
48 int f_rest; /-* how much is in rest? *-/
49*/
50
51 self->stream               = info;
52 self->frame_size           = 256;
53 self->lookahead            = self->frame_size;
54 self->encoder              = NULL;
55 self->decoder              = NULL;
56 self->opened_encoder       = 0;
57 self->opened_decoder       = 0;
58 self->s_buf                = s->info.channels * self->frame_size * 2;
59 self->ibuf                 = malloc(self->s_buf);
60 self->obuf                 = malloc(self->s_buf);
61 self->i_rest               = malloc(self->s_buf);
62 self->o_rest               = malloc(self->s_buf);
63 self->fi_rest              = 0;
64 self->fo_rest              = 0;
65
66 if ( !(self->ibuf && self->obuf && self->i_rest && self->o_rest) ) {
67  if ( self->ibuf )
68   free(self->ibuf);
69
70  if ( self->obuf )
71   free(self->obuf);
72
73  if ( self->i_rest )
74   free(self->o_rest);
75
76  if ( self->o_rest )
77   free(self->o_rest);
78
79  free(self);
80  return -1;
81 }
82 
83 self->mode                 = celt_mode_create(s->info.rate, s->info.channels, self->frame_size, NULL);
84
85 if ( !self->mode ) {
86  free(self);
87  return -1;
88 }
89
90 if ( s->dir == ROAR_DIR_PLAY ) {
91   self->decoder = celt_decoder_create(self->mode);
92 } else if ( s->dir == ROAR_DIR_MONITOR || s->dir == ROAR_DIR_OUTPUT ) {
93   self->encoder = celt_encoder_create(self->mode);
94 } else if ( s->dir == ROAR_DIR_BIDIR ) {
95   self->decoder = celt_decoder_create(self->mode);
96   self->encoder = celt_encoder_create(self->mode);
97 } else {
98  celt_mode_destroy(self->mode);
99  free(self);
100  return -1;
101 }
102
103 *inst = (CODECFILTER_USERDATA_T) self;
104
105 s->info.codec = ROAR_CODEC_DEFAULT;
106 s->info.bits  = 16; // CELT hardcoded
107
108 return 0;
109}
110
111int cf_celt_close(CODECFILTER_USERDATA_T   inst) {
112 struct codecfilter_celt_inst * self = (struct codecfilter_celt_inst *) inst;
113
114 if ( !inst )
115  return -1;
116
117 if ( self->encoder )
118 celt_encoder_destroy(self->encoder);
119
120 if ( self->decoder )
121 celt_decoder_destroy(self->decoder);
122
123 if ( self->mode )
124  celt_mode_destroy(self->mode);
125
126 if ( self->ibuf )
127  free(self->ibuf);
128
129 if ( self->obuf )
130  free(self->obuf);
131
132 if ( self->i_rest )
133  free(self->i_rest);
134
135 if ( self->o_rest )
136  free(self->o_rest);
137
138 free(inst);
139 return 0;
140}
141
142int cf_celt_read(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
143 struct codecfilter_celt_inst * self = (struct codecfilter_celt_inst *) inst;
144 int r = 0;
145 uint16_t fs;
146 char * cbuf;
147 char magic[ROAR_CELT_MAGIC_LEN];
148
149// printf("buf=%p, len=%i\n", buf, len);
150
151 if ( !self->opened_decoder ) {
152  errno = ENOSYS;
153  if ( stream_vio_s_read(self->stream, magic, ROAR_CELT_MAGIC_LEN) != ROAR_CELT_MAGIC_LEN )
154   return -1;
155  if ( memcmp(magic, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) != 0 )
156   return -1;
157
158  errno = 0;
159  self->opened_decoder = 1;
160 }
161
162 if ( self->fi_rest ) {
163  memcpy(buf, self->i_rest, self->fi_rest);
164  r += self->fi_rest;
165  self->fi_rest = 0;
166 }
167
168 while ( r <= (len - self->s_buf) ) {
169  if ( stream_vio_s_read(self->stream, &fs, 2) != 2 )
170   break;
171
172  fs = ROAR_NET2HOST16(fs);
173
174  if ( fs > self->s_buf )
175   return -1;
176
177  if ( stream_vio_s_read(self->stream, self->ibuf, fs) != fs )
178   return -1;
179
180  cbuf = buf + r;
181
182//  printf("buf=%p, r=%i // cbuf=%p\n", buf, r, cbuf);
183  if ( celt_decode(self->decoder, (unsigned char *) self->ibuf, fs, (celt_int16_t *) cbuf) < 0 )
184   return -1;
185
186  r += self->s_buf;
187 }
188
189 if ( r < len ) {
190//  printf("r < len!\n");
191  if ( stream_vio_s_read(self->stream, &fs, 2) == 2 ) {
192   fs = ROAR_NET2HOST16(fs);
193//   printf("next: fs=%i\n", fs);
194   if ( fs > self->s_buf )
195    return -1;
196   if ( stream_vio_s_read(self->stream, self->ibuf, fs) == fs ) {
197//    printf("got data!\n");
198    if ( celt_decode(self->decoder, (unsigned char *) self->ibuf, fs, (celt_int16_t *) self->obuf) >= 0 ) {
199//     printf("{ // decode rest\n");
200//     printf(" r=%i // need %i Bytes\n", r, len - r);
201//     printf(" memcpy(buf+%i, self->obuf, %i) = ?\n", r, len - r);
202     memcpy(buf+r, self->obuf, len - r);
203     self->fi_rest = self->s_buf + r - len;
204     memcpy(self->i_rest, self->obuf + len - r, self->fi_rest);
205//     printf(" len=%i, r=%i, fi_rest=%i, s_buf=%i\n", len, r, self->fi_rest, self->s_buf);
206     r = len;
207//     printf("}\n");
208    }
209   }
210  }
211 }
212
213 ROAR_DBG("cf_celt_read(inst=%p, buf=%p, len=%i) = %i", inst, buf, len, r);
214 return r;
215}
216
217#define BS (ROAR_STREAM(self->stream)->info.channels * 64)
218int cf_celt_write(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
219 struct codecfilter_celt_inst * self = (struct codecfilter_celt_inst *) inst;
220 int have = 0;
221 int org_len = len;
222 int diff;
223 int fs2 = self->frame_size * 2 * ROAR_STREAM(self->stream)->info.channels;
224 uint16_t pkglen_net, pkglen;
225 unsigned char cbits[BS+2];
226
227 if ( !self->opened_encoder ) {
228  if ( stream_vio_s_write(self->stream, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) != ROAR_CELT_MAGIC_LEN )
229   return -1;
230  self->opened_encoder = 1;
231 }
232
233 if ( (self->fo_rest + len) > fs2 ) {
234  if ( self->fo_rest ) {
235   memcpy(self->obuf, self->o_rest, self->fo_rest);
236   have = self->fo_rest;
237   self->fo_rest = 0;
238  }
239
240  memcpy(self->obuf+have, buf, (diff=fs2-have));
241  buf += diff;
242  len -= diff;
243
244  pkglen     = celt_encode(self->encoder, (celt_int16_t *) self->obuf, NULL, cbits+2, BS);
245  pkglen_net = ROAR_HOST2NET16(pkglen);
246  *(uint16_t*)cbits = pkglen_net;
247
248  if ( stream_vio_s_write(self->stream, cbits, pkglen+2) == -1 )
249   return -1;
250
251  while (len >= fs2) {
252   pkglen     = celt_encode(self->encoder, (celt_int16_t *) buf, NULL, cbits+2, BS);
253   pkglen_net = ROAR_HOST2NET16(pkglen);
254   *(uint16_t*)cbits = pkglen_net;
255
256   if ( stream_vio_s_write(self->stream, cbits, pkglen+2) == -1 )
257    return -1;
258   len -= fs2;
259   buf += fs2;
260  }
261 }
262
263 if ( len ) {
264  memcpy(self->o_rest + self->fo_rest, buf, len);
265  self->fo_rest += len;
266  len            = 0;
267 }
268
269 return org_len;
270}
271
272int cf_celt_delay(CODECFILTER_USERDATA_T   inst, uint_least32_t * delay) {
273 struct codecfilter_celt_inst * self = (struct codecfilter_celt_inst *) inst;
274
275 ROAR_DBG("cf_celt_delay(*) = ?");
276
277 if ( self == NULL ) {
278  *delay = (1000000 * 256) / ROAR_RATE_DEFAULT;
279  return 0;
280 } else {
281  *delay = (1000000 * self->frame_size) / ROAR_STREAM(self->stream)->info.rate;
282  ROAR_DBG("cf_celt_delay(*): frame_size=%i, rate=%i, *delay=%i",
283                  self->frame_size, ROAR_STREAM(self->stream)->info.rate, *delay);
284  return 0;
285 }
286
287 return -1;
288}
289
290#endif
291//ll
Note: See TracBrowser for help on using the repository browser.