source: roaraudio/libroardsp/transcode.c @ 5300:190af1adf91c

Last change on this file since 5300:190af1adf91c was 5300:190af1adf91c, checked in by phi, 12 years ago

first commit to move away from old roar_buffer_add() to roar_buffer_moveinto() (See: #126)

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