FlacWriter.cpp 31 KB

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