FlacLoader.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /*
  2. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/DeprecatedFlyString.h>
  8. #include <AK/DeprecatedString.h>
  9. #include <AK/FixedArray.h>
  10. #include <AK/Format.h>
  11. #include <AK/IntegralMath.h>
  12. #include <AK/Math.h>
  13. #include <AK/MemoryStream.h>
  14. #include <AK/ScopeGuard.h>
  15. #include <AK/StdLibExtras.h>
  16. #include <AK/Try.h>
  17. #include <AK/TypedTransfer.h>
  18. #include <AK/UFixedBigInt.h>
  19. #include <LibAudio/FlacLoader.h>
  20. #include <LibAudio/FlacTypes.h>
  21. #include <LibAudio/LoaderError.h>
  22. #include <LibAudio/Resampler.h>
  23. #include <LibCore/File.h>
  24. namespace Audio {
  25. FlacLoaderPlugin::FlacLoaderPlugin(NonnullOwnPtr<SeekableStream> stream)
  26. : LoaderPlugin(move(stream))
  27. {
  28. }
  29. Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> FlacLoaderPlugin::create(StringView path)
  30. {
  31. auto stream = LOADER_TRY(Core::BufferedFile::create(LOADER_TRY(Core::File::open(path, Core::File::OpenMode::Read))));
  32. auto loader = make<FlacLoaderPlugin>(move(stream));
  33. LOADER_TRY(loader->initialize());
  34. return loader;
  35. }
  36. Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> FlacLoaderPlugin::create(Bytes buffer)
  37. {
  38. auto stream = LOADER_TRY(try_make<FixedMemoryStream>(buffer));
  39. auto loader = make<FlacLoaderPlugin>(move(stream));
  40. LOADER_TRY(loader->initialize());
  41. return loader;
  42. }
  43. MaybeLoaderError FlacLoaderPlugin::initialize()
  44. {
  45. TRY(parse_header());
  46. TRY(reset());
  47. return {};
  48. }
  49. // 11.5 STREAM
  50. MaybeLoaderError FlacLoaderPlugin::parse_header()
  51. {
  52. BigEndianInputBitStream bit_input { MaybeOwned<Stream>(*m_stream) };
  53. // A mixture of VERIFY and the non-crashing TRY().
  54. #define FLAC_VERIFY(check, category, msg) \
  55. do { \
  56. if (!(check)) { \
  57. return LoaderError { category, static_cast<size_t>(m_data_start_location), DeprecatedString::formatted("FLAC header: {}", msg) }; \
  58. } \
  59. } while (0)
  60. // Magic number
  61. u32 flac = LOADER_TRY(bit_input.read_bits<u32>(32));
  62. m_data_start_location += 4;
  63. FLAC_VERIFY(flac == 0x664C6143, LoaderError::Category::Format, "Magic number must be 'flaC'"); // "flaC"
  64. // Receive the streaminfo block
  65. auto streaminfo = TRY(next_meta_block(bit_input));
  66. FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO");
  67. FixedMemoryStream streaminfo_data_memory { streaminfo.data.bytes() };
  68. BigEndianInputBitStream streaminfo_data { MaybeOwned<Stream>(streaminfo_data_memory) };
  69. // 11.10 METADATA_BLOCK_STREAMINFO
  70. m_min_block_size = LOADER_TRY(streaminfo_data.read_bits<u16>(16));
  71. FLAC_VERIFY(m_min_block_size >= 16, LoaderError::Category::Format, "Minimum block size must be 16");
  72. m_max_block_size = LOADER_TRY(streaminfo_data.read_bits<u16>(16));
  73. FLAC_VERIFY(m_max_block_size >= 16, LoaderError::Category::Format, "Maximum block size");
  74. m_min_frame_size = LOADER_TRY(streaminfo_data.read_bits<u32>(24));
  75. m_max_frame_size = LOADER_TRY(streaminfo_data.read_bits<u32>(24));
  76. m_sample_rate = LOADER_TRY(streaminfo_data.read_bits<u32>(20));
  77. FLAC_VERIFY(m_sample_rate <= 655350, LoaderError::Category::Format, "Sample rate");
  78. m_num_channels = LOADER_TRY(streaminfo_data.read_bits<u8>(3)) + 1; // 0 = one channel
  79. u8 bits_per_sample = LOADER_TRY(streaminfo_data.read_bits<u8>(5)) + 1;
  80. if (bits_per_sample == 8) {
  81. // FIXME: Signed/Unsigned issues?
  82. m_sample_format = PcmSampleFormat::Uint8;
  83. } else if (bits_per_sample == 16) {
  84. m_sample_format = PcmSampleFormat::Int16;
  85. } else if (bits_per_sample == 24) {
  86. m_sample_format = PcmSampleFormat::Int24;
  87. } else if (bits_per_sample == 32) {
  88. m_sample_format = PcmSampleFormat::Int32;
  89. } else {
  90. FLAC_VERIFY(false, LoaderError::Category::Format, "Sample bit depth invalid");
  91. }
  92. m_total_samples = LOADER_TRY(streaminfo_data.read_bits<u64>(36));
  93. FLAC_VERIFY(m_total_samples > 0, LoaderError::Category::Format, "Number of samples is zero");
  94. // Parse checksum into a buffer first
  95. [[maybe_unused]] u128 md5_checksum;
  96. VERIFY(streaminfo_data.is_aligned_to_byte_boundary());
  97. auto md5_bytes_read = LOADER_TRY(streaminfo_data.read(md5_checksum.bytes()));
  98. FLAC_VERIFY(md5_bytes_read.size() == sizeof(md5_checksum), LoaderError::Category::IO, "MD5 Checksum size");
  99. md5_checksum.bytes().copy_to({ m_md5_checksum, sizeof(m_md5_checksum) });
  100. // Parse other blocks
  101. [[maybe_unused]] u16 meta_blocks_parsed = 1;
  102. [[maybe_unused]] u16 total_meta_blocks = meta_blocks_parsed;
  103. FlacRawMetadataBlock block = streaminfo;
  104. while (!block.is_last_block) {
  105. block = TRY(next_meta_block(bit_input));
  106. switch (block.type) {
  107. case (FlacMetadataBlockType::SEEKTABLE):
  108. TRY(load_seektable(block));
  109. break;
  110. case FlacMetadataBlockType::PICTURE:
  111. TRY(load_picture(block));
  112. break;
  113. case FlacMetadataBlockType::APPLICATION:
  114. // Note: Third-party library can encode specific data in this.
  115. dbgln("Unknown 'Application' metadata block encountered.");
  116. [[fallthrough]];
  117. case FlacMetadataBlockType::PADDING:
  118. // Note: A padding block is empty and does not need any treatment.
  119. default:
  120. // TODO: Parse the remaining metadata block types.
  121. break;
  122. }
  123. ++total_meta_blocks;
  124. }
  125. dbgln_if(AFLACLOADER_DEBUG, "Parsed FLAC header: blocksize {}-{}{}, framesize {}-{}, {}Hz, {}bit, {} channels, {} samples total ({:.2f}s), MD5 {}, data start at {:x} bytes, {} headers total (skipped {})", m_min_block_size, m_max_block_size, is_fixed_blocksize_stream() ? " (constant)" : "", m_min_frame_size, m_max_frame_size, m_sample_rate, pcm_bits_per_sample(m_sample_format), m_num_channels, m_total_samples, static_cast<float>(m_total_samples) / static_cast<float>(m_sample_rate), md5_checksum, m_data_start_location, total_meta_blocks, total_meta_blocks - meta_blocks_parsed);
  126. return {};
  127. }
  128. // 11.19. METADATA_BLOCK_PICTURE
  129. MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
  130. {
  131. FixedMemoryStream memory_stream { block.data.bytes() };
  132. BigEndianInputBitStream picture_block_bytes { MaybeOwned<Stream>(memory_stream) };
  133. PictureData picture {};
  134. picture.type = static_cast<ID3PictureType>(LOADER_TRY(picture_block_bytes.read_bits(32)));
  135. auto const mime_string_length = LOADER_TRY(picture_block_bytes.read_bits(32));
  136. // Note: We are seeking before reading the value to ensure that we stayed inside buffer's size.
  137. auto offset_before_seeking = memory_stream.offset();
  138. LOADER_TRY(memory_stream.seek(mime_string_length, SeekMode::FromCurrentPosition));
  139. picture.mime_string = { block.data.bytes().data() + offset_before_seeking, (size_t)mime_string_length };
  140. auto const description_string_length = LOADER_TRY(picture_block_bytes.read_bits(32));
  141. offset_before_seeking = memory_stream.offset();
  142. LOADER_TRY(memory_stream.seek(description_string_length, SeekMode::FromCurrentPosition));
  143. picture.description_string = Vector<u32> { Span<u32> { reinterpret_cast<u32*>(block.data.bytes().data() + offset_before_seeking), (size_t)description_string_length } };
  144. picture.width = LOADER_TRY(picture_block_bytes.read_bits(32));
  145. picture.height = LOADER_TRY(picture_block_bytes.read_bits(32));
  146. picture.color_depth = LOADER_TRY(picture_block_bytes.read_bits(32));
  147. picture.colors = LOADER_TRY(picture_block_bytes.read_bits(32));
  148. auto const picture_size = LOADER_TRY(picture_block_bytes.read_bits(32));
  149. offset_before_seeking = memory_stream.offset();
  150. LOADER_TRY(memory_stream.seek(picture_size, SeekMode::FromCurrentPosition));
  151. picture.data = Vector<u8> { Span<u8> { block.data.bytes().data() + offset_before_seeking, (size_t)picture_size } };
  152. m_pictures.append(move(picture));
  153. return {};
  154. }
  155. // 11.13. METADATA_BLOCK_SEEKTABLE
  156. MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block)
  157. {
  158. FixedMemoryStream memory_stream { block.data.bytes() };
  159. BigEndianInputBitStream seektable_bytes { MaybeOwned<Stream>(memory_stream) };
  160. for (size_t i = 0; i < block.length / 18; ++i) {
  161. // 11.14. SEEKPOINT
  162. FlacSeekPoint seekpoint {
  163. .sample_index = LOADER_TRY(seektable_bytes.read_bits<u64>(64)),
  164. .byte_offset = LOADER_TRY(seektable_bytes.read_bits<u64>(64)),
  165. .num_samples = LOADER_TRY(seektable_bytes.read_bits<u16>(16))
  166. };
  167. m_seektable.append(seekpoint);
  168. }
  169. dbgln_if(AFLACLOADER_DEBUG, "Loaded seektable of size {}", m_seektable.size());
  170. return {};
  171. }
  172. // 11.6 METADATA_BLOCK
  173. ErrorOr<FlacRawMetadataBlock, LoaderError> FlacLoaderPlugin::next_meta_block(BigEndianInputBitStream& bit_input)
  174. {
  175. // 11.7 METADATA_BLOCK_HEADER
  176. bool is_last_block = LOADER_TRY(bit_input.read_bit());
  177. // The block type enum constants agree with the specification
  178. FlacMetadataBlockType type = (FlacMetadataBlockType)LOADER_TRY(bit_input.read_bits<u8>(7));
  179. m_data_start_location += 1;
  180. FLAC_VERIFY(type != FlacMetadataBlockType::INVALID, LoaderError::Category::Format, "Invalid metadata block");
  181. u32 block_length = LOADER_TRY(bit_input.read_bits<u32>(24));
  182. m_data_start_location += 3;
  183. // Blocks can be zero-sized, which would trip up the raw data reader below.
  184. if (block_length == 0)
  185. return FlacRawMetadataBlock {
  186. .is_last_block = is_last_block,
  187. .type = type,
  188. .length = 0,
  189. .data = LOADER_TRY(ByteBuffer::create_uninitialized(0))
  190. };
  191. auto block_data_result = ByteBuffer::create_uninitialized(block_length);
  192. FLAC_VERIFY(!block_data_result.is_error(), LoaderError::Category::IO, "Out of memory");
  193. auto block_data = block_data_result.release_value();
  194. // Blocks might exceed our buffer size.
  195. auto bytes_left_to_read = block_data.bytes();
  196. while (bytes_left_to_read.size()) {
  197. auto read_bytes = LOADER_TRY(bit_input.read(bytes_left_to_read));
  198. bytes_left_to_read = bytes_left_to_read.slice(read_bytes.size());
  199. }
  200. m_data_start_location += block_length;
  201. return FlacRawMetadataBlock {
  202. is_last_block,
  203. type,
  204. block_length,
  205. block_data,
  206. };
  207. }
  208. #undef FLAC_VERIFY
  209. MaybeLoaderError FlacLoaderPlugin::reset()
  210. {
  211. TRY(seek(0));
  212. m_current_frame.clear();
  213. return {};
  214. }
  215. MaybeLoaderError FlacLoaderPlugin::seek(int int_sample_index)
  216. {
  217. auto sample_index = static_cast<size_t>(int_sample_index);
  218. if (sample_index == m_loaded_samples)
  219. return {};
  220. auto maybe_target_seekpoint = m_seektable.last_matching([sample_index](auto& seekpoint) { return seekpoint.sample_index <= sample_index; });
  221. // No seektable or no fitting entry: Perform normal forward read
  222. if (!maybe_target_seekpoint.has_value()) {
  223. if (sample_index < m_loaded_samples) {
  224. LOADER_TRY(m_stream->seek(m_data_start_location, SeekMode::SetPosition));
  225. m_loaded_samples = 0;
  226. }
  227. auto to_read = sample_index - m_loaded_samples;
  228. if (to_read == 0)
  229. return {};
  230. dbgln_if(AFLACLOADER_DEBUG, "Seeking {} samples manually", to_read);
  231. (void)TRY(load_chunks(to_read));
  232. } else {
  233. auto target_seekpoint = maybe_target_seekpoint.release_value();
  234. // When a small seek happens, we may already be closer to the target than the seekpoint.
  235. if (sample_index - target_seekpoint.sample_index > sample_index - m_loaded_samples) {
  236. dbgln_if(AFLACLOADER_DEBUG, "Close enough to target: seeking {} samples manually", sample_index - m_loaded_samples);
  237. (void)TRY(load_chunks(sample_index - m_loaded_samples));
  238. return {};
  239. }
  240. dbgln_if(AFLACLOADER_DEBUG, "Seeking to seektable: sample index {}, byte offset {}, sample count {}", target_seekpoint.sample_index, target_seekpoint.byte_offset, target_seekpoint.num_samples);
  241. auto position = target_seekpoint.byte_offset + m_data_start_location;
  242. if (m_stream->seek(static_cast<i64>(position), SeekMode::SetPosition).is_error())
  243. return LoaderError { LoaderError::Category::IO, m_loaded_samples, DeprecatedString::formatted("Invalid seek position {}", position) };
  244. auto remaining_samples_after_seekpoint = sample_index - m_data_start_location;
  245. if (remaining_samples_after_seekpoint > 0)
  246. (void)TRY(load_chunks(remaining_samples_after_seekpoint));
  247. m_loaded_samples = target_seekpoint.sample_index;
  248. }
  249. return {};
  250. }
  251. ErrorOr<Vector<FixedArray<Sample>>, LoaderError> FlacLoaderPlugin::load_chunks(size_t samples_to_read_from_input)
  252. {
  253. ssize_t remaining_samples = static_cast<ssize_t>(m_total_samples - m_loaded_samples);
  254. if (remaining_samples <= 0)
  255. return Vector<FixedArray<Sample>> {};
  256. size_t samples_to_read = min(samples_to_read_from_input, remaining_samples);
  257. Vector<FixedArray<Sample>> frames;
  258. size_t sample_index = 0;
  259. while (sample_index < samples_to_read) {
  260. TRY(frames.try_append(TRY(next_frame())));
  261. sample_index += m_current_frame->sample_count;
  262. }
  263. m_loaded_samples += sample_index;
  264. return frames;
  265. }
  266. // 11.21. FRAME
  267. LoaderSamples FlacLoaderPlugin::next_frame()
  268. {
  269. #define FLAC_VERIFY(check, category, msg) \
  270. do { \
  271. if (!(check)) { \
  272. return LoaderError { category, static_cast<size_t>(m_current_sample_or_frame), DeprecatedString::formatted("FLAC header: {}", msg) }; \
  273. } \
  274. } while (0)
  275. BigEndianInputBitStream bit_stream { MaybeOwned<Stream>(*m_stream) };
  276. // TODO: Check the CRC-16 checksum (and others) by keeping track of read data
  277. // 11.22. FRAME_HEADER
  278. u16 sync_code = LOADER_TRY(bit_stream.read_bits<u16>(14));
  279. FLAC_VERIFY(sync_code == 0b11111111111110, LoaderError::Category::Format, "Sync code");
  280. bool reserved_bit = LOADER_TRY(bit_stream.read_bit());
  281. FLAC_VERIFY(reserved_bit == 0, LoaderError::Category::Format, "Reserved frame header bit");
  282. // 11.22.2. BLOCKING STRATEGY
  283. [[maybe_unused]] bool blocking_strategy = LOADER_TRY(bit_stream.read_bit());
  284. u32 sample_count = TRY(convert_sample_count_code(LOADER_TRY(bit_stream.read_bits<u8>(4))));
  285. u32 frame_sample_rate = TRY(convert_sample_rate_code(LOADER_TRY(bit_stream.read_bits<u8>(4))));
  286. u8 channel_type_num = LOADER_TRY(bit_stream.read_bits<u8>(4));
  287. FLAC_VERIFY(channel_type_num < 0b1011, LoaderError::Category::Format, "Channel assignment");
  288. FlacFrameChannelType channel_type = (FlacFrameChannelType)channel_type_num;
  289. PcmSampleFormat bit_depth = TRY(convert_bit_depth_code(LOADER_TRY(bit_stream.read_bits<u8>(3))));
  290. reserved_bit = LOADER_TRY(bit_stream.read_bit());
  291. FLAC_VERIFY(reserved_bit == 0, LoaderError::Category::Format, "Reserved frame header end bit");
  292. // 11.22.8. CODED NUMBER
  293. // FIXME: sample number can be 8-56 bits, frame number can be 8-48 bits
  294. m_current_sample_or_frame = LOADER_TRY(read_utf8_char(bit_stream));
  295. // Conditional header variables
  296. // 11.22.9. BLOCK SIZE INT
  297. if (sample_count == FLAC_BLOCKSIZE_AT_END_OF_HEADER_8) {
  298. sample_count = LOADER_TRY(bit_stream.read_bits<u32>(8)) + 1;
  299. } else if (sample_count == FLAC_BLOCKSIZE_AT_END_OF_HEADER_16) {
  300. sample_count = LOADER_TRY(bit_stream.read_bits<u32>(16)) + 1;
  301. }
  302. // 11.22.10. SAMPLE RATE INT
  303. if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_8) {
  304. frame_sample_rate = LOADER_TRY(bit_stream.read_bits<u32>(8)) * 1000;
  305. } else if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_16) {
  306. frame_sample_rate = LOADER_TRY(bit_stream.read_bits<u32>(16));
  307. } else if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10) {
  308. frame_sample_rate = LOADER_TRY(bit_stream.read_bits<u32>(16)) * 10;
  309. }
  310. // 11.22.11. FRAME CRC
  311. // TODO: check header checksum, see above
  312. [[maybe_unused]] u8 checksum = LOADER_TRY(bit_stream.read_bits<u8>(8));
  313. dbgln_if(AFLACLOADER_DEBUG, "Frame: {} samples, {}bit {}Hz, channeltype {:x}, {} number {}, header checksum {}", sample_count, pcm_bits_per_sample(bit_depth), frame_sample_rate, channel_type_num, blocking_strategy ? "sample" : "frame", m_current_sample_or_frame, checksum);
  314. m_current_frame = FlacFrameHeader {
  315. sample_count,
  316. frame_sample_rate,
  317. channel_type,
  318. bit_depth,
  319. };
  320. u8 subframe_count = frame_channel_type_to_channel_count(channel_type);
  321. Vector<Vector<i32>> current_subframes;
  322. current_subframes.ensure_capacity(subframe_count);
  323. for (u8 i = 0; i < subframe_count; ++i) {
  324. FlacSubframeHeader new_subframe = TRY(next_subframe_header(bit_stream, i));
  325. Vector<i32> subframe_samples = TRY(parse_subframe(new_subframe, bit_stream));
  326. VERIFY(subframe_samples.size() == m_current_frame->sample_count);
  327. current_subframes.unchecked_append(move(subframe_samples));
  328. }
  329. // 11.2. Overview ("The audio data is composed of...")
  330. bit_stream.align_to_byte_boundary();
  331. // 11.23. FRAME_FOOTER
  332. // TODO: check checksum, see above
  333. [[maybe_unused]] u16 footer_checksum = LOADER_TRY(bit_stream.read_bits<u16>(16));
  334. dbgln_if(AFLACLOADER_DEBUG, "Subframe footer checksum: {}", footer_checksum);
  335. float sample_rescale = 1 / static_cast<float>(1 << (pcm_bits_per_sample(m_current_frame->bit_depth) - 1));
  336. dbgln_if(AFLACLOADER_DEBUG, "Sample rescaled from {} bits: factor {:.1f}", pcm_bits_per_sample(m_current_frame->bit_depth), sample_rescale);
  337. FixedArray<Sample> samples = TRY(FixedArray<Sample>::create(m_current_frame->sample_count));
  338. switch (channel_type) {
  339. case FlacFrameChannelType::Mono:
  340. for (size_t i = 0; i < m_current_frame->sample_count; ++i)
  341. samples[i] = Sample { static_cast<float>(current_subframes[0][i]) * sample_rescale };
  342. break;
  343. case FlacFrameChannelType::Stereo:
  344. // TODO mix together surround channels on each side?
  345. case FlacFrameChannelType::StereoCenter:
  346. case FlacFrameChannelType::Surround4p0:
  347. case FlacFrameChannelType::Surround5p0:
  348. case FlacFrameChannelType::Surround5p1:
  349. case FlacFrameChannelType::Surround6p1:
  350. case FlacFrameChannelType::Surround7p1:
  351. for (size_t i = 0; i < m_current_frame->sample_count; ++i)
  352. samples[i] = { static_cast<float>(current_subframes[0][i]) * sample_rescale, static_cast<float>(current_subframes[1][i]) * sample_rescale };
  353. break;
  354. case FlacFrameChannelType::LeftSideStereo:
  355. // channels are left (0) and side (1)
  356. for (size_t i = 0; i < m_current_frame->sample_count; ++i) {
  357. // right = left - side
  358. samples[i] = { static_cast<float>(current_subframes[0][i]) * sample_rescale,
  359. static_cast<float>(current_subframes[0][i] - current_subframes[1][i]) * sample_rescale };
  360. }
  361. break;
  362. case FlacFrameChannelType::RightSideStereo:
  363. // channels are side (0) and right (1)
  364. for (size_t i = 0; i < m_current_frame->sample_count; ++i) {
  365. // left = right + side
  366. samples[i] = { static_cast<float>(current_subframes[1][i] + current_subframes[0][i]) * sample_rescale,
  367. static_cast<float>(current_subframes[1][i]) * sample_rescale };
  368. }
  369. break;
  370. case FlacFrameChannelType::MidSideStereo:
  371. // channels are mid (0) and side (1)
  372. for (size_t i = 0; i < current_subframes[0].size(); ++i) {
  373. i64 mid = current_subframes[0][i];
  374. i64 side = current_subframes[1][i];
  375. mid *= 2;
  376. // prevent integer division errors
  377. samples[i] = { static_cast<float>((mid + side) * .5f) * sample_rescale,
  378. static_cast<float>((mid - side) * .5f) * sample_rescale };
  379. }
  380. break;
  381. }
  382. return samples;
  383. #undef FLAC_VERIFY
  384. }
  385. // 11.22.3. INTERCHANNEL SAMPLE BLOCK SIZE
  386. ErrorOr<u32, LoaderError> FlacLoaderPlugin::convert_sample_count_code(u8 sample_count_code)
  387. {
  388. // single codes
  389. switch (sample_count_code) {
  390. case 0:
  391. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved block size" };
  392. case 1:
  393. return 192;
  394. case 6:
  395. return FLAC_BLOCKSIZE_AT_END_OF_HEADER_8;
  396. case 7:
  397. return FLAC_BLOCKSIZE_AT_END_OF_HEADER_16;
  398. }
  399. if (sample_count_code >= 2 && sample_count_code <= 5) {
  400. return 576 * AK::exp2(sample_count_code - 2);
  401. }
  402. return 256 * AK::exp2(sample_count_code - 8);
  403. }
  404. // 11.22.4. SAMPLE RATE
  405. ErrorOr<u32, LoaderError> FlacLoaderPlugin::convert_sample_rate_code(u8 sample_rate_code)
  406. {
  407. switch (sample_rate_code) {
  408. case 0:
  409. return m_sample_rate;
  410. case 1:
  411. return 88200;
  412. case 2:
  413. return 176400;
  414. case 3:
  415. return 192000;
  416. case 4:
  417. return 8000;
  418. case 5:
  419. return 16000;
  420. case 6:
  421. return 22050;
  422. case 7:
  423. return 24000;
  424. case 8:
  425. return 32000;
  426. case 9:
  427. return 44100;
  428. case 10:
  429. return 48000;
  430. case 11:
  431. return 96000;
  432. case 12:
  433. return FLAC_SAMPLERATE_AT_END_OF_HEADER_8;
  434. case 13:
  435. return FLAC_SAMPLERATE_AT_END_OF_HEADER_16;
  436. case 14:
  437. return FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10;
  438. default:
  439. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Invalid sample rate code" };
  440. }
  441. }
  442. // 11.22.6. SAMPLE SIZE
  443. ErrorOr<PcmSampleFormat, LoaderError> FlacLoaderPlugin::convert_bit_depth_code(u8 bit_depth_code)
  444. {
  445. switch (bit_depth_code) {
  446. case 0:
  447. return m_sample_format;
  448. case 1:
  449. return PcmSampleFormat::Uint8;
  450. case 4:
  451. return PcmSampleFormat::Int16;
  452. case 6:
  453. return PcmSampleFormat::Int24;
  454. case 3:
  455. case 7:
  456. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved sample size" };
  457. default:
  458. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), DeprecatedString::formatted("Unsupported sample size {}", bit_depth_code) };
  459. }
  460. }
  461. // 11.22.5. CHANNEL ASSIGNMENT
  462. u8 frame_channel_type_to_channel_count(FlacFrameChannelType channel_type)
  463. {
  464. if (channel_type <= FlacFrameChannelType::Surround7p1)
  465. return to_underlying(channel_type) + 1;
  466. return 2;
  467. }
  468. // 11.25. SUBFRAME_HEADER
  469. ErrorOr<FlacSubframeHeader, LoaderError> FlacLoaderPlugin::next_subframe_header(BigEndianInputBitStream& bit_stream, u8 channel_index)
  470. {
  471. u8 bits_per_sample = static_cast<u16>(pcm_bits_per_sample(m_current_frame->bit_depth));
  472. // For inter-channel correlation, the side channel needs an extra bit for its samples
  473. switch (m_current_frame->channels) {
  474. case FlacFrameChannelType::LeftSideStereo:
  475. case FlacFrameChannelType::MidSideStereo:
  476. if (channel_index == 1) {
  477. ++bits_per_sample;
  478. }
  479. break;
  480. case FlacFrameChannelType::RightSideStereo:
  481. if (channel_index == 0) {
  482. ++bits_per_sample;
  483. }
  484. break;
  485. // "normal" channel types
  486. default:
  487. break;
  488. }
  489. // zero-bit padding
  490. if (LOADER_TRY(bit_stream.read_bit()) != 0)
  491. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Zero bit padding" };
  492. // 11.25.1. SUBFRAME TYPE
  493. u8 subframe_code = LOADER_TRY(bit_stream.read_bits<u8>(6));
  494. if ((subframe_code >= 0b000010 && subframe_code <= 0b000111) || (subframe_code > 0b001100 && subframe_code < 0b100000))
  495. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Subframe type" };
  496. FlacSubframeType subframe_type;
  497. u8 order = 0;
  498. // LPC has the highest bit set
  499. if ((subframe_code & 0b100000) > 0) {
  500. subframe_type = FlacSubframeType::LPC;
  501. order = (subframe_code & 0b011111) + 1;
  502. } else if ((subframe_code & 0b001000) > 0) {
  503. // Fixed has the third-highest bit set
  504. subframe_type = FlacSubframeType::Fixed;
  505. order = (subframe_code & 0b000111);
  506. } else {
  507. subframe_type = (FlacSubframeType)subframe_code;
  508. }
  509. // 11.25.2. WASTED BITS PER SAMPLE FLAG
  510. bool has_wasted_bits = LOADER_TRY(bit_stream.read_bit());
  511. u8 k = 0;
  512. if (has_wasted_bits) {
  513. bool current_k_bit = 0;
  514. do {
  515. current_k_bit = LOADER_TRY(bit_stream.read_bit());
  516. ++k;
  517. } while (current_k_bit != 1);
  518. }
  519. return FlacSubframeHeader {
  520. subframe_type,
  521. order,
  522. k,
  523. bits_per_sample
  524. };
  525. }
  526. ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::parse_subframe(FlacSubframeHeader& subframe_header, BigEndianInputBitStream& bit_input)
  527. {
  528. Vector<i32> samples;
  529. switch (subframe_header.type) {
  530. case FlacSubframeType::Constant: {
  531. // 11.26. SUBFRAME_CONSTANT
  532. u64 constant_value = LOADER_TRY(bit_input.read_bits<u64>(subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample));
  533. dbgln_if(AFLACLOADER_DEBUG, "Constant subframe: {}", constant_value);
  534. samples.ensure_capacity(m_current_frame->sample_count);
  535. VERIFY(subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample != 0);
  536. i32 constant = sign_extend(static_cast<u32>(constant_value), subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample);
  537. for (u32 i = 0; i < m_current_frame->sample_count; ++i) {
  538. samples.unchecked_append(constant);
  539. }
  540. break;
  541. }
  542. case FlacSubframeType::Fixed: {
  543. dbgln_if(AFLACLOADER_DEBUG, "Fixed LPC subframe order {}", subframe_header.order);
  544. samples = TRY(decode_fixed_lpc(subframe_header, bit_input));
  545. break;
  546. }
  547. case FlacSubframeType::Verbatim: {
  548. dbgln_if(AFLACLOADER_DEBUG, "Verbatim subframe");
  549. samples = TRY(decode_verbatim(subframe_header, bit_input));
  550. break;
  551. }
  552. case FlacSubframeType::LPC: {
  553. dbgln_if(AFLACLOADER_DEBUG, "Custom LPC subframe order {}", subframe_header.order);
  554. samples = TRY(decode_custom_lpc(subframe_header, bit_input));
  555. break;
  556. }
  557. default:
  558. return LoaderError { LoaderError::Category::Unimplemented, static_cast<size_t>(m_current_sample_or_frame), "Unhandled FLAC subframe type" };
  559. }
  560. for (size_t i = 0; i < samples.size(); ++i) {
  561. samples[i] <<= subframe_header.wasted_bits_per_sample;
  562. }
  563. ResampleHelper<i32> resampler(m_current_frame->sample_rate, m_sample_rate);
  564. return resampler.resample(samples);
  565. }
  566. // 11.29. SUBFRAME_VERBATIM
  567. // Decode a subframe that isn't actually encoded, usually seen in random data
  568. ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_verbatim(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  569. {
  570. Vector<i32> decoded;
  571. decoded.ensure_capacity(m_current_frame->sample_count);
  572. VERIFY(subframe.bits_per_sample - subframe.wasted_bits_per_sample != 0);
  573. for (size_t i = 0; i < m_current_frame->sample_count; ++i) {
  574. decoded.unchecked_append(sign_extend(
  575. LOADER_TRY(bit_input.read_bits<u32>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
  576. subframe.bits_per_sample - subframe.wasted_bits_per_sample));
  577. }
  578. return decoded;
  579. }
  580. // 11.28. SUBFRAME_LPC
  581. // Decode a subframe encoded with a custom linear predictor coding, i.e. the subframe provides the polynomial order and coefficients
  582. ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_custom_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  583. {
  584. Vector<i32> decoded;
  585. decoded.ensure_capacity(m_current_frame->sample_count);
  586. VERIFY(subframe.bits_per_sample - subframe.wasted_bits_per_sample != 0);
  587. // warm-up samples
  588. for (auto i = 0; i < subframe.order; ++i) {
  589. decoded.unchecked_append(sign_extend(
  590. LOADER_TRY(bit_input.read_bits<u32>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
  591. subframe.bits_per_sample - subframe.wasted_bits_per_sample));
  592. }
  593. // precision of the coefficients
  594. u8 lpc_precision = LOADER_TRY(bit_input.read_bits<u8>(4));
  595. if (lpc_precision == 0b1111)
  596. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Invalid linear predictor coefficient precision" };
  597. lpc_precision += 1;
  598. // shift needed on the data (signed!)
  599. i8 lpc_shift = sign_extend(LOADER_TRY(bit_input.read_bits<u8>(5)), 5);
  600. Vector<i32> coefficients;
  601. coefficients.ensure_capacity(subframe.order);
  602. // read coefficients
  603. for (auto i = 0; i < subframe.order; ++i) {
  604. u32 raw_coefficient = LOADER_TRY(bit_input.read_bits<u32>(lpc_precision));
  605. i32 coefficient = static_cast<i32>(sign_extend(raw_coefficient, lpc_precision));
  606. coefficients.unchecked_append(coefficient);
  607. }
  608. dbgln_if(AFLACLOADER_DEBUG, "{}-bit {} shift coefficients: {}", lpc_precision, lpc_shift, coefficients);
  609. TRY(decode_residual(decoded, subframe, bit_input));
  610. // approximate the waveform with the predictor
  611. for (size_t i = subframe.order; i < m_current_frame->sample_count; ++i) {
  612. // (see below)
  613. i64 sample = 0;
  614. for (size_t t = 0; t < subframe.order; ++t) {
  615. // It's really important that we compute in 64-bit land here.
  616. // Even though FLAC operates at a maximum bit depth of 32 bits, modern encoders use super-large coefficients for maximum compression.
  617. // These will easily overflow 32 bits and cause strange white noise that abruptly stops intermittently (at the end of a frame).
  618. // The simple fix of course is to do intermediate computations in 64 bits.
  619. // These considerations are not in the original FLAC spec, but have been added to the IETF standard: https://datatracker.ietf.org/doc/html/draft-ietf-cellar-flac-03#appendix-A.3
  620. sample += static_cast<i64>(coefficients[t]) * static_cast<i64>(decoded[i - t - 1]);
  621. }
  622. decoded[i] += sample >> lpc_shift;
  623. }
  624. return decoded;
  625. }
  626. // 11.27. SUBFRAME_FIXED
  627. // Decode a subframe encoded with one of the fixed linear predictor codings
  628. ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_fixed_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  629. {
  630. Vector<i32> decoded;
  631. decoded.ensure_capacity(m_current_frame->sample_count);
  632. VERIFY(subframe.bits_per_sample - subframe.wasted_bits_per_sample != 0);
  633. // warm-up samples
  634. for (auto i = 0; i < subframe.order; ++i) {
  635. decoded.unchecked_append(sign_extend(
  636. LOADER_TRY(bit_input.read_bits<u32>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
  637. subframe.bits_per_sample - subframe.wasted_bits_per_sample));
  638. }
  639. TRY(decode_residual(decoded, subframe, bit_input));
  640. dbgln_if(AFLACLOADER_DEBUG, "decoded length {}, {} order predictor", decoded.size(), subframe.order);
  641. // Skip these comments if you don't care about the neat math behind fixed LPC :^)
  642. // These coefficients for the recursive prediction formula are the only ones that can be resolved to polynomial predictor functions.
  643. // The order equals the degree of the polynomial - 1, so the second-order predictor has an underlying polynomial of degree 1, a straight line.
  644. // More specifically, the closest approximation to a polynomial is used, and the degree depends on how many previous values are available.
  645. // This makes use of a very neat property of polynomials, which is that they are entirely characterized by their finitely many derivatives.
  646. // (Mathematically speaking, the infinite Taylor series of any polynomial equals the polynomial itself.)
  647. // Now remember that derivation is just the slope of the function, which is the same as the difference of two close-by values.
  648. // Therefore, with two samples we can calculate the first derivative at a sample via the difference, which gives us a polynomial of degree 1.
  649. // With three samples, we can do the same but also calculate the second derivative via the difference in the first derivatives.
  650. // This gives us a polynomial of degree 2, as it has two "proper" (non-constant) derivatives.
  651. // This can be continued for higher-order derivatives when we have more coefficients, giving us higher-order polynomials.
  652. // In essence, it's akin to a Lagrangian polynomial interpolation for every sample (but already pre-solved).
  653. // The coefficients for orders 0-3 originate from the SHORTEN codec:
  654. // http://mi.eng.cam.ac.uk/reports/svr-ftp/auto-pdf/robinson_tr156.pdf page 4
  655. // The coefficients for order 4 are undocumented in the original FLAC specification(s), but can now be found in
  656. // https://datatracker.ietf.org/doc/html/draft-ietf-cellar-flac-03#section-10.2.5
  657. switch (subframe.order) {
  658. case 0:
  659. // s_0(t) = 0
  660. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  661. decoded[i] += 0;
  662. break;
  663. case 1:
  664. // s_1(t) = s(t-1)
  665. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  666. decoded[i] += decoded[i - 1];
  667. break;
  668. case 2:
  669. // s_2(t) = 2s(t-1) - s(t-2)
  670. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  671. decoded[i] += 2 * decoded[i - 1] - decoded[i - 2];
  672. break;
  673. case 3:
  674. // s_3(t) = 3s(t-1) - 3s(t-2) + s(t-3)
  675. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  676. decoded[i] += 3 * decoded[i - 1] - 3 * decoded[i - 2] + decoded[i - 3];
  677. break;
  678. case 4:
  679. // s_4(t) = 4s(t-1) - 6s(t-2) + 4s(t-3) - s(t-4)
  680. for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
  681. decoded[i] += 4 * decoded[i - 1] - 6 * decoded[i - 2] + 4 * decoded[i - 3] - decoded[i - 4];
  682. break;
  683. default:
  684. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), DeprecatedString::formatted("Unrecognized predictor order {}", subframe.order) };
  685. }
  686. return decoded;
  687. }
  688. // 11.30. RESIDUAL
  689. // Decode the residual, the "error" between the function approximation and the actual audio data
  690. MaybeLoaderError FlacLoaderPlugin::decode_residual(Vector<i32>& decoded, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  691. {
  692. // 11.30.1. RESIDUAL_CODING_METHOD
  693. auto residual_mode = static_cast<FlacResidualMode>(LOADER_TRY(bit_input.read_bits<u8>(2)));
  694. u8 partition_order = LOADER_TRY(bit_input.read_bits<u8>(4));
  695. size_t partitions = 1 << partition_order;
  696. if (residual_mode == FlacResidualMode::Rice4Bit) {
  697. // 11.30.2. RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB
  698. // decode a single Rice partition with four bits for the order k
  699. for (size_t i = 0; i < partitions; ++i) {
  700. auto rice_partition = TRY(decode_rice_partition(4, partitions, i, subframe, bit_input));
  701. decoded.extend(move(rice_partition));
  702. }
  703. } else if (residual_mode == FlacResidualMode::Rice5Bit) {
  704. // 11.30.3. RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB2
  705. // five bits equivalent
  706. for (size_t i = 0; i < partitions; ++i) {
  707. auto rice_partition = TRY(decode_rice_partition(5, partitions, i, subframe, bit_input));
  708. decoded.extend(move(rice_partition));
  709. }
  710. } else
  711. return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved residual coding method" };
  712. return {};
  713. }
  714. // 11.30.2.1. EXP_GOLOMB_PARTITION and 11.30.3.1. EXP_GOLOMB2_PARTITION
  715. // Decode a single Rice partition as part of the residual, every partition can have its own Rice parameter k
  716. ALWAYS_INLINE ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_rice_partition(u8 partition_type, u32 partitions, u32 partition_index, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
  717. {
  718. // 11.30.2.2. EXP GOLOMB PARTITION ENCODING PARAMETER and 11.30.3.2. EXP-GOLOMB2 PARTITION ENCODING PARAMETER
  719. u8 k = LOADER_TRY(bit_input.read_bits<u8>(partition_type));
  720. u32 residual_sample_count;
  721. if (partitions == 0)
  722. residual_sample_count = m_current_frame->sample_count - subframe.order;
  723. else
  724. residual_sample_count = m_current_frame->sample_count / partitions;
  725. if (partition_index == 0)
  726. residual_sample_count -= subframe.order;
  727. Vector<i32> rice_partition;
  728. rice_partition.resize(residual_sample_count);
  729. // escape code for unencoded binary partition
  730. if (k == (1 << partition_type) - 1) {
  731. u8 unencoded_bps = LOADER_TRY(bit_input.read_bits<u8>(5));
  732. for (size_t r = 0; r < residual_sample_count; ++r) {
  733. rice_partition[r] = LOADER_TRY(bit_input.read_bits<u8>(unencoded_bps));
  734. }
  735. } else {
  736. for (size_t r = 0; r < residual_sample_count; ++r) {
  737. rice_partition[r] = LOADER_TRY(decode_unsigned_exp_golomb(k, bit_input));
  738. }
  739. }
  740. return rice_partition;
  741. }
  742. // Decode a single number encoded with Rice/Exponential-Golomb encoding (the unsigned variant)
  743. ALWAYS_INLINE ErrorOr<i32> decode_unsigned_exp_golomb(u8 k, BigEndianInputBitStream& bit_input)
  744. {
  745. u8 q = 0;
  746. while (TRY(bit_input.read_bit()) == 0)
  747. ++q;
  748. // least significant bits (remainder)
  749. u32 rem = TRY(bit_input.read_bits<u32>(k));
  750. u32 value = q << k | rem;
  751. return rice_to_signed(value);
  752. }
  753. ErrorOr<u64> read_utf8_char(BigEndianInputBitStream& input)
  754. {
  755. u64 character;
  756. u8 buffer = 0;
  757. Bytes buffer_bytes { &buffer, 1 };
  758. TRY(input.read(buffer_bytes));
  759. u8 start_byte = buffer_bytes[0];
  760. // Signal byte is zero: ASCII character
  761. if ((start_byte & 0b10000000) == 0) {
  762. return start_byte;
  763. } else if ((start_byte & 0b11000000) == 0b10000000) {
  764. return Error::from_string_literal("Illegal continuation byte");
  765. }
  766. // This algorithm is too good and supports the theoretical max 0xFF start byte
  767. u8 length = 1;
  768. while (((start_byte << length) & 0b10000000) == 0b10000000)
  769. ++length;
  770. u8 bits_from_start_byte = 8 - (length + 1);
  771. u8 start_byte_bitmask = AK::exp2(bits_from_start_byte) - 1;
  772. character = start_byte_bitmask & start_byte;
  773. for (u8 i = length - 1; i > 0; --i) {
  774. TRY(input.read(buffer_bytes));
  775. u8 current_byte = buffer_bytes[0];
  776. character = (character << 6) | (current_byte & 0b00111111);
  777. }
  778. return character;
  779. }
  780. i64 sign_extend(u32 n, u8 size)
  781. {
  782. // negative
  783. if ((n & (1 << (size - 1))) > 0) {
  784. return static_cast<i64>(n | (0xffffffff << size));
  785. }
  786. // positive
  787. return n;
  788. }
  789. i32 rice_to_signed(u32 x)
  790. {
  791. // positive numbers are even, negative numbers are odd
  792. // bitmask for conditionally inverting the entire number, thereby "negating" it
  793. i32 sign = -static_cast<i32>(x & 1);
  794. // copies the sign's sign onto the actual magnitude of x
  795. return static_cast<i32>(sign ^ (x >> 1));
  796. }
  797. }