1 | //codecfilter_flac.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011 |
---|
5 | * |
---|
6 | * This file is part of roard 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 | * RoarAudio 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 "roard.h" |
---|
27 | #include <roaraudio/units.h> |
---|
28 | |
---|
29 | #ifdef ROAR_HAVE_LIBFLAC |
---|
30 | |
---|
31 | FLAC__StreamDecoderReadStatus cf_flac_cb_read(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data) { |
---|
32 | struct codecfilter_flac_inst * self = client_data; |
---|
33 | ssize_t ret; |
---|
34 | |
---|
35 | ROAR_DBG("cf_flac_cb_read(decoder=%p, buffer=%p, bytes=%p{%u}, client_data=%p) = ?", decoder, buffer, bytes, *bytes, client_data); |
---|
36 | |
---|
37 | ret = stream_vio_s_read(self->ss, buffer, *bytes); |
---|
38 | |
---|
39 | self->decoder.readret = ret; |
---|
40 | |
---|
41 | self->decoder.readc++; |
---|
42 | |
---|
43 | if (ret == -1) { |
---|
44 | *bytes = 0; |
---|
45 | |
---|
46 | #ifdef EAGAIN |
---|
47 | if ( errno == EAGAIN ) { |
---|
48 | ROAR_DBG("cf_flac_cb_read(decoder=%p, buffer=%p, bytes=%p{%u}, client_data=%p) = FLAC__STREAM_DECODER_READ_STATUS_CONTINUE", decoder, buffer, bytes, *bytes, client_data); |
---|
49 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
---|
50 | } |
---|
51 | #endif |
---|
52 | |
---|
53 | ROAR_DBG("cf_flac_cb_read(decoder=%p, buffer=%p, bytes=%p{%u}, client_data=%p) = FLAC__STREAM_DECODER_READ_STATUS_ABORT", decoder, buffer, bytes, *bytes, client_data); |
---|
54 | return FLAC__STREAM_DECODER_READ_STATUS_ABORT; |
---|
55 | } else { |
---|
56 | *bytes = ret; |
---|
57 | |
---|
58 | if ( ret == 0 && self->decoder.readc == 1 ) { |
---|
59 | ROAR_DBG("cf_flac_cb_read(decoder=%p, buffer=%p, bytes=%p{%u}, client_data=%p) = FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM", decoder, buffer, bytes, *bytes, client_data); |
---|
60 | return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; |
---|
61 | } |
---|
62 | ROAR_DBG("cf_flac_cb_read(decoder=%p, buffer=%p, bytes=%p{%u}, client_data=%p) = FLAC__STREAM_DECODER_READ_STATUS_CONTINUE", decoder, buffer, bytes, *bytes, client_data); |
---|
63 | return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | FLAC__StreamDecoderWriteStatus cf_flac_cb_write(const FLAC__StreamDecoder * decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) { |
---|
68 | struct codecfilter_flac_inst * self = client_data; |
---|
69 | struct roar_buffer * buf; |
---|
70 | struct roar_interleave is; |
---|
71 | size_t buflen = _32BIT * frame->header.blocksize * frame->header.channels; |
---|
72 | void * bufdata; |
---|
73 | int32_t * c; |
---|
74 | size_t i; |
---|
75 | int32_t shift = 32 - frame->header.bits_per_sample; |
---|
76 | |
---|
77 | ROAR_DBG("cf_flac_cb_write(decoder=%p, frame=%p, buffer=%p, client_data=%p) = ?", decoder, frame, buffer, client_data); |
---|
78 | |
---|
79 | if ( roar_interl_init(&is, frame->header.channels, 32) == -1 ) { |
---|
80 | ROAR_DBG("cf_flac_cb_write(decoder=%p, frame=%p, buffer=%p, client_data=%p) = FLAC__STREAM_DECODER_WRITE_STATUS_ABORT", decoder, frame, buffer, client_data); |
---|
81 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
---|
82 | } |
---|
83 | |
---|
84 | if ( roar_buffer_new_data(&buf, buflen, &bufdata) == -1 ) { |
---|
85 | ROAR_DBG("cf_flac_cb_write(decoder=%p, frame=%p, buffer=%p, client_data=%p) = FLAC__STREAM_DECODER_WRITE_STATUS_ABORT", decoder, frame, buffer, client_data); |
---|
86 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
---|
87 | } |
---|
88 | |
---|
89 | roar_interl_encode_ext(&is, (void**)buffer, bufdata, buflen); |
---|
90 | |
---|
91 | roar_interl_uninit(&is); |
---|
92 | |
---|
93 | if ( shift ) { |
---|
94 | buflen /= 4; |
---|
95 | for (c = bufdata, i = 0; i < buflen; i++) { |
---|
96 | c[i] <<= shift; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | if ( self->decoder.written == NULL ) { |
---|
101 | self->decoder.written = buf; |
---|
102 | } else { |
---|
103 | if ( roar_buffer_add(self->decoder.written, buf) == -1 ) { |
---|
104 | roar_buffer_free(buf); |
---|
105 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | ROAR_DBG("cf_flac_cb_write(decoder=%p, frame=%p, buffer=%p, client_data=%p) = FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE", decoder, frame, buffer, client_data); |
---|
110 | |
---|
111 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
---|
112 | } |
---|
113 | |
---|
114 | void cf_flac_cb_metadata(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) { |
---|
115 | struct codecfilter_flac_inst * self = client_data; |
---|
116 | const FLAC__StreamMetadata_VorbisComment * vc; |
---|
117 | FLAC__uint32 i; |
---|
118 | const char * key, * value; |
---|
119 | char keycopy[ROAR_META_MAX_NAMELEN]; |
---|
120 | int type; |
---|
121 | float rpg_track = 0, rpg_album = 0; |
---|
122 | |
---|
123 | if ( metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT ) { |
---|
124 | ROAR_DBG("cf_flac_cb_metadata(decoder=%p, metadata=%p, client_data=%p): have METADATA_TYPE_VORBIS_COMMENT", decoder, metadata, client_data); |
---|
125 | |
---|
126 | stream_meta_clear(ROAR_STREAM(self->ss)->id); |
---|
127 | |
---|
128 | vc = &(metadata->data.vorbis_comment); |
---|
129 | |
---|
130 | for (i = 0; i < vc->num_comments; i++) { |
---|
131 | //printf("c='%s'\n", vc->comments[i].entry); |
---|
132 | key = (const char *)vc->comments[i].entry; |
---|
133 | value = strstr(key, "="); |
---|
134 | if ( value == NULL ) |
---|
135 | continue; |
---|
136 | |
---|
137 | if ( (value - key + 1) > sizeof(keycopy) ) |
---|
138 | continue; |
---|
139 | |
---|
140 | memcpy(keycopy, key, value - key); |
---|
141 | keycopy[sizeof(keycopy)-1] = 0; |
---|
142 | keycopy[value - key] = 0; |
---|
143 | |
---|
144 | value++; |
---|
145 | |
---|
146 | //printf("keycopy='%s', value='%s'\n", keycopy, value); |
---|
147 | |
---|
148 | type = roar_meta_inttype(keycopy); |
---|
149 | if ( type == -1 ) |
---|
150 | continue; |
---|
151 | |
---|
152 | if ( stream_meta_add(ROAR_STREAM(self->ss)->id, type, "", value) == -1 ) { |
---|
153 | ROAR_WARN("cf_flac_cb_metadata(..., client_data=%p{ROAR_STREAM(.ss)->id=%i}): Can not add meta data: %s", |
---|
154 | ROAR_STREAM(self->ss)->id, roar_error2str(roar_error)); |
---|
155 | } |
---|
156 | |
---|
157 | if ( strcmp(keycopy, "REPLAYGAIN_TRACK_PEAK") == 0 ) { |
---|
158 | rpg_track = 1/atof(value); |
---|
159 | /* |
---|
160 | } else if ( strcmp(key, "REPLAYGAIN_TRACK_GAIN") == 0 ) { |
---|
161 | rpg_track = powf(10, atof(value)/20); |
---|
162 | */ |
---|
163 | } else if ( strcmp(keycopy, "REPLAYGAIN_ALBUM_PEAK") == 0 ) { |
---|
164 | rpg_album = 1/atof(value); |
---|
165 | /* |
---|
166 | } else if ( strcmp(key, "REPLAYGAIN_ALBUM_GAIN") == 0 ) { |
---|
167 | rpg_album = powf(10, atof(value)/20); |
---|
168 | */ |
---|
169 | } |
---|
170 | |
---|
171 | } |
---|
172 | |
---|
173 | if ( rpg_album ) { |
---|
174 | self->ss->mixer.rpg_div = 2718; // = int(exp(1)*1000) |
---|
175 | self->ss->mixer.rpg_mul = (float)rpg_album*2718; |
---|
176 | } else if ( rpg_track ) { |
---|
177 | self->ss->mixer.rpg_div = 2718; |
---|
178 | self->ss->mixer.rpg_mul = (float)rpg_track*2718; |
---|
179 | } |
---|
180 | |
---|
181 | stream_meta_finalize(ROAR_STREAM(self->ss)->id); |
---|
182 | } |
---|
183 | |
---|
184 | ROAR_DBG("cf_flac_cb_metadata(decoder=%p, metadata=%p, client_data=%p) = (void)", decoder, metadata, client_data); |
---|
185 | } |
---|
186 | |
---|
187 | void cf_flac_cb_error(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) { |
---|
188 | ROAR_DBG("cf_flac_cb_error(decoder=%p, status=%i, client_data=%p) = (void)", decoder, (int)status, client_data); |
---|
189 | } |
---|
190 | |
---|
191 | int cf_flac_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
192 | struct roar_stream_server * info, |
---|
193 | struct roar_codecfilter * filter) { |
---|
194 | struct codecfilter_flac_inst * self; |
---|
195 | |
---|
196 | if ( ROAR_STREAM(info)->dir != ROAR_DIR_PLAY ) |
---|
197 | return -1; |
---|
198 | |
---|
199 | self = roar_mm_malloc(sizeof(struct codecfilter_flac_inst)); |
---|
200 | |
---|
201 | if (self == NULL) |
---|
202 | return -1; |
---|
203 | |
---|
204 | memset(self, 0, sizeof(struct codecfilter_flac_inst)); |
---|
205 | |
---|
206 | self->ss = info; |
---|
207 | |
---|
208 | self->decoder.decoder = FLAC__stream_decoder_new(); |
---|
209 | |
---|
210 | if ( self->decoder.decoder == NULL ) { |
---|
211 | roar_mm_free(self); |
---|
212 | return -1; |
---|
213 | } |
---|
214 | |
---|
215 | #ifndef ROAR_HAVE_FLAC_VERSION_1_2_1 |
---|
216 | FLAC__stream_decoder_set_read_callback(self->decoder.decoder, cf_flac_cb_read); |
---|
217 | FLAC__stream_decoder_set_write_callback(self->decoder.decoder, cf_flac_cb_write); |
---|
218 | FLAC__stream_decoder_set_metadata_callback(self->decoder.decoder, cf_flac_cb_metadata); |
---|
219 | FLAC__stream_decoder_set_error_callback(self->decoder.decoder, cf_flac_cb_error); |
---|
220 | FLAC__stream_decoder_set_client_data(self->decoder.decoder, self); |
---|
221 | #endif |
---|
222 | |
---|
223 | FLAC__stream_decoder_set_metadata_respond(self->decoder.decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT); |
---|
224 | |
---|
225 | #ifdef ROAR_HAVE_FLAC_VERSION_1_2_1 |
---|
226 | FLAC__stream_decoder_init_stream(self->decoder.decoder, cf_flac_cb_read, NULL, NULL, NULL, NULL, cf_flac_cb_write, cf_flac_cb_metadata, cf_flac_cb_error, self); |
---|
227 | #else |
---|
228 | FLAC__stream_decoder_init(self->decoder.decoder); |
---|
229 | #endif |
---|
230 | |
---|
231 | *inst = self; |
---|
232 | |
---|
233 | return 0; |
---|
234 | } |
---|
235 | |
---|
236 | int cf_flac_close(CODECFILTER_USERDATA_T inst) { |
---|
237 | struct codecfilter_flac_inst * self = inst; |
---|
238 | |
---|
239 | if ( self->decoder.decoder != NULL ) { |
---|
240 | FLAC__stream_decoder_delete(self->decoder.decoder); |
---|
241 | } |
---|
242 | |
---|
243 | roar_mm_free(self); |
---|
244 | |
---|
245 | return 0; |
---|
246 | } |
---|
247 | |
---|
248 | int cf_flac_write(CODECFILTER_USERDATA_T inst, char * buf, int len); |
---|
249 | |
---|
250 | int cf_flac_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
251 | struct codecfilter_flac_inst * self = inst; |
---|
252 | struct roar_audio_info * info = &(ROAR_STREAM(self->ss)->info); |
---|
253 | struct roar_buffer_stats stats; |
---|
254 | size_t ret; |
---|
255 | FLAC__StreamDecoderState state; |
---|
256 | |
---|
257 | if ( self->decoder.written == NULL ) { |
---|
258 | stats.bytes = 0; |
---|
259 | } else { |
---|
260 | if ( roar_buffer_ring_stats(self->decoder.written, &stats) == -1 ) |
---|
261 | return -1; |
---|
262 | } |
---|
263 | |
---|
264 | self->decoder.readret = 1; |
---|
265 | self->decoder.readc = 0; |
---|
266 | |
---|
267 | while ( self->decoder.readret > 0 && stats.bytes < len ) { |
---|
268 | if ( !FLAC__stream_decoder_process_single(self->decoder.decoder) ) { |
---|
269 | break; |
---|
270 | } |
---|
271 | |
---|
272 | state = FLAC__stream_decoder_get_state(self->decoder.decoder); |
---|
273 | |
---|
274 | if ( state == FLAC__STREAM_DECODER_END_OF_STREAM || state == FLAC__STREAM_DECODER_ABORTED ) |
---|
275 | break; |
---|
276 | |
---|
277 | if ( self->decoder.written == NULL && state == FLAC__STREAM_DECODER_READ_FRAME ) { |
---|
278 | return -1; |
---|
279 | } |
---|
280 | |
---|
281 | if ( self->decoder.written == NULL ) { |
---|
282 | stats.bytes = 0; |
---|
283 | } else { |
---|
284 | if ( roar_buffer_ring_stats(self->decoder.written, &stats) == -1 ) |
---|
285 | return -1; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | if ( stats.bytes ) { |
---|
290 | ret = len; |
---|
291 | |
---|
292 | if ( roar_buffer_shift_out(&(self->decoder.written), buf, &ret) == -1 ) { |
---|
293 | return -1; |
---|
294 | } |
---|
295 | |
---|
296 | info->codec = ROAR_CODEC_DEFAULT; |
---|
297 | info->bits = 32; |
---|
298 | info->channels = FLAC__stream_decoder_get_channels(self->decoder.decoder); |
---|
299 | info->rate = FLAC__stream_decoder_get_sample_rate(self->decoder.decoder); |
---|
300 | |
---|
301 | return ret; |
---|
302 | } else { |
---|
303 | return self->decoder.readret == -1 ? -1 : 0; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | int cf_flac_ctl(CODECFILTER_USERDATA_T inst, int cmd, void * data); |
---|
308 | |
---|
309 | #endif |
---|
310 | |
---|
311 | //ll |
---|