summaryrefslogtreecommitdiff
path: root/src/plugin_winamp2/in_flac.c
blob: 0db37f10349bd4b9328fff79aae5d4a6f62ebb76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* in_flac - Winamp2 FLAC input plugin
 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#if HAVE_CONFIG_H
#  include <config.h>
#endif

#include <windows.h>
#include <limits.h> /* for INT_MAX */
#include <stdio.h>

#include "winamp2/in2.h"
#include "configure.h"
#include "infobox.h"
#include "tagz.h"

#define PLUGIN_VERSION          "1.1.2"

static In_Module mod_;                      /* the input module (declared near the bottom of this file) */
static char lastfn_[MAX_PATH];              /* currently playing file (used for getting info on the current file) */
flac_config_t flac_cfg;

static stream_data_struct stream_data_;
static int paused;
static FLAC__StreamDecoder *decoder_;
static char sample_buffer_[SAMPLES_PER_WRITE * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS * (24/8) * 2];
/* (24/8) for max bytes per sample, and 2 for DSPs */

static HANDLE thread_handle = NULL;         /* the handle to the decode thread */
static DWORD WINAPI DecodeThread(void *b);  /* the decode thread procedure */

/*
 *  init/quit
 */

static void init()
{
	decoder_ = FLAC__stream_decoder_new();
	strcpy(lastfn_, "");

	InitConfig();
	ReadConfig();
	InitInfobox();
}

static void quit()
{
	WriteConfig();
	DeinitInfobox();
	FLAC_plugin__decoder_delete(decoder_);
	decoder_ = 0;
}

/*
 *  open/close
 */

static int isourfile(char *fn) { return 0; }

static int play(char *fn)
{
	LONGLONG filesize;
	DWORD thread_id;
	int   maxlatency;
	/* checks */
	if (decoder_ == 0) return 1;
	if (!(filesize = FileSize(fn))) return -1;
	/* init decoder */
	if (!FLAC_plugin__decoder_init(decoder_, fn, filesize, &stream_data_, &flac_cfg.output))
		return 1;
	strcpy(lastfn_, fn);
	/* open output */
	maxlatency = mod_.outMod->Open(stream_data_.sample_rate, stream_data_.channels, stream_data_.output_bits_per_sample, -1, -1);
	if (maxlatency < 0)
	{
		FLAC_plugin__decoder_finish(decoder_);
		return 1;
	}
	/* set defaults */
	mod_.outMod->SetVolume(-666);
	mod_.outMod->SetPan(0);
	/* initialize vis stuff */
	mod_.SAVSAInit(maxlatency, stream_data_.sample_rate);
	mod_.VSASetInfo(stream_data_.sample_rate, stream_data_.channels);
	/* set info */
	mod_.SetInfo(stream_data_.average_bps, stream_data_.sample_rate/1000, stream_data_.channels, 1);
	/* start playing thread */
	paused = 0;
	thread_handle = CreateThread(NULL, 0, DecodeThread, NULL, 0, &thread_id);
	if (!thread_handle)	return 1;

	return 0;
}

static void stop()
{
	if (thread_handle)
	{
		stream_data_.is_playing = false;
		if (WaitForSingleObject(thread_handle, 2000) == WAIT_TIMEOUT)
		{
			FLAC_plugin__show_error("Error while stopping decoding thread.");
			TerminateThread(thread_handle, 0);
		}
		CloseHandle(thread_handle);
		thread_handle = NULL;
	}

	FLAC_plugin__decoder_finish(decoder_);
	mod_.outMod->Close();
	mod_.SAVSADeInit();
}

/*
 *  play control
 */

static void pause()
{
	paused = 1;
	mod_.outMod->Pause(1);
}

static void unpause()
{
	paused = 0;
	mod_.outMod->Pause(0);
}

static int ispaused()
{
	return paused;
}

static int getlength()
{
	return stream_data_.length_in_msec;
}

static int getoutputtime()
{
	return mod_.outMod->GetOutputTime();
}

static void setoutputtime(int time_in_ms)
{
	stream_data_.seek_to = time_in_ms;
}

