MP3Types.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2021, Arne Elster <arne@elster.li>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Array.h>
  8. #include <AK/EnumBits.h>
  9. #include <AK/FixedArray.h>
  10. namespace Audio::MP3 {
  11. constexpr size_t const frame_size = 1152;
  12. // 576 samples.
  13. constexpr size_t const granule_size = frame_size / 2;
  14. enum class Mode {
  15. Stereo = 0,
  16. JointStereo = 1,
  17. DualChannel = 2,
  18. SingleChannel = 3,
  19. };
  20. enum class ModeExtension {
  21. Stereo = 0,
  22. IntensityStereo = 1,
  23. MsStereo = 2,
  24. };
  25. AK_ENUM_BITWISE_OPERATORS(ModeExtension)
  26. enum class Emphasis {
  27. None = 0,
  28. Microseconds_50_15 = 1,
  29. Reserved = 2,
  30. CCITT_J17 = 3,
  31. };
  32. enum class BlockType {
  33. Normal = 0,
  34. Start = 1,
  35. Short = 2,
  36. End = 3,
  37. };
  38. struct Header {
  39. i32 id { 0 };
  40. i32 layer { 0 };
  41. bool protection_bit { false };
  42. i32 bitrate { 0 };
  43. i32 samplerate { 0 };
  44. bool padding_bit { false };
  45. bool private_bit { false };
  46. Mode mode { Mode::Stereo };
  47. ModeExtension mode_extension { ModeExtension::Stereo };
  48. bool copyright_bit { false };
  49. bool original_bit { false };
  50. Emphasis emphasis { Emphasis::None };
  51. u16 crc16 { 0 };
  52. size_t header_size { 0 };
  53. size_t frame_size { 0 };
  54. size_t slot_count { 0 };
  55. size_t channel_count() const { return mode == Mode::SingleChannel ? 1 : 2; }
  56. };
  57. struct Granule {
  58. Array<float, MP3::granule_size> samples;
  59. Array<Array<float, 18>, 32> filter_bank_input;
  60. Array<Array<float, 32>, 18> pcm;
  61. u32 part_2_3_length { 0 };
  62. u32 big_values { 0 };
  63. u32 global_gain { 0 };
  64. u32 scalefac_compress { 0 };
  65. bool window_switching_flag { false };
  66. BlockType block_type { BlockType::Normal };
  67. bool mixed_block_flag { false };
  68. Array<int, 3> table_select;
  69. Array<int, 3> sub_block_gain;
  70. u32 region0_count { 0 };
  71. u32 region1_count { 0 };
  72. bool preflag { false };
  73. bool scalefac_scale { false };
  74. bool count1table_select { false };
  75. };
  76. struct Channel {
  77. Array<Granule, 2> granules;
  78. Array<int, 39> scale_factors;
  79. Array<int, 4> scale_factor_selection_info;
  80. };
  81. struct MP3Frame {
  82. Header header;
  83. FixedArray<Channel> channels;
  84. off_t main_data_begin { 0 };
  85. u32 private_bits { 0 };
  86. MP3Frame(Header header)
  87. : header(header)
  88. , channels(FixedArray<Channel>::must_create_but_fixme_should_propagate_errors(header.channel_count()))
  89. {
  90. }
  91. };
  92. }