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