1 /* libxmms-flac - XMMS FLAC input plugin
2 * Copyright (C) 2000,2001,2002,2003 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))
50 extern void FLAC_XMMS__file_info_box(char *filename);
53 FLAC__bool abort_flag;
54 FLAC__bool is_playing;
56 FLAC__bool play_thread_open; /* if true, is_playing must also be true */
57 unsigned total_samples;
58 unsigned bits_per_sample;
61 unsigned length_in_msec;
63 AFormat sample_format;
64 unsigned sample_format_bytes_per_sample;
66 FLAC__bool has_replaygain;
68 DitherContext dither_context;
71 static void FLAC_XMMS__init();
72 static int FLAC_XMMS__is_our_file(char *filename);
73 static void FLAC_XMMS__play_file(char *filename);
74 static void FLAC_XMMS__stop();
75 static void FLAC_XMMS__pause(short p);
76 static void FLAC_XMMS__seek(int time);
77 static int FLAC_XMMS__get_time();
78 static void FLAC_XMMS__cleanup();
79 static void FLAC_XMMS__get_song_info(char *filename, char **title, int *length);
81 static void *play_loop_(void *arg);
82 static FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder);
83 static void safe_decoder_finish_(FLAC__FileDecoder *decoder);
84 static void safe_decoder_delete_(FLAC__FileDecoder *decoder);
85 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
86 static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
87 static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
93 "Reference FLAC Player v" VERSION,
97 FLAC_XMMS__is_our_file,
112 FLAC_XMMS__get_song_info,
113 FLAC_XMMS__file_info_box,
117 #define SAMPLES_PER_WRITE 512
118 #define SAMPLE_BUFFER_SIZE ((FLAC__MAX_BLOCK_SIZE + SAMPLES_PER_WRITE) * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8))
119 static FLAC__byte sample_buffer_[SAMPLE_BUFFER_SIZE];
120 static unsigned sample_buffer_first_, sample_buffer_last_;
122 static FLAC__FileDecoder *decoder_ = 0;
123 static file_info_struct file_info_;
124 static pthread_t decode_thread_;
125 static FLAC__bool audio_error_ = false;
127 #define BITRATE_HIST_SEGMENT_MSEC 500
128 /* 500ms * 50 = 25s should be enough */
129 #define BITRATE_HIST_SIZE 50
130 static unsigned bitrate_history_[BITRATE_HIST_SIZE];
133 InputPlugin *get_iplugin_info()
135 flac_ip.description = g_strdup_printf("Reference FLAC Player v%s", FLAC__VERSION_STRING);
139 void FLAC_XMMS__init()
143 flac_cfg.title.tag_override = FALSE;
144 g_free(flac_cfg.title.tag_format);
145 flac_cfg.title.convert_char_set = FALSE;
147 cfg = xmms_cfg_open_default_file();
151 xmms_cfg_read_boolean(cfg, "flac", "title.tag_override", &flac_cfg.title.tag_override);
153 if(!xmms_cfg_read_string(cfg, "flac", "title.tag_format", &flac_cfg.title.tag_format))
154 flac_cfg.title.tag_format = g_strdup("%p - %t");
156 xmms_cfg_read_boolean(cfg, "flac", "title.convert_char_set", &flac_cfg.title.convert_char_set);
158 if(!xmms_cfg_read_string(cfg, "flac", "title.file_char_set", &flac_cfg.title.file_char_set))
159 flac_cfg.title.file_char_set = FLAC_plugin__charset_get_current();
161 if(!xmms_cfg_read_string(cfg, "flac", "title.user_char_set", &flac_cfg.title.user_char_set))
162 flac_cfg.title.user_char_set = FLAC_plugin__charset_get_current();
166 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.enable", &flac_cfg.output.replaygain.enable);
168 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.album_mode", &flac_cfg.output.replaygain.album_mode);
170 if(!xmms_cfg_read_int(cfg, "flac", "output.replaygain.preamp", &flac_cfg.output.replaygain.preamp))
171 flac_cfg.output.replaygain.preamp = 0;
173 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.hard_limit", &flac_cfg.output.replaygain.hard_limit);
175 xmms_cfg_read_boolean(cfg, "flac", "output.resolution.normal.dither_24_to_16", &flac_cfg.output.resolution.normal.dither_24_to_16);
176 xmms_cfg_read_boolean(cfg, "flac", "output.resolution.replaygain.dither", &flac_cfg.output.resolution.replaygain.dither);
178 if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.noise_shaping", &flac_cfg.output.resolution.replaygain.noise_shaping))
179 flac_cfg.output.resolution.replaygain.noise_shaping = 1;
181 if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.bps_out", &flac_cfg.output.resolution.replaygain.bps_out))
182 flac_cfg.output.resolution.replaygain.bps_out = 16;
184 decoder_ = FLAC__file_decoder_new();
189 int FLAC_XMMS__is_our_file(char *filename)
193 ext = strrchr(filename, '.');
195 if(!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
200 void FLAC_XMMS__play_file(char *filename)
204 sample_buffer_first_ = sample_buffer_last_ = 0;
205 audio_error_ = false;
206 file_info_.abort_flag = false;
207 file_info_.is_playing = false;
208 file_info_.eof = false;
209 file_info_.play_thread_open = false;
210 file_info_.has_replaygain = false;
212 if(0 == (f = fopen(filename, "r")))
219 if(!safe_decoder_init_(filename, decoder_))
222 if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable) {
223 if(flac_cfg.output.resolution.replaygain.bps_out == 8) {
224 file_info_.sample_format = FMT_U8;
225 file_info_.sample_format_bytes_per_sample = 1;
227 else if(flac_cfg.output.resolution.replaygain.bps_out == 16) {
228 file_info_.sample_format = FMT_S16_LE;
229 file_info_.sample_format_bytes_per_sample = 2;
232 /*@@@ 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); */
233 fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", flac_cfg.output.resolution.replaygain.bps_out);
234 safe_decoder_finish_(decoder_);
239 if(file_info_.bits_per_sample == 8) {
240 file_info_.sample_format = FMT_U8;
241 file_info_.sample_format_bytes_per_sample = 1;
243 else if(file_info_.bits_per_sample == 16 || (file_info_.bits_per_sample == 24 && flac_cfg.output.resolution.normal.dither_24_to_16)) {
244 file_info_.sample_format = FMT_S16_LE;
245 file_info_.sample_format_bytes_per_sample = 2;
248 /*@@@ 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); */
249 fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", file_info_.bits_per_sample);
250 safe_decoder_finish_(decoder_);
254 FLAC__plugin_common__init_dither_context(&file_info_.dither_context, file_info_.sample_format_bytes_per_sample * 8, flac_cfg.output.resolution.replaygain.noise_shaping);
255 file_info_.is_playing = true;
257 if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
259 safe_decoder_finish_(decoder_);
263 file_info_.title = flac_format_song_title(filename);
264 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);
266 file_info_.seek_to_in_sec = -1;
267 file_info_.play_thread_open = true;
268 pthread_create(&decode_thread_, NULL, play_loop_, NULL);
271 void FLAC_XMMS__stop()
273 if(file_info_.is_playing) {
274 file_info_.is_playing = false;
275 if(file_info_.play_thread_open) {
276 file_info_.play_thread_open = false;
277 pthread_join(decode_thread_, NULL);
279 flac_ip.output->close_audio();
280 safe_decoder_finish_(decoder_);
284 void FLAC_XMMS__pause(short p)
286 flac_ip.output->pause(p);
289 void FLAC_XMMS__seek(int time)
291 file_info_.seek_to_in_sec = time;
292 file_info_.eof = false;
294 while(file_info_.seek_to_in_sec != -1)
298 int FLAC_XMMS__get_time()
302 if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
305 return flac_ip.output->output_time();
308 void FLAC_XMMS__cleanup()
310 safe_decoder_delete_(decoder_);
314 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
316 FLAC__StreamMetadata streaminfo;
321 if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
322 /* @@@ how to report the error? */
324 static const char *errtitle = "Invalid FLAC File: ";
325 *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
326 sprintf(*title, "%s\"%s\"", errtitle, filename);
329 *length_in_msec = -1;
334 *title = flac_format_song_title(filename);
337 *length_in_msec = streaminfo.data.stream_info.total_samples * 10 / (streaminfo.data.stream_info.sample_rate / 100);
340 /***********************************************************************
342 **********************************************************************/
344 void *play_loop_(void *arg)
346 unsigned written_time_last = 0, bh_index_last_w = 0, bh_index_last_o = BITRATE_HIST_SIZE, blocksize = 1;
347 FLAC__uint64 decode_position_last = 0, decode_position_frame_last = 0, decode_position_frame = 0;
351 while(file_info_.is_playing) {
352 if(!file_info_.eof) {
353 while(sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE) {
356 s = sample_buffer_last_ - sample_buffer_first_;
357 if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
358 file_info_.eof = true;
361 else if(!FLAC__file_decoder_process_single(decoder_)) {
362 /*@@@ this should probably be a dialog */
363 fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
364 file_info_.eof = true;
367 blocksize = sample_buffer_last_ - sample_buffer_first_ - s;
368 decode_position_frame_last = decode_position_frame;
369 if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
370 decode_position_frame = 0;
372 if(sample_buffer_last_ - sample_buffer_first_ > 0) {
373 const unsigned n = min(sample_buffer_last_ - sample_buffer_first_, SAMPLES_PER_WRITE);
374 int bytes = n * file_info_.channels * file_info_.sample_format_bytes_per_sample;
375 FLAC__byte *sample_buffer_start = sample_buffer_ + sample_buffer_first_ * file_info_.channels * file_info_.sample_format_bytes_per_sample;
376 unsigned written_time, bh_index_w;
377 FLAC__uint64 decode_position;
379 sample_buffer_first_ += n;
380 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, file_info_.channels, bytes, sample_buffer_start);
381 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
383 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
384 flac_ip.output->write_audio(sample_buffer_start, bytes);
386 /* compute current bitrate */
388 written_time = flac_ip.output->written_time();
389 bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
390 if(bh_index_w != bh_index_last_w) {
391 bh_index_last_w = bh_index_w;
392 decode_position = decode_position_frame - (double)(sample_buffer_last_ - sample_buffer_first_) * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize;
393 bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] =
394 decode_position > decode_position_last && written_time > written_time_last ?
395 8000 * (decode_position - decode_position_last) / (written_time - written_time_last) :
396 file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample;
397 decode_position_last = decode_position;
398 written_time_last = written_time;
402 file_info_.eof = true;
408 if(file_info_.seek_to_in_sec != -1) {
409 const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
410 unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
411 if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
412 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
413 bh_index_last_w = bh_index_last_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
414 if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
415 decode_position_frame = 0;
416 file_info_.seek_to_in_sec = -1;
417 file_info_.eof = false;
418 sample_buffer_first_ = sample_buffer_last_ = 0;
422 /* display the right bitrate from history */
423 unsigned bh_index_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
424 if(bh_index_o != bh_index_last_o && bh_index_o != bh_index_last_w && bh_index_o != (bh_index_last_w + 1) % BITRATE_HIST_SIZE) {
425 bh_index_last_o = bh_index_o;
426 flac_ip.set_info(file_info_.title, file_info_.length_in_msec, bitrate_history_[bh_index_o], file_info_.sample_rate, file_info_.channels);
431 safe_decoder_finish_(decoder_);
433 /* are these two calls necessary? */
434 flac_ip.output->buffer_free();
435 flac_ip.output->buffer_free();
437 g_free(file_info_.title);
440 return 0; /* to silence the compiler warning about not returning a value */
443 FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder)
448 safe_decoder_finish_(decoder);
450 FLAC__file_decoder_set_md5_checking(decoder, false);
451 FLAC__file_decoder_set_filename(decoder, filename);
452 FLAC__file_decoder_set_metadata_ignore_all(decoder);
453 FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
454 FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
455 FLAC__file_decoder_set_write_callback(decoder, write_callback_);
456 FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
457 FLAC__file_decoder_set_error_callback(decoder, error_callback_);
458 FLAC__file_decoder_set_client_data(decoder, &file_info_);
459 if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
462 if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
468 void safe_decoder_finish_(FLAC__FileDecoder *decoder)
470 if(decoder && FLAC__file_decoder_get_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
471 FLAC__file_decoder_finish(decoder);
474 void safe_decoder_delete_(FLAC__FileDecoder *decoder)
477 safe_decoder_finish_(decoder);
478 FLAC__file_decoder_delete(decoder);
482 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
484 file_info_struct *file_info = (file_info_struct *)client_data;
485 const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
486 const unsigned bits_per_sample = file_info->bits_per_sample;
487 FLAC__byte *sample_buffer_start;
491 if(file_info->abort_flag)
492 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
494 if((sample_buffer_last_ + wide_samples) > (SAMPLE_BUFFER_SIZE / (channels * file_info->sample_format_bytes_per_sample))) {
495 memmove(sample_buffer_, sample_buffer_ + sample_buffer_first_ * channels * file_info->sample_format_bytes_per_sample, (sample_buffer_last_ - sample_buffer_first_) * channels * file_info->sample_format_bytes_per_sample);
496 sample_buffer_last_ -= sample_buffer_first_;
497 sample_buffer_first_ = 0;
499 sample_buffer_start = sample_buffer_ + sample_buffer_last_ * channels * file_info->sample_format_bytes_per_sample;
500 if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable) {
501 FLAC__plugin_common__apply_gain(
507 file_info->sample_format_bytes_per_sample * 8,
508 file_info_.replay_scale,
509 flac_cfg.output.replaygain.hard_limit,
510 flac_cfg.output.resolution.replaygain.dither,
511 &file_info_.dither_context
515 FLAC__plugin_common__pack_pcm_signed_little_endian(
521 file_info->sample_format_bytes_per_sample * 8
525 sample_buffer_last_ += wide_samples;
527 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
530 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
532 file_info_struct *file_info = (file_info_struct *)client_data;
534 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
535 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
536 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
537 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
538 file_info->channels = metadata->data.stream_info.channels;
539 file_info->sample_rate = metadata->data.stream_info.sample_rate;
540 file_info->length_in_msec = (FLAC__uint64)file_info->total_samples * 10 / (file_info->sample_rate / 100);
542 else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
544 if(grabbag__replaygain_load_from_vorbiscomment(metadata, flac_cfg.output.replaygain.album_mode, &gain, &peak)) {
545 file_info_.has_replaygain = true;
546 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);
551 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
553 file_info_struct *file_info = (file_info_struct *)client_data;
555 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
556 file_info->abort_flag = true;