source: roaraudio/libroardsp/transcode.c @ 2178:397f705a9564

Last change on this file since 2178:397f705a9564 was 2178:397f705a9564, checked in by phi, 15 years ago

added roar_bixcoder_*() code

File size: 5.4 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                     NULL, NULL},
36 {ROAR_CODEC_MULAW, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_packet_size_any,
37                     NULL, NULL},
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->packet_len > 0 && state->packet_len != len )
124  return -1;
125
126 if ( state->encode ) {
127  _CHECK_BASIC(encode);
128  return _FUNC(encode)(state, buf, len);
129 } else {
130  _CHECK_BASIC(decode);
131  return _FUNC(decode)(state, buf, len);
132 }
133}
134
135int roar_xcoder_proc       (struct roar_xcoder * state, void * buf, size_t len) {
136 return -1;
137}
138
139int roar_bixcoder_init(struct roar_bixcoder * state, struct roar_audio_info * info, struct roar_vio_calls * vio) {
140 if ( state == NULL || info == NULL || vio == NULL )
141  return -1;
142
143 memset(state, 0, sizeof(struct roar_bixcoder));
144
145 if ( roar_xcoder_init(&(state->encoder), 1, info, vio) == -1 )
146  return -1;
147
148 if ( roar_xcoder_init(&(state->decoder), 0, info, vio) == -1 ) {
149  roar_xcoder_close(&(state->encoder));
150  return -1;
151 }
152
153 return 0;
154}
155
156int roar_bixcoder_packet_size (struct roar_bixcoder * state, int samples);
157int roar_bixcoder_close       (struct roar_bixcoder * state) {
158 int ret = 0;
159
160 if ( state == NULL )
161  return -1;
162
163 ret = roar_xcoder_close(&(state->encoder));
164
165 if ( roar_xcoder_close(&(state->decoder)) == -1 )
166  return -1;
167
168 return ret;
169}
170
171int roar_bixcoder_read_packet (struct roar_bixcoder * state, void * buf, size_t len) {
172 if ( state == NULL )
173  return -1;
174
175 return roar_xcoder_proc_packet(&(state->decoder), buf, len);
176}
177
178int roar_bixcoder_read        (struct roar_bixcoder * state, void * buf, size_t len) {
179 if ( state == NULL )
180  return -1;
181
182 return roar_xcoder_proc(&(state->decoder), buf, len);
183}
184
185int roar_bixcoder_write_packet(struct roar_bixcoder * state, void * buf, size_t len) {
186 if ( state == NULL )
187  return -1;
188
189 return roar_xcoder_proc_packet(&(state->encoder), buf, len);
190}
191
192int roar_bixcoder_write       (struct roar_bixcoder * state, void * buf, size_t len) {
193 if ( state == NULL )
194  return -1;
195
196 return roar_xcoder_proc(&(state->encoder), buf, len);
197}
198
199// dummy functions used by some de/encoders:
200int roar_xcoder_dummy_inituninit(struct roar_xcoder * state) {
201 return 0;
202}
203
204int roar_xcoder_dummy_packet_size_any(struct roar_xcoder * state, int samples) {
205 // the case samples=-1/samples!=-1 based things are done in the general func
206 return 0;
207}
208
209//ll
Note: See TracBrowser for help on using the repository browser.