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 "share/replaygain_synthesis.h"
42 #include "configure.h"
49 #define min(x,y) ((x)<(y)?(x):(y))
51 extern void FLAC_XMMS__file_info_box(char *filename);
54 FLAC__bool abort_flag;
55 FLAC__bool is_playing;
57 FLAC__bool play_thread_open; /* if true, is_playing must also be true */
58 unsigned total_samples;
59 unsigned bits_per_sample;
62 unsigned length_in_msec;
64 AFormat sample_format;
65 unsigned sample_format_bytes_per_sample;
67 FLAC__bool has_replaygain;
69 DitherContext dither_context;
72 static void FLAC_XMMS__init();
73 static int FLAC_XMMS__is_our_file(char *filename);
74 static void FLAC_XMMS__play_file(char *filename);
75 static void FLAC_XMMS__stop();
76 static void FLAC_XMMS__pause(short p);
77 static void FLAC_XMMS__seek(int time);
78 static int FLAC_XMMS__get_time();
79 static void FLAC_XMMS__cleanup();
80 static void FLAC_XMMS__get_song_info(char *filename, char **title, int *length);
82 static void *play_loop_(void *arg);
83 static FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder);
84 static void safe_decoder_finish_(FLAC__FileDecoder *decoder);
85 static void safe_decoder_delete_(FLAC__FileDecoder *decoder);
86 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
87 static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
88 static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
94 "Reference FLAC Player v" VERSION,
98 FLAC_XMMS__is_our_file,
100 FLAC_XMMS__play_file,
113 FLAC_XMMS__get_song_info,
114 FLAC_XMMS__file_info_box,
118 #define SAMPLES_PER_WRITE 512
119 #define SAMPLE_BUFFER_SIZE ((FLAC__MAX_BLOCK_SIZE + SAMPLES_PER_WRITE) * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8))
120 static FLAC__byte sample_buffer_[SAMPLE_BUFFER_SIZE];
121 static unsigned sample_buffer_first_, sample_buffer_last_;
123 static FLAC__FileDecoder *decoder_ = 0;
124 static file_info_struct file_info_;
125 static pthread_t decode_thread_;
126 static FLAC__bool audio_error_ = false;
127 static FLAC__bool is_big_endian_host_;
129 #define BITRATE_HIST_SEGMENT_MSEC 500
130 /* 500ms * 50 = 25s should be enough */
131 #define BITRATE_HIST_SIZE 50
132 static unsigned bitrate_history_[BITRATE_HIST_SIZE];
135 InputPlugin *get_iplugin_info()
137 flac_ip.description = g_strdup_printf("Reference FLAC Player v%s", FLAC__VERSION_STRING);
141 void FLAC_XMMS__init()
144 FLAC__uint32 test = 1;
146 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
148 flac_cfg.title.tag_override = FALSE;
149 g_free(flac_cfg.title.tag_format);
150 flac_cfg.title.convert_char_set = FALSE;
152 cfg = xmms_cfg_open_default_file();
156 xmms_cfg_read_boolean(cfg, "flac", "title.tag_override", &flac_cfg.title.tag_override);
158 if(!xmms_cfg_read_string(cfg, "flac", "title.tag_format", &flac_cfg.title.tag_format))
159 flac_cfg.title.tag_format = g_strdup("%p - %t");
161 xmms_cfg_read_boolean(cfg, "flac", "title.convert_char_set", &flac_cfg.title.convert_char_set);
163 if(!xmms_cfg_read_string(cfg, "flac", "title.file_char_set", &flac_cfg.title.file_char_set))
164 flac_cfg.title.file_char_set = FLAC_plugin__charset_get_current();
166 if(!xmms_cfg_read_string(cfg, "flac", "title.user_char_set", &flac_cfg.title.user_char_set))
167 flac_cfg.title.user_char_set = FLAC_plugin__charset_get_current();
171 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.enable", &flac_cfg.output.replaygain.enable);
173 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.album_mode", &flac_cfg.output.replaygain.album_mode);
175 if(!xmms_cfg_read_int(cfg, "flac", "output.replaygain.preamp", &flac_cfg.output.replaygain.preamp))
176 flac_cfg.output.replaygain.preamp = 0;
178 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.hard_limit", &flac_cfg.output.replaygain.hard_limit);
180 xmms_cfg_read_boolean(cfg, "flac", "output.resolution.normal.dither_24_to_16", &flac_cfg.output.resolution.normal.dither_24_to_16);
181 xmms_cfg_read_boolean(cfg, "flac", "output.resolution.replaygain.dither", &flac_cfg.output.resolution.replaygain.dither);
183 if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.noise_shaping", &flac_cfg.output.resolution.replaygain.noise_shaping))
184 flac_cfg.output.resolution.replaygain.noise_shaping = 1;
186 if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.bps_out", &flac_cfg.output.resolution.replaygain.bps_out))
187 flac_cfg.output.resolution.replaygain.bps_out = 16;
189 decoder_ = FLAC__file_decoder_new();
194 int FLAC_XMMS__is_our_file(char *filename)
198 ext = strrchr(filename, '.');
200 if(!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
205 void FLAC_XMMS__play_file(char *filename)
209 sample_buffer_first_ = sample_buffer_last_ = 0;
210 audio_error_ = false;
211 file_info_.abort_flag = false;
212 file_info_.is_playing = false;
213 file_info_.eof = false;
214 file_info_.play_thread_open = false;
215 file_info_.has_replaygain = false;
217 if(0 == (f = fopen(filename, "r")))
224 if(!safe_decoder_init_(filename, decoder_))
227 if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable) {
228 if(flac_cfg.output.resolution.replaygain.bps_out == 8) {
229 file_info_.sample_format = FMT_U8;
230 file_info_.sample_format_bytes_per_sample = 1;
232 else if(flac_cfg.output.resolution.replaygain.bps_out == 16) {
233 file_info_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
234 file_info_.sample_format_bytes_per_sample = 2;
237 /*@@@ 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); */
238 fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", flac_cfg.output.resolution.replaygain.bps_out);
239 safe_decoder_finish_(decoder_);
244 if(file_info_.bits_per_sample == 8) {
245 file_info_.sample_format = FMT_U8;
246 file_info_.sample_format_bytes_per_sample = 1;
248 else if(file_info_.bits_per_sample == 16 || (file_info_.bits_per_sample == 24 && flac_cfg.output.resolution.normal.dither_24_to_16)) {
249 file_info_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
250 file_info_.sample_format_bytes_per_sample = 2;
253 /*@@@ 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); */
254 fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", file_info_.bits_per_sample);
255 safe_decoder_finish_(decoder_);
259 FLAC__replaygain_synthesis__init_dither_context(&file_info_.dither_context, file_info_.sample_format_bytes_per_sample * 8, flac_cfg.output.resolution.replaygain.noise_shaping);
260 file_info_.is_playing = true;
262 if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
264 safe_decoder_finish_(decoder_);
268 file_info_.title = flac_format_song_title(filename);
269 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);
271 file_info_.seek_to_in_sec = -1;
272 file_info_.play_thread_open = true;
273 pthread_create(&decode_thread_, NULL, play_loop_, NULL);
276 void FLAC_XMMS__stop()
278 if(file_info_.is_playing) {
279 file_info_.is_playing = false;
280 if(file_info_.play_thread_open) {
281 file_info_.play_thread_open = false;
282 pthread_join(decode_thread_, NULL);
284 flac_ip.output->close_audio();
285 safe_decoder_finish_(decoder_);
289 void FLAC_XMMS__pause(short p)
291 flac_ip.output->pause(p);
294 void FLAC_XMMS__seek(int time)
296 file_info_.seek_to_in_sec = time;
297 file_info_.eof = false;
299 while(file_info_.seek_to_in_sec != -1)
303 int FLAC_XMMS__get_time()
307 if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
310 return flac_ip.output->output_time();
313 void FLAC_XMMS__cleanup()
315 safe_decoder_delete_(decoder_);
319 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
321 FLAC__StreamMetadata streaminfo;
326 if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
327 /* @@@ how to report the error? */
329 static const char *errtitle = "Invalid FLAC File: ";
330 *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
331 sprintf(*title, "%s\"%s\"", errtitle, filename);
334 *length_in_msec = -1;
339 *title = flac_format_song_title(filename);
342 *length_in_msec = streaminfo.data.stream_info.total_samples * 10 / (streaminfo.data.stream_info.sample_rate / 100);
345 /***********************************************************************
347 **********************************************************************/
349 void *play_loop_(void *arg)
351 unsigned written_time_last = 0, bh_index_last_w = 0, bh_index_last_o = BITRATE_HIST_SIZE, blocksize = 1;
352 FLAC__uint64 decode_position_last = 0, decode_position_frame_last = 0, decode_position_frame = 0;
356 while(file_info_.is_playing) {
357 if(!file_info_.eof) {
358 while(sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE) {
361 s = sample_buffer_last_ - sample_buffer_first_;
362 if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
363 file_info_.eof = true;
366 else if(!FLAC__file_decoder_process_single(decoder_)) {
367 /*@@@ this should probably be a dialog */
368 fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
369 file_info_.eof = true;
372 blocksize = sample_buffer_last_ - sample_buffer_first_ - s;
373 decode_position_frame_last = decode_position_frame;
374 if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
375 decode_position_frame = 0;
377 if(sample_buffer_last_ - sample_buffer_first_ > 0) {
378 const unsigned n = min(sample_buffer_last_ - sample_buffer_first_, SAMPLES_PER_WRITE);
379 int bytes = n * file_info_.channels * file_info_.sample_format_bytes_per_sample;
380 FLAC__byte *sample_buffer_start = sample_buffer_ + sample_buffer_first_ * file_info_.channels * file_info_.sample_format_bytes_per_sample;
381 unsigned written_time, bh_index_w;
382 FLAC__uint64 decode_position;
384 sample_buffer_first_ += n;
385 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, file_info_.channels, bytes, sample_buffer_start);
386 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
388 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
389 flac_ip.output->write_audio(sample_buffer_start, bytes);
391 /* compute current bitrate */
393 written_time = flac_ip.output->written_time();
394 bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
395 if(bh_index_w != bh_index_last_w) {
396 bh_index_last_w = bh_index_w;
397 decode_position = decode_position_frame - (double)(sample_buffer_last_ - sample_buffer_first_) * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize;
398 bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] =
399 decode_position > decode_position_last && written_time > written_time_last ?
400 8000 * (decode_position - decode_position_last) / (written_time - written_time_last) :
401 file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample;
402 decode_position_last = decode_position;
403 written_time_last = written_time;
407 file_info_.eof = true;
413 if(file_info_.seek_to_in_sec != -1) {
414 const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
415 unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
416 if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
417 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
418 bh_index_last_w = bh_index_last_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
419 if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
420 decode_position_frame = 0;
421 file_info_.seek_to_in_sec = -1;
422 file_info_.eof = false;
423 sample_buffer_first_ = sample_buffer_last_ = 0;
427 /* display the right bitrate from history */
428 unsigned bh_index_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
429 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) {
430 bh_index_last_o = bh_index_o;
431 flac_ip.set_info(file_info_.title, file_info_.length_in_msec, bitrate_history_[bh_index_o], file_info_.sample_rate, file_info_.channels);
436 safe_decoder_finish_(decoder_);
438 /* are these two calls necessary? */
439 flac_ip.output->buffer_free();
440 flac_ip.output->buffer_free();
442 g_free(file_info_.title);
445 return 0; /* to silence the compiler warning about not returning a value */
448 FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder)
453 safe_decoder_finish_(decoder);
455 FLAC__file_decoder_set_md5_checking(decoder, false);
456 FLAC__file_decoder_set_filename(decoder, filename);
457 FLAC__file_decoder_set_metadata_ignore_all(decoder);
458 FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
459 FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
460 FLAC__file_decoder_set_write_callback(decoder, write_callback_);
461 FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
462 FLAC__file_decoder_set_error_callback(decoder, error_callback_);
463 FLAC__file_decoder_set_client_data(decoder, &file_info_);
464 if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
467 if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
473 void safe_decoder_finish_(FLAC__FileDecoder *decoder)
475 if(decoder && FLAC__file_decoder_get_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
476 FLAC__file_decoder_finish(decoder);
479 void safe_decoder_delete_(FLAC__FileDecoder *decoder)
482 safe_decoder_finish_(decoder);
483 FLAC__file_decoder_delete(decoder);
487 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
489 file_info_struct *file_info = (file_info_struct *)client_data;
490 const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
491 const unsigned bits_per_sample = file_info->bits_per_sample;
492 FLAC__byte *sample_buffer_start;
496 if(file_info->abort_flag)
497 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
499 if((sample_buffer_last_ + wide_samples) > (SAMPLE_BUFFER_SIZE / (channels * file_info->sample_format_bytes_per_sample))) {
500 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);
501 sample_buffer_last_ -= sample_buffer_first_;
502 sample_buffer_first_ = 0;
504 sample_buffer_start = sample_buffer_ + sample_buffer_last_ * channels * file_info->sample_format_bytes_per_sample;
505 if(file_info->has_replaygain && flac_cfg.output.replaygain.enable) {
506 FLAC__replaygain_synthesis__apply_gain(
508 !is_big_endian_host_,
509 file_info->sample_format_bytes_per_sample == 1, /* unsigned_data_out */
514 file_info->sample_format_bytes_per_sample * 8,
515 file_info->replay_scale,
516 flac_cfg.output.replaygain.hard_limit,
517 flac_cfg.output.resolution.replaygain.dither,
518 &file_info->dither_context
521 else if(is_big_endian_host_) {
522 FLAC__plugin_common__pack_pcm_signed_big_endian(
528 file_info->sample_format_bytes_per_sample * 8
532 FLAC__plugin_common__pack_pcm_signed_little_endian(
538 file_info->sample_format_bytes_per_sample * 8
542 sample_buffer_last_ += wide_samples;
544 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
547 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
549 file_info_struct *file_info = (file_info_struct *)client_data;
551 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
552 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
553 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
554 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
555 file_info->channels = metadata->data.stream_info.channels;
556 file_info->sample_rate = metadata->data.stream_info.sample_rate;
557 file_info->length_in_msec = (FLAC__uint64)file_info->total_samples * 10 / (file_info->sample_rate / 100);
559 else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
561 if(grabbag__replaygain_load_from_vorbiscomment(metadata, flac_cfg.output.replaygain.album_mode, &gain, &peak)) {
562 file_info->has_replaygain = true;
563 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);
568 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
570 file_info_struct *file_info = (file_info_struct *)client_data;
572 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
573 file_info->abort_flag = true;