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