1 /* in_flac - Winamp2 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.
27 #include "plugin_common/all.h"
32 #define max(a,b) ((a)>(b)?(a):(b))
35 #define FLAC__DO_DITHER
37 BOOL WINAPI _DllMainCRTStartup(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
42 /* post this to the main window at end of file (after playback as stopped) */
43 #define WM_WA_MPEG_EOF WM_USER+2
46 FLAC__bool abort_flag;
47 unsigned total_samples;
48 unsigned bits_per_sample;
51 unsigned length_in_msec;
54 static FLAC__bool safe_decoder_init_(const char *infilename, FLAC__FileDecoder *decoder);
55 static void safe_decoder_finish_(FLAC__FileDecoder *decoder);
56 static void safe_decoder_delete_(FLAC__FileDecoder *decoder);
57 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
58 static void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
59 static void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
60 static void get_description_(const char *filename, char *description, unsigned max_size);
62 In_Module mod_; /* the output module (declared near the bottom of this file) */
63 char lastfn_[MAX_PATH]; /* currently playing file (used for getting info on the current file) */
64 int decode_pos_ms_; /* current decoding position, in milliseconds */
65 int paused_; /* are we paused? */
66 int seek_needed_; /* if != -1, it is the point that the decode thread should seek to, in ms. */
68 #define SAMPLES_PER_WRITE 576
69 FLAC__int32 reservoir_[FLAC__MAX_BLOCK_SIZE * 2/*for overflow*/ * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
70 char sample_buffer_[SAMPLES_PER_WRITE * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8) * 2]; /* (24/8) for max bytes per sample, and 2 for who knows what */
71 unsigned wide_samples_in_reservoir_;
72 static file_info_struct file_info_;
73 static FLAC__FileDecoder *decoder_;
75 int killDecodeThread = 0; /* the kill switch for the decode thread */
76 HANDLE thread_handle = INVALID_HANDLE_VALUE; /* the handle to the decode thread */
78 DWORD WINAPI __stdcall DecodeThread(void *b); /* the decode thread procedure */
81 static void do_vis(char *data, int nch, int resolution, int position, unsigned samples)
83 static char vis_buffer[SAMPLES_PER_WRITE * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
88 * Winamp visuals may have problems accepting sample sizes larger than
89 * 16 bits, so we reduce the sample size here if necessary.
95 size = resolution / 8;
96 count = samples * nch;
101 *ptr++ = data[-1] ^ 0x80;
111 mod_.SAAddPCMData(data, nch, resolution, position);
112 mod_.VSAAddPCMData(data, nch, resolution, position);
116 void config(HWND hwndParent)
118 MessageBox(hwndParent, "No configuration.", "Configuration", MB_OK);
119 /* if we had a configuration we'd want to write it here :) */
121 void about(HWND hwndParent)
123 MessageBox(hwndParent, "Winamp FLAC Plugin v" FLAC__VERSION_STRING ", by Josh Coalson\nSee http://flac.sourceforge.net/", "About FLAC Plugin", MB_OK);
128 decoder_ = FLAC__file_decoder_new();
134 safe_decoder_delete_(decoder_);
138 int isourfile(char *fn) { return 0; }
139 /* used for detecting URL streams.. unused here. strncmp(fn, "http://", 7) to detect HTTP streams, etc */
145 HANDLE input_file = INVALID_HANDLE_VALUE;
146 #ifdef FLAC__DO_DITHER
147 const unsigned output_bits_per_sample = min(file_info_.bits_per_sample, 16);
149 const unsigned output_bits_per_sample = file_info_.bits_per_sample;
156 input_file = CreateFile(fn, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
157 if(input_file == INVALID_HANDLE_VALUE) {
160 CloseHandle(input_file);
162 if(!safe_decoder_init_(fn, decoder_)) {
170 wide_samples_in_reservoir_ = 0;
172 maxlatency = mod_.outMod->Open(file_info_.sample_rate, file_info_.channels, output_bits_per_sample, -1, -1);
173 if(maxlatency < 0) { /* error opening device */
177 /* dividing by 1000 for the first parameter of setinfo makes it */
178 /* display 'H'... for hundred.. i.e. 14H Kbps. */
179 mod_.SetInfo((file_info_.sample_rate*file_info_.bits_per_sample*file_info_.channels)/1000, file_info_.sample_rate/1000, file_info_.channels, 1);
181 /* initialize vis stuff */
182 mod_.SAVSAInit(maxlatency, file_info_.sample_rate);
183 mod_.VSASetInfo(file_info_.sample_rate, file_info_.channels);
185 mod_.outMod->SetVolume(-666); /* set the output plug-ins default volume */
187 killDecodeThread = 0;
188 thread_handle = (HANDLE) CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) DecodeThread, (void *) &killDecodeThread, 0, &thread_id);
196 mod_.outMod->Pause(1);
202 mod_.outMod->Pause(0);
211 if(thread_handle != INVALID_HANDLE_VALUE) {
212 killDecodeThread = 1;
213 if(WaitForSingleObject(thread_handle, INFINITE) == WAIT_TIMEOUT) {
214 MessageBox(mod_.hMainWindow, "error asking thread to die!\n", "error killing decode thread", 0);
215 TerminateThread(thread_handle, 0);
217 CloseHandle(thread_handle);
218 thread_handle = INVALID_HANDLE_VALUE;
220 safe_decoder_finish_(decoder_);
222 mod_.outMod->Close();
229 return (int)file_info_.length_in_msec;
234 return decode_pos_ms_ + (mod_.outMod->GetOutputTime() - mod_.outMod->GetWrittenTime());
237 void setoutputtime(int time_in_ms)
239 seek_needed_ = time_in_ms;
242 void setvolume(int volume) { mod_.outMod->SetVolume(volume); }
243 void setpan(int pan) { mod_.outMod->SetPan(pan); }
245 int infoDlg(char *fn, HWND hwnd)
247 /* @@@TODO: implement info dialog. */
251 void getfileinfo(char *filename, char *title, int *length_in_msec)
253 FLAC__StreamMetadata streaminfo;
255 if(0 == filename || filename[0] == '\0') {
258 *length_in_msec = getlength();
259 length_in_msec = 0; /* force skip in following code */
263 if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
264 MessageBox(mod_.hMainWindow, filename, "ERROR: invalid/missing FLAC metadata", 0);
266 static const char *errtitle = "Invalid FLAC File: ";
267 sprintf(title, "%s\"%s\"", errtitle, filename);
270 *length_in_msec = -1;
275 get_description_(filename, title, MAX_PATH);
278 *length_in_msec = (int)(streaminfo.data.stream_info.total_samples * 10 / (streaminfo.data.stream_info.sample_rate / 100));
281 void eq_set(int on, char data[10], int preamp)
285 DWORD WINAPI __stdcall DecodeThread(void *b)
289 while(! *((int *)b) ) {
290 const unsigned channels = file_info_.channels;
291 const unsigned bits_per_sample = file_info_.bits_per_sample;
292 #ifdef FLAC__DO_DITHER
293 const unsigned target_bps = min(bits_per_sample, 16);
295 const unsigned target_bps = bits_per_sample;
297 const unsigned sample_rate = file_info_.sample_rate;
298 if(seek_needed_ != -1) {
299 const double distance = (double)seek_needed_ / (double)getlength();
300 const unsigned target_sample = (unsigned)(distance * (double)file_info_.total_samples);
301 if(FLAC__file_decoder_seek_absolute(decoder_, (FLAC__uint64)target_sample)) {
302 decode_pos_ms_ = (int)(distance * (double)getlength());
305 mod_.outMod->Flush(decode_pos_ms_);
309 if(!mod_.outMod->IsPlaying()) {
310 PostMessage(mod_.hMainWindow, WM_WA_MPEG_EOF, 0, 0);
315 else if(mod_.outMod->CanWrite() >= ((int)(SAMPLES_PER_WRITE*channels*((target_bps+7)/8)) << (mod_.dsp_isactive()?1:0))) {
316 while(wide_samples_in_reservoir_ < SAMPLES_PER_WRITE) {
317 if(FLAC__file_decoder_get_state(decoder_) == FLAC__FILE_DECODER_END_OF_FILE) {
321 else if(!FLAC__file_decoder_process_single(decoder_)) {
322 MessageBox(mod_.hMainWindow, FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder_)], "READ ERROR processing frame", 0);
328 if(wide_samples_in_reservoir_ == 0) {
332 const unsigned n = min(wide_samples_in_reservoir_, SAMPLES_PER_WRITE);
333 const unsigned delta = n * channels;
334 int bytes = (int)FLAC__plugin_common__pack_pcm_signed_little_endian(sample_buffer_, reservoir_, n, channels, bits_per_sample, target_bps);
337 for(i = delta; i < wide_samples_in_reservoir_ * channels; i++)
338 reservoir_[i-delta] = reservoir_[i];
339 wide_samples_in_reservoir_ -= n;
341 do_vis((char *)sample_buffer_, channels, target_bps, decode_pos_ms_, n);
342 decode_pos_ms_ += (n*1000 + sample_rate/2)/sample_rate;
343 if(mod_.dsp_isactive())
344 bytes = mod_.dsp_dosamples((short *)sample_buffer_, n, target_bps, channels, sample_rate) * (channels*target_bps/8);
345 mod_.outMod->Write(sample_buffer_, bytes);
358 "Reference FLAC Player v" FLAC__VERSION_STRING,
360 0, /* hDllInstance */
361 "FLAC\0FLAC Audio File (*.FLAC)\0"
385 0,0,0,0,0,0,0,0,0, /* vis stuff */
398 __declspec( dllexport ) In_Module * winampGetInModule2()
404 /***********************************************************************
406 **********************************************************************/
407 FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder)
410 MessageBox(mod_.hMainWindow, "Decoder instance is NULL", "ERROR initializing decoder", 0);
414 safe_decoder_finish_(decoder);
416 FLAC__file_decoder_set_md5_checking(decoder, false);
417 FLAC__file_decoder_set_filename(decoder, filename);
418 FLAC__file_decoder_set_write_callback(decoder, write_callback_);
419 FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_);
420 FLAC__file_decoder_set_error_callback(decoder, error_callback_);
421 FLAC__file_decoder_set_client_data(decoder, &file_info_);
422 if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) {
423 MessageBox(mod_.hMainWindow, FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)], "ERROR initializing decoder", 0);
427 file_info_.abort_flag = false;
428 if(!FLAC__file_decoder_process_until_end_of_metadata(decoder)) {
429 MessageBox(mod_.hMainWindow, FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)], "ERROR processing metadata", 0);
433 if(file_info_.abort_flag) {
434 /* metadata callback already popped up the error dialog */
441 void safe_decoder_finish_(FLAC__FileDecoder *decoder)
443 if(decoder && FLAC__file_decoder_get_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED)
444 FLAC__file_decoder_finish(decoder);
447 void safe_decoder_delete_(FLAC__FileDecoder *decoder)
450 safe_decoder_finish_(decoder);
451 FLAC__file_decoder_delete(decoder);
455 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
457 file_info_struct *file_info = (file_info_struct *)client_data;
458 const unsigned channels = file_info->channels, wide_samples = frame->header.blocksize;
459 unsigned wide_sample, offset_sample, channel;
463 if(file_info->abort_flag)
464 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
466 for(offset_sample = wide_samples_in_reservoir_ * channels, wide_sample = 0; wide_sample < wide_samples; wide_sample++)
467 for(channel = 0; channel < channels; channel++, offset_sample++)
468 reservoir_[offset_sample] = buffer[channel][wide_sample];
470 wide_samples_in_reservoir_ += wide_samples;
472 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
475 void metadata_callback_(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
477 file_info_struct *file_info = (file_info_struct *)client_data;
479 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
480 FLAC__ASSERT(metadata->data.stream_info.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
481 file_info->total_samples = (unsigned)(metadata->data.stream_info.total_samples&0xffffffff);
482 file_info->bits_per_sample = metadata->data.stream_info.bits_per_sample;
483 file_info->channels = metadata->data.stream_info.channels;
484 file_info->sample_rate = metadata->data.stream_info.sample_rate;
486 #ifdef FLAC__DO_DITHER
487 if(file_info->bits_per_sample != 8 && file_info->bits_per_sample != 16 && file_info->bits_per_sample != 24) {
488 MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16/24-bit samples\n", "ERROR: plugin can only handle 8/16/24-bit samples", 0);
489 file_info->abort_flag = true;
493 if(file_info->bits_per_sample != 8 && file_info->bits_per_sample != 16 && file_info->bits_per_sample != 24) {
494 MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16/24-bit samples\n", "ERROR: plugin can only handle 8/16/24-bit samples", 0);
495 file_info->abort_flag = true;
499 file_info->length_in_msec = file_info->total_samples * 10 / (file_info->sample_rate / 100);
503 void error_callback_(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
505 file_info_struct *file_info = (file_info_struct *)client_data;
507 if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
508 file_info->abort_flag = true;
511 static FLAC__bool local__is_blank(const char *s)
513 return 0 == s || *s == '\0';
516 void get_description_(const char *filename, char *description, unsigned max_size)
518 FLAC_Plugin__CanonicalTag tag;
520 FLAC_plugin__canonical_tag_init(&tag);
521 FLAC_plugin__canonical_tag_get_combined(filename, &tag);
523 /* @@@ when config window is done, add code here for converting to user charset ala plugin_xmms */
525 if(local__is_blank(tag.performer) && local__is_blank(tag.composer) && local__is_blank(tag.title)) {
526 /* set the description to the filename */
528 const char *temp = strrchr(filename, '\\');
530 temp = strrchr(filename, '/');
535 strncpy(description, temp, max_size);
536 description[max_size-1] = '\0';
537 if(0 != (p = strrchr(description, '.')))
541 char *artist = !local__is_blank(tag.performer)? tag.performer : !local__is_blank(tag.composer)? tag.composer : "Unknown Artist";
542 char *title = !local__is_blank(tag.title)? tag.title : "Untitled";
544 /* there's no snprintf in VC6 so we get sloppy */
546 snprintf(description, max_size, "%s - %s", artist, title);
548 const unsigned needed = strlen(artist) + strlen(title) + 3 + 1;
549 if(needed <= max_size)
550 sprintf(description, "%s - %s", artist, title);
552 char *p = malloc(needed);
554 sprintf(p, "%s - %s", artist, title);
555 p[max_size-1] = '\0';
556 strcpy(description, p);
562 FLAC_plugin__canonical_tag_clear(&tag);