static void setvolume(int volume)
{
	mod_.outMod->SetVolume(volume);
}

static void setpan(int pan)
{
	mod_.outMod->SetPan(pan);
}

static void eq_set(int on, char data[10], int preamp) {}

/*
 *  playing loop
 */

static void do_vis(char *data, int nch, int resolution, int position, unsigned samples)
{
	static char vis_buffer[SAMPLES_PER_WRITE * FLAC_PLUGIN__MAX_SUPPORTED_CHANNELS];
	char *ptr;
	int size, count;

	/*
	 * Winamp visuals may have problems accepting sample sizes larger than
	 * 16 bits, so we reduce the sample size here if necessary.
	 */

	switch(resolution) {
		case 32:
		case 24:
			size  = resolution / 8;
			count = samples * nch;
			data += size - 1;

			ptr = vis_buffer;
			while(count--) {
				*ptr++ = data[0] ^ 0x80;
				data += size;
			}

			data = vis_buffer;
			resolution = 8;
			/* fall through */
		case 16:
		case 8:
			mod_.SAAddPCMData(data, nch, resolution, position);
			mod_.VSAAddPCMData(data, nch, resolution, position);
	}
}

static DWORD WINAPI DecodeThread(void *unused)
{
	const unsigned channels = stream_data_.channels;
	const unsigned bits_per_sample = stream_data_.bits_per_sample;
	const unsigned target_bps = stream_data_.output_bits_per_sample;
	const unsigned sample_rate = stream_data_.sample_rate;
	const unsigned fact = channels * (target_bps/8);

	while (stream_data_.is_playing)
	{
		/* seek needed */
		if (stream_data_.seek_to != -1)
		{
			const int pos = FLAC_plugin__seek(decoder_, &stream_data_);
			if (pos != -1) mod_.outMod->Flush(pos);
		}
		/* stream ended */
		else if (stream_data_.eof)
		{
			if (!mod_.outMod->IsPlaying())
			{
				PostMessage(mod_.hMainWindow, WM_WA_MPEG_EOF, 0, 0);
				return 0;
			}
			Sleep(10);
		}
		/* decode */
		else
		{
			/* decode samples */
			int bytes = FLAC_plugin__decode(decoder_, &stream_data_, sample_buffer_);
			const int n = bytes / fact;
			/* visualization */
			do_vis(sample_buffer_, channels, target_bps, mod_.outMod->GetWrittenTime(), n);
			/* dsp */
			if (mod_.dsp_isactive())
				bytes = mod_.dsp_dosamples((short*)sample_buffer_, n, target_bps, channels, sample_rate) * fact;
			/* output */
			while (mod_.outMod->CanWrite()<bytes && stream_data_.is_playing && stream_data_.seek_to==-1)
				Sleep(20);
			if (stream_data_.is_playing && stream_data_.seek_to==-1)
				mod_.outMod->Write(sample_buffer_, bytes);
			/* show bitrate */
			if (flac_cfg.display.show_bps)
			{
				const int rate = FLAC_plugin__get_rate(mod_.outMod->GetWrittenTime(), mod_.outMod->GetOutputTime(), &stream_data_);
				if (rate) mod_.SetInfo(rate/1000, stream_data_.sample_rate/1000, stream_data_.channels, 1);
			}
		}
	}

	return 0;
}

/*
 *  title formatting
 */

static T_CHAR *get_tag(const T_CHAR *tag, void *param)
{
	FLAC__StreamMetadata *tags = (FLAC__StreamMetadata*)param;
	char *tagname, *p;
	T_CHAR *val;

	if (!tag)
		return 0;
	/* Vorbis comment names must be ASCII, so convert 'tag' first */
	tagname = malloc(wcslen(tag)+1);
	for(p=tagname;*tag;) {
		if(*tag > 0x7d) {
			free(tagname);
			return 0;
		}
		else
			*p++ = (char)(*tag++);
	}
	*p++ = '\0';
	/* now get it */
	val = FLAC_plugin__tags_get_tag_ucs2(tags, tagname);
	free(tagname);
	/* some "user friendly cheavats" */
	if (!val)
	{
		if (!wcsicmp(tag, L"ARTIST"))
		{
			val = FLAC_plugin__tags_get_tag_ucs2(tags, "PERFORMER");
			if (!val) val = FLAC_plugin__tags_get_tag_ucs2(tags, "COMPOSER");
		}
		else if (!wcsicmp(tag, L"YEAR") || !wcsicmp(tag, L"DATE"))
		{
			val = FLAC_plugin__tags_get_tag_ucs2(tags, "YEAR_RECORDED");
			if (!val) val = FLAC_plugin__tags_get_tag_ucs2(tags, "YEAR_PERFORMED");
		}
	}

	return val;
}

