[2168] | 1 | //transcode.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
| 5 | * |
---|
[2169] | 6 | * This file is part of libroardsp a part of RoarAudio, |
---|
[2168] | 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 | * |
---|
[2169] | 14 | * libroardsp is distributed in the hope that it will be useful, |
---|
[2168] | 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 | |
---|
[2169] | 25 | #include "libroardsp.h" |
---|
[2168] | 26 | |
---|
[2182] | 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) |
---|
[2174] | 30 | #define _CHECK_BASIC(func) if ( !_CHECK_FUNC(func) ) return -1 |
---|
[2182] | 31 | #define _CHECK() if ( !_CHECK_STATE() ) return -1 |
---|
[2174] | 32 | |
---|
| 33 | static struct roar_xcoder_entry g_xcoders[] = { |
---|
| 34 | {ROAR_CODEC_ALAW, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_packet_size_any, |
---|
[2183] | 35 | roar_xcoder_alaw_encode, roar_xcoder_alaw_decode}, |
---|
[2174] | 36 | {ROAR_CODEC_MULAW, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_packet_size_any, |
---|
[2183] | 37 | roar_xcoder_mulaw_encode, roar_xcoder_mulaw_decode}, |
---|
[2214] | 38 | #ifdef ROAR_HAVE_LIBCELT |
---|
[2189] | 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}, |
---|
[2214] | 41 | #endif |
---|
[2174] | 42 | {-1, NULL, NULL, NULL, NULL, NULL} |
---|
| 43 | }; |
---|
| 44 | |
---|
[2176] | 45 | int roar_xcoder_init(struct roar_xcoder * state, int encoder, struct roar_audio_info * info, struct roar_vio_calls * vio) { |
---|
| 46 | int i; |
---|
| 47 | |
---|
| 48 | if ( state == NULL || info == NULL ) |
---|
| 49 | return -1; |
---|
| 50 | |
---|
| 51 | memset(state, 0, sizeof(struct roar_xcoder)); |
---|
| 52 | |
---|
| 53 | for (i = 0; g_xcoders[i].codec != -1; i++) { |
---|
| 54 | if ( g_xcoders[i].codec == info->codec ) { |
---|
| 55 | state->entry = &(g_xcoders[i]); |
---|
| 56 | break; |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | if ( state->entry == NULL ) |
---|
| 61 | return -1; |
---|
| 62 | |
---|
| 63 | state->stage = ROAR_XCODER_STAGE_NONE; |
---|
| 64 | state->encode = encoder; |
---|
| 65 | state->packet_len = -1; |
---|
| 66 | |
---|
| 67 | if ( roar_xcoder_set_backend(state, vio) == -1 ) |
---|
| 68 | return -1; |
---|
| 69 | |
---|
| 70 | memcpy(&(state->info.coded), info, sizeof(struct roar_audio_info)); |
---|
| 71 | memcpy(&(state->info.pcm ), info, sizeof(struct roar_audio_info)); |
---|
| 72 | |
---|
| 73 | state->info.pcm.codec = ROAR_CODEC_DEFAULT; |
---|
| 74 | |
---|
| 75 | if ( _FUNC(init) == NULL ) |
---|
| 76 | return -1; |
---|
| 77 | |
---|
| 78 | if ( _FUNC(init)(state) != 0 ) |
---|
| 79 | return -1; |
---|
| 80 | |
---|
| 81 | state->stage = ROAR_XCODER_STAGE_INITED; |
---|
| 82 | |
---|
| 83 | return 0; |
---|
| 84 | } |
---|
| 85 | |
---|
[2174] | 86 | int roar_xcoder_set_backend(struct roar_xcoder * state, struct roar_vio_calls * vio) { |
---|
| 87 | _CHECK(); |
---|
| 88 | |
---|
| 89 | if ( vio == NULL && state->backend != NULL ) |
---|
| 90 | return -1; |
---|
| 91 | |
---|
| 92 | state->backend = vio; |
---|
| 93 | |
---|
| 94 | return 0; |
---|
| 95 | } |
---|
| 96 | int roar_xcoder_packet_size(struct roar_xcoder * state, int samples) { |
---|
| 97 | _CHECK_BASIC(packet_size); |
---|
| 98 | |
---|
| 99 | state->packet_len = state->entry->packet_size(state, samples); |
---|
| 100 | |
---|
| 101 | if ( state->packet_len == 0 ) { |
---|
| 102 | if ( samples < 1 ) { |
---|
| 103 | return ROAR_RATE_DEFAULT/100; |
---|
| 104 | } else { |
---|
| 105 | return samples; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | return state->packet_len; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | int roar_xcoder_close (struct roar_xcoder * state) { |
---|
| 113 | _CHECK_BASIC(uninit); |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | if ( state->iobuffer != NULL ) { |
---|
| 117 | roar_xcoder_proc(state, NULL, 0); // try to flush |
---|
| 118 | roar_buffer_free(state->iobuffer); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | return _FUNC(uninit)(state); |
---|
| 122 | } |
---|
| 123 | |
---|
[2176] | 124 | int roar_xcoder_proc_packet(struct roar_xcoder * state, void * buf, size_t len) { |
---|
| 125 | _CHECK(); |
---|
| 126 | |
---|
[2180] | 127 | if ( state->backend == NULL ) |
---|
| 128 | return -1; |
---|
| 129 | |
---|
[2177] | 130 | if ( state->packet_len > 0 && state->packet_len != len ) |
---|
| 131 | return -1; |
---|
| 132 | |
---|
[2176] | 133 | if ( state->encode ) { |
---|
| 134 | _CHECK_BASIC(encode); |
---|
| 135 | return _FUNC(encode)(state, buf, len); |
---|
| 136 | } else { |
---|
| 137 | _CHECK_BASIC(decode); |
---|
| 138 | return _FUNC(decode)(state, buf, len); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
[2174] | 142 | int roar_xcoder_proc (struct roar_xcoder * state, void * buf, size_t len) { |
---|
| 143 | return -1; |
---|
| 144 | } |
---|
| 145 | |
---|
[2178] | 146 | int roar_bixcoder_init(struct roar_bixcoder * state, struct roar_audio_info * info, struct roar_vio_calls * vio) { |
---|
| 147 | if ( state == NULL || info == NULL || vio == NULL ) |
---|
| 148 | return -1; |
---|
| 149 | |
---|
| 150 | memset(state, 0, sizeof(struct roar_bixcoder)); |
---|
| 151 | |
---|
| 152 | if ( roar_xcoder_init(&(state->encoder), 1, info, vio) == -1 ) |
---|
| 153 | return -1; |
---|
| 154 | |
---|
| 155 | if ( roar_xcoder_init(&(state->decoder), 0, info, vio) == -1 ) { |
---|
| 156 | roar_xcoder_close(&(state->encoder)); |
---|
| 157 | return -1; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | return 0; |
---|
| 161 | } |
---|
| 162 | |
---|
[2184] | 163 | int roar_bixcoder_packet_size (struct roar_bixcoder * state, int samples) { |
---|
| 164 | int ret; |
---|
| 165 | |
---|
| 166 | if ( state == NULL ) |
---|
| 167 | return -1; |
---|
| 168 | |
---|
| 169 | if ( (ret = roar_xcoder_packet_size(&(state->encoder), samples)) == -1 ) |
---|
| 170 | return -1; |
---|
| 171 | |
---|
| 172 | if ( roar_xcoder_packet_size(&(state->decoder), ret) != ret ) |
---|
| 173 | return -1; |
---|
| 174 | |
---|
| 175 | return ret; |
---|
| 176 | } |
---|
| 177 | |
---|
[2178] | 178 | int roar_bixcoder_close (struct roar_bixcoder * state) { |
---|
| 179 | int ret = 0; |
---|
| 180 | |
---|
| 181 | if ( state == NULL ) |
---|
| 182 | return -1; |
---|
| 183 | |
---|
| 184 | ret = roar_xcoder_close(&(state->encoder)); |
---|
| 185 | |
---|
| 186 | if ( roar_xcoder_close(&(state->decoder)) == -1 ) |
---|
| 187 | return -1; |
---|
| 188 | |
---|
| 189 | return ret; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | int roar_bixcoder_read_packet (struct roar_bixcoder * state, void * buf, size_t len) { |
---|
| 193 | if ( state == NULL ) |
---|
| 194 | return -1; |
---|
| 195 | |
---|
| 196 | return roar_xcoder_proc_packet(&(state->decoder), buf, len); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | int roar_bixcoder_read (struct roar_bixcoder * state, void * buf, size_t len) { |
---|
| 200 | if ( state == NULL ) |
---|
| 201 | return -1; |
---|
| 202 | |
---|
| 203 | return roar_xcoder_proc(&(state->decoder), buf, len); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | int roar_bixcoder_write_packet(struct roar_bixcoder * state, void * buf, size_t len) { |
---|
| 207 | if ( state == NULL ) |
---|
| 208 | return -1; |
---|
| 209 | |
---|
| 210 | return roar_xcoder_proc_packet(&(state->encoder), buf, len); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | int roar_bixcoder_write (struct roar_bixcoder * state, void * buf, size_t len) { |
---|
| 214 | if ( state == NULL ) |
---|
| 215 | return -1; |
---|
| 216 | |
---|
| 217 | return roar_xcoder_proc(&(state->encoder), buf, len); |
---|
| 218 | } |
---|
[2174] | 219 | |
---|
| 220 | // dummy functions used by some de/encoders: |
---|
| 221 | int roar_xcoder_dummy_inituninit(struct roar_xcoder * state) { |
---|
| 222 | return 0; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | int roar_xcoder_dummy_packet_size_any(struct roar_xcoder * state, int samples) { |
---|
| 226 | // the case samples=-1/samples!=-1 based things are done in the general func |
---|
| 227 | return 0; |
---|
| 228 | } |
---|
[2168] | 229 | |
---|
| 230 | //ll |
---|