aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Input/in_flac/RawReader.cpp
blob: 9c6814f52d1533326fec2da403f53b6c444efc19 (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
167
168
169
170
171
172
173
174
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include "RawReader.h"
#include "main.h"
#include <limits.h>
#include <shlwapi.h>
#include "../nu/AutoWide.h"
#include <new>
#include <strsafe.h>
#include "nswasabi/ReferenceCounted.h"

static bool IsMyExtension(const wchar_t *filename)
{
	const wchar_t *extension = PathFindExtensionW(filename);
	if (extension && *extension)
	{
		wchar_t exts[128] = {0};
		GetPrivateProfileStringW(L"in_flac", L"extensions", DEFAULT_EXTENSIONSW, exts, 128, winampINI);

		extension++;
		wchar_t *b = exts;
		wchar_t *c;
		do
		{
			wchar_t d[20] = {0};
			StringCchCopyW(d, 15, b);
			if ((c = wcschr(b, L';')))
			{
				if ((c-b)<15)
					d[c - b] = 0;
			}

			if (!lstrcmpiW(extension, d))
				return true;

			b = c + 1;
		}
		while (c);
	}
	return false;
}

int RawMediaReaderService::CreateRawMediaReader(const wchar_t *filename, ifc_raw_media_reader **out_reader)
{
	if (IsMyExtension(filename))
	{
		nx_file_t file;
		ReferenceCountedNXString filename_nx;
		ReferenceCountedNXURI filename_uri;
		NXStringCreateWithUTF16(&filename_nx, filename);
		NXURICreateWithNXString(&filename_uri, filename_nx);

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

		RawMediaReader *reader = new (std::nothrow) RawMediaReader();
		if (!reader)
		{
			NXFileRelease(file);
			return NErr_OutOfMemory;
		}

		ret = reader->Initialize(file);
		NXFileRelease(file);
		if (ret != NErr_Success)
		{
			delete reader;
			return ret;
		}

		*out_reader = reader;
		return NErr_Success;

	}
	else
	{
		return NErr_False;
	}
}

#define CBCLASS RawMediaReaderService
START_DISPATCH;
CB(CREATERAWMEDIAREADER, CreateRawMediaReader);
END_DISPATCH;
#undef CBCLASS

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)
{

}

static FLAC__StreamDecoderWriteStatus OnAudio(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[], void *client_data)
{

	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

RawMediaReader::RawMediaReader()
{
	decoder=0;
	file=0;
}

RawMediaReader::~RawMediaReader()
{
	if (decoder)
		FLAC__stream_decoder_delete(decoder);
	NXFileRelease(file);
}

int RawMediaReader::Initialize(nx_file_t file)
{
	this->file = NXFileRetain(file);
	decoder = FLAC__stream_decoder_new();
	if (!decoder)
		return NErr_FailedCreate;

	FLAC__stream_decoder_set_metadata_ignore(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT);
	state.SetFile(file);
	state.SetObject(this);
	FLAC__stream_decoder_set_md5_checking(decoder, true);
	if(FLAC__stream_decoder_init_stream(
		decoder,
		FLAC_NXFile_Read,
		FLAC_NXFile_Seek,
		FLAC_NXFile_Tell,
		FLAC_NXFile_Length,
		FLAC_NXFile_EOF,
		OnAudio,
		OnMetadata,
		OnError,
		&state 
		) != FLAC__STREAM_DECODER_INIT_STATUS_OK) 
	{
		FLAC__stream_decoder_delete(decoder);
		decoder=0;
		return NErr_Error;	
	}

	FLAC__stream_decoder_process_until_end_of_metadata(decoder);
	uint64_t position;
	FLAC__stream_decoder_get_decode_position(decoder, &position);
	FLAC__stream_decoder_delete(decoder);
	decoder=0;
	NXFileSeek(file, position);
	
	return NErr_Success;
}

int RawMediaReader::Read(void *buffer, size_t buffer_size, size_t *bytes_read)
{
	return NXFileRead(file, buffer, buffer_size, bytes_read);
}

size_t RawMediaReader::Release()
{

	delete this;
	return 0;
}

#define CBCLASS RawMediaReader
START_DISPATCH;
CB(RELEASE, Release);
CB(RAW_READ, Read);
END_DISPATCH;
#undef CBCLASS