add support for synthesis to big-endian in plugins
[flac.git] / src / plugin_xmms / plugin.c
1 /* libxmms-flac - XMMS FLAC input plugin
2  * Copyright (C) 2000,2001,2002,2003  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <pthread.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <glib.h>
24
25 #include <xmms/plugin.h>
26 #include <xmms/util.h>
27 #include <xmms/configfile.h>
28 #include <xmms/titlestring.h>
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #ifdef HAVE_LANGINFO_CODESET
35 #include <langinfo.h>
36 #endif
37
38 #include "FLAC/all.h"
39 #include "plugin_common/all.h"
40 #include "share/grabbag.h"
41 #include "configure.h"
42 #include "wrap_id3.h"
43 #include "charset.h"
44
45 #ifdef min
46 #undef min
47 #endif
48 #define min(x,y) ((x)<(y)?(x):(y))
49
50 extern void FLAC_XMMS__file_info_box(char *filename);
51
52 typedef struct {
53         FLAC__bool abort_flag;
54         FLAC__bool is_playing;
55         FLAC__bool eof;
56         FLAC__bool play_thread_open; /* if true, is_playing must also be true */
57         unsigned total_samples;
58         unsigned bits_per_sample;
59         unsigned channels;
60         unsigned sample_rate;
61         unsigned length_in_msec;
62         gchar *title;
63         AFormat sample_format;
64         unsigned sample_format_bytes_per_sample;
65         int seek_to_in_sec;
66         FLAC__bool has_replaygain;
67         double replay_scale;
68         DitherContext dither_context;
69 } file_info_struct;
70
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);
80
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);
88
89 InputPlugin flac_ip =
90 {
91         NULL,
92         NULL,
93         "Reference FLAC Player v" VERSION,
94         FLAC_XMMS__init,
95         FLAC_XMMS__aboutbox,
96         FLAC_XMMS__configure,
97         FLAC_XMMS__is_our_file,
98         NULL,
99         FLAC_XMMS__play_file,
100         FLAC_XMMS__stop,
101         FLAC_XMMS__pause,
102         FLAC_XMMS__seek,
103         NULL,
104         FLAC_XMMS__get_time,
105         NULL,
106         NULL,
107         FLAC_XMMS__cleanup,
108         NULL,
109         NULL,
110         NULL,
111         NULL,
112         FLAC_XMMS__get_song_info,
113         FLAC_XMMS__file_info_box,
114         NULL
115 };
116
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_;
121
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;
126 static FLAC__bool is_big_endian_host_;
127
128 #define BITRATE_HIST_SEGMENT_MSEC 500
129 /* 500ms * 50 = 25s should be enough */
130 #define BITRATE_HIST_SIZE 50
131 static unsigned bitrate_history_[BITRATE_HIST_SIZE];
132
133
134 InputPlugin *get_iplugin_info()
135 {
136         flac_ip.description = g_strdup_printf("Reference FLAC Player v%s", FLAC__VERSION_STRING);
137         return &flac_ip;
138 }
139
140 void FLAC_XMMS__init()
141 {
142         ConfigFile *cfg;
143         FLAC__uint32 test = 1;
144  
145         is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
146
147         flac_cfg.title.tag_override = FALSE;
148         g_free(flac_cfg.title.tag_format);
149         flac_cfg.title.convert_char_set = FALSE;
150
151         cfg = xmms_cfg_open_default_file();
152
153         /* title */
154
155         xmms_cfg_read_boolean(cfg, "flac", "title.tag_override", &flac_cfg.title.tag_override);
156
157         if(!xmms_cfg_read_string(cfg, "flac", "title.tag_format", &flac_cfg.title.tag_format))
158                 flac_cfg.title.tag_format = g_strdup("%p - %t");
159
160         xmms_cfg_read_boolean(cfg, "flac", "title.convert_char_set", &flac_cfg.title.convert_char_set);
161
162         if(!xmms_cfg_read_string(cfg, "flac", "title.file_char_set", &flac_cfg.title.file_char_set))
163                 flac_cfg.title.file_char_set = FLAC_plugin__charset_get_current();
164
165         if(!xmms_cfg_read_string(cfg, "flac", "title.user_char_set", &flac_cfg.title.user_char_set))
166                 flac_cfg.title.user_char_set = FLAC_plugin__charset_get_current();
167
168         /* replaygain */
169
170         xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.enable", &flac_cfg.output.replaygain.enable);
171
172         xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.album_mode", &flac_cfg.output.replaygain.album_mode);
173
174         if(!xmms_cfg_read_int(cfg, "flac", "output.replaygain.preamp", &flac_cfg.output.replaygain.preamp))
175                 flac_cfg.output.replaygain.preamp = 0;
176
177         xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.hard_limit", &flac_cfg.output.replaygain.hard_limit);
178
179         xmms_cfg_read_boolean(cfg, "flac", "output.resolution.normal.dither_24_to_16", &flac_cfg.output.resolution.normal.dither_24_to_16);
180         xmms_cfg_read_boolean(cfg, "flac", "output.resolution.replaygain.dither", &flac_cfg.output.resolution.replaygain.dither);
181
182         if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.noise_shaping", &flac_cfg.output.resolution.replaygain.noise_shaping))
183                 flac_cfg.output.resolution.replaygain.noise_shaping = 1;
184
185         if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.bps_out", &flac_cfg.output.resolution.replaygain.bps_out))
186                 flac_cfg.output.resolution.replaygain.bps_out = 16;
187
188         decoder_ = FLAC__file_decoder_new();
189
190         xmms_cfg_free(cfg);
191 }
192
193 int FLAC_XMMS__is_our_file(char *filename)
194 {
195         char *ext;
196
197         ext = strrchr(filename, '.');
198         if(ext)
199                 if(!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
200                         return 1;
201         return 0;
202 }
203
204 void FLAC_XMMS__play_file(char *filename)
205 {
206         FILE *f;
207
208         sample_buffer_first_ = sample_buffer_last_ = 0;
209         audio_error_ = false;
210         file_info_.abort_flag = false;
211         file_info_.is_playing = false;
212         file_info_.eof = false;
213         file_info_.play_thread_open = false;
214         file_info_.has_replaygain = false;
215
216         if(0 == (f = fopen(filename, "r")))
217                 return;
218         fclose(f);
219
220         if(decoder_ == 0)
221                 return;
222
223         if(!safe_decoder_init_(filename, decoder_))
224                 return;
225
226         if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable) {
227                 if(flac_cfg.output.resolution.replaygain.bps_out == 8) {
228                         file_info_.sample_format = FMT_U8;
229                         file_info_.sample_format_bytes_per_sample = 1;
230                 }
231                 else if(flac_cfg.output.resolution.replaygain.bps_out == 16) {
232                         file_info_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
233                         file_info_.sample_format_bytes_per_sample = 2;
234                 }
235                 else {
236                         /*@@@ 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); */
237                         fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", flac_cfg.output.resolution.replaygain.bps_out);
238                         safe_decoder_finish_(decoder_);
239                         return;
240                 }
241         }
242         else {
243                 if(file_info_.bits_per_sample == 8) {
244                         file_info_.sample_format = FMT_U8;
245                         file_info_.sample_format_bytes_per_sample = 1;
246                 }
247                 else if(file_info_.bits_per_sample == 16 || (file_info_.bits_per_sample == 24 && flac_cfg.output.resolution.normal.dither_24_to_16)) {
248                         file_info_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
249                         file_info_.sample_format_bytes_per_sample = 2;
250                 }
251                 else {
252                         /*@@@ 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); */
253                         fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", file_info_.bits_per_sample);
254                         safe_decoder_finish_(decoder_);
255                         return;
256                 }
257         }
258         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);
259         file_info_.is_playing = true;
260
261         if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
262                 audio_error_ = true;
263                 safe_decoder_finish_(decoder_);
264                 return;
265         }
266
267         file_info_.title = flac_format_song_title(filename);
268         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);
269
270         file_info_.seek_to_in_sec = -1;
271         file_info_.play_thread_open = true;
272         pthread_create(&decode_thread_, NULL, play_loop_, NULL);
273 }
274
275 void FLAC_XMMS__stop()
276 {
277         if(file_info_.is_playing) {
278                 file_info_.is_playing = false;
279                 if(file_info_.play_thread_open) {
280                         file_info_.play_thread_open = false;
281                         pthread_join(decode_thread_, NULL);
282                 }
283                 flac_ip.output->close_audio();
284                 safe_decoder_finish_(decoder_);
285         }
286 }
287
288 void FLAC_XMMS__pause(short p)
289 {
290         flac_ip.output->pause(p);
291 }
292
293 void FLAC_XMMS__seek(int time)
294 {
295         file_info_.seek_to_in_sec = time;
296         file_info_.eof = false;
297
298         while(file_info_.seek_to_in_sec != -1)
299                 xmms_usleep(10000);
300 }
301
302 int FLAC_XMMS__get_time()
303 {
304         if(audio_error_)
305                 return -2;
306         if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
307                 return -1;
308         else
309                 return flac_ip.output->output_time();
310 }
311
312 void FLAC_XMMS__cleanup()
313 {
314         safe_decoder_delete_(decoder_);
315         decoder_ = 0;
316 }
317
318 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
319 {
320         FLAC__StreamMetadata streaminfo;
321
322         if(0 == filename)
323                 filename = "";
324
325         if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
326                 /* @@@ how to report the error? */
327                 if(title) {
328                         static const char *errtitle = "Invalid FLAC File: ";
329                         *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
330                         sprintf(*title, "%s\"%s\"", errtitle, filename);
331                 }
332                 if(length_in_msec)
333                         *length_in_msec = -1;
334                 return;
335         }
336
337         if(title) {
338                 *title = flac_format_song_title(filename);
339         }
340         if(length_in_msec)
341                 *length_in_msec = streaminfo.data.stream_info.total_samples * 10 / (streaminfo.data.stream_info.sample_rate / 100);
342 }
343
344 /***********************************************************************
345  * local routines
346  **********************************************************************/
347
348 void *play_loop_(void *arg)
349 {
350         unsigned written_time_last = 0, bh_index_last_w = 0, bh_index_last_o = BITRATE_HIST_SIZE, blocksize = 1;
351         FLAC__uint64 decode_position_last = 0, decode_position_frame_last = 0, decode_position_frame = 0;
352
353         (void)arg;
354
355         while(file_info_.is_playing) {
356                 if(!file_info_.eof) {
357                         while(sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE) {
358                                 unsigned s;
359
360                                 s = sample_buffer_last_ - sample_buffer_first_;
361                                 if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
362                                         file_info_.eof = true;
363                                         break;
364                                 }
365                                 else if(!FLAC__file_decoder_process_single(decoder_)) {
366                                         /*@@@ this should probably be a dialog */
367                                         fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
368                                         file_info_.eof = true;
369                                         break;
370                                 }
371                                 blocksize = sample_buffer_last_ - sample_buffer_first_ - s;
372                                 decode_position_frame_last = decode_position_frame;
373                                 if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
374                                         decode_position_frame = 0;
375                         }
376                         if(sample_buffer_last_ - sample_buffer_first_ > 0) {
377                                 const unsigned n = min(sample_buffer_last_ - sample_buffer_first_, SAMPLES_PER_WRITE);
378                                 int bytes = n * file_info_.channels * file_info_.sample_format_bytes_per_sample;
379                                 FLAC__byte *sample_buffer_start = sample_buffer_ + sample_buffer_first_ * file_info_.channels * file_info_.sample_format_bytes_per_sample;
380                                 unsigned written_time, bh_index_w;
381                                 FLAC__uint64 decode_position;
382
383                                 sample_buffer_first_ += n;
384                                 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, file_info_.channels, bytes, sample_buffer_start);
385                                 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
386                                         xmms_usleep(10000);
387                                 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
388                                         flac_ip.output->write_audio(sample_buffer_start, bytes);
389
390                                 /* compute current bitrate */
391
392                                 written_time = flac_ip.output->written_time();
393                                 bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
394                                 if(bh_index_w != bh_index_last_w) {
395                                         bh_index_last_w = bh_index_w;
396                                         decode_position = decode_position_frame - (double)(sample_buffer_last_ - sample_buffer_first_) * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize;
397                                         bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] =
398                                                 decode_position > decode_position_last && written_time > written_time_last ?
399                                                         8000 * (decode_position - decode_position_last) / (written_time - written_time_last) :
400                                                         file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample;
401                                         decode_position_last = decode_position;
402                                         written_time_last = written_time;
403                                 }
404                         }
405                         else {
406                                 file_info_.eof = true;
407                                 xmms_usleep(10000);
408                         }
409                 }
410                 else
411                         xmms_usleep(10000);
412                 if(file_info_.seek_to_in_sec != -1) {
413                         const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
414                         unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
415                         if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
416                                 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
417                                 bh_index_last_w = bh_index_last_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
418                                 if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
419                                         decode_position_frame = 0;
420                                 file_info_.seek_to_in_sec = -1;
421                                 file_info_.eof = false;
422                                 sample_buffer_first_ = sample_buffer_last_ = 0;
423                         }
424                 }
425                 else {
426                         /* display the right bitrate from history */
427                         unsigned bh_index_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
428                         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) {
429                                 bh_index_last_o = bh_index_o;
430                                 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                         }
432                 }
433         }
434
435         safe_decoder_finish_(decoder_);
436
437         /* are these two calls necessary? */
438         flac_ip.output->buffer_free();
439         flac_ip.output->buffer_free();
440
441         g_free(file_info_.title);
442
443         pthread_exit(NULL);
444         return 0; /* to silence the compiler warning about not returning a value */
445 }
446
447 FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder)
448 {
449         if(decoder == 0)
450                 return false;
451
452         safe_decoder_finish_(decoder);
453
454         FLAC__file_decoder_set_md5_checking(decoder, false);
455         FLAC__file_decoder_set_filename(decoder, filename);
456         FLAC__file_decoder_set_metadata_ignore_all(decoder);
457         FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
458         FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
459         FLAC__file_decoder_set_write_callback(decoder, write_callback_);
460         FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
461         FLAC__file_decoder_set_error_callback(decoder, error_callback_);
462         FLAC__file_decoder_set_client_data(decoder, &file_info_);
463         if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
464                 return false;
465
466         if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
467                 return false;
468
469         return true;
470 }
471
472 void safe_decoder_finish_(FLAC__FileDecoder *decoder)
473 {
474         if(decoder && FLAC__file_decoder_get_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
475                 FLAC__file_decoder_finish(decoder);
476 }
477
478 void safe_decoder_delete_(FLAC__FileDecoder *decoder)
479 {
480         if(decoder) {
481                 safe_decoder_finish_(decoder);
482                 FLAC__file_decoder_delete(decoder);
483         }
484 }
485
486 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
487 {
488         file_info_struct *file_info = (file_info_struct *)client_data;
489         const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
490         const unsigned bits_per_sample = file_info->bits_per_sample;
491         FLAC__byte *sample_buffer_start;
492
493         (void)decoder;
494
495         if(file_info->abort_flag)
496                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
497
498         if((sample_buffer_last_ + wide_samples) > (SAMPLE_BUFFER_SIZE / (channels * file_info->sample_format_bytes_per_sample))) {
499                 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);
500                 sample_buffer_last_ -= sample_buffer_first_;
501                 sample_buffer_first_ = 0;
502         }
503         sample_buffer_start = sample_buffer_ + sample_buffer_last_ * channels * file_info->sample_format_bytes_per_sample;
504         if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable) {
505                 FLAC__plugin_common__apply_gain(
506                                 sample_buffer_start,
507                                 !is_big_endian_host_,
508                                 buffer,
509                                 wide_samples,
510                                 channels,
511                                 bits_per_sample,
512                                 file_info->sample_format_bytes_per_sample * 8,
513                                 file_info_.replay_scale,
514                                 flac_cfg.output.replaygain.hard_limit,
515                                 flac_cfg.output.resolution.replaygain.dither,
516                                 &file_info_.dither_context
517                 );
518         }
519         else if(is_big_endian_host_) {
520                 FLAC__plugin_common__pack_pcm_signed_big_endian(
521                         sample_buffer_start,
522                         buffer,
523                         wide_samples,
524                         channels,
525                         bits_per_sample,
526                         file_info->sample_format_bytes_per_sample * 8
527                 );
528         }
529         else {
530                 FLAC__plugin_common__pack_pcm_signed_little_endian(
531                         sample_buffer_start,
532                         buffer,
533                         wide_samples,
534                         channels,
535                         bits_per_sample,
536                         file_info->sample_format_bytes_per_sample * 8
537                 );
538         }
539
540         sample_buffer_last_ += wide_samples;
541
542         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
543 }
544
545 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
546 {
547         file_info_struct *file_info = (file_info_struct *)client_data;
548         (void)decoder;
549         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
550                 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
551                 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
552                 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
553                 file_info->channels = metadata->data.stream_info.channels;
554                 file_info->sample_rate = metadata->data.stream_info.sample_rate;
555                 file_info->length_in_msec = (FLAC__uint64)file_info->total_samples * 10 / (file_info->sample_rate / 100);
556         }
557         else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
558                 double gain, peak;
559                 if(grabbag__replaygain_load_from_vorbiscomment(metadata, flac_cfg.output.replaygain.album_mode, &gain, &peak)) {
560                         file_info_.has_replaygain = true;
561                         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);
562                 }
563         }
564 }
565
566 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
567 {
568         file_info_struct *file_info = (file_info_struct *)client_data;
569         (void)decoder;
570         if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
571                 file_info->abort_flag = true;
572 }