remove id3 support from the plugins
[flac.git] / src / plugin_xmms / plugin.c
index 5503bfe..bdb1e4a 100644 (file)
@@ -1,5 +1,5 @@
 /* libxmms-flac - XMMS FLAC input plugin
- * Copyright (C) 2000,2001,2002  Josh Coalson
+ * Copyright (C) 2000,2001,2002,2003,2004  Josh Coalson
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 #include "FLAC/all.h"
 #include "plugin_common/all.h"
 #include "share/grabbag.h"
+#include "share/replaygain_synthesis.h"
 #include "configure.h"
-#include "wrap_id3.h"
 #include "charset.h"
+#include "tag.h"
 
 #ifdef min
 #undef min
 #endif
 #define min(x,y) ((x)<(y)?(x):(y))
 
+/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
+#ifdef _MSC_VER
+#define FLAC__U64L(x) x
+#else
+#define FLAC__U64L(x) x##LLU
+#endif
+
+extern void FLAC_XMMS__file_info_box(char *filename);
 
 typedef struct {
        FLAC__bool abort_flag;
@@ -58,7 +67,9 @@ typedef struct {
        unsigned channels;
        unsigned sample_rate;
        unsigned length_in_msec;
+       gchar *title;
        AFormat sample_format;
+       unsigned sample_format_bytes_per_sample;
        int seek_to_in_sec;
        FLAC__bool has_replaygain;
        double replay_scale;
@@ -107,18 +118,26 @@ InputPlugin flac_ip =
        NULL,
        NULL,
        FLAC_XMMS__get_song_info,
-       NULL,           /* file_info_box */
+       FLAC_XMMS__file_info_box,
        NULL
 };
 
 #define SAMPLES_PER_WRITE 512
-static FLAC__int32 reservoir_[FLAC__MAX_BLOCK_SIZE * 2/*for overflow*/ * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
-static FLAC__byte sample_buffer_[SAMPLES_PER_WRITE * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8)]; /* (24/8) for max bytes per sample */
-static unsigned wide_samples_in_reservoir_ = 0;
+#define SAMPLE_BUFFER_SIZE ((FLAC__MAX_BLOCK_SIZE + SAMPLES_PER_WRITE) * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8))
+static FLAC__byte sample_buffer_[SAMPLE_BUFFER_SIZE];
+static unsigned sample_buffer_first_, sample_buffer_last_;
+
 static FLAC__FileDecoder *decoder_ = 0;
 static file_info_struct file_info_;
 static pthread_t decode_thread_;
 static FLAC__bool audio_error_ = false;
