FlacWriter.cpp 38 KB

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