aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Input/in_flac/ExtendedRead.cpp
blob: d3c08fc8180eb877ee5a0c6c0b73618e1414a37c (plain) (blame)
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
/*
** Copyright (C) 2007-2011 Nullsoft, Inc.
**
** This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held 
** liable for any damages arising from the use of this software. 
**
** Permission is granted to anyone to use this software for any purpose, including commercial applications, and to 
** alter it and redistribute it freely, subject to the following restrictions:
**
**   1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 
**      If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
**
**   2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
**
**   3. This notice may not be removed or altered from any source distribution.
**
** Author: Ben Allison benski@winamp.com
** Created: March 1, 2007
**
*/

#include "main.h"
#include <FLAC/all.h>
#include "StreamFileWin32.h"
#include "QuickBuf.h"
#include <bfc/platform/types.h>
#include <assert.h>
#include "FLACFileCallbacks.h"
#include "nswasabi/ReferenceCounted.h"

struct ExtendedRead
{
	int bps, channels, samplerate, truebps;
	uint64_t samples;
	QuickBuf output;
	FLAC__StreamDecoder *decoder;
	size_t used;
	FLACClientData client_data;
};

static void OnError(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
	//client_data=client_data; // dummy line so i can set a breakpoint
}

static void OnMetadata(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
	ExtendedRead *ext = FLAC_GetObject<ExtendedRead>(client_data);

	switch(metadata->type)
	{
	case FLAC__METADATA_TYPE_STREAMINFO:
		{
			ext->truebps=metadata->data.stream_info.bits_per_sample;
			ext->bps = (ext->truebps +7) & (~7);
			ext->channels=metadata->data.stream_info.channels;
			ext->samplerate=metadata->data.stream_info.sample_rate;
			ext->samples=metadata->data.stream_info.total_samples;
		}
		break;
	}
}

static FLAC__StreamDecoderWriteStatus OnAudio(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[], void *client_data)
{
	ExtendedRead *ext = FLAC_GetObject<ExtendedRead>(client_data);

	int byteLength = (ext->bps/8) * ext->channels * frame->header.blocksize;
	ext->output.Reserve(byteLength);
	InterleaveAndTruncate(buffer, ext->output, ext->bps, ext->channels, frame->header.blocksize);
	ext->used = byteLength;
	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

extern "C"
{
	__declspec( dllexport ) intptr_t winampGetExtendedRead_openW(const wchar_t *fn, int *size, int *bps, int *nch, int *srate)
	{
		nx_file_t file;
		ReferenceCountedNXString filename_nx;
		ReferenceCountedNXURI filename_uri;
		NXStringCreateWithUTF16(&filename_nx, fn);
		NXURICreateWithNXString(&filename_uri, filename_nx);

		int ret = NXFileOpenFile(&file, filename_uri, nx_file_FILE_read_binary);
		if (ret != NErr_Success)
			return 0;

		ExtendedRead * e = (ExtendedRead *)calloc(sizeof(ExtendedRead), 1);
		e->decoder = FLAC__stream_decoder_new();
		if (e->decoder == 0)
		{
			NXFileRelease(file);
			free(e);
			return 0;
		}

		e->client_data.SetFile(file);
		e->client_data.SetObject(e);

		FLAC__stream_decoder_set_md5_checking(e->decoder, true);
		if(FLAC__stream_decoder_init_stream(
			e->decoder,
			FLAC_NXFile_Read,
			FLAC_NXFile_Seek,
			FLAC_NXFile_Tell,
			FLAC_NXFile_Length,
			FLAC_NXFile_EOF,  
			OnAudio,
			OnMetadata,  
			OnError,
			&e->client_data 
			) != FLAC__STREAM_DECODER_INIT_STATUS_OK) 
		{
			FLAC__stream_decoder_delete(e->decoder);
			NXFileRelease(file);
			free(e);
			return 0;	
		}

		FLAC__stream_decoder_process_until_end_of_metadata(e->decoder);
		*bps = e->truebps;
		*nch = e->channels;
		*srate = e->samplerate;
		*size = (int)(e->samples * (e->bps/8) * e->channels);
		return (intptr_t) e;
	}

	__declspec( dllexport ) intptr_t winampGetExtendedRead_getData(intptr_t handle, char *dest, size_t len, int *killswitch)
	{
		ExtendedRead *ext = (ExtendedRead *)handle;

		while(!ext->used) // loop until we get some data
		{
			if (FLAC__stream_decoder_process_single(ext->decoder) == 0)
				break; // break out if there's an error

			FLAC__StreamDecoderState FLACstate = FLAC__stream_decoder_get_state(ext->decoder);
			if (FLACstate == FLAC__STREAM_DECODER_END_OF_STREAM) // break out if we hit EOF
				break;
		}

		if (ext->used)
		{
			size_t toCopy = min(len, ext->used);
			memcpy(dest, ext->output, toCopy);
			if (toCopy < ext->used)
				ext->output.Move(toCopy);
			ext->used-=toCopy;
			return toCopy;
		}

		return 0;
	}

	__declspec( dllexport ) void winampGetExtendedRead_close(intptr_t handle)
	{
		ExtendedRead *ext = (ExtendedRead *)handle;

		ext->output.Free();
		FLAC__stream_decoder_finish(ext->decoder);
		FLAC__stream_decoder_delete(ext->decoder);
		NXFileRelease(ext->client_data.GetFile());
		free(ext);
	}
}