source: roaraudio/libroardsp/transcode_celt.c @ 3811:49db840fb4f4

Last change on this file since 3811:49db840fb4f4 was 3811:49db840fb4f4, checked in by phi, 14 years ago

fixed some copyright statements

File size: 4.9 KB
Line 
1//transcode_celt.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "libroardsp.h"
27
28#ifdef ROAR_HAVE_LIBCELT
29
30#define _16BIT (16/8)
31#define _SIZE_LEN 2
32
33int roar_xcoder_celt_init       (struct roar_xcoder * state) {
34 struct roar_xcoder_celt * self = roar_mm_malloc(sizeof(struct roar_xcoder_celt));
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 ) {
42  roar_mm_free(self);
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
52 self->bufferlen            = info->channels * 32 + _SIZE_LEN;
53 self->iobuffer             = roar_mm_malloc(self->bufferlen);
54
55 if ( self->iobuffer == NULL ) {
56  roar_mm_free(self);
57  return -1;
58 }
59
60 self->mode                 = celt_mode_create(info->rate, info->channels, self->frame_size, NULL);
61
62 if ( self->mode == NULL ) {
63  roar_mm_free(self->iobuffer);
64  roar_mm_free(self);
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
82 ROAR_DBG("roar_xcoder_celt_init(*) = 0");
83
84 return 0;
85}
86
87int roar_xcoder_celt_uninit     (struct roar_xcoder * state) {
88 struct roar_xcoder_celt * self = state->inst;
89
90 if ( self->iobuffer )
91  roar_mm_free(self->iobuffer);
92
93 if ( self->encoder )
94  celt_encoder_destroy(self->encoder);
95
96 if ( self->decoder )
97  celt_decoder_destroy(self->decoder);
98
99 if ( self->mode )
100  celt_mode_destroy(self->mode);
101
102 roar_mm_free(self);
103
104 ROAR_DBG("roar_xcoder_celt_uninit(*) = 0");
105
106 return 0;
107}
108
109int roar_xcoder_celt_packet_size(struct roar_xcoder * state, int samples) {
110 struct roar_xcoder_celt * self = state->inst;
111 register int ret = self->frame_size * _16BIT * state->info.pcm.channels;
112
113 ROAR_DBG("roar_xcoder_celt_packet_size(state=%p, samples=%i) = %i", state, samples, ret);
114
115 return ret;
116}
117
118int roar_xcoder_celt_encode     (struct roar_xcoder * state, void * buf, size_t len) {
119 struct roar_xcoder_celt * self = state->inst;
120 uint16_t * lenp = self->iobuffer;
121 void     * cp   = self->iobuffer + _SIZE_LEN;
122 uint16_t   pkglen;
123
124 ROAR_DBG("roar_xcoder_celt_encode(*): test if we are in encoding mode...");
125
126 if (!state->encode)
127  return -1;
128
129 ROAR_DBG("roar_xcoder_celt_encode(*): Encoding...");
130
131 if ( state->stage == ROAR_XCODER_STAGE_INITED ) {
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;
135  ROAR_DBG("roar_xcoder_celt_encode(*): Wrote MAGIC");
136 }
137
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;
145}
146
147int roar_xcoder_celt_decode     (struct roar_xcoder * state, void * buf, size_t len) {
148 struct roar_xcoder_celt * self = state->inst;
149 uint16_t * lenp = self->iobuffer;
150 void     * cp   = self->iobuffer + _SIZE_LEN;
151 uint16_t   pkglen;
152 char       magic[ROAR_CELT_MAGIC_LEN];
153
154 ROAR_DBG("roar_xcoder_celt_decode(*): test if we are in decoding mode...");
155
156 if (state->encode)
157  return -1;
158
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;
185}
186
187#endif
188
189//ll
Note: See TracBrowser for help on using the repository browser.