FlacLoader.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 == 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. // TODO: For a simple first implementation, all other blocks are skipped as allowed by the FLAC specification.
  107. // Especially the SEEKTABLE block may become useful in a more sophisticated version.
  108. [[maybe_unused]] u16 meta_blocks_parsed = 1;
  109. [[maybe_unused]] u16 total_meta_blocks = meta_blocks_parsed;
  110. FlacRawMetadataBlock block = streaminfo;
  111. while (!block.is_last_block) {
  112. block = TRY(next_meta_block(*bit_input));
  113. ++total_meta_blocks;
  114. }
  115. 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);
  116. return {};
  117. }
  118. ErrorOr<FlacRawMetadataBlock, LoaderError> FlacLoaderPlugin::next_meta_block(BigEndianInputBitStream& bit_input)
  119. {
  120. bool is_last_block = LOADER_TRY(bit_input.read_bit());
  121. // The block type enum constants agree with the specification
  122. FlacMetadataBlockType type = (FlacMetadataBlockType)LOADER_TRY(bit_input.read_bits<u8>(7));
  123. m_data_start_location += 1;
  124. FLAC_VERIFY(type != FlacMetadataBlockType::INVALID, LoaderError::Category::Format, "Invalid metadata block");
  125. u32 block_length = LOADER_TRY(bit_input.read_bits<u32>(24));
  126. m_data_start_location += 3;
  127. auto block_data_result = ByteBuffer::create_uninitialized(block_length);
  128. FLAC_VERIFY(!block_data_result.is_error(), LoaderError::Category::IO, "Out of memory");
  129. auto block_data = block_data_result.release_value();
  130. // Reads exactly the bytes necessary into the Bytes container
  131. LOADER_TRY(bit_input.read(block_data));
  132. m_data_start_location += block_length;
  133. return FlacRawMetadataBlock {
  134. is_last_block,
  135. type,
  136. block_length,
  137. block_data,
  138. };
  139. }
  140. #undef FLAC_VERIFY
  141. MaybeLoaderError FlacLoaderPlugin::reset()
  142. {
  143. TRY(seek(m_data_start_location));
  144. m_current_frame.clear();
  145. return {};
  146. }
  147. MaybeLoaderError FlacLoaderPlugin::seek(const int position)
  148. {
  149. if (m_stream->seek(position, Core::Stream::SeekMode::SetPosition).is_error())
  150. return LoaderError { LoaderError::Category::IO, m_loaded_samples, String::formatted("Invalid seek position {}", position) };
  151. return {};
  152. }
  153. LoaderSamples FlacLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_input)
  154. {
  155. ssize_t remaining_samples = static_cast<ssize_t>(m_total_samples - m_loaded_samples);
  156. if (remaining_samples <= 0)
  157. return Buffer::create_empty();
  158. size_t samples_to_read = min(max_bytes_to_read_from_input, remaining_samples);
  159. auto samples = FixedArray<Sample>::must_create_but_fixme_should_propagate_errors(samples_to_read);
  160. size_t sample_index = 0;
  161. if (m_unread_data.size() > 0) {
  162. size_t to_transfer = min(m_unread_data.size(), samples_to_read);
  163. dbgln_if(AFLACLOADER_DEBUG, "Reading {} samples from unread sample buffer (size {})", to_transfer, m_unread_data.size());
  164. AK::TypedTransfer<Sample>::move(samples.data(), m_unread_data.data(), to_transfer);
  165. if (to_transfer < m_unread_data.size())
  166. m_unread_data.remove(0, to_transfer);
  167. else
  168. m_unread_data.clear_with_capacity();
  169. sample_index += to_transfer;
  170. }
  171. while (sample_index < samples_to_read) {
  172. TRY(next_frame(samples.span().slice(sample_index)));
  173. sample_index += m_current_frame->sample_count;
  174. }
  175. m_loaded_samples += sample_index;
  176. auto maybe_buffer = Buffer::create_with_samples(move(samples));
  177. if (maybe_buffer.is_error())
  178. return LoaderError { LoaderError::Category::Internal, m_loaded_samples, "Couldn't allocate sample buffer" };
  179. return maybe_buffer.release_value();
  180. }
  181. MaybeLoaderError FlacLoaderPlugin::next_frame(Span<Sample> target_vector)
  182. {
  183. #define FLAC_VERIFY(check, category, msg) \
  184. do { \
  185. if (!(check)) { \
  186. return LoaderError { category, static_cast<size_t>(m_current_sample_or_frame), String::formatted("FLAC header: {}", msg) }; \
  187. } \
  188. } while (0)
  189. auto bit_stream = LOADER_TRY(BigEndianInputBitStream::construct(*m_stream));
  190. // TODO: Check the CRC-16 checksum (and others) by keeping track of read data
  191. // FLAC frame sync code starts header
  192. u16 sync_code = LOADER_TRY(bit_stream->read_bits<u16>(14));
  193. FLAC_VERIFY(sync_code == 0b11111111111110, LoaderError::Category::Format, "Sync code");
  194. bool reserved_bit = LOADER_TRY(bit_stream->read_bit());
  195. FLAC_VERIFY(reserved_bit == 0, LoaderError::Category::Format, "Reserved frame header bit");
  196. [[maybe_unused]] bool blocking_strategy = LOADER_TRY(bit_stream->read_bit());
  197. u32 sample_count = TRY(convert_sample_count_code(LOADER_TRY(bit_stream->read_bits<u8>(4))));
  198. u32 frame_sample_rate = TRY(convert_sample_rate_code(LOADER_TRY(bit_stream->read_bits<u8>(4))));
  199. u8 channel_type_num = LOADER_TRY(bit_stream->read_bits<u8>(4));
  200. FLAC_VERIFY(channel_type_num < 0b1011, LoaderError::Category::Format, "Channel assignment");
  201. FlacFrameChannelType channel_type = (FlacFrameChannelType)channel_type_num;
  202. PcmSampleFormat bit_depth = TRY(convert_bit_depth_code(LOADER_TRY(bit_stream->read_bits<u8>(3))));
  203. reserved_bit = LOADER_TRY(bit_stream->read_bit());
  204. FLAC_VERIFY(reserved_bit == 0, LoaderError::Category::Format, "Reserved frame header end bit");
  205. // FIXME: sample number can be 8-56 bits, frame number can be 8-48 bits
  206. m_current_sample_or_frame = LOADER_TRY(read_utf8_char(*bit_stream));
  207. // Conditional header variables
  208. if (sample_count == FLAC_BLOCKSIZE_AT_END_OF_HEADER_8) {
  209. sample_count = LOADER_TRY(bit_stream->read_bits<u32>(8)) + 1;
  210. } else if (sample_count == FLAC_BLOCKSIZE_AT_END_OF_HEADER_16) {
  211. sample_count = LOADER_TRY(bit_stream->read_bits<u32>(16)) + 1;
  212. }
  213. if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_8) {
  214. frame_sample_rate = LOADER_TRY(bit_stream->read_bits<u32>(8)) * 1000;
  215. } else if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_16) {
  216. frame_sample_rate = LOADER_TRY(bit_stream->read_bits<u32>(16));
  217. } else if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10) {
  218. frame_sample_rate = LOADER_TRY(bit_stream->read_bits<u32>(16)) * 10;
  219. }
  220. // TODO: check header checksum, see above
  221. [[maybe_unused]] u8 checksum = LOADER_TRY(bit_stream->read_bits<u8>(8));
  222. 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);
  223. m_current_frame = FlacFrameHeader {
  224. sample_count,
  225. frame_sample_rate,
  226. channel_type,
  227. bit_depth,
  228. };
  229. u8 subframe_count = frame_channel_type_to_channel_count(channel_type);
  230. Vector<Vector<i32>> current_subframes;
  231. current_subframes.ensure_capacity(subframe_count);
  232. for (u8 i = 0; i < subframe_count; ++i) {
  233. FlacSubframeHeader new_subframe = TRY(next_subframe_header(*bit_stream, i));
  234. Vector<i32> subframe_samples = TRY(parse_subframe(new_subframe, *bit_stream));
  235. current_subframes.unchecked_append(move(subframe_samples));
  236. }
  237. bit_stream->align_to_byte_boundary();
  238. // TODO: check checksum, see above
  239. [[maybe_unused]] u16 footer_checksum = LOADER_TRY(bit_stream->read_bits<u16>(16));
  240. dbgln_if(AFLACLOADER_DEBUG, "Subframe footer checksum: {}", footer_checksum);
  241. Vector<i32> left;
  242. Vector<i32> right;
  243. switch (channel_type) {
  244. case FlacFrameChannelType::Mono:
  245. left = right = current_subframes[0];
  246. break;
  247. case FlacFrameChannelType::Stereo:
  248. // TODO mix together surround channels on each side?
  249. case FlacFrameChannelType::StereoCenter:
  250. case FlacFrameChannelType::Surround4p0:
  251. case FlacFrameChannelType::Surround5p0:
  252. case FlacFrameChannelType::Surround5p1:
  253. case FlacFrameChannelType::Surround6p1:
  254. case FlacFrameChannelType::Surround7p1:
  255. left = current_subframes[0];
  256. right = current_subframes[1];
  257. break;
  258. case FlacFrameChannelType::LeftSideStereo:
  259. // channels are left (0) and side (1)
  260. left = current_subframes[0];
  261. right.ensure_capacity(left.size());
  262. for (size_t i = 0; i < left.size(); ++i) {
  263. // right = left - side
  264. right.unchecked_append(left[i] - current_subframes[1][i]);
  265. }
  266. break;
  267. case FlacFrameChannelType::RightSideStereo:
  268. // channels are side (0) and right (1)
  269. right = current_subframes[1];
  270. left.ensure_capacity(right.size());
  271. for (size_t i = 0; i < right.size(); ++i) {
  272. // left = right + side
  273. left.unchecked_append(right[i] + current_subframes[0][i]);
  274. }
  275. break;
  276. case FlacFrameChannelType::MidSideStereo:
  277. // channels are mid (0) and side (1)
  278. left.ensure_capacity(current_subframes[0].size());
  279. right.ensure_capacity(current_subframes[0].size());
  280. for (size_t i = 0; i < current_subframes[0].size(); ++i) {
  281. i64 mid = current_subframes[0][i];
  282. i64 side = current_subframes[1][i];
  283. mid *= 2;
  284. // prevent integer division errors
  285. left.unchecked_append(static_cast<i32>((mid + side) / 2));
  286. right.unchecked_append(static_cast<i32>((mid - side) / 2));
  287. }
  288. break;
  289. }
  290. VERIFY(left.size() == right.size() && left.size() == m_current_frame->sample_count);
  291. double sample_rescale = static_cast<double>(1 << (pcm_bits_per_sample(m_current_frame->bit_depth) - 1));
  292. dbgln_if(AFLACLOADER_DEBUG, "Sample rescaled from {} bits: factor {:.1f}", pcm_bits_per_sample(m_current_frame->bit_depth), sample_rescale);
  293. // zip together channels
  294. auto samples_to_directly_copy = min(target_vector.size(), m_current_frame->sample_count);
  295. for (size_t i = 0; i < samples_to_directly_copy; ++i) {
  296. Sample frame = { left[i] / sample_rescale, right[i] / sample_rescale };
  297. target_vector[i] = frame;
  298. }
  299. // move superfluous data into the class buffer instead
  300. auto result = m_unread_data.try_grow_capacity(m_current_frame->sample_count - samples_to_directly_copy);
  301. if (result.is_error())
  302. 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" };
  303. for (size_t i = samples_to_directly_copy; i < m_current_frame->sample_count; ++i) {
  304. Sample frame = { left[i] / sample_rescale, right[i] / sample_rescale };
  305. m_unread_data.unchecked_append(frame);
  306. }
  307. return {};
  308. #undef FLAC_VERIFY
  309. }
  310. ErrorOr<u32, LoaderError> FlacLoaderPlugin::convert_sample_count_code(u8 sample_count_code)
  311. {
  312. // single codes
  313. switch (sample_count_code) {
  314. case 0:
  315. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved block size" };
  316. case 1:
  317. return 192;
  318. case 6:
  319. return FLAC_BLOCKSIZE_AT_END_OF_HEADER_8;
  320. case 7:
  321. return FLAC_BLOCKSIZE_AT_END_OF_HEADER_16;
  322. }
  323. if (sample_count_code >= 2 && sample_count_code <= 5) {
  324. return 576 * AK::exp2(sample_count_code - 2);
  325. }
  326. return 256 * AK::exp2(sample_count_code - 8);
  327. }
  328. ErrorOr<u32, LoaderError> FlacLoaderPlugin::convert_sample_rate_code(u8 sample_rate_code)
  329. {
  330. switch (sample_rate_code) {
  331. case 0:
  332. return m_sample_rate;
  333. case 1:
  334. return 88200;
  335. case 2:
  336. return 176400;
  337. case 3:
  338. return 192000;
  339. case 4:
  340. return 8000;
  341. case 5:
  342. return 16000;
  343. case 6:
  344. return 22050;
  345. case 7:
  346. return 24000;
  347. case 8:
  348. return 32000;
  349. case 9:
  350. return 44100;
  351. case 10:
  352. return 48000;
  353. case 11:
  354. return 96000;
  355. case 12:
  356. return FLAC_SAMPLERATE_AT_END_OF_HEADER_8;
  357. case 13:
  358. return FLAC_SAMPLERATE_AT_END_OF_HEADER_16;
  359. case 14:
  360. return FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10;
  361. default:
  362. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Invalid sample rate code" };
  363. }
  364. }
  365. ErrorOr<PcmSampleFormat, LoaderError> FlacLoaderPlugin::convert_bit_depth_code(u8 bit_depth_code)
  366. {
  367. switch (bit_depth_code) {
  368. case 0:
  369. return m_sample_format;
  370. case 1:
  371. return PcmSampleFormat::Uint8;
  372. case 4:
  373. return PcmSampleFormat::Int16;
  374. case 6:
  375. return PcmSampleFormat::Int24;
  376. case 3:
  377. case 7:
  378. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved sample size" };
  379. default:
  380. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), String::formatted("Unsupported sample size {}", bit_depth_code) };
  381. }
  382. }
  383. u8 frame_channel_type_to_channel_count(FlacFrameChannelType channel_type)
  384. {
  385. if (channel_type <= FlacFrameChannelType::Surround7p1)
  386. return to_underlying(channel_type) + 1;
  387. return 2;
  388. }
  389. ErrorOr<FlacSubframeHeader, LoaderError> FlacLoaderPlugin::next_subframe_header(BigEndianInputBitStream& bit_stream, u8 channel_index)
  390. {
  391. u8 bits_per_sample = static_cast<u16>(pcm_bits_per_sample(m_current_frame->bit_depth));
  392. // For inter-channel correlation, the side channel needs an extra bit for its samples
  393. switch (m_current_frame->channels) {
  394. case FlacFrameChannelType::LeftSideStereo:
  395. case FlacFrameChannelType::MidSideStereo:
  396. if (channel_index == 1) {
  397. ++bits_per_sample;
  398. }
  399. break;
  400. case FlacFrameChannelType::RightSideStereo:
  401. if (channel_index == 0) {
  402. ++bits_per_sample;
  403. }
  404. break;
  405. // "normal" channel types
  406. default:
  407. break;
  408. }
  409. // zero-bit padding
  410. if (LOADER_TRY(bit_stream.read_bit()) != 0)
  411. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Zero bit padding" };
  412. // subframe type (encoding)
  413. u8 subframe_code = LOADER_TRY(bit_stream.read_bits<u8>(6));
  414. if ((subframe_code >= 0b000010 && subframe_code <= 0b000111) || (subframe_code > 0b001100 && subframe_code < 0b100000))
  415. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Subframe type" };
  416. FlacSubframeType subframe_type;
  417. u8 order = 0;
  418. // LPC has the highest bit set
  419. if ((subframe_code & 0b100000) > 0) {
  420. subframe_type = FlacSubframeType::LPC;
  421. order = (subframe_code & 0b011111) + 1;
  422. } else if ((subframe_code & 0b001000) > 0) {
  423. // Fixed has the third-highest bit set
  424. subframe_type = FlacSubframeType::Fixed;
  425. order = (subframe_code & 0b000111);
  426. } else {
  427. subframe_type = (FlacSubframeType)subframe_code;
  428. }
  429. // wasted bits per sample (unary encoding)
  430. bool has_wasted_bits = LOADER_TRY(bit_stream.read_bit());
  431. u8 k = 0;
  432. if (has_wasted_bits) {
  433. bool current_k_bit = 0;
  434. do {
  435. current_k_bit = LOADER_TRY(bit_stream.read_bit());
  436. ++k;
  437. } while (current_k_bit != 1);
  438. }
  439. return FlacSubframeHeader {
  440. subframe_type,
  441. order,
  442. k,
  443. bits_per_sample
  444. };
  445. }
  446. ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::parse_subframe(FlacSubframeHeader& subframe_header, BigEndianInputBitStream& bit_input)
  447. {
  448. Vector<i32> samples;
  449. switch (subframe_header.type) {
  450. case FlacSubframeType::Constant: {
  451. u64 constant_value = LOADER_TRY(bit_input.read_bits<u64>(subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample));
  452. dbgln_if(AFLACLOADER_DEBUG, "Constant subframe: {}", constant_value);
  453. samples.ensure_capacity(m_current_frame->sample_count);
  454. VERIFY(subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample != 0);
  455. i32 constant = sign_extend(static_cast<u32>(constant_value), subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample);
  456. for (u32 i = 0; i < m_current_frame->sample_count; ++i) {
  457. samples.unchecked_append(constant);
  458. }
  459. break;
  460. }
  461. case FlacSubframeType::Fixed: {
  462. dbgln_if(AFLACLOADER_DEBUG, "Fixed LPC subframe order {}", subframe_header.order);
  463. samples = TRY(decode_fixed_lpc(subframe_header, bit_input));
  464. break;
  465. }
  466. case FlacSubframeType::Verbatim: {
  467. dbgln_if(AFLACLOADER_DEBUG, "Verbatim subframe");
  468. samples = TRY(decode_verbatim(subframe_header, bit_input));
  469. break;
  470. }
  471. case FlacSubframeType::LPC: {
  472. dbgln_if(AFLACLOADER_DEBUG, "Custom LPC subframe order {}", subframe_header.order);
  473. samples = TRY(decode_custom_lpc(subframe_header, bit_input));
  474. break;
  475. }
  476. default:
  477. return LoaderError { LoaderError::Category::Unimplemented, static_cast<size_t>(m_current_sample_or_frame), "Unhandled FLAC subframe type" };
  478. }
  479. for (size_t i = 0; i < samples.size(); ++i) {
  480. samples[i] <<= subframe_header.wasted_bits_per_sample;
  481. }
  482. ResampleHelper<i32> resampler(m_current_frame->sample_rate, m_sample_rate);
  483. return resampler.resample(samples);
  484. }
  485. // Decode a subframe that isn't actually encoded, usually seen in random data
  486. ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_verbatim(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  487. {
  488. Vector<i32> decoded;
  489. decoded.ensure_capacity(m_current_frame->sample_count);
  490. VERIFY(subframe.bits_per_sample - subframe.wasted_bits_per_sample != 0);
  491. for (size_t i = 0; i < m_current_frame->sample_count; ++i) {
  492. decoded.unchecked_append(sign_extend(
  493. LOADER_TRY(bit_input.read_bits<u32>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
  494. subframe.bits_per_sample - subframe.wasted_bits_per_sample));
  495. }
  496. return decoded;
  497. }
  498. // Decode a subframe encoded with a custom linear predictor coding, i.e. the subframe provides the polynomial order and coefficients
  499. ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_custom_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  500. {
  501. Vector<i32> decoded;
  502. decoded.ensure_capacity(m_current_frame->sample_count);
  503. VERIFY(subframe.bits_per_sample - subframe.wasted_bits_per_sample != 0);
  504. // warm-up samples
  505. for (auto i = 0; i < subframe.order; ++i) {
  506. decoded.unchecked_append(sign_extend(
  507. LOADER_TRY(bit_input.read_bits<u32>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
  508. subframe.bits_per_sample - subframe.wasted_bits_per_sample));
  509. }
  510. // precision of the coefficients
  511. u8 lpc_precision = LOADER_TRY(bit_input.read_bits<u8>(4));
  512. if (lpc_precision == 0b1111)
  513. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Invalid linear predictor coefficient precision" };
  514. lpc_precision += 1;
  515. // shift needed on the data (signed!)
  516. i8 lpc_shift = sign_extend(LOADER_TRY(bit_input.read_bits<u8>(5)), 5);
  517. Vector<i32> coefficients;
  518. coefficients.ensure_capacity(subframe.order);
  519. // read coefficients
  520. for (auto i = 0; i < subframe.order; ++i) {
  521. u32 raw_coefficient = LOADER_TRY(bit_input.read_bits<u32>(lpc_precision));
  522. i32 coefficient = static_cast<i32>(sign_extend(raw_coefficient, lpc_precision));
  523. coefficients.unchecked_append(coefficient);
  524. }
  525. dbgln_if(AFLACLOADER_DEBUG, "{}-bit {} shift coefficients: {}", lpc_precision, lpc_shift, coefficients);
  526. TRY(decode_residual(decoded, subframe, bit_input));
  527. // approximate the waveform with the predictor
  528. for (size_t i = subframe.order; i < m_current_frame->sample_count; ++i) {
  529. // (see below)
  530. i64 sample = 0;
  531. for (size_t t = 0; t < subframe.order; ++t) {
  532. // It's really important that we compute in 64-bit land here.
  533. // Even though FLAC operates at a maximum bit depth of 32 bits, modern encoders use super-large coefficients for maximum compression.
  534. // These will easily overflow 32 bits and cause strange white noise that abruptly stops intermittently (at the end of a frame).
  535. // The simple fix of course is to do intermediate computations in 64 bits.
  536. sample += static_cast<i64>(coefficients[t]) * static_cast<i64>(decoded[i - t - 1]);
  537. }
  538. decoded[i] += sample >> lpc_shift;
  539. }
  540. return decoded;
  541. }
  542. // Decode a subframe encoded with one of the fixed linear predictor codings
  543. ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_fixed_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  544. {
  545. Vector<i32> decoded;
  546. decoded.ensure_capacity(m_current_frame->sample_count);
  547. VERIFY(subframe.bits_per_sample - subframe.wasted_bits_per_sample != 0);
  548. // warm-up samples
  549. for (auto i = 0; i < subframe.order; ++i) {
  550. decoded.unchecked_append(sign_extend(
  551. LOADER_TRY(bit_input.read_bits<u32>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
  552. subframe.bits_per_sample - subframe.wasted_bits_per_sample));
  553. }
  554. TRY(decode_residual(decoded, subframe, bit_input));
  555. dbgln_if(AFLACLOADER_DEBUG, "decoded length {}, {} order predictor", decoded.size(), subframe.order);
  556. switch (subframe.order) {
  557. case 0:
  558. // s_0(t) = 0
  559. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  560. decoded[i] += 0;
  561. break;
  562. case 1:
  563. // s_1(t) = s(t-1)
  564. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  565. decoded[i] += decoded[i - 1];
  566. break;
  567. case 2:
  568. // s_2(t) = 2s(t-1) - s(t-2)
  569. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  570. decoded[i] += 2 * decoded[i - 1] - decoded[i - 2];
  571. break;
  572. case 3:
  573. // s_3(t) = 3s(t-1) - 3s(t-2) + s(t-3)
  574. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  575. decoded[i] += 3 * decoded[i - 1] - 3 * decoded[i - 2] + decoded[i - 3];
  576. break;
  577. case 4:
  578. // s_4(t) = 4s(t-1) - 6s(t-2) + 4s(t-3) - s(t-4)
  579. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  580. decoded[i] += 4 * decoded[i - 1] - 6 * decoded[i - 2] + 4 * decoded[i - 3] - decoded[i - 4];
  581. break;
  582. default:
  583. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), String::formatted("Unrecognized predictor order {}", subframe.order) };
  584. }
  585. return decoded;
  586. }
  587. // Decode the residual, the "error" between the function approximation and the actual audio data
  588. MaybeLoaderError FlacLoaderPlugin::decode_residual(Vector<i32>& decoded, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  589. {
  590. auto residual_mode = static_cast<FlacResidualMode>(LOADER_TRY(bit_input.read_bits<u8>(2)));
  591. u8 partition_order = LOADER_TRY(bit_input.read_bits<u8>(4));
  592. size_t partitions = 1 << partition_order;
  593. if (residual_mode == FlacResidualMode::Rice4Bit) {
  594. // decode a single Rice partition with four bits for the order k
  595. for (size_t i = 0; i < partitions; ++i) {
  596. auto rice_partition = TRY(decode_rice_partition(4, partitions, i, subframe, bit_input));
  597. decoded.extend(move(rice_partition));
  598. }
  599. } else if (residual_mode == FlacResidualMode::Rice5Bit) {
  600. // five bits equivalent
  601. for (size_t i = 0; i < partitions; ++i) {
  602. auto rice_partition = TRY(decode_rice_partition(5, partitions, i, subframe, bit_input));
  603. decoded.extend(move(rice_partition));
  604. }
  605. } else
  606. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved residual coding method" };
  607. return {};
  608. }
  609. // Decode a single Rice partition as part of the residual, every partition can have its own Rice parameter k
  610. ALWAYS_INLINE ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_rice_partition(u8 partition_type, u32 partitions, u32 partition_index, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  611. {
  612. // Rice parameter / Exp-Golomb order
  613. u8 k = LOADER_TRY(bit_input.read_bits<u8>(partition_type));
  614. u32 residual_sample_count;
  615. if (partitions == 0)
  616. residual_sample_count = m_current_frame->sample_count - subframe.order;
  617. else
  618. residual_sample_count = m_current_frame->sample_count / partitions;
  619. if (partition_index == 0)
  620. residual_sample_count -= subframe.order;
  621. Vector<i32> rice_partition;
  622. rice_partition.resize(residual_sample_count);
  623. // escape code for unencoded binary partition
  624. if (k == (1 << partition_type) - 1) {
  625. u8 unencoded_bps = LOADER_TRY(bit_input.read_bits<u8>(5));
  626. for (size_t r = 0; r < residual_sample_count; ++r) {
  627. rice_partition[r] = LOADER_TRY(bit_input.read_bits<u8>(unencoded_bps));
  628. }
  629. } else {
  630. for (size_t r = 0; r < residual_sample_count; ++r) {
  631. rice_partition[r] = LOADER_TRY(decode_unsigned_exp_golomb(k, bit_input));
  632. }
  633. }
  634. return rice_partition;
  635. }
  636. // Decode a single number encoded with Rice/Exponential-Golomb encoding (the unsigned variant)
  637. ALWAYS_INLINE ErrorOr<i32> decode_unsigned_exp_golomb(u8 k, BigEndianInputBitStream& bit_input)
  638. {
  639. u8 q = 0;
  640. while (TRY(bit_input.read_bit()) == 0)
  641. ++q;
  642. // least significant bits (remainder)
  643. u32 rem = TRY(bit_input.read_bits<u32>(k));
  644. u32 value = q << k | rem;
  645. return rice_to_signed(value);
  646. }
  647. ErrorOr<u64> read_utf8_char(BigEndianInputBitStream& input)
  648. {
  649. u64 character;
  650. u8 buffer = 0;
  651. Bytes buffer_bytes { &buffer, 1 };
  652. TRY(input.read(buffer_bytes));
  653. u8 start_byte = buffer_bytes[0];
  654. // Signal byte is zero: ASCII character
  655. if ((start_byte & 0b10000000) == 0) {
  656. return start_byte;
  657. } else if ((start_byte & 0b11000000) == 0b10000000) {
  658. return Error::from_string_literal("Illegal continuation byte"sv);
  659. }
  660. // This algorithm is too good and supports the theoretical max 0xFF start byte
  661. u8 length = 1;
  662. while (((start_byte << length) & 0b10000000) == 0b10000000)
  663. ++length;
  664. u8 bits_from_start_byte = 8 - (length + 1);
  665. u8 start_byte_bitmask = AK::exp2(bits_from_start_byte) - 1;
  666. character = start_byte_bitmask & start_byte;
  667. for (u8 i = length - 1; i > 0; --i) {
  668. TRY(input.read(buffer_bytes));
  669. u8 current_byte = buffer_bytes[0];
  670. character = (character << 6) | (current_byte & 0b00111111);
  671. }
  672. return character;
  673. }
  674. i64 sign_extend(u32 n, u8 size)
  675. {
  676. // negative
  677. if ((n & (1 << (size - 1))) > 0) {
  678. return static_cast<i64>(n | (0xffffffff << size));
  679. }
  680. // positive
  681. return n;
  682. }
  683. i32 rice_to_signed(u32 x)
  684. {
  685. // positive numbers are even, negative numbers are odd
  686. // bitmask for conditionally inverting the entire number, thereby "negating" it
  687. i32 sign = -static_cast<i32>(x & 1);
  688. // copies the sign's sign onto the actual magnitude of x
  689. return static_cast<i32>(sign ^ (x >> 1));
  690. }
  691. }