1 /* libxmms-flac - XMMS FLAC input plugin
2 * Copyright (C) 2000,2001,2002 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 safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder);
73 static void safe_decoder_finish_(FLAC__FileDecoder *decoder);
74 static void safe_decoder_delete_(FLAC__FileDecoder *decoder);
75 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
76 static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
77 static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
84 "FLAC Player v" FLAC__VERSION_STRING,
88 FLAC_XMMS__is_our_file,
103 FLAC_XMMS__get_song_info,
104 NULL, /* file_info_box */
108 #define SAMPLES_PER_WRITE 512
109 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 */
110 static FLAC__byte output_[FLAC__MAX_BLOCK_SIZE * 2 * 2]; /* *2 for max bytes-per-sample, *2 for max channels */
111 static unsigned reservoir_samples_ = 0;
112 static FLAC__FileDecoder *decoder_ = 0;
113 static file_info_struct file_info_;
114 static pthread_t decode_thread_;
115 static FLAC__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_new();
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 reservoir_samples_ = 0;
145 audio_error_ = false;
146 file_info_.abort_flag = false;
147 file_info_.is_playing = false;
148 file_info_.eof = false;
149 file_info_.play_thread_open = false;
151 if(0 == (f = fopen(filename, "r")))
158 if(!safe_decoder_init_(filename, decoder_))
161 file_info_.is_playing = true;
163 if(flac_ip.output->open_audio(file_info_.sample_format, file_info_.sample_rate, file_info_.channels) == 0) {
165 safe_decoder_finish_(decoder_);
169 (void)get_id3v1_tag_(filename, &tag);
170 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);
172 file_info_.seek_to_in_sec = -1;
173 file_info_.play_thread_open = true;
174 pthread_create(&decode_thread_, NULL, play_loop_, NULL);
177 void FLAC_XMMS__stop()
179 if(file_info_.is_playing) {
180 file_info_.is_playing = false;
181 if(file_info_.play_thread_open) {
182 file_info_.play_thread_open = false;
183 pthread_join(decode_thread_, NULL);
185 flac_ip.output->close_audio();
186 safe_decoder_finish_(decoder_);
190 void FLAC_XMMS__pause(short p)
192 flac_ip.output->pause(p);
195 void FLAC_XMMS__seek(int time)
197 file_info_.seek_to_in_sec = time;
198 file_info_.eof = false;
200 while(file_info_.seek_to_in_sec != -1)
204 int FLAC_XMMS__get_time()
208 if(!file_info_.is_playing || (file_info_.eof && !flac_ip.output->buffer_playing()))
211 return flac_ip.output->output_time();
214 void FLAC_XMMS__cleanup()
216 safe_decoder_delete_(decoder_);
220 void FLAC_XMMS__get_song_info(char *filename, char **title, int *length_in_msec)
223 FLAC__StreamMetadata streaminfo;
228 if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
229 /* @@@ how to report the error? */
231 static const char *errtitle = "Invalid FLAC File: ";
232 *title = g_malloc(strlen(errtitle) + 1 + strlen(filename) + 1 + 1);
233 sprintf(*title, "%s\"%s\"", errtitle, filename);
236 *length_in_msec = -1;
241 (void)get_id3v1_tag_(filename, &tag);
242 *title = g_malloc(strlen(tag.description)+1);
243 strcpy(*title, tag.description);
246 *length_in_msec = streaminfo.data.stream_info.total_samples * 10 / (streaminfo.data.stream_info.sample_rate / 100);
249 /***********************************************************************
251 **********************************************************************/
253 FLAC__bool get_id3v1_tag_(const char *filename, id3v1_struct *tag)
256 FILE *f = fopen(filename, "rb");
257 memset(tag, 0, sizeof(id3v1_struct));
259 /* set the title and description to the filename by default */
260 temp = strrchr(filename, '/');
265 strcpy(tag->description, temp);
266 *strrchr(tag->description, '.') = '\0';
267 strncpy(tag->title, tag->description, 30); tag->title[30] = '\0';
271 if(-1 == fseek(f, -128, SEEK_END)) {
275 if(fread(tag->raw, 1, 128, f) < 128) {
280 if(strncmp(tag->raw, "TAG", 3))
285 memcpy(tag->title, tag->raw+3, 30);
286 memcpy(tag->artist, tag->raw+33, 30);
287 memcpy(tag->album, tag->raw+63, 30);
288 memcpy(year_str, tag->raw+93, 4); year_str[4] = '\0'; tag->year = atoi(year_str);
289 memcpy(tag->comment, tag->raw+97, 30);
290 tag->genre = (unsigned)((FLAC__byte)tag->raw[127]);
291 tag->track = (unsigned)((FLAC__byte)tag->raw[126]);
293 sprintf(tag->description, "%s - %s", tag->artist, tag->title);
299 void *play_loop_(void *arg)
303 while(file_info_.is_playing) {
304 if(!file_info_.eof) {
305 while(reservoir_samples_ < SAMPLES_PER_WRITE) {
306 if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
307 file_info_.eof = true;
310 else if(!FLAC__file_decoder_process_one_frame(decoder_)) {
311 /*@@@ this should probably be a dialog */
312 fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n");
313 file_info_.eof = true;
317 if(reservoir_samples_ > 0) {
318 const unsigned channels = file_info_.channels;
319 const unsigned bytes_per_sample = (file_info_.bits_per_sample+7)/8;
320 unsigned i, n = min(reservoir_samples_, SAMPLES_PER_WRITE), delta;
321 unsigned bytes = n * bytes_per_sample * channels;
323 for(i = 0; i < bytes; i++)
324 output_[i] = reservoir_[i];
326 for( ; i < reservoir_samples_*bytes_per_sample*channels; i++)
327 reservoir_[i-delta] = reservoir_[i];
328 reservoir_samples_ -= n;
330 flac_ip.add_vis_pcm(flac_ip.output->written_time(), file_info_.sample_format, channels, bytes, output_);
331 while(flac_ip.output->buffer_free() < (int)bytes && file_info_.is_playing && file_info_.seek_to_in_sec == -1)
333 if(file_info_.is_playing && file_info_.seek_to_in_sec == -1)
334 flac_ip.output->write_audio(output_, bytes);
337 file_info_.eof = true;
343 if(file_info_.seek_to_in_sec != -1) {
344 const double distance = (double)file_info_.seek_to_in_sec * 1000.0 / (double)file_info_.length_in_msec;
345 unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
346 if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
347 flac_ip.output->flush(file_info_.seek_to_in_sec * 1000);
348 file_info_.seek_to_in_sec = -1;
349 file_info_.eof = false;
350 reservoir_samples_ = 0;
355 safe_decoder_finish_(decoder_);
357 /* are these two calls necessary? */
358 flac_ip.output->buffer_free();
359 flac_ip.output->buffer_free();
362 return 0; /* to silence the compiler warning about not returning a value */
365 FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder)
370 safe_decoder_finish_(decoder);
372 FLAC__file_decoder_set_md5_checking(decoder, false);
373 FLAC__file_decoder_set_filename(decoder, filename);
374 FLAC__file_decoder_set_write_callback(decoder, write_callback_);
375 FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
376 FLAC__file_decoder_set_error_callback(decoder, error_callback_);
377 FLAC__file_decoder_set_client_data(decoder, &file_info_);
378 if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK)
381 if(!FLAC__file_decoder_process_metadata(decoder))
387 void safe_decoder_finish_(FLAC__FileDecoder *decoder)
389 if(decoder && FLAC__file_decoder_get_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
390 FLAC__file_decoder_finish(decoder);
393 void safe_decoder_delete_(FLAC__FileDecoder *decoder)
396 safe_decoder_finish_(decoder);
397 FLAC__file_decoder_delete(decoder);
401 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
403 file_info_struct *file_info = (file_info_struct *)client_data;
404 const unsigned bps = file_info->bits_per_sample, channels = file_info->channels, wide_samples = frame->header.blocksize;
405 unsigned wide_sample, sample, channel;
406 FLAC__int8 *scbuffer = (FLAC__int8*)reservoir_;
407 FLAC__int16 *ssbuffer = (FLAC__int16*)reservoir_;
411 if(file_info->abort_flag)
412 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
415 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
416 for(channel = 0; channel < channels; channel++, sample++)
417 scbuffer[sample] = (FLAC__int8)buffer[channel][wide_sample];
420 for(sample = reservoir_samples_*channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
421 for(channel = 0; channel < channels; channel++, sample++)
422 ssbuffer[sample] = (FLAC__int16)buffer[channel][wide_sample];
425 file_info->abort_flag = true;
426 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
429 reservoir_samples_ += wide_samples;
431 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
434 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
436 file_info_struct *file_info = (file_info_struct *)client_data;
438 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
439 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
440 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
441 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
442 file_info->channels = metadata->data.stream_info.channels;
443 file_info->sample_rate = metadata->data.stream_info.sample_rate;
445 if(file_info->bits_per_sample == 8) {
446 file_info->sample_format = FMT_S8;
448 else if(file_info->bits_per_sample == 16) {
449 file_info->sample_format = FMT_S16_NE;
452 file_info->abort_flag = true;
455 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
459 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
461 file_info_struct *file_info = (file_info_struct *)client_data;
463 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
464 file_info->abort_flag = true;