[4590] | 1 | //codecfilter_flac.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011 |
---|
[4590] | 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; |
---|
[4595] | 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 | |
---|
[4590] | 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 | 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 | |
---|
| 111 | void cf_flac_cb_metadata(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) { |
---|
[4592] | 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 | stream_meta_add(ROAR_STREAM(self->ss)->id, type, "", value); |
---|
| 150 | |
---|
| 151 | if ( strcmp(keycopy, "REPLAYGAIN_TRACK_PEAK") == 0 ) { |
---|
| 152 | rpg_track = 1/atof(value); |
---|
| 153 | /* |
---|
| 154 | } else if ( strcmp(key, "REPLAYGAIN_TRACK_GAIN") == 0 ) { |
---|
| 155 | rpg_track = powf(10, atof(value)/20); |
---|
| 156 | */ |
---|
| 157 | } else if ( strcmp(keycopy, "REPLAYGAIN_ALBUM_PEAK") == 0 ) { |
---|
| 158 | rpg_album = 1/atof(value); |
---|
| 159 | /* |
---|
| 160 | } else if ( strcmp(key, "REPLAYGAIN_ALBUM_GAIN") == 0 ) { |
---|
| 161 | rpg_album = powf(10, atof(value)/20); |
---|
| 162 | */ |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | if ( rpg_album ) { |
---|
| 168 | self->ss->mixer.rpg_div = 2718; // = int(exp(1)*1000) |
---|
| 169 | self->ss->mixer.rpg_mul = (float)rpg_album*2718; |
---|
| 170 | } else if ( rpg_track ) { |
---|
| 171 | self->ss->mixer.rpg_div = 2718; |
---|
| 172 | self->ss->mixer.rpg_mul = (float)rpg_track*2718; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | stream_meta_finalize(ROAR_STREAM(self->ss)->id); |
---|
| 176 | } |
---|
| 177 | |
---|
[4590] | 178 | ROAR_DBG("cf_flac_cb_metadata(decoder=%p, metadata=%p, client_data=%p) = (void)", decoder, metadata, client_data); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | void cf_flac_cb_error(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) { |
---|
| 182 | ROAR_DBG("cf_flac_cb_error(decoder=%p, status=%i, client_data=%p) = (void)", decoder, (int)status, client_data); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | int cf_flac_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 186 | struct roar_stream_server * info, |
---|
| 187 | struct roar_codecfilter * filter) { |
---|
| 188 | struct codecfilter_flac_inst * self; |
---|
| 189 | |
---|
| 190 | if ( ROAR_STREAM(info)->dir != ROAR_DIR_PLAY ) |
---|
| 191 | return -1; |
---|
| 192 | |
---|
| 193 | self = roar_mm_malloc(sizeof(struct codecfilter_flac_inst)); |
---|
| 194 | |
---|
| 195 | if (self == NULL) |
---|
| 196 | return -1; |
---|
| 197 | |
---|
| 198 | memset(self, 0, sizeof(struct codecfilter_flac_inst)); |
---|
| 199 | |
---|
| 200 | self->ss = info; |
---|
| 201 | |
---|
| 202 | self->decoder.decoder = FLAC__stream_decoder_new(); |
---|
| 203 | |
---|
| 204 | if ( self->decoder.decoder == NULL ) { |
---|
| 205 | roar_mm_free(self); |
---|
| 206 | return -1; |
---|
| 207 | } |
---|
| 208 | |
---|
[4600] | 209 | #ifndef ROAR_HAVE_FLAC_VERSION_1_2_1 |
---|
[4590] | 210 | FLAC__stream_decoder_set_read_callback(self->decoder.decoder, cf_flac_cb_read); |
---|
| 211 | FLAC__stream_decoder_set_write_callback(self->decoder.decoder, cf_flac_cb_write); |
---|
| 212 | FLAC__stream_decoder_set_metadata_callback(self->decoder.decoder, cf_flac_cb_metadata); |
---|
| 213 | FLAC__stream_decoder_set_error_callback(self->decoder.decoder, cf_flac_cb_error); |
---|
| 214 | FLAC__stream_decoder_set_client_data(self->decoder.decoder, self); |
---|
[4600] | 215 | #endif |
---|
[4590] | 216 | |
---|
[4592] | 217 | FLAC__stream_decoder_set_metadata_respond(self->decoder.decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT); |
---|
| 218 | |
---|
[4600] | 219 | #ifdef ROAR_HAVE_FLAC_VERSION_1_2_1 |
---|
| 220 | 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); |
---|
| 221 | #else |
---|
[4590] | 222 | FLAC__stream_decoder_init(self->decoder.decoder); |
---|
[4600] | 223 | #endif |
---|
[4590] | 224 | |
---|
| 225 | *inst = self; |
---|
| 226 | |
---|
| 227 | return 0; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | int cf_flac_close(CODECFILTER_USERDATA_T inst) { |
---|
| 231 | struct codecfilter_flac_inst * self = inst; |
---|
| 232 | |
---|
| 233 | if ( self->decoder.decoder != NULL ) { |
---|
| 234 | FLAC__stream_decoder_delete(self->decoder.decoder); |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | roar_mm_free(self); |
---|
| 238 | |
---|
| 239 | return 0; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | int cf_flac_write(CODECFILTER_USERDATA_T inst, char * buf, int len); |
---|
| 243 | |
---|
| 244 | int cf_flac_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 245 | struct codecfilter_flac_inst * self = inst; |
---|
| 246 | struct roar_audio_info * info = &(ROAR_STREAM(self->ss)->info); |
---|
| 247 | struct roar_buffer_stats stats; |
---|
| 248 | size_t ret; |
---|
| 249 | FLAC__StreamDecoderState state; |
---|
| 250 | |
---|
| 251 | if ( self->decoder.written == NULL ) { |
---|
| 252 | stats.bytes = 0; |
---|
| 253 | } else { |
---|
| 254 | roar_buffer_ring_stats(self->decoder.written, &stats); |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | self->decoder.readret = 1; |
---|
| 258 | self->decoder.readc = 0; |
---|
| 259 | |
---|
| 260 | while ( self->decoder.readret > 0 && stats.bytes < len ) { |
---|
| 261 | if ( !FLAC__stream_decoder_process_single(self->decoder.decoder) ) { |
---|
| 262 | break; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | state = FLAC__stream_decoder_get_state(self->decoder.decoder); |
---|
| 266 | |
---|
[4594] | 267 | if ( state == FLAC__STREAM_DECODER_END_OF_STREAM || state == FLAC__STREAM_DECODER_ABORTED ) |
---|
[4590] | 268 | break; |
---|
| 269 | |
---|
| 270 | if ( self->decoder.written == NULL && state == FLAC__STREAM_DECODER_READ_FRAME ) { |
---|
| 271 | return -1; |
---|
| 272 | } |
---|
| 273 | |
---|
[5131] | 274 | if ( self->decoder.written == NULL ) { |
---|
| 275 | stats.bytes = 0; |
---|
| 276 | } else { |
---|
| 277 | roar_buffer_ring_stats(self->decoder.written, &stats); |
---|
| 278 | } |
---|
[4590] | 279 | } |
---|
| 280 | |
---|
| 281 | if ( stats.bytes ) { |
---|
| 282 | ret = len; |
---|
| 283 | |
---|
| 284 | if ( roar_buffer_shift_out(&(self->decoder.written), buf, &ret) == -1 ) { |
---|
| 285 | return -1; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | info->codec = ROAR_CODEC_DEFAULT; |
---|
| 289 | info->bits = 32; |
---|
| 290 | info->channels = FLAC__stream_decoder_get_channels(self->decoder.decoder); |
---|
| 291 | info->rate = FLAC__stream_decoder_get_sample_rate(self->decoder.decoder); |
---|
| 292 | |
---|
| 293 | return ret; |
---|
| 294 | } else { |
---|
| 295 | return self->decoder.readret == -1 ? -1 : 0; |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | int cf_flac_ctl(CODECFILTER_USERDATA_T inst, int cmd, void * data); |
---|
| 300 | |
---|
| 301 | #endif |
---|
| 302 | |
---|
| 303 | //ll |
---|