source: roaraudio/libroardsp/transcode.c @ 2965:3e0e878ed13c

Last change on this file since 2965:3e0e878ed13c was 2923:8ea2211037fa, checked in by phi, 15 years ago

added stub of roar_xcoder_speex_proc_header()

File size: 11.5 KB
Line 
1//transcode.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
5 *
6 *  This file is part of libroardsp 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 *  libroardsp 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 "libroardsp.h"
26
27#define _FUNC(func)        (state->entry->func)
28#define _CHECK_STATE()     (!(state == NULL || state->entry == NULL))
29#define _CHECK_FUNC(func)  (_CHECK_STATE() && _FUNC(func) != NULL)
30#define _CHECK_BASIC(func) if ( !_CHECK_FUNC(func) ) return -1
31#define _CHECK()           if ( !_CHECK_STATE() ) return -1
32
33static struct roar_xcoder_entry g_xcoders[] = {
34 {ROAR_CODEC_ALAW,  roar_xcoder_dummy_inituninit, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_packet_size_any,
35                     roar_xcoder_alaw_encode,  roar_xcoder_alaw_decode, roar_xcoder_dummy_proc_header},
36 {ROAR_CODEC_MULAW, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_packet_size_any,
37                     roar_xcoder_mulaw_encode, roar_xcoder_mulaw_decode, roar_xcoder_dummy_proc_header},
38#ifdef ROAR_HAVE_LIBCELT
39 {ROAR_CODEC_ROAR_CELT, roar_xcoder_celt_init, roar_xcoder_celt_uninit, roar_xcoder_celt_packet_size,
40                     roar_xcoder_celt_encode,  roar_xcoder_celt_decode, NULL},
41#endif
42#ifdef ROAR_HAVE_LIBSPEEX
43 {ROAR_CODEC_ROAR_SPEEX, roar_xcoder_speex_init, roar_xcoder_speex_uninit, roar_xcoder_speex_packet_size,
44                     roar_xcoder_speex_encode, roar_xcoder_speex_decode, roar_xcoder_speex_proc_header},
45#endif
46 {-1, NULL, NULL, NULL, NULL, NULL, NULL}
47};
48
49int roar_xcoder_init(struct roar_xcoder * state, int encoder, struct roar_audio_info * info, struct roar_vio_calls * vio) {
50 int i;
51
52 if ( state == NULL || info == NULL )
53  return -1;
54
55 memset(state, 0, sizeof(struct roar_xcoder));
56
57 for (i = 0; g_xcoders[i].codec != -1; i++) {
58  if ( g_xcoders[i].codec == info->codec ) {
59   state->entry = &(g_xcoders[i]);
60   break;
61  }
62 }
63
64 if ( state->entry == NULL )
65  return -1;
66
67 state->stage      = ROAR_XCODER_STAGE_NONE;
68 state->encode     = encoder;
69 state->packet_len = -1;
70
71 if ( roar_xcoder_set_backend(state, vio) == -1 )
72  return -1;
73
74 memcpy(&(state->info.coded), info, sizeof(struct roar_audio_info));
75 memcpy(&(state->info.pcm  ), info, sizeof(struct roar_audio_info));
76
77 state->info.pcm.codec = ROAR_CODEC_DEFAULT;
78
79 if ( _FUNC(init) == NULL )
80  return -1;
81
82 if ( _FUNC(init)(state) != 0 )
83  return -1;
84
85 state->stage      = ROAR_XCODER_STAGE_INITED;
86
87 return 0;
88}
89
90int roar_xcoder_set_backend(struct roar_xcoder * state, struct roar_vio_calls * vio) {
91 _CHECK();
92
93 if ( vio == NULL && state->backend != NULL )
94  return -1;
95
96 state->backend = vio;
97
98 return 0;
99}
100int roar_xcoder_packet_size(struct roar_xcoder * state, int samples) {
101 _CHECK_BASIC(packet_size);
102
103 state->packet_len = state->entry->packet_size(state, samples);
104
105 if ( state->packet_len == 0 ) {
106  if ( samples < 1 ) {
107   return ROAR_RATE_DEFAULT/100;
108  } else {
109   return samples;
110  }
111 }
112
113 return state->packet_len;
114}
115
116int roar_xcoder_close      (struct roar_xcoder * state) {
117 _CHECK_BASIC(uninit);
118
119
120 if ( state->iobuffer != NULL ) {
121  roar_xcoder_proc(state, NULL, 0); // try to flush
122  roar_buffer_free(state->iobuffer);
123 }
124
125 return _FUNC(uninit)(state);
126}
127
128int roar_xcoder_proc_header(struct roar_xcoder * state) {
129 _CHECK_BASIC(proc_header);
130
131 return _FUNC(proc_header)(state);
132}
133
134int roar_xcoder_proc_packet(struct roar_xcoder * state, void * buf, size_t len) {
135 _CHECK();
136
137 ROAR_DBG("roar_xcoder_proc_packet(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
138
139 if ( state->backend == NULL )
140  return -1;
141
142 if ( state->packet_len > 0 && state->packet_len != len )
143  return -1;
144
145 ROAR_DBG("roar_xcoder_proc_packet(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
146
147 if ( state->encode ) {
148  _CHECK_BASIC(encode);
149  ROAR_DBG("roar_xcoder_proc_packet(state=%p, buf=%p, len=%lu) = ? // _CHECK_BASIC(encode) -> OK", state, buf, (unsigned long)len);
150  return _FUNC(encode)(state, buf, len);
151 } else {
152  _CHECK_BASIC(decode);
153  ROAR_DBG("roar_xcoder_proc_packet(state=%p, buf=%p, len=%lu) = ? // _CHECK_BASIC(decode) -> OK", state, buf, (unsigned long)len);
154  return _FUNC(decode)(state, buf, len);
155 }
156}
157
158int roar_xcoder_proc       (struct roar_xcoder * state, void * buf, size_t len) {
159 struct roar_buffer_stats ringstats;
160 struct roar_buffer * bufbuf;
161 void               * bufdata;
162 size_t               curlen;
163
164 ROAR_DBG("roar_xcoder_proc(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
165
166 if ( state == NULL )
167  return -1;
168
169 if ( buf == NULL && len != 0 )
170  return -1;
171
172 if ( state->packet_len == -1 )
173  if ( roar_xcoder_packet_size(state, -1) == -1 )
174   return -1;
175
176 ROAR_DBG("roar_xcoder_proc(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
177
178 if ( state->packet_len == 0 )
179  return roar_xcoder_proc_packet(state, buf, len);
180
181 ROAR_DBG("roar_xcoder_proc(state=%p, buf=%p, len=%lu): state->packet_len=%li", state, buf, (unsigned long)len, (long int) state->packet_len);
182
183 if ( state->iobuffer == NULL ) {
184  while ( len >= state->packet_len ) {
185   if ( roar_xcoder_proc_packet(state, buf, state->packet_len) == -1 ) {
186    ROAR_DBG("roar_xcoder_proc(state=%p, buf=%p, len=%lu) = -1 // roar_xcoder_proc_packet() error", state, buf, (unsigned long)len);
187    return -1;
188   }
189
190   buf += state->packet_len;
191   len -= state->packet_len;
192  }
193
194  if ( !len ) {
195   ROAR_DBG("roar_xcoder_proc(state=%p, buf=%p, len=%lu) = 0", state, buf, (unsigned long)len);
196   return 0;
197  }
198
199  ROAR_DBG("roar_xcoder_proc(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
200
201  if ( state->encode ) {
202   curlen = len;
203  } else {
204   curlen = state->packet_len;
205  }
206
207  if ( roar_buffer_new(&bufbuf, curlen) == -1 )
208   return -1;
209
210  if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) {
211   roar_buffer_free(bufbuf);
212   return -1;
213  }
214
215  if ( state->encode ) {
216   memcpy(bufdata, buf, len);
217  } else {
218   if ( roar_xcoder_proc_packet(state, bufdata, state->packet_len) == -1 ) {
219    roar_buffer_free(bufbuf);
220    return -1;
221   }
222
223   curlen = len;
224
225   if ( roar_buffer_shift_out(&bufbuf, buf, &curlen) == -1 ) {
226    roar_buffer_free(bufbuf);
227    return -1;
228   }
229
230   if ( curlen < len ) { // this should never happen!
231    roar_buffer_free(bufbuf);
232    return -1;
233   }
234  }
235
236  state->iobuffer = bufbuf;
237 } else {
238  if ( state->encode ) {
239   if ( roar_buffer_new(&bufbuf, len) == -1 )
240    return -1;
241
242   if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) {
243    roar_buffer_free(bufbuf);
244    return -1;
245   }
246
247   memcpy(bufdata, buf, len);
248
249   if ( roar_buffer_add(state->iobuffer, bufbuf) == -1 ) {
250    roar_buffer_free(bufbuf);
251    return -1;
252   }
253
254   if ( roar_buffer_ring_stats(state->iobuffer, &ringstats) == -1 )
255    return -1;
256
257   if ( roar_buffer_new(&bufbuf, state->packet_len) == -1 )
258    return -1;
259
260   if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) {
261    roar_buffer_free(bufbuf);
262    return -1;
263   }
264
265   while ( ringstats.bytes > state->packet_len ) {
266    curlen = state->packet_len;
267    if ( roar_buffer_shift_out(&(state->iobuffer), bufdata, &curlen) == -1 ) {
268     roar_buffer_free(bufbuf);
269     return -1;
270    }
271
272    if ( curlen < state->packet_len ) { // this should not happen...
273     roar_buffer_free(bufbuf);
274     return -1;
275    }
276
277    if ( roar_xcoder_proc_packet(state, bufdata, state->packet_len) == -1 ) {
278     roar_buffer_free(bufbuf);
279     return -1;
280    }
281
282    if ( roar_buffer_ring_stats(state->iobuffer, &ringstats) == -1 ) {
283     roar_buffer_free(bufbuf);
284     return -1;
285    }
286   }
287
288   if ( roar_buffer_free(bufbuf) == -1 )
289    return -1;
290  } else {
291   curlen = len;
292
293   if ( roar_buffer_shift_out(&(state->iobuffer), buf, &curlen) == -1 ) {
294    return -1;
295   }
296
297   if ( curlen == len )
298    return -1;
299
300   // we now have curlen < len and state->iobuffer == NULL
301   // as no data is left in the buffer, need to get some new data.
302   // we simply call ourself to get some more data...
303
304   if ( state->iobuffer == NULL ) {
305    ROAR_WARN("roar_xcoder_proc(state=%p, buf=%p, len=%lu): iobuffer != NULL, "
306                                "This is a bug in libroar{dsp,} or some hardware is broken",
307                   state, buf, (unsigned long)len);
308    return -1;
309   }
310
311   len -= curlen;
312   buf += curlen;
313
314   return roar_xcoder_proc(state, buf, len);
315  }
316 }
317
318 return 0;
319}
320
321int roar_bixcoder_init(struct roar_bixcoder * state, struct roar_audio_info * info, struct roar_vio_calls * vio) {
322 if ( state == NULL || info == NULL || vio == NULL )
323  return -1;
324
325 memset(state, 0, sizeof(struct roar_bixcoder));
326
327 if ( roar_xcoder_init(&(state->encoder), 1, info, vio) == -1 )
328  return -1;
329
330 if ( roar_xcoder_init(&(state->decoder), 0, info, vio) == -1 ) {
331  roar_xcoder_close(&(state->encoder));
332  return -1;
333 }
334
335 return 0;
336}
337
338int roar_bixcoder_packet_size (struct roar_bixcoder * state, int samples) {
339 int ret;
340
341 ROAR_DBG("roar_bixcoder_packet_size(state=%p, samples=%i) = ?", state, samples);
342
343 if ( state == NULL )
344  return -1;
345
346 if ( (ret = roar_xcoder_packet_size(&(state->encoder), samples)) == -1 )
347  return -1;
348
349 ROAR_DBG("roar_bixcoder_packet_size(state=%p, samples=%i): ret=%i", state, samples, ret);
350
351// TODO: we need a lot hope here...
352/*
353 if ( roar_xcoder_packet_size(&(state->decoder), ret) != ret )
354  return -1;
355*/
356
357 ROAR_DBG("roar_bixcoder_packet_size(state=%p, samples=%i) = %i", state, samples, ret);
358 return ret;
359}
360
361int roar_bixcoder_close       (struct roar_bixcoder * state) {
362 int ret = 0;
363
364 if ( state == NULL )
365  return -1;
366
367 ret = roar_xcoder_close(&(state->encoder));
368
369 if ( roar_xcoder_close(&(state->decoder)) == -1 )
370  return -1;
371
372 return ret;
373}
374
375int roar_bixcoder_read_header (struct roar_bixcoder * state) {
376 if ( state == NULL )
377  return -1;
378
379 return roar_xcoder_proc_header(&(state->decoder));
380}
381
382int roar_bixcoder_read_packet (struct roar_bixcoder * state, void * buf, size_t len) {
383
384 ROAR_DBG("roar_bixcoder_read_packet(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
385
386 if ( state == NULL )
387  return -1;
388
389 return roar_xcoder_proc_packet(&(state->decoder), buf, len);
390}
391
392int roar_bixcoder_read        (struct roar_bixcoder * state, void * buf, size_t len) {
393 if ( state == NULL )
394  return -1;
395
396 return roar_xcoder_proc(&(state->decoder), buf, len);
397}
398
399int roar_bixcoder_write_header(struct roar_bixcoder * state) {
400 if ( state == NULL )
401  return -1;
402
403 return roar_xcoder_proc_header(&(state->decoder));
404}
405
406int roar_bixcoder_write_packet(struct roar_bixcoder * state, void * buf, size_t len) {
407 if ( state == NULL )
408  return -1;
409
410 return roar_xcoder_proc_packet(&(state->encoder), buf, len);
411}
412
413int roar_bixcoder_write       (struct roar_bixcoder * state, void * buf, size_t len) {
414 if ( state == NULL )
415  return -1;
416
417 return roar_xcoder_proc(&(state->encoder), buf, len);
418}
419
420// dummy functions used by some de/encoders:
421int roar_xcoder_dummy_inituninit(struct roar_xcoder * state) {
422 return 0;
423}
424
425int roar_xcoder_dummy_packet_size_any(struct roar_xcoder * state, int samples) {
426 // the case samples=-1/samples!=-1 based things are done in the general func
427 return 0;
428}
429
430int roar_xcoder_dummy_proc_header(struct roar_xcoder * state) {
431 return 0;
432}
433
434//ll
Note: See TracBrowser for help on using the repository browser.