fix some compile bugs related to RESERVOIR_TEST
[flac.git] / src / plugin_xmms / plugin.c
1 /* libxmms-flac - XMMS FLAC input plugin
2  * Copyright (C) 2000,2001  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 <assert.h>
24 #include <glib.h>
25
26 #include "xmms/plugin.h"
27 #include "xmms/util.h"
28 #include "FLAC/all.h"
29
30 #ifdef min
31 #undef min
32 #endif
33 #define min(x,y) ((x)<(y)?(x):(y))
34
35 typedef struct {
36         byte raw[128];
37         char title[31];
38         char artist[31];
39         char album[31];
40         char comment[31];
41         unsigned year;
42         unsigned track; /* may be 0 if v1 (not v1.1) tag */
43         unsigned genre;
44         char description[1024]; /* the formatted description passed to xmms */
45 } id3v1_struct;
46
47 typedef struct {
48         bool abort_flag;
49         bool is_playing;
50         bool eof;
51         unsigned total_samples;
52         unsigned bits_per_sample;
53         unsigned channels;
54         unsigned sample_rate;
55         unsigned length_in_msec;
56         AFormat sample_format;
57         int seek_to_in_sec;
58 } file_info_struct;
59
60 static void FLAC_XMMS__init();
61 static int  FLAC_XMMS__is_our_file(char *filename);
62 static void FLAC_XMMS__play_file(char *filename);
63 static void FLAC_XMMS__stop();
64 static void FLAC_XMMS__pause(short p);
65 static void FLAC_XMMS__seek(int time);
66 static int  FLAC_XMMS__get_time();
67 static void FLAC_XMMS__cleanup();
68 static void FLAC_XMMS__get_song_info(char *filename, char **title, int *length);
69
70 static bool get_id3v1_tag_(const char *filename, id3v1_struct *tag);
71 static void *play_loop_(void *arg);
72 static bool decoder_init_(const char *filename);
73 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
74 static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
75 static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
76
77
78 InputPlugin flac_ip =
79 {
80         NULL,
81         NULL,
82         "FLAC Player v" FLAC__VERSION_STRING,
83         FLAC_XMMS__init,
84         NULL,
85         NULL,
86         FLAC_XMMS__is_our_file,
87         NULL,
88         FLAC_XMMS__play_file,
89         FLAC_XMMS__stop,
90         FLAC_XMMS__pause,
91         FLAC_XMMS__seek,
92         NULL,
93         FLAC_XMMS__get_time,
94         NULL,
95         NULL,
96         FLAC_XMMS__cleanup,
97         NULL,
98         NULL,
99         NULL,
100         NULL,
101         FLAC_XMMS__get_song_info,
102         NULL,                   /* file_info_box */
103         NULL
104 };
105
106 static byte reservoir_[FLAC__MAX_BLOCK_SIZE * 2 * 2 * 2]; /* *2 for max bytes-per-sample, *2 for max channels, another *2 for overflow */
107 #ifdef RESERVOIR_TEST
108 #define SAMPLES_PER_WRITE 512
109 static byte output_[FLAC__MAX_BLOCK_SIZE * 2 * 2]; /* *2 for max bytes-per-sample, *2 for max channels */
110 #endif
111 static unsigned reservoir_samples_;
112 static FLAC__FileDecoder *decoder_;
113 static file_info_struct file_info_;
114 static pthread_t decode_thread_;
115 static bool audio_error_ = false;
116
117 InputPlugin *get_iplugin_info()
118 {
119         flac_ip.description = g_strdup_printf("FLAC Player v%s", FLAC__VERSION_STRING);
120         return &flac_ip;
121 }
122
123 void FLAC_XMMS__init()
124 {
125         decoder_ = FLAC__file_decoder_get_new_instance();
126 }
127
128 int FLAC_XMMS__is_our_file(char *filename)
129 {
130         char *ext;
131
132         ext = strrchr(filename, '.');
133         if (ext)
134                 if (!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
135                         return 1;
136         return 0;
137 }
138
139 void FLAC_XMMS__play_file(char *filename)
140 {
141         FILE *f;
142         id3v1_struct tag;
143
144         if(0 == (f = fopen(filename, "r")))
145                 return;
146         fclose(f);
147
148         if(!decoder_init_(filename))
149                 return;
150
151         reservoir_samples_ = 0;
152         audio_error_ = false;
153         file_info_.is_playing = true;
154         file_info_.eof = false;
155
156         if (flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
157                 audio_error_ = true;
158                 if(decoder_ && decoder_->state != FLAC__FILE_DECODER_UNINITIALIZED)
159                         FLAC__file_decoder_finish(decoder_);
160                 return;
161         }
162
163         (void)get_id3v1_tag_(filename, &tag);
164         flac_ip.set_info(tag.description, file_info_.length_in_msec, file_info_.sample_rate * file_info_.channels * file_info_.bits_per_sample, file_info_.sample_rate, file_info_.channels);
165
166         file_info_.seek_to_in_sec = -1;
167         pthread_create(&decode_thread_, NULL, play_loop_, NULL);
168 }
169
170 void FLAC_XMMS__stop()
171 {
172         if(file_info_.is_playing) {
173                 file_info_.is_playing = false;
174                 pthread_join(decode_thread_, NULL);
175                 flac_ip.output->close_audio();
176                 if(decoder_ && decoder_->state != FLAC__FILE_DECODER_UNINITIALIZED)
177                         FLAC__file_decoder_finish(decoder_);
178         }
179 }
180
181 void FLAC_XMMS__pause(short p)
182 {
183         flac_ip.output->pause(p);
184 }
185
186 void FLAC_XMMS__seek(int time)
187 {
188         file_info_.seek_to_in_sec = time;
189         file_info_.eof = false;
190
191         while(file_info_.seek_to_in_sec != -1)
192                 xmms_usleep(10000);
193 }
194
195 int FLAC_XMMS__get_time()
196 {
197         if(audio_error_)
198                 return -2;
199         if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
200                 return -1;
201         else
202                 return flac_ip.output->output_time();
203 }
204
205 void FLAC_XMMS__cleanup()
206 {
207         if(decoder_)
208                 FLAC__file_decoder_free_instance(decoder_);
209 }
210
211 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
212 {
213         id3v1_struct tag;
214
215         if(title) {
216                 (void)get_id3v1_tag_(filename, &tag);
217                 *title = g_malloc(strlen(tag.description)+1);
218                 strcpy(*title, tag.description);
219         }
220         if(length_in_msec) {
221                 FLAC__FileDecoder *tmp_decoder = FLAC__file_decoder_get_new_instance();
222                 file_info_struct tmp_file_info;
223                 if(0 == tmp_decoder) {
224                         *length_in_msec = -1;
225                         return;
226                 }
227                 tmp_file_info.abort_flag = false;
228                 tmp_decoder->check_md5 = false; /* turn off MD5 checking in the decoder */
229                 if(FLAC__file_decoder_init(tmp_decoder, filename, write_callback_, metadata_callback_, error_callback_, &tmp_file_info) != FLAC__FILE_DECODER_OK) {
230                         *length_in_msec = -1;
231                         return;
232                 }
233                 if(!FLAC__file_decoder_process_metadata(tmp_decoder)) {
234                         *length_in_msec = -1;
235                         return;
236                 }
237
238                 *length_in_msec = (int)tmp_file_info.length_in_msec;
239
240                 if(tmp_decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
241                         FLAC__file_decoder_finish(tmp_decoder);
242                 FLAC__file_decoder_free_instance(tmp_decoder);
243         }
244 }
245
246 /***********************************************************************
247  * local routines
248  **********************************************************************/
249
250 bool get_id3v1_tag_(const char *filename, id3v1_struct *tag)
251 {
252         const char *temp;
253         FILE *f = fopen(filename, "rb");
254         memset(tag, 0, sizeof(id3v1_struct));
255
256         /* set the title and description to the filename by default */
257         temp = strrchr(filename, '/');
258         if(!temp)
259                 temp = filename;
260         else
261                 temp++;
262         strcpy(tag->description, temp);
263         *strrchr(tag->description, '.') = '\0';
264         strncpy(tag->title, tag->description, 30); tag->title[30] = '\0';
265
266         if(0 == f)
267                 return false;
268         if(-1 == fseek(f, -128, SEEK_END)) {
269                 fclose(f);
270                 return false;
271         }
272         if(fread(tag->raw, 1, 128, f) < 128) {
273                 fclose(f);
274                 return false;
275         }
276         fclose(f);
277         if(strncmp(tag->raw, "TAG", 3))
278                 return false;
279         else {
280                 char year_str[5];
281
282                 memcpy(tag->title, tag->raw+3, 30);
283                 memcpy(tag->artist, tag->raw+33, 30);
284                 memcpy(tag->album, tag->raw+63, 30);
285                 memcpy(year_str, tag->raw+93, 4); year_str[4] = '\0'; tag->year = atoi(year_str);
286                 memcpy(tag->comment, tag->raw+97, 30);
287                 tag->genre = (unsigned)((byte)tag->raw[127]);
288                 tag->track = (unsigned)((byte)tag->raw[126]);
289
290                 sprintf(tag->description, "%s - %s", tag->artist, tag->title);
291
292                 return true;
293         }
294 }
295
296 #ifndef RESERVOIR_TEST
297 void *play_loop_(void *arg)
298 {
299
300         (void)arg;
301
302         while(file_info_.is_playing) {
303                 if(!file_info_.eof) {
304                         (void)FLAC__file_decoder_process_one_frame(decoder_);
305                         if(reservoir_samples_ > 0) {
306                                 unsigned bytes = reservoir_samples_ * ((file_info_.bits_per_sample+7)/8) * file_info_.channels;
307                                 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, file_info_.channels, bytes, reservoir_);
308                                 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
309                                         xmms_usleep(10000);
310                                 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
311                                         flac_ip.output->write_audio(reservoir_, bytes);
312                                 reservoir_samples_ = 0;
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_, (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                         }
329                 }
330
331         }
332         if(decoder_ && decoder_->state != FLAC__FILE_DECODER_UNINITIALIZED)
333                 FLAC__file_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 #else
343 void *play_loop_(void *arg)
344 {
345
346         (void)arg;
347
348         while(file_info_.is_playing) {
349                 if(!file_info_.eof) {
350                         while(reservoir_samples_ < SAMPLES_PER_WRITE) {
351                                 if(decoder_->state == FLAC__FILE_DECODER_END_OF_FILE) {
352                                         file_info_.eof = true;
353                                         break;
354                                 }
355                                 else if(!FLAC__file_decoder_process_one_frame(decoder_))
356                                         break;
357                         }
358                         if(reservoir_samples_ > 0) {
359                                 const unsigned channels = file_info_.channels;
360                                 const unsigned bytes_per_sample = (file_info_.bits_per_sample+7)/8;
361                                 unsigned i, n = min(reservoir_samples_, SAMPLES_PER_WRITE), delta;
362                                 unsigned bytes = n * bytes_per_sample * channels;
363
364                                 for(i = 0; i < bytes; i++)
365                                         output_[i] = reservoir_[i];
366                                 delta = i;
367                                 for( ; i < reservoir_samples_*bytes_per_sample*channels; i++)
368                                         reservoir_[i-delta] = reservoir_[i];
369                                 reservoir_samples_ -= n;
370
371                                 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, channels, bytes, output_);
372                                 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
373                                         xmms_usleep(10000);
374                                 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
375                                         flac_ip.output->write_audio(output_, bytes);
376                         }
377                         else {
378                                 file_info_.eof = true;
379                                 xmms_usleep(10000);
380                         }
381                 }
382                 else
383                         xmms_usleep(10000);
384                 if (file_info_.seek_to_in_sec != -1) {
385                         const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
386                         unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
387                         if(FLAC__file_decoder_seek_absolute(decoder_, (uint64)target_sample)) {
388                                 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
389                                 file_info_.seek_to_in_sec = -1;
390                                 file_info_.eof = false;
391                                 reservoir_samples_ = 0;
392                         }
393                 }
394
395         }
396         if(decoder_ && decoder_->state != FLAC__FILE_DECODER_UNINITIALIZED)
397                 FLAC__file_decoder_finish(decoder_);
398
399         /* are these two calls necessary? */
400         flac_ip.output->buffer_free();
401         flac_ip.output->buffer_free();
402
403         pthread_exit(NULL);
404         return 0; /* to silence the compiler warning about not returning a value */
405 }
406 #endif
407
408 bool decoder_init_(const char *filename)
409 {
410         if(decoder_ == 0)
411                 return false;
412
413         decoder_->check_md5 = false; /* turn off MD5 checking in the decoder */
414
415         if(FLAC__file_decoder_init(decoder_, filename, write_callback_, metadata_callback_, error_callback_, &file_info_) != FLAC__FILE_DECODER_OK)
416                 return false;
417
418         file_info_.abort_flag = false;
419
420         if(!FLAC__file_decoder_process_metadata(decoder_))
421                 return false;
422
423         return true;
424 }
425
426 #ifndef RESERVOIR_TEST
427 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
428 {
429         file_info_struct *file_info = (file_info_struct *)client_data;
430         const unsigned bps = file_info->bits_per_sample, channels = file_info->channels, wide_samples = frame->header.blocksize;
431         unsigned wide_sample, sample, channel;
432         int8 *scbuffer = (int8*)reservoir_;
433         int16 *ssbuffer = (int16*)reservoir_;
434
435         (void)decoder;
436
437         if(file_info->abort_flag)
438                 return FLAC__STREAM_DECODER_WRITE_ABORT;
439
440         if(bps == 8) {
441                 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
442                         for(channel = 0; channel < channels; channel++, sample++)
443                                 scbuffer[sample] = (int8)buffer[channel][wide_sample];
444         }
445         else if(bps == 16) {
446                 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
447                         for(channel = 0; channel < channels; channel++, sample++)
448                                 ssbuffer[sample] = (int16)buffer[channel][wide_sample];
449         }
450         else {
451                 file_info->abort_flag = true;
452                 return FLAC__STREAM_DECODER_WRITE_ABORT;
453         }
454
455         reservoir_samples_ = wide_samples;
456
457         return FLAC__STREAM_DECODER_WRITE_CONTINUE;
458 }
459 #else
460 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
461 {
462         file_info_struct *file_info = (file_info_struct *)client_data;
463         const unsigned bps = file_info->bits_per_sample, channels = file_info->channels, wide_samples = frame->header.blocksize;
464         unsigned wide_sample, sample, channel;
465         int8 *scbuffer = (int8*)reservoir_;
466         int16 *ssbuffer = (int16*)reservoir_;
467
468         (void)decoder;
469
470         if(file_info->abort_flag)
471                 return FLAC__STREAM_DECODER_WRITE_ABORT;
472
473         if(bps == 8) {
474                 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
475                         for(channel = 0; channel < channels; channel++, sample++)
476                                 scbuffer[sample] = (int8)buffer[channel][wide_sample];
477         }
478         else if(bps == 16) {
479                 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
480                         for(channel = 0; channel < channels; channel++, sample++)
481                                 ssbuffer[sample] = (int16)buffer[channel][wide_sample];
482         }
483         else {
484                 file_info->abort_flag = true;
485                 return FLAC__STREAM_DECODER_WRITE_ABORT;
486         }
487
488         reservoir_samples_ += wide_samples;
489
490         return FLAC__STREAM_DECODER_WRITE_CONTINUE;
491 }
492 #endif
493
494 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
495 {
496         file_info_struct *file_info = (file_info_struct *)client_data;
497         (void)decoder;
498         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
499                 assert(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
500                 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
501                 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
502                 file_info->channels = metadata->data.stream_info.channels;
503                 file_info->sample_rate = metadata->data.stream_info.sample_rate;
504
505                 if(file_info->bits_per_sample == 8) {
506                         file_info->sample_format = FMT_S8;
507                 }
508                 else if(file_info->bits_per_sample == 16) {
509                         file_info->sample_format = FMT_S16_NE;
510                 }
511                 else {
512                         file_info->abort_flag = true;
513                         return;
514                 }
515                 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
516         }
517 }
518
519 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
520 {
521         file_info_struct *file_info = (file_info_struct *)client_data;
522         (void)decoder;
523         if(status != FLAC__STREAM_DECODER_ERROR_LOST_SYNC)
524                 file_info->abort_flag = true;
525 }