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