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