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