source: roaraudio/roard/codecfilter_celt.c @ 1144:a17ed9fd2af0

Last change on this file since 1144:a17ed9fd2af0 was 1144:a17ed9fd2af0, checked in by phi, 15 years ago

added support to some codec filters to calc the codec delay

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