FlacLoader.cpp 44 KB

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