Skip to content

Commit 9b68c99

Browse files
committed
fix sample rate in mp4a box
1 parent 425469a commit 9b68c99

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

ISOBMFF/include/ISOBMFF/MP4A.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ namespace ISOBMFF
5858

5959
uint16_t GetChannelCount() const;
6060
uint16_t GetSampleSize() const;
61-
uint32_t GetSampleRate() const;
61+
uint32_t GetSampleRateRaw() const;
62+
float GetSampleRate() const;
6263

6364
void SetChannelCount( uint16_t value );
6465
void SetSampleSize( uint16_t value );
65-
void SetSampleRate( uint32_t value );
66+
void SetSampleRateRaw( uint32_t value );
6667

6768
void AddBox( std::shared_ptr< Box > box ) override;
6869
std::vector< std::shared_ptr< Box > > GetBoxes() const override;

ISOBMFF/source/MP4A.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ namespace ISOBMFF
111111
stream.ReadUInt16();
112112

113113
// template unsigned int(32) samplerate = { default samplerate of media } << 16;
114-
uint32_t sampleRateFixed = stream.ReadBigEndianUInt32();
115-
SetSampleRate( (sampleRateFixed >> 16) + ((sampleRateFixed & 0xFFFF) / 65536) );
114+
SetSampleRateRaw( stream.ReadBigEndianUInt32() );
116115

117116
container.ReadData( parser, stream );
118117
this->impl->_boxes = container.GetBoxes();
@@ -124,6 +123,7 @@ namespace ISOBMFF
124123

125124
props.push_back( { "Channel Count", std::to_string(this->GetChannelCount()) } );
126125
props.push_back( { "Sample Size", std::to_string(this->GetSampleSize()) } );
126+
props.push_back( { "Sample Rate (raw)", std::to_string(this->GetSampleRateRaw()) } );
127127
props.push_back( { "Sample Rate", std::to_string(this->GetSampleRate()) } );
128128

129129
return props;
@@ -145,11 +145,25 @@ namespace ISOBMFF
145145
return this->impl->_samplesize;
146146
}
147147

148-
uint32_t MP4A::GetSampleRate() const
148+
uint32_t MP4A::GetSampleRateRaw() const
149149
{
150150
return this->impl->_samplerate;
151151
}
152152

153+
float MP4A::GetSampleRate() const
154+
{
155+
uint32_t sampleRateFixed = this->GetSampleRateRaw();
156+
// Read Sample Rate:
157+
// The sample rate is stored as a 16.16 fixed-point number (32 bits).
158+
// The integer part is obtained by shifting the value right by 16 bits
159+
// (>> 16).
160+
float sampleRate = (sampleRateFixed >> 16);
161+
// The fractional part is obtained by masking the lower 16 bits
162+
// (& 0xFFFF) and dividing by 65536.0.
163+
sampleRate += ((sampleRateFixed & 0xffff) / 65536.0);
164+
return sampleRate;
165+
}
166+
153167
void MP4A::SetChannelCount( uint16_t channelcount )
154168
{
155169
this->impl->_channelcount = channelcount;
@@ -160,7 +174,7 @@ namespace ISOBMFF
160174
this->impl->_samplesize = samplesize;
161175
}
162176

163-
void MP4A::SetSampleRate( uint32_t samplerate )
177+
void MP4A::SetSampleRateRaw( uint32_t samplerate )
164178
{
165179
this->impl->_samplerate = samplerate;
166180
}

0 commit comments

Comments
 (0)