1 /*
2  * SampleFormatBRR.cpp
3  * -------------------
4  * Purpose: BRR (SNES Bit Rate Reduction) sample format import.
5  * Notes  : This format has no magic bytes, so frame headers are thoroughly validated.
6  * Authors: OpenMPT Devs
7  * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
8  */
9 
10 
11 #include "stdafx.h"
12 #include "Sndfile.h"
13 #include "../common/FileReader.h"
14 
15 
16 OPENMPT_NAMESPACE_BEGIN
17 
18 
ProcessBRRSample(int32 sample,int16 * output,uint8 range,uint8 filter)19 static void ProcessBRRSample(int32 sample, int16 *output, uint8 range, uint8 filter)
20 {
21 	if(sample >= 8)
22 		sample -= 16;
23 
24 	if(range <= 12)
25 		sample = mpt::rshift_signed(mpt::lshift_signed(sample, range), 1);
26 	else
27 		sample = (sample < 0) ? -2048 : 0;  // Implementations do not fully agree on what to do in this case. This is what bsnes does.
28 
29 	// Apply prediction filter
30 	// Note 1: It is okay that we may access data before the first sample point because this memory is reserved for interpolation
31 	// Note 2: The use of signed shift arithmetic is crucial for some samples (e.g. killer lead.brr, Mac2.brr)
32 	// Note 3: Divisors are twice of what is written in the respective comments, as all sample data is divided by 2 (again crucial for accuracy)
33 	static_assert(InterpolationLookaheadBufferSize >= 2);
34 	switch(filter)
35 	{
36 	case 1:  // y(n) = x(n) + x(n-1) * 15/16
37 		sample += mpt::rshift_signed(output[-1] * 15, 5);
38 		break;
39 	case 2:  // y(n) = x(n) + x(n-1) * 61/32 - x(n-2) * 15/16
40 		sample += mpt::rshift_signed(output[-1] * 61, 6) + mpt::rshift_signed(output[-2] * -15, 5);
41 		break;
42 	case 3:  // y(n) = x(n) + x(n-1) * 115/64 - x(n-2) * 13/16
43 		sample += mpt::rshift_signed(output[-1] * 115, 7) + mpt::rshift_signed(output[-2] * -13, 5);
44 		break;
45 	}
46 
47 	sample = std::clamp(sample, int32(-32768), int32(32767)) * 2;
48 	if(sample > 32767)
49 		sample -= 65536;
50 	else if(sample < -32768)
51 		sample += 65536;
52 	output[0] = static_cast<int16>(sample);
53 }
54 
55 
ReadBRRSample(SAMPLEINDEX sample,FileReader & file)56 bool CSoundFile::ReadBRRSample(SAMPLEINDEX sample, FileReader &file)
57 {
58 	const auto fileSize = file.GetLength();
59 	if(fileSize < 9 || fileSize > uint16_max)
60 		return false;
61 	const bool hasLoopInfo = (fileSize % 9) == 2;
62 	if((fileSize % 9) != 0 && !hasLoopInfo)
63 		return false;
64 
65 	file.Rewind();
66 
67 	SmpLength loopStart = 0;
68 	if(hasLoopInfo)
69 	{
70 		loopStart = file.ReadUint16LE();
71 		if(loopStart >= fileSize)
72 			return false;
73 		if((loopStart % 9) != 0)
74 			return false;
75 	}
76 
77 	// First scan the file for validity and consistency
78 	// Note: There are some files with loop start set but ultimately the loop is never enabled. Cannot use this as a consistency check.
79 	// Very few files also have a filter set on the first block, so we cannot reject those either.
80 	bool enableLoop = false, first = true;
81 	while(!file.EndOfFile())
82 	{
83 		const auto block = file.ReadArray<uint8, 9>();
84 		const bool isLast = (block[0] & 0x01) != 0;
85 		const bool isLoop = (block[0] & 0x02) != 0;
86 		const uint8 range = block[0] >> 4u;
87 		if(isLast != file.EndOfFile())
88 			return false;
89 		if(!first && enableLoop != isLoop)
90 			return false;
91 		// While a range of 13 is technically invalid as well, it can be found in the wild.
92 		if(range > 13)
93 			return false;
94 		enableLoop = isLoop;
95 		first = false;
96 	}
97 
98 	file.Seek(hasLoopInfo ? 2 : 0);
99 
100 	DestroySampleThreadsafe(sample);
101 	ModSample &mptSmp = Samples[sample];
102 	mptSmp.Initialize();
103 	mptSmp.uFlags = CHN_16BIT;
104 	mptSmp.nLength = mpt::saturate_cast<SmpLength>((fileSize - (hasLoopInfo ? 2 : 0)) * 16 / 9);
105 	if(enableLoop)
106 		mptSmp.SetLoop(loopStart * 16 / 9, mptSmp.nLength, true, false, *this);
107 	mptSmp.nC5Speed = 32000;
108 	m_szNames[sample] = "";
109 
110 	if(!mptSmp.AllocateSample())
111 		return false;
112 
113 	int16 *output = mptSmp.sample16();
114 	while(!file.EndOfFile())
115 	{
116 		const auto block = file.ReadArray<uint8, 9>();
117 		const uint8 range = block[0] >> 4u;
118 		const uint8 filter = (block[0] >> 2) & 0x03;
119 
120 		for(int i = 0; i < 8; i++)
121 		{
122 			ProcessBRRSample(block[i + 1] >> 4u, output, range, filter);
123 			ProcessBRRSample(block[i + 1] & 0x0F, output + 1, range, filter);
124 			output += 2;
125 		}
126 	}
127 	mptSmp.Convert(MOD_TYPE_IT, GetType());
128 	mptSmp.PrecomputeLoops(*this, false);
129 
130 	return true;
131 }
132 
133 
134 OPENMPT_NAMESPACE_END
135