static void free_tag(T_CHAR *tag, void *param)
{
	(void)param;
	free(tag);
}

static void format_title(const char *filename, WCHAR *title, unsigned max_size)
{
	FLAC__StreamMetadata *tags;

	ReadTags(filename, &tags, /*forDisplay=*/true);

	tagz_format(flac_cfg.title.tag_format_w, get_tag, free_tag, tags, title, max_size);

	FLAC_plugin__tags_destroy(&tags);
}

static void getfileinfo(char *filename, char *title, int *length_in_msec)
{
	FLAC__StreamMetadata streaminfo;

	if (!filename || !*filename) {
		filename = lastfn_;
		if (length_in_msec) {
			*length_in_msec = stream_data_.length_in_msec;
			length_in_msec = 0;    /* force skip in following code */
		}
	}

	if (!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
		if (length_in_msec)
			*length_in_msec = -1;
		return;
	}

	if (title) {
		static WCHAR buffer[400];
		format_title(filename, buffer, 400);
		WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK, buffer, -1, title, 400, NULL, NULL);
	}

	if (length_in_msec) {
		/* with VC++ you have to spoon feed it the casting from uint64->int64->double */
		FLAC__uint64 l = (FLAC__uint64)((double)(FLAC__int64)streaminfo.data.stream_info.total_samples / (double)streaminfo.data.stream_info.sample_rate * 1000.0 + 0.5);
		if (l > INT_MAX)
			l = INT_MAX;
		*length_in_msec = (int)l;
	}
}

/*
 *  interface
 */

void FLAC_plugin__show_error(const char *message,...)
{
	char foo[512];
	va_list args;
	va_start(args, message);
	vsprintf(foo, message, args);
	va_end(args);
	MessageBox(mod_.hMainWindow, foo, "FLAC Plug-in Error", MB_ICONSTOP);
}

static void about(HWND hwndParent)
{
	MessageBox(hwndParent, "Winamp2 FLAC Plugin v"PLUGIN_VERSION"\nby Josh Coalson and X-Fixer\n\nuses libFLAC "VERSION"\nSee http://flac.sourceforge.net/\n", "About FLAC Plugin", MB_ICONINFORMATION);
}

static void config(HWND hwndParent)
{
	if (DoConfig(mod_.hDllInstance, hwndParent))
		WriteConfig();
}

static int infobox(char *fn, HWND hwnd)
{
	DoInfoBox(mod_.hDllInstance, hwnd, fn);
	return 0;
}

/*
 *  exported stuff
 */

static In_Module mod_ =
{
	IN_VER,
	"FLAC Decoder v" PLUGIN_VERSION,
	0,                                    /* hMainWindow */
	0,                                    /* hDllInstance */
	"FLAC\0FLAC Audio File (*.FLAC)\0",
	1,                                    /* is_seekable */
	1,                                    /* uses output */
	config,
	about,
	init,
	quit,
	getfileinfo,
	infobox,
	isourfile,
	play,
	pause,
	unpause,
	ispaused,
	stop,

	getlength,
	getoutputtime,
	setoutputtime,

	setvolume,
	setpan,

	0,0,0,0,0,0,0,0,0,                    /* vis stuff */
	0,0,                                  /* dsp */
	eq_set,
	NULL,                                 /* setinfo */
	0                                     /* out_mod */
};

__declspec(dllexport) In_Module *winampGetInModule2()
{
	return &mod_;
}

BOOL WINAPI _DllMainCRTStartup(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
	return TRUE;
}