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"
33 #define min(x,y) ((x)<(y)?(x):(y))
42 unsigned track; /* may be 0 if v1 (not v1.1) tag */
44 char description[1024]; /* the formatted description passed to xmms */
51 unsigned total_samples;
52 unsigned bits_per_sample;
55 unsigned length_in_msec;
56 AFormat sample_format;
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);
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);
82 "FLAC Player v" FLAC__VERSION_STRING,
86 FLAC_XMMS__is_our_file,
101 FLAC_XMMS__get_song_info,
102 NULL, /* file_info_box */
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 */
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;
117 InputPlugin *get_iplugin_info()
119 flac_ip.description = g_strdup_printf("FLAC Player v%s", FLAC__VERSION_STRING);
123 void FLAC_XMMS__init()
125 decoder_ = FLAC__file_decoder_get_new_instance();
128 int FLAC_XMMS__is_our_file(char *filename)
132 ext = strrchr(filename, '.');
134 if (!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
139 void FLAC_XMMS__play_file(char *filename)
144 if(0 == (f = fopen(filename, "r")))
148 if(!decoder_init_(filename))
151 reservoir_samples_ = 0;
152 audio_error_ = false;
153 file_info_.is_playing = true;
154 file_info_.eof = false;
156 if (flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
158 if(decoder_ && decoder_->state != FLAC__FILE_DECODER_UNINITIALIZED)
159 FLAC__file_decoder_finish(decoder_);
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);
166 file_info_.seek_to_in_sec = -1;
167 pthread_create(&decode_thread_, NULL, play_loop_, NULL);
170 void FLAC_XMMS__stop()
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_);
181 void FLAC_XMMS__pause(short p)
183 flac_ip.output->pause(p);
186 void FLAC_XMMS__seek(int time)
188 file_info_.seek_to_in_sec = time;
189 file_info_.eof = false;
191 while(file_info_.seek_to_in_sec != -1)
195 int FLAC_XMMS__get_time()
199 if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
202 return flac_ip.output->output_time();
205 void FLAC_XMMS__cleanup()
208 FLAC__file_decoder_free_instance(decoder_);
211 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
216 (void)get_id3v1_tag_(filename, &tag);
217 *title = g_malloc(strlen(tag.description)+1);
218 strcpy(*title, tag.description);
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;
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;
233 if(!FLAC__file_decoder_process_metadata(tmp_decoder)) {
234 *length_in_msec = -1;
238 *length_in_msec = (int)tmp_file_info.length_in_msec;
240 if(tmp_decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
241 FLAC__file_decoder_finish(tmp_decoder);
242 FLAC__file_decoder_free_instance(tmp_decoder);
246 /***********************************************************************
248 **********************************************************************/
250 bool get_id3v1_tag_(const char *filename, id3v1_struct *tag)
253 FILE *f = fopen(filename, "rb");
254 memset(tag, 0, sizeof(id3v1_struct));
256 /* set the title and description to the filename by default */
257 temp = strrchr(filename, '/');
262 strcpy(tag->description, temp);
263 *strrchr(tag->description, '.') = '\0';
264 strncpy(tag->title, tag->description, 30); tag->title[30] = '\0';
268 if(-1 == fseek(f, -128, SEEK_END)) {
272 if(fread(tag->raw, 1, 128, f) < 128) {
277 if(strncmp(tag->raw, "TAG", 3))
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]);
290 sprintf(tag->description, "%s - %s", tag->artist, tag->title);
296 #ifndef RESERVOIR_TEST
297 void *play_loop_(void *arg)
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)
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;
315 file_info_.eof = true;
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;
332 if(decoder_ && decoder_->state != FLAC__FILE_DECODER_UNINITIALIZED)
333 FLAC__file_decoder_finish(decoder_);
335 /* are these two calls necessary? */
336 flac_ip.output->buffer_free();
337 flac_ip.output->buffer_free();
340 return 0; /* to silence the compiler warning about not returning a value */
343 void *play_loop_(void *arg)
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;
355 else if(!FLAC__file_decoder_process_one_frame(decoder_))
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;
364 for(i = 0; i < bytes; i++)
365 output_[i] = reservoir_[i];
367 for( ; i < reservoir_samples_*bytes_per_sample*channels; i++)
368 reservoir_[i-delta] = reservoir_[i];
369 reservoir_samples_ -= n;
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)
374 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
375 flac_ip.output->write_audio(output_, bytes);
378 file_info_.eof = true;
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;
396 if(decoder_ && decoder_->state != FLAC__FILE_DECODER_UNINITIALIZED)
397 FLAC__file_decoder_finish(decoder_);
399 /* are these two calls necessary? */
400 flac_ip.output->buffer_free();
401 flac_ip.output->buffer_free();
404 return 0; /* to silence the compiler warning about not returning a value */
408 bool decoder_init_(const char *filename)
413 decoder_->check_md5 = false; /* turn off MD5 checking in the decoder */
415 if(FLAC__file_decoder_init(decoder_, filename, write_callback_, metadata_callback_, error_callback_, &file_info_) != FLAC__FILE_DECODER_OK)
418 file_info_.abort_flag = false;
420 if(!FLAC__file_decoder_process_metadata(decoder_))
426 #ifndef RESERVOIR_TEST
427 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
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_;
437 if(file_info->abort_flag)
438 return FLAC__STREAM_DECODER_WRITE_ABORT;
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];
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];
451 file_info->abort_flag = true;
452 return FLAC__STREAM_DECODER_WRITE_ABORT;
455 reservoir_samples_ = wide_samples;
457 return FLAC__STREAM_DECODER_WRITE_CONTINUE;
460 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
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_;
470 if(file_info->abort_flag)
471 return FLAC__STREAM_DECODER_WRITE_ABORT;
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];
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];
484 file_info->abort_flag = true;
485 return FLAC__STREAM_DECODER_WRITE_ABORT;
488 reservoir_samples_ += wide_samples;
490 return FLAC__STREAM_DECODER_WRITE_CONTINUE;
494 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
496 file_info_struct *file_info = (file_info_struct *)client_data;
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;
505 if(file_info->bits_per_sample == 8) {
506 file_info->sample_format = FMT_S8;
508 else if(file_info->bits_per_sample == 16) {
509 file_info->sample_format = FMT_S16_NE;
512 file_info->abort_flag = true;
515 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
519 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
521 file_info_struct *file_info = (file_info_struct *)client_data;
523 if(status != FLAC__STREAM_DECODER_ERROR_LOST_SYNC)
524 file_info->abort_flag = true;