+static FLAC__bool is_big_endian_host_;
+
+#define BITRATE_HIST_SEGMENT_MSEC 500
+/* 500ms * 50 = 25s should be enough */
+#define BITRATE_HIST_SIZE 50
+static unsigned bitrate_history_[BITRATE_HIST_SIZE];
+
 
 InputPlugin *get_iplugin_info()
 {
@@ -129,6 +148,9 @@ InputPlugin *get_iplugin_info()
 void FLAC_XMMS__init()
 {
        ConfigFile *cfg;
+       FLAC__uint32 test = 1;
+       is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
 
        flac_cfg.title.tag_override = FALSE;
        g_free(flac_cfg.title.tag_format);
@@ -145,9 +167,6 @@ void FLAC_XMMS__init()
 
        xmms_cfg_read_boolean(cfg, "flac", "title.convert_char_set", &flac_cfg.title.convert_char_set);
 
-       if(!xmms_cfg_read_string(cfg, "flac", "title.file_char_set", &flac_cfg.title.file_char_set))
-               flac_cfg.title.file_char_set = FLAC_plugin__charset_get_current();
-
        if(!xmms_cfg_read_string(cfg, "flac", "title.user_char_set", &flac_cfg.title.user_char_set))
                flac_cfg.title.user_char_set = FLAC_plugin__charset_get_current();
 
@@ -172,6 +191,8 @@ void FLAC_XMMS__init()
                flac_cfg.output.resolution.replaygain.bps_out = 16;
 
        decoder_ = FLAC__file_decoder_new();
+
+       xmms_cfg_free(cfg);
 }
 
 int FLAC_XMMS__is_our_file(char *filename)
@@ -188,9 +209,8 @@ int FLAC_XMMS__is_our_file(char *filename)
 void FLAC_XMMS__play_file(char *filename)
 {
        FILE *f;
-       gchar *ret;
 
-       wide_samples_in_reservoir_ = 0;
+       sample_buffer_first_ = sample_buffer_last_ = 0;
        audio_error_ = false;
        file_info_.abort_flag = false;
        file_info_.is_playing = false;
@@ -208,9 +228,39 @@ void FLAC_XMMS__play_file(char *filename)
        if(!safe_decoder_init_(filename, decoder_))
                return;
 
-       if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable && flac_cfg.output.resolution.replaygain.dither)
-               FLAC__plugin_common__init_dither_context(&file_info_.dither_context, file_info_.bits_per_sample, flac_cfg.output.resolution.replaygain.noise_shaping);
-
+       if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable) {
+               if(flac_cfg.output.resolution.replaygain.bps_out == 8) {
+                       file_info_.sample_format = FMT_U8;
+                       file_info_.sample_format_bytes_per_sample = 1;
+               }
+               else if(flac_cfg.output.resolution.replaygain.bps_out == 16) {
+                       file_info_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
+                       file_info_.sample_format_bytes_per_sample = 2;
+               }
+               else {
+                       /*@@@ 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); */
+                       fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", flac_cfg.output.resolution.replaygain.bps_out);
+                       safe_decoder_finish_(decoder_);
+                       return;
+               }
+       }
+       else {
+               if(file_info_.bits_per_sample == 8) {
+                       file_info_.sample_format = FMT_U8;
+                       file_info_.sample_format_bytes_per_sample = 1;
+               }
+               else if(file_info_.bits_per_sample == 16 || (file_info_.bits_per_sample == 24 && flac_cfg.output.resolution.normal.dither_24_to_16)) {
+                       file_info_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
+                       file_info_.sample_format_bytes_per_sample = 2;
+               }
+               else {
+                       /*@@@ 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); */
+                       fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", file_info_.bits_per_sample);
+                       safe_decoder_finish_(decoder_);
+                       return;
+               }
+       }
+       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);
        file_info_.is_playing = true;
 
        if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
@@ -219,10 +269,8 @@ void FLAC_XMMS__play_file(char *filename)
                return;
        }
 
-       ret = flac_format_song_title(filename);
-       flac_ip.set_info(ret, file_info_.length_in_msec, file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample, file_info_.sample_rate, file_info_.channels);
-
-       g_free(ret);
+       file_info_.title = flac_format_song_title(filename);
+       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);
 
        file_info_.seek_to_in_sec = -1;
        file_info_.play_thread_open = true;
