1 /* libxmms-flac - XMMS 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,
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 <sys/types.h>
28 #include <xmms/plugin.h>
29 #include <xmms/util.h>
30 #include <xmms/configfile.h>
31 #include <xmms/titlestring.h>
37 #ifdef HAVE_LANGINFO_CODESET
42 #include "plugin_common/all.h"
43 #include "share/grabbag.h"
44 #include "share/replaygain_synthesis.h"
45 #include "configure.h"
53 #define min(x,y) ((x)<(y)?(x):(y))
55 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
57 #define FLAC__U64L(x) x
59 #define FLAC__U64L(x) x##LLU
62 extern void FLAC_XMMS__file_info_box(char *filename);
65 FLAC__bool abort_flag;
66 FLAC__bool is_playing;
68 FLAC__bool play_thread_open; /* if true, is_playing must also be true */
69 unsigned total_samples;
70 unsigned bits_per_sample;
73 unsigned length_in_msec;
75 AFormat sample_format;
76 unsigned sample_format_bytes_per_sample;
78 FLAC__bool has_replaygain;
80 DitherContext dither_context;
83 typedef FLAC__StreamDecoderWriteStatus (*WriteCallback) (const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
84 typedef void (*MetadataCallback) (const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
85 typedef void (*ErrorCallback) (const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
89 void* (*new_decoder) (void);
90 FLAC__bool (*set_md5_checking) (void *decoder, FLAC__bool value);
91 FLAC__bool (*set_source) (void *decoder, const char* source);
92 FLAC__bool (*set_metadata_ignore_all) (void *decoder);
93 FLAC__bool (*set_metadata_respond) (void *decoder, FLAC__MetadataType type);
94 FLAC__bool (*set_write_callback) (void *decoder, WriteCallback value);
95 FLAC__bool (*set_metadata_callback) (void *decoder, MetadataCallback value);
96 FLAC__bool (*set_error_callback) (void *decoder, ErrorCallback value);
97 FLAC__bool (*set_client_data) (void *decoder, void *value);
98 FLAC__bool (*decoder_init) (void *decoder);
99 void (*safe_decoder_finish) (void *decoder);
100 void (*safe_decoder_delete) (void *decoder);
101 FLAC__bool (*process_until_end_of_metadata) (void *decoder);
102 FLAC__bool (*process_single) (void *decoder);
103 FLAC__bool (*is_eof) (void *decoder);
106 #define NUM_DECODER_TYPES 2
112 static void FLAC_XMMS__init();
113 static int FLAC_XMMS__is_our_file(char *filename);
114 static void FLAC_XMMS__play_file(char *filename);
115 static void FLAC_XMMS__stop();
116 static void FLAC_XMMS__pause(short p);
117 static void FLAC_XMMS__seek(int time);
118 static int FLAC_XMMS__get_time();
119 static void FLAC_XMMS__cleanup();
120 static void FLAC_XMMS__get_song_info(char *filename, char **title, int *length);
122 static void *play_loop_(void *arg);
124 static FLAC__bool safe_decoder_init_(const char *filename, void **decoderp, decoder_funcs_t const ** fnsp);
125 static void file_decoder_safe_decoder_finish_(void *decoder);
126 static void file_decoder_safe_decoder_delete_(void *decoder);
127 static FLAC__StreamDecoderWriteStatus write_callback_(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
128 static void metadata_callback_(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
129 static void error_callback_(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
131 static void init_decoder_func_tables();
132 static decoder_t source_to_decoder_type (const char *source);
134 InputPlugin flac_ip =
138 "FLAC Player v" VERSION,
141 FLAC_XMMS__configure,
142 FLAC_XMMS__is_our_file,
144 FLAC_XMMS__play_file,
157 FLAC_XMMS__get_song_info,
158 FLAC_XMMS__file_info_box,
162 #define SAMPLES_PER_WRITE 512
163 #define SAMPLE_BUFFER_SIZE ((FLAC__MAX_BLOCK_SIZE + SAMPLES_PER_WRITE) * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8))
164 static FLAC__byte sample_buffer_[SAMPLE_BUFFER_SIZE];
165 static unsigned sample_buffer_first_, sample_buffer_last_;
167 static void *decoder_ = 0;
168 static file_info_struct file_info_;
169 static pthread_t decode_thread_;
170 static FLAC__bool audio_error_ = false;
171 static FLAC__bool is_big_endian_host_;
173 #define BITRATE_HIST_SEGMENT_MSEC 500
174 /* 500ms * 50 = 25s should be enough */
175 #define BITRATE_HIST_SIZE 50
176 static unsigned bitrate_history_[BITRATE_HIST_SIZE];
178 /* A table of sets of decoder functions, indexed by decoder_t */
179 static const decoder_funcs_t* DECODER_FUNCS[NUM_DECODER_TYPES];
181 static decoder_funcs_t const * decoder_func_table_;
184 InputPlugin *get_iplugin_info()
186 flac_ip.description = g_strdup_printf("Reference FLAC Player v%s", FLAC__VERSION_STRING);
190 void set_track_info(const char* title, int length_in_msec)
192 if (file_info_.is_playing) {
193 flac_ip.set_info((char*) title, length_in_msec, file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample, file_info_.sample_rate, file_info_.channels);
197 static gchar* homedir()
200 char *env_home = getenv("HOME");
202 result = g_strdup (env_home);
204 uid_t uid = getuid();
205 struct passwd *pwent;
208 } while (pwent && pwent->pw_uid != uid);
209 result = pwent ? g_strdup (pwent->pw_dir) : NULL;
215 void FLAC_XMMS__init()
218 FLAC__uint32 test = 1;
220 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
222 flac_cfg.title.tag_override = FALSE;
223 if (flac_cfg.title.tag_format)
224 g_free(flac_cfg.title.tag_format);
225 flac_cfg.title.convert_char_set = FALSE;
227 cfg = xmms_cfg_open_default_file();
231 xmms_cfg_read_boolean(cfg, "flac", "title.tag_override", &flac_cfg.title.tag_override);
233 if(!xmms_cfg_read_string(cfg, "flac", "title.tag_format", &flac_cfg.title.tag_format))
234 flac_cfg.title.tag_format = g_strdup("%p - %t");
236 xmms_cfg_read_boolean(cfg, "flac", "title.convert_char_set", &flac_cfg.title.convert_char_set);
238 if(!xmms_cfg_read_string(cfg, "flac", "title.user_char_set", &flac_cfg.title.user_char_set))
239 flac_cfg.title.user_char_set = FLAC_plugin__charset_get_current();
243 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.enable", &flac_cfg.output.replaygain.enable);
245 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.album_mode", &flac_cfg.output.replaygain.album_mode);
247 if(!xmms_cfg_read_int(cfg, "flac", "output.replaygain.preamp", &flac_cfg.output.replaygain.preamp))
248 flac_cfg.output.replaygain.preamp = 0;
250 xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.hard_limit", &flac_cfg.output.replaygain.hard_limit);
252 xmms_cfg_read_boolean(cfg, "flac", "output.resolution.normal.dither_24_to_16", &flac_cfg.output.resolution.normal.dither_24_to_16);
253 xmms_cfg_read_boolean(cfg, "flac", "output.resolution.replaygain.dither", &flac_cfg.output.resolution.replaygain.dither);
255 if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.noise_shaping", &flac_cfg.output.resolution.replaygain.noise_shaping))
256 flac_cfg.output.resolution.replaygain.noise_shaping = 1;
258 if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.bps_out", &flac_cfg.output.resolution.replaygain.bps_out))
259 flac_cfg.output.resolution.replaygain.bps_out = 16;
263 xmms_cfg_read_int(cfg, "flac", "stream.http_buffer_size", &flac_cfg.stream.http_buffer_size);
264 xmms_cfg_read_int(cfg, "flac", "stream.http_prebuffer", &flac_cfg.stream.http_prebuffer);
265 xmms_cfg_read_boolean(cfg, "flac", "stream.use_proxy", &flac_cfg.stream.use_proxy);
266 if(flac_cfg.stream.proxy_host)
267 g_free(flac_cfg.stream.proxy_host);
268 if(!xmms_cfg_read_string(cfg, "flac", "stream.proxy_host", &flac_cfg.stream.proxy_host))
269 flac_cfg.stream.proxy_host = g_strdup("");
270 xmms_cfg_read_int(cfg, "flac", "stream.proxy_port", &flac_cfg.stream.proxy_port);
271 xmms_cfg_read_boolean(cfg, "flac", "stream.proxy_use_auth", &flac_cfg.stream.proxy_use_auth);
272 if(flac_cfg.stream.proxy_user)
273 g_free(flac_cfg.stream.proxy_user);
274 flac_cfg.stream.proxy_user = NULL;
275 xmms_cfg_read_string(cfg, "flac", "stream.proxy_user", &flac_cfg.stream.proxy_user);
276 if(flac_cfg.stream.proxy_pass)
277 g_free(flac_cfg.stream.proxy_pass);
278 flac_cfg.stream.proxy_pass = NULL;
279 xmms_cfg_read_string(cfg, "flac", "stream.proxy_pass", &flac_cfg.stream.proxy_pass);
280 xmms_cfg_read_boolean(cfg, "flac", "stream.save_http_stream", &flac_cfg.stream.save_http_stream);
281 if (flac_cfg.stream.save_http_path)
282 g_free (flac_cfg.stream.save_http_path);
283 if (!xmms_cfg_read_string(cfg, "flac", "stream.save_http_path", &flac_cfg.stream.save_http_path) || ! *flac_cfg.stream.save_http_path) {
284 if (flac_cfg.stream.save_http_path)
285 g_free (flac_cfg.stream.save_http_path);
286 flac_cfg.stream.save_http_path = homedir();
288 xmms_cfg_read_boolean(cfg, "flac", "stream.cast_title_streaming", &flac_cfg.stream.cast_title_streaming);
289 xmms_cfg_read_boolean(cfg, "flac", "stream.use_udp_channel", &flac_cfg.stream.use_udp_channel);
291 init_decoder_func_tables();
292 decoder_func_table_ = DECODER_FUNCS [DECODER_FILE];
293 decoder_ = decoder_func_table_ -> new_decoder();
298 int FLAC_XMMS__is_our_file(char *filename)
302 ext = strrchr(filename, '.');
304 if(!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
309 void FLAC_XMMS__play_file(char *filename)
313 sample_buffer_first_ = sample_buffer_last_ = 0;
314 audio_error_ = false;
315 file_info_.abort_flag = false;
316 file_info_.is_playing = false;
317 file_info_.eof = false;
318 file_info_.play_thread_open = false;
319 file_info_.has_replaygain = false;
321 if (source_to_decoder_type (filename) == DECODER_FILE) {
322 if(0 == (f = fopen(filename, "r")))
330 if(!safe_decoder_init_(filename, &decoder_, &decoder_func_table_))
333 if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable) {
334 if(flac_cfg.output.resolution.replaygain.bps_out == 8) {
335 file_info_.sample_format = FMT_U8;
336 file_info_.sample_format_bytes_per_sample = 1;
338 else if(flac_cfg.output.resolution.replaygain.bps_out == 16) {
339 file_info_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
340 file_info_.sample_format_bytes_per_sample = 2;
343 /*@@@ 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); */
344 fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", flac_cfg.output.resolution.replaygain.bps_out);
345 decoder_func_table_ -> safe_decoder_finish(decoder_);
350 if(file_info_.bits_per_sample == 8) {
351 file_info_.sample_format = FMT_U8;
352 file_info_.sample_format_bytes_per_sample = 1;
354 else if(file_info_.bits_per_sample == 16 || (file_info_.bits_per_sample == 24 && flac_cfg.output.resolution.normal.dither_24_to_16)) {
355 file_info_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
356 file_info_.sample_format_bytes_per_sample = 2;
359 /*@@@ 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); */
360 fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", file_info_.bits_per_sample);
361 decoder_func_table_ -> safe_decoder_finish(decoder_);
365 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);
366 file_info_.is_playing = true;
368 if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
370 decoder_func_table_ -> safe_decoder_finish(decoder_);
374 file_info_.title = flac_format_song_title(filename);
375 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);
377 file_info_.seek_to_in_sec = -1;
378 file_info_.play_thread_open = true;
379 pthread_create(&decode_thread_, NULL, play_loop_, NULL);
382 void FLAC_XMMS__stop()
384 if(file_info_.is_playing) {
385 file_info_.is_playing = false;
386 if(file_info_.play_thread_open) {
387 file_info_.play_thread_open = false;
388 pthread_join(decode_thread_, NULL);
390 flac_ip.output->close_audio();
391 decoder_func_table_ -> safe_decoder_finish (decoder_);
395 void FLAC_XMMS__pause(short p)
397 flac_ip.output->pause(p);
400 void FLAC_XMMS__seek(int time)
402 if (decoder_func_table_->seekable) {
403 file_info_.seek_to_in_sec = time;
404 file_info_.eof = false;
406 while(file_info_.seek_to_in_sec != -1)
411 int FLAC_XMMS__get_time()
415 if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
418 return flac_ip.output->output_time();
421 void FLAC_XMMS__cleanup()
423 decoder_func_table_ -> safe_decoder_delete(decoder_);
427 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
429 FLAC__StreamMetadata streaminfo;
434 if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
435 /* @@@ how to report the error? */
437 if (source_to_decoder_type (filename) == DECODER_FILE) {
438 static const char *errtitle = "Invalid FLAC File: ";
439 *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
440 sprintf(*title, "%s\"%s\"", errtitle, filename);
446 *length_in_msec = -1;
451 *title = flac_format_song_title(filename);
454 *length_in_msec = (unsigned)((double)streaminfo.data.stream_info.total_samples / (double)streaminfo.data.stream_info.sample_rate * 1000.0 + 0.5);
457 /***********************************************************************
459 **********************************************************************/
461 void *play_loop_(void *arg)
463 unsigned written_time_last = 0, bh_index_last_w = 0, bh_index_last_o = BITRATE_HIST_SIZE, blocksize = 1;
464 FLAC__uint64 decode_position_last = 0, decode_position_frame_last = 0, decode_position_frame = 0;
468 while(file_info_.is_playing) {
469 if(!file_info_.eof) {
470 while(sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE) {
473 s = sample_buffer_last_ - sample_buffer_first_;
474 if(decoder_func_table_ -> is_eof(decoder_)) {
475 file_info_.eof = true;
478 else if (!decoder_func_table_ -> process_single(decoder_)) {
479 /*@@@ this should probably be a dialog */
480 fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
481 file_info_.eof = true;
484 blocksize = sample_buffer_last_ - sample_buffer_first_ - s;
485 decode_position_frame_last = decode_position_frame;
486 if(!decoder_func_table_->seekable || !FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
487 decode_position_frame = 0;
489 if(sample_buffer_last_ - sample_buffer_first_ > 0) {
490 const unsigned n = min(sample_buffer_last_ - sample_buffer_first_, SAMPLES_PER_WRITE);
491 int bytes = n * file_info_.channels * file_info_.sample_format_bytes_per_sample;
492 FLAC__byte *sample_buffer_start = sample_buffer_ + sample_buffer_first_ * file_info_.channels * file_info_.sample_format_bytes_per_sample;
493 unsigned written_time, bh_index_w;
494 FLAC__uint64 decode_position;
496 sample_buffer_first_ += n;
497 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, file_info_.channels, bytes, sample_buffer_start);
498 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
500 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
501 flac_ip.output->write_audio(sample_buffer_start, bytes);
503 /* compute current bitrate */
505 written_time = flac_ip.output->written_time();
506 bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
507 if(bh_index_w != bh_index_last_w) {
508 bh_index_last_w = bh_index_w;
509 decode_position = decode_position_frame - (double)(sample_buffer_last_ - sample_buffer_first_) * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize;
510 bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] =
511 decode_position > decode_position_last && written_time > written_time_last ?
512 8000 * (decode_position - decode_position_last) / (written_time - written_time_last) :
513 file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample;
514 decode_position_last = decode_position;
515 written_time_last = written_time;
519 file_info_.eof = true;
525 if(decoder_func_table_->seekable && file_info_.seek_to_in_sec != -1) {
526 const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
527 unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
528 if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
529 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
530 bh_index_last_w = bh_index_last_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
531 if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
532 decode_position_frame = 0;
533 file_info_.seek_to_in_sec = -1;
534 file_info_.eof = false;
535 sample_buffer_first_ = sample_buffer_last_ = 0;
539 /* display the right bitrate from history */
540 unsigned bh_index_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
541 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) {
542 bh_index_last_o = bh_index_o;
543 flac_ip.set_info(file_info_.title, file_info_.length_in_msec, bitrate_history_[bh_index_o], file_info_.sample_rate, file_info_.channels);
548 decoder_func_table_ -> safe_decoder_finish(decoder_);
550 /* are these two calls necessary? */
551 flac_ip.output->buffer_free();
552 flac_ip.output->buffer_free();
554 g_free(file_info_.title);
557 return 0; /* to silence the compiler warning about not returning a value */
560 /*********** File decoder functions */
562 static FLAC__bool file_decoder_init (void *decoder)
564 return FLAC__file_decoder_init( (FLAC__FileDecoder*) decoder) == FLAC__FILE_DECODER_OK;
567 static void file_decoder_safe_decoder_finish_(void *decoder)
569 if(decoder && FLAC__file_decoder_get_state((FLAC__FileDecoder *) decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
570 FLAC__file_decoder_finish((FLAC__FileDecoder *) decoder);
573 static void file_decoder_safe_decoder_delete_(void *decoder)
576 file_decoder_safe_decoder_finish_(decoder);
577 FLAC__file_decoder_delete( (FLAC__FileDecoder *) decoder);
581 static FLAC__bool file_decoder_is_eof(void *decoder)
583 return FLAC__file_decoder_get_state((FLAC__FileDecoder *) decoder) == FLAC__FILE_DECODER_END_OF_FILE;
586 static const decoder_funcs_t FILE_DECODER_FUNCTIONS = {
588 (void* (*) (void)) FLAC__file_decoder_new,
589 (FLAC__bool (*) (void *, FLAC__bool)) FLAC__file_decoder_set_md5_checking,
590 (FLAC__bool (*) (void *, const char*)) FLAC__file_decoder_set_filename,
591 (FLAC__bool (*) (void *)) FLAC__file_decoder_set_metadata_ignore_all,
592 (FLAC__bool (*) (void *, FLAC__MetadataType)) FLAC__file_decoder_set_metadata_respond,
593 (FLAC__bool (*) (void *, WriteCallback)) FLAC__file_decoder_set_write_callback,
594 (FLAC__bool (*) (void *, MetadataCallback)) FLAC__file_decoder_set_metadata_callback,
595 (FLAC__bool (*) (void *, ErrorCallback)) FLAC__file_decoder_set_error_callback,
596 (FLAC__bool (*) (void *, void *)) FLAC__file_decoder_set_client_data,
597 (FLAC__bool (*) (void *)) file_decoder_init,
598 (void (*) (void *)) file_decoder_safe_decoder_finish_,
599 (void (*) (void *)) file_decoder_safe_decoder_delete_,
600 (FLAC__bool (*) (void *)) FLAC__file_decoder_process_until_end_of_metadata,
601 (FLAC__bool (*) (void *)) FLAC__file_decoder_process_single,
605 /*********** HTTP decoder functions */
609 static FLAC__bool http_decoder_set_md5_checking (void *decoder, FLAC__bool value)
612 // operation unsupported
613 return FLAC__stream_decoder_get_state ((const FLAC__StreamDecoder *) decoder) ==
614 FLAC__STREAM_DECODER_UNINITIALIZED;
617 static FLAC__bool http_decoder_set_url (void *decoder, const char* url)
620 url_ = g_strdup (url);
624 static FLAC__StreamDecoderReadStatus http_decoder_read_callback (const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
628 *bytes = flac_http_read (buffer, *bytes);
629 return *bytes ? FLAC__STREAM_DECODER_READ_STATUS_CONTINUE : FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
632 static FLAC__bool http_decoder_init (void *decoder)
634 flac_http_open (url_, 0);
636 FLAC__stream_decoder_set_read_callback (decoder, http_decoder_read_callback);
637 return FLAC__stream_decoder_init( (FLAC__StreamDecoder*) decoder) == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
640 static void http_decoder_safe_decoder_finish_(void *decoder)
642 if(decoder && FLAC__stream_decoder_get_state((FLAC__StreamDecoder *) decoder) != FLAC__STREAM_DECODER_UNINITIALIZED) {
643 FLAC__stream_decoder_finish((FLAC__StreamDecoder *) decoder);
648 static void http_decoder_safe_decoder_delete_(void *decoder)
651 http_decoder_safe_decoder_finish_(decoder);
652 FLAC__stream_decoder_delete( (FLAC__StreamDecoder *) decoder);
656 static FLAC__bool http_decoder_is_eof(void *decoder)
658 return FLAC__stream_decoder_get_state((FLAC__StreamDecoder *) decoder) == FLAC__STREAM_DECODER_END_OF_STREAM;
661 static const decoder_funcs_t HTTP_DECODER_FUNCTIONS = {
663 (void* (*) (void)) FLAC__stream_decoder_new,
664 http_decoder_set_md5_checking,
665 (FLAC__bool (*) (void *, const char*)) http_decoder_set_url,
666 (FLAC__bool (*) (void *)) FLAC__stream_decoder_set_metadata_ignore_all,
667 (FLAC__bool (*) (void *, FLAC__MetadataType)) FLAC__stream_decoder_set_metadata_respond,
668 (FLAC__bool (*) (void *, WriteCallback)) FLAC__stream_decoder_set_write_callback,
669 (FLAC__bool (*) (void *, MetadataCallback)) FLAC__stream_decoder_set_metadata_callback,
670 (FLAC__bool (*) (void *, ErrorCallback)) FLAC__stream_decoder_set_error_callback,
671 (FLAC__bool (*) (void *, void *)) FLAC__stream_decoder_set_client_data,
672 (FLAC__bool (*) (void *)) http_decoder_init,
673 (void (*) (void *)) http_decoder_safe_decoder_finish_,
674 (void (*) (void *)) http_decoder_safe_decoder_delete_,
675 (FLAC__bool (*) (void *)) FLAC__stream_decoder_process_until_end_of_metadata,
676 (FLAC__bool (*) (void *)) FLAC__stream_decoder_process_single,
680 static decoder_funcs_t const *decoder_func_table_;
682 static void init_decoder_func_tables()
684 DECODER_FUNCS [DECODER_FILE] = & FILE_DECODER_FUNCTIONS;
685 DECODER_FUNCS [DECODER_HTTP] = & HTTP_DECODER_FUNCTIONS;
688 static decoder_t source_to_decoder_type (const char *source)
690 return strncasecmp(source, "http://", 7) ? DECODER_FILE : DECODER_HTTP;
693 static void change_decoder_if_needed (decoder_t new_decoder_type, void **decoderp, decoder_funcs_t const ** fntabp)
695 const decoder_funcs_t *new_fn_table = DECODER_FUNCS [new_decoder_type];
696 if (*fntabp != new_fn_table) {
697 (*fntabp)->safe_decoder_delete(*decoderp);
698 *fntabp = new_fn_table;
699 *decoderp = new_fn_table -> new_decoder();
703 FLAC__bool safe_decoder_init_(const char *filename, void **decoderp, decoder_funcs_t const ** fntabp)
705 if(decoderp == 0 || *decoderp == 0)
708 (*fntabp)->safe_decoder_finish(*decoderp);
710 change_decoder_if_needed(source_to_decoder_type(filename), decoderp, fntabp);
713 decoder_funcs_t const *fntab = *fntabp;
714 void *decoder = *decoderp;
719 fntab -> set_md5_checking(decoder, false);
720 fntab -> set_source(decoder, filename);
721 fntab -> set_metadata_ignore_all(decoder);
722 fntab -> set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
723 fntab -> set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
724 fntab -> set_write_callback(decoder, write_callback_);
725 fntab -> set_metadata_callback(decoder, metadata_callback_);
726 fntab -> set_error_callback(decoder, error_callback_);
727 fntab -> set_client_data(decoder, &file_info_);
728 if(!fntab -> decoder_init(decoder))
731 if(!fntab -> process_until_end_of_metadata(decoder))
738 FLAC__StreamDecoderWriteStatus write_callback_(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
740 file_info_struct *file_info = (file_info_struct *)client_data;
741 const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
742 const unsigned bits_per_sample = file_info->bits_per_sample;
743 FLAC__byte *sample_buffer_start;
747 if(file_info->abort_flag)
748 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
750 if((sample_buffer_last_ + wide_samples) > (SAMPLE_BUFFER_SIZE / (channels * file_info->sample_format_bytes_per_sample))) {
751 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);
752 sample_buffer_last_ -= sample_buffer_first_;
753 sample_buffer_first_ = 0;
755 sample_buffer_start = sample_buffer_ + sample_buffer_last_ * channels * file_info->sample_format_bytes_per_sample;
756 if(file_info->has_replaygain && flac_cfg.output.replaygain.enable) {
757 FLAC__replaygain_synthesis__apply_gain(
759 !is_big_endian_host_,
760 file_info->sample_format_bytes_per_sample == 1, /* unsigned_data_out */
765 file_info->sample_format_bytes_per_sample * 8,
766 file_info->replay_scale,
767 flac_cfg.output.replaygain.hard_limit,
768 flac_cfg.output.resolution.replaygain.dither,
769 &file_info->dither_context
772 else if(is_big_endian_host_) {
773 FLAC__plugin_common__pack_pcm_signed_big_endian(
779 file_info->sample_format_bytes_per_sample * 8
783 FLAC__plugin_common__pack_pcm_signed_little_endian(
789 file_info->sample_format_bytes_per_sample * 8
793 sample_buffer_last_ += wide_samples;
795 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
798 void metadata_callback_(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
800 file_info_struct *file_info = (file_info_struct *)client_data;
802 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
803 FLAC__ASSERT(metadata->data.stream_info.total_samples < FLAC__U64L(0x100000000)); /* this plugin can only handle < 4 gigasamples */
804 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
805 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
806 file_info->channels = metadata->data.stream_info.channels;
807 file_info->sample_rate = metadata->data.stream_info.sample_rate;
808 file_info->length_in_msec = (unsigned)((double)file_info->total_samples / (double)file_info->sample_rate * 1000.0 + 0.5);
810 else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
812 if(grabbag__replaygain_load_from_vorbiscomment(metadata, flac_cfg.output.replaygain.album_mode, /*strict=*/false, &gain, &peak)) {
813 file_info->has_replaygain = true;
814 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);
819 void error_callback_(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
821 file_info_struct *file_info = (file_info_struct *)client_data;
823 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
824 file_info->abort_flag = true;