FlacTypes.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "Queue.h"
  8. #include "SampleFormats.h"
  9. #include <AK/ByteBuffer.h>
  10. #include <AK/Types.h>
  11. #include <AK/Variant.h>
  12. #include <LibCrypto/Checksum/CRC16.h>
  13. #include <LibCrypto/Checksum/CRC8.h>
  14. namespace Audio {
  15. // These are not the actual values stored in the file! They are marker constants instead, only used temporarily in the decoder.
  16. // 11.22.3. INTERCHANNEL SAMPLE BLOCK SIZE
  17. #define FLAC_BLOCKSIZE_AT_END_OF_HEADER_8 0xffffffff
  18. #define FLAC_BLOCKSIZE_AT_END_OF_HEADER_16 0xfffffffe
  19. // 11.22.4. SAMPLE RATE
  20. #define FLAC_SAMPLERATE_AT_END_OF_HEADER_8 0xffffffff
  21. #define FLAC_SAMPLERATE_AT_END_OF_HEADER_16 0xfffffffe
  22. #define FLAC_SAMPLERATE_AT_END_OF_HEADER_16X10 0xfffffffd
  23. constexpr StringView flac_magic = "fLaC"sv;
  24. // 11.22.11. FRAME CRC
  25. // The polynomial used here is known as CRC-8-CCITT.
  26. static constexpr u8 flac_polynomial = 0x07;
  27. using FlacFrameHeaderCRC = Crypto::Checksum::CRC8<flac_polynomial>;
  28. // 11.23. FRAME_FOOTER
  29. // The polynomial used here is known as CRC-16-IBM.
  30. static constexpr u16 ibm_polynomial = 0xA001;
  31. using IBMCRC = Crypto::Checksum::CRC16<ibm_polynomial>;
  32. static constexpr size_t flac_seekpoint_size = (64 + 64 + 16) / 8;
  33. // 11.8 BLOCK_TYPE (7 bits)
  34. enum class FlacMetadataBlockType : u8 {
  35. STREAMINFO = 0, // Important data about the audio format
  36. PADDING = 1, // Non-data block to be ignored
  37. APPLICATION = 2, // Ignored
  38. SEEKTABLE = 3, // Seeking info, maybe to be used later
  39. VORBIS_COMMENT = 4, // Ignored
  40. CUESHEET = 5, // Ignored
  41. PICTURE = 6, // Ignored
  42. INVALID = 127, // Error
  43. };
  44. // 11.22.5. CHANNEL ASSIGNMENT
  45. enum class FlacFrameChannelType : u8 {
  46. Mono = 0,
  47. Stereo = 1,
  48. StereoCenter = 2, // left, right, center
  49. Surround4p0 = 3, // front left/right, back left/right
  50. Surround5p0 = 4, // front left/right, center, back left/right
  51. Surround5p1 = 5, // front left/right, center, LFE, back left/right
  52. Surround6p1 = 6, // front left/right, center, LFE, back center, side left/right
  53. Surround7p1 = 7, // front left/right, center, LFE, back left/right, side left/right
  54. LeftSideStereo = 8, // channel coupling: left and difference
  55. RightSideStereo = 9, // channel coupling: difference and right
  56. MidSideStereo = 10, // channel coupling: center and difference
  57. // others are reserved
  58. };
  59. // 11.25.1. SUBFRAME TYPE
  60. enum class FlacSubframeType : u8 {
  61. Constant = 0,
  62. Verbatim = 1,
  63. Fixed = 0b001000,
  64. LPC = 0b100000,
  65. // others are reserved
  66. };
  67. // 11.30.1. RESIDUAL_CODING_METHOD
  68. enum class FlacResidualMode : u8 {
  69. Rice4Bit = 0,
  70. Rice5Bit = 1,
  71. };
  72. // 11.6. METADATA_BLOCK
  73. struct FlacRawMetadataBlock {
  74. bool is_last_block;
  75. FlacMetadataBlockType type;
  76. u32 length; // 24 bits
  77. ByteBuffer data;
  78. ErrorOr<void> write_to_stream(Stream&) const;
  79. };
  80. enum class BlockingStrategy : u8 {
  81. Fixed = 0,
  82. Variable = 1,
  83. };
  84. // Block sample count can be stored in one of 5 ways.
  85. enum class BlockSizeCategory : u8 {
  86. Reserved = 0b0000,
  87. S192 = 0b0001,
  88. // The formula for these four is 144 * (2^x), and it appears to be an MP3 compatibility feature.
  89. S576 = 0b0010,
  90. S1152 = 0b0011,
  91. S2304 = 0b0100,
  92. S4608 = 0b0101,
  93. // Actual size is stored later on.
  94. Uncommon8Bits = 0b0110,
  95. Uncommon16Bits = 0b0111,
  96. // Formula 2^x.
  97. S256 = 0b1000,
  98. S512 = 0b1001,
  99. S1024 = 0b1010,
  100. S2048 = 0b1011,
  101. S4096 = 0b1100,
  102. S8192 = 0b1101,
  103. S16384 = 0b1110,
  104. S32768 = 0b1111,
  105. };
  106. // 11.22. FRAME_HEADER
  107. struct FlacFrameHeader {
  108. u32 sample_rate;
  109. // Referred to as “block size” in the specification.
  110. u16 sample_count;
  111. // If blocking strategy is fixed, this encodes the frame index instead of the sample index.
  112. u32 sample_or_frame_index;
  113. BlockingStrategy blocking_strategy;
  114. FlacFrameChannelType channels;
  115. u8 bit_depth;
  116. u8 checksum;
  117. ErrorOr<void> write_to_stream(Stream&) const;
  118. };
  119. // 11.25. SUBFRAME_HEADER
  120. struct FlacSubframeHeader {
  121. FlacSubframeType type;
  122. // order for fixed and LPC subframes
  123. u8 order;
  124. u8 wasted_bits_per_sample;
  125. u8 bits_per_sample;
  126. };
  127. enum class FlacFixedLPC : size_t {
  128. Zero = 0,
  129. One = 1,
  130. Two = 2,
  131. Three = 3,
  132. Four = 4,
  133. };
  134. struct FlacLPCEncodedSubframe {
  135. Vector<i64> warm_up_samples;
  136. Variant<Vector<i64>, FlacFixedLPC> coefficients;
  137. Vector<i64> residuals;
  138. size_t residual_cost_bits;
  139. // If we’re only using one Rice partition, this is the optimal order to use.
  140. u8 single_partition_optimal_order;
  141. };
  142. }