[2187] | 1 | //transcode_celt.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[3811] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
[2187] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[2187] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "libroardsp.h" |
---|
| 27 | |
---|
| 28 | #ifdef ROAR_HAVE_LIBCELT |
---|
| 29 | |
---|
[2193] | 30 | #define _16BIT (16/8) |
---|
[2199] | 31 | #define _SIZE_LEN 2 |
---|
[2192] | 32 | |
---|
[2188] | 33 | int roar_xcoder_celt_init (struct roar_xcoder * state) { |
---|
[3063] | 34 | struct roar_xcoder_celt * self = roar_mm_malloc(sizeof(struct roar_xcoder_celt)); |
---|
[2191] | 35 | struct roar_audio_info * info = &(state->info.pcm); |
---|
| 36 | |
---|
| 37 | if ( self == NULL ) |
---|
| 38 | return -1; |
---|
| 39 | |
---|
| 40 | // curruntly only 16 bit mode is supported |
---|
| 41 | if ( info->bits != 16 ) { |
---|
[3063] | 42 | roar_mm_free(self); |
---|
[2191] | 43 | return -1; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | memset(self, 0, sizeof(struct roar_xcoder_celt)); |
---|
| 47 | |
---|
| 48 | state->inst = self; |
---|
| 49 | |
---|
| 50 | self->frame_size = 256; |
---|
| 51 | |
---|
[2204] | 52 | self->bufferlen = info->channels * 32 + _SIZE_LEN; |
---|
[3063] | 53 | self->iobuffer = roar_mm_malloc(self->bufferlen); |
---|
[2199] | 54 | |
---|
| 55 | if ( self->iobuffer == NULL ) { |
---|
[3063] | 56 | roar_mm_free(self); |
---|
[2199] | 57 | return -1; |
---|
| 58 | } |
---|
| 59 | |
---|
[2191] | 60 | self->mode = celt_mode_create(info->rate, info->channels, self->frame_size, NULL); |
---|
| 61 | |
---|
| 62 | if ( self->mode == NULL ) { |
---|
[3063] | 63 | roar_mm_free(self->iobuffer); |
---|
| 64 | roar_mm_free(self); |
---|
[2191] | 65 | return -1; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | if (state->encode) { |
---|
| 69 | self->encoder = celt_encoder_create(self->mode); |
---|
| 70 | if ( self->encoder == NULL ) { |
---|
| 71 | roar_xcoder_celt_uninit(state); |
---|
| 72 | return -1; |
---|
| 73 | } |
---|
| 74 | } else { |
---|
| 75 | self->decoder = celt_decoder_create(self->mode); |
---|
| 76 | if ( self->decoder == NULL ) { |
---|
| 77 | roar_xcoder_celt_uninit(state); |
---|
| 78 | return -1; |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
[2197] | 82 | ROAR_DBG("roar_xcoder_celt_init(*) = 0"); |
---|
| 83 | |
---|
[2191] | 84 | return 0; |
---|
[2188] | 85 | } |
---|
| 86 | |
---|
| 87 | int roar_xcoder_celt_uninit (struct roar_xcoder * state) { |
---|
[2191] | 88 | struct roar_xcoder_celt * self = state->inst; |
---|
| 89 | |
---|
[2199] | 90 | if ( self->iobuffer ) |
---|
[3063] | 91 | roar_mm_free(self->iobuffer); |
---|
[2199] | 92 | |
---|
[2191] | 93 | if ( self->encoder ) |
---|
[2200] | 94 | celt_encoder_destroy(self->encoder); |
---|
[2191] | 95 | |
---|
| 96 | if ( self->decoder ) |
---|
[2200] | 97 | celt_decoder_destroy(self->decoder); |
---|
[2191] | 98 | |
---|
| 99 | if ( self->mode ) |
---|
| 100 | celt_mode_destroy(self->mode); |
---|
| 101 | |
---|
[3063] | 102 | roar_mm_free(self); |
---|
[2191] | 103 | |
---|
[2197] | 104 | ROAR_DBG("roar_xcoder_celt_uninit(*) = 0"); |
---|
| 105 | |
---|
| 106 | return 0; |
---|
[2188] | 107 | } |
---|
| 108 | |
---|
| 109 | int roar_xcoder_celt_packet_size(struct roar_xcoder * state, int samples) { |
---|
[2191] | 110 | struct roar_xcoder_celt * self = state->inst; |
---|
[2197] | 111 | register int ret = self->frame_size * _16BIT * state->info.pcm.channels; |
---|
[2191] | 112 | |
---|
[2197] | 113 | ROAR_DBG("roar_xcoder_celt_packet_size(state=%p, samples=%i) = %i", state, samples, ret); |
---|
| 114 | |
---|
| 115 | return ret; |
---|
[2188] | 116 | } |
---|
| 117 | |
---|
| 118 | int roar_xcoder_celt_encode (struct roar_xcoder * state, void * buf, size_t len) { |
---|
[2191] | 119 | struct roar_xcoder_celt * self = state->inst; |
---|
[2199] | 120 | uint16_t * lenp = self->iobuffer; |
---|
| 121 | void * cp = self->iobuffer + _SIZE_LEN; |
---|
[2201] | 122 | uint16_t pkglen; |
---|
[2191] | 123 | |
---|
[2197] | 124 | ROAR_DBG("roar_xcoder_celt_encode(*): test if we are in encoding mode..."); |
---|
| 125 | |
---|
[2191] | 126 | if (!state->encode) |
---|
| 127 | return -1; |
---|
| 128 | |
---|
[2197] | 129 | ROAR_DBG("roar_xcoder_celt_encode(*): Encoding..."); |
---|
| 130 | |
---|
[2193] | 131 | if ( state->stage == ROAR_XCODER_STAGE_INITED ) { |
---|
[2192] | 132 | if ( roar_vio_write(state->backend, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) != ROAR_CELT_MAGIC_LEN ) |
---|
| 133 | return -1; |
---|
| 134 | state->stage = ROAR_XCODER_STAGE_MAGIC; |
---|
[2197] | 135 | ROAR_DBG("roar_xcoder_celt_encode(*): Wrote MAGIC"); |
---|
[2192] | 136 | } |
---|
| 137 | |
---|
[2201] | 138 | pkglen = celt_encode(self->encoder, (celt_int16_t *) buf, NULL, cp, self->bufferlen - _SIZE_LEN); |
---|
| 139 | *lenp = ROAR_HOST2NET16(pkglen); |
---|
| 140 | |
---|
| 141 | if ( roar_vio_write(state->backend, self->iobuffer, pkglen+2) == -1 ) |
---|
| 142 | return -1; |
---|
| 143 | |
---|
| 144 | return 0; |
---|
[2188] | 145 | } |
---|
| 146 | |
---|
| 147 | int roar_xcoder_celt_decode (struct roar_xcoder * state, void * buf, size_t len) { |
---|
[2191] | 148 | struct roar_xcoder_celt * self = state->inst; |
---|
[2203] | 149 | uint16_t * lenp = self->iobuffer; |
---|
| 150 | void * cp = self->iobuffer + _SIZE_LEN; |
---|
| 151 | uint16_t pkglen; |
---|
| 152 | char magic[ROAR_CELT_MAGIC_LEN]; |
---|
[2191] | 153 | |
---|
[2197] | 154 | ROAR_DBG("roar_xcoder_celt_decode(*): test if we are in decoding mode..."); |
---|
| 155 | |
---|
[2191] | 156 | if (state->encode) |
---|
| 157 | return -1; |
---|
| 158 | |
---|
[2203] | 159 | if ( state->stage == ROAR_XCODER_STAGE_INITED ) { |
---|
| 160 | if ( roar_vio_read(state->backend, magic, ROAR_CELT_MAGIC_LEN) != ROAR_CELT_MAGIC_LEN ) |
---|
| 161 | return -1; |
---|
| 162 | |
---|
| 163 | if ( memcmp(magic, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) != 0 ) |
---|
| 164 | return -1; |
---|
| 165 | |
---|
| 166 | state->stage = ROAR_XCODER_STAGE_MAGIC; |
---|
| 167 | ROAR_DBG("roar_xcoder_celt_decode(*): Found valid Magic"); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | if ( roar_vio_read(state->backend, lenp, _SIZE_LEN) != _SIZE_LEN ) |
---|
| 171 | return -1; |
---|
| 172 | |
---|
| 173 | pkglen = ROAR_NET2HOST16(*lenp); |
---|
| 174 | |
---|
| 175 | if ( pkglen > (self->bufferlen - _SIZE_LEN) ) |
---|
| 176 | return -1; |
---|
| 177 | |
---|
| 178 | if ( roar_vio_read(state->backend, cp, pkglen) != pkglen ) |
---|
| 179 | return -1; |
---|
| 180 | |
---|
| 181 | if ( celt_decode(self->decoder, cp, pkglen, (celt_int16_t *) buf) < 0 ) |
---|
| 182 | return -1; |
---|
| 183 | |
---|
| 184 | return 0; |
---|
[2188] | 185 | } |
---|
[2187] | 186 | |
---|
| 187 | #endif |
---|
| 188 | |
---|
| 189 | //ll |
---|