FlacTypes.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // 11.8 BLOCK_TYPE (7 bits)
  33. enum class FlacMetadataBlockType : u8 {
  34. STREAMINFO = 0, // Important data about the audio format
  35. PADDING = 1, // Non-data block to be ignored
  36. APPLICATION = 2, // Ignored
  37. SEEKTABLE = 3, // Seeking info, maybe to be used later
  38. VORBIS_COMMENT = 4, // Ignored
  39. CUESHEET = 5, // Ignored
  40. PICTURE = 6, // Ignored
  41. INVALID = 127, // Error
  42. };
  43. // 11.22.5. CHANNEL ASSIGNMENT
  44. enum class FlacFrameChannelType : u8 {
  45. Mono = 0,
  46. Stereo = 1,
  47. StereoCenter = 2, // left, right, center
  48. Surround4p0 = 3, // front left/right, back left/right
  49. Surround5p0 = 4, // front left/right, center, back left/right
  50. Surround5p1 = 5, // front left/right, center, LFE, back left/right
  51. Surround6p1 = 6, // front left/right, center, LFE, back center, side left/right
  52. Surround7p1 = 7, // front left/right, center, LFE, back left/right, side left/right
  53. LeftSideStereo = 8, // channel coupling: left and difference
  54. RightSideStereo = 9, // channel coupling: difference and right
  55. MidSideStereo = 10, // channel coupling: center and difference
  56. // others are reserved
  57. };
  58. // 11.25.1. SUBFRAME TYPE
  59. enum class FlacSubframeType : u8 {
  60. Constant = 0,
  61. Verbatim = 1,
  62. Fixed = 0b001000,
  63. LPC = 0b100000,
  64. // others are reserved
  65. };
  66. // 11.30.1. RESIDUAL_CODING_METHOD
  67. enum class FlacResidualMode : u8 {
  68. Rice4Bit = 0,
  69. Rice5Bit = 1,
  70. };
  71. // 11.6. METADATA_BLOCK
  72. struct FlacRawMetadataBlock {
  73. bool is_last_block;
  74. FlacMetadataBlockType type;
  75. u32 length; // 24 bits
  76. ByteBuffer data;
  77. ErrorOr<void> write_to_stream(Stream&) const;
  78. };
  79. enum class BlockingStrategy : u8 {
  80. Fixed = 0,
  81. Variable = 1,
  82. };
  83. // Block sample count can be stored in one of 5 ways.
  84. enum class BlockSizeCategory : u8 {
  85. Reserved = 0b0000,
  86. S192 = 0b0001,
  87. // The formula for these four is 144 * (2^x), and it appears to be an MP3 compatibility feature.
  88. S576 = 0b0010,
  89. S1152 = 0b0011,
  90. S2304 = 0b0100,
  91. S4608 = 0b0101,
  92. // Actual size is stored later on.
  93. Uncommon8Bits = 0b0110,
  94. Uncommon16Bits = 0b1111,
  95. // Formula 2^x.
  96. S256 = 0b1000,
  97. S512 = 0b1001,
  98. S1024 = 0b1010,
  99. S2048 = 0b1011,
  100. S4096 = 0b1100,
  101. S8192 = 0b1101,
  102. S16384 = 0b1110,
  103. S32768 = 0b1111,
  104. };
  105. // 11.22. FRAME_HEADER
  106. struct FlacFrameHeader {
  107. u32 sample_rate;
  108. // Referred to as “block size” in the specification.
  109. u16 sample_count;
  110. // If blocking strategy is fixed, this encodes the frame index instead of the sample index.
  111. u32 sample_or_frame_index;
  112. BlockingStrategy blocking_strategy;
  113. FlacFrameChannelType channels;
  114. u8 bit_depth;
  115. u8 checksum;
  116. ErrorOr<void> write_to_stream(Stream&) const;
  117. };
  118. // 11.25. SUBFRAME_HEADER
  119. struct FlacSubframeHeader {
  120. FlacSubframeType type;
  121. // order for fixed and LPC subframes
  122. u8 order;
  123. u8 wasted_bits_per_sample;
  124. u8 bits_per_sample;
  125. };
  126. enum class FlacFixedLPC : size_t {
  127. Zero = 0,
  128. One = 1,
  129. Two = 2,
  130. Three = 3,
  131. Four = 4,
  132. };
  133. struct FlacLPCEncodedSubframe {
  134. Vector<i64> warm_up_samples;
  135. Variant<Vector<i64>, FlacFixedLPC> coefficients;
  136. Vector<i64> residuals;
  137. size_t residual_cost_bits;
  138. // If we’re only using one Rice partition, this is the optimal order to use.
  139. u8 single_partition_optimal_order;
  140. };
  141. }