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