1 /* libxmms-flac - XMMS FLAC input plugin
2 * Copyright (C) 2000,2001,2002 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,
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.
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.
25 #include <xmms/plugin.h>
26 #include <xmms/util.h>
27 #include <xmms/configfile.h>
28 #include <xmms/titlestring.h>
34 #ifdef HAVE_LANGINFO_CODESET
39 #include "plugin_common/all.h"
40 #include "share/grabbag.h"
41 #include "configure.h"
48 #define min(x,y) ((x)<(y)?(x):(y))
52 FLAC__bool abort_flag;
53 FLAC__bool is_playing;
55 FLAC__bool play_thread_open; /* if true, is_playing must also be true */
56 unsigned total_samples;
57 unsigned bits_per_sample;
60 unsigned length_in_msec;
62 AFormat sample_format;
64 FLAC__bool has_replaygain;
66 DitherContext dither_context;
69 static void FLAC_XMMS__init();
70 static int FLAC_XMMS__is_our_file(char *filename);
71 static void FLAC_XMMS__play_file(char *filename);
72 static void FLAC_XMMS__stop();
73 static void FLAC_XMMS__pause(short p);
74 static void FLAC_XMMS__seek(int time);
75 static int FLAC_XMMS__get_time();
76 static void FLAC_XMMS__cleanup();
77 static void FLAC_XMMS__get_song_info(char *filename, char **title, int *length);
79 static void *play_loop_(void *arg);
80 static FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder);
81 static void safe_decoder_finish_(FLAC__FileDecoder *decoder);
82 static void safe_decoder_delete_(FLAC__FileDecoder *decoder);
83 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
84 static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
85 static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
91 "Reference FLAC Player v" VERSION,
95 FLAC_XMMS__is_our_file,
110 FLAC_XMMS__get_song_info,
111 NULL, /* file_info_box */
115 #define SAMPLES_PER_WRITE 512
116 static FLAC__int32 reservoir_[FLAC__MAX_BLOCK_SIZE * 2/*for overflow*/ * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
117 static FLAC__byte sample_buffer_[SAMPLES_PER_WRITE * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8)]; /* (24/8) for max bytes per sample */
118 static unsigned wide_samples_in_reservoir_ = 0;
120 static FLAC__FileDecoder *decoder_ = 0;
121 static file_info_struct file_info_;
122 static pthread_t decode_thread_;
123 static FLAC__bool audio_error_ = false;
125 #define BITRATE_HIST_SEGMENT_MSEC 500
126 /* 500ms * 50 = 25s should be enough */
127 #define BITRATE_HIST_SIZE 50
128 static unsigned bitrate_history_[BITRATE_HIST_SIZE];
131 InputPlugin *get_iplugin_info()
133 flac_ip.description = g_strdup_printf("Reference FLAC Player v%s", FLAC__VERSION_STRING);
137 void FLAC_XMMS__init()
141 flac_cfg.title.tag_override = FALSE;
142 g_free(flac_cfg.title.tag_format);
143 flac_cfg.title.convert_char_set = FALSE;
145 cfg = xmms_cfg_open_default_file();
149 xmms_cfg_read_boolean(cfg, "flac", "title.tag_override", &flac_cfg.title.tag_override);
151 if(!xmms_cfg_read_string(cfg, "flac", "title.tag_format", &flac_cfg.title.tag_format))
152 flac_cfg.title.tag_format = g_strdup("%p - %t");
154 xmms_cfg_read_boolean(cfg, "flac", "title.convert_char_set", &flac_cfg.title.convert_char_set);
156 if(!xmms_cfg_read_string(cfg, "flac", "title.file_char_set", &flac_cfg.title.file_char_set))
157 flac_cfg.title.file_char_set = FLAC_plugin__charset_get_current();
159 if(!xmms_cfg_read_string(cfg, "flac", "title.user_char_set", &flac_cfg.title.user_char_set))
160 flac_cfg.title.user_char_set = FLAC_plugin__charset_get_current();
164 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.enable", &flac_cfg.output.replaygain.enable);
166 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.album_mode", &flac_cfg.output.replaygain.album_mode);
168 if(!xmms_cfg_read_int(cfg, "flac", "output.replaygain.preamp", &flac_cfg.output.replaygain.preamp))
169 flac_cfg.output.replaygain.preamp = 0;
171 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.hard_limit", &flac_cfg.output.replaygain.hard_limit);
173 xmms_cfg_read_boolean(cfg, "flac", "output.resolution.normal.dither_24_to_16", &flac_cfg.output.resolution.normal.dither_24_to_16);
174 xmms_cfg_read_boolean(cfg, "flac", "output.resolution.replaygain.dither", &flac_cfg.output.resolution.replaygain.dither);
176 if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.noise_shaping", &flac_cfg.output.resolution.replaygain.noise_shaping))
177 flac_cfg.output.resolution.replaygain.noise_shaping = 1;
179 if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.bps_out", &flac_cfg.output.resolution.replaygain.bps_out))
180 flac_cfg.output.resolution.replaygain.bps_out = 16;
182 decoder_ = FLAC__file_decoder_new();
185 int FLAC_XMMS__is_our_file(char *filename)
189 ext = strrchr(filename, '.');
191 if(!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
196 void FLAC_XMMS__play_file(char *filename)
200 wide_samples_in_reservoir_ = 0;
201 audio_error_ = false;
202 file_info_.abort_flag = false;
203 file_info_.is_playing = false;
204 file_info_.eof = false;
205 file_info_.play_thread_open = false;
206 file_info_.has_replaygain = false;
208 if(0 == (f = fopen(filename, "r")))
215 if(!safe_decoder_init_(filename, decoder_))
218 if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable && flac_cfg.output.resolution.replaygain.dither)
219 FLAC__plugin_common__init_dither_context(&file_info_.dither_context, file_info_.bits_per_sample, flac_cfg.output.resolution.replaygain.noise_shaping);
221 file_info_.is_playing = true;
223 if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
225 safe_decoder_finish_(decoder_);
229 file_info_.title = flac_format_song_title(filename);
230 flac_ip.set_info(file_info_.title, file_info_.length_in_msec, file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample, file_info_.sample_rate, file_info_.channels);
232 file_info_.seek_to_in_sec = -1;
233 file_info_.play_thread_open = true;
234 pthread_create(&decode_thread_, NULL, play_loop_, NULL);
237 void FLAC_XMMS__stop()
239 if(file_info_.is_playing) {
240 file_info_.is_playing = false;
241 if(file_info_.play_thread_open) {
242 file_info_.play_thread_open = false;
243 pthread_join(decode_thread_, NULL);
245 flac_ip.output->close_audio();
246 safe_decoder_finish_(decoder_);
250 void FLAC_XMMS__pause(short p)
252 flac_ip.output->pause(p);
255 void FLAC_XMMS__seek(int time)
257 file_info_.seek_to_in_sec = time;
258 file_info_.eof = false;
260 while(file_info_.seek_to_in_sec != -1)
264 int FLAC_XMMS__get_time()
268 if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
271 return flac_ip.output->output_time();
274 void FLAC_XMMS__cleanup()
276 safe_decoder_delete_(decoder_);
280 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
282 FLAC__StreamMetadata streaminfo;
287 if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
288 /* @@@ how to report the error? */
290 static const char *errtitle = "Invalid FLAC File: ";
291 *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
292 sprintf(*title, "%s\"%s\"", errtitle, filename);
295 *length_in_msec = -1;
300 *title = flac_format_song_title(filename);
303 *length_in_msec = streaminfo.data.stream_info.total_samples * 10 / (streaminfo.data.stream_info.sample_rate / 100);
306 /***********************************************************************
308 **********************************************************************/
310 void *play_loop_(void *arg)
312 unsigned written_time_last = 0, bh_index_last_w = 0, bh_index_last_o = BITRATE_HIST_SIZE;
313 FLAC__uint64 decode_position_last = 0;
317 while(file_info_.is_playing) {
318 if(!file_info_.eof) {
319 while(wide_samples_in_reservoir_ < SAMPLES_PER_WRITE) {
320 if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
321 file_info_.eof = true;
324 else if(!FLAC__file_decoder_process_single(decoder_)) {
325 /*@@@ this should probably be a dialog */
326 fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
327 file_info_.eof = true;
331 if(wide_samples_in_reservoir_ > 0) {
332 const unsigned channels = file_info_.channels;
333 const unsigned bits_per_sample = file_info_.bits_per_sample;
334 const unsigned n = min(wide_samples_in_reservoir_, SAMPLES_PER_WRITE);
335 const unsigned delta = n * channels;
337 unsigned i, written_time, bh_index_w;
338 FLAC__uint64 decode_position;
340 if(flac_cfg.output.replaygain.enable && file_info_.has_replaygain) {
341 bytes = (int)FLAC__plugin_common__apply_gain(
347 flac_cfg.output.resolution.replaygain.bps_out,
348 file_info_.replay_scale,
349 flac_cfg.output.replaygain.hard_limit,
350 flac_cfg.output.resolution.replaygain.dither,
351 (NoiseShaping)flac_cfg.output.resolution.replaygain.noise_shaping,
352 &file_info_.dither_context
356 bytes = (int)FLAC__plugin_common__pack_pcm_signed_little_endian(
362 flac_cfg.output.resolution.normal.dither_24_to_16?
363 min(bits_per_sample, 16) :
368 for(i = delta; i < wide_samples_in_reservoir_ * channels; i++)
369 reservoir_[i-delta] = reservoir_[i];
370 wide_samples_in_reservoir_ -= n;
372 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, channels, bytes, sample_buffer_);
373 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
375 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
376 flac_ip.output->write_audio(sample_buffer_, bytes);
378 /* compute current bitrate */
380 written_time = flac_ip.output->written_time();
381 bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE ;
382 if(bh_index_w != bh_index_last_w && wide_samples_in_reservoir_ < SAMPLES_PER_WRITE) {
383 bh_index_last_w = bh_index_w;
384 if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position))
387 bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] =
388 decode_position > decode_position_last && written_time > written_time_last ?
389 8000 * (decode_position - decode_position_last) / (written_time - written_time_last) :
390 file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample;
391 decode_position_last = decode_position;
392 written_time_last = written_time;
396 file_info_.eof = true;
402 if(file_info_.seek_to_in_sec != -1) {
403 const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
404 unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
405 if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
406 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
407 bh_index_last_w = bh_index_last_o = file_info_.seek_to_in_sec * 1000 / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
408 file_info_.seek_to_in_sec = -1;
409 file_info_.eof = false;
410 wide_samples_in_reservoir_ = 0;
414 /* display the right bitrate from history */
415 unsigned bh_index_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
416 if(bh_index_o != bh_index_last_o && bh_index_last_w != bh_index_o) {
417 bh_index_last_o = bh_index_o;
418 flac_ip.set_info(file_info_.title, file_info_.length_in_msec, bitrate_history_[bh_index_o], file_info_.sample_rate, file_info_.channels);
423 safe_decoder_finish_(decoder_);
425 /* are these two calls necessary? */
426 flac_ip.output->buffer_free();
427 flac_ip.output->buffer_free();
429 g_free(file_info_.title);
432 return 0; /* to silence the compiler warning about not returning a value */
435 FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder)
440 safe_decoder_finish_(decoder);
442 FLAC__file_decoder_set_md5_checking(decoder, false);
443 FLAC__file_decoder_set_filename(decoder, filename);
444 FLAC__file_decoder_set_metadata_ignore_all(decoder);
445 FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
446 FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
447 FLAC__file_decoder_set_write_callback(decoder, write_callback_);
448 FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
449 FLAC__file_decoder_set_error_callback(decoder, error_callback_);
450 FLAC__file_decoder_set_client_data(decoder, &file_info_);
451 if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
454 if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
460 void safe_decoder_finish_(FLAC__FileDecoder *decoder)
462 if(decoder && FLAC__file_decoder_get_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
463 FLAC__file_decoder_finish(decoder);
466 void safe_decoder_delete_(FLAC__FileDecoder *decoder)
469 safe_decoder_finish_(decoder);
470 FLAC__file_decoder_delete(decoder);
474 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
476 file_info_struct *file_info = (file_info_struct *)client_data;
477 const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
478 unsigned wide_sample, offset_sample, channel;
482 if(file_info->abort_flag)
483 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
485 for(offset_sample = wide_samples_in_reservoir_ * channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
486 for(channel = 0; channel < channels; channel++, offset_sample++)
487 reservoir_[offset_sample] = buffer[channel][wide_sample];
489 wide_samples_in_reservoir_ += wide_samples;
491 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
494 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
496 file_info_struct *file_info = (file_info_struct *)client_data;
498 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
499 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
500 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
501 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
502 file_info->channels = metadata->data.stream_info.channels;
503 file_info->sample_rate = metadata->data.stream_info.sample_rate;
505 #ifdef FLAC__DO_DITHER
506 if(file_info->bits_per_sample == 8) {
507 file_info->sample_format = FMT_S8;
509 else if(file_info->bits_per_sample == 16 || file_info->bits_per_sample == 24) {
510 file_info->sample_format = FMT_S16_LE;
513 /*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16/24-bit samples\n", "ERROR: plugin can only handle 8/16/24-bit samples", 0); */
514 file_info->abort_flag = true;
518 if(file_info->bits_per_sample == 8) {
519 file_info->sample_format = FMT_S8;
521 else if(file_info->bits_per_sample == 16) {
522 file_info->sample_format = FMT_S16_LE;
525 /*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16-bit samples\n", "ERROR: plugin can only handle 8/16-bit samples", 0); */
526 file_info->abort_flag = true;
530 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
532 else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
534 if(grabbag__replaygain_load_from_vorbiscomment(metadata, flac_cfg.output.replaygain.album_mode, &gain, &peak)) {
535 file_info_.has_replaygain = true;
536 file_info_.replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)flac_cfg.output.replaygain.preamp, /*prevent_clipping=*/!flac_cfg.output.replaygain.hard_limit);
541 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
543 file_info_struct *file_info = (file_info_struct *)client_data;
545 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
546 file_info->abort_flag = true;