[2187] | 1 | //transcode_celt.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 | #ifdef ROAR_HAVE_LIBCELT |
---|
| 28 | |
---|
[2193] | 29 | #define _16BIT (16/8) |
---|
[2199] | 30 | #define _SIZE_LEN 2 |
---|
[2192] | 31 | |
---|
[2188] | 32 | int roar_xcoder_celt_init (struct roar_xcoder * state) { |
---|
[2191] | 33 | struct roar_xcoder_celt * self = malloc(sizeof(struct roar_xcoder_celt)); |
---|
| 34 | struct roar_audio_info * info = &(state->info.pcm); |
---|
| 35 | |
---|
| 36 | if ( self == NULL ) |
---|
| 37 | return -1; |
---|
| 38 | |
---|
| 39 | // curruntly only 16 bit mode is supported |
---|
| 40 | if ( info->bits != 16 ) { |
---|
| 41 | free(self); |
---|
| 42 | return -1; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | memset(self, 0, sizeof(struct roar_xcoder_celt)); |
---|
| 46 | |
---|
| 47 | state->inst = self; |
---|
| 48 | |
---|
| 49 | self->frame_size = 256; |
---|
| 50 | |
---|
[2199] | 51 | self->bufferlen = info->channels * 64 + _SIZE_LEN; |
---|
| 52 | self->iobuffer = malloc(self->bufferlen); |
---|
| 53 | |
---|
| 54 | if ( self->iobuffer == NULL ) { |
---|
| 55 | free(self); |
---|
| 56 | return -1; |
---|
| 57 | } |
---|
| 58 | |
---|
[2191] | 59 | self->mode = celt_mode_create(info->rate, info->channels, self->frame_size, NULL); |
---|
| 60 | |
---|
| 61 | if ( self->mode == NULL ) { |
---|
| 62 | free(self); |
---|
| 63 | return -1; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | if (state->encode) { |
---|
| 67 | self->encoder = celt_encoder_create(self->mode); |
---|
| 68 | if ( self->encoder == NULL ) { |
---|
| 69 | roar_xcoder_celt_uninit(state); |
---|
| 70 | return -1; |
---|
| 71 | } |
---|
| 72 | } else { |
---|
| 73 | self->decoder = celt_decoder_create(self->mode); |
---|
| 74 | if ( self->decoder == NULL ) { |
---|
| 75 | roar_xcoder_celt_uninit(state); |
---|
| 76 | return -1; |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
[2197] | 80 | ROAR_DBG("roar_xcoder_celt_init(*) = 0"); |
---|
| 81 | |
---|
[2191] | 82 | return 0; |
---|
[2188] | 83 | } |
---|
| 84 | |
---|
| 85 | int roar_xcoder_celt_uninit (struct roar_xcoder * state) { |
---|
[2191] | 86 | struct roar_xcoder_celt * self = state->inst; |
---|
| 87 | |
---|
[2199] | 88 | if ( self->iobuffer ) |
---|
| 89 | free(self->iobuffer); |
---|
| 90 | |
---|
[2191] | 91 | if ( self->encoder ) |
---|
[2200] | 92 | celt_encoder_destroy(self->encoder); |
---|
[2191] | 93 | |
---|
| 94 | if ( self->decoder ) |
---|
[2200] | 95 | celt_decoder_destroy(self->decoder); |
---|
[2191] | 96 | |
---|
| 97 | if ( self->mode ) |
---|
| 98 | celt_mode_destroy(self->mode); |
---|
| 99 | |
---|
| 100 | free(self); |
---|
| 101 | |
---|
[2197] | 102 | ROAR_DBG("roar_xcoder_celt_uninit(*) = 0"); |
---|
| 103 | |
---|
| 104 | return 0; |
---|
[2188] | 105 | } |
---|
| 106 | |
---|
| 107 | int roar_xcoder_celt_packet_size(struct roar_xcoder * state, int samples) { |
---|
[2191] | 108 | struct roar_xcoder_celt * self = state->inst; |
---|
[2197] | 109 | register int ret = self->frame_size * _16BIT * state->info.pcm.channels; |
---|
[2191] | 110 | |
---|
[2197] | 111 | ROAR_DBG("roar_xcoder_celt_packet_size(state=%p, samples=%i) = %i", state, samples, ret); |
---|
| 112 | |
---|
| 113 | return ret; |
---|
[2188] | 114 | } |
---|
| 115 | |
---|
| 116 | int roar_xcoder_celt_encode (struct roar_xcoder * state, void * buf, size_t len) { |
---|
[2191] | 117 | struct roar_xcoder_celt * self = state->inst; |
---|
[2199] | 118 | uint16_t * lenp = self->iobuffer; |
---|
| 119 | void * cp = self->iobuffer + _SIZE_LEN; |
---|
[2191] | 120 | |
---|
[2197] | 121 | ROAR_DBG("roar_xcoder_celt_encode(*): test if we are in encoding mode..."); |
---|
| 122 | |
---|
[2191] | 123 | if (!state->encode) |
---|
| 124 | return -1; |
---|
| 125 | |
---|
[2197] | 126 | ROAR_DBG("roar_xcoder_celt_encode(*): Encoding..."); |
---|
| 127 | |
---|
[2193] | 128 | if ( len != self->frame_size * _16BIT * state->info.pcm.channels ) |
---|
[2192] | 129 | return -1; |
---|
| 130 | |
---|
[2197] | 131 | ROAR_DBG("roar_xcoder_celt_encode(*): Frame size check OK"); |
---|
| 132 | |
---|
[2193] | 133 | if ( state->stage == ROAR_XCODER_STAGE_INITED ) { |
---|
[2192] | 134 | if ( roar_vio_write(state->backend, ROAR_CELT_MAGIC, ROAR_CELT_MAGIC_LEN) != ROAR_CELT_MAGIC_LEN ) |
---|
| 135 | return -1; |
---|
| 136 | state->stage = ROAR_XCODER_STAGE_MAGIC; |
---|
[2197] | 137 | ROAR_DBG("roar_xcoder_celt_encode(*): Wrote MAGIC"); |
---|
[2192] | 138 | } |
---|
| 139 | |
---|
[2188] | 140 | return -1; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | int roar_xcoder_celt_decode (struct roar_xcoder * state, void * buf, size_t len) { |
---|
[2191] | 144 | struct roar_xcoder_celt * self = state->inst; |
---|
| 145 | |
---|
[2197] | 146 | ROAR_DBG("roar_xcoder_celt_decode(*): test if we are in decoding mode..."); |
---|
| 147 | |
---|
[2191] | 148 | if (state->encode) |
---|
| 149 | return -1; |
---|
| 150 | |
---|
[2188] | 151 | return -1; |
---|
| 152 | } |
---|
[2187] | 153 | |
---|
| 154 | #endif |
---|
| 155 | |
---|
| 156 | //ll |
---|