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