1 /* in_flac - Winamp2 FLAC input plugin
\r
2 * Copyright (C) 2000,2001,2002,2003,2004,2005 Josh Coalson
\r
4 * This program is free software; you can redistribute it and/or
\r
5 * modify it under the terms of the GNU General Public License
\r
6 * as published by the Free Software Foundation; either version 2
\r
7 * of the License, or (at your option) any later version.
\r
9 * This program is distributed in the hope that it will be useful,
\r
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * GNU General Public License for more details.
\r
14 * You should have received a copy of the GNU General Public License
\r
15 * along with this program; if not, write to the Free Software
\r
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
\r
20 #include <string.h> /* for memmove() */
\r
21 #include "playback.h"
\r
22 #include "share/grabbag.h"
\r
25 static FLAC__int32 reservoir_[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS][FLAC__MAX_BLOCK_SIZE * 2/*for overflow*/];
\r
26 static FLAC__int32 *reservoir__[FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS] = { reservoir_[0], reservoir_[1] }; /*@@@ kind of a hard-coded hack */
\r
27 static unsigned wide_samples_in_reservoir_;
\r
28 static output_config_t cfg; /* local copy */
\r
30 static unsigned bh_index_last_w, bh_index_last_o, written_time_last;
\r
31 static FLAC__int64 decode_position, decode_position_last;
\r
37 static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
\r
39 file_info_struct *file_info = (file_info_struct*)client_data;
\r
40 const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
\r
45 if (file_info->abort_flag)
\r
46 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
\r
48 for (channel = 0; channel < channels; channel++)
\r
49 memcpy(&reservoir_[channel][wide_samples_in_reservoir_], buffer[channel], sizeof(buffer[0][0]) * wide_samples);
\r
51 wide_samples_in_reservoir_ += wide_samples;
\r
53 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
\r
56 static void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
\r
58 file_info_struct *file_info = (file_info_struct*)client_data;
\r
61 if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
\r
63 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
\r
64 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xfffffffful);
\r
65 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
\r
66 file_info->channels = metadata->data.stream_info.channels;
\r
67 file_info->sample_rate = metadata->data.stream_info.sample_rate;
\r
69 if (file_info->bits_per_sample!=8 && file_info->bits_per_sample!=16 && file_info->bits_per_sample!=24)
\r
71 FLAC_plugin__show_error("This plugin can only handle 8/16/24-bit samples.");
\r
72 file_info->abort_flag = true;
\r
75 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
\r
77 else if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
\r
80 if (grabbag__replaygain_load_from_vorbiscomment(metadata, cfg.replaygain.album_mode, &gain, &peak))
\r
82 file_info->has_replaygain = true;
\r
83 file_info->replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)cfg.replaygain.preamp, !cfg.replaygain.hard_limit);
\r
88 static void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
\r
90 file_info_struct *file_info = (file_info_struct*)client_data;
\r
93 if (cfg.misc.stop_err || status!=FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
\r
94 file_info->abort_flag = true;
\r
101 FLAC__bool FLAC_plugin__decoder_init(FLAC__FileDecoder *decoder, const char *filename, FLAC__int64 filesize, file_info_struct *file_info, output_config_t *config)
\r
103 FLAC__ASSERT(decoder);
\r
104 FLAC_plugin__decoder_finish(decoder);
\r
106 FLAC__file_decoder_set_md5_checking(decoder, false);
\r
107 FLAC__file_decoder_set_filename(decoder, filename);
\r
108 FLAC__file_decoder_set_metadata_ignore_all(decoder);
\r
109 FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
\r
110 FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
\r
111 FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback);
\r
112 FLAC__file_decoder_set_write_callback(decoder, write_callback);
\r
113 FLAC__file_decoder_set_error_callback(decoder, error_callback);
\r
114 FLAC__file_decoder_set_client_data(decoder, file_info);
\r
116 if (FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
\r
118 FLAC_plugin__show_error("Error while initializing decoder (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);
\r
123 wide_samples_in_reservoir_ = 0;
\r
124 file_info->is_playing = false;
\r
125 file_info->abort_flag = false;
\r
126 file_info->has_replaygain = false;
\r
128 if (!FLAC__file_decoder_process_until_end_of_metadata(decoder))
\r
130 FLAC_plugin__show_error("Error while processing metadata (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);
\r
133 /* check results */
\r
134 if (file_info->abort_flag) return false; /* metadata callback already popped up the error dialog */
\r
135 /* init replaygain */
\r
136 file_info->output_bits_per_sample = file_info->has_replaygain && cfg.replaygain.enable ?
\r
137 cfg.resolution.replaygain.bps_out :
\r
138 cfg.resolution.normal.dither_24_to_16 ? min(file_info->bits_per_sample, 16) : file_info->bits_per_sample;
\r
140 if (file_info->has_replaygain && cfg.replaygain.enable && cfg.resolution.replaygain.dither)
\r
141 FLAC__replaygain_synthesis__init_dither_context(&file_info->dither_context, file_info->bits_per_sample, cfg.resolution.replaygain.noise_shaping);
\r
143 file_info->eof = false;
\r
144 file_info->seek_to = -1;
\r
145 file_info->is_playing = true;
\r
146 file_info->average_bps = (unsigned)(filesize / (125.*file_info->total_samples/file_info->sample_rate));
\r
148 bh_index_last_w = 0;
\r
149 bh_index_last_o = BITRATE_HIST_SIZE;
\r
150 decode_position = 0;
\r
151 decode_position_last = 0;
\r
152 written_time_last = 0;
\r
157 void FLAC_plugin__decoder_finish(FLAC__FileDecoder *decoder)
\r
159 if (decoder && FLAC__file_decoder_get_state(decoder)!=FLAC__FILE_DECODER_UNINITIALIZED)
\r
160 FLAC__file_decoder_finish(decoder);
\r
163 void FLAC_plugin__decoder_delete(FLAC__FileDecoder *decoder)
\r
167 FLAC_plugin__decoder_finish(decoder);
\r
168 FLAC__file_decoder_delete(decoder);
\r
176 int FLAC_plugin__seek(FLAC__FileDecoder *decoder, file_info_struct *file_info)
\r
179 const FLAC__uint64 target_sample =
\r
180 (FLAC__uint64)file_info->total_samples*file_info->seek_to / file_info->length_in_msec;
\r
182 if (!FLAC__file_decoder_seek_absolute(decoder, target_sample))
\r
185 file_info->seek_to = -1;
\r
186 file_info->eof = false;
\r
187 wide_samples_in_reservoir_ = 0;
\r
188 pos = (int)(target_sample*1000 / file_info->sample_rate);
\r
190 bh_index_last_o = bh_index_last_w = (pos/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;
\r
191 if (!FLAC__file_decoder_get_decode_position(decoder, &decode_position))
\r
192 decode_position = 0;
\r
197 unsigned FLAC_plugin__decode(FLAC__FileDecoder *decoder, file_info_struct *file_info, char *sample_buffer)
\r
199 /* fill reservoir */
\r
200 while (wide_samples_in_reservoir_ < SAMPLES_PER_WRITE)
\r
202 if (FLAC__file_decoder_get_state(decoder) == FLAC__FILE_DECODER_END_OF_FILE)
\r
204 file_info->eof = true;
\r
207 else if (!FLAC__file_decoder_process_single(decoder))
\r
209 FLAC_plugin__show_error("Error while processing frame (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);
\r
210 file_info->eof = true;
\r
213 if (!FLAC__file_decoder_get_decode_position(decoder, &decode_position))
\r
214 decode_position = 0;
\r
216 /* output samples */
\r
217 if (wide_samples_in_reservoir_ > 0)
\r
219 const unsigned n = min(wide_samples_in_reservoir_, SAMPLES_PER_WRITE);
\r
220 const unsigned channels = file_info->channels;
\r
224 if (cfg.replaygain.enable && file_info->has_replaygain)
\r
226 bytes = FLAC__replaygain_synthesis__apply_gain(
\r
228 true, /* little_endian_data_out */
\r
229 file_info->output_bits_per_sample == 8, /* unsigned_data_out */
\r
233 file_info->bits_per_sample,
\r
234 file_info->output_bits_per_sample,
\r
235 file_info->replay_scale,
\r
236 cfg.replaygain.hard_limit,
\r
237 cfg.resolution.replaygain.dither,
\r
238 &file_info->dither_context
\r
243 bytes = FLAC__plugin_common__pack_pcm_signed_little_endian(
\r
248 file_info->bits_per_sample,
\r
249 file_info->output_bits_per_sample
\r
253 wide_samples_in_reservoir_ -= n;
\r
254 for (i = 0; i < channels; i++)
\r
255 memmove(&reservoir_[i][0], &reservoir_[i][n], sizeof(reservoir_[0][0]) * wide_samples_in_reservoir_);
\r
261 file_info->eof = true;
\r
266 int FLAC_plugin__get_rate(unsigned written_time, unsigned output_time, file_info_struct *file_info)
\r
268 static int bitrate_history_[BITRATE_HIST_SIZE];
\r
269 unsigned bh_index_w = (written_time/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;
\r
270 unsigned bh_index_o = (output_time/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;
\r
272 /* written bitrate */
\r
273 if (bh_index_w != bh_index_last_w)
\r
275 bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE-1)%BITRATE_HIST_SIZE] =
\r
276 decode_position>decode_position_last && written_time > written_time_last ?
\r
277 (unsigned)(8000*(decode_position - decode_position_last)/(written_time - written_time_last)) :
\r
278 file_info->average_bps;
\r
280 bh_index_last_w = bh_index_w;
\r
281 written_time_last = written_time;
\r
282 decode_position_last = decode_position;
\r
285 /* output bitrate */
\r
286 if (bh_index_o!=bh_index_last_o && bh_index_o!=bh_index_last_w)
\r
288 bh_index_last_o = bh_index_o;
\r
289 return bitrate_history_[bh_index_o];
\r