source: roaraudio/libroardsp/transcode.c @ 2204:ed8a8343e658

Last change on this file since 2204:ed8a8343e658 was 2189:729a72d2fb09, checked in by phi, 15 years ago

addedlinks to the CELT functions

File size: 5.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 {ROAR_CODEC_ROAR_CELT, roar_xcoder_celt_init, roar_xcoder_celt_uninit, roar_xcoder_celt_packet_size,
39                     roar_xcoder_celt_encode,  roar_xcoder_celt_decode},
40 {-1, NULL, NULL, NULL, NULL, NULL}
41};
42
43int roar_xcoder_init(struct roar_xcoder * state, int encoder, struct roar_audio_info * info, struct roar_vio_calls * vio) {
44 int i;
45
46 if ( state == NULL || info == NULL )
47  return -1;
48
49 memset(state, 0, sizeof(struct roar_xcoder));
50
51 for (i = 0; g_xcoders[i].codec != -1; i++) {
52  if ( g_xcoders[i].codec == info->codec ) {
53   state->entry = &(g_xcoders[i]);
54   break;
55  }
56 }
57
58 if ( state->entry == NULL )
59  return -1;
60
61 state->stage      = ROAR_XCODER_STAGE_NONE;
62 state->encode     = encoder;
63 state->packet_len = -1;
64
65 if ( roar_xcoder_set_backend(state, vio) == -1 )
66  return -1;
67
68 memcpy(&(state->info.coded), info, sizeof(struct roar_audio_info));
69 memcpy(&(state->info.pcm  ), info, sizeof(struct roar_audio_info));
70
71 state->info.pcm.codec = ROAR_CODEC_DEFAULT;
72
73 if ( _FUNC(init) == NULL )
74  return -1;
75
76 if ( _FUNC(init)(state) != 0 )
77  return -1;
78
79 state->stage      = ROAR_XCODER_STAGE_INITED;
80
81 return 0;
82}
83
84int roar_xcoder_set_backend(struct roar_xcoder * state, struct roar_vio_calls * vio) {
85 _CHECK();
86
87 if ( vio == NULL && state->backend != NULL )
88  return -1;
89
90 state->backend = vio;
91
92 return 0;
93}
94int roar_xcoder_packet_size(struct roar_xcoder * state, int samples) {
95 _CHECK_BASIC(packet_size);
96
97 state->packet_len = state->entry->packet_size(state, samples);
98
99 if ( state->packet_len == 0 ) {
100  if ( samples < 1 ) {
101   return ROAR_RATE_DEFAULT/100;
102  } else {
103   return samples;
104  }
105 }
106
107 return state->packet_len;
108}
109
110int roar_xcoder_close      (struct roar_xcoder * state) {
111 _CHECK_BASIC(uninit);
112
113
114 if ( state->iobuffer != NULL ) {
115  roar_xcoder_proc(state, NULL, 0); // try to flush
116  roar_buffer_free(state->iobuffer);
117 }
118
119 return _FUNC(uninit)(state);
120}
121
122int roar_xcoder_proc_packet(struct roar_xcoder * state, void * buf, size_t len) {
123 _CHECK();
124
125 if ( state->backend == NULL )
126  return -1;
127
128 if ( state->packet_len > 0 && state->packet_len != len )
129  return -1;
130
131 if ( state->encode ) {
132  _CHECK_BASIC(encode);
133  return _FUNC(encode)(state, buf, len);
134 } else {
135  _CHECK_BASIC(decode);
136  return _FUNC(decode)(state, buf, len);
137 }
138}
139
140int roar_xcoder_proc       (struct roar_xcoder * state, void * buf, size_t len) {
141 return -1;
142}
143
144int roar_bixcoder_init(struct roar_bixcoder * state, struct roar_audio_info * info, struct roar_vio_calls * vio) {
145 if ( state == NULL || info == NULL || vio == NULL )
146  return -1;
147
148 memset(state, 0, sizeof(struct roar_bixcoder));
149
150 if ( roar_xcoder_init(&(state->encoder), 1, info, vio) == -1 )
151  return -1;
152
153 if ( roar_xcoder_init(&(state->decoder), 0, info, vio) == -1 ) {
154  roar_xcoder_close(&(state->encoder));
155  return -1;
156 }
157
158 return 0;
159}
160
161int roar_bixcoder_packet_size (struct roar_bixcoder * state, int samples) {
162 int ret;
163
164 if ( state == NULL )
165  return -1;
166
167 if ( (ret = roar_xcoder_packet_size(&(state->encoder), samples)) == -1 )
168  return -1;
169
170 if ( roar_xcoder_packet_size(&(state->decoder), ret) != ret )
171  return -1;
172
173 return ret;
174}
175
176int roar_bixcoder_close       (struct roar_bixcoder * state) {
177 int ret = 0;
178
179 if ( state == NULL )
180  return -1;
181
182 ret = roar_xcoder_close(&(state->encoder));
183
184 if ( roar_xcoder_close(&(state->decoder)) == -1 )
185  return -1;
186
187 return ret;
188}
189
190int roar_bixcoder_read_packet (struct roar_bixcoder * state, void * buf, size_t len) {
191 if ( state == NULL )
192  return -1;
193
194 return roar_xcoder_proc_packet(&(state->decoder), buf, len);
195}
196
197int roar_bixcoder_read        (struct roar_bixcoder * state, void * buf, size_t len) {
198 if ( state == NULL )
199  return -1;
200
201 return roar_xcoder_proc(&(state->decoder), buf, len);
202}
203
204int roar_bixcoder_write_packet(struct roar_bixcoder * state, void * buf, size_t len) {
205 if ( state == NULL )
206  return -1;
207
208 return roar_xcoder_proc_packet(&(state->encoder), buf, len);
209}
210
211int roar_bixcoder_write       (struct roar_bixcoder * state, void * buf, size_t len) {
212 if ( state == NULL )
213  return -1;
214
215 return roar_xcoder_proc(&(state->encoder), buf, len);
216}
217
218// dummy functions used by some de/encoders:
219int roar_xcoder_dummy_inituninit(struct roar_xcoder * state) {
220 return 0;
221}
222
223int roar_xcoder_dummy_packet_size_any(struct roar_xcoder * state, int samples) {
224 // the case samples=-1/samples!=-1 based things are done in the general func
225 return 0;
226}
227
228//ll
Note: See TracBrowser for help on using the repository browser.