1 /* libxmms-flac - XMMS FLAC input plugin
2 * Copyright (C) 2000,2001 Josh Coalson
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.
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.
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.
26 #include "xmms/plugin.h"
27 #include "xmms/util.h"
37 unsigned track; /* may be 0 if v1 (not v1.1) tag */
39 char description[1024]; /* the formatted description passed to xmms */
46 unsigned total_samples;
47 unsigned bits_per_sample;
50 unsigned length_in_msec;
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);
64 static void *play_loop_(void *arg);
65 static bool decoder_init_(const char *filename);
66 static bool get_id3v1_tag_(const char *filename, id3v1_struct *tag);
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);
76 "FLAC Player v" FLAC__VERSION_STRING,
80 FLAC_XMMS__is_our_file,
95 FLAC_XMMS__get_song_info,
96 NULL, /* file_info_box */
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 */
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;
110 InputPlugin *get_iplugin_info()
112 flac_ip.description = g_strdup_printf("FLAC Player v%s", FLAC__VERSION_STRING);
116 void FLAC_XMMS__init()
118 decoder_ = FLAC__file_decoder_get_new_instance();
121 int FLAC_XMMS__is_our_file(char *filename)
125 ext = strrchr(filename, '.');
127 if (!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
132 void FLAC_XMMS__play_file(char *filename)
137 if(0 == (f = fopen(filename, "r")))
141 if(!decoder_init_(filename))
144 reservoir_samples_ = 0;
145 audio_error_ = false;
146 file_info_.is_playing = true;
147 file_info_.eof = false;
149 if (flac_ip.output->open_audio(FMT_S16_NE, file_info_.sample_rate, file_info_.channels) == 0) {
151 if(decoder_ && decoder_->state != FLAC__FILE_DECODER_UNINITIALIZED)
152 FLAC__file_decoder_finish(decoder_);
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);
159 file_info_.seek_to_in_sec = -1;
160 pthread_create(&decode_thread_, NULL, play_loop_, NULL);
163 void FLAC_XMMS__stop()
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_);
174 void FLAC_XMMS__pause(short p)
176 flac_ip.output->pause(p);
179 void FLAC_XMMS__seek(int time)
181 file_info_.seek_to_in_sec = time;
182 file_info_.eof = false;
184 while(file_info_.seek_to_in_sec != -1)
188 int FLAC_XMMS__get_time()
192 if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
195 return flac_ip.output->output_time();
198 void FLAC_XMMS__cleanup()
201 FLAC__file_decoder_free_instance(decoder_);
204 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
209 (void)get_id3v1_tag_(filename, &tag);
210 *title = g_malloc(strlen(tag.description)+1);
211 strcpy(*title, tag.description);
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;
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;
226 if(!FLAC__file_decoder_process_metadata(tmp_decoder)) {
227 *length_in_msec = -1;
231 *length_in_msec = (int)tmp_file_info.length_in_msec;
233 if(tmp_decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
234 FLAC__file_decoder_finish(tmp_decoder);
235 FLAC__file_decoder_free_instance(tmp_decoder);
239 /***********************************************************************
241 **********************************************************************/
243 #ifndef RESERVOIR_TEST
244 void *play_loop_(void *arg)
249 while(file_info_.is_playing) {
250 if(!file_info_.eof) {
251 (void)FLAC__file_decoder_process_one_frame(decoder_);
252 if(reservoir_samples_ > 0) {
253 unsigned bytes = reservoir_samples_ * ((file_info_.bits_per_sample+7)/8) * file_info_.channels;
254 flac_ip.add_vis_pcm(flac_ip.output->written_time(), FMT_S16_NE, file_info_.channels, bytes, (char*)reservoir_);
255 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
257 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
258 flac_ip.output->write_audio((char*)reservoir_, bytes);
259 reservoir_samples_ = 0;
262 file_info_.eof = true;
268 if (file_info_.seek_to_in_sec != -1) {
269 const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
270 unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
271 if(FLAC__file_decoder_seek_absolute(decoder_, (uint64)target_sample)) {
272 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
273 file_info_.seek_to_in_sec = -1;
274 file_info_.eof = false;
279 if(decoder_ && decoder_->state != FLAC__FILE_DECODER_UNINITIALIZED)
280 FLAC__file_decoder_finish(decoder_);
282 /* are these two calls necessary? */
283 flac_ip.output->buffer_free();
284 flac_ip.output->buffer_free();
287 return 0; /* to silence the compiler warning about not returning a value */
290 void *play_loop_(void *arg)
295 while(file_info_.is_playing) {
296 if(!file_info_.eof) {
297 while(samples_in_reservoir < 576) {
298 if(decoder->state == FLAC__FILE_DECODER_END_OF_FILE) {
299 file_info_.eof = true;
302 else if(!FLAC__file_decoder_process_one_frame(decoder_))
305 if(reservoir_samples_ > 0) {
306 unsigned i, n = min(samples_in_reservoir, 576), delta;
307 unsigned bytes = reservoir_samples_ * ((file_info_.bits_per_sample+7)/8) * file_info_.channels;
308 const unsigned channels = file_info_.channels;
310 for(i = 0; i < n*channels; i++)
311 output_[i] = reservoir_[i];
313 for( ; i < reservoir_samples_*channels; i++)
314 reservoir[i-delta] = reservoir[i];
315 reservoir_samples_ -= n;
317 flac_ip.add_vis_pcm(flac_ip.output->written_time(), FMT_S16_NE, channels, bytes, (char*)output_);
318 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
320 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
321 flac_ip.output->write_audio((char*)output_, bytes);
324 file_info_.eof = true;
330 if (file_info_.seek_to_in_sec != -1) {
331 const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
332 unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
333 if(FLAC__file_decoder_seek_absolute(decoder_, (uint64)target_sample)) {
334 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
335 file_info_.seek_to_in_sec = -1;
336 file_info_.eof = false;
337 reservoir_samples_ = 0;
342 if(decoder_ && decoder_->state != FLAC__FILE_DECODER_UNINITIALIZED)
343 FLAC__file_decoder_finish(decoder_);
345 /* are these two calls necessary? */
346 flac_ip.output->buffer_free();
347 flac_ip.output->buffer_free();
350 return 0; /* to silence the compiler warning about not returning a value */
354 bool decoder_init_(const char *filename)
359 decoder_->check_md5 = false; /* turn off MD5 checking in the decoder */
361 if(FLAC__file_decoder_init(decoder_, filename, write_callback_, metadata_callback_, error_callback_, &file_info_) != FLAC__FILE_DECODER_OK)
364 file_info_.abort_flag = false;
366 if(!FLAC__file_decoder_process_metadata(decoder_))
372 bool get_id3v1_tag_(const char *filename, id3v1_struct *tag)
375 FILE *f = fopen(filename, "rb");
376 memset(tag, 0, sizeof(id3v1_struct));
378 /* set the description to the filename by default */
379 temp = strrchr(filename, '/');
384 strcpy(tag->description, temp);
385 *strrchr(tag->description, '.') = '\0';
389 if(-1 == fseek(f, -128, SEEK_END)) {
393 if(fread(tag->raw, 1, 128, f) < 128) {
398 if(strncmp(tag->raw, "TAG", 3))
403 memcpy(tag->title, tag->raw+3, 30);
404 memcpy(tag->artist, tag->raw+33, 30);
405 memcpy(tag->album, tag->raw+63, 30);
406 memcpy(year_str, tag->raw+93, 4); year_str[4] = '\0'; tag->year = atoi(year_str);
407 memcpy(tag->comment, tag->raw+97, 30);
408 tag->genre = (unsigned)((byte)tag->raw[127]);
409 tag->track = (unsigned)((byte)tag->raw[126]);
411 sprintf(tag->description, "%s - %s", tag->artist, tag->title);
417 #ifndef RESERVOIR_TEST
418 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
420 file_info_struct *file_info = (file_info_struct *)client_data;
421 const unsigned bps = file_info->bits_per_sample, channels = file_info->channels, wide_samples = frame->header.blocksize;
422 unsigned wide_sample, sample, channel;
426 if(file_info->abort_flag)
427 return FLAC__STREAM_DECODER_WRITE_ABORT;
431 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
432 for(channel = 0; channel < channels; channel++, sample++)
433 reservoir_[sample] = (int16)buffer[channel][wide_sample];
435 reservoir_samples_ = wide_samples;
437 return FLAC__STREAM_DECODER_WRITE_CONTINUE;
440 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
442 file_info_struct *file_info = (file_info_struct *)client_data;
443 const unsigned bps = file_info->bits_per_sample, channels = file_info->channels, wide_samples = frame->header.blocksize;
444 unsigned wide_sample, sample, channel;
448 if(file_info->abort_flag)
449 return FLAC__STREAM_DECODER_WRITE_ABORT;
453 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
454 for(channel = 0; channel < channels; channel++, sample++)
455 reservoir_[sample] = (int16)buffer[channel][wide_sample];
457 reservoir_samples_ += wide_samples;
459 return FLAC__STREAM_DECODER_WRITE_CONTINUE;
463 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
465 file_info_struct *file_info = (file_info_struct *)client_data;
467 if(metadata->type == FLAC__METADATA_TYPE_ENCODING) {
468 assert(metadata->data.encoding.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
469 file_info->total_samples = (unsigned)(metadata->data.encoding.total_samples&0xffffffff);
470 file_info->bits_per_sample = metadata->data.encoding.bits_per_sample;
471 file_info->channels = metadata->data.encoding.channels;
472 file_info->sample_rate = metadata->data.encoding.sample_rate;
474 if(file_info->bits_per_sample != 16) {
475 file_info->abort_flag = true;
478 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
482 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
484 file_info_struct *file_info = (file_info_struct *)client_data;
486 if(status != FLAC__STREAM_DECODER_ERROR_LOST_SYNC)
487 file_info->abort_flag = true;