comparison src/flac113/plugin.c @ 103:117bc56d906b trunk

[svn] flac -> flac113
author nenolod
date Mon, 23 Oct 2006 19:51:39 -0700
parents src/flac/plugin.c@a19f24790f3c
children 063e31ae8f92
comparison
equal deleted inserted replaced
102:aff1cf3e86dd 103:117bc56d906b
1 /* libxmms-flac - XMMS FLAC input plugin
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include <limits.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <glib.h>
24 #include <pwd.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include "audacious/plugin.h"
29 #include "audacious/output.h"
30 #include "audacious/util.h"
31 #include "audacious/configdb.h"
32 #include "audacious/titlestring.h"
33 #include "audacious/vfs.h"
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #ifdef HAVE_LANGINFO_CODESET
40 #include <langinfo.h>
41 #endif
42
43 #include "FLAC/all.h"
44 #include "plugin_common/all.h"
45 #include "grabbag.h"
46 #include "replaygain_synthesis.h"
47 #include "configure.h"
48 #include "charset.h"
49 #include "http.h"
50 #include "tag.h"
51
52 #ifdef min
53 #undef min
54 #endif
55 #define min(x,y) ((x)<(y)?(x):(y))
56
57 extern void FLAC_XMMS__file_info_box(char *filename);
58
59 typedef struct {
60 FLAC__bool abort_flag;
61 FLAC__bool is_playing;
62 FLAC__bool is_http_source;
63 FLAC__bool eof;
64 FLAC__bool play_thread_open; /* if true, is_playing must also be true */
65 FLAC__uint64 total_samples;
66 unsigned bits_per_sample;
67 unsigned channels;
68 unsigned sample_rate;
69 int length_in_msec; /* int (instead of FLAC__uint64) only because that's what XMMS uses; seeking won't work right if this maxes out */
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 } stream_data_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
91 static FLAC__bool safe_decoder_init_(char *filename, FLAC__StreamDecoder *decoder);
92 static void safe_decoder_finish_(FLAC__StreamDecoder *decoder);
93 static void safe_decoder_delete_(FLAC__StreamDecoder *decoder);
94
95 static FLAC__StreamDecoderReadStatus http_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
96 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
97 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
98 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
99
100 InputPlugin flac_ip =
101 {
102 NULL,
103 NULL,
104 NULL,
105 FLAC_XMMS__init,
106 FLAC_XMMS__aboutbox,
107 FLAC_XMMS__configure,
108 FLAC_XMMS__is_our_file,
109 NULL,
110 FLAC_XMMS__play_file,
111 FLAC_XMMS__stop,
112 FLAC_XMMS__pause,
113 FLAC_XMMS__seek,
114 NULL,
115 FLAC_XMMS__get_time,
116 NULL,
117 NULL,
118 FLAC_XMMS__cleanup,
119 NULL,
120 NULL,
121 NULL,
122 NULL,
123 FLAC_XMMS__get_song_info,
124 FLAC_XMMS__file_info_box,
125 NULL,
126 flac_get_tuple
127 };
128
129 #define SAMPLES_PER_WRITE 512
130 #define SAMPLE_BUFFER_SIZE ((FLAC__MAX_BLOCK_SIZE + SAMPLES_PER_WRITE) * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8))
131 static FLAC__byte sample_buffer_[SAMPLE_BUFFER_SIZE];
132 static unsigned sample_buffer_first_, sample_buffer_last_;
133
134 static FLAC__StreamDecoder *decoder_ = 0, *decoder2 = 0;
135 static stream_data_struct stream_data_;
136 static GThread *decode_thread_;
137 static FLAC__bool audio_error_ = false;
138 static FLAC__bool is_big_endian_host_;
139
140 #define BITRATE_HIST_SEGMENT_MSEC 500
141 /* 500ms * 50 = 25s should be enough */
142 #define BITRATE_HIST_SIZE 50
143 static unsigned bitrate_history_[BITRATE_HIST_SIZE];
144
145 #ifdef SUPPORT_ATTRIBUTE_VISIBILITY
146 InputPlugin *get_iplugin_info() __attribute__((visibility("default")));
147 #endif
148
149 InputPlugin *get_iplugin_info()
150 {
151 flac_ip.description = g_strdup_printf(_("FLAC Audio Plugin"));
152 return &flac_ip;
153 }
154
155 void set_track_info(const char* title, int length_in_msec)
156 {
157 if (stream_data_.is_playing) {
158 flac_ip.set_info((char*) title, length_in_msec, stream_data_.sample_rate * stream_data_.channels * stream_data_.bits_per_sample, stream_data_.sample_rate, stream_data_.channels);
159 }
160 }
161
162 static gchar* homedir()
163 {
164 gchar *result;
165 char *env_home = getenv("HOME");
166 if (env_home) {
167 result = g_strdup (env_home);
168 } else {
169 uid_t uid = getuid();
170 struct passwd *pwent;
171 do {
172 pwent = getpwent();
173 } while (pwent && pwent->pw_uid != uid);
174 result = pwent ? g_strdup (pwent->pw_dir) : NULL;
175 endpwent();
176 }
177 return result;
178 }
179
180 static FLAC__bool is_http_source(const char *source)
181 {
182 return 0 == strncasecmp(source, "http://", 7);
183 }
184
185 void FLAC_XMMS__init()
186 {
187 ConfigDb *db;
188 FLAC__uint32 test = 1;
189 gchar *tmp;
190
191 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
192
193 flac_cfg.title.tag_override = FALSE;
194 if (flac_cfg.title.tag_format)
195 g_free(flac_cfg.title.tag_format);
196 flac_cfg.title.convert_char_set = FALSE;
197
198 db = bmp_cfg_db_open();
199
200 /* title */
201
202 bmp_cfg_db_get_bool(db, "flac", "title.tag_override", &flac_cfg.title.tag_override);
203
204 if(!bmp_cfg_db_get_string(db, "flac", "title.tag_format", &flac_cfg.title.tag_format))
205 flac_cfg.title.tag_format = g_strdup("%p - %t");
206
207 bmp_cfg_db_get_bool(db, "flac", "title.convert_char_set", &flac_cfg.title.convert_char_set);
208
209 if(!bmp_cfg_db_get_string(db, "flac", "title.user_char_set", &flac_cfg.title.user_char_set))
210 flac_cfg.title.user_char_set = FLAC_plugin__charset_get_current();
211
212 /* replaygain */
213
214 bmp_cfg_db_get_bool(db, "flac", "output.replaygain.enable", &flac_cfg.output.replaygain.enable);
215
216 bmp_cfg_db_get_bool(db, "flac", "output.replaygain.album_mode", &flac_cfg.output.replaygain.album_mode);
217
218 if(!bmp_cfg_db_get_int(db, "flac", "output.replaygain.preamp", &flac_cfg.output.replaygain.preamp))
219 flac_cfg.output.replaygain.preamp = 0;
220
221 bmp_cfg_db_get_bool(db, "flac", "output.replaygain.hard_limit", &flac_cfg.output.replaygain.hard_limit);
222
223 bmp_cfg_db_get_bool(db, "flac", "output.resolution.normal.dither_24_to_16", &flac_cfg.output.resolution.normal.dither_24_to_16);
224 bmp_cfg_db_get_bool(db, "flac", "output.resolution.replaygain.dither", &flac_cfg.output.resolution.replaygain.dither);
225
226 if(!bmp_cfg_db_get_int(db, "flac", "output.resolution.replaygain.noise_shaping", &flac_cfg.output.resolution.replaygain.noise_shaping))
227 flac_cfg.output.resolution.replaygain.noise_shaping = 1;
228
229 if(!bmp_cfg_db_get_int(db, "flac", "output.resolution.replaygain.bps_out", &flac_cfg.output.resolution.replaygain.bps_out))
230 flac_cfg.output.resolution.replaygain.bps_out = 16;
231
232 /* stream */
233
234 bmp_cfg_db_get_int(db, "flac", "stream.http_buffer_size", &flac_cfg.stream.http_buffer_size);
235 bmp_cfg_db_get_int(db, "flac", "stream.http_prebuffer", &flac_cfg.stream.http_prebuffer);
236 bmp_cfg_db_get_bool(db, "flac", "stream.save_http_stream", &flac_cfg.stream.save_http_stream);
237 if (!bmp_cfg_db_get_string(db, "flac", "stream.save_http_path", &flac_cfg.stream.save_http_path) ||
238 ! *flac_cfg.stream.save_http_path) {
239 /* TODO: Is this a memory leak ?? */
240 /*
241 if (flac_cfg.stream.save_http_path)
242 g_free (flac_cfg.stream.save_http_path);
243 */
244 flac_cfg.stream.save_http_path = homedir();
245 }
246 bmp_cfg_db_get_bool(db, "flac", "stream.cast_title_streaming", &flac_cfg.stream.cast_title_streaming);
247 bmp_cfg_db_get_bool(db, "flac", "stream.use_udp_channel", &flac_cfg.stream.use_udp_channel);
248
249 bmp_cfg_db_get_bool(db, NULL, "use_proxy", &flac_cfg.stream.use_proxy);
250 bmp_cfg_db_get_string(db, NULL, "proxy_host", &flac_cfg.stream.proxy_host);
251 bmp_cfg_db_get_string(db, NULL, "proxy_port", &tmp);
252
253 bmp_cfg_db_get_bool(db, NULL, "proxy_use_auth", &flac_cfg.stream.proxy_use_auth);
254 bmp_cfg_db_get_string(db, NULL, "proxy_user", &flac_cfg.stream.proxy_user);
255 bmp_cfg_db_get_string(db, NULL, "proxy_pass", &flac_cfg.stream.proxy_pass);
256
257 decoder_ = FLAC__stream_decoder_new();
258 bmp_cfg_db_close(db);
259 }
260
261 int FLAC_XMMS__is_our_file(char *filename)
262 {
263 FILE *f;
264 FLAC__StreamMetadata streaminfo;
265
266 if(!is_http_source(filename)) {
267 if(0 == (f = fopen(filename, "r")))
268 return 0;
269 fclose(f);
270 if(FLAC__metadata_get_streaminfo(filename, &streaminfo))
271 return 1;
272 return 0;
273 }
274
275 if(!safe_decoder_init_(filename, decoder2))
276 return 0;
277
278 safe_decoder_finish_(decoder2);
279 return 1;
280 }
281
282 void FLAC_XMMS__play_file(char *filename)
283 {
284 FILE *f;
285
286 sample_buffer_first_ = sample_buffer_last_ = 0;
287 audio_error_ = false;
288 stream_data_.abort_flag = false;
289 stream_data_.is_playing = false;
290 stream_data_.is_http_source = is_http_source(filename);
291 stream_data_.eof = false;
292 stream_data_.play_thread_open = false;
293 stream_data_.has_replaygain = false;
294
295 if(!is_http_source(filename)) {
296 if(0 == (f = fopen(filename, "r")))
297 return;
298 fclose(f);
299 }
300
301 if(decoder_ == 0)
302 return;
303
304 if(!safe_decoder_init_(filename, decoder_))
305 return;
306
307 if(stream_data_.has_replaygain && flac_cfg.output.replaygain.enable) {
308 if(flac_cfg.output.resolution.replaygain.bps_out == 8) {
309 stream_data_.sample_format = FMT_U8;
310 stream_data_.sample_format_bytes_per_sample = 1;
311 }
312 else if(flac_cfg.output.resolution.replaygain.bps_out == 16) {
313 stream_data_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
314 stream_data_.sample_format_bytes_per_sample = 2;
315 }
316 else {
317 /*@@@ 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); */
318 fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", flac_cfg.output.resolution.replaygain.bps_out);
319 safe_decoder_finish_(decoder_);
320 return;
321 }
322 }
323 else {
324 if(stream_data_.bits_per_sample == 8) {
325 stream_data_.sample_format = FMT_U8;
326 stream_data_.sample_format_bytes_per_sample = 1;
327 }
328 else if(stream_data_.bits_per_sample == 16 || (stream_data_.bits_per_sample == 24 && flac_cfg.output.resolution.normal.dither_24_to_16)) {
329 stream_data_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
330 stream_data_.sample_format_bytes_per_sample = 2;
331 }
332 else {
333 /*@@@ 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); */
334 fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", stream_data_.bits_per_sample);
335 safe_decoder_finish_(decoder_);
336 return;
337 }
338 }
339 FLAC__replaygain_synthesis__init_dither_context(&stream_data_.dither_context, stream_data_.sample_format_bytes_per_sample * 8, flac_cfg.output.resolution.replaygain.noise_shaping);
340 stream_data_.is_playing = true;
341
342 if(flac_ip.output->open_audio(stream_data_.sample_format, stream_data_.sample_rate, stream_data_.channels) == 0) {
343 audio_error_ = true;
344 safe_decoder_finish_(decoder_);
345 return;
346 }
347
348 stream_data_.title = flac_format_song_title(filename);
349 flac_ip.set_info(stream_data_.title, stream_data_.length_in_msec, stream_data_.sample_rate * stream_data_.channels * stream_data_.bits_per_sample, stream_data_.sample_rate, stream_data_.channels);
350
351 stream_data_.seek_to_in_sec = -1;
352 stream_data_.play_thread_open = true;
353 decode_thread_ = g_thread_create((GThreadFunc)play_loop_, NULL, TRUE, NULL);
354 }
355
356 void FLAC_XMMS__stop()
357 {
358 if(stream_data_.is_playing) {
359 stream_data_.is_playing = false;
360 if(stream_data_.play_thread_open) {
361 stream_data_.play_thread_open = false;
362 g_thread_join(decode_thread_);
363 }
364 flac_ip.output->close_audio();
365 safe_decoder_finish_(decoder_);
366 }
367 }
368
369 void FLAC_XMMS__pause(short p)
370 {
371 flac_ip.output->pause(p);
372 }
373
374 void FLAC_XMMS__seek(int time)
375 {
376 if(!stream_data_.is_http_source) {
377 stream_data_.seek_to_in_sec = time;
378 stream_data_.eof = false;
379
380 while(stream_data_.seek_to_in_sec != -1)
381 xmms_usleep(10000);
382 }
383 }
384
385 int FLAC_XMMS__get_time()
386 {
387 if(audio_error_)
388 return -2;
389 if(!stream_data_.is_playing || (stream_data_.eof && !flac_ip.output->buffer_playing()))
390 return -1;
391 else
392 return flac_ip.output->output_time();
393 }
394
395 void FLAC_XMMS__cleanup()
396 {
397 g_free(flac_ip.description);
398 flac_ip.description = NULL;
399
400 if (flac_cfg.title.tag_format) {
401 free(flac_cfg.title.tag_format);
402 flac_cfg.title.tag_format = NULL;
403 }
404
405 if (flac_cfg.title.user_char_set) {
406 free(flac_cfg.title.user_char_set);
407 flac_cfg.title.user_char_set = NULL;
408 }
409
410 if (flac_cfg.stream.proxy_host) {
411 free(flac_cfg.stream.proxy_host);
412 flac_cfg.stream.proxy_host = NULL;
413 }
414
415 if (flac_cfg.stream.proxy_user) {
416 free(flac_cfg.stream.proxy_user);
417 flac_cfg.stream.proxy_user = NULL;
418
419 }
420
421 if (flac_cfg.stream.proxy_pass) {
422 free(flac_cfg.stream.proxy_pass);
423 flac_cfg.stream.proxy_pass = NULL;
424 }
425
426 safe_decoder_delete_(decoder_);
427 decoder_ = 0;
428 }
429
430 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
431 {
432 FLAC__StreamMetadata streaminfo;
433
434 if(0 == filename)
435 filename = "";
436
437 if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
438 /* @@@ how to report the error? */
439 if(title) {
440 if (!is_http_source(filename)) {
441 static const char *errtitle = "Invalid FLAC File: ";
442 *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
443 sprintf(*title, "%s\"%s\"", errtitle, filename);
444 } else {
445 *title = NULL;
446 }
447 }
448 if(length_in_msec)
449 *length_in_msec = -1;
450 return;
451 }
452
453 if(title) {
454 *title = flac_format_song_title(filename);
455 }
456 if(length_in_msec) {
457 FLAC__uint64 l = (FLAC__uint64)((double)streaminfo.data.stream_info.total_samples / (double)streaminfo.data.stream_info.sample_rate * 1000.0 + 0.5);
458 if (l > INT_MAX)
459 l = INT_MAX;
460 *length_in_msec = (int)l;
461 }
462 }
463
464 /***********************************************************************
465 * local routines
466 **********************************************************************/
467
468 void *play_loop_(void *arg)
469 {
470 unsigned written_time_last = 0, bh_index_last_w = 0, bh_index_last_o = BITRATE_HIST_SIZE, blocksize = 1;
471 FLAC__uint64 decode_position_last = 0, decode_position_frame_last = 0, decode_position_frame = 0;
472
473 (void)arg;
474
475 while(stream_data_.is_playing) {
476 if(!stream_data_.eof) {
477 while(sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE) {
478 unsigned s;
479
480 s = sample_buffer_last_ - sample_buffer_first_;
481 if(FLAC__stream_decoder_get_state(decoder_) == FLAC__STREAM_DECODER_END_OF_STREAM) {
482 stream_data_.eof = true;
483 break;
484 }
485 else if(!FLAC__stream_decoder_process_single(decoder_)) {
486 /*@@@ this should probably be a dialog */
487 fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
488 stream_data_.eof = true;
489 break;
490 }
491 blocksize = sample_buffer_last_ - sample_buffer_first_ - s;
492 decode_position_frame_last = decode_position_frame;
493 if(stream_data_.is_http_source || !FLAC__stream_decoder_get_decode_position(decoder_, &decode_position_frame))
494 decode_position_frame = 0;
495 }
496 if(sample_buffer_last_ - sample_buffer_first_ > 0) {
497 const unsigned n = min(sample_buffer_last_ - sample_buffer_first_, SAMPLES_PER_WRITE);
498 int bytes = n * stream_data_.channels * stream_data_.sample_format_bytes_per_sample;
499 FLAC__byte *sample_buffer_start = sample_buffer_ + sample_buffer_first_ * stream_data_.channels * stream_data_.sample_format_bytes_per_sample;
500 unsigned written_time, bh_index_w;
501 FLAC__uint64 decode_position;
502
503 sample_buffer_first_ += n;
504 while(flac_ip.output->buffer_free() < (int)bytes && stream_data_.is_playing && stream_data_.seek_to_in_sec == -1)
505 xmms_usleep(10000);
506 if(stream_data_.is_playing && stream_data_.seek_to_in_sec == -1)
507 produce_audio(flac_ip.output->written_time(), stream_data_.sample_format,
508 stream_data_.channels, bytes, sample_buffer_start, NULL);
509
510 /* compute current bitrate */
511
512 written_time = flac_ip.output->written_time();
513 bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
514 if(bh_index_w != bh_index_last_w) {
515 bh_index_last_w = bh_index_w;
516 decode_position = decode_position_frame - (double)(sample_buffer_last_ - sample_buffer_first_) * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize;
517 bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] =
518 decode_position > decode_position_last && written_time > written_time_last ?
519 8000 * (decode_position - decode_position_last) / (written_time - written_time_last) :
520 stream_data_.sample_rate * stream_data_.channels * stream_data_.bits_per_sample;
521 decode_position_last = decode_position;
522 written_time_last = written_time;
523 }
524 }
525 else {
526 stream_data_.eof = true;
527 xmms_usleep(10000);
528 }
529 }
530 else
531 xmms_usleep(10000);
532 if(!stream_data_.is_http_source && stream_data_.seek_to_in_sec != -1) {
533 const double distance = (double)stream_data_.seek_to_in_sec * 1000.0 / (double)stream_data_.length_in_msec;
534 FLAC__uint64 target_sample = (FLAC__uint64)(distance * (double)stream_data_.total_samples);
535 if(stream_data_.total_samples > 0 && target_sample >= stream_data_.total_samples)
536 target_sample = stream_data_.total_samples - 1;
537 if(FLAC__stream_decoder_seek_absolute(decoder_, target_sample)) {
538 flac_ip.output->flush(stream_data_.seek_to_in_sec * 1000);
539 bh_index_last_w = bh_index_last_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
540 if(!FLAC__stream_decoder_get_decode_position(decoder_, &decode_position_frame))
541 decode_position_frame = 0;
542 stream_data_.eof = false;
543 sample_buffer_first_ = sample_buffer_last_ = 0;
544 }
545 else if(FLAC__stream_decoder_get_state(decoder_) == FLAC__STREAM_DECODER_SEEK_ERROR) {
546 /*@@@ this should probably be a dialog */
547 fprintf(stderr, "libxmms-flac: SEEK ERROR\n");
548 FLAC__stream_decoder_flush(decoder_);
549 stream_data_.eof = false;
550 sample_buffer_first_ = sample_buffer_last_ = 0;
551 }
552 stream_data_.seek_to_in_sec = -1;
553 }
554 else {
555 /* display the right bitrate from history */
556 unsigned bh_index_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
557 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) {
558 bh_index_last_o = bh_index_o;
559 flac_ip.set_info(stream_data_.title, stream_data_.length_in_msec, bitrate_history_[bh_index_o], stream_data_.sample_rate, stream_data_.channels);
560 }
561 }
562 }
563
564 safe_decoder_finish_(decoder_);
565
566 /* are these two calls necessary? */
567 flac_ip.output->buffer_free();
568 flac_ip.output->buffer_free();
569
570 g_free(stream_data_.title);
571
572 g_thread_exit(NULL);
573 return 0; /* to silence the compiler warning about not returning a value */
574 }
575
576 FLAC__bool safe_decoder_init_(char *filename, FLAC__StreamDecoder *decoder)
577 {
578 if(decoder == 0)
579 return false;
580
581 safe_decoder_finish_(decoder);
582
583 FLAC__stream_decoder_set_md5_checking(decoder, false);
584 FLAC__stream_decoder_set_metadata_ignore_all(decoder);
585 FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
586 FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
587 if(stream_data_.is_http_source) {
588 flac_http_open(filename, 0);
589 if(FLAC__stream_decoder_init_stream(decoder, http_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, write_callback_, metadata_callback_, error_callback_, /*client_data=*/&stream_data_) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
590 return false;
591 }
592 else {
593 if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, /*client_data=*/&stream_data_) != FLAC__STREAM_DECODER_INIT_STATUS_OK)
594 return false;
595 }
596
597 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
598 return false;
599
600 return true;
601 }
602
603 void safe_decoder_finish_(FLAC__StreamDecoder *decoder)
604 {
605 if(decoder && FLAC__stream_decoder_get_state(decoder) != FLAC__STREAM_DECODER_UNINITIALIZED)
606 FLAC__stream_decoder_finish(decoder);
607 if(stream_data_.is_http_source)
608 flac_http_close();
609 }
610
611 void safe_decoder_delete_(FLAC__StreamDecoder *decoder)
612 {
613 if(decoder) {
614 safe_decoder_finish_(decoder);
615 FLAC__stream_decoder_delete(decoder);
616 }
617 }
618
619 FLAC__StreamDecoderReadStatus http_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
620 {
621 (void)decoder;
622 (void)client_data;
623 *bytes = flac_http_read(buffer, *bytes);
624 return *bytes ? FLAC__STREAM_DECODER_READ_STATUS_CONTINUE : FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
625 }
626
627 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
628 {
629 stream_data_struct *stream_data = (stream_data_struct *)client_data;
630 const unsigned channels = stream_data->channels, wide_samples = frame->header.blocksize;
631 const unsigned bits_per_sample = stream_data->bits_per_sample;
632 FLAC__byte *sample_buffer_start;
633
634 (void)decoder;
635
636 if(stream_data->abort_flag)
637 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
638
639 if((sample_buffer_last_ + wide_samples) > (SAMPLE_BUFFER_SIZE / (channels * stream_data->sample_format_bytes_per_sample))) {
640 memmove(sample_buffer_, sample_buffer_ + sample_buffer_first_ * channels * stream_data->sample_format_bytes_per_sample, (sample_buffer_last_ - sample_buffer_first_) * channels * stream_data->sample_format_bytes_per_sample);
641 sample_buffer_last_ -= sample_buffer_first_;
642 sample_buffer_first_ = 0;
643 }
644 sample_buffer_start = sample_buffer_ + sample_buffer_last_ * channels * stream_data->sample_format_bytes_per_sample;
645 if(stream_data->has_replaygain && flac_cfg.output.replaygain.enable) {
646 FLAC__replaygain_synthesis__apply_gain(
647 sample_buffer_start,
648 !is_big_endian_host_,
649 stream_data->sample_format_bytes_per_sample == 1, /* unsigned_data_out */
650 buffer,
651 wide_samples,
652 channels,
653 bits_per_sample,
654 stream_data->sample_format_bytes_per_sample * 8,
655 stream_data->replay_scale,
656 flac_cfg.output.replaygain.hard_limit,
657 flac_cfg.output.resolution.replaygain.dither,
658 &stream_data->dither_context
659 );
660 }
661 else if(is_big_endian_host_) {
662 FLAC__plugin_common__pack_pcm_signed_big_endian(
663 sample_buffer_start,
664 buffer,
665 wide_samples,
666 channels,
667 bits_per_sample,
668 stream_data->sample_format_bytes_per_sample * 8
669 );
670 }
671 else {
672 FLAC__plugin_common__pack_pcm_signed_little_endian(
673 sample_buffer_start,
674 buffer,
675 wide_samples,
676 channels,
677 bits_per_sample,
678 stream_data->sample_format_bytes_per_sample * 8
679 );
680 }
681
682 sample_buffer_last_ += wide_samples;
683
684 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
685 }
686
687 void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
688 {
689 stream_data_struct *stream_data = (stream_data_struct *)client_data;
690 (void)decoder;
691 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
692 stream_data->total_samples = metadata->data.stream_info.total_samples;
693 stream_data->bits_per_sample = metadata->data.stream_info.bits_per_sample;
694 stream_data->channels = metadata->data.stream_info.channels;
695 stream_data->sample_rate = metadata->data.stream_info.sample_rate;
696 {
697 FLAC__uint64 l = (FLAC__uint64)((double)stream_data->total_samples / (double)stream_data->sample_rate * 1000.0 + 0.5);
698 if (l > INT_MAX)
699 l = INT_MAX;
700 stream_data->length_in_msec = (int)l;
701 }
702 }
703 else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
704 double reference, gain, peak;
705 if(grabbag__replaygain_load_from_vorbiscomment(metadata, flac_cfg.output.replaygain.album_mode, /*strict=*/false, &reference, &gain, &peak)) {
706 stream_data->has_replaygain = true;
707 stream_data->replay_scale = grabbag__replaygain_compute_scale_factor(peak, gain, (double)flac_cfg.output.replaygain.preamp, /*prevent_clipping=*/!flac_cfg.output.replaygain.hard_limit);
708 }
709 }
710 }
711
712 void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
713 {
714 stream_data_struct *stream_data = (stream_data_struct *)client_data;
715 (void)decoder;
716 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
717 stream_data->abort_flag = true;
718 }