1 /* in_flac - Winamp2 FLAC input plugin
2  * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007  Josh Coalson
3  *
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.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
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
17  */
18 
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22 
23 #include <limits.h> /* for INT_MAX */
24 #include <stdlib.h>
25 #include <string.h> /* for memmove() */
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 
write_callback(const FLAC__StreamDecoder * decoder,const FLAC__Frame * frame,const FLAC__int32 * const buffer[],void * client_data)42 static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
43 {
44 	stream_data_struct *stream_data = (stream_data_struct*)client_data;
45 	const unsigned channels = stream_data->channels, wide_samples = frame->header.blocksize;
46 	unsigned channel;
47 
48 	(void)decoder;
49 
50 	if (stream_data->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 
metadata_callback(const FLAC__StreamDecoder * decoder,const FLAC__StreamMetadata * metadata,void * client_data)61 static void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
62 {
63 	stream_data_struct *stream_data = (stream_data_struct*)client_data;
64 	(void)decoder;
65 
66 	if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
67 	{
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;
72 
73 		if (stream_data->bits_per_sample!=8 && stream_data->bits_per_sample!=16 && stream_data->bits_per_sample!=24)
74 		{
75 			FLAC_plugin__show_error("This plugin can only handle 8/16/24-bit samples.");
76 			stream_data->abort_flag = true;
77 			return;
78 		}
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)stream_data->total_samples / (double)stream_data->sample_rate * 1000.0 + 0.5);
83 			if (l > INT_MAX)
84 				l = INT_MAX;
85 			stream_data->length_in_msec = (int)l;
86 		}
87 	}
88 	else if (metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT)
89 	{
90 		double reference, gain, peak;
91 		if (grabbag__replaygain_load_from_vorbiscomment(metadata, cfg.replaygain.album_mode, /*strict=*/false, &reference, &gain, &peak))
92 		{
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);
95 		}
96 	}
97 }
98 
error_callback(const FLAC__StreamDecoder * decoder,FLAC__StreamDecoderErrorStatus status,void * client_data)99 static void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
100 {
101 	stream_data_struct *stream_data = (stream_data_struct*)client_data;
102 	(void)decoder;
103 
104 	if (cfg.misc.stop_err || status!=FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
105 		stream_data->abort_flag = true;
106 }
107 
108 /*
109  *  init/delete
110  */
111 
FLAC_plugin__decoder_init(FLAC__StreamDecoder * decoder,const char * filename,FLAC__int64 filesize,stream_data_struct * stream_data,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)
113 {
114 	FLAC__StreamDecoderInitStatus init_status;
115 
116 	FLAC__ASSERT(decoder);
117 	FLAC_plugin__decoder_finish(decoder);
118 	/* init decoder */
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);
123 
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)
125 	{
126 		FLAC_plugin__show_error("Error while initializing decoder (%s [%s]).", FLAC__StreamDecoderInitStatusString[init_status], FLAC__stream_decoder_get_resolved_state_string(decoder));
127 		return false;
128 	}
129 	/* process */
130 	cfg = *config;
131 	wide_samples_in_reservoir_ = 0;
132 	stream_data->is_playing = false;
133 	stream_data->abort_flag = false;
134 	stream_data->has_replaygain = false;
135 
136 	if (!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
137 	{
138 		FLAC_plugin__show_error("Error while processing metadata (%s).", FLAC__stream_decoder_get_resolved_state_string(decoder));
139 		return false;
140 	}
141 	/* check results */
142 	if (stream_data->abort_flag) return false;                /* metadata callback already popped up the error dialog */
143 	/* init replaygain */
144 	stream_data->output_bits_per_sample = stream_data->has_replaygain && cfg.replaygain.enable ?
145 		cfg.resolution.replaygain.bps_out :
146 		cfg.resolution.normal.dither_24_to_16 ? min(stream_data->bits_per_sample, 16) : stream_data->bits_per_sample;
147 
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);
150 	/* more inits */
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));
155 
156 	bh_index_last_w = 0;
157 	bh_index_last_o = BITRATE_HIST_SIZE;
158 	decode_position = 0;
159 	decode_position_last = 0;
160 	written_time_last = 0;
161 
162 	return true;
163 }
164 
FLAC_plugin__decoder_finish(FLAC__StreamDecoder * decoder)165 void FLAC_plugin__decoder_finish(FLAC__StreamDecoder *decoder)
166 {
167 	if (decoder && FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_UNINITIALIZED)
168 		(void)FLAC__stream_decoder_finish(decoder);
169 }
170 
FLAC_plugin__decoder_delete(FLAC__StreamDecoder * decoder)171 void FLAC_plugin__decoder_delete(FLAC__StreamDecoder *decoder)
172 {
173 	if (decoder)
174 	{
175 		FLAC_plugin__decoder_finish(decoder);
176 		FLAC__stream_decoder_delete(decoder);
177 	}
178 }
179 
180 /*
181  *  decode
182  */
183 
FLAC_plugin__seek(FLAC__StreamDecoder * decoder,stream_data_struct * stream_data)184 int FLAC_plugin__seek(FLAC__StreamDecoder *decoder, stream_data_struct *stream_data)
185 {
186 	int pos;
187 	FLAC__uint64 target_sample = stream_data->total_samples * stream_data->seek_to / stream_data->length_in_msec;
188 
189 	if (stream_data->total_samples > 0 && target_sample >= stream_data->total_samples && target_sample > 0)
190 		target_sample = stream_data->total_samples - 1;
191 
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;
195 	wide_samples_in_reservoir_ = 0;
196 	pos = (int)(target_sample*1000 / stream_data->sample_rate);
197 
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);
201 		pos = -1;
202 	}
203 
204 	bh_index_last_o = bh_index_last_w = (pos/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;
205 	if (!FLAC__stream_decoder_get_decode_position(decoder, &decode_position))
206 		decode_position = 0;
207 
208 	return pos;
209 }
210 
FLAC_plugin__decode(FLAC__StreamDecoder * decoder,stream_data_struct * stream_data,char * sample_buffer)211 unsigned FLAC_plugin__decode(FLAC__StreamDecoder *decoder, stream_data_struct *stream_data, char *sample_buffer)
212 {
213 	/* fill reservoir */
214 	while (wide_samples_in_reservoir_ < SAMPLES_PER_WRITE)
215 	{
216 		if (FLAC__stream_decoder_get_state(decoder) == FLAC__STREAM_DECODER_END_OF_STREAM)
217 		{
218 			stream_data->eof = true;
219 			break;
220 		}
221 		else if (!FLAC__stream_decoder_process_single(decoder))
222 		{
223 			FLAC_plugin__show_error("Error while processing frame (%s).", FLAC__stream_decoder_get_resolved_state_string(decoder));
224 			stream_data->eof = true;
225 			break;
226 		}
227 		if (!FLAC__stream_decoder_get_decode_position(decoder, &decode_position))
228 			decode_position = 0;
229 	}
230 	/* output samples */
231 	if (wide_samples_in_reservoir_ > 0)
232 	{
233 		const unsigned n = min(wide_samples_in_reservoir_, SAMPLES_PER_WRITE);
234 		const unsigned channels = stream_data->channels;
235 		unsigned i;
236 		int bytes;
237 
238 		if (cfg.replaygain.enable && stream_data->has_replaygain)
239 		{
240 			bytes = FLAC__replaygain_synthesis__apply_gain(
241 				sample_buffer,
242 				true, /* little_endian_data_out */
243 				stream_data->output_bits_per_sample == 8, /* unsigned_data_out */
244 				reservoir__,
245 				n,
246 				channels,
247 				stream_data->bits_per_sample,
248 				stream_data->output_bits_per_sample,
249 				stream_data->replay_scale,
250 				cfg.replaygain.hard_limit,
251 				cfg.resolution.replaygain.dither,
252 				&stream_data->dither_context
253 			);
254 		}
255 		else
256 		{
257 			bytes = FLAC__plugin_common__pack_pcm_signed_little_endian(
258 				sample_buffer,
259 				reservoir__,
260 				n,
261 				channels,
262 				stream_data->bits_per_sample,
263 				stream_data->output_bits_per_sample
264 			);
265 		}
266 
267 		wide_samples_in_reservoir_ -= n;
268 		for (i = 0; i < channels; i++)
269 			memmove(&reservoir_[i][0], &reservoir_[i][n], sizeof(reservoir_[0][0]) * wide_samples_in_reservoir_);
270 
271 		return bytes;
272 	}
273 	else
274 	{
275 		stream_data->eof = true;
276 		return 0;
277 	}
278 }
279 
FLAC_plugin__get_rate(unsigned written_time,unsigned output_time,stream_data_struct * stream_data)280 int FLAC_plugin__get_rate(unsigned written_time, unsigned output_time, stream_data_struct *stream_data)
281 {
282 	static int bitrate_history_[BITRATE_HIST_SIZE];
283 	unsigned bh_index_w = (written_time/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;
284 	unsigned bh_index_o = (output_time/BITRATE_HIST_SEGMENT_MSEC) % BITRATE_HIST_SIZE;
285 
286 	/* written bitrate */
287 	if (bh_index_w != bh_index_last_w)
288 	{
289 		bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE-1)%BITRATE_HIST_SIZE] =
290 			decode_position>decode_position_last && written_time > written_time_last ?
291 			(unsigned)(8000*(decode_position - decode_position_last)/(written_time - written_time_last)) :
292 			stream_data->average_bps;
293 
294 		bh_index_last_w = bh_index_w;
295 		written_time_last = written_time;
296 		decode_position_last = decode_position;
297 	}
298 
299 	/* output bitrate */
300 	if (bh_index_o!=bh_index_last_o && bh_index_o!=bh_index_last_w)
301 	{
302 		bh_index_last_o = bh_index_o;
303 		return bitrate_history_[bh_index_o];
304 	}
305 
306 	return 0;
307 }
308