source: roaraudio/roard/codecfilter_flac.c @ 4592:e51aef50fe8c

Last change on this file since 4592:e51aef50fe8c was 4592:e51aef50fe8c, checked in by phi, 13 years ago

added metadata support

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