SampleFormatBRR.cpp 4.0 KB

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