Daisuke Shimamura's id3 v1/v2 + i18n patches
[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         xmms_cfg_read_boolean(cfg, "flac", "tag_override", &flac_cfg.tag_override);
129         if (!xmms_cfg_read_string(cfg, "flac", "tag_format",
130                                   &flac_cfg.tag_format))
131                 flac_cfg.tag_format = g_strdup("%p - %t");
132
133         xmms_cfg_read_boolean(cfg, "flac", "convert_char_set", &flac_cfg.convert_char_set);
134         if (!xmms_cfg_read_string(cfg, "flac", "file_char_set",
135                                   &flac_cfg.file_char_set))
136         {
137                 flac_cfg.file_char_set = get_current_charset();
138         }
139         if (!xmms_cfg_read_string(cfg, "flac", "user_char_set",
140                                   &flac_cfg.user_char_set))
141         {
142                 flac_cfg.user_char_set = get_current_charset();
143         }
144
145         decoder_ = FLAC__file_decoder_new();
146 }
147
148 int FLAC_XMMS__is_our_file(char *filename)
149 {
150         char *ext;
151
152         ext = strrchr(filename, '.');
153         if(ext)
154                 if(!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
155                         return 1;
156         return 0;
157 }
158
159 void FLAC_XMMS__play_file(char *filename)
160 {
161         FILE *f;
162         gchar *ret;
163
164         reservoir_samples_ = 0;
165         audio_error_ = false;
166         file_info_.abort_flag = false;
167         file_info_.is_playing = false;
168         file_info_.eof = false;
169         file_info_.play_thread_open = false;
170
171         if(0 == (f = fopen(filename, "r")))
172                 return;
173         fclose(f);
174
175         if(decoder_ == 0)
176                 return;
177
178         if(!safe_decoder_init_(filename, decoder_))
179                 return;
180
181         file_info_.is_playing = true;
182
183         if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
184                 audio_error_ = true;
185                 safe_decoder_finish_(decoder_);
186                 return;
187         }
188
189         ret = flac_format_song_title(filename);
190         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);
191
192         g_free(ret);
193
194         file_info_.seek_to_in_sec = -1;
195         file_info_.play_thread_open = true;
196         pthread_create(&decode_thread_, NULL, play_loop_, NULL);
197 }
198
199 void FLAC_XMMS__stop()
200 {
201         if(file_info_.is_playing) {
202                 file_info_.is_playing = false;
203                 if(file_info_.play_thread_open) {
204                         file_info_.play_thread_open = false;
205                         pthread_join(decode_thread_, NULL);
206                 }
207                 flac_ip.output->close_audio();
208                 safe_decoder_finish_(decoder_);
209         }
210 }
211
212 void FLAC_XMMS__pause(short p)
213 {
214         flac_ip.output->pause(p);
215 }
216
217 void FLAC_XMMS__seek(int time)
218 {
219         file_info_.seek_to_in_sec = time;
220         file_info_.eof = false;
221
222         while(file_info_.seek_to_in_sec != -1)
223                 xmms_usleep(10000);
224 }
225
226 int FLAC_XMMS__get_time()
227 {
228         if(audio_error_)
229                 return -2;
230         if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
231                 return -1;
232         else
233                 return flac_ip.output->output_time();
234 }
235
236 void FLAC_XMMS__cleanup()
237 {
238         safe_decoder_delete_(decoder_);
239         decoder_ = 0;
240 }
241
242 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
243 {
244         FLAC__StreamMetadata streaminfo;
245
246         if(0 == filename)
247                 filename = "";
248
249         if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
250                 /* @@@ how to report the error? */
251                 if(title) {
252                         static const char *errtitle = "Invalid FLAC File: ";
253                         *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
254                         sprintf(*title, "%s\"%s\"", errtitle, filename);
255                 }
256                 if(length_in_msec)
257                         *length_in_msec = -1;
258                 return;
259         }
260
261         if(title) {
262                 *title = flac_format_song_title(filename);
263         }
264         if(length_in_msec)
265                 *length_in_msec = streaminfo.data.stream_info.total_samples * 10 / (streaminfo.data.stream_info.sample_rate / 100);
266 }
267
268 /***********************************************************************
269  * local routines
270  **********************************************************************/
271
272 void *play_loop_(void *arg)
273 {
274         (void)arg;
275
276         while(file_info_.is_playing) {
277                 if(!file_info_.eof) {
278                         while(reservoir_samples_ < SAMPLES_PER_WRITE) {
279                                 if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
280                                         file_info_.eof = true;
281                                         break;
282                                 }
283                                 else if(!FLAC__file_decoder_process_one_frame(decoder_)) {
284                                         /*@@@ this should probably be a dialog */
285                                         fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
286                                         file_info_.eof = true;
287                                         break;
288                                 }
289                         }
290                         if(reservoir_samples_ > 0) {
291                                 const unsigned channels = file_info_.channels;
292                                 const unsigned bytes_per_sample = (file_info_.bits_per_sample+7)/8;
293                                 unsigned i, n = min(reservoir_samples_, SAMPLES_PER_WRITE), delta;
294                                 unsigned bytes = n * bytes_per_sample * channels;
295
296                                 for(i = 0; i < bytes; i++)
297                                         output_[i] = reservoir_[i];
298                                 delta = i;
299                                 for( ; i < reservoir_samples_*bytes_per_sample*channels; i++)
300                                         reservoir_[i-delta] = reservoir_[i];
301                                 reservoir_samples_ -= n;
302
303                                 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, channels, bytes, output_);
304                                 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
305                                         xmms_usleep(10000);
306                                 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
307                                         flac_ip.output->write_audio(output_, bytes);
308                         }
309                         else {
310                                 file_info_.eof = true;
311                                 xmms_usleep(10000);
312                         }
313                 }
314                 else
315                         xmms_usleep(10000);
316                 if(file_info_.seek_to_in_sec != -1) {
317                         const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
318                         unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
319                         if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
320                                 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
321                                 file_info_.seek_to_in_sec = -1;
322                                 file_info_.eof = false;
323                                 reservoir_samples_ = 0;
324                         }
325                 }
326         }
327
328         safe_decoder_finish_(decoder_);
329
330         /* are these two calls necessary? */
331         flac_ip.output->buffer_free();
332         flac_ip.output->buffer_free();
333
334         pthread_exit(NULL);
335         return 0; /* to silence the compiler warning about not returning a value */
336 }
337
338 FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder)
339 {
340         if(decoder == 0)
341                 return false;
342
343         safe_decoder_finish_(decoder);
344
345         FLAC__file_decoder_set_md5_checking(decoder, false);
346         FLAC__file_decoder_set_filename(decoder, filename);
347         FLAC__file_decoder_set_write_callback(decoder, write_callback_);
348         FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
349         FLAC__file_decoder_set_error_callback(decoder, error_callback_);
350         FLAC__file_decoder_set_client_data(decoder, &file_info_);
351         if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
352                 return false;
353
354         if(!FLAC__file_decoder_process_metadata(decoder))
355                 return false;
356
357         return true;
358 }
359
360 void safe_decoder_finish_(FLAC__FileDecoder *decoder)
361 {
362         if(decoder && FLAC__file_decoder_get_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
363                 FLAC__file_decoder_finish(decoder);
364 }
365
366 void safe_decoder_delete_(FLAC__FileDecoder *decoder)
367 {
368         if(decoder) {
369                 safe_decoder_finish_(decoder);
370                 FLAC__file_decoder_delete(decoder);
371         }
372 }
373
374 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
375 {
376         file_info_struct *file_info = (file_info_struct *)client_data;
377         const unsigned bps = file_info->bits_per_sample, channels = file_info->channels, wide_samples = frame->header.blocksize;
378         unsigned wide_sample, sample, channel;
379         FLAC__int8 *scbuffer = (FLAC__int8*)reservoir_;
380         FLAC__int16 *ssbuffer = (FLAC__int16*)reservoir_;
381
382         (void)decoder;
383
384         if(file_info->abort_flag)
385                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
386
387         if(bps == 8) {
388                 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
389                         for(channel = 0; channel < channels; channel++, sample++)
390                                 scbuffer[sample] = (FLAC__int8)buffer[channel][wide_sample];
391         }
392         else if(bps == 16) {
393                 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
394                         for(channel = 0; channel < channels; channel++, sample++)
395                                 ssbuffer[sample] = (FLAC__int16)buffer[channel][wide_sample];
396         }
397         else {
398                 file_info->abort_flag = true;
399                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
400         }
401
402         reservoir_samples_ += wide_samples;
403
404         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
405 }
406
407 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
408 {
409         file_info_struct *file_info = (file_info_struct *)client_data;
410         (void)decoder;
411         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
412                 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
413                 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
414                 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
415                 file_info->channels = metadata->data.stream_info.channels;
416                 file_info->sample_rate = metadata->data.stream_info.sample_rate;
417
418                 if(file_info->bits_per_sample == 8) {
419                         file_info->sample_format = FMT_S8;
420                 }
421                 else if(file_info->bits_per_sample == 16) {
422                         file_info->sample_format = FMT_S16_NE;
423                 }
424                 else {
425                         file_info->abort_flag = true;
426                         return;
427                 }
428                 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
429         }
430 }
431
432 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
433 {
434         file_info_struct *file_info = (file_info_struct *)client_data;
435         (void)decoder;
436         if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
437                 file_info->abort_flag = true;
438 }
439