FlacLoader.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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/FixedArray.h>
  8. #include <AK/FlyString.h>
  9. #include <AK/Format.h>
  10. #include <AK/IntegralMath.h>
  11. #include <AK/Math.h>
  12. #include <AK/ScopeGuard.h>
  13. #include <AK/StdLibExtras.h>
  14. #include <AK/String.h>
  15. #include <AK/StringBuilder.h>
  16. #include <AK/Try.h>
  17. #include <AK/TypedTransfer.h>
  18. #include <AK/UFixedBigInt.h>
  19. #include <LibAudio/Buffer.h>
  20. #include <LibAudio/FlacLoader.h>
  21. #include <LibAudio/FlacTypes.h>
  22. #include <LibAudio/LoaderError.h>
  23. #include <LibCore/MemoryStream.h>
  24. #include <LibCore/Stream.h>
  25. namespace Audio {
  26. FlacLoaderPlugin::FlacLoaderPlugin(StringView path)
  27. : m_file(Core::File::construct(path))
  28. {
  29. if (!m_file->open(Core::OpenMode::ReadOnly)) {
  30. m_error = LoaderError { String::formatted("Can't open file: {}", m_file->error_string()) };
  31. return;
  32. }
  33. auto maybe_stream = Core::Stream::BufferedFile::create(MUST(Core::Stream::File::open(path, Core::Stream::OpenMode::Read)), FLAC_BUFFER_SIZE);
  34. if (maybe_stream.is_error())
  35. m_error = LoaderError { "Can't open file stream" };
  36. else
  37. m_stream = maybe_stream.release_value();
  38. }
  39. FlacLoaderPlugin::FlacLoaderPlugin(Bytes& buffer)
  40. {
  41. auto maybe_stream = Core::Stream::MemoryStream::construct(buffer);
  42. if (maybe_stream.is_error())
  43. m_error = LoaderError { "Can't open memory stream" };
  44. else
  45. m_stream = maybe_stream.release_value();
  46. }
  47. MaybeLoaderError FlacLoaderPlugin::initialize()
  48. {
  49. if (m_error.has_value())
  50. return m_error.release_value();
  51. TRY(parse_header());
  52. TRY(reset());
  53. return {};
  54. }
  55. MaybeLoaderError FlacLoaderPlugin::parse_header()
  56. {
  57. auto bit_input = LOADER_TRY(BigEndianInputBitStream::construct(*m_stream));
  58. // A mixture of VERIFY and the non-crashing TRY().
  59. #define FLAC_VERIFY(check, category, msg) \
  60. do { \
  61. if (!(check)) { \
  62. return LoaderError { category, static_cast<size_t>(m_data_start_location), String::formatted("FLAC header: {}", msg) }; \
  63. } \
  64. } while (0)
  65. // Magic number
  66. u32 flac = LOADER_TRY(bit_input->read_bits<u32>(32));
  67. m_data_start_location += 4;
  68. FLAC_VERIFY(flac == 0x664C6143, LoaderError::Category::Format, "Magic number must be 'flaC'"); // "flaC"
  69. // Receive the streaminfo block
  70. auto streaminfo = TRY(next_meta_block(*bit_input));
  71. FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO");
  72. auto streaminfo_data_memory = LOADER_TRY(Core::Stream::MemoryStream::construct(streaminfo.data.bytes()));
  73. auto streaminfo_data = LOADER_TRY(BigEndianInputBitStream::construct(*streaminfo_data_memory));
  74. // STREAMINFO block
  75. m_min_block_size = LOADER_TRY(streaminfo_data->read_bits<u16>(16));
  76. FLAC_VERIFY(m_min_block_size >= 16, LoaderError::Category::Format, "Minimum block size must be 16");
  77. m_max_block_size = LOADER_TRY(streaminfo_data->read_bits<u16>(16));
  78. FLAC_VERIFY(m_max_block_size >= 16, LoaderError::Category::Format, "Maximum block size");
  79. m_min_frame_size = LOADER_TRY(streaminfo_data->read_bits<u32>(24));
  80. m_max_frame_size = LOADER_TRY(streaminfo_data->read_bits<u32>(24));
  81. m_sample_rate = LOADER_TRY(streaminfo_data->read_bits<u32>(20));
  82. FLAC_VERIFY(m_sample_rate <= 655350, LoaderError::Category::Format, "Sample rate");
  83. m_num_channels = LOADER_TRY(streaminfo_data->read_bits<u8>(3)) + 1; // 0 = one channel
  84. u8 bits_per_sample = LOADER_TRY(streaminfo_data->read_bits<u8>(5)) + 1;
  85. if (bits_per_sample == 8) {
  86. // FIXME: Signed/Unsigned issues?
  87. m_sample_format = PcmSampleFormat::Uint8;
  88. } else if (bits_per_sample == 16) {
  89. m_sample_format = PcmSampleFormat::Int16;
  90. } else if (bits_per_sample == 24) {
  91. m_sample_format = PcmSampleFormat::Int24;
  92. } else if (bits_per_sample == 32) {
  93. m_sample_format = PcmSampleFormat::Int32;
  94. } else {
  95. FLAC_VERIFY(false, LoaderError::Category::Format, "Sample bit depth invalid");
  96. }
  97. m_total_samples = LOADER_TRY(streaminfo_data->read_bits<u64>(36));
  98. FLAC_VERIFY(m_total_samples > 0, LoaderError::Category::Format, "Number of samples is zero");
  99. // Parse checksum into a buffer first
  100. [[maybe_unused]] u128 md5_checksum;
  101. VERIFY(streaminfo_data->is_aligned_to_byte_boundary());
  102. auto md5_bytes_read = LOADER_TRY(streaminfo_data->read(md5_checksum.bytes()));
  103. FLAC_VERIFY(md5_bytes_read.size() == md5_checksum.my_size(), LoaderError::Category::IO, "MD5 Checksum size");
  104. md5_checksum.bytes().copy_to({ m_md5_checksum, sizeof(m_md5_checksum) });
  105. // Parse other blocks
  106. [[maybe_unused]] u16 meta_blocks_parsed = 1;
  107. [[maybe_unused]] u16 total_meta_blocks = meta_blocks_parsed;
  108. FlacRawMetadataBlock block = streaminfo;
  109. while (!block.is_last_block) {
  110. block = TRY(next_meta_block(*bit_input));
  111. switch (block.type) {
  112. case (FlacMetadataBlockType::SEEKTABLE):
  113. TRY(load_seektable(block));
  114. break;
  115. default:
  116. // TODO: Parse the remaining metadata block types.
  117. // Currently only STREAMINFO and SEEKTABLE are handled.
  118. break;
  119. }
  120. ++total_meta_blocks;
  121. }
  122. 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<double>(m_total_samples) / static_cast<double>(m_sample_rate), md5_checksum, m_data_start_location, total_meta_blocks, total_meta_blocks - meta_blocks_parsed);
  123. return {};
  124. }
  125. MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block)
  126. {
  127. auto memory_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(block.data.bytes()));
  128. auto seektable_bytes = LOADER_TRY(BigEndianInputBitStream::construct(*memory_stream));
  129. for (size_t i = 0; i < block.length / 18; ++i) {
  130. FlacSeekPoint seekpoint {
  131. .sample_index = LOADER_TRY(seektable_bytes->read_bits<u64>(64)),
  132. .byte_offset = LOADER_TRY(seektable_bytes->read_bits<u64>(64)),
  133. .num_samples = LOADER_TRY(seektable_bytes->read_bits<u16>(16))
  134. };
  135. m_seektable.append(seekpoint);
  136. }
  137. dbgln_if(AFLACLOADER_DEBUG, "Loaded seektable of size {}", m_seektable.size());
  138. return {};
  139. }
  140. ErrorOr<FlacRawMetadataBlock, LoaderError> FlacLoaderPlugin::next_meta_block(BigEndianInputBitStream& bit_input)
  141. {
  142. bool is_last_block = LOADER_TRY(bit_input.read_bit());
  143. // The block type enum constants agree with the specification
  144. FlacMetadataBlockType type = (FlacMetadataBlockType)LOADER_TRY(bit_input.read_bits<u8>(7));
  145. m_data_start_location += 1;
  146. FLAC_VERIFY(type != FlacMetadataBlockType::INVALID, LoaderError::Category::Format, "Invalid metadata block");
  147. u32 block_length = LOADER_TRY(bit_input.read_bits<u32>(24));
  148. m_data_start_location += 3;
  149. // Blocks can be zero-sized, which would trip up the raw data reader below.
  150. if (block_length == 0)
  151. return FlacRawMetadataBlock {
  152. .is_last_block = is_last_block,
  153. .type = type,
  154. .length = 0,
  155. .data = LOADER_TRY(ByteBuffer::create_uninitialized(0))
  156. };
  157. auto block_data_result = ByteBuffer::create_uninitialized(block_length);
  158. FLAC_VERIFY(!block_data_result.is_error(), LoaderError::Category::IO, "Out of memory");
  159. auto block_data = block_data_result.release_value();
  160. // Reads exactly the bytes necessary into the Bytes container
  161. LOADER_TRY(bit_input.read(block_data));
  162. m_data_start_location += block_length;
  163. return FlacRawMetadataBlock {
  164. is_last_block,
  165. type,
  166. block_length,
  167. block_data,
  168. };
  169. }
  170. #undef FLAC_VERIFY
  171. MaybeLoaderError FlacLoaderPlugin::reset()
  172. {
  173. TRY(seek(m_data_start_location));
  174. m_current_frame.clear();
  175. return {};
  176. }
  177. MaybeLoaderError FlacLoaderPlugin::seek(int int_sample_index)
  178. {
  179. auto sample_index = static_cast<size_t>(int_sample_index);
  180. if (sample_index == m_loaded_samples)
  181. return {};
  182. auto maybe_target_seekpoint = m_seektable.last_matching([sample_index](auto& seekpoint) { return seekpoint.sample_index <= sample_index; });
  183. // No seektable or no fitting entry: Perform normal forward read
  184. if (!maybe_target_seekpoint.has_value()) {
  185. if (sample_index < m_loaded_samples) {
  186. LOADER_TRY(m_stream->seek(m_data_start_location, Core::Stream::SeekMode::SetPosition));
  187. m_loaded_samples = 0;
  188. }
  189. auto to_read = sample_index - m_loaded_samples;
  190. if (to_read == 0)
  191. return {};
  192. dbgln_if(AFLACLOADER_DEBUG, "Seeking {} samples manually", to_read);
  193. (void)TRY(get_more_samples(to_read));
  194. } else {
  195. auto target_seekpoint = maybe_target_seekpoint.release_value();
  196. // When a small seek happens, we may already be closer to the target than the seekpoint.
  197. if (sample_index - target_seekpoint.sample_index > sample_index - m_loaded_samples) {
  198. dbgln_if(AFLACLOADER_DEBUG, "Close enough to target: seeking {} samples manually", sample_index - m_loaded_samples);
  199. (void)TRY(get_more_samples(sample_index - m_loaded_samples));
  200. return {};
  201. }
  202. dbgln_if(AFLACLOADER_DEBUG, "Seeking to seektable: sample index {}, byte offset {}, sample count {}", target_seekpoint.sample_index, target_seekpoint.byte_offset, target_seekpoint.num_samples);
  203. auto position = target_seekpoint.byte_offset + m_data_start_location;
  204. if (m_stream->seek(static_cast<i64>(position), Core::Stream::SeekMode::SetPosition).is_error())
  205. return LoaderError { LoaderError::Category::IO, m_loaded_samples, String::formatted("Invalid seek position {}", position) };
  206. auto remaining_samples_after_seekpoint = sample_index - m_data_start_location;
  207. if (remaining_samples_after_seekpoint > 0)
  208. (void)TRY(get_more_samples(remaining_samples_after_seekpoint));
  209. m_loaded_samples = target_seekpoint.sample_index;
  210. }
  211. return {};
  212. }
  213. LoaderSamples FlacLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_input)
  214. {
  215. ssize_t remaining_samples = static_cast<ssize_t>(m_total_samples - m_loaded_samples);
  216. if (remaining_samples <= 0)
  217. return LegacyBuffer::create_empty();
  218. // FIXME: samples_to_read is calculated wrong, because when seeking not all samples are loaded.
  219. size_t samples_to_read = min(max_bytes_to_read_from_input, remaining_samples);
  220. auto samples = FixedArray<Sample>::must_create_but_fixme_should_propagate_errors(samples_to_read);
  221. size_t sample_index = 0;
  222. if (m_unread_data.size() > 0) {
  223. size_t to_transfer = min(m_unread_data.size(), samples_to_read);
  224. dbgln_if(AFLACLOADER_DEBUG, "Reading {} samples from unread sample buffer (size {})", to_transfer, m_unread_data.size());
  225. AK::TypedTransfer<Sample>::move(samples.data(), m_unread_data.data(), to_transfer);
  226. if (to_transfer < m_unread_data.size())
  227. m_unread_data.remove(0, to_transfer);
  228. else
  229. m_unread_data.clear_with_capacity();
  230. sample_index += to_transfer;
  231. }
  232. while (sample_index < samples_to_read) {
  233. TRY(next_frame(samples.span().slice(sample_index)));
  234. sample_index += m_current_frame->sample_count;
  235. }
  236. m_loaded_samples += sample_index;
  237. auto maybe_buffer = LegacyBuffer::create_with_samples(move(samples));
  238. if (maybe_buffer.is_error())
  239. return LoaderError { LoaderError::Category::Internal, m_loaded_samples, "Couldn't allocate sample buffer" };
  240. return maybe_buffer.release_value();
  241. }
  242. MaybeLoaderError FlacLoaderPlugin::next_frame(Span<Sample> target_vector)
  243. {
  244. #define FLAC_VERIFY(check, category, msg) \
  245. do { \
  246. if (!(check)) { \
  247. return LoaderError { category, static_cast<size_t>(m_current_sample_or_frame), String::formatted("FLAC header: {}", msg) }; \
  248. } \
  249. } while (0)
  250. auto bit_stream = LOADER_TRY(BigEndianInputBitStream::construct(*m_stream));
  251. // TODO: Check the CRC-16 checksum (and others) by keeping track of read data
  252. // FLAC frame sync code starts header
  253. u16 sync_code = LOADER_TRY(bit_stream->read_bits<u16>(14));
  254. FLAC_VERIFY(sync_code == 0b11111111111110, LoaderError::Category::Format, "Sync code");
  255. bool reserved_bit = LOADER_TRY(bit_stream->read_bit());
  256. FLAC_VERIFY(reserved_bit == 0, LoaderError::Category::Format, "Reserved frame header bit");
  257. [[maybe_unused]] bool blocking_strategy = LOADER_TRY(bit_stream->read_bit());
  258. u32 sample_count = TRY(convert_sample_count_code(LOADER_TRY(bit_stream->read_bits<u8>(4))));
  259. u32 frame_sample_rate = TRY(convert_sample_rate_code(LOADER_TRY(bit_stream->read_bits<u8>(4))));
  260. u8 channel_type_num = LOADER_TRY(bit_stream->read_bits<u8>(4));
  261. FLAC_VERIFY(channel_type_num < 0b1011, LoaderError::Category::Format, "Channel assignment");
  262. FlacFrameChannelType channel_type = (FlacFrameChannelType)channel_type_num;
  263. PcmSampleFormat bit_depth = TRY(convert_bit_depth_code(LOADER_TRY(bit_stream->read_bits<u8>(3))));
  264. reserved_bit = LOADER_TRY(bit_stream->read_bit());
  265. FLAC_VERIFY(reserved_bit == 0, LoaderError::Category::Format, "Reserved frame header end bit");
  266. // FIXME: sample number can be 8-56 bits, frame number can be 8-48 bits
  267. m_current_sample_or_frame = LOADER_TRY(read_utf8_char(*bit_stream));
  268. // Conditional header variables
  269. if (sample_count == FLAC_BLOCKSIZE_AT_END_OF_HEADER_8) {
  270. sample_count = LOADER_TRY(bit_stream->read_bits<u32>(8)) + 1;
  271. } else if (sample_count == FLAC_BLOCKSIZE_AT_END_OF_HEADER_16) {
  272. sample_count = LOADER_TRY(bit_stream->read_bits<u32>(16)) + 1;
  273. }
  274. if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_8) {
  275. frame_sample_rate = LOADER_TRY(bit_stream->read_bits<u32>(8)) * 1000;
  276. } else if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_16) {
  277. frame_sample_rate = LOADER_TRY(bit_stream->read_bits<u32>(16));
  278. } else if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10) {
  279. frame_sample_rate = LOADER_TRY(bit_stream->read_bits<u32>(16)) * 10;
  280. }
  281. // TODO: check header checksum, see above
  282. [[maybe_unused]] u8 checksum = LOADER_TRY(bit_stream->read_bits<u8>(8));
  283. dbgln_if(AFLACLOADER_DEBUG, "Frame: {} samples, {}bit {}Hz, channeltype {:x}, {} number {}, header checksum {}", sample_count, pcm_bits_per_sample(bit_depth), frame_sample_rate, channel_type_num, blocking_strategy ? "sample" : "frame", m_current_sample_or_frame, checksum);
  284. m_current_frame = FlacFrameHeader {
  285. sample_count,
  286. frame_sample_rate,
  287. channel_type,
  288. bit_depth,
  289. };
  290. u8 subframe_count = frame_channel_type_to_channel_count(channel_type);
  291. Vector<Vector<i32>> current_subframes;
  292. current_subframes.ensure_capacity(subframe_count);
  293. for (u8 i = 0; i < subframe_count; ++i) {
  294. FlacSubframeHeader new_subframe = TRY(next_subframe_header(*bit_stream, i));
  295. Vector<i32> subframe_samples = TRY(parse_subframe(new_subframe, *bit_stream));
  296. current_subframes.unchecked_append(move(subframe_samples));
  297. }
  298. bit_stream->align_to_byte_boundary();
  299. // TODO: check checksum, see above
  300. [[maybe_unused]] u16 footer_checksum = LOADER_TRY(bit_stream->read_bits<u16>(16));
  301. dbgln_if(AFLACLOADER_DEBUG, "Subframe footer checksum: {}", footer_checksum);
  302. Vector<i32> left;
  303. Vector<i32> right;
  304. switch (channel_type) {
  305. case FlacFrameChannelType::Mono:
  306. left = right = current_subframes[0];
  307. break;
  308. case FlacFrameChannelType::Stereo:
  309. // TODO mix together surround channels on each side?
  310. case FlacFrameChannelType::StereoCenter:
  311. case FlacFrameChannelType::Surround4p0:
  312. case FlacFrameChannelType::Surround5p0:
  313. case FlacFrameChannelType::Surround5p1:
  314. case FlacFrameChannelType::Surround6p1:
  315. case FlacFrameChannelType::Surround7p1:
  316. left = current_subframes[0];
  317. right = current_subframes[1];
  318. break;
  319. case FlacFrameChannelType::LeftSideStereo:
  320. // channels are left (0) and side (1)
  321. left = current_subframes[0];
  322. right.ensure_capacity(left.size());
  323. for (size_t i = 0; i < left.size(); ++i) {
  324. // right = left - side
  325. right.unchecked_append(left[i] - current_subframes[1][i]);
  326. }
  327. break;
  328. case FlacFrameChannelType::RightSideStereo:
  329. // channels are side (0) and right (1)
  330. right = current_subframes[1];
  331. left.ensure_capacity(right.size());
  332. for (size_t i = 0; i < right.size(); ++i) {
  333. // left = right + side
  334. left.unchecked_append(right[i] + current_subframes[0][i]);
  335. }
  336. break;
  337. case FlacFrameChannelType::MidSideStereo:
  338. // channels are mid (0) and side (1)
  339. left.ensure_capacity(current_subframes[0].size());
  340. right.ensure_capacity(current_subframes[0].size());
  341. for (size_t i = 0; i < current_subframes[0].size(); ++i) {
  342. i64 mid = current_subframes[0][i];
  343. i64 side = current_subframes[1][i];
  344. mid *= 2;
  345. // prevent integer division errors
  346. left.unchecked_append(static_cast<i32>((mid + side) / 2));
  347. right.unchecked_append(static_cast<i32>((mid - side) / 2));
  348. }
  349. break;
  350. }
  351. VERIFY(left.size() == right.size() && left.size() == m_current_frame->sample_count);
  352. double sample_rescale = static_cast<double>(1 << (pcm_bits_per_sample(m_current_frame->bit_depth) - 1));
  353. dbgln_if(AFLACLOADER_DEBUG, "Sample rescaled from {} bits: factor {:.1f}", pcm_bits_per_sample(m_current_frame->bit_depth), sample_rescale);
  354. // zip together channels
  355. auto samples_to_directly_copy = min(target_vector.size(), m_current_frame->sample_count);
  356. for (size_t i = 0; i < samples_to_directly_copy; ++i) {
  357. Sample frame = { left[i] / sample_rescale, right[i] / sample_rescale };
  358. target_vector[i] = frame;
  359. }
  360. // move superfluous data into the class buffer instead
  361. auto result = m_unread_data.try_grow_capacity(m_current_frame->sample_count - samples_to_directly_copy);
  362. if (result.is_error())
  363. return LoaderError { LoaderError::Category::Internal, static_cast<size_t>(samples_to_directly_copy + m_current_sample_or_frame), "Couldn't allocate sample buffer for superfluous data" };
  364. for (size_t i = samples_to_directly_copy; i < m_current_frame->sample_count; ++i) {
  365. Sample frame = { left[i] / sample_rescale, right[i] / sample_rescale };
  366. m_unread_data.unchecked_append(frame);
  367. }
  368. return {};
  369. #undef FLAC_VERIFY
  370. }
  371. ErrorOr<u32, LoaderError> FlacLoaderPlugin::convert_sample_count_code(u8 sample_count_code)
  372. {
  373. // single codes
  374. switch (sample_count_code) {
  375. case 0:
  376. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved block size" };
  377. case 1:
  378. return 192;
  379. case 6:
  380. return FLAC_BLOCKSIZE_AT_END_OF_HEADER_8;
  381. case 7:
  382. return FLAC_BLOCKSIZE_AT_END_OF_HEADER_16;
  383. }
  384. if (sample_count_code >= 2 && sample_count_code <= 5) {
  385. return 576 * AK::exp2(sample_count_code - 2);
  386. }
  387. return 256 * AK::exp2(sample_count_code - 8);
  388. }
  389. ErrorOr<u32, LoaderError> FlacLoaderPlugin::convert_sample_rate_code(u8 sample_rate_code)
  390. {
  391. switch (sample_rate_code) {
  392. case 0:
  393. return m_sample_rate;
  394. case 1:
  395. return 88200;
  396. case 2:
  397. return 176400;
  398. case 3:
  399. return 192000;
  400. case 4:
  401. return 8000;
  402. case 5:
  403. return 16000;
  404. case 6:
  405. return 22050;
  406. case 7:
  407. return 24000;
  408. case 8:
  409. return 32000;
  410. case 9:
  411. return 44100;
  412. case 10:
  413. return 48000;
  414. case 11:
  415. return 96000;
  416. case 12:
  417. return FLAC_SAMPLERATE_AT_END_OF_HEADER_8;
  418. case 13:
  419. return FLAC_SAMPLERATE_AT_END_OF_HEADER_16;
  420. case 14:
  421. return FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10;
  422. default:
  423. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Invalid sample rate code" };
  424. }
  425. }
  426. ErrorOr<PcmSampleFormat, LoaderError> FlacLoaderPlugin::convert_bit_depth_code(u8 bit_depth_code)
  427. {
  428. switch (bit_depth_code) {
  429. case 0:
  430. return m_sample_format;
  431. case 1:
  432. return PcmSampleFormat::Uint8;
  433. case 4:
  434. return PcmSampleFormat::Int16;
  435. case 6:
  436. return PcmSampleFormat::Int24;
  437. case 3:
  438. case 7:
  439. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved sample size" };
  440. default:
  441. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), String::formatted("Unsupported sample size {}", bit_depth_code) };
  442. }
  443. }
  444. u8 frame_channel_type_to_channel_count(FlacFrameChannelType channel_type)
  445. {
  446. if (channel_type <= FlacFrameChannelType::Surround7p1)
  447. return to_underlying(channel_type) + 1;
  448. return 2;
  449. }
  450. ErrorOr<FlacSubframeHeader, LoaderError> FlacLoaderPlugin::next_subframe_header(BigEndianInputBitStream& bit_stream, u8 channel_index)
  451. {
  452. u8 bits_per_sample = static_cast<u16>(pcm_bits_per_sample(m_current_frame->bit_depth));
  453. // For inter-channel correlation, the side channel needs an extra bit for its samples
  454. switch (m_current_frame->channels) {
  455. case FlacFrameChannelType::LeftSideStereo:
  456. case FlacFrameChannelType::MidSideStereo:
  457. if (channel_index == 1) {
  458. ++bits_per_sample;
  459. }
  460. break;
  461. case FlacFrameChannelType::RightSideStereo:
  462. if (channel_index == 0) {
  463. ++bits_per_sample;
  464. }
  465. break;
  466. // "normal" channel types
  467. default:
  468. break;
  469. }
  470. // zero-bit padding
  471. if (LOADER_TRY(bit_stream.read_bit()) != 0)
  472. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Zero bit padding" };
  473. // subframe type (encoding)
  474. u8 subframe_code = LOADER_TRY(bit_stream.read_bits<u8>(6));
  475. if ((subframe_code >= 0b000010 && subframe_code <= 0b000111) || (subframe_code > 0b001100 && subframe_code < 0b100000))
  476. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Subframe type" };
  477. FlacSubframeType subframe_type;
  478. u8 order = 0;
  479. // LPC has the highest bit set
  480. if ((subframe_code & 0b100000) > 0) {
  481. subframe_type = FlacSubframeType::LPC;
  482. order = (subframe_code & 0b011111) + 1;
  483. } else if ((subframe_code & 0b001000) > 0) {
  484. // Fixed has the third-highest bit set
  485. subframe_type = FlacSubframeType::Fixed;
  486. order = (subframe_code & 0b000111);
  487. } else {
  488. subframe_type = (FlacSubframeType)subframe_code;
  489. }
  490. // wasted bits per sample (unary encoding)
  491. bool has_wasted_bits = LOADER_TRY(bit_stream.read_bit());
  492. u8 k = 0;
  493. if (has_wasted_bits) {
  494. bool current_k_bit = 0;
  495. do {
  496. current_k_bit = LOADER_TRY(bit_stream.read_bit());
  497. ++k;
  498. } while (current_k_bit != 1);
  499. }
  500. return FlacSubframeHeader {
  501. subframe_type,
  502. order,
  503. k,
  504. bits_per_sample
  505. };
  506. }
  507. ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::parse_subframe(FlacSubframeHeader& subframe_header, BigEndianInputBitStream& bit_input)
  508. {
  509. Vector<i32> samples;
  510. switch (subframe_header.type) {
  511. case FlacSubframeType::Constant: {
  512. u64 constant_value = LOADER_TRY(bit_input.read_bits<u64>(subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample));
  513. dbgln_if(AFLACLOADER_DEBUG, "Constant subframe: {}", constant_value);
  514. samples.ensure_capacity(m_current_frame->sample_count);
  515. VERIFY(subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample != 0);
  516. i32 constant = sign_extend(static_cast<u32>(constant_value), subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample);
  517. for (u32 i = 0; i < m_current_frame->sample_count; ++i) {
  518. samples.unchecked_append(constant);
  519. }
  520. break;
  521. }
  522. case FlacSubframeType::Fixed: {
  523. dbgln_if(AFLACLOADER_DEBUG, "Fixed LPC subframe order {}", subframe_header.order);
  524. samples = TRY(decode_fixed_lpc(subframe_header, bit_input));
  525. break;
  526. }
  527. case FlacSubframeType::Verbatim: {
  528. dbgln_if(AFLACLOADER_DEBUG, "Verbatim subframe");
  529. samples = TRY(decode_verbatim(subframe_header, bit_input));
  530. break;
  531. }
  532. case FlacSubframeType::LPC: {
  533. dbgln_if(AFLACLOADER_DEBUG, "Custom LPC subframe order {}", subframe_header.order);
  534. samples = TRY(decode_custom_lpc(subframe_header, bit_input));
  535. break;
  536. }
  537. default:
  538. return LoaderError { LoaderError::Category::Unimplemented, static_cast<size_t>(m_current_sample_or_frame), "Unhandled FLAC subframe type" };
  539. }
  540. for (size_t i = 0; i < samples.size(); ++i) {
  541. samples[i] <<= subframe_header.wasted_bits_per_sample;
  542. }
  543. ResampleHelper<i32> resampler(m_current_frame->sample_rate, m_sample_rate);
  544. return resampler.resample(samples);
  545. }
  546. // Decode a subframe that isn't actually encoded, usually seen in random data
  547. ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_verbatim(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  548. {
  549. Vector<i32> decoded;
  550. decoded.ensure_capacity(m_current_frame->sample_count);
  551. VERIFY(subframe.bits_per_sample - subframe.wasted_bits_per_sample != 0);
  552. for (size_t i = 0; i < m_current_frame->sample_count; ++i) {
  553. decoded.unchecked_append(sign_extend(
  554. LOADER_TRY(bit_input.read_bits<u32>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
  555. subframe.bits_per_sample - subframe.wasted_bits_per_sample));
  556. }
  557. return decoded;
  558. }
  559. // Decode a subframe encoded with a custom linear predictor coding, i.e. the subframe provides the polynomial order and coefficients
  560. ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_custom_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  561. {
  562. Vector<i32> decoded;
  563. decoded.ensure_capacity(m_current_frame->sample_count);
  564. VERIFY(subframe.bits_per_sample - subframe.wasted_bits_per_sample != 0);
  565. // warm-up samples
  566. for (auto i = 0; i < subframe.order; ++i) {
  567. decoded.unchecked_append(sign_extend(
  568. LOADER_TRY(bit_input.read_bits<u32>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
  569. subframe.bits_per_sample - subframe.wasted_bits_per_sample));
  570. }
  571. // precision of the coefficients
  572. u8 lpc_precision = LOADER_TRY(bit_input.read_bits<u8>(4));
  573. if (lpc_precision == 0b1111)
  574. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Invalid linear predictor coefficient precision" };
  575. lpc_precision += 1;
  576. // shift needed on the data (signed!)
  577. i8 lpc_shift = sign_extend(LOADER_TRY(bit_input.read_bits<u8>(5)), 5);
  578. Vector<i32> coefficients;
  579. coefficients.ensure_capacity(subframe.order);
  580. // read coefficients
  581. for (auto i = 0; i < subframe.order; ++i) {
  582. u32 raw_coefficient = LOADER_TRY(bit_input.read_bits<u32>(lpc_precision));
  583. i32 coefficient = static_cast<i32>(sign_extend(raw_coefficient, lpc_precision));
  584. coefficients.unchecked_append(coefficient);
  585. }
  586. dbgln_if(AFLACLOADER_DEBUG, "{}-bit {} shift coefficients: {}", lpc_precision, lpc_shift, coefficients);
  587. TRY(decode_residual(decoded, subframe, bit_input));
  588. // approximate the waveform with the predictor
  589. for (size_t i = subframe.order; i < m_current_frame->sample_count; ++i) {
  590. // (see below)
  591. i64 sample = 0;
  592. for (size_t t = 0; t < subframe.order; ++t) {
  593. // It's really important that we compute in 64-bit land here.
  594. // Even though FLAC operates at a maximum bit depth of 32 bits, modern encoders use super-large coefficients for maximum compression.
  595. // These will easily overflow 32 bits and cause strange white noise that abruptly stops intermittently (at the end of a frame).
  596. // The simple fix of course is to do intermediate computations in 64 bits.
  597. sample += static_cast<i64>(coefficients[t]) * static_cast<i64>(decoded[i - t - 1]);
  598. }
  599. decoded[i] += sample >> lpc_shift;
  600. }
  601. return decoded;
  602. }
  603. // Decode a subframe encoded with one of the fixed linear predictor codings
  604. ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_fixed_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  605. {
  606. Vector<i32> decoded;
  607. decoded.ensure_capacity(m_current_frame->sample_count);
  608. VERIFY(subframe.bits_per_sample - subframe.wasted_bits_per_sample != 0);
  609. // warm-up samples
  610. for (auto i = 0; i < subframe.order; ++i) {
  611. decoded.unchecked_append(sign_extend(
  612. LOADER_TRY(bit_input.read_bits<u32>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
  613. subframe.bits_per_sample - subframe.wasted_bits_per_sample));
  614. }
  615. TRY(decode_residual(decoded, subframe, bit_input));
  616. dbgln_if(AFLACLOADER_DEBUG, "decoded length {}, {} order predictor", decoded.size(), subframe.order);
  617. switch (subframe.order) {
  618. case 0:
  619. // s_0(t) = 0
  620. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  621. decoded[i] += 0;
  622. break;
  623. case 1:
  624. // s_1(t) = s(t-1)
  625. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  626. decoded[i] += decoded[i - 1];
  627. break;
  628. case 2:
  629. // s_2(t) = 2s(t-1) - s(t-2)
  630. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  631. decoded[i] += 2 * decoded[i - 1] - decoded[i - 2];
  632. break;
  633. case 3:
  634. // s_3(t) = 3s(t-1) - 3s(t-2) + s(t-3)
  635. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  636. decoded[i] += 3 * decoded[i - 1] - 3 * decoded[i - 2] + decoded[i - 3];
  637. break;
  638. case 4:
  639. // s_4(t) = 4s(t-1) - 6s(t-2) + 4s(t-3) - s(t-4)
  640. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  641. decoded[i] += 4 * decoded[i - 1] - 6 * decoded[i - 2] + 4 * decoded[i - 3] - decoded[i - 4];
  642. break;
  643. default:
  644. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), String::formatted("Unrecognized predictor order {}", subframe.order) };
  645. }
  646. return decoded;
  647. }
  648. // Decode the residual, the "error" between the function approximation and the actual audio data
  649. MaybeLoaderError FlacLoaderPlugin::decode_residual(Vector<i32>& decoded, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  650. {
  651. auto residual_mode = static_cast<FlacResidualMode>(LOADER_TRY(bit_input.read_bits<u8>(2)));
  652. u8 partition_order = LOADER_TRY(bit_input.read_bits<u8>(4));
  653. size_t partitions = 1 << partition_order;
  654. if (residual_mode == FlacResidualMode::Rice4Bit) {
  655. // decode a single Rice partition with four bits for the order k
  656. for (size_t i = 0; i < partitions; ++i) {
  657. auto rice_partition = TRY(decode_rice_partition(4, partitions, i, subframe, bit_input));
  658. decoded.extend(move(rice_partition));
  659. }
  660. } else if (residual_mode == FlacResidualMode::Rice5Bit) {
  661. // five bits equivalent
  662. for (size_t i = 0; i < partitions; ++i) {
  663. auto rice_partition = TRY(decode_rice_partition(5, partitions, i, subframe, bit_input));
  664. decoded.extend(move(rice_partition));
  665. }
  666. } else
  667. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved residual coding method" };
  668. return {};
  669. }
  670. // Decode a single Rice partition as part of the residual, every partition can have its own Rice parameter k
  671. ALWAYS_INLINE ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_rice_partition(u8 partition_type, u32 partitions, u32 partition_index, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  672. {
  673. // Rice parameter / Exp-Golomb order
  674. u8 k = LOADER_TRY(bit_input.read_bits<u8>(partition_type));
  675. u32 residual_sample_count;
  676. if (partitions == 0)
  677. residual_sample_count = m_current_frame->sample_count - subframe.order;
  678. else
  679. residual_sample_count = m_current_frame->sample_count / partitions;
  680. if (partition_index == 0)
  681. residual_sample_count -= subframe.order;
  682. Vector<i32> rice_partition;
  683. rice_partition.resize(residual_sample_count);
  684. // escape code for unencoded binary partition
  685. if (k == (1 << partition_type) - 1) {
  686. u8 unencoded_bps = LOADER_TRY(bit_input.read_bits<u8>(5));
  687. for (size_t r = 0; r < residual_sample_count; ++r) {
  688. rice_partition[r] = LOADER_TRY(bit_input.read_bits<u8>(unencoded_bps));
  689. }
  690. } else {
  691. for (size_t r = 0; r < residual_sample_count; ++r) {
  692. rice_partition[r] = LOADER_TRY(decode_unsigned_exp_golomb(k, bit_input));
  693. }
  694. }
  695. return rice_partition;
  696. }
  697. // Decode a single number encoded with Rice/Exponential-Golomb encoding (the unsigned variant)
  698. ALWAYS_INLINE ErrorOr<i32> decode_unsigned_exp_golomb(u8 k, BigEndianInputBitStream& bit_input)
  699. {
  700. u8 q = 0;
  701. while (TRY(bit_input.read_bit()) == 0)
  702. ++q;
  703. // least significant bits (remainder)
  704. u32 rem = TRY(bit_input.read_bits<u32>(k));
  705. u32 value = q << k | rem;
  706. return rice_to_signed(value);
  707. }
  708. ErrorOr<u64> read_utf8_char(BigEndianInputBitStream& input)
  709. {
  710. u64 character;
  711. u8 buffer = 0;
  712. Bytes buffer_bytes { &buffer, 1 };
  713. TRY(input.read(buffer_bytes));
  714. u8 start_byte = buffer_bytes[0];
  715. // Signal byte is zero: ASCII character
  716. if ((start_byte & 0b10000000) == 0) {
  717. return start_byte;
  718. } else if ((start_byte & 0b11000000) == 0b10000000) {
  719. return Error::from_string_literal("Illegal continuation byte"sv);
  720. }
  721. // This algorithm is too good and supports the theoretical max 0xFF start byte
  722. u8 length = 1;
  723. while (((start_byte << length) & 0b10000000) == 0b10000000)
  724. ++length;
  725. u8 bits_from_start_byte = 8 - (length + 1);
  726. u8 start_byte_bitmask = AK::exp2(bits_from_start_byte) - 1;
  727. character = start_byte_bitmask & start_byte;
  728. for (u8 i = length - 1; i > 0; --i) {
  729. TRY(input.read(buffer_bytes));
  730. u8 current_byte = buffer_bytes[0];
  731. character = (character << 6) | (current_byte & 0b00111111);
  732. }
  733. return character;
  734. }
  735. i64 sign_extend(u32 n, u8 size)
  736. {
  737. // negative
  738. if ((n & (1 << (size - 1))) > 0) {
  739. return static_cast<i64>(n | (0xffffffff << size));
  740. }
  741. // positive
  742. return n;
  743. }
  744. i32 rice_to_signed(u32 x)
  745. {
  746. // positive numbers are even, negative numbers are odd
  747. // bitmask for conditionally inverting the entire number, thereby "negating" it
  748. i32 sign = -static_cast<i32>(x & 1);
  749. // copies the sign's sign onto the actual magnitude of x
  750. return static_cast<i32>(sign ^ (x >> 1));
  751. }
  752. }