fixes based on a pass over everything with autoconf/automake
[platform/upstream/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_LANGINFO_CODESET
31 #include <langinfo.h>
32 #endif
33
34 #include "FLAC/all.h"
35 #include "configure.h"
36 #include "wrap_id3.h"
37 #include "charset.h"
38
39 #ifdef min
40 #undef min
41 #endif
42 #define min(x,y) ((x)<(y)?(x):(y))
43
44 typedef struct {
45         FLAC__bool abort_flag;
46         FLAC__bool is_playing;
47         FLAC__bool eof;
48         FLAC__bool play_thread_open; /* if true, is_playing must also be true */
49         unsigned total_samples;
50         unsigned bits_per_sample;
51         unsigned channels;
52         unsigned sample_rate;
53         unsigned length_in_msec;
54         AFormat sample_format;
55         int seek_to_in_sec;
56 } file_info_struct;
57
58 static void FLAC_XMMS__init();
59 static int  FLAC_XMMS__is_our_file(char *filename);
60 static void FLAC_XMMS__play_file(char *filename);
61 static void FLAC_XMMS__stop();
62 static void FLAC_XMMS__pause(short p);
63 static void FLAC_XMMS__seek(int time);
64 static int  FLAC_XMMS__get_time();
65 static void FLAC_XMMS__cleanup();
66 static void FLAC_XMMS__get_song_info(char *filename, char **title, int *length);
67
68 static void *play_loop_(void *arg);
69 static FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder);
70 static void safe_decoder_finish_(FLAC__FileDecoder *decoder);
71 static void safe_decoder_delete_(FLAC__FileDecoder *decoder);
72 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
73 static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
74 static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
75
76 InputPlugin flac_ip =
77 {
78         NULL,
79         NULL,
80         "FLAC Player v" FLAC__VERSION_STRING,
81         FLAC_XMMS__init,
82         FLAC_XMMS__aboutbox,
83         FLAC_XMMS__configure,
84         FLAC_XMMS__is_our_file,
85         NULL,
86         FLAC_XMMS__play_file,
87         FLAC_XMMS__stop,
88         FLAC_XMMS__pause,
89         FLAC_XMMS__seek,
90         NULL,
91         FLAC_XMMS__get_time,
92         NULL,
93         NULL,
94         FLAC_XMMS__cleanup,
95         NULL,
96         NULL,
97         NULL,
98         NULL,
99         FLAC_XMMS__get_song_info,
100         NULL,           /* file_info_box */
101         NULL
102 };
103
104 #define SAMPLES_PER_WRITE 512
105 static FLAC__byte reservoir_[FLAC__MAX_BLOCK_SIZE * 2 * 2 * 2]; /* *2 for max bytes-per-sample, *2 for max channels, another *2 for overflow */
106 static FLAC__byte output_[FLAC__MAX_BLOCK_SIZE * 2 * 2]; /* *2 for max bytes-per-sample, *2 for max channels */
107 static unsigned reservoir_samples_ = 0;
108 static FLAC__FileDecoder *decoder_ = 0;
109 static file_info_struct file_info_;
110 static pthread_t decode_thread_;
111 static FLAC__bool audio_error_ = false;
112
113 InputPlugin *get_iplugin_info()
114 {
115         flac_ip.description = g_strdup_printf("FLAC Player v%s", FLAC__VERSION_STRING);
116         return &flac_ip;
117 }
118
119 void FLAC_XMMS__init()
120 {
121         ConfigFile *cfg;
122
123         flac_cfg.tag_override = FALSE;
124         g_free(flac_cfg.tag_format);
125         flac_cfg.convert_char_set = FALSE;
126                 
127         cfg = xmms_cfg_open_default_file();
128
129         xmms_cfg_read_boolean(cfg, "flac", "tag_override", &flac_cfg.tag_override);
130
131         if(!xmms_cfg_read_string(cfg, "flac", "tag_format", &flac_cfg.tag_format))
132                 flac_cfg.tag_format = g_strdup("%p - %t");
133
134         xmms_cfg_read_boolean(cfg, "flac", "convert_char_set", &flac_cfg.convert_char_set);
135
136         if(!xmms_cfg_read_string(cfg, "flac", "file_char_set", &flac_cfg.file_char_set))
137                 flac_cfg.file_char_set = get_current_charset();
138
139         if(!xmms_cfg_read_string(cfg, "flac", "user_char_set", &flac_cfg.user_char_set))
140                 flac_cfg.user_char_set = get_current_charset();
141
142         decoder_ = FLAC__file_decoder_new();
143 }
144
145 int FLAC_XMMS__is_our_file(char *filename)
146 {
147         char *ext;
148
149         ext = strrchr(filename, '.');
150         if(ext)
151                 if(!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
152                         return 1;
153         return 0;
154 }
155
156 void FLAC_XMMS__play_file(char *filename)
157 {
158         FILE *f;
159         gchar *ret;
160
161         reservoir_samples_ = 0;
162         audio_error_ = false;
163         file_info_.abort_flag = false;
164         file_info_.is_playing = false;
165         file_info_.eof = false;
166         file_info_.play_thread_open = false;
167
168         if(0 == (f = fopen(filename, "r")))
169                 return;
170         fclose(f);
171
172         if(decoder_ == 0)
173                 return;
174
175         if(!safe_decoder_init_(filename, decoder_))
176                 return;
177
178         file_info_.is_playing = true;
179
180         if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
181                 audio_error_ = true;
182                 safe_decoder_finish_(decoder_);
183                 return;
184         }
185
186         ret = flac_format_song_title(filename);
187         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);
188
189         g_free(ret);
190
191         file_info_.seek_to_in_sec = -1;
192         file_info_.play_thread_open = true;
193         pthread_create(&decode_thread_, NULL, play_loop_, NULL);
194 }
195
196 void FLAC_XMMS__stop()
197 {
198         if(file_info_.is_playing) {
199                 file_info_.is_playing = false;
200                 if(file_info_.play_thread_open) {
201                         file_info_.play_thread_open = false;
202                         pthread_join(decode_thread_, NULL);
203                 }
204                 flac_ip.output->close_audio();
205                 safe_decoder_finish_(decoder_);
206         }
207 }
208
209 void FLAC_XMMS__pause(short p)
210 {
211         flac_ip.output->pause(p);
212 }
213
214 void FLAC_XMMS__seek(int time)
215 {
216         file_info_.seek_to_in_sec = time;
217         file_info_.eof = false;
218
219         while(file_info_.seek_to_in_sec != -1)
220                 xmms_usleep(10000);
221 }
222
223 int FLAC_XMMS__get_time()
224 {
225         if(audio_error_)
226                 return -2;
227         if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
228                 return -1;
229         else
230                 return flac_ip.output->output_time();
231 }
232
233 void FLAC_XMMS__cleanup()
234 {
235         safe_decoder_delete_(decoder_);
236         decoder_ = 0;
237 }
238
239 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
240 {
241         FLAC__StreamMetadata streaminfo;
242
243         if(0 == filename)
244                 filename = "";
245
246         if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
247                 /* @@@ how to report the error? */
248                 if(title) {
249                         static const char *errtitle = "Invalid FLAC File: ";
250                         *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
251                         sprintf(*title, "%s\"%s\"", errtitle, filename);
252                 }
253                 if(length_in_msec)
254                         *length_in_msec = -1;
255                 return;
256         }
257
258         if(title) {
259                 *title = flac_format_song_title(filename);
260         }
261         if(length_in_msec)
262                 *length_in_msec = streaminfo.data.stream_info.total_samples * 10 / (streaminfo.data.stream_info.sample_rate / 100);
263 }
264
265 /***********************************************************************
266  * local routines
267  **********************************************************************/
268
269 void *play_loop_(void *arg)
270 {
271         (void)arg;
272
273         while(file_info_.is_playing) {
274                 if(!file_info_.eof) {
275                         while(reservoir_samples_ < SAMPLES_PER_WRITE) {
276                                 if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
277                                         file_info_.eof = true;
278                                         break;
279                                 }
280                                 else if(!FLAC__file_decoder_process_single(decoder_)) {
281                                         /*@@@ this should probably be a dialog */
282                                         fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
283                                         file_info_.eof = true;
284                                         break;
285                                 }
286                         }
287                         if(reservoir_samples_ > 0) {
288                                 const unsigned channels = file_info_.channels;
289                                 const unsigned bytes_per_sample = (file_info_.bits_per_sample+7)/8;
290                                 unsigned i, n = min(reservoir_samples_, SAMPLES_PER_WRITE), delta;
291                                 unsigned bytes = n * bytes_per_sample * channels;
292
293                                 for(i = 0; i < bytes; i++)
294                                         output_[i] = reservoir_[i];
295                                 delta = i;
296                                 for( ; i < reservoir_samples_*bytes_per_sample*channels; i++)
297                                         reservoir_[i-delta] = reservoir_[i];
298                                 reservoir_samples_ -= n;
299
300                                 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, channels, bytes, output_);
301                                 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
302                                         xmms_usleep(10000);
303                                 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
304                                         flac_ip.output->write_audio(output_, bytes);
305                         }
306                         else {
307                                 file_info_.eof = true;
308                                 xmms_usleep(10000);
309                         }
310                 }
311                 else
312                         xmms_usleep(10000);
313                 if(file_info_.seek_to_in_sec != -1) {
314                         const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
315                         unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
316                         if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
317                                 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
318                                 file_info_.seek_to_in_sec = -1;
319                                 file_info_.eof = false;
320                                 reservoir_samples_ = 0;
321                         }
322                 }
323         }
324
325         safe_decoder_finish_(decoder_);
326
327         /* are these two calls necessary? */
328         flac_ip.output->buffer_free();
329         flac_ip.output->buffer_free();
330
331         pthread_exit(NULL);
332         return 0; /* to silence the compiler warning about not returning a value */
333 }
334
335 FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder)
336 {
337         if(decoder == 0)
338                 return false;
339
340         safe_decoder_finish_(decoder);
341
342         FLAC__file_decoder_set_md5_checking(decoder, false);
343         FLAC__file_decoder_set_filename(decoder, filename);
344         FLAC__file_decoder_set_write_callback(decoder, write_callback_);
345         FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
346         FLAC__file_decoder_set_error_callback(decoder, error_callback_);
347         FLAC__file_decoder_set_client_data(decoder, &file_info_);
348         if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
349                 return false;
350
351         if(!FLAC__file_decoder_process_until_end_of_metadata(decoder))
352                 return false;
353
354         return true;
355 }
356
357 void safe_decoder_finish_(FLAC__FileDecoder *decoder)
358 {
359         if(decoder && FLAC__file_decoder_get_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
360                 FLAC__file_decoder_finish(decoder);
361 }
362
363 void safe_decoder_delete_(FLAC__FileDecoder *decoder)
364 {
365         if(decoder) {
366                 safe_decoder_finish_(decoder);
367                 FLAC__file_decoder_delete(decoder);
368         }
369 }
370
371 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
372 {
373         file_info_struct *file_info = (file_info_struct *)client_data;
374         const unsigned bps = file_info->bits_per_sample, channels = file_info->channels, wide_samples = frame->header.blocksize;
375         unsigned wide_sample, sample, channel;
376         FLAC__int8 *scbuffer = (FLAC__int8*)reservoir_;
377         FLAC__int16 *ssbuffer = (FLAC__int16*)reservoir_;
378
379         (void)decoder;
380
381         if(file_info->abort_flag)
382                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
383
384         if(bps == 8) {
385                 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
386                         for(channel = 0; channel < channels; channel++, sample++)
387                                 scbuffer[sample] = (FLAC__int8)buffer[channel][wide_sample];
388         }
389         else if(bps == 16) {
390                 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
391                         for(channel = 0; channel < channels; channel++, sample++)
392                                 ssbuffer[sample] = (FLAC__int16)buffer[channel][wide_sample];
393         }
394         else {
395                 file_info->abort_flag = true;
396                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
397         }
398
399         reservoir_samples_ += wide_samples;
400
401         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
402 }
403
404 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
405 {
406         file_info_struct *file_info = (file_info_struct *)client_data;
407         (void)decoder;
408         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
409                 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
410                 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
411                 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
412                 file_info->channels = metadata->data.stream_info.channels;
413                 file_info->sample_rate = metadata->data.stream_info.sample_rate;
414
415                 if(file_info->bits_per_sample == 8) {
416                         file_info->sample_format = FMT_S8;
417                 }
418                 else if(file_info->bits_per_sample == 16) {
419                         file_info->sample_format = FMT_S16_NE;
420                 }
421                 else {
422                         file_info->abort_flag = true;
423                         return;
424                 }
425                 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
426         }
427 }
428
429 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
430 {
431         file_info_struct *file_info = (file_info_struct *)client_data;
432         (void)decoder;
433         if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
434                 file_info->abort_flag = true;
435 }
436