summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibAudio/FlacLoader.cpp
blob: c5e20f656b06ca27026863b51f8449a4182b75bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/*
 * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Debug.h>
#include <AK/DeprecatedFlyString.h>
#include <AK/DeprecatedString.h>
#include <AK/FixedArray.h>
#include <AK/Format.h>
#include <AK/IntegralMath.h>
#include <AK/Math.h>
#include <AK/MemoryStream.h>
#include <AK/ScopeGuard.h>
#include <AK/StdLibExtras.h>
#include <AK/Try.h>
#include <AK/TypedTransfer.h>
#include <AK/UFixedBigInt.h>
#include <LibAudio/FlacLoader.h>
#include <LibAudio/FlacTypes.h>
#include <LibAudio/GenericTypes.h>
#include <LibAudio/LoaderError.h>
#include <LibAudio/Resampler.h>
#include <LibAudio/VorbisComment.h>
#include <LibCore/File.h>
#include <LibCrypto/Checksum/ChecksumFunction.h>
#include <LibCrypto/Checksum/ChecksummingStream.h>

namespace Audio {

FlacLoaderPlugin::FlacLoaderPlugin(NonnullOwnPtr<SeekableStream> stream)
    : LoaderPlugin(move(stream))
{
}

Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> FlacLoaderPlugin::create(StringView path)
{
    auto stream = LOADER_TRY(Core::InputBufferedFile::create(LOADER_TRY(Core::File::open(path, Core::File::OpenMode::Read))));
    auto loader = make<FlacLoaderPlugin>(move(stream));

    LOADER_TRY(loader->initialize());

    return loader;
}

Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> FlacLoaderPlugin::create(Bytes buffer)
{
    auto stream = LOADER_TRY(try_make<FixedMemoryStream>(buffer));
    auto loader = make<FlacLoaderPlugin>(move(stream));

    LOADER_TRY(loader->initialize());

    return loader;
}

MaybeLoaderError FlacLoaderPlugin::initialize()
{
    TRY(parse_header());
    TRY(reset());
    return {};
}

// 11.5 STREAM
MaybeLoaderError FlacLoaderPlugin::parse_header()
{
    BigEndianInputBitStream bit_input { MaybeOwned<Stream>(*m_stream) };

    // A mixture of VERIFY and the non-crashing TRY().
#define FLAC_VERIFY(check, category, msg)                                                                                       \
    do {                                                                                                                        \
        if (!(check)) {                                                                                                         \
            return LoaderError { category, LOADER_TRY(m_stream->tell()), DeprecatedString::formatted("FLAC header: {}", msg) }; \
        }                                                                                                                       \
    } while (0)

    // Magic number
    u32 flac = LOADER_TRY(bit_input.read_bits<u32>(32));
    m_data_start_location += 4;
    FLAC_VERIFY(flac == 0x664C6143, LoaderError::Category::Format, "Magic number must be 'flaC'"); // "flaC"

    // Receive the streaminfo block
    auto streaminfo = TRY(next_meta_block(bit_input));
    FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO");
    FixedMemoryStream streaminfo_data_memory { streaminfo.data.bytes() };
    BigEndianInputBitStream streaminfo_data { MaybeOwned<Stream>(streaminfo_data_memory) };

    // 11.10 METADATA_BLOCK_STREAMINFO
    m_min_block_size = LOADER_TRY(streaminfo_data.read_bits<u16>(16));
    FLAC_VERIFY(m_min_block_size >= 16, LoaderError::Category::Format, "Minimum block size must be 16");
    m_max_block_size = LOADER_TRY(streaminfo_data.read_bits<u16>(16));
    FLAC_VERIFY(m_max_block_size >= 16, LoaderError::Category::Format, "Maximum block size");
    m_min_frame_size = LOADER_TRY(streaminfo_data.read_bits<u32>(24));
    m_max_frame_size = LOADER_TRY(streaminfo_data.read_bits<u32>(24));
    m_sample_rate = LOADER_TRY(streaminfo_data.read_bits<u32>(20));
    FLAC_VERIFY(m_sample_rate <= 655350, LoaderError::Category::Format, "Sample rate");
    m_num_channels = LOADER_TRY(streaminfo_data.read_bits<u8>(3)) + 1; // 0 = one channel

    m_bits_per_sample = LOADER_TRY(streaminfo_data.read_bits<u8>(5)) + 1;
    if (m_bits_per_sample <= 8) {
        // FIXME: Signed/Unsigned issues?
        m_sample_format = PcmSampleFormat::Uint8;
    } else if (m_bits_per_sample <= 16) {
        m_sample_format = PcmSampleFormat::Int16;
    } else if (m_bits_per_sample <= 24) {
        m_sample_format = PcmSampleFormat::Int24;
    } else if (m_bits_per_sample <= 32) {
        m_sample_format = PcmSampleFormat::Int32;
    } else {
        FLAC_VERIFY(false, LoaderError::Category::Format, "Sample bit depth too large");
    }

    m_total_samples = LOADER_TRY(streaminfo_data.read_bits<u64>(36));
    if (m_total_samples == 0) {
        // "A value of zero here means the number of total samples is unknown."
        dbgln("FLAC Warning: File has unknown amount of samples, the loader will not stop before EOF");
        m_total_samples = NumericLimits<decltype(m_total_samples)>::max();
    }

    VERIFY(streaminfo_data.is_aligned_to_byte_boundary());
    LOADER_TRY(streaminfo_data.read_until_filled({ m_md5_checksum, sizeof(m_md5_checksum) }));

    // Parse other blocks
    [[maybe_unused]] u16 meta_blocks_parsed = 1;
    [[maybe_unused]] u16 total_meta_blocks = meta_blocks_parsed;
    FlacRawMetadataBlock block = streaminfo;
    while (!block.is_last_block) {
        block = TRY(next_meta_block(bit_input));
        switch (block.type) {
        case (FlacMetadataBlockType::SEEKTABLE):
            TRY(load_seektable(block));
            break;
        case FlacMetadataBlockType::PICTURE:
            TRY(load_picture(block));
            break;
        case FlacMetadataBlockType::APPLICATION:
            // Note: Third-party library can encode specific data in this.
            dbgln("FLAC Warning: Unknown 'Application' metadata block encountered.");
            [[fallthrough]];
        case FlacMetadataBlockType::PADDING:
            // Note: A padding block is empty and does not need any treatment.
            break;
        case FlacMetadataBlockType::VORBIS_COMMENT:
            load_vorbis_comment(block);
            break;
        default:
            // TODO: Parse the remaining metadata block types.
            break;
        }
        ++total_meta_blocks;
    }

    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);

    return {};
}

// 11.19. METADATA_BLOCK_PICTURE
MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
{
    FixedMemoryStream memory_stream { block.data.bytes() };
    BigEndianInputBitStream picture_block_bytes { MaybeOwned<Stream>(memory_stream) };

    PictureData picture {};

    picture.type = static_cast<ID3PictureType>(LOADER_TRY(picture_block_bytes.read_bits(32)));

    auto const mime_string_length = LOADER_TRY(picture_block_bytes.read_bits(32));
    // Note: We are seeking before reading the value to ensure that we stayed inside buffer's size.
    auto offset_before_seeking = memory_stream.offset();
    LOADER_TRY(memory_stream.seek(mime_string_length, SeekMode::FromCurrentPosition));
    picture.mime_string = { block.data.bytes().data() + offset_before_seeking, (size_t)mime_string_length };

    auto const description_string_length = LOADER_TRY(picture_block_bytes.read_bits(32));
    offset_before_seeking = memory_stream.offset();
    LOADER_TRY(memory_stream.seek(description_string_length, SeekMode::FromCurrentPosition));
    picture.description_string = Vector<u32> { Span<u32> { reinterpret_cast<u32*>(block.data.bytes().data() + offset_before_seeking), (size_t)description_string_length } };

    picture.width = LOADER_TRY(picture_block_bytes.read_bits(32));
    picture.height = LOADER_TRY(picture_block_bytes.read_bits(32));

    picture.color_depth = LOADER_TRY(picture_block_bytes.read_bits(32));
    picture.colors = LOADER_TRY(picture_block_bytes.read_bits(32));

    auto const picture_size = LOADER_TRY(picture_block_bytes.read_bits(32));
    offset_before_seeking = memory_stream.offset();
    LOADER_TRY(memory_stream.seek(picture_size, SeekMode::FromCurrentPosition));
    picture.data = Vector<u8> { Span<u8> { block.data.bytes().data() + offset_before_seeking, (size_t)picture_size } };

    m_pictures.append(move(picture));

    return {};
}

// 11.15. METADATA_BLOCK_VORBIS_COMMENT
void FlacLoaderPlugin::load_vorbis_comment(FlacRawMetadataBlock& block)
{
    auto metadata_or_error = Audio::load_vorbis_comment(block.data);
    if (metadata_or_error.is_error()) {
        dbgln("FLAC Warning: Vorbis comment invalid, error: {}", metadata_or_error.release_error());
        return;
    }
    m_metadata = metadata_or_error.release_value();
}

// 11.13. METADATA_BLOCK_SEEKTABLE
MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block)
{
    FixedMemoryStream memory_stream { block.data.bytes() };
    BigEndianInputBitStream seektable_bytes { MaybeOwned<Stream>(memory_stream) };
    for (size_t i = 0; i < block.length / 18; ++i) {
        // 11.14. SEEKPOINT
        u64 sample_index = LOADER_TRY(seektable_bytes.read_bits<u64>(64));
        u64 byte_offset = LOADER_TRY(seektable_bytes.read_bits<u64>(64));
        // The sample count of a seek point is not relevant to us.
        [[maybe_unused]] u16 sample_count = LOADER_TRY(seektable_bytes.read_bits<u16>(16));
        // Placeholder, to be ignored.
        if (sample_index == 0xFFFFFFFFFFFFFFFF)
            continue;

        SeekPoint seekpoint {
            .sample_index = sample_index,
            .byte_offset = byte_offset,
        };
        TRY(m_seektable.insert_seek_point(seekpoint));
    }
    dbgln_if(AFLACLOADER_DEBUG, "Loaded seektable of size {}", m_seektable.size());
    return {};
}

// 11.6 METADATA_BLOCK
ErrorOr<FlacRawMetadataBlock, LoaderError> FlacLoaderPlugin::next_meta_block(BigEndianInputBitStream& bit_input)
{
    // 11.7 METADATA_BLOCK_HEADER
    bool is_last_block = LOADER_TRY(bit_input.read_bit());
    // The block type enum constants agree with the specification
    FlacMetadataBlockType type = (FlacMetadataBlockType)LOADER_TRY(bit_input.read_bits<u8>(7));
    m_data_start_location += 1;
    FLAC_VERIFY(type != FlacMetadataBlockType::INVALID, LoaderError::Category::Format, "Invalid metadata block");

    u32 block_length = LOADER_TRY(bit_input.read_bits<u32>(24));
    m_data_start_location += 3;
    // Blocks can be zero-sized, which would trip up the raw data reader below.
    if (block_length == 0)
        return FlacRawMetadataBlock {
            .is_last_block = is_last_block,
            .type = type,
            .length = 0,
            .data = LOADER_TRY(ByteBuffer::create_uninitialized(0))
        };
    auto block_data_result = ByteBuffer::create_uninitialized(block_length);
    FLAC_VERIFY(!block_data_result.is_error(), LoaderError::Category::IO, "Out of memory");
    auto block_data = block_data_result.release_value();

    LOADER_TRY(bit_input.read_until_filled(block_data));

    m_data_start_location += block_length;
    return FlacRawMetadataBlock {
        is_last_block,
        type,
        block_length,
        block_data,
    };
}
#undef FLAC_VERIFY

MaybeLoaderError FlacLoaderPlugin::reset()
{
    TRY(seek(0));
    m_current_frame.clear();
    return {};
}

MaybeLoaderError FlacLoaderPlugin::seek(int int_sample_index)
{
    auto sample_index = static_cast<size_t>(int_sample_index);
    if (sample_index == m_loaded_samples)
        return {};

    auto maybe_target_seekpoint = m_seektable.seek_point_before(sample_index);
    auto const seek_tolerance = (seek_tolerance_ms * m_sample_rate) / 1000;
    // No seektable or no fitting entry: Perform normal forward read
    if (!maybe_target_seekpoint.has_value()) {
        if (sample_index < m_loaded_samples) {
            LOADER_TRY(m_stream->seek(m_data_start_location, SeekMode::SetPosition));
            m_loaded_samples = 0;
        }
        if (sample_index - m_loaded_samples == 0)
            return {};
        dbgln_if(AFLACLOADER_DEBUG, "Seeking {} samples manually", sample_index - m_loaded_samples);
    } else {
        auto target_seekpoint = maybe_target_seekpoint.release_value();

        // When a small seek happens, we may already be closer to the target than the seekpoint.
        if (sample_index - target_seekpoint.sample_index > sample_index - m_loaded_samples) {
            dbgln_if(AFLACLOADER_DEBUG, "Close enough to target ({} samples): not seeking", sample_index - m_loaded_samples);
            return {};
        }

        dbgln_if(AFLACLOADER_DEBUG, "Seeking to seektable: sample index {}, byte offset {}", target_seekpoint.sample_index, target_seekpoint.byte_offset);
        auto position = target_seekpoint.byte_offset + m_data_start_location;
        if (m_stream->seek(static_cast<i64>(position), SeekMode::SetPosition).is_error())
            return LoaderError { LoaderError::Category::IO, m_loaded_samples, DeprecatedString::formatted("Invalid seek position {}", position) };
        m_loaded_samples = target_seekpoint.sample_index;
    }

    // Skip frames until we're within the seek tolerance.
    while (sample_index - m_loaded_samples > seek_tolerance) {
        (void)TRY(next_frame());
        m_loaded_samples += m_current_frame->sample_count;
    }

    return {};
}

bool FlacLoaderPlugin::should_insert_seekpoint_at(u64 sample_index) const
{
    auto const max_seekpoint_distance = (maximum_seekpoint_distance_ms * m_sample_rate) / 1000;
    auto const seek_tolerance = (seek_tolerance_ms * m_sample_rate) / 1000;
    auto const current_seekpoint_distance = m_seektable.seek_point_sample_distance_around(sample_index).value_or(NumericLimits<u64>::max());
    auto const distance_to_previous_seekpoint = sample_index - m_seektable.seek_point_before(sample_index).value_or({ 0, 0 }).sample_index;

    // We insert a seekpoint only under two conditions:
    // - The seek points around us are spaced too far for what the loader recommends.
    //   Prevents inserting too many seek points between pre-loaded seek points.
    // - We are so far away from the previous seek point that seeking will become too imprecise if we don't insert a seek point at least here.
    //   Prevents inserting too many seek points at the end of files without pre-loaded seek points.
    return current_seekpoint_distance >= max_seekpoint_distance && distance_to_previous_seekpoint >= seek_tolerance;
}

ErrorOr<Vector<FixedArray<Sample>>, LoaderError> FlacLoaderPlugin::load_chunks(size_t samples_to_read_from_input)
{
    ssize_t remaining_samples = static_cast<ssize_t>(m_total_samples - m_loaded_samples);
    // The first condition is relevant for unknown-size streams (total samples = 0 in the header)
    if (m_stream->is_eof() || remaining_samples <= 0)
        return Vector<FixedArray<Sample>> {};

    size_t samples_to_read = min(samples_to_read_from_input, remaining_samples);
    Vector<FixedArray<Sample>> frames;
    size_t sample_index = 0;

    while (!m_stream->is_eof() && sample_index < samples_to_read) {
        TRY(frames.try_append(TRY(next_frame())));
        sample_index += m_current_frame->sample_count;
    }

    m_loaded_samples += sample_index;

    return frames;
}

// 11.21. FRAME
LoaderSamples FlacLoaderPlugin::next_frame()
{
#define FLAC_VERIFY(check, category, msg)                                                                                                         \
    do {                                                                                                                                          \
        if (!(check)) {                                                                                                                           \
            return LoaderError { category, static_cast<size_t>(m_current_sample_or_frame), DeprecatedString::formatted("FLAC header: {}", msg) }; \
        }                                                                                                                                         \
    } while (0)

    auto frame_byte_index = TRY(m_stream->tell());
    auto sample_index = m_loaded_samples;
    // Insert a new seek point if we don't have enough here.
    if (should_insert_seekpoint_at(sample_index)) {
        dbgln_if(AFLACLOADER_DEBUG, "Inserting ad-hoc seek point for sample {} at byte {:x} (seekpoint spacing {} samples)", sample_index, frame_byte_index, m_seektable.seek_point_sample_distance_around(sample_index).value_or(NumericLimits<u64>::max()));
        auto maybe_error = m_seektable.insert_seek_point({ .sample_index = sample_index, .byte_offset = frame_byte_index - m_data_start_location });
        if (maybe_error.is_error())
            dbgln("FLAC Warning: Inserting seek point for sample {} failed: {}", sample_index, maybe_error.release_error());
    }

    auto checksum_stream = TRY(try_make<Crypto::Checksum::ChecksummingStream<FlacFrameHeaderCRC>>(MaybeOwned<Stream>(*m_stream)));
    BigEndianInputBitStream bit_stream { MaybeOwned<Stream> { *checksum_stream } };

    // TODO: Check the CRC-16 checksum by keeping track of read data.

    // 11.22. FRAME_HEADER
    u16 sync_code = LOADER_TRY(bit_stream.read_bits<u16>(14));
    FLAC_VERIFY(sync_code == 0b11111111111110, LoaderError::Category::Format, "Sync code");
    bool reserved_bit = LOADER_TRY(bit_stream.read_bit());
    FLAC_VERIFY(reserved_bit == 0, LoaderError::Category::Format, "Reserved frame header bit");
    // 11.22.2. BLOCKING STRATEGY
    [[maybe_unused]] bool blocking_strategy = LOADER_TRY(bit_stream.read_bit());

    u32 sample_count = TRY(convert_sample_count_code(LOADER_TRY(bit_stream.read_bits<u8>(4))));

    u32 frame_sample_rate = TRY(convert_sample_rate_code(LOADER_TRY(bit_stream.read_bits<u8>(4))));

    u8 channel_type_num = LOADER_TRY(bit_stream.read_bits<u8>(4));
    FLAC_VERIFY(channel_type_num < 0b1011, LoaderError::Category::Format, "Channel assignment");
    FlacFrameChannelType channel_type = (FlacFrameChannelType)channel_type_num;

    u8 bit_depth = TRY(convert_bit_depth_code(LOADER_TRY(bit_stream.read_bits<u8>(3))));

    reserved_bit = LOADER_TRY(bit_stream.read_bit());
    FLAC_VERIFY(reserved_bit == 0, LoaderError::Category::Format, "Reserved frame header end bit");

    // 11.22.8. CODED NUMBER
    m_current_sample_or_frame = LOADER_TRY(read_utf8_char(bit_stream));

    // Conditional header variables
    // 11.22.9. BLOCK SIZE INT
    if (sample_count == FLAC_BLOCKSIZE_AT_END_OF_HEADER_8) {
        sample_count = LOADER_TRY(bit_stream.read_bits<u32>(8)) + 1;
    } else if (sample_count == FLAC_BLOCKSIZE_AT_END_OF_HEADER_16) {
        sample_count = LOADER_TRY(bit_stream.read_bits<u32>(16)) + 1;
    }

    // 11.22.10. SAMPLE RATE INT
    if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_8) {
        frame_sample_rate = LOADER_TRY(bit_stream.read_bits<u32>(8)) * 1000;
    } else if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_16) {
        frame_sample_rate = LOADER_TRY(bit_stream.read_bits<u32>(16));
    } else if (frame_sample_rate == FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10) {
        frame_sample_rate = LOADER_TRY(bit_stream.read_bits<u32>(16)) * 10;
    }

    // It does not matter whether we extract the checksum from the digest here, or extract the digest 0x00 after processing the checksum.
    auto const calculated_checksum = checksum_stream->digest();
    // 11.22.11. FRAME CRC
    u8 specified_checksum = LOADER_TRY(bit_stream.read_bits<u8>(8));
    VERIFY(bit_stream.is_aligned_to_byte_boundary());
    if (specified_checksum != calculated_checksum)
        dbgln("FLAC frame {}: Calculated header checksum {:02x} is different from specified checksum {:02x}", m_current_sample_or_frame, calculated_checksum, specified_checksum);

    dbgln_if(AFLACLOADER_DEBUG, "Frame: {} samples, {}bit {}Hz, channeltype {:x}, {} number {}, header checksum {:02x}{}", sample_count, bit_depth, frame_sample_rate, channel_type_num, blocking_strategy ? "sample" : "frame", m_current_sample_or_frame, specified_checksum, specified_checksum != calculated_checksum ? " (checksum error)"sv : ""sv);

    m_current_frame = FlacFrameHeader {
        sample_count,
        frame_sample_rate,
        channel_type,
        bit_depth,
        specified_checksum,
    };

    u8 subframe_count = frame_channel_type_to_channel_count(channel_type);
    Vector<Vector<i32>> current_subframes;
    current_subframes.ensure_capacity(subframe_count);

    for (u8 i = 0; i < subframe_count; ++i) {
        FlacSubframeHeader new_subframe = TRY(next_subframe_header(bit_stream, i));
        Vector<i32> subframe_samples = TRY(parse_subframe(new_subframe, bit_stream));
        VERIFY(subframe_samples.size() == m_current_frame->sample_count);
        current_subframes.unchecked_append(move(subframe_samples));
    }

    // 11.2. Overview ("The audio data is composed of...")
    bit_stream.align_to_byte_boundary();

    // 11.23. FRAME_FOOTER
    // TODO: check checksum, see above
    [[maybe_unused]] u16 footer_checksum = LOADER_TRY(bit_stream.read_bits<u16>(16));
    dbgln_if(AFLACLOADER_DEBUG, "Subframe footer checksum: {}", footer_checksum);

    float sample_rescale = 1 / static_cast<float>(1 << (m_current_frame->bit_depth - 1));
    dbgln_if(AFLACLOADER_DEBUG, "Sample rescaled from {} bits: factor {:.8f}", m_current_frame->bit_depth, sample_rescale);

    FixedArray<Sample> samples = TRY(FixedArray<Sample>::create(m_current_frame->sample_count));

    switch (channel_type) {
    case FlacFrameChannelType::Mono:
        for (size_t i = 0; i < m_current_frame->sample_count; ++i)
            samples[i] = Sample { static_cast<float>(current_subframes[0][i]) * sample_rescale };
        break;
    case FlacFrameChannelType::Stereo:
    // TODO mix together surround channels on each side?
    case FlacFrameChannelType::StereoCenter:
    case FlacFrameChannelType::Surround4p0:
    case FlacFrameChannelType::Surround5p0:
    case FlacFrameChannelType::Surround5p1:
    case FlacFrameChannelType::Surround6p1:
    case FlacFrameChannelType::Surround7p1:
        for (size_t i = 0; i < m_current_frame->sample_count; ++i)
            samples[i] = { static_cast<float>(current_subframes[0][i]) * sample_rescale, static_cast<float>(current_subframes[1][i]) * sample_rescale };
        break;
    case FlacFrameChannelType::LeftSideStereo:
        // channels are left (0) and side (1)
        for (size_t i = 0; i < m_current_frame->sample_count; ++i) {
            // right = left - side
            samples[i] = { static_cast<float>(current_subframes[0][i]) * sample_rescale,
                static_cast<float>(current_subframes[0][i] - current_subframes[1][i]) * sample_rescale };
        }
        break;
    case FlacFrameChannelType::RightSideStereo:
        // channels are side (0) and right (1)
        for (size_t i = 0; i < m_current_frame->sample_count; ++i) {
            // left = right + side
            samples[i] = { static_cast<float>(current_subframes[1][i] + current_subframes[0][i]) * sample_rescale,
                static_cast<float>(current_subframes[1][i]) * sample_rescale };
        }
        break;
    case FlacFrameChannelType::MidSideStereo:
        // channels are mid (0) and side (1)
        for (size_t i = 0; i < current_subframes[0].size(); ++i) {
            i64 mid = current_subframes[0][i];
            i64 side = current_subframes[1][i];
            mid *= 2;
            // prevent integer division errors
            samples[i] = { static_cast<float>((mid + side) * .5f) * sample_rescale,
                static_cast<float>((mid - side) * .5f) * sample_rescale };
        }
        break;
    }

    return samples;
#undef FLAC_VERIFY
}

// 11.22.3. INTERCHANNEL SAMPLE BLOCK SIZE
ErrorOr<u32, LoaderError> FlacLoaderPlugin::convert_sample_count_code(u8 sample_count_code)
{
    // single codes
    switch (sample_count_code) {
    case 0:
        return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved block size" };
    case 1:
        return 192;
    case 6:
        return FLAC_BLOCKSIZE_AT_END_OF_HEADER_8;
    case 7:
        return FLAC_BLOCKSIZE_AT_END_OF_HEADER_16;
    }
    if (sample_count_code >= 2 && sample_count_code <= 5) {
        return 576 * AK::exp2(sample_count_code - 2);
    }
    return 256 * AK::exp2(sample_count_code - 8);
}

// 11.22.4. SAMPLE RATE
ErrorOr<u32, LoaderError> FlacLoaderPlugin::convert_sample_rate_code(u8 sample_rate_code)
{
    switch (sample_rate_code) {
    case 0:
        return m_sample_rate;
    case 1:
        return 88200;
    case 2:
        return 176400;
    case 3:
        return 192000;
    case 4:
        return 8000;
    case 5:
        return 16000;
    case 6:
        return 22050;
    case 7:
        return 24000;
    case 8:
        return 32000;
    case 9:
        return 44100;
    case 10:
        return 48000;
    case 11:
        return 96000;
    case 12:
        return FLAC_SAMPLERATE_AT_END_OF_HEADER_8;
    case 13:
        return FLAC_SAMPLERATE_AT_END_OF_HEADER_16;
    case 14:
        return FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10;
    default:
        return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Invalid sample rate code" };
    }
}

// 11.22.6. SAMPLE SIZE
ErrorOr<u8, LoaderError> FlacLoaderPlugin::convert_bit_depth_code(u8 bit_depth_code)
{
    switch (bit_depth_code) {
    case 0:
        return m_bits_per_sample;
    case 1:
        return 8;
    case 2:
        return 12;
    case 3:
        return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved sample size" };
    case 4:
        return 16;
    case 5:
        return 20;
    case 6:
        return 24;
    case 7:
        return 32;
    default:
        return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), DeprecatedString::formatted("Unsupported sample size {}", bit_depth_code) };
    }
}

// 11.22.5. CHANNEL ASSIGNMENT
u8 frame_channel_type_to_channel_count(FlacFrameChannelType channel_type)
{
    if (channel_type <= FlacFrameChannelType::Surround7p1)
        return to_underlying(channel_type) + 1;
    return 2;
}

// 11.25. SUBFRAME_HEADER
ErrorOr<FlacSubframeHeader, LoaderError> FlacLoaderPlugin::next_subframe_header(BigEndianInputBitStream& bit_stream, u8 channel_index)
{
    u8 bits_per_sample = m_current_frame->bit_depth;

    // For inter-channel correlation, the side channel needs an extra bit for its samples
    switch (m_current_frame->channels) {
    case FlacFrameChannelType::LeftSideStereo:
    case FlacFrameChannelType::MidSideStereo:
        if (channel_index == 1) {
            ++bits_per_sample;
        }
        break;
    case FlacFrameChannelType::RightSideStereo:
        if (channel_index == 0) {
            ++bits_per_sample;
        }
        break;
    // "normal" channel types
    default:
        break;
    }

    // zero-bit padding
    if (LOADER_TRY(bit_stream.read_bit()) != 0)
        return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Zero bit padding" };

    // 11.25.1. SUBFRAME TYPE
    u8 subframe_code = LOADER_TRY(bit_stream.read_bits<u8>(6));
    if ((subframe_code >= 0b000010 && subframe_code <= 0b000111) || (subframe_code > 0b001100 && subframe_code < 0b100000))
        return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Subframe type" };

    FlacSubframeType subframe_type;
    u8 order = 0;
    // LPC has the highest bit set
    if ((subframe_code & 0b100000) > 0) {
        subframe_type = FlacSubframeType::LPC;
        order = (subframe_code & 0b011111) + 1;
    } else if ((subframe_code & 0b001000) > 0) {
        // Fixed has the third-highest bit set
        subframe_type = FlacSubframeType::Fixed;
        order = (subframe_code & 0b000111);
    } else {
        subframe_type = (FlacSubframeType)subframe_code;
    }

    // 11.25.2. WASTED BITS PER SAMPLE FLAG
    bool has_wasted_bits = LOADER_TRY(bit_stream.read_bit());
    u8 k = 0;
    if (has_wasted_bits) {
        bool current_k_bit = 0;
        do {
            current_k_bit = LOADER_TRY(bit_stream.read_bit());
            ++k;
        } while (current_k_bit != 1);
    }

    return FlacSubframeHeader {
        subframe_type,
        order,
        k,
        bits_per_sample
    };
}

ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::parse_subframe(FlacSubframeHeader& subframe_header, BigEndianInputBitStream& bit_input)
{
    Vector<i32> samples;

    switch (subframe_header.type) {
    case FlacSubframeType::Constant: {
        // 11.26. SUBFRAME_CONSTANT
        u64 constant_value = LOADER_TRY(bit_input.read_bits<u64>(subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample));
        dbgln_if(AFLACLOADER_DEBUG, "Constant subframe: {}", constant_value);

        samples.ensure_capacity(m_current_frame->sample_count);
        VERIFY(subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample != 0);
        i32 constant = sign_extend(static_cast<u32>(constant_value), subframe_header.bits_per_sample - subframe_header.wasted_bits_per_sample);
        for (u32 i = 0; i < m_current_frame->sample_count; ++i) {
            samples.unchecked_append(constant);
        }
        break;
    }
    case FlacSubframeType::Fixed: {
        dbgln_if(AFLACLOADER_DEBUG, "Fixed LPC subframe order {}", subframe_header.order);
        samples = TRY(decode_fixed_lpc(subframe_header, bit_input));
        break;
    }
    case FlacSubframeType::Verbatim: {
        dbgln_if(AFLACLOADER_DEBUG, "Verbatim subframe");
        samples = TRY(decode_verbatim(subframe_header, bit_input));
        break;
    }
    case FlacSubframeType::LPC: {
        dbgln_if(AFLACLOADER_DEBUG, "Custom LPC subframe order {}", subframe_header.order);
        samples = TRY(decode_custom_lpc(subframe_header, bit_input));
        break;
    }
    default:
        return LoaderError { LoaderError::Category::Unimplemented, static_cast<size_t>(m_current_sample_or_frame), "Unhandled FLAC subframe type" };
    }

    for (size_t i = 0; i < samples.size(); ++i) {
        samples[i] <<= subframe_header.wasted_bits_per_sample;
    }

    ResampleHelper<i32> resampler(m_current_frame->sample_rate, m_sample_rate);
    return resampler.resample(samples);
}

// 11.29. SUBFRAME_VERBATIM
// Decode a subframe that isn't actually encoded, usually seen in random data
ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_verbatim(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
{
    Vector<i32> decoded;
    decoded.ensure_capacity(m_current_frame->sample_count);

    VERIFY(subframe.bits_per_sample - subframe.wasted_bits_per_sample != 0);
    for (size_t i = 0; i < m_current_frame->sample_count; ++i) {
        decoded.unchecked_append(sign_extend(
            LOADER_TRY(bit_input.read_bits<u32>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
            subframe.bits_per_sample - subframe.wasted_bits_per_sample));
    }

    return decoded;
}

// 11.28. SUBFRAME_LPC
// Decode a subframe encoded with a custom linear predictor coding, i.e. the subframe provides the polynomial order and coefficients
ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_custom_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
{
    Vector<i32> decoded;
    decoded.ensure_capacity(m_current_frame->sample_count);

    VERIFY(subframe.bits_per_sample - subframe.wasted_bits_per_sample != 0);
    // warm-up samples
    for (auto i = 0; i < subframe.order; ++i) {
        decoded.unchecked_append(sign_extend(
            LOADER_TRY(bit_input.read_bits<u32>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
            subframe.bits_per_sample - subframe.wasted_bits_per_sample));
    }

    // precision of the coefficients
    u8 lpc_precision = LOADER_TRY(bit_input.read_bits<u8>(4));
    if (lpc_precision == 0b1111)
        return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Invalid linear predictor coefficient precision" };
    lpc_precision += 1;

    // shift needed on the data (signed!)
    i8 lpc_shift = sign_extend(LOADER_TRY(bit_input.read_bits<u8>(5)), 5);

    Vector<i32> coefficients;
    coefficients.ensure_capacity(subframe.order);
    // read coefficients
    for (auto i = 0; i < subframe.order; ++i) {
        u32 raw_coefficient = LOADER_TRY(bit_input.read_bits<u32>(lpc_precision));
        i32 coefficient = static_cast<i32>(sign_extend(raw_coefficient, lpc_precision));
        coefficients.unchecked_append(coefficient);
    }

    dbgln_if(AFLACLOADER_DEBUG, "{}-bit {} shift coefficients: {}", lpc_precision, lpc_shift, coefficients);

    TRY(decode_residual(decoded, subframe, bit_input));

    // approximate the waveform with the predictor
    for (size_t i = subframe.order; i < m_current_frame->sample_count; ++i) {
        // (see below)
        i64 sample = 0;
        for (size_t t = 0; t < subframe.order; ++t) {
            // It's really important that we compute in 64-bit land here.
            // Even though FLAC operates at a maximum bit depth of 32 bits, modern encoders use super-large coefficients for maximum compression.
            // These will easily overflow 32 bits and cause strange white noise that abruptly stops intermittently (at the end of a frame).
            // The simple fix of course is to do intermediate computations in 64 bits.
            // 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
            sample += static_cast<i64>(coefficients[t]) * static_cast<i64>(decoded[i - t - 1]);
        }
        decoded[i] += sample >> lpc_shift;
    }

    return decoded;
}

// 11.27. SUBFRAME_FIXED
// Decode a subframe encoded with one of the fixed linear predictor codings
ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_fixed_lpc(FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
{
    Vector<i32> decoded;
    decoded.ensure_capacity(m_current_frame->sample_count);

    VERIFY(subframe.bits_per_sample - subframe.wasted_bits_per_sample != 0);
    // warm-up samples
    for (auto i = 0; i < subframe.order; ++i) {
        decoded.unchecked_append(sign_extend(
            LOADER_TRY(bit_input.read_bits<u32>(subframe.bits_per_sample - subframe.wasted_bits_per_sample)),
            subframe.bits_per_sample - subframe.wasted_bits_per_sample));
    }

    TRY(decode_residual(decoded, subframe, bit_input));

    dbgln_if(AFLACLOADER_DEBUG, "decoded length {}, {} order predictor", decoded.size(), subframe.order);

    // Skip these comments if you don't care about the neat math behind fixed LPC :^)
    // These coefficients for the recursive prediction formula are the only ones that can be resolved to polynomial predictor functions.
    // The order equals the degree of the polynomial - 1, so the second-order predictor has an underlying polynomial of degree 1, a straight line.
    // More specifically, the closest approximation to a polynomial is used, and the degree depends on how many previous values are available.
    // This makes use of a very neat property of polynomials, which is that they are entirely characterized by their finitely many derivatives.
    // (Mathematically speaking, the infinite Taylor series of any polynomial equals the polynomial itself.)
    // Now remember that derivation is just the slope of the function, which is the same as the difference of two close-by values.
    // Therefore, with two samples we can calculate the first derivative at a sample via the difference, which gives us a polynomial of degree 1.
    // With three samples, we can do the same but also calculate the second derivative via the difference in the first derivatives.
    // This gives us a polynomial of degree 2, as it has two "proper" (non-constant) derivatives.
    // This can be continued for higher-order derivatives when we have more coefficients, giving us higher-order polynomials.
    // In essence, it's akin to a Lagrangian polynomial interpolation for every sample (but already pre-solved).

    // The coefficients for orders 0-3 originate from the SHORTEN codec:
    // http://mi.eng.cam.ac.uk/reports/svr-ftp/auto-pdf/robinson_tr156.pdf page 4
    // The coefficients for order 4 are undocumented in the original FLAC specification(s), but can now be found in
    // https://datatracker.ietf.org/doc/html/draft-ietf-cellar-flac-03#section-10.2.5
    switch (subframe.order) {
    case 0:
        // s_0(t) = 0
        for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
            decoded[i] += 0;
        break;
    case 1:
        // s_1(t) = s(t-1)
        for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
            decoded[i] += decoded[i - 1];
        break;
    case 2:
        // s_2(t) = 2s(t-1) - s(t-2)
        for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
            decoded[i] += 2 * decoded[i - 1] - decoded[i - 2];
        break;
    case 3:
        // s_3(t) = 3s(t-1) - 3s(t-2) + s(t-3)
        for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
            decoded[i] += 3 * decoded[i - 1] - 3 * decoded[i - 2] + decoded[i - 3];
        break;
    case 4:
        // s_4(t) = 4s(t-1) - 6s(t-2) + 4s(t-3) - s(t-4)
        for (u32 i = subframe.order; i < m_current_frame->sample_count; ++i)
            decoded[i] += 4 * decoded[i - 1] - 6 * decoded[i - 2] + 4 * decoded[i - 3] - decoded[i - 4];
        break;
    default:
        return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), DeprecatedString::formatted("Unrecognized predictor order {}", subframe.order) };
    }
    return decoded;
}

// 11.30. RESIDUAL
// Decode the residual, the "error" between the function approximation and the actual audio data
MaybeLoaderError FlacLoaderPlugin::decode_residual(Vector<i32>& decoded, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
{
    // 11.30.1. RESIDUAL_CODING_METHOD
    auto residual_mode = static_cast<FlacResidualMode>(LOADER_TRY(bit_input.read_bits<u8>(2)));
    u8 partition_order = LOADER_TRY(bit_input.read_bits<u8>(4));
    size_t partitions = 1 << partition_order;

    if (residual_mode == FlacResidualMode::Rice4Bit) {
        // 11.30.2. RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB
        // decode a single Rice partition with four bits for the order k
        for (size_t i = 0; i < partitions; ++i) {
            auto rice_partition = TRY(decode_rice_partition(4, partitions, i, subframe, bit_input));
            decoded.extend(move(rice_partition));
        }
    } else if (residual_mode == FlacResidualMode::Rice5Bit) {
        // 11.30.3. RESIDUAL_CODING_METHOD_PARTITIONED_EXP_GOLOMB2
        // five bits equivalent
        for (size_t i = 0; i < partitions; ++i) {
            auto rice_partition = TRY(decode_rice_partition(5, partitions, i, subframe, bit_input));
            decoded.extend(move(rice_partition));
        }
    } else
        return LoaderError { LoaderError::Category::Format, static_cast<size_t>(m_current_sample_or_frame), "Reserved residual coding method" };

    return {};
}

// 11.30.2.1. EXP_GOLOMB_PARTITION and 11.30.3.1. EXP_GOLOMB2_PARTITION
// Decode a single Rice partition as part of the residual, every partition can have its own Rice parameter k
ALWAYS_INLINE ErrorOr<Vector<i32>, LoaderError> FlacLoaderPlugin::decode_rice_partition(u8 partition_type, u32 partitions, u32 partition_index, FlacSubframeHeader& subframe, BigEndianInputBitStream& bit_input)
{
    // 11.30.2.2. EXP GOLOMB PARTITION ENCODING PARAMETER and 11.30.3.2. EXP-GOLOMB2 PARTITION ENCODING PARAMETER
    u8 k = LOADER_TRY(bit_input.read_bits<u8>(partition_type));

    u32 residual_sample_count;
    if (partitions == 0)
        residual_sample_count = m_current_frame->sample_count - subframe.order;
    else
        residual_sample_count = m_current_frame->sample_count / partitions;
    if (partition_index == 0)
        residual_sample_count -= subframe.order;

    Vector<i32> rice_partition;
    rice_partition.resize(residual_sample_count);

    // escape code for unencoded binary partition
    if (k == (1 << partition_type) - 1) {
        u8 unencoded_bps = LOADER_TRY(bit_input.read_bits<u8>(5));
        for (size_t r = 0; r < residual_sample_count; ++r) {
            rice_partition[r] = LOADER_TRY(bit_input.read_bits<u8>(unencoded_bps));
        }
    } else {
        for (size_t r = 0; r < residual_sample_count; ++r) {
            rice_partition[r] = LOADER_TRY(decode_unsigned_exp_golomb(k, bit_input));
        }
    }

    return rice_partition;
}

// Decode a single number encoded with Rice/Exponential-Golomb encoding (the unsigned variant)
ALWAYS_INLINE ErrorOr<i32> decode_unsigned_exp_golomb(u8 k, BigEndianInputBitStream& bit_input)
{
    u8 q = 0;
    while (TRY(bit_input.read_bit()) == 0)
        ++q;

    // least significant bits (remainder)
    u32 rem = TRY(bit_input.read_bits<u32>(k));
    u32 value = q << k | rem;

    return rice_to_signed(value);
}

ErrorOr<u64> read_utf8_char(BigEndianInputBitStream& input)
{
    u64 character;
    u8 start_byte = TRY(input.read_value<u8>());
    // Signal byte is zero: ASCII character
    if ((start_byte & 0b10000000) == 0) {
        return start_byte;
    } else if ((start_byte & 0b11000000) == 0b10000000) {
        return Error::from_string_literal("Illegal continuation byte");
    }
    // This algorithm is too good and supports the theoretical max 0xFF start byte
    u8 length = 1;
    while (((start_byte << length) & 0b10000000) == 0b10000000)
        ++length;
    u8 bits_from_start_byte = 8 - (length + 1);
    u8 start_byte_bitmask = AK::exp2(bits_from_start_byte) - 1;
    character = start_byte_bitmask & start_byte;
    for (u8 i = length - 1; i > 0; --i) {
        u8 current_byte = TRY(input.read_value<u8>());
        character = (character << 6) | (current_byte & 0b00111111);
    }
    return character;
}

i64 sign_extend(u32 n, u8 size)
{
    // negative
    if ((n & (1 << (size - 1))) > 0) {
        return static_cast<i64>(n | (0xffffffff << size));
    }
    // positive
    return n;
}

i32 rice_to_signed(u32 x)
{
    // positive numbers are even, negative numbers are odd
    // bitmask for conditionally inverting the entire number, thereby "negating" it
    i32 sign = -static_cast<i32>(x & 1);
    // copies the sign's sign onto the actual magnitude of x
    return static_cast<i32>(sign ^ (x >> 1));
}
}