FlacLoader.cpp 29 KB

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