FlacWriter.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "FlacWriter.h"
  7. #include <AK/BitStream.h>
  8. #include <AK/Endian.h>
  9. #include <AK/IntegralMath.h>
  10. #include <AK/MemoryStream.h>
  11. #include <LibCrypto/Checksum/ChecksummingStream.h>
  12. namespace Audio {
  13. ErrorOr<NonnullOwnPtr<FlacWriter>> FlacWriter::create(NonnullOwnPtr<SeekableStream> stream, u32 sample_rate, u8 num_channels, u16 bits_per_sample)
  14. {
  15. auto writer = TRY(AK::adopt_nonnull_own_or_enomem(new (nothrow) FlacWriter(move(stream))));
  16. TRY(writer->set_bits_per_sample(bits_per_sample));
  17. TRY(writer->set_sample_rate(sample_rate));
  18. TRY(writer->set_num_channels(num_channels));
  19. return writer;
  20. }
  21. FlacWriter::FlacWriter(NonnullOwnPtr<SeekableStream> stream)
  22. : m_stream(move(stream))
  23. {
  24. }
  25. FlacWriter::~FlacWriter()
  26. {
  27. if (m_state != WriteState::FullyFinalized)
  28. (void)finalize();
  29. }
  30. ErrorOr<void> FlacWriter::finalize()
  31. {
  32. if (m_state == WriteState::FullyFinalized)
  33. return Error::from_string_view("File is already finalized"sv);
  34. // TODO: Write missing sample data instead of discarding it.
  35. if (m_state == WriteState::HeaderUnwritten)
  36. TRY(finalize_header_format());
  37. {
  38. // 1 byte metadata block header + 3 bytes size + 2*2 bytes min/max block size
  39. TRY(m_stream->seek(m_streaminfo_start_index + 8, AK::SeekMode::SetPosition));
  40. BigEndianOutputBitStream bit_stream { MaybeOwned<Stream> { *m_stream } };
  41. TRY(bit_stream.write_bits(m_min_frame_size, 24));
  42. TRY(bit_stream.write_bits(m_max_frame_size, 24));
  43. TRY(bit_stream.write_bits(m_sample_rate, 20));
  44. TRY(bit_stream.write_bits(m_num_channels - 1u, 3));
  45. TRY(bit_stream.write_bits(m_bits_per_sample - 1u, 5));
  46. TRY(bit_stream.write_bits(m_sample_count, 36));
  47. TRY(bit_stream.align_to_byte_boundary());
  48. }
  49. // TODO: Write the audio data MD5 to the header.
  50. m_stream->close();
  51. m_state = WriteState::FullyFinalized;
  52. return {};
  53. }
  54. ErrorOr<void> FlacWriter::finalize_header_format()
  55. {
  56. if (m_state != WriteState::HeaderUnwritten)
  57. return Error::from_string_view("Header format is already finalized"sv);
  58. TRY(write_header());
  59. m_state = WriteState::FormatFinalized;
  60. return {};
  61. }
  62. ErrorOr<void> FlacWriter::set_num_channels(u8 num_channels)
  63. {
  64. if (m_state != WriteState::HeaderUnwritten)
  65. return Error::from_string_view("Header format is already finalized"sv);
  66. if (num_channels > 8)
  67. return Error::from_string_view("FLAC doesn't support more than 8 channels"sv);
  68. m_num_channels = num_channels;
  69. return {};
  70. }
  71. ErrorOr<void> FlacWriter::set_sample_rate(u32 sample_rate)
  72. {
  73. if (m_state != WriteState::HeaderUnwritten)
  74. return Error::from_string_view("Header format is already finalized"sv);
  75. m_sample_rate = sample_rate;
  76. return {};
  77. }
  78. ErrorOr<void> FlacWriter::set_bits_per_sample(u16 bits_per_sample)
  79. {
  80. if (m_state != WriteState::HeaderUnwritten)
  81. return Error::from_string_view("Header format is already finalized"sv);
  82. if (bits_per_sample < 8 || bits_per_sample > 32)
  83. return Error::from_string_view("FLAC only supports bits per sample between 8 and 32"sv);
  84. m_bits_per_sample = bits_per_sample;
  85. return {};
  86. }
  87. ErrorOr<void> FlacWriter::write_header()
  88. {
  89. TRY(m_stream->write_until_depleted(flac_magic.bytes()));
  90. m_streaminfo_start_index = TRY(m_stream->tell());
  91. ByteBuffer data;
  92. // STREAMINFO is always exactly 34 bytes long.
  93. TRY(data.try_resize(34));
  94. BigEndianOutputBitStream header_stream { TRY(try_make<FixedMemoryStream>(data.bytes())) };
  95. // Duplication on purpose:
  96. // Minimum frame size.
  97. TRY(header_stream.write_bits(block_size, 16));
  98. // Maximum frame size.
  99. TRY(header_stream.write_bits(block_size, 16));
  100. // Leave the frame sizes as unknown for now.
  101. TRY(header_stream.write_bits(0u, 24));
  102. TRY(header_stream.write_bits(0u, 24));
  103. TRY(header_stream.write_bits(m_sample_rate, 20));
  104. TRY(header_stream.write_bits(m_num_channels - 1u, 3));
  105. TRY(header_stream.write_bits(m_bits_per_sample - 1u, 5));
  106. // Leave the sample count as unknown for now.
  107. TRY(header_stream.write_bits(0u, 36));
  108. // TODO: Calculate the MD5 signature of all of the audio data.
  109. auto md5 = TRY(ByteBuffer::create_zeroed(128u / 8u));
  110. TRY(header_stream.write_until_depleted(md5));
  111. FlacRawMetadataBlock streaminfo_block = {
  112. .is_last_block = true,
  113. .type = FlacMetadataBlockType::STREAMINFO,
  114. .length = static_cast<u32>(data.size()),
  115. .data = data,
  116. };
  117. TRY(m_stream->write_value(streaminfo_block));
  118. return {};
  119. }
  120. ErrorOr<void> FlacRawMetadataBlock::write_to_stream(Stream& stream) const
  121. {
  122. BigEndianOutputBitStream bit_stream { MaybeOwned<Stream> { stream } };
  123. TRY(bit_stream.write_bits(static_cast<u8>(is_last_block), 1));
  124. TRY(bit_stream.write_bits(to_underlying(type), 7));
  125. TRY(bit_stream.write_bits(length, 24));
  126. VERIFY(data.size() == length);
  127. TRY(bit_stream.write_until_depleted(data));
  128. return {};
  129. }
  130. // If the given sample count is uncommon, this function will return one of the uncommon marker block sizes.
  131. // The caller has to handle and add these later manually.
  132. static BlockSizeCategory to_common_block_size(u16 sample_count)
  133. {
  134. switch (sample_count) {
  135. case 192:
  136. return BlockSizeCategory::S192;
  137. case 576:
  138. return BlockSizeCategory::S576;
  139. case 1152:
  140. return BlockSizeCategory::S1152;
  141. case 2304:
  142. return BlockSizeCategory::S2304;
  143. case 4608:
  144. return BlockSizeCategory::S4608;
  145. case 256:
  146. return BlockSizeCategory::S256;
  147. case 512:
  148. return BlockSizeCategory::S512;
  149. case 1024:
  150. return BlockSizeCategory::S1024;
  151. case 2048:
  152. return BlockSizeCategory::S2048;
  153. case 4096:
  154. return BlockSizeCategory::S4096;
  155. case 8192:
  156. return BlockSizeCategory::S8192;
  157. case 16384:
  158. return BlockSizeCategory::S16384;
  159. case 32768:
  160. return BlockSizeCategory::S32768;
  161. }
  162. if (sample_count - 1 <= 0xff)
  163. return BlockSizeCategory::Uncommon8Bits;
  164. // Data type guarantees that 16-bit storage is possible.
  165. return BlockSizeCategory::Uncommon16Bits;
  166. }
  167. static ByteBuffer to_utf8(u64 value)
  168. {
  169. ByteBuffer buffer;
  170. if (value < 0x7f) {
  171. buffer.append(static_cast<u8>(value));
  172. } else if (value < 0x7ff) {
  173. buffer.append(static_cast<u8>(0b110'00000 | (value >> 6)));
  174. buffer.append(static_cast<u8>(0b10'000000 | (value & 0b111111)));
  175. } else if (value < 0xffff) {
  176. buffer.append(static_cast<u8>(0b1110'0000 | (value >> 12)));
  177. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 6) & 0b111111)));
  178. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 0) & 0b111111)));
  179. } else if (value < 0x1f'ffff) {
  180. buffer.append(static_cast<u8>(0b11110'000 | (value >> 18)));
  181. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 12) & 0b111111)));
  182. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 6) & 0b111111)));
  183. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 0) & 0b111111)));
  184. } else if (value < 0x3ff'ffff) {
  185. buffer.append(static_cast<u8>(0b111110'00 | (value >> 24)));
  186. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 18) & 0b111111)));
  187. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 12) & 0b111111)));
  188. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 6) & 0b111111)));
  189. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 0) & 0b111111)));
  190. } else if (value < 0x7fff'ffff) {
  191. buffer.append(static_cast<u8>(0b1111110'0 | (value >> 30)));
  192. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 24) & 0b111111)));
  193. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 18) & 0b111111)));
  194. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 12) & 0b111111)));
  195. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 6) & 0b111111)));
  196. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 0) & 0b111111)));
  197. } else if (value < 0xf'ffff'ffff) {
  198. buffer.append(static_cast<u8>(0b11111110));
  199. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 30) & 0b111111)));
  200. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 24) & 0b111111)));
  201. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 18) & 0b111111)));
  202. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 12) & 0b111111)));
  203. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 6) & 0b111111)));
  204. buffer.append(static_cast<u8>(0b10'000000 | ((value >> 0) & 0b111111)));
  205. } else {
  206. // Anything larger is illegal even in expanded UTF-8, but FLAC only passes 32-bit values anyways.
  207. VERIFY_NOT_REACHED();
  208. }
  209. return buffer;
  210. }
  211. ErrorOr<void> FlacFrameHeader::write_to_stream(Stream& stream) const
  212. {
  213. Crypto::Checksum::ChecksummingStream<FlacFrameHeaderCRC> checksumming_stream { MaybeOwned<Stream> { stream } };
  214. BigEndianOutputBitStream bit_stream { MaybeOwned<Stream> { checksumming_stream } };
  215. TRY(bit_stream.write_bits(0b11111111111110u, 14));
  216. TRY(bit_stream.write_bits(0u, 1));
  217. TRY(bit_stream.write_bits(to_underlying(blocking_strategy), 1));
  218. auto common_block_size = to_common_block_size(sample_count);
  219. TRY(bit_stream.write_bits(to_underlying(common_block_size), 4));
  220. // We always store sample rate in the file header.
  221. TRY(bit_stream.write_bits(0u, 4));
  222. TRY(bit_stream.write_bits(to_underlying(channels), 4));
  223. // We always store bit depth in the file header.
  224. TRY(bit_stream.write_bits(0u, 3));
  225. // Reserved zero bit.
  226. TRY(bit_stream.write_bits(0u, 1));
  227. auto coded_number = to_utf8(sample_or_frame_index);
  228. TRY(bit_stream.write_until_depleted(coded_number));
  229. if (common_block_size == BlockSizeCategory::Uncommon8Bits)
  230. TRY(bit_stream.write_value(static_cast<u8>(sample_count - 1)));
  231. if (common_block_size == BlockSizeCategory::Uncommon16Bits)
  232. TRY(bit_stream.write_value(BigEndian<u16>(static_cast<u16>(sample_count - 1))));
  233. // Ensure that the checksum is calculated correctly.
  234. TRY(bit_stream.align_to_byte_boundary());
  235. auto checksum = checksumming_stream.digest();
  236. TRY(bit_stream.write_value(checksum));
  237. return {};
  238. }
  239. ErrorOr<void> FlacWriter::write_samples(ReadonlySpan<Sample> samples)
  240. {
  241. if (m_state == WriteState::FullyFinalized)
  242. return Error::from_string_view("File is already finalized"sv);
  243. auto remaining_samples = samples;
  244. while (remaining_samples.size() > 0) {
  245. if (m_sample_buffer.size() == block_size) {
  246. TRY(write_frame());
  247. m_sample_buffer.clear();
  248. }
  249. auto amount_to_copy = min(remaining_samples.size(), m_sample_buffer.capacity() - m_sample_buffer.size());
  250. auto current_buffer_size = m_sample_buffer.size();
  251. TRY(m_sample_buffer.try_resize_and_keep_capacity(current_buffer_size + amount_to_copy));
  252. remaining_samples.copy_trimmed_to(m_sample_buffer.span().slice(current_buffer_size));
  253. remaining_samples = remaining_samples.slice(amount_to_copy);
  254. }
  255. // Ensure that the buffer is flushed if possible.
  256. if (m_sample_buffer.size() == block_size) {
  257. TRY(write_frame());
  258. m_sample_buffer.clear();
  259. }
  260. return {};
  261. }
  262. ErrorOr<void> FlacWriter::write_frame()
  263. {
  264. auto frame_samples = move(m_sample_buffer);
  265. // De-interleave and integer-quantize subframes.
  266. float sample_rescale = static_cast<float>(1 << (m_bits_per_sample - 1));
  267. auto subframe_samples = Vector<Vector<i64, block_size>>();
  268. TRY(subframe_samples.try_resize_and_keep_capacity(m_num_channels));
  269. for (auto const& sample : frame_samples) {
  270. TRY(subframe_samples[0].try_append(static_cast<i64>(sample.left * sample_rescale)));
  271. // FIXME: We don't have proper data for any channels past 2.
  272. for (auto i = 1; i < m_num_channels; ++i)
  273. TRY(subframe_samples[i].try_append(static_cast<i64>(sample.right * sample_rescale)));
  274. }
  275. FlacFrameHeader header {
  276. .sample_rate = m_sample_rate,
  277. .sample_count = static_cast<u16>(frame_samples.size()),
  278. .sample_or_frame_index = static_cast<u32>(m_current_frame),
  279. .blocking_strategy = BlockingStrategy::Fixed,
  280. // FIXME: We should brute-force channel coupling for stereo.
  281. .channels = static_cast<FlacFrameChannelType>(m_num_channels - 1),
  282. .bit_depth = static_cast<u8>(m_bits_per_sample),
  283. // Calculated for us during header write.
  284. .checksum = 0,
  285. };
  286. auto frame_stream = Crypto::Checksum::ChecksummingStream<IBMCRC> { MaybeOwned<Stream> { *m_stream } };
  287. auto frame_start_offset = TRY(m_stream->tell());
  288. TRY(frame_stream.write_value(header));
  289. BigEndianOutputBitStream bit_stream { MaybeOwned<Stream> { frame_stream } };
  290. for (auto const& subframe : subframe_samples)
  291. TRY(write_subframe(subframe.span(), bit_stream));
  292. TRY(bit_stream.align_to_byte_boundary());
  293. auto frame_crc = frame_stream.digest();
  294. dbgln_if(FLAC_ENCODER_DEBUG, "Frame {:4} CRC: {:04x}", m_current_frame, frame_crc);
  295. TRY(frame_stream.write_value<AK::BigEndian<u16>>(frame_crc));
  296. auto frame_end_offset = TRY(m_stream->tell());
  297. auto frame_size = frame_end_offset - frame_start_offset;
  298. m_max_frame_size = max(m_max_frame_size, frame_size);
  299. m_min_frame_size = min(m_min_frame_size, frame_size);
  300. m_current_frame++;
  301. m_sample_count += frame_samples.size();
  302. return {};
  303. }
  304. ErrorOr<void> FlacWriter::write_subframe(ReadonlySpan<i64> subframe, BigEndianOutputBitStream& bit_stream)
  305. {
  306. // The current subframe encoding strategy is as follows:
  307. // - Check if the subframe is constant; use constant encoding in this case.
  308. // - Try all fixed predictors and record the resulting residuals.
  309. // - Estimate their encoding cost by taking the sum of all absolute logarithmic residuals,
  310. // which is an accurate estimate of the final encoded size of the residuals.
  311. // - Accurately estimate the encoding cost of a verbatim subframe.
  312. // - Select the encoding strategy with the lowest cost out of this selection.
  313. auto constant_value = subframe[0];
  314. auto is_constant = true;
  315. for (auto const sample : subframe) {
  316. if (sample != constant_value) {
  317. is_constant = false;
  318. break;
  319. }
  320. }
  321. if (is_constant) {
  322. dbgln_if(FLAC_ENCODER_DEBUG, "Encoding constant frame with value {}", constant_value);
  323. TRY(bit_stream.write_bits(1u, 0));
  324. TRY(bit_stream.write_bits(to_underlying(FlacSubframeType::Constant), 6));
  325. TRY(bit_stream.write_bits(1u, 0));
  326. TRY(bit_stream.write_bits(bit_cast<u64>(constant_value), m_bits_per_sample));
  327. return {};
  328. }
  329. auto verbatim_cost_bits = subframe.size() * m_bits_per_sample;
  330. Optional<FlacLPCEncodedSubframe> best_lpc_subframe;
  331. auto current_min_cost = verbatim_cost_bits;
  332. for (auto order : { FlacFixedLPC::Zero, FlacFixedLPC::One, FlacFixedLPC::Two, FlacFixedLPC::Three, FlacFixedLPC::Four }) {
  333. // Too many warm-up samples would be required; the lower-level encoding procedures assume that this was checked.
  334. if (to_underlying(order) > subframe.size())
  335. continue;
  336. auto encode_result = TRY(encode_fixed_lpc(order, subframe, current_min_cost));
  337. if (encode_result.has_value() && encode_result.value().residual_cost_bits < current_min_cost) {
  338. current_min_cost = encode_result.value().residual_cost_bits;
  339. best_lpc_subframe = encode_result.release_value();
  340. }
  341. }
  342. // No LPC encoding was better than verbatim.
  343. if (!best_lpc_subframe.has_value()) {
  344. dbgln_if(FLAC_ENCODER_DEBUG, "Best subframe type was Verbatim; encoding {} samples at {} bps = {} bits", subframe.size(), m_bits_per_sample, verbatim_cost_bits);
  345. TRY(write_verbatim_subframe(subframe, bit_stream));
  346. } else {
  347. dbgln_if(FLAC_ENCODER_DEBUG, "Best subframe type was Fixed LPC order {} (estimated cost {} bits); encoding {} samples", to_underlying(best_lpc_subframe->coefficients.get<FlacFixedLPC>()), best_lpc_subframe->residual_cost_bits, subframe.size());
  348. TRY(write_lpc_subframe(best_lpc_subframe.release_value(), bit_stream));
  349. }
  350. return {};
  351. }
  352. ErrorOr<Optional<FlacLPCEncodedSubframe>> FlacWriter::encode_fixed_lpc(FlacFixedLPC order, ReadonlySpan<i64> subframe, size_t current_min_cost)
  353. {
  354. FlacLPCEncodedSubframe lpc {
  355. .warm_up_samples = Vector<i64> { subframe.trim(to_underlying(order)) },
  356. .coefficients = order,
  357. .residuals {},
  358. // Warm-up sample cost.
  359. .residual_cost_bits = to_underlying(order) * m_bits_per_sample,
  360. .single_partition_optimal_order {},
  361. };
  362. TRY(lpc.residuals.try_ensure_capacity(subframe.size() - to_underlying(order)));
  363. Vector<i64> predicted;
  364. TRY(predicted.try_resize_and_keep_capacity(subframe.size()));
  365. lpc.warm_up_samples.span().copy_trimmed_to(predicted);
  366. // NOTE: Although we can't interrupt the prediction if the corresponding residuals would become too bad,
  367. // we don't need to branch on the order in every loop during prediction, meaning this shouldn't cost us much.
  368. predict_fixed_lpc(order, subframe, predicted);
  369. // There isn’t really a way of computing an LPC’s cost without performing most of the calculations, including a Rice parameter search.
  370. // This is nevertheless optimized in multiple ways, so that we always bail out once we are sure no improvements can be made.
  371. auto extra_residual_cost = NumericLimits<size_t>::max();
  372. // Keep track of when we want to estimate costs again. We don't do this for every new residual since it's an expensive procedure.
  373. // The likelihood for misprediction is pretty high for large orders; start with a later index for them.
  374. auto next_cost_estimation_index = min(subframe.size() - 1, first_residual_estimation * (to_underlying(order) + 1));
  375. for (auto i = to_underlying(order); i < subframe.size(); ++i) {
  376. auto residual = subframe[i] - predicted[i];
  377. if (!AK::is_within_range<i32>(residual)) {
  378. dbgln_if(FLAC_ENCODER_DEBUG, "Bailing from Fixed LPC order {} due to residual overflow ({} is outside the 32-bit range)", to_underlying(order), residual);
  379. return Optional<FlacLPCEncodedSubframe> {};
  380. }
  381. lpc.residuals.append(residual);
  382. if (i >= next_cost_estimation_index) {
  383. // Find best exponential Golomb order.
  384. // Storing this in the LPC data allows us to automatically reuse the computation during LPC encoding.
  385. // FIXME: Use more than one partition to improve compression.
  386. // FIXME: Investigate whether this can be estimated “good enough” to improve performance at the cost of compression strength.
  387. // Especially at larger sample counts, it is unlikely that we will find a different optimal order.
  388. // Therefore, use a zig-zag search around the previous optimal order.
  389. extra_residual_cost = NumericLimits<size_t>::max();
  390. auto start_order = lpc.single_partition_optimal_order;
  391. size_t useless_parameters = 0;
  392. size_t steps = 0;
  393. constexpr auto max_rice_parameter = AK::exp2(4) - 1;
  394. for (auto offset = 0; start_order + offset < max_rice_parameter || start_order - offset >= 0; ++offset) {
  395. for (auto factor : { -1, 1 }) {
  396. auto k = start_order + factor * offset;
  397. if (k >= max_rice_parameter || k < 0)
  398. continue;
  399. auto order_cost = count_exp_golomb_bits_in(k, lpc.residuals);
  400. if (order_cost < extra_residual_cost) {
  401. extra_residual_cost = order_cost;
  402. lpc.single_partition_optimal_order = k;
  403. } else {
  404. useless_parameters++;
  405. }
  406. steps++;
  407. // Don’t do 0 twice.
  408. if (offset == 0)
  409. break;
  410. }
  411. // If we found enough useless parameters, we probably won't find useful ones anymore.
  412. // The only exception is the first ever parameter search, where we search everything.
  413. if (useless_parameters >= useless_parameter_threshold && start_order != 0)
  414. break;
  415. }
  416. // Min cost exceeded; bail out.
  417. if (lpc.residual_cost_bits + extra_residual_cost > current_min_cost) {
  418. dbgln_if(FLAC_ENCODER_DEBUG, " Bailing from Fixed LPC order {} at sample index {} and cost {} (best {})", to_underlying(order), i, lpc.residual_cost_bits + extra_residual_cost, current_min_cost);
  419. return Optional<FlacLPCEncodedSubframe> {};
  420. }
  421. // Figure out when to next estimate costs.
  422. auto estimated_bits_per_residual = static_cast<double>(extra_residual_cost) / static_cast<double>(i);
  423. auto estimated_residuals_for_min_cost = static_cast<double>(current_min_cost) / estimated_bits_per_residual;
  424. auto unchecked_next_cost_estimation_index = AK::round_to<size_t>(estimated_residuals_for_min_cost * (1 - residual_cost_margin));
  425. // Check either at the estimated residual, or the next residual if that is in the past, or the last residual.
  426. next_cost_estimation_index = min(subframe.size() - 1, max(unchecked_next_cost_estimation_index, i + min_residual_estimation_step));
  427. dbgln_if(FLAC_ENCODER_DEBUG, " {} {:4} Estimate cost/residual {:.1f} (param {:2} after {:2} steps), will hit at {:6.1f}, jumping to {:4} (sanitized to {:4})", to_underlying(order), i, estimated_bits_per_residual, lpc.single_partition_optimal_order, steps, estimated_residuals_for_min_cost, unchecked_next_cost_estimation_index, next_cost_estimation_index);
  428. }
  429. }
  430. lpc.residual_cost_bits += extra_residual_cost;
  431. return lpc;
  432. }
  433. void predict_fixed_lpc(FlacFixedLPC order, ReadonlySpan<i64> samples, Span<i64> predicted_output)
  434. {
  435. switch (order) {
  436. case FlacFixedLPC::Zero:
  437. // s_0(t) = 0
  438. for (auto i = to_underlying(order); i < predicted_output.size(); ++i)
  439. predicted_output[i] += 0;
  440. break;
  441. case FlacFixedLPC::One:
  442. // s_1(t) = s(t-1)
  443. for (auto i = to_underlying(order); i < predicted_output.size(); ++i)
  444. predicted_output[i] += samples[i - 1];
  445. break;
  446. case FlacFixedLPC::Two:
  447. // s_2(t) = 2s(t-1) - s(t-2)
  448. for (auto i = to_underlying(order); i < predicted_output.size(); ++i)
  449. predicted_output[i] += 2 * samples[i - 1] - samples[i - 2];
  450. break;
  451. case FlacFixedLPC::Three:
  452. // s_3(t) = 3s(t-1) - 3s(t-2) + s(t-3)
  453. for (auto i = to_underlying(order); i < predicted_output.size(); ++i)
  454. predicted_output[i] += 3 * samples[i - 1] - 3 * samples[i - 2] + samples[i - 3];
  455. break;
  456. case FlacFixedLPC::Four:
  457. // s_4(t) = 4s(t-1) - 6s(t-2) + 4s(t-3) - s(t-4)
  458. for (auto i = to_underlying(order); i < predicted_output.size(); ++i)
  459. predicted_output[i] += 4 * samples[i - 1] - 6 * samples[i - 2] + 4 * samples[i - 3] - samples[i - 4];
  460. break;
  461. default:
  462. VERIFY_NOT_REACHED();
  463. }
  464. }
  465. // https://www.ietf.org/archive/id/draft-ietf-cellar-flac-08.html#name-verbatim-subframe
  466. ErrorOr<void> FlacWriter::write_verbatim_subframe(ReadonlySpan<i64> subframe, BigEndianOutputBitStream& bit_stream)
  467. {
  468. TRY(bit_stream.write_bits(0u, 1));
  469. TRY(bit_stream.write_bits(to_underlying(FlacSubframeType::Verbatim), 6));
  470. TRY(bit_stream.write_bits(0u, 1));
  471. for (auto const& sample : subframe)
  472. TRY(bit_stream.write_bits(bit_cast<u64>(sample), m_bits_per_sample));
  473. return {};
  474. }
  475. // https://www.ietf.org/archive/id/draft-ietf-cellar-flac-08.html#name-fixed-predictor-subframe
  476. ErrorOr<void> FlacWriter::write_lpc_subframe(FlacLPCEncodedSubframe lpc_subframe, BigEndianOutputBitStream& bit_stream)
  477. {
  478. // Reserved.
  479. TRY(bit_stream.write_bits(0u, 1));
  480. // 9.2.1 Subframe header (https://www.ietf.org/archive/id/draft-ietf-cellar-flac-08.html#name-subframe-header)
  481. u8 encoded_type;
  482. if (lpc_subframe.coefficients.has<FlacFixedLPC>())
  483. encoded_type = to_underlying(lpc_subframe.coefficients.get<FlacFixedLPC>()) + to_underlying(FlacSubframeType::Fixed);
  484. else
  485. encoded_type = lpc_subframe.coefficients.get<Vector<i64>>().size() - 1 + to_underlying(FlacSubframeType::LPC);
  486. TRY(bit_stream.write_bits(encoded_type, 6));
  487. // No wasted bits per sample (unnecessary for the vast majority of data).
  488. TRY(bit_stream.write_bits(0u, 1));
  489. for (auto const& warm_up_sample : lpc_subframe.warm_up_samples)
  490. TRY(bit_stream.write_bits(bit_cast<u64>(warm_up_sample), m_bits_per_sample));
  491. // 4-bit Rice parameters.
  492. TRY(bit_stream.write_bits(0b00u, 2));
  493. // Only one partition (2^0 = 1).
  494. TRY(bit_stream.write_bits(0b0000u, 4));
  495. TRY(write_rice_partition(lpc_subframe.single_partition_optimal_order, lpc_subframe.residuals, bit_stream));
  496. return {};
  497. }
  498. ErrorOr<void> FlacWriter::write_rice_partition(u8 k, ReadonlySpan<i64> residuals, BigEndianOutputBitStream& bit_stream)
  499. {
  500. TRY(bit_stream.write_bits(k, 4));
  501. for (auto const& residual : residuals)
  502. TRY(encode_unsigned_exp_golomb(k, static_cast<i32>(residual), bit_stream));
  503. return {};
  504. }
  505. u32 signed_to_rice(i32 x)
  506. {
  507. // Implements (x < 0 ? -1 : 0) + 2 * abs(x) in about half as many instructions.
  508. // The reference encoder’s implementation is known to be the fastest on -O2/3 clang and gcc:
  509. // x << 1 = multiply by 2.
  510. // For negative numbers, x >> 31 will create an all-ones XOR mask, meaning that the number will be inverted.
  511. // In two's complement this is -value - 1, exactly what we need.
  512. // For positive numbers, x >> 31 == 0.
  513. return static_cast<u32>((x << 1) ^ (x >> 31));
  514. }
  515. // Adopted from https://github.com/xiph/flac/blob/28e4f0528c76b296c561e922ba67d43751990599/src/libFLAC/bitwriter.c#L727
  516. ErrorOr<void> encode_unsigned_exp_golomb(u8 k, i32 value, BigEndianOutputBitStream& bit_stream)
  517. {
  518. auto zigzag_encoded = signed_to_rice(value);
  519. auto msbs = zigzag_encoded >> k;
  520. auto pattern = 1u << k;
  521. pattern |= zigzag_encoded & ((1 << k) - 1);
  522. TRY(bit_stream.write_bits(0u, msbs));
  523. TRY(bit_stream.write_bits(pattern, k + 1));
  524. return {};
  525. }
  526. // Adopted from count_rice_bits_in_partition():
  527. // https://github.com/xiph/flac/blob/28e4f0528c76b296c561e922ba67d43751990599/src/libFLAC/stream_encoder.c#L4299
  528. size_t count_exp_golomb_bits_in(u8 k, ReadonlySpan<i64> residuals)
  529. {
  530. // Exponential Golomb order size (4).
  531. // One unary stop bit and the entire exponential Golomb parameter for every residual.
  532. size_t partition_bits = 4 + (1 + k) * residuals.size();
  533. // Bit magic to compute the amount of leading unary bits.
  534. for (auto const& residual : residuals)
  535. partition_bits += (static_cast<u32>((residual << 1) ^ (residual >> 31)) >> k);
  536. return partition_bits;
  537. }
  538. }