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