[4590] | 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 | |
---|
| 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 | 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 | |
---|
| 59 | FLAC__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 | |
---|
| 103 | void cf_flac_cb_metadata(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) { |
---|
| 104 | ROAR_DBG("cf_flac_cb_metadata(decoder=%p, metadata=%p, client_data=%p) = (void)", decoder, metadata, client_data); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | void cf_flac_cb_error(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) { |
---|
| 108 | ROAR_DBG("cf_flac_cb_error(decoder=%p, status=%i, client_data=%p) = (void)", decoder, (int)status, client_data); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | int cf_flac_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 112 | struct roar_stream_server * info, |
---|
| 113 | struct roar_codecfilter * filter) { |
---|
| 114 | struct codecfilter_flac_inst * self; |
---|
| 115 | |
---|
| 116 | if ( ROAR_STREAM(info)->dir != ROAR_DIR_PLAY ) |
---|
| 117 | return -1; |
---|
| 118 | |
---|
| 119 | self = roar_mm_malloc(sizeof(struct codecfilter_flac_inst)); |
---|
| 120 | |
---|
| 121 | if (self == NULL) |
---|
| 122 | return -1; |
---|
| 123 | |
---|
| 124 | memset(self, 0, sizeof(struct codecfilter_flac_inst)); |
---|
| 125 | |
---|
| 126 | self->ss = info; |
---|
| 127 | |
---|
| 128 | self->decoder.decoder = FLAC__stream_decoder_new(); |
---|
| 129 | |
---|
| 130 | if ( self->decoder.decoder == NULL ) { |
---|
| 131 | roar_mm_free(self); |
---|
| 132 | return -1; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | FLAC__stream_decoder_set_read_callback(self->decoder.decoder, cf_flac_cb_read); |
---|
| 136 | FLAC__stream_decoder_set_write_callback(self->decoder.decoder, cf_flac_cb_write); |
---|
| 137 | FLAC__stream_decoder_set_metadata_callback(self->decoder.decoder, cf_flac_cb_metadata); |
---|
| 138 | FLAC__stream_decoder_set_error_callback(self->decoder.decoder, cf_flac_cb_error); |
---|
| 139 | FLAC__stream_decoder_set_client_data(self->decoder.decoder, self); |
---|
| 140 | |
---|
| 141 | FLAC__stream_decoder_init(self->decoder.decoder); |
---|
| 142 | |
---|
| 143 | *inst = self; |
---|
| 144 | |
---|
| 145 | return 0; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | int cf_flac_close(CODECFILTER_USERDATA_T inst) { |
---|
| 149 | struct codecfilter_flac_inst * self = inst; |
---|
| 150 | |
---|
| 151 | if ( self->decoder.decoder != NULL ) { |
---|
| 152 | FLAC__stream_decoder_delete(self->decoder.decoder); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | roar_mm_free(self); |
---|
| 156 | |
---|
| 157 | return 0; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | int cf_flac_write(CODECFILTER_USERDATA_T inst, char * buf, int len); |
---|
| 161 | |
---|
| 162 | int cf_flac_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 163 | struct codecfilter_flac_inst * self = inst; |
---|
| 164 | struct roar_audio_info * info = &(ROAR_STREAM(self->ss)->info); |
---|
| 165 | struct roar_buffer_stats stats; |
---|
| 166 | size_t ret; |
---|
| 167 | FLAC__StreamDecoderState state; |
---|
| 168 | |
---|
| 169 | if ( self->decoder.written == NULL ) { |
---|
| 170 | stats.bytes = 0; |
---|
| 171 | } else { |
---|
| 172 | roar_buffer_ring_stats(self->decoder.written, &stats); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | self->decoder.readret = 1; |
---|
| 176 | self->decoder.readc = 0; |
---|
| 177 | |
---|
| 178 | while ( self->decoder.readret > 0 && stats.bytes < len ) { |
---|
| 179 | if ( !FLAC__stream_decoder_process_single(self->decoder.decoder) ) { |
---|
| 180 | break; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | state = FLAC__stream_decoder_get_state(self->decoder.decoder); |
---|
| 184 | |
---|
| 185 | if ( state == FLAC__STREAM_DECODER_END_OF_STREAM ) |
---|
| 186 | break; |
---|
| 187 | |
---|
| 188 | if ( self->decoder.written == NULL && state == FLAC__STREAM_DECODER_READ_FRAME ) { |
---|
| 189 | return -1; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | roar_buffer_ring_stats(self->decoder.written, &stats); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | if ( stats.bytes ) { |
---|
| 196 | ret = len; |
---|
| 197 | |
---|
| 198 | if ( roar_buffer_shift_out(&(self->decoder.written), buf, &ret) == -1 ) { |
---|
| 199 | return -1; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | info->codec = ROAR_CODEC_DEFAULT; |
---|
| 203 | info->bits = 32; |
---|
| 204 | info->channels = FLAC__stream_decoder_get_channels(self->decoder.decoder); |
---|
| 205 | info->rate = FLAC__stream_decoder_get_sample_rate(self->decoder.decoder); |
---|
| 206 | |
---|
| 207 | return ret; |
---|
| 208 | } else { |
---|
| 209 | return self->decoder.readret == -1 ? -1 : 0; |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | int cf_flac_ctl(CODECFILTER_USERDATA_T inst, int cmd, void * data); |
---|
| 214 | |
---|
| 215 | #endif |
---|
| 216 | |
---|
| 217 | //ll |
---|