plugin.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:29k
源码类别:

Windows CE

开发平台:

C/C++

  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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  */
  18. #include <pthread.h>
  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. #include <xmms/plugin.h>
  27. #include <xmms/util.h>
  28. #include <xmms/configfile.h>
  29. #include <xmms/titlestring.h>
  30. #ifdef HAVE_CONFIG_H
  31. #include <config.h>
  32. #endif
  33. #ifdef HAVE_LANGINFO_CODESET
  34. #include <langinfo.h>
  35. #endif
  36. #include "FLAC/all.h"
  37. #include "plugin_common/all.h"
  38. #include "share/grabbag.h"
  39. #include "share/replaygain_synthesis.h"
  40. #include "configure.h"
  41. #include "charset.h"
  42. #include "http.h"
  43. #include "tag.h"
  44. #ifdef min
  45. #undef min
  46. #endif
  47. #define min(x,y) ((x)<(y)?(x):(y))
  48. /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
  49. #ifdef _MSC_VER
  50. #define FLAC__U64L(x) x
  51. #else
  52. #define FLAC__U64L(x) x##LLU
  53. #endif
  54. extern void FLAC_XMMS__file_info_box(char *filename);
  55. typedef struct {
  56. FLAC__bool abort_flag;
  57. FLAC__bool is_playing;
  58. FLAC__bool eof;
  59. FLAC__bool play_thread_open; /* if true, is_playing must also be true */
  60. unsigned total_samples;
  61. unsigned bits_per_sample;
  62. unsigned channels;
  63. unsigned sample_rate;
  64. unsigned length_in_msec;
  65. gchar *title;
  66. AFormat sample_format;
  67. unsigned sample_format_bytes_per_sample;
  68. int seek_to_in_sec;
  69. FLAC__bool has_replaygain;
  70. double replay_scale;
  71. DitherContext dither_context;
  72. } file_info_struct;
  73. typedef FLAC__StreamDecoderWriteStatus (*WriteCallback) (const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
  74. typedef void (*MetadataCallback) (const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
  75. typedef void (*ErrorCallback) (const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
  76. typedef struct {
  77. FLAC__bool seekable;
  78. void* (*new_decoder) (void);
  79. FLAC__bool (*set_md5_checking) (void *decoder, FLAC__bool value);
  80. FLAC__bool (*set_source) (void *decoder, const char* source);
  81. FLAC__bool (*set_metadata_ignore_all) (void *decoder);
  82. FLAC__bool (*set_metadata_respond) (void *decoder, FLAC__MetadataType type);
  83. FLAC__bool (*set_write_callback) (void *decoder, WriteCallback value);
  84. FLAC__bool (*set_metadata_callback) (void *decoder, MetadataCallback value);
  85. FLAC__bool (*set_error_callback) (void *decoder, ErrorCallback value);
  86. FLAC__bool (*set_client_data) (void *decoder, void *value);
  87. FLAC__bool (*decoder_init) (void *decoder);
  88. void (*safe_decoder_finish) (void *decoder);
  89. void (*safe_decoder_delete) (void *decoder);
  90. FLAC__bool (*process_until_end_of_metadata) (void *decoder);
  91. FLAC__bool (*process_single) (void *decoder);
  92. FLAC__bool (*is_eof) (void *decoder);
  93. } decoder_funcs_t;
  94. #define NUM_DECODER_TYPES 2
  95. typedef enum {
  96. DECODER_FILE,
  97. DECODER_HTTP
  98. } decoder_t;
  99. static void FLAC_XMMS__init();
  100. static int  FLAC_XMMS__is_our_file(char *filename);
  101. static void FLAC_XMMS__play_file(char *filename);
  102. static void FLAC_XMMS__stop();
  103. static void FLAC_XMMS__pause(short p);
  104. static void FLAC_XMMS__seek(int time);
  105. static int  FLAC_XMMS__get_time();
  106. static void FLAC_XMMS__cleanup();
  107. static void FLAC_XMMS__get_song_info(char *filename, char **title, int *length);
  108. static void *play_loop_(void *arg);
  109. static FLAC__bool safe_decoder_init_(const char *filename, void **decoderp, decoder_funcs_t const ** fnsp);
  110. static void file_decoder_safe_decoder_finish_(void *decoder);
  111. static void file_decoder_safe_decoder_delete_(void *decoder);
  112. static FLAC__StreamDecoderWriteStatus write_callback_(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
  113. static void metadata_callback_(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
  114. static void error_callback_(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
  115. static void init_decoder_func_tables();
  116. static decoder_t source_to_decoder_type (const char *source);
  117. InputPlugin flac_ip =
  118. {
  119. NULL,
  120. NULL,
  121. "FLAC Player v" VERSION,
  122. FLAC_XMMS__init,
  123. FLAC_XMMS__aboutbox,
  124. FLAC_XMMS__configure,
  125. FLAC_XMMS__is_our_file,
  126. NULL,
  127. FLAC_XMMS__play_file,
  128. FLAC_XMMS__stop,
  129. FLAC_XMMS__pause,
  130. FLAC_XMMS__seek,
  131. NULL,
  132. FLAC_XMMS__get_time,
  133. NULL,
  134. NULL,
  135. FLAC_XMMS__cleanup,
  136. NULL,
  137. NULL,
  138. NULL,
  139. NULL,
  140. FLAC_XMMS__get_song_info,
  141. FLAC_XMMS__file_info_box,
  142. NULL
  143. };
  144. #define SAMPLES_PER_WRITE 512
  145. #define SAMPLE_BUFFER_SIZE ((FLAC__MAX_BLOCK_SIZE + SAMPLES_PER_WRITE) * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8))
  146. static FLAC__byte sample_buffer_[SAMPLE_BUFFER_SIZE];
  147. static unsigned sample_buffer_first_, sample_buffer_last_;
  148. static void *decoder_ = 0;
  149. static file_info_struct file_info_;
  150. static pthread_t decode_thread_;
  151. static FLAC__bool audio_error_ = false;
  152. static FLAC__bool is_big_endian_host_;
  153. #define BITRATE_HIST_SEGMENT_MSEC 500
  154. /* 500ms * 50 = 25s should be enough */
  155. #define BITRATE_HIST_SIZE 50
  156. static unsigned bitrate_history_[BITRATE_HIST_SIZE];
  157. /* A table of sets of decoder functions, indexed by decoder_t */
  158. static const decoder_funcs_t* DECODER_FUNCS[NUM_DECODER_TYPES];
  159. static decoder_funcs_t const * decoder_func_table_;
  160. InputPlugin *get_iplugin_info()
  161. {
  162. flac_ip.description = g_strdup_printf("Reference FLAC Player v%s", FLAC__VERSION_STRING);
  163. return &flac_ip;
  164. }
  165. void set_track_info(const char* title, int length_in_msec)
  166. {
  167. if (file_info_.is_playing) {
  168. 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);
  169. }
  170. }
  171. static gchar* homedir()
  172. {
  173. gchar *result;
  174. char *env_home = getenv("HOME");
  175. if (env_home) {
  176. result = g_strdup (env_home);
  177. } else {
  178. uid_t uid = getuid();
  179. struct passwd *pwent;
  180. do {
  181. pwent = getpwent();
  182. } while (pwent && pwent->pw_uid != uid);
  183. result = pwent ? g_strdup (pwent->pw_dir) : NULL;
  184. endpwent();
  185. }
  186. return result;
  187. }
  188. void FLAC_XMMS__init()
  189. {
  190. ConfigFile *cfg;
  191. FLAC__uint32 test = 1;
  192. is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
  193. flac_cfg.title.tag_override = FALSE;
  194. g_free(flac_cfg.title.tag_format);
  195. flac_cfg.title.convert_char_set = FALSE;
  196. cfg = xmms_cfg_open_default_file();
  197. /* title */
  198. xmms_cfg_read_boolean(cfg, "flac", "title.tag_override", &flac_cfg.title.tag_override);
  199. if(!xmms_cfg_read_string(cfg, "flac", "title.tag_format", &flac_cfg.title.tag_format))
  200. flac_cfg.title.tag_format = g_strdup("%p - %t");
  201. xmms_cfg_read_boolean(cfg, "flac", "title.convert_char_set", &flac_cfg.title.convert_char_set);
  202. if(!xmms_cfg_read_string(cfg, "flac", "title.user_char_set", &flac_cfg.title.user_char_set))
  203. flac_cfg.title.user_char_set = FLAC_plugin__charset_get_current();
  204. /* replaygain */
  205. xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.enable", &flac_cfg.output.replaygain.enable);
  206. xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.album_mode", &flac_cfg.output.replaygain.album_mode);
  207. if(!xmms_cfg_read_int(cfg, "flac", "output.replaygain.preamp", &flac_cfg.output.replaygain.preamp))
  208. flac_cfg.output.replaygain.preamp = 0;
  209. xmms_cfg_read_boolean(cfg, "flac", "output.replaygain.hard_limit", &flac_cfg.output.replaygain.hard_limit);
  210. xmms_cfg_read_boolean(cfg, "flac", "output.resolution.normal.dither_24_to_16", &flac_cfg.output.resolution.normal.dither_24_to_16);
  211. xmms_cfg_read_boolean(cfg, "flac", "output.resolution.replaygain.dither", &flac_cfg.output.resolution.replaygain.dither);
  212. if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.noise_shaping", &flac_cfg.output.resolution.replaygain.noise_shaping))
  213. flac_cfg.output.resolution.replaygain.noise_shaping = 1;
  214. if(!xmms_cfg_read_int(cfg, "flac", "output.resolution.replaygain.bps_out", &flac_cfg.output.resolution.replaygain.bps_out))
  215. flac_cfg.output.resolution.replaygain.bps_out = 16;
  216. /* stream */
  217. xmms_cfg_read_int(cfg, "flac", "stream.http_buffer_size", &flac_cfg.stream.http_buffer_size);
  218. xmms_cfg_read_int(cfg, "flac", "stream.http_prebuffer", &flac_cfg.stream.http_prebuffer);
  219. xmms_cfg_read_boolean(cfg, "flac", "stream.use_proxy", &flac_cfg.stream.use_proxy);
  220. xmms_cfg_read_string(cfg, "flac", "stream.proxy_host", &flac_cfg.stream.proxy_host);
  221. xmms_cfg_read_int(cfg, "flac", "stream.proxy_port", &flac_cfg.stream.proxy_port);
  222. xmms_cfg_read_boolean(cfg, "flac", "stream.proxy_use_auth", &flac_cfg.stream.proxy_use_auth);
  223. xmms_cfg_read_string(cfg, "flac", "stream.proxy_user", &flac_cfg.stream.proxy_user);
  224. xmms_cfg_read_string(cfg, "flac", "stream.proxy_pass", &flac_cfg.stream.proxy_pass);
  225. xmms_cfg_read_boolean(cfg, "flac", "stream.save_http_stream", &flac_cfg.stream.save_http_stream);
  226. if (!xmms_cfg_read_string(cfg, "flac", "stream.save_http_path", &flac_cfg.stream.save_http_path) ||
  227.  ! *flac_cfg.stream.save_http_path) {
  228. if (flac_cfg.stream.save_http_path)
  229. g_free (flac_cfg.stream.save_http_path);
  230. flac_cfg.stream.save_http_path = homedir();
  231. }
  232. xmms_cfg_read_boolean(cfg, "flac", "stream.cast_title_streaming", &flac_cfg.stream.cast_title_streaming);
  233. xmms_cfg_read_boolean(cfg, "flac", "stream.use_udp_channel", &flac_cfg.stream.use_udp_channel);
  234. init_decoder_func_tables();
  235.   decoder_func_table_ = DECODER_FUNCS [DECODER_FILE];
  236. decoder_ = decoder_func_table_ -> new_decoder();
  237. xmms_cfg_free(cfg);
  238. }
  239. int FLAC_XMMS__is_our_file(char *filename)
  240. {
  241. char *ext;
  242. ext = strrchr(filename, '.');
  243. if(ext)
  244. if(!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
  245. return 1;
  246. return 0;
  247. }
  248. void FLAC_XMMS__play_file(char *filename)
  249. {
  250. FILE *f;
  251. sample_buffer_first_ = sample_buffer_last_ = 0;
  252. audio_error_ = false;
  253. file_info_.abort_flag = false;
  254. file_info_.is_playing = false;
  255. file_info_.eof = false;
  256. file_info_.play_thread_open = false;
  257. file_info_.has_replaygain = false;
  258. if (source_to_decoder_type (filename) == DECODER_FILE) {
  259. if(0 == (f = fopen(filename, "r")))
  260. return;
  261. fclose(f);
  262. }
  263. if(decoder_ == 0)
  264. return;
  265. if(!safe_decoder_init_(filename, &decoder_, &decoder_func_table_))
  266. return;
  267. if(file_info_.has_replaygain && flac_cfg.output.replaygain.enable) {
  268. if(flac_cfg.output.resolution.replaygain.bps_out == 8) {
  269. file_info_.sample_format = FMT_U8;
  270. file_info_.sample_format_bytes_per_sample = 1;
  271. }
  272. else if(flac_cfg.output.resolution.replaygain.bps_out == 16) {
  273. file_info_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
  274. file_info_.sample_format_bytes_per_sample = 2;
  275. }
  276. else {
  277. /*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16-bit samplesn", "ERROR: plugin can only handle 8/16-bit samples", 0); */
  278. fprintf(stderr, "libxmms-flac: can't handle %d bit outputn", flac_cfg.output.resolution.replaygain.bps_out);
  279. decoder_func_table_ -> safe_decoder_finish(decoder_);
  280. return;
  281. }
  282. }
  283. else {
  284. if(file_info_.bits_per_sample == 8) {
  285. file_info_.sample_format = FMT_U8;
  286. file_info_.sample_format_bytes_per_sample = 1;
  287. }
  288. else if(file_info_.bits_per_sample == 16 || (file_info_.bits_per_sample == 24 && flac_cfg.output.resolution.normal.dither_24_to_16)) {
  289. file_info_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE;
  290. file_info_.sample_format_bytes_per_sample = 2;
  291. }
  292. else {
  293. /*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16-bit samplesn", "ERROR: plugin can only handle 8/16-bit samples", 0); */
  294. fprintf(stderr, "libxmms-flac: can't handle %d bit outputn", file_info_.bits_per_sample);
  295. decoder_func_table_ -> safe_decoder_finish(decoder_);
  296. return;
  297. }
  298. }
  299. 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);
  300. file_info_.is_playing = true;
  301. if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
  302. audio_error_ = true;
  303. decoder_func_table_ -> safe_decoder_finish(decoder_);
  304. return;
  305. }
  306. file_info_.title = flac_format_song_title(filename);
  307. 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);
  308. file_info_.seek_to_in_sec = -1;
  309. file_info_.play_thread_open = true;
  310. pthread_create(&decode_thread_, NULL, play_loop_, NULL);
  311. }
  312. void FLAC_XMMS__stop()
  313. {
  314. if(file_info_.is_playing) {
  315. file_info_.is_playing = false;
  316. if(file_info_.play_thread_open) {
  317. file_info_.play_thread_open = false;
  318. pthread_join(decode_thread_, NULL);
  319. }
  320. flac_ip.output->close_audio();
  321. decoder_func_table_ -> safe_decoder_finish (decoder_);
  322. }
  323. }
  324. void FLAC_XMMS__pause(short p)
  325. {
  326. flac_ip.output->pause(p);
  327. }
  328. void FLAC_XMMS__seek(int time)
  329. {
  330. if (decoder_func_table_->seekable) {
  331. file_info_.seek_to_in_sec = time;
  332. file_info_.eof = false;
  333. while(file_info_.seek_to_in_sec != -1)
  334. xmms_usleep(10000);
  335. }
  336. }
  337. int FLAC_XMMS__get_time()
  338. {
  339. if(audio_error_)
  340. return -2;
  341. if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
  342. return -1;
  343. else
  344. return flac_ip.output->output_time();
  345. }
  346. void FLAC_XMMS__cleanup()
  347. {
  348. decoder_func_table_ -> safe_decoder_delete(decoder_);
  349. decoder_ = 0;
  350. }
  351. void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
  352. {
  353. FLAC__StreamMetadata streaminfo;
  354. if(0 == filename)
  355. filename = "";
  356. if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
  357. /* @@@ how to report the error? */
  358. if(title) {
  359. if (source_to_decoder_type (filename) == DECODER_FILE) {
  360. static const char *errtitle = "Invalid FLAC File: ";
  361. *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
  362. sprintf(*title, "%s"%s"", errtitle, filename);
  363. } else {
  364. *title = NULL;
  365. }
  366. }
  367. if(length_in_msec)
  368. *length_in_msec = -1;
  369. return;
  370. }
  371. if(title) {
  372. *title = flac_format_song_title(filename);
  373. }
  374. if(length_in_msec)
  375. *length_in_msec = (unsigned)((double)streaminfo.data.stream_info.total_samples / (double)streaminfo.data.stream_info.sample_rate * 1000.0 + 0.5);
  376. }
  377. /***********************************************************************
  378.  * local routines
  379.  **********************************************************************/
  380. void *play_loop_(void *arg)
  381. {
  382. unsigned written_time_last = 0, bh_index_last_w = 0, bh_index_last_o = BITRATE_HIST_SIZE, blocksize = 1;
  383. FLAC__uint64 decode_position_last = 0, decode_position_frame_last = 0, decode_position_frame = 0;
  384. (void)arg;
  385. while(file_info_.is_playing) {
  386. if(!file_info_.eof) {
  387. while(sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE) {
  388. unsigned s;
  389. s = sample_buffer_last_ - sample_buffer_first_;
  390. if(decoder_func_table_ -> is_eof(decoder_)) {
  391. file_info_.eof = true;
  392. break;
  393. }
  394. else if (!decoder_func_table_ -> process_single(decoder_)) {
  395. /*@@@ this should probably be a dialog */
  396. fprintf(stderr, "libxmms-flac: READ ERROR processing framen");
  397. file_info_.eof = true;
  398. break;
  399. }
  400. blocksize = sample_buffer_last_ - sample_buffer_first_ - s;
  401. decode_position_frame_last = decode_position_frame;
  402. if(!decoder_func_table_->seekable || !FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
  403. decode_position_frame = 0;
  404. }
  405. if(sample_buffer_last_ - sample_buffer_first_ > 0) {
  406. const unsigned n = min(sample_buffer_last_ - sample_buffer_first_, SAMPLES_PER_WRITE);
  407. int bytes = n * file_info_.channels * file_info_.sample_format_bytes_per_sample;
  408. FLAC__byte *sample_buffer_start = sample_buffer_ + sample_buffer_first_ * file_info_.channels * file_info_.sample_format_bytes_per_sample;
  409. unsigned written_time, bh_index_w;
  410. FLAC__uint64 decode_position;
  411. sample_buffer_first_ += n;
  412. flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, file_info_.channels, bytes, sample_buffer_start);
  413. while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
  414. xmms_usleep(10000);
  415. if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
  416. flac_ip.output->write_audio(sample_buffer_start, bytes);
  417. /* compute current bitrate */
  418. written_time = flac_ip.output->written_time();
  419. bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
  420. if(bh_index_w != bh_index_last_w) {
  421. bh_index_last_w = bh_index_w;
  422. decode_position = decode_position_frame - (double)(sample_buffer_last_ - sample_buffer_first_) * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize;
  423. bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] =
  424. decode_position > decode_position_last && written_time > written_time_last ?
  425. 8000 * (decode_position - decode_position_last) / (written_time - written_time_last) :
  426. file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample;
  427. decode_position_last = decode_position;
  428. written_time_last = written_time;
  429. }
  430. }
  431. else {
  432. file_info_.eof = true;
  433. xmms_usleep(10000);
  434. }
  435. }
  436. else
  437. xmms_usleep(10000);
  438. if(decoder_func_table_->seekable && file_info_.seek_to_in_sec != -1) {
  439. const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
  440. unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
  441. if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
  442. flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
  443. bh_index_last_w = bh_index_last_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
  444. if(!FLAC__file_decoder_get_decode_position(decoder_, &decode_position_frame))
  445. decode_position_frame = 0;
  446. file_info_.seek_to_in_sec = -1;
  447. file_info_.eof = false;
  448. sample_buffer_first_ = sample_buffer_last_ = 0;
  449. }
  450. }
  451. else {
  452. /* display the right bitrate from history */
  453. unsigned bh_index_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE;
  454. 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) {
  455. bh_index_last_o = bh_index_o;
  456. flac_ip.set_info(file_info_.title, file_info_.length_in_msec, bitrate_history_[bh_index_o], file_info_.sample_rate, file_info_.channels);
  457. }
  458. }
  459. }
  460. decoder_func_table_ -> safe_decoder_finish(decoder_);
  461. /* are these two calls necessary? */
  462. flac_ip.output->buffer_free();
  463. flac_ip.output->buffer_free();
  464. g_free(file_info_.title);
  465. pthread_exit(NULL);
  466. return 0; /* to silence the compiler warning about not returning a value */
  467. }
  468. /*********** File decoder functions */
  469. static FLAC__bool file_decoder_init (void *decoder)
  470. {
  471. return FLAC__file_decoder_init( (FLAC__FileDecoder*) decoder) == FLAC__FILE_DECODER_OK;
  472. }
  473. static void file_decoder_safe_decoder_finish_(void *decoder)
  474. {
  475. if(decoder && FLAC__file_decoder_get_state((FLAC__FileDecoder *) decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
  476. FLAC__file_decoder_finish((FLAC__FileDecoder *) decoder);
  477. }
  478. static void file_decoder_safe_decoder_delete_(void *decoder)
  479. {
  480. if(decoder) {
  481. file_decoder_safe_decoder_finish_(decoder);
  482. FLAC__file_decoder_delete( (FLAC__FileDecoder *) decoder);
  483. }
  484. }
  485. static FLAC__bool file_decoder_is_eof(void *decoder)
  486. {
  487. return FLAC__file_decoder_get_state((FLAC__FileDecoder *) decoder) == FLAC__FILE_DECODER_END_OF_FILE;
  488. }
  489. static const decoder_funcs_t FILE_DECODER_FUNCTIONS = {
  490. true,
  491. (void* (*) (void)) FLAC__file_decoder_new,
  492. (FLAC__bool (*) (void *, FLAC__bool)) FLAC__file_decoder_set_md5_checking,
  493. (FLAC__bool (*) (void *, const char*)) FLAC__file_decoder_set_filename,
  494. (FLAC__bool (*) (void *)) FLAC__file_decoder_set_metadata_ignore_all,
  495. (FLAC__bool (*) (void *, FLAC__MetadataType)) FLAC__file_decoder_set_metadata_respond,
  496. (FLAC__bool (*) (void *, WriteCallback)) FLAC__file_decoder_set_write_callback,
  497. (FLAC__bool (*) (void *, MetadataCallback)) FLAC__file_decoder_set_metadata_callback,
  498. (FLAC__bool (*) (void *, ErrorCallback)) FLAC__file_decoder_set_error_callback,
  499. (FLAC__bool (*) (void *, void *)) FLAC__file_decoder_set_client_data,
  500. (FLAC__bool (*) (void *)) file_decoder_init,
  501. (void (*) (void *)) file_decoder_safe_decoder_finish_,
  502. (void (*) (void *)) file_decoder_safe_decoder_delete_,
  503. (FLAC__bool (*) (void *)) FLAC__file_decoder_process_until_end_of_metadata,
  504. (FLAC__bool (*) (void *)) FLAC__file_decoder_process_single,
  505. file_decoder_is_eof
  506. };
  507. /*********** HTTP decoder functions */
  508. static gchar *url_;
  509. static FLAC__bool http_decoder_set_md5_checking (void *decoder, FLAC__bool value)
  510. {
  511. (void) value;
  512. // operation unsupported
  513. return FLAC__stream_decoder_get_state ((const FLAC__StreamDecoder *) decoder) ==
  514. FLAC__STREAM_DECODER_UNINITIALIZED;
  515. }
  516. static FLAC__bool http_decoder_set_url (void *decoder, const char* url)
  517. {
  518. (void) decoder;
  519. url_ = g_strdup (url);
  520. return true;
  521. }
  522. static FLAC__StreamDecoderReadStatus http_decoder_read_callback (const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
  523. {
  524. (void) decoder;
  525. (void) client_data;
  526. *bytes = flac_http_read (buffer, *bytes);
  527. return *bytes ? FLAC__STREAM_DECODER_READ_STATUS_CONTINUE : FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
  528. }
  529. static FLAC__bool http_decoder_init (void *decoder)
  530. {
  531. flac_http_open (url_, 0);
  532. g_free (url_);
  533. FLAC__stream_decoder_set_read_callback (decoder, http_decoder_read_callback);
  534. return FLAC__stream_decoder_init( (FLAC__StreamDecoder*) decoder) == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
  535. }
  536. static void http_decoder_safe_decoder_finish_(void *decoder)
  537. {
  538. if(decoder && FLAC__stream_decoder_get_state((FLAC__StreamDecoder *) decoder) != FLAC__STREAM_DECODER_UNINITIALIZED) {
  539. FLAC__stream_decoder_finish((FLAC__StreamDecoder *) decoder);
  540. flac_http_close();
  541. }
  542. }
  543. static void http_decoder_safe_decoder_delete_(void *decoder)
  544. {
  545. if(decoder) {
  546. http_decoder_safe_decoder_finish_(decoder);
  547. FLAC__stream_decoder_delete( (FLAC__StreamDecoder *) decoder);
  548. }
  549. }
  550. static FLAC__bool http_decoder_is_eof(void *decoder)
  551. {
  552. return FLAC__stream_decoder_get_state((FLAC__StreamDecoder *) decoder) == FLAC__STREAM_DECODER_END_OF_STREAM;
  553. }
  554. static const decoder_funcs_t HTTP_DECODER_FUNCTIONS = {
  555. false,
  556. (void* (*) (void)) FLAC__stream_decoder_new,
  557. http_decoder_set_md5_checking,
  558. (FLAC__bool (*) (void *, const char*)) http_decoder_set_url,
  559. (FLAC__bool (*) (void *)) FLAC__stream_decoder_set_metadata_ignore_all,
  560. (FLAC__bool (*) (void *, FLAC__MetadataType)) FLAC__stream_decoder_set_metadata_respond,
  561. (FLAC__bool (*) (void *, WriteCallback)) FLAC__stream_decoder_set_write_callback,
  562. (FLAC__bool (*) (void *, MetadataCallback)) FLAC__stream_decoder_set_metadata_callback,
  563. (FLAC__bool (*) (void *, ErrorCallback)) FLAC__stream_decoder_set_error_callback,
  564. (FLAC__bool (*) (void *, void *)) FLAC__stream_decoder_set_client_data,
  565. (FLAC__bool (*) (void *)) http_decoder_init,
  566. (void (*) (void *)) http_decoder_safe_decoder_finish_,
  567. (void (*) (void *)) http_decoder_safe_decoder_delete_,
  568. (FLAC__bool (*) (void *)) FLAC__stream_decoder_process_until_end_of_metadata,
  569. (FLAC__bool (*) (void *)) FLAC__stream_decoder_process_single,
  570. http_decoder_is_eof
  571. };
  572. static decoder_funcs_t const *decoder_func_table_;
  573. static void init_decoder_func_tables()
  574. {
  575. DECODER_FUNCS [DECODER_FILE] = & FILE_DECODER_FUNCTIONS;
  576. DECODER_FUNCS [DECODER_HTTP] = & HTTP_DECODER_FUNCTIONS;
  577. }
  578. static decoder_t source_to_decoder_type (const char *source)
  579. {
  580. return strncasecmp(source, "http://", 7) ? DECODER_FILE : DECODER_HTTP;
  581. }
  582. static void change_decoder_if_needed (decoder_t new_decoder_type, void **decoderp, decoder_funcs_t const ** fntabp)
  583. {
  584. const decoder_funcs_t *new_fn_table = DECODER_FUNCS [new_decoder_type];
  585. if (*fntabp != new_fn_table) {
  586. (*fntabp)->safe_decoder_delete(*decoderp);
  587. *fntabp = new_fn_table;
  588. *decoderp = new_fn_table -> new_decoder();
  589. }
  590. }
  591. FLAC__bool safe_decoder_init_(const char *filename, void **decoderp, decoder_funcs_t const ** fntabp)
  592. {
  593. if(decoderp == 0 || *decoderp == 0)
  594. return false;
  595. (*fntabp)->safe_decoder_finish(*decoderp);
  596. change_decoder_if_needed(source_to_decoder_type(filename), decoderp, fntabp);
  597. {
  598. decoder_funcs_t const *fntab = *fntabp;
  599. void *decoder = *decoderp;
  600. decoder = *decoderp;
  601. fntab = *fntabp;
  602. fntab -> set_md5_checking(decoder, false);
  603. fntab -> set_source(decoder, filename);
  604. fntab -> set_metadata_ignore_all(decoder);
  605. fntab -> set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO);
  606. fntab -> set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
  607. fntab -> set_write_callback(decoder, write_callback_);
  608. fntab -> set_metadata_callback(decoder, metadata_callback_);
  609. fntab -> set_error_callback(decoder, error_callback_);
  610. fntab -> set_client_data(decoder, &file_info_);
  611. if(!fntab -> decoder_init(decoder))
  612. return false;
  613. if(!fntab -> process_until_end_of_metadata(decoder))
  614. return false;
  615. }
  616. return true;
  617. }
  618. FLAC__StreamDecoderWriteStatus write_callback_(const void *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
  619. {
  620. file_info_struct *file_info = (file_info_struct *)client_data;
  621. const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
  622. const unsigned bits_per_sample = file_info->bits_per_sample;
  623. FLAC__byte *sample_buffer_start;
  624. (void)decoder;
  625. if(file_info->abort_flag)
  626. return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
  627. if((sample_buffer_last_ + wide_samples) > (SAMPLE_BUFFER_SIZE / (channels * file_info->sample_format_bytes_per_sample))) {
  628. 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);
  629. sample_buffer_last_ -= sample_buffer_first_;
  630. sample_buffer_first_ = 0;
  631. }
  632. sample_buffer_start = sample_buffer_ + sample_buffer_last_ * channels * file_info->sample_format_bytes_per_sample;
  633. if(file_info->has_replaygain && flac_cfg.output.replaygain.enable) {
  634. FLAC__replaygain_synthesis__apply_gain(
  635. sample_buffer_start,
  636. !is_big_endian_host_,
  637. file_info->sample_format_bytes_per_sample == 1, /* unsigned_data_out */
  638. buffer,
  639. wide_samples,
  640. channels,
  641. bits_per_sample,
  642. file_info->sample_format_bytes_per_sample * 8,
  643. file_info->replay_scale,
  644. flac_cfg.output.replaygain.hard_limit,
  645. flac_cfg.output.resolution.replaygain.dither,
  646. &file_info->dither_context
  647. );
  648. }
  649. else if(is_big_endian_host_) {
  650. FLAC__plugin_common__pack_pcm_signed_big_endian(
  651. sample_buffer_start,
  652. buffer,
  653. wide_samples,
  654. channels,
  655. bits_per_sample,
  656. file_info->sample_format_bytes_per_sample * 8
  657. );
  658. }
  659. else {
  660. FLAC__plugin_common__pack_pcm_signed_little_endian(
  661. sample_buffer_start,
  662. buffer,
  663. wide_samples,
  664. channels,
  665. bits_per_sample,
  666. file_info->sample_format_bytes_per_sample * 8
  667. );
  668. }
  669. sample_buffer_last_ += wide_samples;
  670. return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
  671. }
  672. void metadata_callback_(const void *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
  673. {
  674. file_info_struct *file_info = (file_info_struct *)client_data;
  675. (void)decoder;
  676. if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
  677. FLAC__ASSERT(metadata->data.stream_info.total_samples < FLAC__U64L(0x100000000)); /* this plugin can only handle < 4 gigasamples */
  678. file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
  679. file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
  680. file_info->channels = metadata->data.stream_info.channels;
  681. file_info->sample_rate = metadata->data.stream_info.sample_rate;
  682. file_info->length_in_msec = (unsigned)((double)file_info->total_samples / (double)file_info->sample_rate * 1000.0 + 0.5);
  683. }
  684. else if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
  685. double gain, peak;
  686. if(grabbag__replaygain_load_from_vorbiscomment(metadata, flac_cfg.output.replaygain.album_mode, &gain, &peak)) {
  687. file_info->has_replaygain = true;
  688. 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);
  689. }
  690. }
  691. }
  692. void error_callback_(const void *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
  693. {
  694. file_info_struct *file_info = (file_info_struct *)client_data;
  695. (void)decoder;
  696. if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
  697. file_info->abort_flag = true;
  698. }