1 /* in_flac - Winamp 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.
28 BOOL WINAPI _DllMainCRTStartup(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
33 /* post this to the main window at end of file (after playback as stopped) */
34 #define WM_WA_MPEG_EOF WM_USER+2
38 unsigned total_samples;
39 unsigned bits_per_sample;
42 unsigned length_in_ms;
45 static bool stream_init(const char *infile);
46 static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
47 static void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
48 static void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
50 In_Module mod; /* the output module (declared near the bottom of this file) */
51 char lastfn[MAX_PATH]; /* currently playing file (used for getting info on the current file) */
52 int decode_pos_ms; /* current decoding position, in milliseconds */
53 int paused; /* are we paused? */
54 int seek_needed; /* if != -1, it is the point that the decode thread should seek to, in ms. */
55 int16 reservoir[FLAC__MAX_BLOCK_SIZE * 2]; /* 2 for max channels */
56 char sample_buffer[576 * 2 * (16/8) * 2]; /* 2 for max channels, (16/8) for max bytes per sample, and 2 for who knows what */
57 unsigned samples_in_reservoir;
58 static stream_info_struct stream_info;
59 static FLAC__FileDecoder *decoder;
61 int killDecodeThread=0; /* the kill switch for the decode thread */
62 HANDLE thread_handle=INVALID_HANDLE_VALUE; /* the handle to the decode thread */
64 DWORD WINAPI __stdcall DecodeThread(void *b); /* the decode thread procedure */
66 void config(HWND hwndParent)
68 MessageBox(hwndParent, "No configuration.", "Configuration", MB_OK);
69 /* if we had a configuration we'd want to write it here :) */
71 void about(HWND hwndParent)
73 MessageBox(hwndParent,"Winamp FLAC Plugin v" FLAC__VERSION_STRING ", by Josh Coalson\nSee http://flac.sourceforge.net/","About FLAC Plugin",MB_OK);
78 decoder = FLAC__file_decoder_get_new_instance();
84 FLAC__file_decoder_free_instance(decoder);
87 int isourfile(char *fn) { return 0; }
88 /* used for detecting URL streams.. unused here. strncmp(fn,"http://",7) to detect HTTP streams, etc */
94 HANDLE input_file=INVALID_HANDLE_VALUE;
100 input_file = CreateFile(fn,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
101 if (input_file == INVALID_HANDLE_VALUE) {
104 CloseHandle(input_file);
106 if(!stream_init(fn)) {
114 samples_in_reservoir = 0;
116 maxlatency = mod.outMod->Open(stream_info.sample_rate, stream_info.channels, stream_info.bits_per_sample, -1,-1);
117 if (maxlatency < 0) { /* error opening device */
121 /* dividing by 1000 for the first parameter of setinfo makes it */
122 /* display 'H'... for hundred.. i.e. 14H Kbps. */
123 mod.SetInfo((stream_info.sample_rate*stream_info.bits_per_sample*stream_info.channels)/1000,stream_info.sample_rate/1000,stream_info.channels,1);
125 /* initialize vis stuff */
126 mod.SAVSAInit(maxlatency,stream_info.sample_rate);
127 mod.VSASetInfo(stream_info.sample_rate,stream_info.channels);
129 mod.outMod->SetVolume(-666); /* set the output plug-ins default volume */
132 thread_handle = (HANDLE) CreateThread(NULL,0,(LPTHREAD_START_ROUTINE) DecodeThread,(void *) &killDecodeThread,0,&thread_id);
140 mod.outMod->Pause(1);
146 mod.outMod->Pause(0);
155 if (thread_handle != INVALID_HANDLE_VALUE) {
157 if (WaitForSingleObject(thread_handle,INFINITE) == WAIT_TIMEOUT) {
158 MessageBox(mod.hMainWindow,"error asking thread to die!\n","error killing decode thread",0);
159 TerminateThread(thread_handle,0);
161 CloseHandle(thread_handle);
162 thread_handle = INVALID_HANDLE_VALUE;
165 if(decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
166 FLAC__file_decoder_finish(decoder);
176 return (int)stream_info.length_in_ms;
181 return decode_pos_ms+(mod.outMod->GetOutputTime()-mod.outMod->GetWrittenTime());
184 void setoutputtime(int time_in_ms)
186 seek_needed=time_in_ms;
189 void setvolume(int volume) { mod.outMod->SetVolume(volume); }
190 void setpan(int pan) { mod.outMod->SetPan(pan); }
192 int infoDlg(char *fn, HWND hwnd)
194 /* TODO: implement info dialog. */
198 void getfileinfo(char *filename, char *title, int *length_in_ms)
200 if (!filename || !*filename) { /* currently playing file */
202 *length_in_ms=getlength();
204 char *p=lastfn+strlen(lastfn);
205 while (*p != '\\' && p >= lastfn) p--;
209 else { /* some other file */
211 FLAC__FileDecoder *tmp_decoder = FLAC__file_decoder_get_new_instance();
212 stream_info_struct tmp_stream_info;
213 tmp_decoder->check_md5 = false; /* turn off MD5 checking in the decoder */
214 tmp_stream_info.abort_flag = false;
215 if(FLAC__file_decoder_init(tmp_decoder, filename, write_callback, metadata_callback, error_callback, &tmp_stream_info) != FLAC__FILE_DECODER_OK)
217 if(!FLAC__file_decoder_process_metadata(tmp_decoder))
220 *length_in_ms = (int)tmp_stream_info.length_in_ms;
222 if(tmp_decoder->state != FLAC__FILE_DECODER_UNINITIALIZED)
223 FLAC__file_decoder_finish(tmp_decoder);
224 FLAC__file_decoder_free_instance(tmp_decoder);
227 char *p=filename+strlen(filename);
228 while (*p != '\\' && p >= filename) p--;
234 void eq_set(int on, char data[10], int preamp)
238 DWORD WINAPI __stdcall DecodeThread(void *b)
242 while (! *((int *)b) ) {
243 unsigned channels = stream_info.channels;
244 unsigned bits_per_sample = stream_info.bits_per_sample;
245 unsigned bytes_per_sample = (bits_per_sample+7)/8;
246 unsigned sample_rate = stream_info.sample_rate;
247 if (seek_needed != -1) {
248 const double distance = (double)seek_needed / (double)getlength();
249 unsigned target_sample = (unsigned)(distance * (double)stream_info.total_samples);
250 if(FLAC__file_decoder_seek_absolute(decoder, (uint64)target_sample)) {
251 decode_pos_ms = (int)(distance * (double)getlength());
254 mod.outMod->Flush(decode_pos_ms);
258 if (!mod.outMod->IsPlaying()) {
259 PostMessage(mod.hMainWindow,WM_WA_MPEG_EOF,0,0);
264 else if (mod.outMod->CanWrite() >= ((int)(576*channels*bytes_per_sample) << (mod.dsp_isactive()?1:0))) {
265 while(samples_in_reservoir < 576) {
266 if(decoder->state == FLAC__FILE_DECODER_END_OF_FILE) {
270 else if(!FLAC__file_decoder_process_one_frame(decoder))
274 if (samples_in_reservoir == 0) {
278 unsigned i, n = min(samples_in_reservoir, 576), delta;
280 signed short *ssbuffer = (signed short *)sample_buffer;
282 for(i = 0; i < n*channels; i++)
283 ssbuffer[i] = reservoir[i];
285 for( ; i < samples_in_reservoir*channels; i++)
286 reservoir[i-delta] = reservoir[i];
287 samples_in_reservoir -= n;
288 l = n * channels * bytes_per_sample;
290 mod.SAAddPCMData((char *)sample_buffer,channels,bits_per_sample,decode_pos_ms);
291 mod.VSAAddPCMData((char *)sample_buffer,channels,bits_per_sample,decode_pos_ms);
292 decode_pos_ms+=(576*1000)/sample_rate;
293 if (mod.dsp_isactive())
294 l=mod.dsp_dosamples((short *)sample_buffer,n/channels/bytes_per_sample,bits_per_sample,channels,sample_rate) * (channels*bytes_per_sample);
295 mod.outMod->Write(sample_buffer,l);
308 "Reference FLAC Player v0.0"
316 0, /* hDllInstance */
317 "FLAC\0FLAC Audio File (*.FLAC)\0"
341 0,0,0,0,0,0,0,0,0, /* vis stuff */
354 __declspec( dllexport ) In_Module * winampGetInModule2()
360 /***********************************************************************
362 **********************************************************************/
363 bool stream_init(const char *infile)
365 decoder->check_md5 = false; /* turn off MD5 checking in the decoder */
366 if(FLAC__file_decoder_init(decoder, infile, write_callback, metadata_callback, error_callback, &stream_info) != FLAC__FILE_DECODER_OK) {
367 MessageBox(mod.hMainWindow,"ERROR initializing decoder, state = %d\n","ERROR initializing decoder",0);
371 stream_info.abort_flag = false;
372 if(!FLAC__file_decoder_process_metadata(decoder)) {
379 FLAC__StreamDecoderWriteStatus write_callback(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
381 stream_info_struct *stream_info = (stream_info_struct *)client_data;
382 unsigned bps = stream_info->bits_per_sample, channels = stream_info->channels;
383 unsigned wide_samples = frame->header.blocksize, wide_sample, sample, channel, offset;
387 if(stream_info->abort_flag)
388 return FLAC__STREAM_DECODER_WRITE_ABORT;
390 offset = samples_in_reservoir * channels;
392 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
393 for(channel = 0; channel < channels; channel++, sample++)
394 reservoir[offset+sample] = (int16)buffer[channel][wide_sample];
396 samples_in_reservoir += wide_samples;
398 return FLAC__STREAM_DECODER_WRITE_CONTINUE;
401 void metadata_callback(const FLAC__FileDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
403 stream_info_struct *stream_info = (stream_info_struct *)client_data;
405 if(metadata->type == FLAC__METADATA_TYPE_ENCODING) {
406 assert(metadata->data.encoding.total_samples < 0x100000000); /* this plugin can only handle < 4 gigasamples */
407 stream_info->total_samples = (unsigned)(metadata->data.encoding.total_samples&0xffffffff);
408 stream_info->bits_per_sample = metadata->data.encoding.bits_per_sample;
409 stream_info->channels = metadata->data.encoding.channels;
410 stream_info->sample_rate = metadata->data.encoding.sample_rate;
412 if(stream_info->bits_per_sample != 16) {
413 MessageBox(mod.hMainWindow,"ERROR: plugin can only handle 16-bit samples\n","ERROR: plugin can only handle 16-bit samples",0);
414 stream_info->abort_flag = true;
417 stream_info->length_in_ms = stream_info->total_samples * 10 / (stream_info->sample_rate / 100);
421 void error_callback(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
423 stream_info_struct *stream_info = (stream_info_struct *)client_data;
425 if(status != FLAC__STREAM_DECODER_ERROR_LOST_SYNC)
426 stream_info->abort_flag = true;