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