1
0

MPEGHeader.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "MPEGHeader.h"
  2. #include <math.h>
  3. // [mpeg_version][layer][index]
  4. static const int bitrates[4][4][15] =
  5. {
  6. {
  7. // MPEG-2.5
  8. { 0,},
  9. { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000}, // Layer 3
  10. { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000}, // Layer 2
  11. { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 176000, 192000, 224000, 256000}, // Layer 1
  12. },
  13. {
  14. // invalid
  15. { 0, },
  16. { 0, },
  17. { 0, },
  18. { 0, },
  19. },
  20. {
  21. // MPEG-2
  22. { 0,},
  23. { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000}, // Layer 3
  24. { 0, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000}, // Layer 2
  25. { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 176000, 192000, 224000, 256000}, // Layer 1
  26. },
  27. {
  28. // MPEG-1
  29. { 0,},
  30. { 0, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000}, // Layer 3
  31. { 0, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000}, // Layer 2
  32. { 0, 32000, 64000, 96000, 128000, 160000, 192000, 224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000}, // Layer 1
  33. },
  34. };
  35. // [mpeg_version][index]
  36. static const int sample_rates[4][4] =
  37. {
  38. {11025, 12000, 8000, 0}, // MPEG-2.5
  39. {0, },
  40. {22050, 24000, 16000, 0}, // MPEG-2
  41. {44100, 48000, 32000, 0}, // MPEG-1
  42. };
  43. // [mpeg_version][layer]
  44. static const int samples_per_frame[4][4] =
  45. {
  46. // Layer 3, Layer 2, Layer 1
  47. { 0, 576, 1152, 384}, // MPEG2.5
  48. { 0, },
  49. { 0, 576, 1152, 384}, // MPEG2
  50. { 0, 1152, 1152, 384}, // MPEG1
  51. };
  52. // [layer]
  53. static const int bits_per_slot[4] = { 0, 8, 8, 32 };
  54. void MPEGHeader::ReadBuffer(const uint8_t *buffer)
  55. {
  56. sync = ((uint16_t)buffer[0] << 3) | (buffer[1] >> 5);
  57. mpeg_version = (buffer[1] >> 3) & 3;
  58. layer = (buffer[1] >> 1) & 3;
  59. protection = (buffer[1]) & 1;
  60. bitrate_index = (buffer[2] >> 4) & 0xF;
  61. sample_rate_index = (buffer[2] >> 2) & 3;
  62. padding_bit = (buffer[2] >> 1) & 1;
  63. private_bit = buffer[2] & 1;
  64. channel_mode = (buffer[3] >> 6) & 3;
  65. mode_extension = (buffer[3] >> 4) & 3;
  66. copyright = (buffer[3] >> 3) & 1;
  67. original = (buffer[3] >> 2) & 1;
  68. emphasis = (buffer[3]) & 3;
  69. }
  70. bool MPEGHeader::IsSync() const
  71. {
  72. return sync == 0x07FF
  73. && layer != LayerError
  74. && mpeg_version != MPEG_Error
  75. && bitrate_index != 15
  76. && bitrate_index != 0
  77. && sample_rate_index != 3
  78. && !(mpeg_version == MPEG2 && layer != Layer3)
  79. && !(mpeg_version == MPEG2_5 && layer != Layer3);
  80. }
  81. int MPEGHeader::GetBitrate() const
  82. {
  83. return bitrates[mpeg_version][layer][bitrate_index];
  84. }
  85. int MPEGHeader::HeaderSize() const
  86. {
  87. if (protection == CRC)
  88. return 4 + 2; // 32bits frame header, 16bits CRC
  89. else
  90. return 4; // 32bits frame ehader
  91. }
  92. int MPEGHeader::GetSampleRate() const
  93. {
  94. return sample_rates[mpeg_version][sample_rate_index];
  95. }
  96. bool MPEGHeader::IsCopyright() const
  97. {
  98. return copyright == 1;
  99. }
  100. bool MPEGHeader::IsCRC() const
  101. {
  102. return protection == CRC;
  103. }
  104. bool MPEGHeader::IsOriginal() const
  105. {
  106. return original == 1;
  107. }
  108. int MPEGHeader::GetSamplesPerFrame() const
  109. {
  110. return samples_per_frame[mpeg_version][layer];
  111. }
  112. int MPEGHeader::FrameSize() const
  113. {
  114. int nBitsPerSlot;
  115. int nAvgSlotsPerFrame;
  116. nBitsPerSlot = bits_per_slot[layer];
  117. nAvgSlotsPerFrame = (GetSamplesPerFrame() * (bitrates[mpeg_version][layer][bitrate_index] / nBitsPerSlot)) / sample_rates[mpeg_version][sample_rate_index];
  118. return (nAvgSlotsPerFrame + padding_bit) * nBitsPerSlot / 8;
  119. }
  120. int MPEGHeader::GetLayer() const
  121. {
  122. switch(layer)
  123. {
  124. case Layer1:
  125. return 1;
  126. case Layer2:
  127. return 2;
  128. case Layer3:
  129. return 3;
  130. default:
  131. return 0;
  132. }
  133. }
  134. int MPEGHeader::GetNumChannels() const
  135. {
  136. switch(channel_mode)
  137. {
  138. case Stereo:
  139. return 2;
  140. case JointStereo:
  141. return 2;
  142. case DualChannel:
  143. return 2;
  144. case Mono:
  145. return 1;
  146. default:
  147. return 0;
  148. }
  149. }