Use config.h rather than passing all defs in CFLAGS
[flac.git] / src / plugin_xmms / plugin.c
1 /* libxmms-flac - XMMS FLAC input plugin
2  * Copyright (C) 2000,2001,2002  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #include <pthread.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <glib.h>
24
25 #include <xmms/plugin.h>
26 #include <xmms/util.h>
27 #include <xmms/configfile.h>
28 #include <xmms/titlestring.h>
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #ifdef HAVE_LANGINFO_CODESET
35 #include <langinfo.h>
36 #endif
37
38 #include "FLAC/all.h"
39 #include "plugin_common/all.h"
40 #include "configure.h"
41 #include "wrap_id3.h"
42 #include "charset.h"
43
44 #ifdef min
45 #undef min
46 #endif
47 #define min(x,y) ((x)<(y)?(x):(y))
48
49 #define FLAC__DO_DITHER
50
51 typedef struct {
52         FLAC__bool abort_flag;
53         FLAC__bool is_playing;
54         FLAC__bool eof;
55         FLAC__bool play_thread_open; /* if true, is_playing must also be true */
56         unsigned total_samples;
57         unsigned bits_per_sample;
58         unsigned channels;
59         unsigned sample_rate;
60         unsigned length_in_msec;
61         AFormat sample_format;
62         int seek_to_in_sec;
63 } file_info_struct;
64
65 static void FLAC_XMMS__init();
66 static int  FLAC_XMMS__is_our_file(char *filename);
67 static void FLAC_XMMS__play_file(char *filename);
68 static void FLAC_XMMS__stop();
69 static void FLAC_XMMS__pause(short p);
70 static void FLAC_XMMS__seek(int time);
71 static int  FLAC_XMMS__get_time();
72 static void FLAC_XMMS__cleanup();
73 static void FLAC_XMMS__get_song_info(char *filename, char **title, int *length);
74
75 static void *play_loop_(void *arg);
76 static FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder);
77 static void safe_decoder_finish_(FLAC__FileDecoder *decoder);
78 static void safe_decoder_delete_(FLAC__FileDecoder *decoder);
79 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
80 static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
81 static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
82
83 InputPlugin flac_ip =
84 {
85         NULL,
86         NULL,
87         "Reference FLAC Player v" VERSION,
88         FLAC_XMMS__init,
89         FLAC_XMMS__aboutbox,
90         FLAC_XMMS__configure,
91         FLAC_XMMS__is_our_file,
92         NULL,
93         FLAC_XMMS__play_file,
94         FLAC_XMMS__stop,
95         FLAC_XMMS__pause,
96         FLAC_XMMS__seek,
97         NULL,
98         FLAC_XMMS__get_time,
99         NULL,
100         NULL,
101         FLAC_XMMS__cleanup,
102         NULL,
103         NULL,
104         NULL,
105         NULL,
106         FLAC_XMMS__get_song_info,
107         NULL,           /* file_info_box */
108         NULL
109 };
110
111 #define SAMPLES_PER_WRITE 512
112 static FLAC__int32 reservoir_[FLAC__MAX_BLOCK_SIZE * 2/*for overflow*/ * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
113 static FLAC__byte sample_buffer_[SAMPLES_PER_WRITE * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8)]; /* (24/8) for max bytes per sample */
114 static unsigned wide_samples_in_reservoir_ = 0;
115 static FLAC__FileDecoder *decoder_ = 0;
116 static file_info_struct file_info_;
117 static pthread_t decode_thread_;
118 static FLAC__bool audio_error_ = false;
119
120 InputPlugin *get_iplugin_info()
121 {
122         flac_ip.description = g_strdup_printf("Reference FLAC Player v%s", FLAC__VERSION_STRING);
123         return &flac_ip;
124 }
125
126 void FLAC_XMMS__init()
127 {
128         ConfigFile *cfg;
129
130         flac_cfg.tag_override = FALSE;
131         g_free(flac_cfg.tag_format);
132         flac_cfg.convert_char_set = FALSE;
133
134         cfg = xmms_cfg_open_default_file();
135
136         xmms_cfg_read_boolean(cfg, "flac", "tag_override", &flac_cfg.tag_override);
137
138         if(!xmms_cfg_read_string(cfg, "flac", "tag_format", &flac_cfg.tag_format))
139                 flac_cfg.tag_format = g_strdup("%p - %t");
140
141         xmms_cfg_read_boolean(cfg, "flac", "convert_char_set", &flac_cfg.convert_char_set);
142
143         if(!xmms_cfg_read_string(cfg, "flac", "file_char_set", &flac_cfg.file_char_set))
144                 flac_cfg.file_char_set = FLAC_plugin__charset_get_current();
145
146         if(!xmms_cfg_read_string(cfg, "flac", "user_char_set", &flac_cfg.user_char_set))
147                 flac_cfg.user_char_set = FLAC_plugin__charset_get_current();
148
149         decoder_ = FLAC__file_decoder_new();
150 }
151
152 int FLAC_XMMS__is_our_file(char *filename)
153 {
154         char *ext;
155
156         ext = strrchr(filename, '.');
157         if(ext)
158                 if(!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
159                         return 1;
160         return 0;
161 }
162
163 void FLAC_XMMS__play_file(char *filename)
164 {
165         FILE *f;
166         gchar *ret;
167
168         wide_samples_in_reservoir_ = 0;
169         audio_error_ = false;
170         file_info_.abort_flag = false;
171         file_info_.is_playing = false;
172         file_info_.eof = false;
173         file_info_.play_thread_open = false;
174
175         if(0 == (f = fopen(filename, "r")))
176                 return;
177         fclose(f);
178
179         if(decoder_ == 0)
180                 return;
181
182         if(!safe_decoder_init_(filename, decoder_))
183                 return;
184
185         file_info_.is_playing = true;
186
187         if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
188                 audio_error_ = true;
189                 safe_decoder_finish_(decoder_);
190                 return;
191         }
192
193         ret = flac_format_song_title(filename);
194         flac_ip.set_info(ret, file_info_.length_in_msec, file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample, file_info_.sample_rate, file_info_.channels);
195
196         g_free(ret);
197
198         file_info_.seek_to_in_sec = -1;
199         file_info_.play_thread_open = true;
200         pthread_create(&decode_thread_, NULL, play_loop_, NULL);
201 }
202
203 void FLAC_XMMS__stop()
204 {
205         if(file_info_.is_playing) {
206                 file_info_.is_playing = false;
207                 if(file_info_.play_thread_open) {
208                         file_info_.play_thread_open = false;
209                         pthread_join(decode_thread_, NULL);
210                 }
211                 flac_ip.output->close_audio();
212                 safe_decoder_finish_(decoder_);
213         }
214 }
215
216 void FLAC_XMMS__pause(short p)
217 {
218         flac_ip.output->pause(p);
219 }
220
221 void FLAC_XMMS__seek(int time)
222 {
223         file_info_.seek_to_in_sec = time;
224         file_info_.eof = false;
225
226         while(file_info_.seek_to_in_sec != -1)
227                 xmms_usleep(10000);
228 }
229
230 int FLAC_XMMS__get_time()
231 {
232         if(audio_error_)
233                 return -2;
234         if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
235                 return -1;
236         else
237                 return flac_ip.output->output_time();
238 }
239
240 void FLAC_XMMS__cleanup()
241 {
242         safe_decoder_delete_(decoder_);
243         decoder_ = 0;
244 }
245
246 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
247 {
248         FLAC__StreamMetadata streaminfo;
249
250         if(0 == filename)
251                 filename = "";
252
253         if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
254                 /* @@@ how to report the error? */
255                 if(title) {
256                         static const char *errtitle = "Invalid FLAC File: ";
257                         *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
258                         sprintf(*title, "%s\"%s\"", errtitle, filename);
259                 }
260                 if(length_in_msec)
261                         *length_in_msec = -1;
262                 return;
263         }
264
265         if(title) {
266                 *title = flac_format_song_title(filename);
267         }
268         if(length_in_msec)
269                 *length_in_msec = streaminfo.data.stream_info.total_samples * 10 / (streaminfo.data.stream_info.sample_rate / 100);
270 }
271
272 /***********************************************************************
273  * local routines
274  **********************************************************************/
275
276 void *play_loop_(void *arg)
277 {
278         (void)arg;
279
280         while(file_info_.is_playing) {
281                 if(!file_info_.eof) {
282                         while(wide_samples_in_reservoir_ < SAMPLES_PER_WRITE) {
283                                 if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
284                                         file_info_.eof = true;
285                                         break;
286                                 }
287                                 else if(!FLAC__file_decoder_process_single(decoder_)) {
288                                         /*@@@ this should probably be a dialog */
289                                         fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
290                                         file_info_.eof = true;
291                                         break;
292                                 }
293                         }
294                         if(wide_samples_in_reservoir_ > 0) {
295                                 const unsigned channels = file_info_.channels;
296                                 const unsigned bits_per_sample = file_info_.bits_per_sample;
297 #ifdef FLAC__DO_DITHER
298                                 const unsigned target_bps = min(bits_per_sample, 16);
299 #else
300                                 const unsigned target_bps = bits_per_sample;
301 #endif
302                                 const unsigned n = min(wide_samples_in_reservoir_, SAMPLES_PER_WRITE);
303                                 const unsigned delta = n * channels;
304                                 int bytes = (int)FLAC__plugin_common__pack_pcm_signed_little_endian(sample_buffer_, reservoir_, n, channels, bits_per_sample, target_bps);
305                                 unsigned i;
306
307                                 for(i = delta; i < wide_samples_in_reservoir_ * channels; i++)
308                                         reservoir_[i-delta] = reservoir_[i];
309                                 wide_samples_in_reservoir_ -= n;
310
311                                 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, channels, bytes, sample_buffer_);
312                                 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
313                                         xmms_usleep(10000);
314                                 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
315                                         flac_ip.output->write_audio(sample_buffer_, bytes);
316                         }
317                         else {
318                                 file_info_.eof = true;
319                                 xmms_usleep(10000);
320                         }
321                 }
322                 else
323                         xmms_usleep(10000);
324                 if(file_info_.seek_to_in_sec != -1) {
325                         const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
326                         unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
327                         if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
328                                 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
329                                 file_info_.seek_to_in_sec = -1;
330                                 file_info_.eof = false;
331                                 wide_samples_in_reservoir_ = 0;
332                         }
333                 }
334         }
335
336         safe_decoder_finish_(decoder_);
337
338         /* are these two calls necessary? */
339         flac_ip.output->buffer_free();
340         flac_ip.output->buffer_free();
341
342         pthread_exit(NULL);
343         return 0; /* to silence the compiler warning about not returning a value */
344 }
345
346 FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder)
347 {
348         if(decoder == 0)
349                 return false;
350
351         safe_decoder_finish_(decoder);
352
353         FLAC__file_decoder_set_md5_checking(decoder, false);
354         FLAC__file_decoder_set_filename(decoder, filename);
355         FLAC__file_decoder_set_write_callback(decoder, write_callback_);
356         FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
357         FLAC__file_decoder_set_error_callback(decoder, error_callback_);
358         FLAC__file_decoder_set_client_data(decoder, &file_info_);
359         if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
360                 return false;
361
362         if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
363                 return false;
364
365         return true;
366 }
367
368 void safe_decoder_finish_(FLAC__FileDecoder *decoder)
369 {
370         if(decoder && FLAC__file_decoder_get_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
371                 FLAC__file_decoder_finish(decoder);
372 }
373
374 void safe_decoder_delete_(FLAC__FileDecoder *decoder)
375 {
376         if(decoder) {
377                 safe_decoder_finish_(decoder);
378                 FLAC__file_decoder_delete(decoder);
379         }
380 }
381
382 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
383 {
384         file_info_struct *file_info = (file_info_struct *)client_data;
385         const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
386         unsigned wide_sample, offset_sample, channel;
387
388         (void)decoder;
389
390         if(file_info->abort_flag)
391                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
392
393         for(offset_sample = wide_samples_in_reservoir_ * channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
394                 for(channel = 0; channel < channels; channel++, offset_sample++)
395                         reservoir_[offset_sample] = buffer[channel][wide_sample];
396
397         wide_samples_in_reservoir_ += wide_samples;
398
399         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
400 }
401
402 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
403 {
404         file_info_struct *file_info = (file_info_struct *)client_data;
405         (void)decoder;
406         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
407                 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
408                 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
409                 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
410                 file_info->channels = metadata->data.stream_info.channels;
411                 file_info->sample_rate = metadata->data.stream_info.sample_rate;
412
413 #ifdef FLAC__DO_DITHER
414                 if(file_info->bits_per_sample == 8) {
415                         file_info->sample_format = FMT_S8;
416                 }
417                 else if(file_info->bits_per_sample == 16 || file_info->bits_per_sample == 24) {
418                         file_info->sample_format = FMT_S16_LE;
419                 }
420                 else {
421                         /*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16/24-bit samples\n", "ERROR: plugin can only handle 8/16/24-bit samples", 0); */
422                         file_info->abort_flag = true;
423                         return;
424                 }
425 #else
426                 if(file_info->bits_per_sample == 8) {
427                         file_info->sample_format = FMT_S8;
428                 }
429                 else if(file_info->bits_per_sample == 16) {
430                         file_info->sample_format = FMT_S16_LE;
431                 }
432                 else {
433                         /*@@@ 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); */
434                         file_info->abort_flag = true;
435                         return;
436                 }
437 #endif
438                 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
439         }
440 }
441
442 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
443 {
444         file_info_struct *file_info = (file_info_struct *)client_data;
445         (void)decoder;
446         if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
447                 file_info->abort_flag = true;
448 }