FlacLoader.cpp 32 KB

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