@@ -304,11 +352,17 @@ void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
 
 void *play_loop_(void *arg)
 {
+       unsigned written_time_last = 0, bh_index_last_w = 0, bh_index_last_o = BITRATE_HIST_SIZE, blocksize = 1;
+       FLAC__uint64 decode_position_last = 0, decode_position_frame_last = 0, decode_position_frame = 0;
+
        (void)arg;
 
        while(file_info_.is_playing) {
                if(!file_info_.eof) {
-                       while(wide_samples_in_reservoir_ < SAMPLES_PER_WRITE) {
+                       while(sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE) {
+                               unsigned s;
+
+                               s = sample_buffer_last_ - sample_buffer_first_;
                                if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
                                        file_info_.eof = true;
                                        break;
@@ -319,52 +373,39 @@ void *play_loop_(void *arg)
                                        file_info_.eof = true;
                                        break;
                                }
+                               blocksize = sample_buffer_last_ - sample_buffer_first_ - s;
+                               decode_position_frame_last = decode_position_frame;
+                               if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
+                                       decode_position_frame = 0;
                        }
-                       if(wide_samples_in_reservoir_ > 0) {
-                               const unsigned channels = file_info_.channels;
-                               const unsigned bits_per_sample = file_info_.bits_per_sample;
-                               const unsigned n = min(wide_samples_in_reservoir_, SAMPLES_PER_WRITE);
-                               const unsigned delta = n * channels;
-                               int bytes;
-                               unsigned i;
-
-                               if(flac_cfg.output.replaygain.enable && file_info_.has_replaygain) {
-                                       bytes = (int)FLAC__plugin_common__apply_gain(
-                                               sample_buffer_,
-                                               reservoir_,
-                                               n,
-                                               channels,
-                                               bits_per_sample,
-                                               flac_cfg.output.resolution.replaygain.bps_out,
-                                               file_info_.replay_scale,
-                                               flac_cfg.output.replaygain.hard_limit,
-                                               flac_cfg.output.resolution.replaygain.dither,
-                                               (NoiseShaping)flac_cfg.output.resolution.replaygain.noise_shaping,
-                                               &file_info_.dither_context
-                                       );
-                               }
-                               else {
-                                       bytes = (int)FLAC__plugin_common__pack_pcm_signed_little_endian(
-                                               sample_buffer_,
-                                               reservoir_,
-                                               n,
-                                               channels,
-                                               bits_per_sample,
-                                               flac_cfg.output.resolution.normal.dither_24_to_16?
-                                                       min(bits_per_sample, 16) :
-                                                       bits_per_sample
-                                       );
-                               }
-
-                               for(i = delta; i < wide_samples_in_reservoir_ * channels; i++)
-                                       reservoir_[i-delta] = reservoir_[i];
-                               wide_samples_in_reservoir_ -= n;
-
-                               flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, channels, bytes, sample_buffer_);
+                       if(sample_buffer_last_ - sample_buffer_first_ > 0) {
+                               const unsigned n = min(sample_buffer_last_ - sample_buffer_first_, SAMPLES_PER_WRITE);
+                               int bytes = n * file_info_.channels * file_info_.sample_format_bytes_per_sample;
+                               FLAC__byte *sample_buffer_start = sample_buffer_ + sample_buffer_first_ * file_info_.channels * file_info_.sample_format_bytes_per_sample;
+                               unsigned written_time, bh_index_w;
+                               FLAC__uint64 decode_position;
+
+                               sample_buffer_first_ += n;
+                               flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, file_info_.channels, bytes, sample_buffer_start);
                                while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
                                        xmms_usleep(10000);
                                if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
-                                       flac_ip.output->write_audio(sample_buffer_, bytes);
+                                       flac_ip.output->write_audio(sample_buffer_start, bytes);
+
+                               /* compute current bitrate */
+
+                               written_time = flac_ip.output->written_time();
+                               bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
+                               if(bh_index_w != bh_index_last_w) {
+                                       bh_index_last_w = bh_index_w;
+                                       decode_position = decode_position_frame - (double)(sample_buffer_last_ - sample_buffer_first_) * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize;
+                                       bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] =
+                                               decode_position > decode_position_last && written_time > written_time_last ?
+                                                       8000 * (decode_position - decode_position_last) / (written_time - written_time_last) :
+                                                       file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample;
+                                       decode_position_last = decode_position;
+                                       written_time_last = written_time;
+                               }
                        }
                        else {
                                file_info_.eof = true;
@@ -378,9 +419,20 @@ void *play_loop_(void *arg)
                        unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
                        if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
                                flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
+                               bh_index_last_w = bh_index_last_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
+                               if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
+                                       decode_position_frame = 0;
                                file_info_.seek_to_in_sec = -1;
                                file_info_.eof = false;
-                               wide_samples_in_reservoir_ = 0;
+                               sample_buffer_first_ = sample_buffer_last_ = 0;
+                       }
+               }
+               else {
+                       /* display the right bitrate from history */
+                       unsigned bh_index_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
+                       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) {
+                               bh_index_last_o = bh_index_o;
+                               flac_ip.set_info(file_info_.title, file_info_.length_in_msec, bitrate_history_[bh_index_o], file_info_.sample_rate, file_info_.channels);
                        }
                }
        }
@@ -391,6 +443,8 @@ void *play_loop_(void *arg)
        flac_ip.output->buffer_free();
        flac_ip.output->buffer_free();
 
+       g_free(file_info_.title);
+
        pthread_exit(NULL);
        return 0; /* to silence the compiler warning about not returning a value */
 }
