FlacLoader.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /*
  2. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteString.h>
  7. #include <AK/Debug.h>
  8. #include <AK/DeprecatedFlyString.h>
  9. #include <AK/FixedArray.h>
  10. #include <AK/Format.h>
  11. #include <AK/IntegralMath.h>
  12. #include <AK/Math.h>
  13. #include <AK/MemoryStream.h>
  14. #include <AK/NonnullOwnPtr.h>
  15. #include <AK/ScopeGuard.h>
  16. #include <AK/StdLibExtras.h>
  17. #include <AK/Try.h>
  18. #include <AK/TypedTransfer.h>
  19. #include <AK/UFixedBigInt.h>
  20. #include <LibAudio/FlacLoader.h>
  21. #include <LibAudio/FlacTypes.h>
  22. #include <LibAudio/GenericTypes.h>
  23. #include <LibAudio/LoaderError.h>
  24. #include <LibAudio/MultiChannel.h>
  25. #include <LibAudio/Resampler.h>
  26. #include <LibAudio/VorbisComment.h>
  27. #include <LibCore/File.h>
  28. #include <LibCrypto/Checksum/ChecksumFunction.h>
  29. #include <LibCrypto/Checksum/ChecksummingStream.h>
  30. namespace Audio {
  31. FlacLoaderPlugin::FlacLoaderPlugin(NonnullOwnPtr<SeekableStream> stream)
  32. : LoaderPlugin(move(stream))
  33. {
  34. }
  35. ErrorOr<NonnullOwnPtr<LoaderPlugin>, LoaderError> FlacLoaderPlugin::create(NonnullOwnPtr<SeekableStream> stream)
  36. {
  37. auto loader = make<FlacLoaderPlugin>(move(stream));
  38. TRY(loader->initialize());
  39. return loader;
  40. }
  41. MaybeLoaderError FlacLoaderPlugin::initialize()
  42. {
  43. TRY(parse_header());
  44. TRY(reset());
  45. return {};
  46. }
  47. bool FlacLoaderPlugin::sniff(SeekableStream& stream)
  48. {
  49. BigEndianInputBitStream bit_input { MaybeOwned<Stream>(stream) };
  50. auto maybe_flac = bit_input.read_bits<u32>(32);
  51. return !maybe_flac.is_error() && maybe_flac.value() == 0x664C6143; // "flaC"
  52. }
  53. // 11.5 STREAM
  54. MaybeLoaderError FlacLoaderPlugin::parse_header()
  55. {
  56. BigEndianInputBitStream bit_input { MaybeOwned<Stream>(*m_stream) };
  57. // A mixture of VERIFY and the non-crashing TRY().
  58. #define FLAC_VERIFY(check, category, msg) \
  59. do { \
  60. if (!(check)) { \
  61. return LoaderError { category, TRY(m_stream->tell()), ByteString::formatted("FLAC header: {}", msg) }; \
  62. } \
  63. } while (0)
  64. // Magic number
  65. u32 flac = TRY(bit_input.read_bits<u32>(32));
  66. m_data_start_location += 4;
  67. FLAC_VERIFY(flac == 0x664C6143, LoaderError::Category::Format, "Magic number must be 'flaC'"); // "flaC"
  68. // Receive the streaminfo block
  69. auto streaminfo = TRY(next_meta_block(bit_input));
  70. FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO");
  71. FixedMemoryStream streaminfo_data_memory { streaminfo.data.bytes() };
  72. BigEndianInputBitStream streaminfo_data { MaybeOwned<Stream>(streaminfo_data_memory) };
  73. // 11.10 METADATA_BLOCK_STREAMINFO
  74. m_min_block_size = TRY(streaminfo_data.read_bits<u16>(16));
  75. FLAC_VERIFY(m_min_block_size >= 16, LoaderError::Category::Format, "Minimum block size must be 16");
  76. m_max_block_size = TRY(streaminfo_data.read_bits<u16>(16));
  77. FLAC_VERIFY(m_max_block_size >= 16, LoaderError::Category::Format, "Maximum block size");
  78. m_min_frame_size = TRY(streaminfo_data.read_bits<u32>(24));
  79. m_max_frame_size = TRY(streaminfo_data.read_bits<u32>(24));
  80. m_sample_rate = TRY(streaminfo_data.read_bits<u32>(20));
  81. FLAC_VERIFY(m_sample_rate <= 655350, LoaderError::Category::Format, "Sample rate");
  82. m_num_channels = TRY(streaminfo_data.read_bits<u8>(3)) + 1; // 0 = one channel
  83. m_bits_per_sample = TRY(streaminfo_data.read_bits<u8>(5)) + 1;
  84. if (m_bits_per_sample <= 8) {
  85. // FIXME: Signed/Unsigned issues?
  86. m_sample_format = PcmSampleFormat::Uint8;
  87. } else if (m_bits_per_sample <= 16) {
  88. m_sample_format = PcmSampleFormat::Int16;
  89. } else if (m_bits_per_sample <= 24) {
  90. m_sample_format = PcmSampleFormat::Int24;
  91. } else if (m_bits_per_sample <= 32) {
  92. m_sample_format = PcmSampleFormat::Int32;
  93. } else {
  94. FLAC_VERIFY(false, LoaderError::Category::Format, "Sample bit depth too large");
  95. }
  96. m_total_samples = TRY(streaminfo_data.read_bits<u64>(36));
  97. if (m_total_samples == 0) {
  98. // "A value of zero here means the number of total samples is unknown."
  99. dbgln("FLAC Warning: File has unknown amount of samples, the loader will not stop before EOF");
  100. m_total_samples = NumericLimits<decltype(m_total_samples)>::max();
  101. }
  102. VERIFY(streaminfo_data.is_aligned_to_byte_boundary());
  103. TRY(streaminfo_data.read_until_filled({ m_md5_checksum, sizeof(m_md5_checksum) }));
  104. // Parse other blocks
  105. [[maybe_unused]] u16 meta_blocks_parsed = 1;
  106. [[maybe_unused]] u16 total_meta_blocks = meta_blocks_parsed;
  107. FlacRawMetadataBlock block = streaminfo;
  108. while (!block.is_last_block) {
  109. block = TRY(next_meta_block(bit_input));
  110. switch (block.type) {
  111. case (FlacMetadataBlockType::SEEKTABLE):
  112. TRY(load_seektable(block));
  113. break;
  114. case FlacMetadataBlockType::PICTURE:
  115. TRY(load_picture(block));
  116. break;
  117. case FlacMetadataBlockType::APPLICATION:
  118. // Note: Third-party library can encode specific data in this.
  119. dbgln("FLAC Warning: Unknown 'Application' metadata block encountered.");
  120. [[fallthrough]];
  121. case FlacMetadataBlockType::PADDING:
  122. // Note: A padding block is empty and does not need any treatment.
  123. break;
  124. case FlacMetadataBlockType::VORBIS_COMMENT:
  125. load_vorbis_comment(block);
  126. break;
  127. default:
  128. // TODO: Parse the remaining metadata block types.
  129. break;
  130. }
  131. ++total_meta_blocks;
  132. }
  133. dbgln_if(AFLACLOADER_DEBUG, "Parsed FLAC header: blocksize {}-{}{}, framesize {}-{}, {}Hz, {}bit, {} channels, {} samples total ({:.2f}s), MD5 {}, data start at {:x} bytes, {} headers total (skipped {})", m_min_block_size, m_max_block_size, is_fixed_blocksize_stream() ? " (constant)" : "", m_min_frame_size, m_max_frame_size, m_sample_rate, pcm_bits_per_sample(m_sample_format), m_num_channels, m_total_samples, static_cast<float>(m_total_samples) / static_cast<float>(m_sample_rate), m_md5_checksum, m_data_start_location, total_meta_blocks, total_meta_blocks - meta_blocks_parsed);
  134. TRY(m_seektable.insert_seek_point({ 0, 0 }));
  135. return {};
  136. }
  137. // 11.19. METADATA_BLOCK_PICTURE
  138. MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
  139. {
  140. FixedMemoryStream memory_stream { block.data.bytes() };
  141. BigEndianInputBitStream picture_block_bytes { MaybeOwned<Stream>(memory_stream) };
  142. PictureData picture;
  143. picture.type = static_cast<ID3PictureType>(TRY(picture_block_bytes.read_bits(32)));
  144. auto const mime_string_length = TRY(picture_block_bytes.read_bits(32));
  145. auto offset_before_seeking = memory_stream.offset();
  146. if (offset_before_seeking + mime_string_length >= block.data.size())
  147. return LoaderError { LoaderError::Category::Format, TRY(m_stream->tell()), "Picture MIME type exceeds available data" };
  148. // "The MIME type string, in printable ASCII characters 0x20-0x7E."
  149. picture.mime_string = TRY(String::from_stream(memory_stream, mime_string_length));
  150. for (auto code_point : picture.mime_string.code_points()) {
  151. if (code_point < 0x20 || code_point > 0x7E)
  152. return LoaderError { LoaderError::Category::Format, TRY(m_stream->tell()), "Picture MIME type is not ASCII in range 0x20 - 0x7E" };
  153. }
  154. auto const description_string_length = TRY(picture_block_bytes.read_bits(32));
  155. offset_before_seeking = memory_stream.offset();
  156. if (offset_before_seeking + description_string_length >= block.data.size())
  157. return LoaderError { LoaderError::Category::Format, TRY(m_stream->tell()), "Picture description exceeds available data" };
  158. picture.description_string = TRY(String::from_stream(memory_stream, description_string_length));
  159. picture.width = TRY(picture_block_bytes.read_bits(32));
  160. picture.height = TRY(picture_block_bytes.read_bits(32));
  161. picture.color_depth = TRY(picture_block_bytes.read_bits(32));
  162. picture.colors = TRY(picture_block_bytes.read_bits(32));
  163. auto const picture_size = TRY(picture_block_bytes.read_bits(32));
  164. offset_before_seeking = memory_stream.offset();
  165. if (offset_before_seeking + picture_size > block.data.size())
  166. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(TRY(m_stream->tell())), "Picture size exceeds available data" };
  167. TRY(memory_stream.seek(picture_size, SeekMode::FromCurrentPosition));
  168. picture.data = Vector<u8> { block.data.bytes().slice(offset_before_seeking, picture_size) };
  169. m_pictures.append(move(picture));
  170. return {};
  171. }
  172. // 11.15. METADATA_BLOCK_VORBIS_COMMENT
  173. void FlacLoaderPlugin::load_vorbis_comment(FlacRawMetadataBlock& block)
  174. {
  175. auto metadata_or_error = Audio::load_vorbis_comment(block.data);
  176. if (metadata_or_error.is_error()) {
  177. dbgln("FLAC Warning: Vorbis comment invalid, error: {}", metadata_or_error.release_error());
  178. return;
  179. }
  180. m_metadata = metadata_or_error.release_value();
  181. }
  182. // 11.13. METADATA_BLOCK_SEEKTABLE
  183. MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block)
  184. {
  185. FixedMemoryStream memory_stream { block.data.bytes() };
  186. BigEndianInputBitStream seektable_bytes { MaybeOwned<Stream>(memory_stream) };
  187. for (size_t i = 0; i < block.length / 18; ++i) {
  188. // 11.14. SEEKPOINT
  189. u64 sample_index = TRY(seektable_bytes.read_bits<u64>(64));
  190. u64 byte_offset = TRY(seektable_bytes.read_bits<u64>(64));
  191. // The sample count of a seek point is not relevant to us.
  192. [[maybe_unused]] u16 sample_count = TRY(seektable_bytes.read_bits<u16>(16));
  193. // Placeholder, to be ignored.
  194. if (sample_index == 0xFFFFFFFFFFFFFFFF)
  195. continue;
  196. SeekPoint seekpoint {
  197. .sample_index = sample_index,
  198. .byte_offset = byte_offset,
  199. };
  200. TRY(m_seektable.insert_seek_point(seekpoint));
  201. }
  202. dbgln_if(AFLACLOADER_DEBUG, "Loaded seektable of size {}", m_seektable.size());
  203. return {};
  204. }
  205. // 11.6 METADATA_BLOCK
  206. ErrorOr<FlacRawMetadataBlock, LoaderError> FlacLoaderPlugin::next_meta_block(BigEndianInputBitStream& bit_input)
  207. {
  208. // 11.7 METADATA_BLOCK_HEADER
  209. bool is_last_block = TRY(bit_input.read_bit());
  210. // The block type enum constants agree with the specification
  211. FlacMetadataBlockType type = (FlacMetadataBlockType)TRY(bit_input.read_bits<u8>(7));
  212. m_data_start_location += 1;
  213. FLAC_VERIFY(type != FlacMetadataBlockType::INVALID, LoaderError::Category::Format, "Invalid metadata block");
  214. u32 block_length = TRY(bit_input.read_bits<u32>(24));
  215. m_data_start_location += 3;
  216. // Blocks can be zero-sized, which would trip up the raw data reader below.
  217. if (block_length == 0)
  218. return FlacRawMetadataBlock {
  219. .is_last_block = is_last_block,
  220. .type = type,
  221. .length = 0,
  222. .data = TRY(ByteBuffer::create_uninitialized(0))
  223. };
  224. auto block_data_result = ByteBuffer::create_uninitialized(block_length);
  225. FLAC_VERIFY(!block_data_result.is_error(), LoaderError::Category::IO, "Out of memory");
  226. auto block_data = block_data_result.release_value();
  227. TRY(bit_input.read_until_filled(block_data));
  228. m_data_start_location += block_length;
  229. return FlacRawMetadataBlock {
  230. is_last_block,
  231. type,
  232. block_length,
  233. block_data,
  234. };
  235. }
  236. #undef FLAC_VERIFY
  237. MaybeLoaderError FlacLoaderPlugin::reset()
  238. {
  239. TRY(seek(0));
  240. m_current_frame.clear();
  241. return {};
  242. }
  243. MaybeLoaderError FlacLoaderPlugin::seek(int int_sample_index)
  244. {
  245. auto sample_index = static_cast<size_t>(int_sample_index);
  246. if (sample_index == m_loaded_samples)
  247. return {};
  248. auto maybe_target_seekpoint = m_seektable.seek_point_before(sample_index);
  249. // No seektable or no fitting entry: Perform normal forward read
  250. if (!maybe_target_seekpoint.has_value()) {
  251. if (sample_index < m_loaded_samples) {
  252. TRY(m_stream->seek(m_data_start_location, SeekMode::SetPosition));
  253. m_loaded_samples = 0;
  254. }
  255. if (sample_index - m_loaded_samples == 0)
  256. return {};
  257. dbgln_if(AFLACLOADER_DEBUG, "Seeking {} samples manually", sample_index - m_loaded_samples);
  258. } else {
  259. auto target_seekpoint = maybe_target_seekpoint.release_value();
  260. // When a small seek happens, we may already be closer to the target than the seekpoint.
  261. if (sample_index - target_seekpoint.sample_index > sample_index - m_loaded_samples) {
  262. dbgln_if(AFLACLOADER_DEBUG, "Close enough to target ({} samples): ignoring seek point", sample_index - m_loaded_samples);
  263. } else {
  264. dbgln_if(AFLACLOADER_DEBUG, "Seeking to seektable: sample index {}, byte offset {}", target_seekpoint.sample_index, target_seekpoint.byte_offset);
  265. auto position = target_seekpoint.byte_offset + m_data_start_location;
  266. if (m_stream->seek(static_cast<i64>(position), SeekMode::SetPosition).is_error())
  267. return LoaderError { LoaderError::Category::IO, m_loaded_samples, ByteString::formatted("Invalid seek position {}", position) };
  268. m_loaded_samples = target_seekpoint.sample_index;
  269. }
  270. }
  271. // Skip frames until we're just before the target sample.
  272. VERIFY(m_loaded_samples <= sample_index);
  273. size_t frame_start_location;
  274. while (m_loaded_samples <= sample_index) {
  275. frame_start_location = TRY(m_stream->tell());
  276. (void)TRY(next_frame());
  277. m_loaded_samples += m_current_frame->sample_count;
  278. }
  279. TRY(m_stream->seek(frame_start_location, SeekMode::SetPosition));
  280. return {};
  281. }
  282. bool FlacLoaderPlugin::should_insert_seekpoint_at(u64 sample_index) const
  283. {
  284. auto const max_seekpoint_distance = (maximum_seekpoint_distance_ms * m_sample_rate) / 1000;
  285. auto const seek_tolerance = (seek_tolerance_ms * m_sample_rate) / 1000;
  286. auto const current_seekpoint_distance = m_seektable.seek_point_sample_distance_around(sample_index).value_or(NumericLimits<u64>::max());
  287. auto const previous_seekpoint = m_seektable.seek_point_before(sample_index);
  288. auto const distance_to_previous_seekpoint = previous_seekpoint.has_value() ? sample_index - previous_seekpoint->sample_index : NumericLimits<u64>::max();
  289. // We insert a seekpoint only under two conditions:
  290. // - The seek points around us are spaced too far for what the loader recommends.
  291. // Prevents inserting too many seek points between pre-loaded seek points.
  292. // - We are so far away from the previous seek point that seeking will become too imprecise if we don't insert a seek point at least here.
  293. // Prevents inserting too many seek points at the end of files without pre-loaded seek points.
  294. return current_seekpoint_distance >= max_seekpoint_distance && distance_to_previous_seekpoint >= seek_tolerance;
  295. }
  296. ErrorOr<Vector<FixedArray<Sample>>, LoaderError> FlacLoaderPlugin::load_chunks(size_t samples_to_read_from_input)
  297. {
  298. ssize_t remaining_samples = static_cast<ssize_t>(m_total_samples - m_loaded_samples);
  299. // The first condition is relevant for unknown-size streams (total samples = 0 in the header)
  300. if (m_stream->is_eof() || (m_total_samples < NumericLimits<u64>::max() && remaining_samples <= 0))
  301. return Vector<FixedArray<Sample>> {};
  302. size_t samples_to_read = min(samples_to_read_from_input, remaining_samples);
  303. Vector<FixedArray<Sample>> frames;
  304. // In this case we can know exactly how many frames we're going to read.
  305. if (is_fixed_blocksize_stream() && m_current_frame.has_value())
  306. TRY(frames.try_ensure_capacity(samples_to_read / m_current_frame->sample_count + 1));
  307. size_t sample_index = 0;
  308. while (!m_stream->is_eof() && sample_index < samples_to_read) {
  309. TRY(frames.try_append(TRY(next_frame())));
  310. sample_index += m_current_frame->sample_count;
  311. }
  312. m_loaded_samples += sample_index;
  313. return frames;
  314. }
  315. // 11.21. FRAME
  316. LoaderSamples FlacLoaderPlugin::next_frame()
  317. {
  318. #define FLAC_VERIFY(check, category, msg) \
  319. do { \
  320. if (!(check)) { \
  321. return LoaderError { category, static_cast<size_t>(m_current_sample_or_frame), ByteString::formatted("FLAC header: {}", msg) }; \
  322. } \
  323. } while (0)
  324. auto frame_byte_index = TRY(m_stream->tell());
  325. auto sample_index = m_loaded_samples;
  326. // Insert a new seek point if we don't have enough here.
  327. if (should_insert_seekpoint_at(sample_index)) {
  328. dbgln_if(AFLACLOADER_DEBUG, "Inserting ad-hoc seek point for sample {} at byte {:x} (seekpoint spacing {} samples)", sample_index, frame_byte_index, m_seektable.seek_point_sample_distance_around(sample_index).value_or(NumericLimits<u64>::max()));
  329. auto maybe_error = m_seektable.insert_seek_point({ .sample_index = sample_index, .byte_offset = frame_byte_index - m_data_start_location });
  330. if (maybe_error.is_error())
  331. dbgln("FLAC Warning: Inserting seek point for sample {} failed: {}", sample_index, maybe_error.release_error());
  332. }
  333. auto frame_checksum_stream = TRY(try_make<Crypto::Checksum::ChecksummingStream<IBMCRC>>(MaybeOwned<Stream>(*m_stream)));
  334. auto header_checksum_stream = TRY(try_make<Crypto::Checksum::ChecksummingStream<FlacFrameHeaderCRC>>(MaybeOwned<Stream>(*frame_checksum_stream)));
  335. BigEndianInputBitStream bit_stream { MaybeOwned<Stream> { *header_checksum_stream } };
  336. // 11.22. FRAME_HEADER
  337. u16 sync_code = TRY(bit_stream.read_bits<u16>(14));
  338. FLAC_VERIFY(sync_code == 0b11111111111110, LoaderError::Category::Format, "Sync code");
  339. bool reserved_bit = TRY(bit_stream.read_bit());
  340. FLAC_VERIFY(reserved_bit == 0, LoaderError::Category::Format, "Reserved frame header bit");
  341. // 11.22.2. BLOCKING STRATEGY
  342. [[maybe_unused]] bool blocking_strategy = TRY(bit_stream.read_bit());
  343. u32 sample_count = TRY(convert_sample_count_code(TRY(bit_stream.read_bits<u8>(4))));
  344. u32 frame_sample_rate = TRY(convert_sample_rate_code(TRY(bit_stream.read_bits<u8>(4))));
  345. u8 channel_type_num = TRY(bit_stream.read_bits<u8>(4));
  346. FLAC_VERIFY(channel_type_num < 0b1011, LoaderError::Category::Format, "Channel assignment");
  347. FlacFrameChannelType channel_type = (FlacFrameChannelType)channel_type_num;
  348. u8 bit_depth = TRY(convert_bit_depth_code(TRY(bit_stream.read_bits<u8>(3))));
  349. reserved_bit = TRY(bit_stream.read_bit());
  350. FLAC_VERIFY(reserved_bit == 0, LoaderError::Category::Format, "Reserved frame header end bit");
  351. // 11.22.8. CODED NUMBER
  352. m_current_sample_or_frame = TRY(read_utf8_char(bit_stream));
  353. // Conditional header variables
  354. // 11.22.9. BLOCK SIZE INT
  355. if (sample_count == FLAC_BLOCKSIZE_AT_END_OF_HEADER_8) {
  356. sample_count = TRY(bit_stream.read_bits<u32>(8)) + 1;
  357. } else if (sample_count == FLAC_BLOCKSIZE_AT_END_OF_HEADER_16) {
  358. sample_count = TRY(bit_stream.read_bits<u32>(16)) + 1;
  359. }
  360. // 11.22.10. SAMPLE RATE INT
  361. if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_8) {
  362. frame_sample_rate = TRY(bit_stream.read_bits<u32>(8)) * 1000;
  363. } else if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_16) {
  364. frame_sample_rate = TRY(bit_stream.read_bits<u32>(16));
  365. } else if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10) {
  366. frame_sample_rate = TRY(bit_stream.read_bits<u32>(16)) * 10;
  367. }
  368. // It does not matter whether we extract the checksum from the digest here, or extract the digest 0x00 after processing the checksum.
  369. auto const calculated_header_checksum = header_checksum_stream->digest();
  370. // 11.22.11. FRAME CRC
  371. u8 specified_header_checksum = TRY(bit_stream.read_bits<u8>(8));
  372. VERIFY(bit_stream.is_aligned_to_byte_boundary());
  373. if (specified_header_checksum != calculated_header_checksum)
  374. dbgln("FLAC frame {}: Calculated header checksum {:02x} is different from specified checksum {:02x}", m_current_sample_or_frame, calculated_header_checksum, specified_header_checksum);
  375. dbgln_if(AFLACLOADER_DEBUG, "Frame: {} samples, {}bit {}Hz, channeltype {:x}, {} number {}, header checksum {:02x}{}", sample_count, bit_depth, frame_sample_rate, channel_type_num, blocking_strategy ? "sample" : "frame", m_current_sample_or_frame, specified_header_checksum, specified_header_checksum != calculated_header_checksum ? " (checksum error)"sv : ""sv);
  376. m_current_frame = FlacFrameHeader {
  377. .sample_rate = frame_sample_rate,
  378. .sample_count = static_cast<u16>(sample_count),
  379. .sample_or_frame_index = static_cast<u32>(m_current_sample_or_frame),
  380. .blocking_strategy = static_cast<BlockingStrategy>(blocking_strategy),
  381. .channels = channel_type,
  382. .bit_depth = bit_depth,
  383. .checksum = specified_header_checksum,
  384. };
  385. u8 subframe_count = frame_channel_type_to_channel_count(channel_type);
  386. TRY(m_subframe_buffers.try_resize_and_keep_capacity(subframe_count));
  387. float sample_rescale = 1 / static_cast<float>(1 << (m_current_frame->bit_depth - 1));
  388. dbgln_if(AFLACLOADER_DEBUG, "Samples will be rescaled from {} bits: factor {:.8f}", m_current_frame->bit_depth, sample_rescale);
  389. for (u8 i = 0; i < subframe_count; ++i) {
  390. FlacSubframeHeader new_subframe = TRY(next_subframe_header(bit_stream, i));
  391. auto& subframe_samples = m_subframe_buffers[i];
  392. subframe_samples.clear_with_capacity();
  393. TRY(parse_subframe(subframe_samples, new_subframe, bit_stream));
  394. // We only verify the sample count for the common case of a constant sample rate.
  395. if (m_sample_rate == m_current_frame->sample_rate)
  396. VERIFY(subframe_samples.size() == m_current_frame->sample_count);
  397. }
  398. // 11.2. Overview ("The audio data is composed of...")
  399. bit_stream.align_to_byte_boundary();
  400. // 11.23. FRAME_FOOTER
  401. auto const calculated_frame_checksum = frame_checksum_stream->digest();
  402. auto const specified_frame_checksum = TRY(bit_stream.read_bits<u16>(16));
  403. if (calculated_frame_checksum != specified_frame_checksum)
  404. dbgln("FLAC frame {}: Calculated frame checksum {:04x} is different from specified checksum {:04x}", m_current_sample_or_frame, calculated_frame_checksum, specified_frame_checksum);
  405. dbgln_if(AFLACLOADER_DEBUG, "Subframe footer checksum: {:04x}{}", specified_frame_checksum, specified_frame_checksum != calculated_frame_checksum ? " (checksum error)"sv : ""sv);
  406. FixedArray<Sample> samples;
  407. switch (channel_type) {
  408. case FlacFrameChannelType::Mono:
  409. case FlacFrameChannelType::Stereo:
  410. case FlacFrameChannelType::StereoCenter:
  411. case FlacFrameChannelType::Surround4p0:
  412. case FlacFrameChannelType::Surround5p0:
  413. case FlacFrameChannelType::Surround5p1:
  414. case FlacFrameChannelType::Surround6p1:
  415. case FlacFrameChannelType::Surround7p1: {
  416. auto new_samples = TRY(downmix_surround_to_stereo<Vector<i64>>(m_subframe_buffers, sample_rescale));
  417. samples.swap(new_samples);
  418. break;
  419. }
  420. case FlacFrameChannelType::LeftSideStereo: {
  421. auto new_samples = TRY(FixedArray<Sample>::create(m_current_frame->sample_count));
  422. samples.swap(new_samples);
  423. // channels are left (0) and side (1)
  424. for (size_t i = 0; i < m_current_frame->sample_count; ++i) {
  425. // right = left - side
  426. samples[i] = { static_cast<float>(m_subframe_buffers[0][i]) * sample_rescale,
  427. static_cast<float>(m_subframe_buffers[0][i] - m_subframe_buffers[1][i]) * sample_rescale };
  428. }
  429. break;
  430. }
  431. case FlacFrameChannelType::RightSideStereo: {
  432. auto new_samples = TRY(FixedArray<Sample>::create(m_current_frame->sample_count));
  433. samples.swap(new_samples);
  434. // channels are side (0) and right (1)
  435. for (size_t i = 0; i < m_current_frame->sample_count; ++i) {
  436. // left = right + side
  437. samples[i] = { static_cast<float>(m_subframe_buffers[1][i] + m_subframe_buffers[0][i]) * sample_rescale,
  438. static_cast<float>(m_subframe_buffers[1][i]) * sample_rescale };
  439. }
  440. break;
  441. }
  442. case FlacFrameChannelType::MidSideStereo: {
  443. auto new_samples = TRY(FixedArray<Sample>::create(m_current_frame->sample_count));
  444. samples.swap(new_samples);
  445. // channels are mid (0) and side (1)
  446. for (size_t i = 0; i < m_subframe_buffers[0].size(); ++i) {
  447. i64 mid = m_subframe_buffers[0][i];
  448. i64 side = m_subframe_buffers[1][i];
  449. mid *= 2;
  450. // prevent integer division errors
  451. samples[i] = { static_cast<float>(mid + side) * .5f * sample_rescale,
  452. static_cast<float>(mid - side) * .5f * sample_rescale };
  453. }
  454. break;
  455. }
  456. }
  457. return samples;
  458. #undef FLAC_VERIFY
  459. }
  460. // 11.22.3. INTERCHANNEL SAMPLE BLOCK SIZE
  461. ErrorOr<u32, LoaderError> FlacLoaderPlugin::convert_sample_count_code(u8 sample_count_code)
  462. {
  463. // single codes
  464. switch (sample_count_code) {
  465. case 0:
  466. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved block size" };
  467. case 1:
  468. return 192;
  469. case 6:
  470. return FLAC_BLOCKSIZE_AT_END_OF_HEADER_8;
  471. case 7:
  472. return FLAC_BLOCKSIZE_AT_END_OF_HEADER_16;
  473. }
  474. if (sample_count_code >= 2 && sample_count_code <= 5) {
  475. return 576 * AK::exp2(sample_count_code - 2);
  476. }
  477. return 256 * AK::exp2(sample_count_code - 8);
  478. }
  479. // 11.22.4. SAMPLE RATE
  480. ErrorOr<u32, LoaderError> FlacLoaderPlugin::convert_sample_rate_code(u8 sample_rate_code)
  481. {
  482. switch (sample_rate_code) {
  483. case 0:
  484. return m_sample_rate;
  485. case 1:
  486. return 88200;
  487. case 2:
  488. return 176400;
  489. case 3:
  490. return 192000;
  491. case 4:
  492. return 8000;
  493. case 5:
  494. return 16000;
  495. case 6:
  496. return 22050;
  497. case 7:
  498. return 24000;
  499. case 8:
  500. return 32000;
  501. case 9:
  502. return 44100;
  503. case 10:
  504. return 48000;
  505. case 11:
  506. return 96000;
  507. case 12:
  508. return FLAC_SAMPLERATE_AT_END_OF_HEADER_8;
  509. case 13:
  510. return FLAC_SAMPLERATE_AT_END_OF_HEADER_16;
  511. case 14:
  512. return FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10;
  513. default:
  514. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Invalid sample rate code" };
  515. }
  516. }
  517. // 11.22.6. SAMPLE SIZE
  518. ErrorOr<u8, LoaderError> FlacLoaderPlugin::convert_bit_depth_code(u8 bit_depth_code)
  519. {
  520. switch (bit_depth_code) {
  521. case 0:
  522. return m_bits_per_sample;
  523. case 1:
  524. return 8;
  525. case 2:
  526. return 12;
  527. case 3:
  528. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved sample size" };
  529. case 4:
  530. return 16;
  531. case 5:
  532. return 20;
  533. case 6:
  534. return 24;
  535. case 7:
  536. return 32;
  537. default:
  538. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), ByteString::formatted("Unsupported sample size {}", bit_depth_code) };
  539. }
  540. }
  541. // 11.22.5. CHANNEL ASSIGNMENT
  542. u8 frame_channel_type_to_channel_count(FlacFrameChannelType channel_type)
  543. {
  544. if (channel_type <= FlacFrameChannelType::Surround7p1)
  545. return to_underlying(channel_type) + 1;
  546. return 2;
  547. }
  548. // 11.25. SUBFRAME_HEADER
  549. ErrorOr<FlacSubframeHeader, LoaderError> FlacLoaderPlugin::next_subframe_header(BigEndianInputBitStream& bit_stream, u8 channel_index)
  550. {
  551. u8 bits_per_sample = m_current_frame->bit_depth;
  552. // For inter-channel correlation, the side channel needs an extra bit for its samples
  553. switch (m_current_frame->channels) {
  554. case FlacFrameChannelType::LeftSideStereo:
  555. case FlacFrameChannelType::MidSideStereo:
  556. if (channel_index == 1) {
  557. ++bits_per_sample;
  558. }
  559. break;
  560. case FlacFrameChannelType::RightSideStereo:
  561. if (channel_index == 0) {
  562. ++bits_per_sample;
  563. }
  564. break;
  565. // "normal" channel types
  566. default:
  567. break;
  568. }
  569. // zero-bit padding
  570. if (TRY(bit_stream.read_bit()) != 0)
  571. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Zero bit padding" };
  572. // 11.25.1. SUBFRAME TYPE
  573. u8 subframe_code = TRY(bit_stream.read_bits<u8>(6));
  574. if ((subframe_code >= 0b000010 && subframe_code <= 0b000111) || (subframe_code > 0b001100 && subframe_code < 0b100000))
  575. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Subframe type" };
  576. FlacSubframeType subframe_type;
  577. u8 order = 0;
  578. // LPC has the highest bit set
  579. if ((subframe_code & 0b100000) > 0) {
  580. subframe_type = FlacSubframeType::LPC;
  581. order = (subframe_code & 0b011111) + 1;
  582. } else if ((subframe_code & 0b001000) > 0) {
  583. // Fixed has the third-highest bit set
  584. subframe_type = FlacSubframeType::Fixed;
  585. order = (subframe_code & 0b000111);
  586. } else {
  587. subframe_type = (FlacSubframeType)subframe_code;
  588. }
  589. // 11.25.2. WASTED BITS PER SAMPLE FLAG
  590. bool has_wasted_bits = TRY(bit_stream.read_bit());
  591. u8 k = 0;
  592. if (has_wasted_bits) {
  593. bool current_k_bit = 0;
  594. do {
  595. current_k_bit = TRY(bit_stream.read_bit());
  596. ++k;
  597. } while (current_k_bit != 1);
  598. }
  599. return FlacSubframeHeader {
  600. subframe_type,
  601. order,
  602. k,
  603. bits_per_sample
  604. };
  605. }
  606. ErrorOr<void, LoaderError> FlacLoaderPlugin::parse_subframe(Vector<i64>& samples, FlacSubframeHeader& subframe_header, BigEndianInputBitStream& bit_input)
  607. {
  608. TRY(samples.try_ensure_capacity(m_current_frame->sample_count));
  609. switch (subframe_header.type) {
  610. case FlacSubframeType::Constant: {
  611. // 11.26. SUBFRAME_CONSTANT
  612. u64 constant_value = TRY(bit_input.read_bits<u64>(subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample));
  613. dbgln_if(AFLACLOADER_DEBUG, " Constant subframe: {}", constant_value);
  614. VERIFY(subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample != 0);
  615. i64 constant = sign_extend(static_cast<u64>(constant_value), subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample);
  616. for (u64 i = 0; i < m_current_frame->sample_count; ++i) {
  617. samples.unchecked_append(constant);
  618. }
  619. break;
  620. }
  621. case FlacSubframeType::Fixed: {
  622. dbgln_if(AFLACLOADER_DEBUG, " Fixed LPC subframe order {}", subframe_header.order);
  623. samples = TRY(decode_fixed_lpc(subframe_header, bit_input));
  624. break;
  625. }
  626. case FlacSubframeType::Verbatim: {
  627. dbgln_if(AFLACLOADER_DEBUG, " Verbatim subframe");
  628. samples = TRY(decode_verbatim(subframe_header, bit_input));
  629. break;
  630. }
  631. case FlacSubframeType::LPC: {
  632. dbgln_if(AFLACLOADER_DEBUG, " Custom LPC subframe order {}", subframe_header.order);
  633. TRY(decode_custom_lpc(samples, subframe_header, bit_input));
  634. break;
  635. }
  636. default:
  637. return LoaderError { LoaderError::Category::Unimplemented, static_cast<size_t>(m_current_sample_or_frame), "Unhandled FLAC subframe type" };
  638. }
  639. for (size_t i = 0; i < samples.size(); ++i) {
  640. samples[i] <<= subframe_header.wasted_bits_per_sample;
  641. }
  642. // Resamplers VERIFY that the sample rate is non-zero.
  643. if (m_current_frame->sample_rate == 0 || m_sample_rate == 0
  644. || m_current_frame->sample_rate == m_sample_rate)
  645. return {};
  646. ResampleHelper<i64> resampler(m_current_frame->sample_rate, m_sample_rate);
  647. samples = resampler.resample(samples);
  648. return {};
  649. }
  650. // 11.29. SUBFRAME_VERBATIM
  651. // Decode a subframe that isn't actually encoded, usually seen in random data
  652. ErrorOr<Vector<i64>, LoaderError> FlacLoaderPlugin::decode_verbatim(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  653. {
  654. Vector<i64> decoded;
  655. decoded.ensure_capacity(m_current_frame->sample_count);
  656. if (subframe.bits_per_sample <= subframe.wasted_bits_per_sample) {
  657. return LoaderError {
  658. LoaderError::Category::Format,
  659. TRY(m_stream->tell()),
  660. "Effective verbatim bits per sample are zero"sv,
  661. };
  662. }
  663. for (size_t i = 0; i < m_current_frame->sample_count; ++i) {
  664. decoded.unchecked_append(sign_extend(
  665. TRY(bit_input.read_bits<u64>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
  666. subframe.bits_per_sample - subframe.wasted_bits_per_sample));
  667. }
  668. return decoded;
  669. }
  670. // 11.28. SUBFRAME_LPC
  671. // Decode a subframe encoded with a custom linear predictor coding, i.e. the subframe provides the polynomial order and coefficients
  672. ErrorOr<void, LoaderError> FlacLoaderPlugin::decode_custom_lpc(Vector<i64>& decoded, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  673. {
  674. // LPC must provide at least as many samples as its order.
  675. if (subframe.order > m_current_frame->sample_count)
  676. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Too small frame for LPC order" };
  677. decoded.ensure_capacity(m_current_frame->sample_count);
  678. if (subframe.bits_per_sample <= subframe.wasted_bits_per_sample) {
  679. return LoaderError {
  680. LoaderError::Category::Format,
  681. TRY(m_stream->tell()),
  682. "Effective verbatim bits per sample are zero"sv,
  683. };
  684. }
  685. // warm-up samples
  686. for (auto i = 0; i < subframe.order; ++i) {
  687. decoded.unchecked_append(sign_extend(
  688. TRY(bit_input.read_bits<u64>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
  689. subframe.bits_per_sample - subframe.wasted_bits_per_sample));
  690. }
  691. // precision of the coefficients
  692. u8 lpc_precision = TRY(bit_input.read_bits<u8>(4));
  693. if (lpc_precision == 0b1111)
  694. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Invalid linear predictor coefficient precision" };
  695. lpc_precision += 1;
  696. // shift needed on the data (signed!)
  697. i8 lpc_shift = static_cast<i8>(sign_extend(TRY(bit_input.read_bits<u8>(5)), 5));
  698. Vector<i64, 32> coefficients;
  699. coefficients.ensure_capacity(subframe.order);
  700. // read coefficients
  701. for (auto i = 0; i < subframe.order; ++i) {
  702. u64 raw_coefficient = TRY(bit_input.read_bits<u64>(lpc_precision));
  703. i64 coefficient = sign_extend(raw_coefficient, lpc_precision);
  704. coefficients.unchecked_append(coefficient);
  705. }
  706. dbgln_if(AFLACLOADER_DEBUG, " {}-bit {} shift coefficients: {}", lpc_precision, lpc_shift, coefficients);
  707. TRY(decode_residual(decoded, subframe, bit_input));
  708. // approximate the waveform with the predictor
  709. for (size_t i = subframe.order; i < m_current_frame->sample_count; ++i) {
  710. // (see below)
  711. Checked<i64> sample = 0;
  712. for (size_t t = 0; t < subframe.order; ++t) {
  713. // It's really important that we compute in 64-bit land here.
  714. // Even though FLAC operates at a maximum bit depth of 32 bits, modern encoders use super-large coefficients for maximum compression.
  715. // These will easily overflow 32 bits and cause strange white noise that abruptly stops intermittently (at the end of a frame).
  716. // The simple fix of course is to do intermediate computations in 64 bits, but we additionally use saturating arithmetic.
  717. // These considerations are not in the original FLAC spec, but have been added to the IETF standard: https://datatracker.ietf.org/doc/html/draft-ietf-cellar-flac-03#appendix-A.3
  718. sample.saturating_add(Checked<i64>::saturating_mul(static_cast<i64>(coefficients[t]), static_cast<i64>(decoded[i - t - 1])));
  719. }
  720. decoded[i] += lpc_shift >= 0 ? (sample.value() >> lpc_shift) : (sample.value() << -lpc_shift);
  721. }
  722. return {};
  723. }
  724. // 11.27. SUBFRAME_FIXED
  725. // Decode a subframe encoded with one of the fixed linear predictor codings
  726. ErrorOr<Vector<i64>, LoaderError> FlacLoaderPlugin::decode_fixed_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  727. {
  728. // LPC must provide at least as many samples as its order.
  729. if (subframe.order > m_current_frame->sample_count)
  730. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Too small frame for LPC order" };
  731. Vector<i64> decoded;
  732. decoded.ensure_capacity(m_current_frame->sample_count);
  733. if (subframe.bits_per_sample <= subframe.wasted_bits_per_sample) {
  734. return LoaderError {
  735. LoaderError::Category::Format,
  736. TRY(m_stream->tell()),
  737. "Effective verbatim bits per sample are zero"sv,
  738. };
  739. }
  740. // warm-up samples
  741. for (auto i = 0; i < subframe.order; ++i) {
  742. decoded.unchecked_append(sign_extend(
  743. TRY(bit_input.read_bits<u64>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
  744. subframe.bits_per_sample - subframe.wasted_bits_per_sample));
  745. }
  746. TRY(decode_residual(decoded, subframe, bit_input));
  747. dbgln_if(AFLACLOADER_DEBUG, " decoded length {}, {} order predictor, now at file offset {:x}", decoded.size(), subframe.order, TRY(m_stream->tell()));
  748. // Skip these comments if you don't care about the neat math behind fixed LPC :^)
  749. // These coefficients for the recursive prediction formula are the only ones that can be resolved to polynomial predictor functions.
  750. // The order equals the degree of the polynomial - 1, so the second-order predictor has an underlying polynomial of degree 1, a straight line.
  751. // More specifically, the closest approximation to a polynomial is used, and the degree depends on how many previous values are available.
  752. // This makes use of a very neat property of polynomials, which is that they are entirely characterized by their finitely many derivatives.
  753. // (Mathematically speaking, the infinite Taylor series of any polynomial equals the polynomial itself.)
  754. // Now remember that derivation is just the slope of the function, which is the same as the difference of two close-by values.
  755. // Therefore, with two samples we can calculate the first derivative at a sample via the difference, which gives us a polynomial of degree 1.
  756. // With three samples, we can do the same but also calculate the second derivative via the difference in the first derivatives.
  757. // This gives us a polynomial of degree 2, as it has two "proper" (non-constant) derivatives.
  758. // This can be continued for higher-order derivatives when we have more coefficients, giving us higher-order polynomials.
  759. // In essence, it's akin to a Lagrangian polynomial interpolation for every sample (but already pre-solved).
  760. // The coefficients for orders 0-3 originate from the SHORTEN codec:
  761. // http://mi.eng.cam.ac.uk/reports/svr-ftp/auto-pdf/robinson_tr156.pdf page 4
  762. // The coefficients for order 4 are undocumented in the original FLAC specification(s), but can now be found in
  763. // https://datatracker.ietf.org/doc/html/draft-ietf-cellar-flac-03#section-10.2.5
  764. // FIXME: Share this code with predict_fixed_lpc().
  765. switch (subframe.order) {
  766. case 0:
  767. // s_0(t) = 0
  768. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  769. decoded[i] += 0;
  770. break;
  771. case 1:
  772. // s_1(t) = s(t-1)
  773. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  774. decoded[i] += decoded[i - 1];
  775. break;
  776. case 2:
  777. // s_2(t) = 2s(t-1) - s(t-2)
  778. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  779. decoded[i] += 2 * decoded[i - 1] - decoded[i - 2];
  780. break;
  781. case 3:
  782. // s_3(t) = 3s(t-1) - 3s(t-2) + s(t-3)
  783. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  784. decoded[i] += 3 * decoded[i - 1] - 3 * decoded[i - 2] + decoded[i - 3];
  785. break;
  786. case 4:
  787. // s_4(t) = 4s(t-1) - 6s(t-2) + 4s(t-3) - s(t-4)
  788. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  789. decoded[i] += 4 * decoded[i - 1] - 6 * decoded[i - 2] + 4 * decoded[i - 3] - decoded[i - 4];
  790. break;
  791. default:
  792. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), ByteString::formatted("Unrecognized predictor order {}", subframe.order) };
  793. }
  794. return decoded;
  795. }
  796. // 11.30. RESIDUAL
  797. // Decode the residual, the "error" between the function approximation and the actual audio data
  798. MaybeLoaderError FlacLoaderPlugin::decode_residual(Vector<i64>& decoded, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  799. {
  800. // 11.30.1. RESIDUAL_CODING_METHOD
  801. auto residual_mode = static_cast<FlacResidualMode>(TRY(bit_input.read_bits<u8>(2)));
  802. u8 partition_order = TRY(bit_input.read_bits<u8>(4));
  803. size_t partitions = 1 << partition_order;
  804. dbgln_if(AFLACLOADER_DEBUG, " {}-bit Rice partitions, {} total (order {})", residual_mode == FlacResidualMode::Rice4Bit ? "4"sv : "5"sv, partitions, partition_order);
  805. if (partitions > m_current_frame->sample_count)
  806. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Too many Rice partitions, each partition must contain at least one sample" };
  807. // “The partition order MUST be such that the block size is evenly divisible by the number of partitions.”
  808. // FIXME: Check “The partition order also MUST be such that the (block size >> partition order) is larger than the predictor order.”
  809. if (m_current_frame->sample_count % partitions != 0)
  810. return LoaderError { LoaderError::Category::Format, TRY(m_stream->tell()), "Block size is not evenly divisible by number of partitions" };
  811. if (residual_mode == FlacResidualMode::Rice4Bit) {
  812. // 11.30.2. RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB
  813. // decode a single Rice partition with four bits for the order k
  814. for (size_t i = 0; i < partitions; ++i) {
  815. // FIXME: Write into the decode buffer directly.
  816. auto rice_partition = TRY(decode_rice_partition(4, partitions, i, subframe, bit_input));
  817. decoded.extend(move(rice_partition));
  818. }
  819. } else if (residual_mode == FlacResidualMode::Rice5Bit) {
  820. // 11.30.3. RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB2
  821. // five bits equivalent
  822. for (size_t i = 0; i < partitions; ++i) {
  823. // FIXME: Write into the decode buffer directly.
  824. auto rice_partition = TRY(decode_rice_partition(5, partitions, i, subframe, bit_input));
  825. decoded.extend(move(rice_partition));
  826. }
  827. } else
  828. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved residual coding method" };
  829. return {};
  830. }
  831. // 11.30.2.1. EXP_GOLOMB_PARTITION and 11.30.3.1. EXP_GOLOMB2_PARTITION
  832. // Decode a single Rice partition as part of the residual, every partition can have its own Rice parameter k
  833. ALWAYS_INLINE ErrorOr<Vector<i64>, LoaderError> FlacLoaderPlugin::decode_rice_partition(u8 partition_type, u32 partitions, u32 partition_index, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  834. {
  835. // 11.30.2.2. EXP GOLOMB PARTITION ENCODING PARAMETER and 11.30.3.2. EXP-GOLOMB2 PARTITION ENCODING PARAMETER
  836. u8 k = TRY(bit_input.read_bits<u8>(partition_type));
  837. u32 residual_sample_count;
  838. if (partitions == 0)
  839. residual_sample_count = m_current_frame->sample_count - subframe.order;
  840. else
  841. residual_sample_count = m_current_frame->sample_count / partitions;
  842. if (partition_index == 0) {
  843. if (subframe.order > residual_sample_count)
  844. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "First Rice partition must advertise more residuals than LPC order" };
  845. residual_sample_count -= subframe.order;
  846. }
  847. Vector<i64> rice_partition;
  848. rice_partition.resize(residual_sample_count);
  849. // escape code for unencoded binary partition
  850. if (k == (1 << partition_type) - 1) {
  851. u8 unencoded_bps = TRY(bit_input.read_bits<u8>(5));
  852. if (unencoded_bps != 0) {
  853. for (size_t r = 0; r < residual_sample_count; ++r) {
  854. rice_partition[r] = sign_extend(TRY(bit_input.read_bits<u32>(unencoded_bps)), unencoded_bps);
  855. }
  856. }
  857. } else {
  858. for (size_t r = 0; r < residual_sample_count; ++r) {
  859. rice_partition[r] = TRY(decode_unsigned_exp_golomb(k, bit_input));
  860. }
  861. }
  862. return rice_partition;
  863. }
  864. // Decode a single number encoded with Rice/Exponential-Golomb encoding (the unsigned variant)
  865. ALWAYS_INLINE ErrorOr<i32> decode_unsigned_exp_golomb(u8 k, BigEndianInputBitStream& bit_input)
  866. {
  867. u8 q = 0;
  868. while (TRY(bit_input.read_bit()) == 0)
  869. ++q;
  870. // least significant bits (remainder)
  871. u32 rem = TRY(bit_input.read_bits<u32>(k));
  872. u32 value = q << k | rem;
  873. return rice_to_signed(value);
  874. }
  875. ErrorOr<u64> read_utf8_char(BigEndianInputBitStream& input)
  876. {
  877. u64 character;
  878. u8 start_byte = TRY(input.read_value<u8>());
  879. // Signal byte is zero: ASCII character
  880. if ((start_byte & 0b10000000) == 0) {
  881. return start_byte;
  882. } else if ((start_byte & 0b11000000) == 0b10000000) {
  883. return Error::from_string_literal("Illegal continuation byte");
  884. }
  885. // This algorithm supports the theoretical max 0xFF start byte, which is not part of the regular UTF-8 spec.
  886. u8 length = 1;
  887. while (((start_byte << length) & 0b10000000) == 0b10000000)
  888. ++length;
  889. // This is technically not spec-compliant, but if we take UTF-8 to its logical extreme,
  890. // we can say 0xFF means there's 7 following continuation bytes and no data at all in the leading character.
  891. if (length == 8) [[unlikely]] {
  892. character = 0;
  893. } else {
  894. u8 bits_from_start_byte = 8 - (length + 1);
  895. u8 start_byte_bitmask = AK::exp2(bits_from_start_byte) - 1;
  896. character = start_byte_bitmask & start_byte;
  897. }
  898. for (u8 i = length - 1; i > 0; --i) {
  899. u8 current_byte = TRY(input.read_value<u8>());
  900. character = (character << 6) | (current_byte & 0b00111111);
  901. }
  902. return character;
  903. }
  904. i64 sign_extend(u32 n, u8 size)
  905. {
  906. // negative
  907. if ((n & (1 << (size - 1))) > 0) {
  908. return static_cast<i64>(n | (0xffffffffffffffffLL << size));
  909. }
  910. // positive
  911. return n;
  912. }
  913. i32 rice_to_signed(u32 x)
  914. {
  915. // positive numbers are even, negative numbers are odd
  916. // bitmask for conditionally inverting the entire number, thereby "negating" it
  917. i32 sign = -static_cast<i32>(x & 1);
  918. // copies the sign's sign onto the actual magnitude of x
  919. return static_cast<i32>(sign ^ (x >> 1));
  920. }
  921. }