source: roaraudio/roard/codecfilter_flac.c @ 5162:85c55ccd12a9

Last change on this file since 5162:85c55ccd12a9 was 5162:85c55ccd12a9, checked in by phi, 12 years ago

fixed some compiler warnings

File size: 9.8 KB
Line 
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
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
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
67FLAC__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  roar_buffer_add(self->decoder.written, buf);
104 }
105
106 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);
107
108 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
109}
110
111void cf_flac_cb_metadata(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) {
112 struct codecfilter_flac_inst * self = client_data;
113 const FLAC__StreamMetadata_VorbisComment * vc;
114 FLAC__uint32 i;
115 const char * key, * value;
116 char keycopy[ROAR_META_MAX_NAMELEN];
117 int type;
118 float rpg_track = 0, rpg_album = 0;
119
120 if ( metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT ) {
121  ROAR_DBG("cf_flac_cb_metadata(decoder=%p, metadata=%p, client_data=%p): have METADATA_TYPE_VORBIS_COMMENT", decoder, metadata, client_data);
122
123  stream_meta_clear(ROAR_STREAM(self->ss)->id);
124
125  vc = &(metadata->data.vorbis_comment);
126
127  for (i = 0; i < vc->num_comments; i++) {
128   //printf("c='%s'\n", vc->comments[i].entry);
129   key = (const char *)vc->comments[i].entry;
130   value = strstr(key, "=");
131   if ( value == NULL )
132    continue;
133
134   if ( (value - key + 1) > sizeof(keycopy) )
135    continue;
136
137   memcpy(keycopy, key, value - key);
138   keycopy[sizeof(keycopy)-1] = 0;
139   keycopy[value - key] = 0;
140
141   value++;
142
143   //printf("keycopy='%s', value='%s'\n", keycopy, value);
144
145   type = roar_meta_inttype(keycopy);
146   if ( type == -1 )
147    continue;
148
149   if ( stream_meta_add(ROAR_STREAM(self->ss)->id, type, "", value) == -1 ) {
150    ROAR_WARN("cf_flac_cb_metadata(..., client_data=%p{ROAR_STREAM(.ss)->id=%i}): Can not add meta data: %s",
151              ROAR_STREAM(self->ss)->id, roar_error2str(roar_error));
152   }
153
154    if ( strcmp(keycopy, "REPLAYGAIN_TRACK_PEAK") == 0 ) {
155     rpg_track = 1/atof(value);
156/*
157    } else if ( strcmp(key, "REPLAYGAIN_TRACK_GAIN") == 0 ) {
158     rpg_track = powf(10, atof(value)/20);
159*/
160    } else if ( strcmp(keycopy, "REPLAYGAIN_ALBUM_PEAK") == 0 ) {
161     rpg_album = 1/atof(value);
162/*
163    } else if ( strcmp(key, "REPLAYGAIN_ALBUM_GAIN") == 0 ) {
164     rpg_album = powf(10, atof(value)/20);
165*/
166    }
167
168  }
169
170 if ( rpg_album ) {
171  self->ss->mixer.rpg_div = 2718;  // = int(exp(1)*1000)
172  self->ss->mixer.rpg_mul = (float)rpg_album*2718;
173 } else if ( rpg_track ) {
174  self->ss->mixer.rpg_div = 2718;
175  self->ss->mixer.rpg_mul = (float)rpg_track*2718;
176 }
177
178  stream_meta_finalize(ROAR_STREAM(self->ss)->id);
179 }
180
181 ROAR_DBG("cf_flac_cb_metadata(decoder=%p, metadata=%p, client_data=%p) = (void)", decoder, metadata, client_data);
182}
183
184void cf_flac_cb_error(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) {
185 ROAR_DBG("cf_flac_cb_error(decoder=%p, status=%i, client_data=%p) = (void)", decoder, (int)status, client_data);
186}
187
188int cf_flac_open(CODECFILTER_USERDATA_T * inst, int codec,
189                                            struct roar_stream_server * info,
190                                            struct roar_codecfilter   * filter) {
191 struct codecfilter_flac_inst * self;
192
193 if ( ROAR_STREAM(info)->dir != ROAR_DIR_PLAY )
194  return -1;
195
196 self = roar_mm_malloc(sizeof(struct codecfilter_flac_inst));
197
198 if (self == NULL)
199  return -1;
200
201 memset(self, 0, sizeof(struct codecfilter_flac_inst));
202
203 self->ss = info;
204
205 self->decoder.decoder = FLAC__stream_decoder_new();
206
207 if ( self->decoder.decoder == NULL ) {
208  roar_mm_free(self);
209  return -1;
210 }
211
212#ifndef ROAR_HAVE_FLAC_VERSION_1_2_1
213 FLAC__stream_decoder_set_read_callback(self->decoder.decoder, cf_flac_cb_read);
214 FLAC__stream_decoder_set_write_callback(self->decoder.decoder, cf_flac_cb_write);
215 FLAC__stream_decoder_set_metadata_callback(self->decoder.decoder, cf_flac_cb_metadata);
216 FLAC__stream_decoder_set_error_callback(self->decoder.decoder, cf_flac_cb_error);
217 FLAC__stream_decoder_set_client_data(self->decoder.decoder, self);
218#endif
219
220 FLAC__stream_decoder_set_metadata_respond(self->decoder.decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
221
222#ifdef ROAR_HAVE_FLAC_VERSION_1_2_1
223 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);
224#else
225 FLAC__stream_decoder_init(self->decoder.decoder);
226#endif
227
228 *inst = self;
229
230 return 0;
231}
232
233int cf_flac_close(CODECFILTER_USERDATA_T   inst) {
234 struct codecfilter_flac_inst * self = inst;
235
236 if ( self->decoder.decoder != NULL ) {
237  FLAC__stream_decoder_delete(self->decoder.decoder);
238 }
239
240 roar_mm_free(self);
241
242 return 0;
243}
244
245int cf_flac_write(CODECFILTER_USERDATA_T   inst, char * buf, int len);
246
247int cf_flac_read(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
248 struct codecfilter_flac_inst * self = inst;
249 struct roar_audio_info * info = &(ROAR_STREAM(self->ss)->info);
250 struct roar_buffer_stats stats;
251 size_t ret;
252 FLAC__StreamDecoderState state;
253
254 if ( self->decoder.written == NULL ) {
255  stats.bytes = 0;
256 } else {
257  if ( roar_buffer_ring_stats(self->decoder.written, &stats) == -1 )
258   return -1;
259 }
260
261 self->decoder.readret = 1;
262 self->decoder.readc   = 0;
263
264 while ( self->decoder.readret > 0 && stats.bytes < len ) {
265  if ( !FLAC__stream_decoder_process_single(self->decoder.decoder) ) {
266   break;
267  }
268
269  state = FLAC__stream_decoder_get_state(self->decoder.decoder);
270
271  if ( state == FLAC__STREAM_DECODER_END_OF_STREAM || state == FLAC__STREAM_DECODER_ABORTED )
272   break;
273
274  if ( self->decoder.written == NULL && state == FLAC__STREAM_DECODER_READ_FRAME ) {
275   return -1;
276  }
277
278  if ( self->decoder.written == NULL ) {
279   stats.bytes = 0;
280  } else {
281   if ( roar_buffer_ring_stats(self->decoder.written, &stats) == -1 )
282    return -1;
283  }
284 }
285
286 if ( stats.bytes ) {
287  ret = len;
288
289  if ( roar_buffer_shift_out(&(self->decoder.written), buf, &ret) == -1 ) {
290   return -1;
291  }
292
293  info->codec    = ROAR_CODEC_DEFAULT;
294  info->bits     = 32;
295  info->channels = FLAC__stream_decoder_get_channels(self->decoder.decoder);
296  info->rate     = FLAC__stream_decoder_get_sample_rate(self->decoder.decoder);
297
298  return ret;
299 } else {
300  return self->decoder.readret == -1 ? -1 : 0;
301 }
302}
303
304int cf_flac_ctl(CODECFILTER_USERDATA_T   inst, int cmd, void * data);
305
306#endif
307
308//ll
Note: See TracBrowser for help on using the repository browser.