@@ -438,18 +492,58 @@ FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder,
 {
        file_info_struct *file_info = (file_info_struct *)client_data;
        const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
-       unsigned wide_sample, offset_sample, channel;
+       const unsigned bits_per_sample = file_info->bits_per_sample;
+       FLAC__byte *sample_buffer_start;
 
        (void)decoder;
 
        if(file_info->abort_flag)
                return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
 
-       for(offset_sample = wide_samples_in_reservoir_ * channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
-               for(channel = 0; channel < channels; channel++, offset_sample++)
-                       reservoir_[offset_sample] = buffer[channel][wide_sample];
+       if((sample_buffer_last_ + wide_samples) > (SAMPLE_BUFFER_SIZE / (channels * file_info->sample_format_bytes_per_sample))) {
+               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);
+               sample_buffer_last_ -= sample_buffer_first_;
+               sample_buffer_first_ = 0;
+       }
+       sample_buffer_start = sample_buffer_ + sample_buffer_last_ * channels * file_info->sample_format_bytes_per_sample;
+       if(file_info->has_replaygain && flac_cfg.output.replaygain.enable) {
+               FLAC__replaygain_synthesis__apply_gain(
+                               sample_buffer_start,
+                               !is_big_endian_host_,
+                               file_info->sample_format_bytes_per_sample == 1, /* unsigned_data_out */
+                               buffer,
+                               wide_samples,
+                               channels,
+                               bits_per_sample,
+                               file_info->sample_format_bytes_per_sample * 8,
+                               file_info->replay_scale,
+                               flac_cfg.output.replaygain.hard_limit,
+                               flac_cfg.output.resolution.replaygain.dither,
+                               &file_info->dither_context
+               );
+       }
+       else if(is_big_endian_host_) {
+               FLAC__plugin_common__pack_pcm_signed_big_endian(
+                       sample_buffer_start,
+                       buffer,
+                       wide_samples,
+                       channels,
+                       bits_per_sample,
+                       file_info->sample_format_bytes_per_sample * 8
+               );
+       }
+       else {
+               FLAC__plugin_common__pack_pcm_signed_little_endian(
+                       sample_buffer_start,
+                       buffer,
+                       wide_samples,
+                       channels,
+                       bits_per_sample,
+                       file_info->sample_format_bytes_per_sample * 8
+               );
+       }
 
-       wide_samples_in_reservoir_ += wide_samples;
+       sample_buffer_last_ += wide_samples;
 
        return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
 }
@@ -459,44 +553,18 @@ void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMeta
        file_info_struct *file_info = (file_info_struct *)client_data;
        (void)decoder;
        if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
-               FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
+               FLAC__ASSERT(metadata->data.stream_info.total_samples < FLAC__U64L(0x100000000)); /* this plugin can only handle < 4 gigasamples */
                file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
                file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
                file_info->channels = metadata->data.stream_info.channels;
                file_info->sample_rate = metadata->data.stream_info.sample_rate;
-
-#ifdef FLAC__DO_DITHER
-               if(file_info->bits_per_sample == 8) {
-                       file_info->sample_format = FMT_S8;
-               }
-               else if(file_info->bits_per_sample == 16 || file_info->bits_per_sample == 24) {
-                       file_info->sample_format = FMT_S16_LE;
-               }
-               else {
-                       /*@@@ 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); */
-                       file_info->abort_flag = true;
-                       return;
-               }
-#else
-               if(file_info->bits_per_sample == 8) {
-                       file_info->sample_format = FMT_S8;
-               }
-               else if(file_info->bits_per_sample == 16) {
-                       file_info->sample_format = FMT_S16_LE;
-               }
-               else {
-                       /*@@@ 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); */
-                       file_info->abort_flag = true;
-                       return;
-               }
-#endif
-               file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
+               file_info->length_in_msec = (FLAC__uint64)file_info->total_samples * 10 / (file_info->sample_rate / 100);
        }
        else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
                double gain, peak;
                if(grabbag__replaygain_load_from_vorbiscomment(metadata, flac_cfg.output.replaygain.album_mode, &gain, &peak)) {
-                       file_info_.has_replaygain = true;
-                       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);
+                       file_info->has_replaygain = true;
+                       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);
                }
        }
 }