source: roaraudio/libroardsp/transcode.c @ 2184:429492a8c3b9

Last change on this file since 2184:429492a8c3b9 was 2184:429492a8c3b9, checked in by phi, 15 years ago

added roar_bixcoder_packet_size()

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