MP3Types.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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;
  40. i32 layer;
  41. bool protection_bit;
  42. i32 bitrate;
  43. i32 samplerate;
  44. bool padding_bit;
  45. bool private_bit;
  46. Mode mode;
  47. ModeExtension mode_extension;
  48. bool copyright_bit;
  49. bool original_bit;
  50. Emphasis emphasis;
  51. u16 crc16;
  52. size_t frame_size;
  53. size_t slot_count;
  54. size_t channel_count() const { return mode == Mode::SingleChannel ? 1 : 2; }
  55. };
  56. struct Granule {
  57. Array<float, MP3::granule_size> samples;
  58. Array<Array<float, 18>, 32> filter_bank_input;
  59. Array<Array<float, 32>, 18> pcm;
  60. u32 part_2_3_length;
  61. u32 big_values;
  62. u32 global_gain;
  63. u32 scalefac_compress;
  64. bool window_switching_flag;
  65. BlockType block_type;
  66. bool mixed_block_flag;
  67. Array<int, 3> table_select;
  68. Array<int, 3> sub_block_gain;
  69. u32 region0_count;
  70. u32 region1_count;
  71. bool preflag;
  72. bool scalefac_scale;
  73. bool count1table_select;
  74. };
  75. struct Channel {
  76. Array<Granule, 2> granules;
  77. Array<int, 39> scale_factors;
  78. Array<int, 4> scale_factor_selection_info;
  79. };
  80. struct MP3Frame {
  81. Header header;
  82. FixedArray<Channel> channels;
  83. off_t main_data_begin;
  84. u32 private_bits;
  85. MP3Frame(Header header)
  86. : header(header)
  87. , channels(FixedArray<Channel>::must_create_but_fixme_should_propagate_errors(header.channel_count()))
  88. {
  89. }
  90. };
  91. }