Josh Coalson | 9745f25 | 2004-09-24 13:57:40 +0000 | [diff] [blame] | 1 | /* in_flac - Winamp2 FLAC input plugin
|
Josh Coalson | 0395dac | 2006-04-25 06:59:33 +0000 | [diff] [blame] | 2 | * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 Josh Coalson
|
Josh Coalson | 9745f25 | 2004-09-24 13:57:40 +0000 | [diff] [blame] | 3 | *
|
| 4 | * This program is free software; you can redistribute it and/or
|
| 5 | * modify it under the terms of the GNU General Public License
|
| 6 | * as published by the Free Software Foundation; either version 2
|
| 7 | * of the License, or (at your option) any later version.
|
| 8 | *
|
| 9 | * This program is distributed in the hope that it will be useful,
|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 | * GNU General Public License for more details.
|
| 13 | *
|
| 14 | * You should have received a copy of the GNU General Public License
|
| 15 | * along with this program; if not, write to the Free Software
|
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 17 | */
|
| 18 |
|
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 19 | #if HAVE_CONFIG_H
|
| 20 | # include <config.h>
|
| 21 | #endif
|
| 22 |
|
| 23 | #include <limits.h> /* for INT_MAX */
|
Josh Coalson | 9745f25 | 2004-09-24 13:57:40 +0000 | [diff] [blame] | 24 | #include <stdlib.h>
|
Josh Coalson | 403fb5c | 2004-09-28 01:26:30 +0000 | [diff] [blame] | 25 | #include <string.h> /* for memmove() */
|
Josh Coalson | 9745f25 | 2004-09-24 13:57:40 +0000 | [diff] [blame] | 26 | #include "playback.h"
|
| 27 | #include "share/grabbag.h"
|
| 28 |
|
| 29 |
|
| 30 | static FLAC__int32 reservoir_[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS][FLAC__MAX_BLOCK_SIZE * 2/*for overflow*/];
|
| 31 | static FLAC__int32 *reservoir__[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS] = { reservoir_[0], reservoir_[1] }; /*@@@ kind of a hard-coded hack */
|
| 32 | static unsigned wide_samples_in_reservoir_;
|
| 33 | static output_config_t cfg; /* local copy */
|
| 34 |
|
| 35 | static unsigned bh_index_last_w, bh_index_last_o, written_time_last;
|
| 36 | static FLAC__int64 decode_position, decode_position_last;
|
| 37 |
|
| 38 | /*
|
| 39 | * callbacks
|
| 40 | */
|
| 41 |
|
| 42 | static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
|
| 43 | {
|
| 44 | file_info_struct *file_info = (file_info_struct*)client_data;
|
| 45 | const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
|
| 46 | unsigned channel;
|
| 47 |
|
| 48 | (void)decoder;
|
| 49 |
|
| 50 | if (file_info->abort_flag)
|
| 51 | return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
| 52 |
|
| 53 | for (channel = 0; channel < channels; channel++)
|
| 54 | memcpy(&reservoir_[channel][wide_samples_in_reservoir_], buffer[channel], sizeof(buffer[0][0]) * wide_samples);
|
| 55 |
|
| 56 | wide_samples_in_reservoir_ += wide_samples;
|
| 57 |
|
| 58 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
| 59 | }
|
| 60 |
|
| 61 | static void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
|
| 62 | {
|
| 63 | file_info_struct *file_info = (file_info_struct*)client_data;
|
| 64 | (void)decoder;
|
| 65 |
|
| 66 | if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
|
| 67 | {
|
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 68 | file_info->total_samples = metadata->data.stream_info.total_samples;
|
Josh Coalson | 9745f25 | 2004-09-24 13:57:40 +0000 | [diff] [blame] | 69 | file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
|
| 70 | file_info->channels = metadata->data.stream_info.channels;
|
| 71 | file_info->sample_rate = metadata->data.stream_info.sample_rate;
|
| 72 |
|
| 73 | if (file_info->bits_per_sample!=8 && file_info->bits_per_sample!=16 && file_info->bits_per_sample!=24)
|
| 74 | {
|
| 75 | FLAC_plugin__show_error("This plugin can only handle 8/16/24-bit samples.");
|
| 76 | file_info->abort_flag = true;
|
| 77 | return;
|
| 78 | }
|
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 79 |
|
| 80 | {
|
| 81 | /* with VC++ you have to spoon feed it the casting from uint64->int64->double */
|
| 82 | FLAC__uint64 l = (FLAC__uint64)((double)(FLAC__int64)file_info->total_samples / (double)file_info->sample_rate * 1000.0 + 0.5);
|
| 83 | if (l > INT_MAX)
|
| 84 | l = INT_MAX;
|
| 85 | file_info->length_in_msec = (int)l;
|
| 86 | }
|
Josh Coalson | 9745f25 | 2004-09-24 13:57:40 +0000 | [diff] [blame] | 87 | }
|
| 88 | else if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
|
| 89 | {
|
| 90 | double gain, peak;
|
Josh Coalson | 95e39a8 | 2005-09-01 01:01:08 +0000 | [diff] [blame] | 91 | if (grabbag__replaygain_load_from_vorbiscomment(metadata, cfg.replaygain.album_mode, /*strict=*/false, &gain, &peak))
|
Josh Coalson | 9745f25 | 2004-09-24 13:57:40 +0000 | [diff] [blame] | 92 | {
|
| 93 | file_info->has_replaygain = true;
|
| 94 | file_info->replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)cfg.replaygain.preamp, !cfg.replaygain.hard_limit);
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | static void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
|
| 100 | {
|
| 101 | file_info_struct *file_info = (file_info_struct*)client_data;
|
| 102 | (void)decoder;
|
| 103 |
|
Josh Coalson | 30d7666 | 2004-09-27 04:38:52 +0000 | [diff] [blame] | 104 | if (cfg.misc.stop_err || status!=FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
|
Josh Coalson | 9745f25 | 2004-09-24 13:57:40 +0000 | [diff] [blame] | 105 | file_info->abort_flag = true;
|
| 106 | }
|
| 107 |
|
| 108 | /*
|
| 109 | * init/delete
|
| 110 | */
|
| 111 |
|
| 112 | FLAC__bool FLAC_plugin__decoder_init(FLAC__FileDecoder *decoder, const char *filename, FLAC__int64 filesize, file_info_struct *file_info, output_config_t *config)
|
| 113 | {
|
| 114 | FLAC__ASSERT(decoder);
|
| 115 | FLAC_plugin__decoder_finish(decoder);
|
| 116 | /* init decoder */
|
| 117 | FLAC__file_decoder_set_md5_checking(decoder, false);
|
| 118 | FLAC__file_decoder_set_filename(decoder, filename);
|
| 119 | FLAC__file_decoder_set_metadata_ignore_all(decoder);
|
| 120 | FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
|
| 121 | FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
|
| 122 | FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback);
|
| 123 | FLAC__file_decoder_set_write_callback(decoder, write_callback);
|
| 124 | FLAC__file_decoder_set_error_callback(decoder, error_callback);
|
| 125 | FLAC__file_decoder_set_client_data(decoder, file_info);
|
| 126 |
|
| 127 | if (FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
|
| 128 | {
|
| 129 | FLAC_plugin__show_error("Error while initializing decoder (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);
|
| 130 | return false;
|
| 131 | }
|
| 132 | /* process */
|
| 133 | cfg = *config;
|
| 134 | wide_samples_in_reservoir_ = 0;
|
| 135 | file_info->is_playing = false;
|
| 136 | file_info->abort_flag = false;
|
| 137 | file_info->has_replaygain = false;
|
| 138 |
|
| 139 | if (!FLAC__file_decoder_process_until_end_of_metadata(decoder))
|
| 140 | {
|
| 141 | FLAC_plugin__show_error("Error while processing metadata (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);
|
| 142 | return false;
|
| 143 | }
|
| 144 | /* check results */
|
| 145 | if (file_info->abort_flag) return false; /* metadata callback already popped up the error dialog */
|
| 146 | /* init replaygain */
|
| 147 | file_info->output_bits_per_sample = file_info->has_replaygain && cfg.replaygain.enable ?
|
| 148 | cfg.resolution.replaygain.bps_out :
|
| 149 | cfg.resolution.normal.dither_24_to_16 ? min(file_info->bits_per_sample, 16) : file_info->bits_per_sample;
|
| 150 |
|
| 151 | if (file_info->has_replaygain && cfg.replaygain.enable && cfg.resolution.replaygain.dither)
|
| 152 | FLAC__replaygain_synthesis__init_dither_context(&file_info->dither_context, file_info->bits_per_sample, cfg.resolution.replaygain.noise_shaping);
|
| 153 | /* more inits */
|
| 154 | file_info->eof = false;
|
| 155 | file_info->seek_to = -1;
|
| 156 | file_info->is_playing = true;
|
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 157 | file_info->average_bps = (unsigned)(filesize / (125.*(double)(FLAC__int64)file_info->total_samples/(double)file_info->sample_rate));
|
Josh Coalson | 9745f25 | 2004-09-24 13:57:40 +0000 | [diff] [blame] | 158 |
|
| 159 | bh_index_last_w = 0;
|
| 160 | bh_index_last_o = BITRATE_HIST_SIZE;
|
| 161 | decode_position = 0;
|
| 162 | decode_position_last = 0;
|
| 163 | written_time_last = 0;
|
| 164 |
|
| 165 | return true;
|
| 166 | }
|
| 167 |
|
| 168 | void FLAC_plugin__decoder_finish(FLAC__FileDecoder *decoder)
|
| 169 | {
|
| 170 | if (decoder && FLAC__file_decoder_get_state(decoder)!=FLAC__FILE_DECODER_UNINITIALIZED)
|
| 171 | FLAC__file_decoder_finish(decoder);
|
| 172 | }
|
| 173 |
|
| 174 | void FLAC_plugin__decoder_delete(FLAC__FileDecoder *decoder)
|
| 175 | {
|
| 176 | if (decoder)
|
| 177 | {
|
| 178 | FLAC_plugin__decoder_finish(decoder);
|
| 179 | FLAC__file_decoder_delete(decoder);
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | /*
|
| 184 | * decode
|
| 185 | */
|
| 186 |
|
| 187 | int FLAC_plugin__seek(FLAC__FileDecoder *decoder, file_info_struct *file_info)
|
| 188 | {
|
| 189 | int pos;
|
Josh Coalson | b1ec796 | 2006-05-24 04:41:36 +0000 | [diff] [blame] | 190 | const FLAC__uint64 target_sample = file_info->total_samples * file_info->seek_to / file_info->length_in_msec;
|
Josh Coalson | 9745f25 | 2004-09-24 13:57:40 +0000 | [diff] [blame] | 191 |
|
| 192 | if (!FLAC__file_decoder_seek_absolute(decoder, target_sample))
|
| 193 | return -1;
|
| 194 |
|
| 195 | file_info->seek_to = -1;
|
| 196 | file_info->eof = false;
|
| 197 | wide_samples_in_reservoir_ = 0;
|
| 198 | pos = (int)(target_sample*1000 / file_info->sample_rate);
|
| 199 |
|
| 200 | bh_index_last_o = bh_index_last_w = (pos/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;
|
| 201 | if (!FLAC__file_decoder_get_decode_position(decoder, &decode_position))
|
| 202 | decode_position = 0;
|
| 203 |
|
| 204 | return pos;
|
| 205 | }
|
| 206 |
|
| 207 | unsigned FLAC_plugin__decode(FLAC__FileDecoder *decoder, file_info_struct *file_info, char *sample_buffer)
|
| 208 | {
|
| 209 | /* fill reservoir */
|
| 210 | while (wide_samples_in_reservoir_ < SAMPLES_PER_WRITE)
|
| 211 | {
|
| 212 | if (FLAC__file_decoder_get_state(decoder) == FLAC__FILE_DECODER_END_OF_FILE)
|
| 213 | {
|
| 214 | file_info->eof = true;
|
| 215 | break;
|
| 216 | }
|
| 217 | else if (!FLAC__file_decoder_process_single(decoder))
|
| 218 | {
|
| 219 | FLAC_plugin__show_error("Error while processing frame (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);
|
| 220 | file_info->eof = true;
|
| 221 | break;
|
| 222 | }
|
| 223 | if (!FLAC__file_decoder_get_decode_position(decoder, &decode_position))
|
| 224 | decode_position = 0;
|
| 225 | }
|
| 226 | /* output samples */
|
| 227 | if (wide_samples_in_reservoir_ > 0)
|
| 228 | {
|
| 229 | const unsigned n = min(wide_samples_in_reservoir_, SAMPLES_PER_WRITE);
|
| 230 | const unsigned channels = file_info->channels;
|
| 231 | unsigned i;
|
| 232 | int bytes;
|
| 233 |
|
| 234 | if (cfg.replaygain.enable && file_info->has_replaygain)
|
| 235 | {
|
| 236 | bytes = FLAC__replaygain_synthesis__apply_gain(
|
| 237 | sample_buffer,
|
| 238 | true, /* little_endian_data_out */
|
| 239 | file_info->output_bits_per_sample == 8, /* unsigned_data_out */
|
| 240 | reservoir__,
|
| 241 | n,
|
| 242 | channels,
|
| 243 | file_info->bits_per_sample,
|
| 244 | file_info->output_bits_per_sample,
|
| 245 | file_info->replay_scale,
|
| 246 | cfg.replaygain.hard_limit,
|
| 247 | cfg.resolution.replaygain.dither,
|
| 248 | &file_info->dither_context
|
| 249 | );
|
| 250 | }
|
| 251 | else
|
| 252 | {
|
| 253 | bytes = FLAC__plugin_common__pack_pcm_signed_little_endian(
|
| 254 | sample_buffer,
|
| 255 | reservoir__,
|
| 256 | n,
|
| 257 | channels,
|
| 258 | file_info->bits_per_sample,
|
| 259 | file_info->output_bits_per_sample
|
| 260 | );
|
| 261 | }
|
| 262 |
|
| 263 | wide_samples_in_reservoir_ -= n;
|
| 264 | for (i = 0; i < channels; i++)
|
| 265 | memmove(&reservoir_[i][0], &reservoir_[i][n], sizeof(reservoir_[0][0]) * wide_samples_in_reservoir_);
|
| 266 |
|
| 267 | return bytes;
|
| 268 | }
|
| 269 | else
|
| 270 | {
|
| 271 | file_info->eof = true;
|
| 272 | return 0;
|
| 273 | }
|
| 274 | }
|
| 275 |
|
| 276 | int FLAC_plugin__get_rate(unsigned written_time, unsigned output_time, file_info_struct *file_info)
|
| 277 | {
|
| 278 | static int bitrate_history_[BITRATE_HIST_SIZE];
|
| 279 | unsigned bh_index_w = (written_time/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;
|
| 280 | unsigned bh_index_o = (output_time/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;
|
| 281 |
|
| 282 | /* written bitrate */
|
| 283 | if (bh_index_w != bh_index_last_w)
|
| 284 | {
|
| 285 | bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE-1)%BITRATE_HIST_SIZE] =
|
| 286 | decode_position>decode_position_last && written_time > written_time_last ?
|
| 287 | (unsigned)(8000*(decode_position - decode_position_last)/(written_time - written_time_last)) :
|
| 288 | file_info->average_bps;
|
| 289 |
|
| 290 | bh_index_last_w = bh_index_w;
|
| 291 | written_time_last = written_time;
|
| 292 | decode_position_last = decode_position;
|
| 293 | }
|
| 294 |
|
| 295 | /* output bitrate */
|
| 296 | if (bh_index_o!=bh_index_last_o && bh_index_o!=bh_index_last_w)
|
| 297 | {
|
| 298 | bh_index_last_o = bh_index_o;
|
| 299 | return bitrate_history_[bh_index_o];
|
| 300 | }
|
| 301 |
|
| 302 | return 0;
|
| 303 | }
|