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.
25 #include "xmms/plugin.h"
26 #include "xmms/util.h"
32 #define min(x,y) ((x)<(y)?(x):(y))
41 unsigned track; /* may be 0 if v1 (not v1.1) tag */
43 char description[1024]; /* the formatted description passed to xmms */
47 FLAC__bool abort_flag;
48 FLAC__bool is_playing;
50 FLAC__bool play_thread_open; /* if true, is_playing must also be true */
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 FLAC__bool get_id3v1_tag_(const char *filename, id3v1_struct *tag);
71 static void *play_loop_(void *arg);
72 static FLAC__bool decoder_init_(const char *filename);
73 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__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 #define SAMPLES_PER_WRITE 512
107 static FLAC__byte reservoir_[FLAC__MAX_BLOCK_SIZE * 2 * 2 * 2]; /* *2 for max bytes-per-sample, *2 for max channels, another *2 for overflow */
108 static FLAC__byte output_[FLAC__MAX_BLOCK_SIZE * 2 * 2]; /* *2 for max bytes-per-sample, *2 for max channels */
109 static unsigned reservoir_samples_ = 0;
110 static FLAC__FileDecoder *decoder_ = 0;
111 static file_info_struct file_info_;
112 static pthread_t decode_thread_;
113 static FLAC__bool audio_error_ = false;
115 InputPlugin *get_iplugin_info()
117 flac_ip.description = g_strdup_printf("FLAC Player v%s", FLAC__VERSION_STRING);
121 void FLAC_XMMS__init()
123 decoder_ = FLAC__file_decoder_new();
126 int FLAC_XMMS__is_our_file(char *filename)
130 ext = strrchr(filename, '.');
132 if(!strcasecmp(ext, ".flac") || !strcasecmp(ext, ".fla"))
137 void FLAC_XMMS__play_file(char *filename)
142 reservoir_samples_ = 0;
143 audio_error_ = false;
144 file_info_.abort_flag = false;
145 file_info_.is_playing = false;
146 file_info_.eof = false;
147 file_info_.play_thread_open = false;
149 if(0 == (f = fopen(filename, "r")))
153 if(!decoder_init_(filename))
156 file_info_.is_playing = true;
158 if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
161 FLAC__file_decoder_finish(decoder_);
165 (void)get_id3v1_tag_(filename, &tag);
166 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);
168 file_info_.seek_to_in_sec = -1;
169 file_info_.play_thread_open = true;
170 pthread_create(&decode_thread_, NULL, play_loop_, NULL);
173 void FLAC_XMMS__stop()
175 if(file_info_.is_playing) {
176 file_info_.is_playing = false;
177 if(file_info_.play_thread_open) {
178 file_info_.play_thread_open = false;
179 pthread_join(decode_thread_, NULL);
181 flac_ip.output->close_audio();
183 FLAC__file_decoder_finish(decoder_);
187 void FLAC_XMMS__pause(short p)
189 flac_ip.output->pause(p);
192 void FLAC_XMMS__seek(int time)
194 file_info_.seek_to_in_sec = time;
195 file_info_.eof = false;
197 while(file_info_.seek_to_in_sec != -1)
201 int FLAC_XMMS__get_time()
205 if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
208 return flac_ip.output->output_time();
211 void FLAC_XMMS__cleanup()
214 FLAC__file_decoder_delete(decoder_);
219 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
224 (void)get_id3v1_tag_(filename, &tag);
225 *title = g_malloc(strlen(tag.description)+1);
226 strcpy(*title, tag.description);
229 FLAC__FileDecoder *tmp_decoder = FLAC__file_decoder_new();
230 file_info_struct tmp_file_info;
231 if(0 == tmp_decoder) {
232 *length_in_msec = -1;
235 tmp_file_info.abort_flag = false;
236 FLAC__file_decoder_set_md5_checking(tmp_decoder, false);
237 FLAC__file_decoder_set_filename(tmp_decoder, filename);
238 FLAC__file_decoder_set_write_callback(tmp_decoder, write_callback_);
239 FLAC__file_decoder_set_metadata_callback(tmp_decoder, metadata_callback_);
240 FLAC__file_decoder_set_error_callback(tmp_decoder, error_callback_);
241 FLAC__file_decoder_set_client_data(tmp_decoder, &tmp_file_info);
242 if(FLAC__file_decoder_init(tmp_decoder) != FLAC__FILE_DECODER_OK) {
243 *length_in_msec = -1;
246 if(!FLAC__file_decoder_process_metadata(tmp_decoder)) {
247 *length_in_msec = -1;
251 *length_in_msec = (int)tmp_file_info.length_in_msec;
253 FLAC__file_decoder_finish(tmp_decoder);
254 FLAC__file_decoder_delete(tmp_decoder);
258 /***********************************************************************
260 **********************************************************************/
262 FLAC__bool get_id3v1_tag_(const char *filename, id3v1_struct *tag)
265 FILE *f = fopen(filename, "rb");
266 memset(tag, 0, sizeof(id3v1_struct));
268 /* set the title and description to the filename by default */
269 temp = strrchr(filename, '/');
274 strcpy(tag->description, temp);
275 *strrchr(tag->description, '.') = '\0';
276 strncpy(tag->title, tag->description, 30); tag->title[30] = '\0';
280 if(-1 == fseek(f, -128, SEEK_END)) {
284 if(fread(tag->raw, 1, 128, f) < 128) {
289 if(strncmp(tag->raw, "TAG", 3))
294 memcpy(tag->title, tag->raw+3, 30);
295 memcpy(tag->artist, tag->raw+33, 30);
296 memcpy(tag->album, tag->raw+63, 30);
297 memcpy(year_str, tag->raw+93, 4); year_str[4] = '\0'; tag->year = atoi(year_str);
298 memcpy(tag->comment, tag->raw+97, 30);
299 tag->genre = (unsigned)((FLAC__byte)tag->raw[127]);
300 tag->track = (unsigned)((FLAC__byte)tag->raw[126]);
302 sprintf(tag->description, "%s - %s", tag->artist, tag->title);
308 void *play_loop_(void *arg)
312 while(file_info_.is_playing) {
313 if(!file_info_.eof) {
314 while(reservoir_samples_ < SAMPLES_PER_WRITE) {
315 if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
316 file_info_.eof = true;
319 else if(!FLAC__file_decoder_process_one_frame(decoder_))
322 if(reservoir_samples_ > 0) {
323 const unsigned channels = file_info_.channels;
324 const unsigned bytes_per_sample = (file_info_.bits_per_sample+7)/8;
325 unsigned i, n = min(reservoir_samples_, SAMPLES_PER_WRITE), delta;
326 unsigned bytes = n * bytes_per_sample * channels;
328 for(i = 0; i < bytes; i++)
329 output_[i] = reservoir_[i];
331 for( ; i < reservoir_samples_*bytes_per_sample*channels; i++)
332 reservoir_[i-delta] = reservoir_[i];
333 reservoir_samples_ -= n;
335 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, channels, bytes, output_);
336 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
338 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
339 flac_ip.output->write_audio(output_, bytes);
342 file_info_.eof = true;
348 if(file_info_.seek_to_in_sec != -1) {
349 const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
350 unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
351 if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
352 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
353 file_info_.seek_to_in_sec = -1;
354 file_info_.eof = false;
355 reservoir_samples_ = 0;
361 FLAC__file_decoder_finish(decoder_);
363 /* are these two calls necessary? */
364 flac_ip.output->buffer_free();
365 flac_ip.output->buffer_free();
368 return 0; /* to silence the compiler warning about not returning a value */
371 FLAC__bool decoder_init_(const char *filename)
376 FLAC__file_decoder_set_md5_checking(decoder_, false);
377 FLAC__file_decoder_set_filename(decoder_, filename);
378 FLAC__file_decoder_set_write_callback(decoder_, write_callback_);
379 FLAC__file_decoder_set_metadata_callback(decoder_, metadata_callback_);
380 FLAC__file_decoder_set_error_callback(decoder_, error_callback_);
381 FLAC__file_decoder_set_client_data(decoder_, &file_info_);
382 if(FLAC__file_decoder_init(decoder_) != FLAC__FILE_DECODER_OK)
385 if(!FLAC__file_decoder_process_metadata(decoder_))
391 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)
393 file_info_struct *file_info = (file_info_struct *)client_data;
394 const unsigned bps = file_info->bits_per_sample, channels = file_info->channels, wide_samples = frame->header.blocksize;
395 unsigned wide_sample, sample, channel;
396 FLAC__int8 *scbuffer = (FLAC__int8*)reservoir_;
397 FLAC__int16 *ssbuffer = (FLAC__int16*)reservoir_;
401 if(file_info->abort_flag)
402 return FLAC__STREAM_DECODER_WRITE_ABORT;
405 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
406 for(channel = 0; channel < channels; channel++, sample++)
407 scbuffer[sample] = (FLAC__int8)buffer[channel][wide_sample];
410 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
411 for(channel = 0; channel < channels; channel++, sample++)
412 ssbuffer[sample] = (FLAC__int16)buffer[channel][wide_sample];
415 file_info->abort_flag = true;
416 return FLAC__STREAM_DECODER_WRITE_ABORT;
419 reservoir_samples_ += wide_samples;
421 return FLAC__STREAM_DECODER_WRITE_CONTINUE;
424 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
426 file_info_struct *file_info = (file_info_struct *)client_data;
428 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
429 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
430 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
431 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
432 file_info->channels = metadata->data.stream_info.channels;
433 file_info->sample_rate = metadata->data.stream_info.sample_rate;
435 if(file_info->bits_per_sample == 8) {
436 file_info->sample_format = FMT_S8;
438 else if(file_info->bits_per_sample == 16) {
439 file_info->sample_format = FMT_S16_NE;
442 file_info->abort_flag = true;
445 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
449 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
451 file_info_struct *file_info = (file_info_struct *)client_data;
453 if(status != FLAC__STREAM_DECODER_ERROR_LOST_SYNC)
454 file_info->abort_flag = true;