Mercurial > audlegacy-plugins
comparison src/Input/flac/plugin.c @ 0:13389e613d67 trunk
[svn] - initial import of audacious-plugins tree (lots to do)
| author | nenolod |
|---|---|
| date | Mon, 18 Sep 2006 01:11:49 -0700 |
| parents | |
| children | 088092a52fea |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:13389e613d67 |
|---|---|
| 1 /* libxmms-flac - XMMS FLAC input plugin | |
| 2 * Copyright (C) 2000,2001,2002,2003,2004,2005 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 <stdlib.h> | |
| 20 #include <string.h> | |
| 21 #include <stdio.h> | |
| 22 #include <glib.h> | |
| 23 #include <pwd.h> | |
| 24 #include <sys/types.h> | |
| 25 #include <unistd.h> | |
| 26 | |
| 27 #include "audacious/plugin.h" | |
| 28 #include "audacious/output.h" | |
| 29 #include "libaudacious/util.h" | |
| 30 #include "libaudacious/configdb.h" | |
| 31 #include "libaudacious/titlestring.h" | |
| 32 #include "libaudacious/vfs.h" | |
| 33 | |
| 34 #ifdef HAVE_CONFIG_H | |
| 35 #include <config.h> | |
| 36 #endif | |
| 37 | |
| 38 #ifdef HAVE_LANGINFO_CODESET | |
| 39 #include <langinfo.h> | |
| 40 #endif | |
| 41 | |
| 42 #include "FLAC/all.h" | |
| 43 #include "plugin_common/all.h" | |
| 44 #include "grabbag.h" | |
| 45 #include "replaygain_synthesis.h" | |
| 46 #include "configure.h" | |
| 47 #include "charset.h" | |
| 48 #include "http.h" | |
| 49 #include "tag.h" | |
| 50 | |
| 51 #ifdef min | |
| 52 #undef min | |
| 53 #endif | |
| 54 #define min(x,y) ((x)<(y)?(x):(y)) | |
| 55 | |
| 56 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */ | |
| 57 #ifdef _MSC_VER | |
| 58 #define FLAC__U64L(x) x | |
| 59 #else | |
| 60 #define FLAC__U64L(x) x##LLU | |
| 61 #endif | |
| 62 | |
| 63 extern void FLAC_XMMS__file_info_box(char *filename); | |
| 64 | |
| 65 typedef struct { | |
| 66 FLAC__bool abort_flag; | |
| 67 FLAC__bool is_playing; | |
| 68 FLAC__bool eof; | |
| 69 FLAC__bool play_thread_open; /* if true, is_playing must also be true */ | |
| 70 unsigned total_samples; | |
| 71 unsigned bits_per_sample; | |
| 72 unsigned channels; | |
| 73 unsigned sample_rate; | |
| 74 unsigned length_in_msec; | |
| 75 gchar *title; | |
| 76 AFormat sample_format; | |
| 77 unsigned sample_format_bytes_per_sample; | |
| 78 int seek_to_in_sec; | |
| 79 FLAC__bool has_replaygain; | |
| 80 double replay_scale; | |
| 81 DitherContext dither_context; | |
| 82 } file_info_struct; | |
| 83 | |
| 84 typedef FLAC__StreamDecoderWriteStatus (*WriteCallback) (const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); | |
| 85 typedef void (*MetadataCallback) (const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data); | |
| 86 typedef void (*ErrorCallback) (const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); | |
| 87 | |
| 88 typedef struct { | |
| 89 FLAC__bool seekable; | |
| 90 void* (*new_decoder) (void); | |
| 91 FLAC__bool (*set_md5_checking) (void *decoder, FLAC__bool value); | |
| 92 FLAC__bool (*set_source) (void *decoder, const char* source); | |
| 93 FLAC__bool (*set_metadata_ignore_all) (void *decoder); | |
| 94 FLAC__bool (*set_metadata_respond) (void *decoder, FLAC__MetadataType type); | |
| 95 FLAC__bool (*set_write_callback) (void *decoder, WriteCallback value); | |
| 96 FLAC__bool (*set_metadata_callback) (void *decoder, MetadataCallback value); | |
| 97 FLAC__bool (*set_error_callback) (void *decoder, ErrorCallback value); | |
| 98 FLAC__bool (*set_client_data) (void *decoder, void *value); | |
| 99 FLAC__bool (*decoder_init) (void *decoder); | |
| 100 void (*safe_decoder_finish) (void *decoder); | |
| 101 void (*safe_decoder_delete) (void *decoder); | |
| 102 FLAC__bool (*process_until_end_of_metadata) (void *decoder); | |
| 103 FLAC__bool (*process_single) (void *decoder); | |
| 104 FLAC__bool (*is_eof) (void *decoder); | |
| 105 } decoder_funcs_t; | |
| 106 | |
| 107 #define NUM_DECODER_TYPES 2 | |
| 108 typedef enum { | |
| 109 DECODER_FILE, | |
| 110 DECODER_HTTP | |
| 111 } decoder_t; | |
| 112 | |
| 113 static void FLAC_XMMS__init(); | |
| 114 static int FLAC_XMMS__is_our_file(char *filename); | |
| 115 static void FLAC_XMMS__play_file(char *filename); | |
| 116 static void FLAC_XMMS__stop(); | |
| 117 static void FLAC_XMMS__pause(short p); | |
| 118 static void FLAC_XMMS__seek(int time); | |
| 119 static int FLAC_XMMS__get_time(); | |
| 120 static void FLAC_XMMS__cleanup(); | |
| 121 static void FLAC_XMMS__get_song_info(char *filename, char **title, int *length); | |
| 122 | |
| 123 static void *play_loop_(void *arg); | |
| 124 | |
| 125 static FLAC__bool safe_decoder_init_(const char *filename, void **decoderp, decoder_funcs_t const ** fnsp); | |
| 126 static void file_decoder_safe_decoder_finish_(void *decoder); | |
| 127 static void file_decoder_safe_decoder_delete_(void *decoder); | |
| 128 static FLAC__StreamDecoderWriteStatus write_callback_(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); | |
| 129 static void metadata_callback_(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data); | |
| 130 static void error_callback_(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data); | |
| 131 | |
| 132 static void init_decoder_func_tables(); | |
| 133 static decoder_t source_to_decoder_type (const char *source); | |
| 134 | |
| 135 InputPlugin flac_ip = | |
| 136 { | |
| 137 NULL, | |
| 138 NULL, | |
| 139 NULL, | |
| 140 FLAC_XMMS__init, | |
| 141 FLAC_XMMS__aboutbox, | |
| 142 FLAC_XMMS__configure, | |
| 143 FLAC_XMMS__is_our_file, | |
| 144 NULL, | |
| 145 FLAC_XMMS__play_file, | |
| 146 FLAC_XMMS__stop, | |
| 147 FLAC_XMMS__pause, | |
| 148 FLAC_XMMS__seek, | |
| 149 NULL, | |
| 150 FLAC_XMMS__get_time, | |
| 151 NULL, | |
| 152 NULL, | |
| 153 FLAC_XMMS__cleanup, | |
| 154 NULL, | |
| 155 NULL, | |
| 156 NULL, | |
| 157 NULL, | |
| 158 FLAC_XMMS__get_song_info, | |
| 159 FLAC_XMMS__file_info_box, | |
| 160 NULL, | |
| 161 flac_get_tuple | |
| 162 }; | |
| 163 | |
| 164 #define SAMPLES_PER_WRITE 512 | |
| 165 #define SAMPLE_BUFFER_SIZE ((FLAC__MAX_BLOCK_SIZE + SAMPLES_PER_WRITE) * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8)) | |
| 166 static FLAC__byte sample_buffer_[SAMPLE_BUFFER_SIZE]; | |
| 167 static unsigned sample_buffer_first_, sample_buffer_last_; | |
| 168 | |
| 169 static void *decoder_ = 0, *decoder2 = 0; | |
| 170 static file_info_struct file_info_; | |
| 171 static GThread *decode_thread_; | |
| 172 static FLAC__bool audio_error_ = false; | |
| 173 static FLAC__bool is_big_endian_host_; | |
| 174 | |
| 175 #define BITRATE_HIST_SEGMENT_MSEC 500 | |
| 176 /* 500ms * 50 = 25s should be enough */ | |
| 177 #define BITRATE_HIST_SIZE 50 | |
| 178 static unsigned bitrate_history_[BITRATE_HIST_SIZE]; | |
| 179 | |
| 180 /* A table of sets of decoder functions, indexed by decoder_t */ | |
| 181 static const decoder_funcs_t* DECODER_FUNCS[NUM_DECODER_TYPES]; | |
| 182 | |
| 183 static decoder_funcs_t const * decoder_func_table_, * decoder_func_table2; | |
| 184 | |
| 185 | |
| 186 InputPlugin *get_iplugin_info() | |
| 187 { | |
| 188 flac_ip.description = g_strdup_printf(_("FLAC Audio Plugin")); | |
| 189 return &flac_ip; | |
| 190 } | |
| 191 | |
| 192 void set_track_info(const char* title, int length_in_msec) | |
| 193 { | |
| 194 if (file_info_.is_playing) { | |
| 195 flac_ip.set_info((char*) title, length_in_msec, file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample, file_info_.sample_rate, file_info_.channels); | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 static gchar* homedir() | |
| 200 { | |
| 201 gchar *result; | |
| 202 char *env_home = getenv("HOME"); | |
| 203 if (env_home) { | |
| 204 result = g_strdup (env_home); | |
| 205 } else { | |
| 206 uid_t uid = getuid(); | |
| 207 struct passwd *pwent; | |
| 208 do { | |
| 209 pwent = getpwent(); | |
| 210 } while (pwent && pwent->pw_uid != uid); | |
| 211 result = pwent ? g_strdup (pwent->pw_dir) : NULL; | |
| 212 endpwent(); | |
| 213 } | |
| 214 return result; | |
| 215 } | |
| 216 | |
| 217 void FLAC_XMMS__init() | |
| 218 { | |
| 219 ConfigDb *db; | |
| 220 FLAC__uint32 test = 1; | |
| 221 gchar *tmp = NULL; | |
| 222 | |
| 223 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true; | |
| 224 | |
| 225 memset(&flac_cfg, 0, sizeof(flac_cfg)); | |
| 226 | |
| 227 db = bmp_cfg_db_open(); | |
| 228 | |
| 229 /* title */ | |
| 230 | |
| 231 bmp_cfg_db_get_bool(db, "flac", "title.tag_override", &flac_cfg.title.tag_override); | |
| 232 | |
| 233 if(!bmp_cfg_db_get_string(db, "flac", "title.tag_format", &flac_cfg.title.tag_format)) | |
| 234 flac_cfg.title.tag_format = g_strdup("%p - %t"); | |
| 235 | |
| 236 bmp_cfg_db_get_bool(db, "flac", "title.convert_char_set", &flac_cfg.title.convert_char_set); | |
| 237 | |
| 238 if(!bmp_cfg_db_get_string(db, "flac", "title.user_char_set", &flac_cfg.title.user_char_set)) | |
| 239 flac_cfg.title.user_char_set = FLAC_plugin__charset_get_current(); | |
| 240 | |
| 241 /* replaygain */ | |
| 242 | |
| 243 bmp_cfg_db_get_bool(db, "flac", "output.replaygain.enable", &flac_cfg.output.replaygain.enable); | |
| 244 | |
| 245 bmp_cfg_db_get_bool(db, "flac", "output.replaygain.album_mode", &flac_cfg.output.replaygain.album_mode); | |
| 246 | |
| 247 if(!bmp_cfg_db_get_int(db, "flac", "output.replaygain.preamp", &flac_cfg.output.replaygain.preamp)) | |
| 248 flac_cfg.output.replaygain.preamp = 0; | |
| 249 | |
| 250 bmp_cfg_db_get_bool(db, "flac", "output.replaygain.hard_limit", &flac_cfg.output.replaygain.hard_limit); | |
| 251 | |
| 252 bmp_cfg_db_get_bool(db, "flac", "output.resolution.normal.dither_24_to_16", &flac_cfg.output.resolution.normal.dither_24_to_16); | |
| 253 bmp_cfg_db_get_bool(db, "flac", "output.resolution.replaygain.dither", &flac_cfg.output.resolution.replaygain.dither); | |
| 254 | |
| 255 if(!bmp_cfg_db_get_int(db, "flac", "output.resolution.replaygain.noise_shaping", &flac_cfg.output.resolution.replaygain.noise_shaping)) | |
| 256 flac_cfg.output.resolution.replaygain.noise_shaping = 1; | |
| 257 | |
| 258 if(!bmp_cfg_db_get_int(db, "flac", "output.resolution.replaygain.bps_out", &flac_cfg.output.resolution.replaygain.bps_out)) | |
| 259 flac_cfg.output.resolution.replaygain.bps_out = 16; | |
| 260 | |
| 261 /* stream */ | |
| 262 | |
| 263 bmp_cfg_db_get_int(db, "flac", "stream.http_buffer_size", &flac_cfg.stream.http_buffer_size); | |
| 264 bmp_cfg_db_get_int(db, "flac", "stream.http_prebuffer", &flac_cfg.stream.http_prebuffer); | |
| 265 bmp_cfg_db_get_bool(db, "flac", "stream.save_http_stream", &flac_cfg.stream.save_http_stream); | |
| 266 if (!bmp_cfg_db_get_string(db, "flac", "stream.save_http_path", &flac_cfg.stream.save_http_path) || | |
| 267 ! *flac_cfg.stream.save_http_path) { | |
| 268 /* TODO: Is this a memory leak ?? */ | |
| 269 /* | |
| 270 if (flac_cfg.stream.save_http_path) | |
| 271 g_free (flac_cfg.stream.save_http_path); | |
| 272 */ | |
| 273 flac_cfg.stream.save_http_path = homedir(); | |
| 274 } | |
| 275 bmp_cfg_db_get_bool(db, "flac", "stream.cast_title_streaming", &flac_cfg.stream.cast_title_streaming); | |
| 276 bmp_cfg_db_get_bool(db, "flac", "stream.use_udp_channel", &flac_cfg.stream.use_udp_channel); | |
| 277 | |
| 278 init_decoder_func_tables(); | |
| 279 decoder_func_table_ = DECODER_FUNCS [DECODER_FILE]; | |
| 280 decoder_ = decoder_func_table_ -> new_decoder(); | |
| 281 | |
| 282 bmp_cfg_db_get_bool(db, NULL, "use_proxy", &flac_cfg.stream.use_proxy); | |
| 283 bmp_cfg_db_get_string(db, NULL, "proxy_host", &flac_cfg.stream.proxy_host); | |
| 284 bmp_cfg_db_get_string(db, NULL, "proxy_port", &tmp); | |
| 285 | |
| 286 if (tmp != NULL) | |
| 287 flac_cfg.stream.proxy_port = atoi(tmp); | |
| 288 | |
| 289 bmp_cfg_db_get_bool(db, NULL, "proxy_use_auth", &flac_cfg.stream.proxy_use_auth); | |
| 290 bmp_cfg_db_get_string(db, NULL, "proxy_user", &flac_cfg.stream.proxy_user); | |
| 291 bmp_cfg_db_get_string(db, NULL, "proxy_pass", &flac_cfg.stream.proxy_pass); | |
| 292 | |
| 293 bmp_cfg_db_close(db); | |
| 294 } | |
| 295 | |
| 296 int FLAC_XMMS__is_our_file(char *filename) | |
| 297 { | |
| 298 FILE *f; | |
| 299 FLAC__StreamMetadata streaminfo; | |
| 300 | |
| 301 if (source_to_decoder_type (filename) == DECODER_FILE) { | |
| 302 if(0 == (f = fopen(filename, "r"))) | |
| 303 return 0; | |
| 304 fclose(f); | |
| 305 if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) | |
| 306 return 0; | |
| 307 return 1; | |
| 308 } | |
| 309 | |
| 310 if(!safe_decoder_init_(filename, &decoder2, &decoder_func_table2)) | |
| 311 return 0; | |
| 312 | |
| 313 decoder_func_table2 -> safe_decoder_finish(decoder2); | |
| 314 return 1; | |
| 315 } | |
| 316 | |
| 317 void FLAC_XMMS__play_file(char *filename) | |
| 318 { | |
| 319 FILE *f; | |
| 320 | |
| 321 sample_buffer_first_ = sample_buffer_last_ = 0; | |
| 322 audio_error_ = false; | |
| 323 file_info_.abort_flag = false; | |
| 324 file_info_.is_playing = false; | |
| 325 file_info_.eof = false; | |
| 326 file_info_.play_thread_open = false; | |
| 327 file_info_.has_replaygain = false; | |
| 328 | |
| 329 if (source_to_decoder_type (filename) == DECODER_FILE) { | |
| 330 if(0 == (f = fopen(filename, "r"))) | |
| 331 return; | |
| 332 fclose(f); | |
| 333 } | |
| 334 | |
| 335 if(decoder_ == 0) | |
| 336 return; | |
| 337 | |
| 338 if(!safe_decoder_init_(filename, &decoder_, &decoder_func_table_)) | |
| 339 return; | |
| 340 | |
| 341 if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable) { | |
| 342 if(flac_cfg.output.resolution.replaygain.bps_out == 8) { | |
| 343 file_info_.sample_format = FMT_U8; | |
| 344 file_info_.sample_format_bytes_per_sample = 1; | |
| 345 } | |
| 346 else if(flac_cfg.output.resolution.replaygain.bps_out == 16) { | |
| 347 file_info_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE; | |
| 348 file_info_.sample_format_bytes_per_sample = 2; | |
| 349 } | |
| 350 else { | |
| 351 /*@@@ 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); */ | |
| 352 fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", flac_cfg.output.resolution.replaygain.bps_out); | |
| 353 decoder_func_table_ -> safe_decoder_finish(decoder_); | |
| 354 return; | |
| 355 } | |
| 356 } | |
| 357 else { | |
| 358 if(file_info_.bits_per_sample == 8) { | |
| 359 file_info_.sample_format = FMT_U8; | |
| 360 file_info_.sample_format_bytes_per_sample = 1; | |
| 361 } | |
| 362 else if(file_info_.bits_per_sample == 16 || (file_info_.bits_per_sample == 24 && flac_cfg.output.resolution.normal.dither_24_to_16)) { | |
| 363 file_info_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE; | |
| 364 file_info_.sample_format_bytes_per_sample = 2; | |
| 365 } | |
| 366 else { | |
| 367 /*@@@ 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); */ | |
| 368 fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", file_info_.bits_per_sample); | |
| 369 decoder_func_table_ -> safe_decoder_finish(decoder_); | |
| 370 return; | |
| 371 } | |
| 372 } | |
| 373 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); | |
| 374 file_info_.is_playing = true; | |
| 375 | |
| 376 if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) { | |
| 377 audio_error_ = true; | |
| 378 decoder_func_table_ -> safe_decoder_finish(decoder_); | |
| 379 return; | |
| 380 } | |
| 381 | |
| 382 file_info_.title = flac_format_song_title(filename); | |
| 383 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); | |
| 384 | |
| 385 file_info_.seek_to_in_sec = -1; | |
| 386 file_info_.play_thread_open = true; | |
| 387 decode_thread_ = g_thread_create((GThreadFunc)play_loop_, NULL, TRUE, NULL); | |
| 388 } | |
| 389 | |
| 390 void FLAC_XMMS__stop() | |
| 391 { | |
| 392 if(file_info_.is_playing) { | |
| 393 file_info_.is_playing = false; | |
| 394 if(file_info_.play_thread_open) { | |
| 395 file_info_.play_thread_open = false; | |
| 396 g_thread_join(decode_thread_); | |
| 397 } | |
| 398 flac_ip.output->close_audio(); | |
| 399 decoder_func_table_ -> safe_decoder_finish (decoder_); | |
| 400 } | |
| 401 } | |
| 402 | |
| 403 void FLAC_XMMS__pause(short p) | |
| 404 { | |
| 405 flac_ip.output->pause(p); | |
| 406 } | |
| 407 | |
| 408 void FLAC_XMMS__seek(int time) | |
| 409 { | |
| 410 if (decoder_func_table_->seekable) { | |
| 411 file_info_.seek_to_in_sec = time; | |
| 412 file_info_.eof = false; | |
| 413 | |
| 414 while(file_info_.seek_to_in_sec != -1) | |
| 415 xmms_usleep(10000); | |
| 416 } | |
| 417 } | |
| 418 | |
| 419 int FLAC_XMMS__get_time() | |
| 420 { | |
| 421 if(audio_error_) | |
| 422 return -2; | |
| 423 if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing())) | |
| 424 return -1; | |
| 425 else | |
| 426 return flac_ip.output->output_time(); | |
| 427 } | |
| 428 | |
| 429 void FLAC_XMMS__cleanup() | |
| 430 { | |
| 431 g_free(flac_ip.description); | |
| 432 flac_ip.description = NULL; | |
| 433 | |
| 434 if (flac_cfg.title.tag_format) { | |
| 435 free(flac_cfg.title.tag_format); | |
| 436 flac_cfg.title.tag_format = NULL; | |
| 437 } | |
| 438 | |
| 439 if (flac_cfg.title.user_char_set) { | |
| 440 free(flac_cfg.title.user_char_set); | |
| 441 flac_cfg.title.user_char_set = NULL; | |
| 442 } | |
| 443 | |
| 444 if (flac_cfg.stream.proxy_host) { | |
| 445 free(flac_cfg.stream.proxy_host); | |
| 446 flac_cfg.stream.proxy_host = NULL; | |
| 447 } | |
| 448 | |
| 449 if (flac_cfg.stream.proxy_user) { | |
| 450 free(flac_cfg.stream.proxy_user); | |
| 451 flac_cfg.stream.proxy_user = NULL; | |
| 452 | |
| 453 } | |
| 454 | |
| 455 if (flac_cfg.stream.proxy_pass) { | |
| 456 free(flac_cfg.stream.proxy_pass); | |
| 457 flac_cfg.stream.proxy_pass = NULL; | |
| 458 } | |
| 459 | |
| 460 decoder_func_table_ -> safe_decoder_delete(decoder_); | |
| 461 decoder_ = 0; | |
| 462 } | |
| 463 | |
| 464 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec) | |
| 465 { | |
| 466 FLAC__StreamMetadata streaminfo; | |
| 467 | |
| 468 if(0 == filename) | |
| 469 filename = ""; | |
| 470 | |
| 471 if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) { | |
| 472 /* @@@ how to report the error? */ | |
| 473 if(title) { | |
| 474 if (source_to_decoder_type (filename) == DECODER_FILE) { | |
| 475 static const char *errtitle = "Invalid FLAC File: "; | |
| 476 *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1); | |
| 477 sprintf(*title, "%s\"%s\"", errtitle, filename); | |
| 478 } else { | |
| 479 *title = NULL; | |
| 480 } | |
| 481 } | |
| 482 if(length_in_msec) | |
| 483 *length_in_msec = -1; | |
| 484 return; | |
| 485 } | |
| 486 | |
| 487 if(title) { | |
| 488 *title = flac_format_song_title(filename); | |
| 489 } | |
| 490 if(length_in_msec) | |
| 491 *length_in_msec = (unsigned)((double)streaminfo.data.stream_info.total_samples / (double)streaminfo.data.stream_info.sample_rate * 1000.0 + 0.5); | |
| 492 } | |
| 493 | |
| 494 /*********************************************************************** | |
| 495 * local routines | |
| 496 **********************************************************************/ | |
| 497 | |
| 498 void *play_loop_(void *arg) | |
| 499 { | |
| 500 unsigned written_time_last = 0, bh_index_last_w = 0, bh_index_last_o = BITRATE_HIST_SIZE, blocksize = 1; | |
| 501 FLAC__uint64 decode_position_last = 0, decode_position_frame_last = 0, decode_position_frame = 0; | |
| 502 | |
| 503 (void)arg; | |
| 504 | |
| 505 while(file_info_.is_playing) { | |
| 506 if(!file_info_.eof) { | |
| 507 while(sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE) { | |
| 508 unsigned s; | |
| 509 | |
| 510 s = sample_buffer_last_ - sample_buffer_first_; | |
| 511 if(decoder_func_table_ -> is_eof(decoder_)) { | |
| 512 file_info_.eof = true; | |
| 513 break; | |
| 514 } | |
| 515 else if (!decoder_func_table_ -> process_single(decoder_)) { | |
| 516 /*@@@ this should probably be a dialog */ | |
| 517 fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n"); | |
| 518 file_info_.eof = true; | |
| 519 break; | |
| 520 } | |
| 521 blocksize = sample_buffer_last_ - sample_buffer_first_ - s; | |
| 522 decode_position_frame_last = decode_position_frame; | |
| 523 if(!decoder_func_table_->seekable || !FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame)) | |
| 524 decode_position_frame = 0; | |
| 525 } | |
| 526 if(sample_buffer_last_ - sample_buffer_first_ > 0) { | |
| 527 const unsigned n = min(sample_buffer_last_ - sample_buffer_first_, SAMPLES_PER_WRITE); | |
| 528 int bytes = n * file_info_.channels * file_info_.sample_format_bytes_per_sample; | |
| 529 FLAC__byte *sample_buffer_start = sample_buffer_ + sample_buffer_first_ * file_info_.channels * file_info_.sample_format_bytes_per_sample; | |
| 530 unsigned written_time, bh_index_w; | |
| 531 FLAC__uint64 decode_position; | |
| 532 | |
| 533 sample_buffer_first_ += n; | |
| 534 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1) | |
| 535 xmms_usleep(10000); | |
| 536 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1) | |
| 537 produce_audio(flac_ip.output->written_time(), file_info_.sample_format, | |
| 538 file_info_.channels, bytes, sample_buffer_start, NULL); | |
| 539 | |
| 540 /* compute current bitrate */ | |
| 541 | |
| 542 written_time = flac_ip.output->written_time(); | |
| 543 bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE; | |
| 544 if(bh_index_w != bh_index_last_w) { | |
| 545 bh_index_last_w = bh_index_w; | |
| 546 decode_position = decode_position_frame - (double)(sample_buffer_last_ - sample_buffer_first_) * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize; | |
| 547 bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] = | |
| 548 decode_position > decode_position_last && written_time > written_time_last ? | |
| 549 8000 * (decode_position - decode_position_last) / (written_time - written_time_last) : | |
| 550 file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample; | |
| 551 decode_position_last = decode_position; | |
| 552 written_time_last = written_time; | |
| 553 } | |
| 554 } | |
| 555 else { | |
| 556 file_info_.eof = true; | |
| 557 xmms_usleep(10000); | |
| 558 } | |
| 559 } | |
| 560 else | |
| 561 xmms_usleep(10000); | |
| 562 if(decoder_func_table_->seekable && file_info_.seek_to_in_sec != -1) { | |
| 563 const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec; | |
| 564 unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples); | |
| 565 if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) { | |
| 566 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000); | |
| 567 bh_index_last_w = bh_index_last_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE; | |
| 568 if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame)) | |
| 569 decode_position_frame = 0; | |
| 570 file_info_.seek_to_in_sec = -1; | |
| 571 file_info_.eof = false; | |
| 572 sample_buffer_first_ = sample_buffer_last_ = 0; | |
| 573 } | |
| 574 } | |
| 575 else { | |
| 576 /* display the right bitrate from history */ | |
| 577 unsigned bh_index_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE; | |
| 578 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) { | |
| 579 bh_index_last_o = bh_index_o; | |
| 580 flac_ip.set_info(file_info_.title, file_info_.length_in_msec, bitrate_history_[bh_index_o], file_info_.sample_rate, file_info_.channels); | |
| 581 } | |
| 582 } | |
| 583 } | |
| 584 | |
| 585 decoder_func_table_ -> safe_decoder_finish(decoder_); | |
| 586 | |
| 587 /* are these two calls necessary? */ | |
| 588 flac_ip.output->buffer_free(); | |
| 589 flac_ip.output->buffer_free(); | |
| 590 | |
| 591 g_free(file_info_.title); | |
| 592 | |
| 593 g_thread_exit(NULL); | |
| 594 return 0; /* to silence the compiler warning about not returning a value */ | |
| 595 } | |
| 596 | |
| 597 /*********** File decoder functions */ | |
| 598 | |
| 599 static FLAC__bool file_decoder_init (void *decoder) | |
| 600 { | |
| 601 return FLAC__file_decoder_init( (FLAC__FileDecoder*) decoder) == FLAC__FILE_DECODER_OK; | |
| 602 } | |
| 603 | |
| 604 static void file_decoder_safe_decoder_finish_(void *decoder) | |
| 605 { | |
| 606 if(decoder && FLAC__file_decoder_get_state((FLAC__FileDecoder *) decoder) != FLAC__FILE_DECODER_UNINITIALIZED) | |
| 607 FLAC__file_decoder_finish((FLAC__FileDecoder *) decoder); | |
| 608 } | |
| 609 | |
| 610 static void file_decoder_safe_decoder_delete_(void *decoder) | |
| 611 { | |
| 612 if(decoder) { | |
| 613 file_decoder_safe_decoder_finish_(decoder); | |
| 614 FLAC__file_decoder_delete( (FLAC__FileDecoder *) decoder); | |
| 615 } | |
| 616 } | |
| 617 | |
| 618 static FLAC__bool file_decoder_is_eof(void *decoder) | |
| 619 { | |
| 620 return FLAC__file_decoder_get_state((FLAC__FileDecoder *) decoder) == FLAC__FILE_DECODER_END_OF_FILE; | |
| 621 } | |
| 622 | |
| 623 static const decoder_funcs_t FILE_DECODER_FUNCTIONS = { | |
| 624 true, | |
| 625 (void* (*) (void)) FLAC__file_decoder_new, | |
| 626 (FLAC__bool (*) (void *, FLAC__bool)) FLAC__file_decoder_set_md5_checking, | |
| 627 (FLAC__bool (*) (void *, const char*)) FLAC__file_decoder_set_filename, | |
| 628 (FLAC__bool (*) (void *)) FLAC__file_decoder_set_metadata_ignore_all, | |
| 629 (FLAC__bool (*) (void *, FLAC__MetadataType)) FLAC__file_decoder_set_metadata_respond, | |
| 630 (FLAC__bool (*) (void *, WriteCallback)) FLAC__file_decoder_set_write_callback, | |
| 631 (FLAC__bool (*) (void *, MetadataCallback)) FLAC__file_decoder_set_metadata_callback, | |
| 632 (FLAC__bool (*) (void *, ErrorCallback)) FLAC__file_decoder_set_error_callback, | |
| 633 (FLAC__bool (*) (void *, void *)) FLAC__file_decoder_set_client_data, | |
| 634 (FLAC__bool (*) (void *)) file_decoder_init, | |
| 635 (void (*) (void *)) file_decoder_safe_decoder_finish_, | |
| 636 (void (*) (void *)) file_decoder_safe_decoder_delete_, | |
| 637 (FLAC__bool (*) (void *)) FLAC__file_decoder_process_until_end_of_metadata, | |
| 638 (FLAC__bool (*) (void *)) FLAC__file_decoder_process_single, | |
| 639 file_decoder_is_eof | |
| 640 }; | |
| 641 | |
| 642 /*********** HTTP decoder functions */ | |
| 643 | |
| 644 static gchar *url_; | |
| 645 | |
| 646 static FLAC__bool http_decoder_set_md5_checking (void *decoder, FLAC__bool value) | |
| 647 { | |
| 648 (void) value; | |
| 649 // operation unsupported | |
| 650 return FLAC__stream_decoder_get_state ((const FLAC__StreamDecoder *) decoder) == | |
| 651 FLAC__STREAM_DECODER_UNINITIALIZED; | |
| 652 } | |
| 653 | |
| 654 static FLAC__bool http_decoder_set_url (void *decoder, const char* url) | |
| 655 { | |
| 656 (void) decoder; | |
| 657 url_ = g_strdup (url); | |
| 658 return true; | |
| 659 } | |
| 660 | |
| 661 static FLAC__StreamDecoderReadStatus http_decoder_read_callback (const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data) | |
| 662 { | |
| 663 (void) decoder; | |
| 664 (void) client_data; | |
| 665 *bytes = flac_http_read (buffer, *bytes); | |
| 666 return *bytes ? FLAC__STREAM_DECODER_READ_STATUS_CONTINUE : FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM; | |
| 667 } | |
| 668 | |
| 669 static FLAC__bool http_decoder_init (void *decoder) | |
| 670 { | |
| 671 flac_http_open (url_, 0); | |
| 672 g_free (url_); | |
| 673 FLAC__stream_decoder_set_read_callback (decoder, http_decoder_read_callback); | |
| 674 return FLAC__stream_decoder_init( (FLAC__StreamDecoder*) decoder) == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA; | |
| 675 } | |
| 676 | |
| 677 static void http_decoder_safe_decoder_finish_(void *decoder) | |
| 678 { | |
| 679 if(decoder && FLAC__stream_decoder_get_state((FLAC__StreamDecoder *) decoder) != FLAC__STREAM_DECODER_UNINITIALIZED) { | |
| 680 FLAC__stream_decoder_finish((FLAC__StreamDecoder *) decoder); | |
| 681 flac_http_close(); | |
| 682 } | |
| 683 } | |
| 684 | |
| 685 static void http_decoder_safe_decoder_delete_(void *decoder) | |
| 686 { | |
| 687 if(decoder) { | |
| 688 http_decoder_safe_decoder_finish_(decoder); | |
| 689 FLAC__stream_decoder_delete( (FLAC__StreamDecoder *) decoder); | |
| 690 } | |
| 691 } | |
| 692 | |
| 693 static FLAC__bool http_decoder_is_eof(void *decoder) | |
| 694 { | |
| 695 return FLAC__stream_decoder_get_state((FLAC__StreamDecoder *) decoder) == FLAC__STREAM_DECODER_END_OF_STREAM; | |
| 696 } | |
| 697 | |
| 698 static const decoder_funcs_t HTTP_DECODER_FUNCTIONS = { | |
| 699 false, | |
| 700 (void* (*) (void)) FLAC__stream_decoder_new, | |
| 701 http_decoder_set_md5_checking, | |
| 702 (FLAC__bool (*) (void *, const char*)) http_decoder_set_url, | |
| 703 (FLAC__bool (*) (void *)) FLAC__stream_decoder_set_metadata_ignore_all, | |
| 704 (FLAC__bool (*) (void *, FLAC__MetadataType)) FLAC__stream_decoder_set_metadata_respond, | |
| 705 (FLAC__bool (*) (void *, WriteCallback)) FLAC__stream_decoder_set_write_callback, | |
| 706 (FLAC__bool (*) (void *, MetadataCallback)) FLAC__stream_decoder_set_metadata_callback, | |
| 707 (FLAC__bool (*) (void *, ErrorCallback)) FLAC__stream_decoder_set_error_callback, | |
| 708 (FLAC__bool (*) (void *, void *)) FLAC__stream_decoder_set_client_data, | |
| 709 (FLAC__bool (*) (void *)) http_decoder_init, | |
| 710 (void (*) (void *)) http_decoder_safe_decoder_finish_, | |
| 711 (void (*) (void *)) http_decoder_safe_decoder_delete_, | |
| 712 (FLAC__bool (*) (void *)) FLAC__stream_decoder_process_until_end_of_metadata, | |
| 713 (FLAC__bool (*) (void *)) FLAC__stream_decoder_process_single, | |
| 714 http_decoder_is_eof | |
| 715 }; | |
| 716 | |
| 717 static decoder_funcs_t const *decoder_func_table_; | |
| 718 | |
| 719 static void init_decoder_func_tables() | |
| 720 { | |
| 721 DECODER_FUNCS [DECODER_FILE] = & FILE_DECODER_FUNCTIONS; | |
| 722 DECODER_FUNCS [DECODER_HTTP] = & HTTP_DECODER_FUNCTIONS; | |
| 723 } | |
| 724 | |
| 725 static decoder_t source_to_decoder_type (const char *source) | |
| 726 { | |
| 727 return strncasecmp(source, "http://", 7) ? DECODER_FILE : DECODER_HTTP; | |
| 728 } | |
| 729 | |
| 730 static void change_decoder_if_needed (decoder_t new_decoder_type, void **decoderp, decoder_funcs_t const ** fntabp) | |
| 731 { | |
| 732 const decoder_funcs_t *new_fn_table = DECODER_FUNCS [new_decoder_type]; | |
| 733 if (*fntabp != new_fn_table) { | |
| 734 (*fntabp)->safe_decoder_delete(*decoderp); | |
| 735 *fntabp = new_fn_table; | |
| 736 *decoderp = new_fn_table -> new_decoder(); | |
| 737 } | |
| 738 } | |
| 739 | |
| 740 FLAC__bool safe_decoder_init_(const char *filename, void **decoderp, decoder_funcs_t const ** fntabp) | |
| 741 { | |
| 742 if(decoderp == 0 || *decoderp == 0) | |
| 743 return false; | |
| 744 | |
| 745 (*fntabp)->safe_decoder_finish(*decoderp); | |
| 746 | |
| 747 change_decoder_if_needed(source_to_decoder_type(filename), decoderp, fntabp); | |
| 748 | |
| 749 { | |
| 750 decoder_funcs_t const *fntab = *fntabp; | |
| 751 void *decoder = *decoderp; | |
| 752 | |
| 753 decoder = *decoderp; | |
| 754 fntab = *fntabp; | |
| 755 | |
| 756 fntab -> set_md5_checking(decoder, false); | |
| 757 fntab -> set_source(decoder, filename); | |
| 758 fntab -> set_metadata_ignore_all(decoder); | |
| 759 fntab -> set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO); | |
| 760 fntab -> set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT); | |
| 761 fntab -> set_write_callback(decoder, write_callback_); | |
| 762 fntab -> set_metadata_callback(decoder, metadata_callback_); | |
| 763 fntab -> set_error_callback(decoder, error_callback_); | |
| 764 fntab -> set_client_data(decoder, &file_info_); | |
| 765 if(!fntab -> decoder_init(decoder)) | |
| 766 return false; | |
| 767 | |
| 768 if(!fntab -> process_until_end_of_metadata(decoder)) | |
| 769 return false; | |
| 770 } | |
| 771 | |
| 772 return true; | |
| 773 } | |
| 774 | |
| 775 FLAC__StreamDecoderWriteStatus write_callback_(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) | |
| 776 { | |
| 777 file_info_struct *file_info = (file_info_struct *)client_data; | |
| 778 const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize; | |
| 779 const unsigned bits_per_sample = file_info->bits_per_sample; | |
| 780 FLAC__byte *sample_buffer_start; | |
| 781 | |
| 782 (void)decoder; | |
| 783 | |
| 784 if(file_info->abort_flag) | |
| 785 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; | |
| 786 | |
| 787 if((sample_buffer_last_ + wide_samples) > (SAMPLE_BUFFER_SIZE / (channels * file_info->sample_format_bytes_per_sample))) { | |
| 788 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); | |
| 789 sample_buffer_last_ -= sample_buffer_first_; | |
| 790 sample_buffer_first_ = 0; | |
| 791 } | |
| 792 sample_buffer_start = sample_buffer_ + sample_buffer_last_ * channels * file_info->sample_format_bytes_per_sample; | |
| 793 if(file_info->has_replaygain && flac_cfg.output.replaygain.enable) { | |
| 794 FLAC__replaygain_synthesis__apply_gain( | |
| 795 sample_buffer_start, | |
| 796 !is_big_endian_host_, | |
| 797 file_info->sample_format_bytes_per_sample == 1, /* unsigned_data_out */ | |
| 798 buffer, | |
| 799 wide_samples, | |
| 800 channels, | |
| 801 bits_per_sample, | |
| 802 file_info->sample_format_bytes_per_sample * 8, | |
| 803 file_info->replay_scale, | |
| 804 flac_cfg.output.replaygain.hard_limit, | |
| 805 flac_cfg.output.resolution.replaygain.dither, | |
| 806 &file_info->dither_context | |
| 807 ); | |
| 808 } | |
| 809 else if(is_big_endian_host_) { | |
| 810 FLAC__plugin_common__pack_pcm_signed_big_endian( | |
| 811 sample_buffer_start, | |
| 812 buffer, | |
| 813 wide_samples, | |
| 814 channels, | |
| 815 bits_per_sample, | |
| 816 file_info->sample_format_bytes_per_sample * 8 | |
| 817 ); | |
| 818 } | |
| 819 else { | |
| 820 FLAC__plugin_common__pack_pcm_signed_little_endian( | |
| 821 sample_buffer_start, | |
| 822 buffer, | |
| 823 wide_samples, | |
| 824 channels, | |
| 825 bits_per_sample, | |
| 826 file_info->sample_format_bytes_per_sample * 8 | |
| 827 ); | |
| 828 } | |
| 829 | |
| 830 sample_buffer_last_ += wide_samples; | |
| 831 | |
| 832 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; | |
| 833 } | |
| 834 | |
| 835 void metadata_callback_(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data) | |
| 836 { | |
| 837 file_info_struct *file_info = (file_info_struct *)client_data; | |
| 838 (void)decoder; | |
| 839 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) { | |
| 840 FLAC__ASSERT(metadata->data.stream_info.total_samples < FLAC__U64L(0x100000000)); /* this plugin can only handle < 4 gigasamples */ | |
| 841 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff); | |
| 842 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample; | |
| 843 file_info->channels = metadata->data.stream_info.channels; | |
| 844 file_info->sample_rate = metadata->data.stream_info.sample_rate; | |
| 845 file_info->length_in_msec = (unsigned)((double)file_info->total_samples / (double)file_info->sample_rate * 1000.0 + 0.5); | |
| 846 } | |
| 847 else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) { | |
| 848 double gain, peak; | |
| 849 if(grabbag__replaygain_load_from_vorbiscomment(metadata, flac_cfg.output.replaygain.album_mode, &gain, &peak)) { | |
| 850 file_info->has_replaygain = true; | |
| 851 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); | |
| 852 } | |
| 853 } | |
| 854 } | |
| 855 | |
| 856 void error_callback_(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) | |
| 857 { | |
| 858 file_info_struct *file_info = (file_info_struct *)client_data; | |
| 859 (void)decoder; | |
| 860 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC) | |
| 861 file_info->abort_flag = true; | |
| 862 } |
