forked from jfchapman/VUPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecoderFlac.cpp
185 lines (167 loc) · 5.19 KB
/
DecoderFlac.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "DecoderFlac.h"
DecoderFlac::DecoderFlac( const std::wstring& filename ) :
Decoder(),
FLAC::Decoder::Stream(),
m_FileStream(),
m_FLACFrame(),
m_FLACBuffer( nullptr ),
m_FLACFramePos( 0 ),
m_Valid( false )
{
m_FileStream.open( filename, std::ios::binary | std::ios::in );
if ( m_FileStream.is_open() ) {
if ( init() == FLAC__STREAM_DECODER_INIT_STATUS_OK ) {
process_until_end_of_metadata();
}
}
if ( m_Valid ) {
SetBitrate( CalculateBitrate() );
} else {
finish();
m_FileStream.close();
throw std::runtime_error( "DecoderFlac could not load file" );
}
}
DecoderFlac::~DecoderFlac()
{
finish();
m_FileStream.close();
}
long DecoderFlac::Read( float* buffer, const long sampleCount )
{
long samplesRead = 0;
while ( samplesRead < sampleCount ) {
if ( m_FLACFramePos >= m_FLACFrame.header.blocksize ) {
m_FLACFramePos = 0;
m_FLACFrame = {};
if ( !process_single() || ( 0 == m_FLACFrame.header.blocksize ) ) {
break;
}
}
for ( unsigned int channel = 0; channel < m_FLACFrame.header.channels; channel++ ) {
buffer[ samplesRead * m_FLACFrame.header.channels + channel ] =
static_cast<float>( m_FLACBuffer[ channel ][ m_FLACFramePos ] ) / ( 1 << ( m_FLACFrame.header.bits_per_sample - 1 ) );
}
++samplesRead;
++m_FLACFramePos;
}
return samplesRead;
}
float DecoderFlac::Seek( const float position )
{
float seekPosition = 0;
m_FLACFramePos = 0;
m_FLACFrame = {};
if ( ( GetSampleRate() > 0 ) && seek_absolute( static_cast<FLAC__uint64>( position * GetSampleRate() ) ) ) {
process_single();
seekPosition = static_cast<float>( m_FLACFrame.header.number.sample_number ) / GetSampleRate();
}
else {
reset();
}
return seekPosition;
}
std::optional<float> DecoderFlac::CalculateBitrate()
{
std::optional<float> bitrate;
if ( const float duration = GetDuration(); ( duration > 0 ) && m_FileStream.good() ) {
const auto initial = m_FileStream.tellg();
m_FileStream.seekg( 0, std::ios_base::end );
const long long filesize = m_FileStream.tellg();
m_FileStream.seekg( 0, std::ios_base::beg );
std::vector<char> block( 4 );
m_FileStream.read( block.data(), 4 );
if ( ( 'f' == block[ 0 ] ) && ( 'L' == block[ 1 ] ) && ( 'a' == block[ 2 ] ) && ('C' == block[ 3 ] ) ) {
m_FileStream.read( block.data(), 4 );
while ( m_FileStream.good() ) {
const long long currentPos = m_FileStream.tellg();
const unsigned long blockSize = ( static_cast<unsigned char>( block[ 1 ] ) << 16 ) | ( static_cast<unsigned char>( block[ 2 ] ) << 8 ) | static_cast<unsigned char>( block[ 3 ] );
if ( ( currentPos + blockSize ) < filesize ) {
const bool lastBlock = block[ 0 ] & 0x80;
if ( lastBlock ) {
const long long streamsize = filesize - currentPos - blockSize;
bitrate = ( streamsize * 8 ) / ( duration * 1000 );
break;
}
} else {
break;
}
m_FileStream.seekg( blockSize, std::ios_base::cur );
m_FileStream.read( block.data(), 4 );
};
}
if ( !m_FileStream.good() ) {
m_FileStream.clear();
}
m_FileStream.seekg( initial );
}
return bitrate;
}
FLAC__StreamDecoderReadStatus DecoderFlac::read_callback( FLAC__byte buf[], size_t * size )
{
FLAC__StreamDecoderReadStatus status = FLAC__STREAM_DECODER_READ_STATUS_ABORT;
if ( m_FileStream.eof() ) {
*size = 0;
status = FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
} else {
m_FileStream.read( reinterpret_cast<char*>( buf ), *size );
*size = static_cast<size_t>( m_FileStream.gcount() );
if ( *size > 0 ) {
status = FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
}
return status;
}
FLAC__StreamDecoderSeekStatus DecoderFlac::seek_callback( FLAC__uint64 pos )
{
if ( !m_FileStream.good() ) {
m_FileStream.clear();
}
m_FileStream.seekg( pos, std::ios_base::beg );
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
}
FLAC__StreamDecoderTellStatus DecoderFlac::tell_callback( FLAC__uint64 * pos )
{
if ( !m_FileStream.good() ) {
m_FileStream.clear();
}
*pos = m_FileStream.tellg();
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}
FLAC__StreamDecoderLengthStatus DecoderFlac::length_callback( FLAC__uint64 * pos )
{
if ( !m_FileStream.good() ) {
m_FileStream.clear();
}
const std::streampos currentPos = m_FileStream.tellg();
m_FileStream.seekg( 0, std::ios_base::end );
*pos = m_FileStream.tellg();
m_FileStream.seekg( currentPos );
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}
bool DecoderFlac::eof_callback()
{
const bool eof = m_FileStream.eof();
return eof;
}
FLAC__StreamDecoderWriteStatus DecoderFlac::write_callback( const FLAC__Frame * frame, const FLAC__int32 *const buffer[] )
{
m_FLACFrame = *frame;
m_FLACBuffer = const_cast<FLAC__int32**>( buffer );
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
void DecoderFlac::metadata_callback( const FLAC__StreamMetadata * metadata )
{
if ( metadata->type == FLAC__METADATA_TYPE_STREAMINFO ) {
SetBPS( metadata->data.stream_info.bits_per_sample );
SetChannels( metadata->data.stream_info.channels );
SetSampleRate( metadata->data.stream_info.sample_rate );
if ( GetSampleRate() > 0 ) {
SetDuration( static_cast<float>( metadata->data.stream_info.total_samples ) / GetSampleRate() );
}
m_Valid = true;
}
}
void DecoderFlac::error_callback( FLAC__StreamDecoderErrorStatus )
{
}