1
1
/* in_flac - Winamp2 FLAC input plugin
2
* Copyright (C) 2000,2001,2002,2003,2004,2005 Josh Coalson
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.
9
* This program is distributed in the hope that it will be useful,
2
* Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
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.
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
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.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
#include <limits.h> /* for INT_MAX */
19
24
#include <stdlib.h>
20
25
#include <string.h> /* for memmove() */
21
26
#include "playback.h"
53
58
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
56
static void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
61
static void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
58
file_info_struct *file_info = (file_info_struct*)client_data;
63
stream_data_struct *stream_data = (stream_data_struct*)client_data;
61
66
if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
63
FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
64
file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xfffffffful);
65
file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
66
file_info->channels = metadata->data.stream_info.channels;
67
file_info->sample_rate = metadata->data.stream_info.sample_rate;
68
stream_data->total_samples = metadata->data.stream_info.total_samples;
69
stream_data->bits_per_sample = metadata->data.stream_info.bits_per_sample;
70
stream_data->channels = metadata->data.stream_info.channels;
71
stream_data->sample_rate = metadata->data.stream_info.sample_rate;
69
if (file_info->bits_per_sample!=8 && file_info->bits_per_sample!=16 && file_info->bits_per_sample!=24)
73
if (stream_data->bits_per_sample!=8 && stream_data->bits_per_sample!=16 && stream_data->bits_per_sample!=24)
71
75
FLAC_plugin__show_error("This plugin can only handle 8/16/24-bit samples.");
72
file_info->abort_flag = true;
76
stream_data->abort_flag = true;
75
file_info->length_in_msec = (unsigned)((double)file_info->total_samples / (double)file_info->sample_rate * 1000.0 + 0.5);
81
/* with VC++ you have to spoon feed it the casting from uint64->int64->double */
82
FLAC__uint64 l = (FLAC__uint64)((double)(FLAC__int64)stream_data->total_samples / (double)stream_data->sample_rate * 1000.0 + 0.5);
85
stream_data->length_in_msec = (int)l;
77
88
else if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
80
if (grabbag__replaygain_load_from_vorbiscomment(metadata, cfg.replaygain.album_mode, &gain, &peak))
90
double reference, gain, peak;
91
if (grabbag__replaygain_load_from_vorbiscomment(metadata, cfg.replaygain.album_mode, /*strict=*/false, &reference, &gain, &peak))
82
file_info->has_replaygain = true;
83
file_info->replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)cfg.replaygain.preamp, !cfg.replaygain.hard_limit);
93
stream_data->has_replaygain = true;
94
stream_data->replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)cfg.replaygain.preamp, !cfg.replaygain.hard_limit);
88
static void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
99
static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
90
file_info_struct *file_info = (file_info_struct*)client_data;
101
stream_data_struct *stream_data = (stream_data_struct*)client_data;
93
104
if (cfg.misc.stop_err || status!=FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
94
file_info->abort_flag = true;
105
stream_data->abort_flag = true;
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)
112
FLAC__bool FLAC_plugin__decoder_init(FLAC__StreamDecoder *decoder, const char *filename, FLAC__int64 filesize, stream_data_struct *stream_data, output_config_t *config)
114
FLAC__StreamDecoderInitStatus init_status;
103
116
FLAC__ASSERT(decoder);
104
117
FLAC_plugin__decoder_finish(decoder);
105
118
/* init decoder */
106
FLAC__file_decoder_set_md5_checking(decoder, false);
107
FLAC__file_decoder_set_filename(decoder, filename);
108
FLAC__file_decoder_set_metadata_ignore_all(decoder);
109
FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
110
FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
111
FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback);
112
FLAC__file_decoder_set_write_callback(decoder, write_callback);
113
FLAC__file_decoder_set_error_callback(decoder, error_callback);
114
FLAC__file_decoder_set_client_data(decoder, file_info);
119
FLAC__stream_decoder_set_md5_checking(decoder, false);
120
FLAC__stream_decoder_set_metadata_ignore_all(decoder);
121
FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
122
FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
116
if (FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
124
if ((init_status = FLAC__stream_decoder_init_file(decoder, filename, write_callback, metadata_callback, error_callback, /*client_data=*/stream_data)) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
118
FLAC_plugin__show_error("Error while initializing decoder (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);
126
FLAC_plugin__show_error("Error while initializing decoder (%s [%s]).", FLAC__StreamDecoderInitStatusString[init_status], FLAC__stream_decoder_get_resolved_state_string(decoder));
123
131
wide_samples_in_reservoir_ = 0;
124
file_info->is_playing = false;
125
file_info->abort_flag = false;
126
file_info->has_replaygain = false;
132
stream_data->is_playing = false;
133
stream_data->abort_flag = false;
134
stream_data->has_replaygain = false;
128
if (!FLAC__file_decoder_process_until_end_of_metadata(decoder))
136
if (!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
130
FLAC_plugin__show_error("Error while processing metadata (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);
138
FLAC_plugin__show_error("Error while processing metadata (%s).", FLAC__stream_decoder_get_resolved_state_string(decoder));
133
141
/* check results */
134
if (file_info->abort_flag) return false; /* metadata callback already popped up the error dialog */
142
if (stream_data->abort_flag) return false; /* metadata callback already popped up the error dialog */
135
143
/* init replaygain */
136
file_info->output_bits_per_sample = file_info->has_replaygain && cfg.replaygain.enable ?
144
stream_data->output_bits_per_sample = stream_data->has_replaygain && cfg.replaygain.enable ?
137
145
cfg.resolution.replaygain.bps_out :
138
cfg.resolution.normal.dither_24_to_16 ? min(file_info->bits_per_sample, 16) : file_info->bits_per_sample;
146
cfg.resolution.normal.dither_24_to_16 ? min(stream_data->bits_per_sample, 16) : stream_data->bits_per_sample;
140
if (file_info->has_replaygain && cfg.replaygain.enable && cfg.resolution.replaygain.dither)
141
FLAC__replaygain_synthesis__init_dither_context(&file_info->dither_context, file_info->bits_per_sample, cfg.resolution.replaygain.noise_shaping);
148
if (stream_data->has_replaygain && cfg.replaygain.enable && cfg.resolution.replaygain.dither)
149
FLAC__replaygain_synthesis__init_dither_context(&stream_data->dither_context, stream_data->bits_per_sample, cfg.resolution.replaygain.noise_shaping);
143
file_info->eof = false;
144
file_info->seek_to = -1;
145
file_info->is_playing = true;
146
file_info->average_bps = (unsigned)(filesize / (125.*file_info->total_samples/file_info->sample_rate));
151
stream_data->eof = false;
152
stream_data->seek_to = -1;
153
stream_data->is_playing = true;
154
stream_data->average_bps = (unsigned)(filesize / (125.*(double)(FLAC__int64)stream_data->total_samples/(double)stream_data->sample_rate));
148
156
bh_index_last_w = 0;
149
157
bh_index_last_o = BITRATE_HIST_SIZE;
176
int FLAC_plugin__seek(FLAC__FileDecoder *decoder, file_info_struct *file_info)
184
int FLAC_plugin__seek(FLAC__StreamDecoder *decoder, stream_data_struct *stream_data)
179
const FLAC__uint64 target_sample =
180
(FLAC__uint64)file_info->total_samples*file_info->seek_to / file_info->length_in_msec;
182
if (!FLAC__file_decoder_seek_absolute(decoder, target_sample))
185
file_info->seek_to = -1;
186
file_info->eof = false;
187
FLAC__uint64 target_sample = stream_data->total_samples * stream_data->seek_to / stream_data->length_in_msec;
189
if (stream_data->total_samples > 0 && target_sample >= stream_data->total_samples && target_sample > 0)
190
target_sample = stream_data->total_samples - 1;
192
/* even if the seek fails we have to reset these so that we don't repeat the seek */
193
stream_data->seek_to = -1;
194
stream_data->eof = false;
187
195
wide_samples_in_reservoir_ = 0;
188
pos = (int)(target_sample*1000 / file_info->sample_rate);
196
pos = (int)(target_sample*1000 / stream_data->sample_rate);
198
if (!FLAC__stream_decoder_seek_absolute(decoder, target_sample)) {
199
if(FLAC__stream_decoder_get_state(decoder) == FLAC__STREAM_DECODER_SEEK_ERROR)
200
FLAC__stream_decoder_flush(decoder);
190
204
bh_index_last_o = bh_index_last_w = (pos/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;
191
if (!FLAC__file_decoder_get_decode_position(decoder, &decode_position))
205
if (!FLAC__stream_decoder_get_decode_position(decoder, &decode_position))
192
206
decode_position = 0;
197
unsigned FLAC_plugin__decode(FLAC__FileDecoder *decoder, file_info_struct *file_info, char *sample_buffer)
211
unsigned FLAC_plugin__decode(FLAC__StreamDecoder *decoder, stream_data_struct *stream_data, char *sample_buffer)
199
213
/* fill reservoir */
200
214
while (wide_samples_in_reservoir_ < SAMPLES_PER_WRITE)
202
if (FLAC__file_decoder_get_state(decoder) == FLAC__FILE_DECODER_END_OF_FILE)
204
file_info->eof = true;
207
else if (!FLAC__file_decoder_process_single(decoder))
209
FLAC_plugin__show_error("Error while processing frame (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]);
210
file_info->eof = true;
213
if (!FLAC__file_decoder_get_decode_position(decoder, &decode_position))
216
if (FLAC__stream_decoder_get_state(decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
218
stream_data->eof = true;
221
else if (!FLAC__stream_decoder_process_single(decoder))
223
FLAC_plugin__show_error("Error while processing frame (%s).", FLAC__stream_decoder_get_resolved_state_string(decoder));
224
stream_data->eof = true;
227
if (!FLAC__stream_decoder_get_decode_position(decoder, &decode_position))
214
228
decode_position = 0;
216
230
/* output samples */
217
231
if (wide_samples_in_reservoir_ > 0)
219
233
const unsigned n = min(wide_samples_in_reservoir_, SAMPLES_PER_WRITE);
220
const unsigned channels = file_info->channels;
234
const unsigned channels = stream_data->channels;
224
if (cfg.replaygain.enable && file_info->has_replaygain)
238
if (cfg.replaygain.enable && stream_data->has_replaygain)
226
240
bytes = FLAC__replaygain_synthesis__apply_gain(
228
242
true, /* little_endian_data_out */
229
file_info->output_bits_per_sample == 8, /* unsigned_data_out */
243
stream_data->output_bits_per_sample == 8, /* unsigned_data_out */
233
file_info->bits_per_sample,
234
file_info->output_bits_per_sample,
235
file_info->replay_scale,
247
stream_data->bits_per_sample,
248
stream_data->output_bits_per_sample,
249
stream_data->replay_scale,
236
250
cfg.replaygain.hard_limit,
237
251
cfg.resolution.replaygain.dither,
238
&file_info->dither_context
252
&stream_data->dither_context