source: roaraudio/libroardsp/transcode.c @ 2637:38fc8e7a38c9

Last change on this file since 2637:38fc8e7a38c9 was 2637:38fc8e7a38c9, checked in by phi, 15 years ago

some code used to read/write non block aligned data from/to xcoder

File size: 9.9 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},
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},
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},
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},
45#endif
46 {-1, 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_packet(struct roar_xcoder * state, void * buf, size_t len) {
129 _CHECK();
130
131 ROAR_DBG("roar_xcoder_proc_packet(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
132
133 if ( state->backend == NULL )
134  return -1;
135
136 if ( state->packet_len > 0 && state->packet_len != len )
137  return -1;
138
139 if ( state->encode ) {
140  _CHECK_BASIC(encode);
141  return _FUNC(encode)(state, buf, len);
142 } else {
143  _CHECK_BASIC(decode);
144  return _FUNC(decode)(state, buf, len);
145 }
146}
147
148int roar_xcoder_proc       (struct roar_xcoder * state, void * buf, size_t len) {
149 struct roar_buffer_stats ringstats;
150 struct roar_buffer * bufbuf;
151 void               * bufdata;
152 size_t               curlen;
153
154 if ( state == NULL )
155  return -1;
156
157 if ( buf == NULL && len != 0 )
158  return -1;
159
160 if ( state->packet_len == -1 )
161  if ( roar_xcoder_packet_size(state, -1) == -1 )
162   return -1;
163
164 if ( state->packet_len == 0 )
165  return roar_xcoder_proc_packet(state, buf, len);
166
167 if ( state->iobuffer == NULL ) {
168  while ( len >= state->packet_len ) {
169   if ( roar_xcoder_proc_packet(state, buf, state->packet_len) == -1 )
170    return -1;
171
172   buf += state->packet_len;
173   len -= state->packet_len;
174  }
175
176  if ( !len )
177   return 0;
178
179  if ( state->encode ) {
180   curlen = len;
181  } else {
182   curlen = state->packet_len;
183  }
184
185  if ( roar_buffer_new(&bufbuf, curlen) == -1 )
186   return -1;
187
188  if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) {
189   roar_buffer_free(bufbuf);
190   return -1;
191  }
192
193  if ( state->encode ) {
194   memcpy(bufdata, buf, len);
195  } else {
196   if ( roar_xcoder_proc_packet(state, bufdata, state->packet_len) == -1 ) {
197    roar_buffer_free(bufbuf);
198    return -1;
199   }
200
201   curlen = len;
202
203   if ( roar_buffer_shift_out(&bufbuf, buf, &curlen) == -1 ) {
204    roar_buffer_free(bufbuf);
205    return -1;
206   }
207
208   if ( curlen < len ) { // this should never happen!
209    roar_buffer_free(bufbuf);
210    return -1;
211   }
212  }
213
214  state->iobuffer = bufbuf;
215 } else {
216  if ( state->encode ) {
217   if ( roar_buffer_new(&bufbuf, len) == -1 )
218    return -1;
219
220   if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) {
221    roar_buffer_free(bufbuf);
222    return -1;
223   }
224
225   memcpy(bufdata, buf, len);
226
227   if ( roar_buffer_add(state->iobuffer, bufbuf) == -1 ) {
228    roar_buffer_free(bufbuf);
229    return -1;
230   }
231
232   if ( roar_buffer_ring_stats(state->iobuffer, &ringstats) == -1 )
233    return -1;
234
235   if ( roar_buffer_new(&bufbuf, state->packet_len) == -1 )
236    return -1;
237
238   if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) {
239    roar_buffer_free(bufbuf);
240    return -1;
241   }
242
243   while ( ringstats.bytes > state->packet_len ) {
244    curlen = state->packet_len;
245    if ( roar_buffer_shift_out(&(state->iobuffer), bufdata, &curlen) == -1 ) {
246     roar_buffer_free(bufbuf);
247     return -1;
248    }
249
250    if ( curlen < state->packet_len ) { // this should not happen...
251     roar_buffer_free(bufbuf);
252     return -1;
253    }
254
255    if ( roar_xcoder_proc_packet(state, bufdata, state->packet_len) == -1 ) {
256     roar_buffer_free(bufbuf);
257     return -1;
258    }
259
260    if ( roar_buffer_ring_stats(state->iobuffer, &ringstats) == -1 ) {
261     roar_buffer_free(bufbuf);
262     return -1;
263    }
264   }
265
266   if ( roar_buffer_free(bufbuf) == -1 )
267    return -1;
268  } else {
269   curlen = len;
270
271   if ( roar_buffer_shift_out(&(state->iobuffer), buf, &curlen) == -1 ) {
272    return -1;
273   }
274
275   if ( curlen == len )
276    return -1;
277
278   // we now have curlen < len and state->iobuffer == NULL
279   // as no data is left in the buffer, need to get some new data.
280   // we simply call ourself to get some more data...
281
282   if ( state->iobuffer == NULL ) {
283    ROAR_WARN("roar_xcoder_proc(state=%p, buf=%p, len=%lu): iobuffer != NULL, "
284                                "This is a bug in libroar{dsp,} or some hardware is broken",
285                   state, buf, (unsigned long)len);
286    return -1;
287   }
288
289   len -= curlen;
290   buf += curlen;
291
292   return roar_xcoder_proc(state, buf, len);
293  }
294 }
295
296 return 0;
297}
298
299int roar_bixcoder_init(struct roar_bixcoder * state, struct roar_audio_info * info, struct roar_vio_calls * vio) {
300 if ( state == NULL || info == NULL || vio == NULL )
301  return -1;
302
303 memset(state, 0, sizeof(struct roar_bixcoder));
304
305 if ( roar_xcoder_init(&(state->encoder), 1, info, vio) == -1 )
306  return -1;
307
308 if ( roar_xcoder_init(&(state->decoder), 0, info, vio) == -1 ) {
309  roar_xcoder_close(&(state->encoder));
310  return -1;
311 }
312
313 return 0;
314}
315
316int roar_bixcoder_packet_size (struct roar_bixcoder * state, int samples) {
317 int ret;
318
319 ROAR_DBG("roar_bixcoder_packet_size(state=%p, samples=%i) = ?", state, samples);
320
321 if ( state == NULL )
322  return -1;
323
324 if ( (ret = roar_xcoder_packet_size(&(state->encoder), samples)) == -1 )
325  return -1;
326
327 ROAR_DBG("roar_bixcoder_packet_size(state=%p, samples=%i): ret=%i", state, samples, ret);
328
329// TODO: we need a lot hope here...
330/*
331 if ( roar_xcoder_packet_size(&(state->decoder), ret) != ret )
332  return -1;
333*/
334
335 ROAR_DBG("roar_bixcoder_packet_size(state=%p, samples=%i) = %i", state, samples, ret);
336 return ret;
337}
338
339int roar_bixcoder_close       (struct roar_bixcoder * state) {
340 int ret = 0;
341
342 if ( state == NULL )
343  return -1;
344
345 ret = roar_xcoder_close(&(state->encoder));
346
347 if ( roar_xcoder_close(&(state->decoder)) == -1 )
348  return -1;
349
350 return ret;
351}
352
353int roar_bixcoder_read_packet (struct roar_bixcoder * state, void * buf, size_t len) {
354
355 ROAR_DBG("roar_bixcoder_read_packet(state=%p, buf=%p, len=%lu) = ?", state, buf, (unsigned long)len);
356
357 if ( state == NULL )
358  return -1;
359
360 return roar_xcoder_proc_packet(&(state->decoder), buf, len);
361}
362
363int roar_bixcoder_read        (struct roar_bixcoder * state, void * buf, size_t len) {
364 if ( state == NULL )
365  return -1;
366
367 return roar_xcoder_proc(&(state->decoder), buf, len);
368}
369
370int roar_bixcoder_write_packet(struct roar_bixcoder * state, void * buf, size_t len) {
371 if ( state == NULL )
372  return -1;
373
374 return roar_xcoder_proc_packet(&(state->encoder), buf, len);
375}
376
377int roar_bixcoder_write       (struct roar_bixcoder * state, void * buf, size_t len) {
378 if ( state == NULL )
379  return -1;
380
381 return roar_xcoder_proc(&(state->encoder), buf, len);
382}
383
384// dummy functions used by some de/encoders:
385int roar_xcoder_dummy_inituninit(struct roar_xcoder * state) {
386 return 0;
387}
388
389int roar_xcoder_dummy_packet_size_any(struct roar_xcoder * state, int samples) {
390 // the case samples=-1/samples!=-1 based things are done in the general func
391 return 0;
392}
393
394//ll
Note: See TracBrowser for help on using the repository browser.