Tag Parser  6.2.2
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
mpegaudioframe.cpp
Go to the documentation of this file.
1 #include "./mpegaudioframe.h"
2 
3 #include "../exceptions.h"
4 
5 #include <c++utilities/io/binaryreader.h>
6 
7 using namespace std;
8 using namespace IoUtilities;
9 
10 namespace Media {
11 
15 const char *mpegChannelModeString(MpegChannelMode channelMode)
16 {
17  switch(channelMode) {
18  case MpegChannelMode::Stereo: return "2 channels: stereo";
19  case MpegChannelMode::JointStereo: return "2 channels: joint stereo";
20  case MpegChannelMode::DualChannel: return "2 channels: dual channel";
21  case MpegChannelMode::SingleChannel: return "1 channel: single channel";
22  default: return nullptr;
23  }
24 }
25 
26 const uint64 MpegAudioFrame::m_xingHeaderOffset = 0x24;
27 
33 const int MpegAudioFrame::m_bitrateTable[0x2][0x3][0xF] =
34 {
35  {{0,32,64,96,128,160,192,224,256,288,320,352,384,416,448},
36  {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384},
37  {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320}},
38  {{0,32,48,56,64,80,96,112,128,144,160,176,192,224,256},
39  {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160},
40  {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160}}
41 };
42 
43 const uint32 MpegAudioFrame::m_sync = 0xFFE00000u;
44 
50 void MpegAudioFrame::parseHeader(BinaryReader &reader)
51 {
52  m_header = reader.readUInt32BE();
53  if(!isValid()) {
54  throw InvalidDataException();
55  }
56  reader.stream()->seekg(m_xingHeaderOffset - 4, ios_base::cur);
57  m_xingHeader = reader.readUInt64BE();
58  m_xingHeaderFlags = static_cast<XingHeaderFlags>(m_xingHeader & 0xffffffffuL);
59  if(isXingHeaderAvailable()) {
60  if(isXingFramefieldPresent()) {
61  m_xingFramefield = reader.readUInt32BE();
62  }
63  if(isXingBytesfieldPresent()) {
64  m_xingBytesfield = reader.readUInt32BE();
65  }
66  if(isXingTocFieldPresent()) {
67  reader.stream()->seekg(64, ios_base::cur);
68  }
69  if(isXingQualityIndicatorFieldPresent()) {
70  m_xingQualityIndicator = reader.readUInt32BE();
71  }
72  }
73 }
74 
78 double MpegAudioFrame::mpegVersion() const
79 {
80  switch(m_header & 0x180000u) {
81  case 0x180000u:
82  return 1.0;
83  case 0x100000u:
84  return 2.0;
85  case 0x0u:
86  return 2.5;
87  default:
88  return 0.0;
89  }
90 }
91 
95 int MpegAudioFrame::layer() const
96 {
97  switch (m_header & 0x60000u) {
98  case 0x60000u:
99  return 1;
100  case 0x40000u:
101  return 2;
102  case 0x20000u:
103  return 3;
104  default:
105  return 0;
106  }
107 }
108 
112 uint32 MpegAudioFrame::samplingFrequency() const
113 {
114  switch (m_header & 0xc00u) {
115  case 0xc00u:
116  return 0;
117  case 0x800u:
118  switch (m_header & 0x180000u) {
119  case 0x180000u:
120  return 32000;
121  case 0x100000u:
122  return 16000;
123  case 0x0u:
124  return 8000u;
125  }
126  break;
127  case 0x400u:
128  switch (m_header & 0x180000u) {
129  case 0x180000u:
130  return 48000;
131  case 0x100000u:
132  return 24000;
133  case 0x0u:
134  return 12000;
135  }
136  break;
137  case 0x0u:
138  switch (m_header & 0x180000u) {
139  case 0x180000u:
140  return 44100;
141  case 0x100000:
142  return 22050;
143  case 0x0u:
144  return 11025;
145  }
146  break;
147  }
148  return 0;
149 }
150 
154 MpegChannelMode MpegAudioFrame::channelMode() const
155 {
156  if(isValid()) {
157  switch (m_header & 0xc0u) {
158  case 0xc0u:
159  return MpegChannelMode::SingleChannel;
160  case 0x80u:
161  return MpegChannelMode::DualChannel;
162  case 0x40u:
163  return MpegChannelMode::JointStereo;
164  case 0x00:
165  return MpegChannelMode::Stereo;
166  default:
167  ;
168  }
169  }
170  return MpegChannelMode::Unspecifed;
171 }
172 
176 uint32 MpegAudioFrame::sampleCount() const
177 {
178  switch (m_header & 0x60000u) {
179  case 0x60000u:
180  return 384u;
181  case 0x40000u:
182  return 1152u;
183  case 0x20000u:
184  switch (m_header & 0x180000u) {
185  case 0x180000u:
186  return 1152u;
187  case 0x100000u:
188  case 0x0u:
189  return 576u;
190  }
191  default:
192  ;
193  }
194  return 0;
195 }
196 
200 uint32 MpegAudioFrame::size() const
201 {
202  switch (m_header & 0x60000u) {
203  case 0x60000u:
204  return static_cast<uint32>(((static_cast<double>(bitrate()) * 1024.0 / 8.0) / static_cast<double>(samplingFrequency())) * static_cast<double>(sampleCount()) + static_cast<double>(paddingSize()));
205  case 0x40000u:
206  case 0x20000u:
207  return static_cast<uint32>(((static_cast<double>(bitrate()) * 1024.0 / 8.0) / static_cast<double>(samplingFrequency())) * static_cast<double>(sampleCount()) + static_cast<double>(paddingSize()));
208  default:
209  return 0;
210  }
211 }
212 
213 }
STL namespace.
Contains utility classes helping to read and write streams.
The exception that is thrown when the data to be parsed or to be made seems invalid and therefore can...
Definition: exceptions.h:27
MpegChannelMode
Specifies the channel mode.
Contains all classes and functions of the TagInfo library.
Definition: exceptions.h:9
TAG_PARSER_EXPORT const char * mpegChannelModeString(MpegChannelMode channelMode)
Returns the string representation for the specified channelMode.