forked from jfchapman/VUPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandlerFlac.cpp
355 lines (327 loc) · 11.6 KB
/
HandlerFlac.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "HandlerFlac.h"
#include "DecoderFlac.h"
#include "EncoderFlac.h"
#include <share/windows_unicode_filenames.h>
#include "Utility.h"
// Amount of padding to add when writing out FLAC files that don't contain any padding.
constexpr uint32_t kPaddingSize = 1024;
HandlerFlac::HandlerFlac() :
Handler()
{
}
HandlerFlac::~HandlerFlac()
{
}
std::wstring HandlerFlac::GetDescription() const
{
const std::wstring description = L"Free Lossless Audio Codec " + UTF8ToWideString( FLAC__VERSION_STRING );
return description;
}
std::set<std::wstring> HandlerFlac::GetSupportedFileExtensions() const
{
const std::set<std::wstring> fileTypes = { L"flac" };
return fileTypes;
}
bool HandlerFlac::GetTags( const std::wstring& filename, Tags& tags ) const
{
bool success = false;
tags.clear();
flac_internal_set_utf8_filenames( true );
FLAC::Metadata::SimpleIterator iterator;
if ( iterator.is_valid() && iterator.init( WideStringToUTF8( filename ).c_str(), true /*readOnly*/, true /*preserveFileStats*/ ) ) {
success = true;
do {
const FLAC__MetadataType blockType = iterator.get_block_type();
if ( FLAC__METADATA_TYPE_VORBIS_COMMENT == blockType ) {
FLAC::Metadata::Prototype* block = iterator.get_block();
if ( nullptr != block ) {
FLAC::Metadata::VorbisComment* vorbisComment = dynamic_cast<FLAC::Metadata::VorbisComment*>( block );
if ( ( nullptr != vorbisComment ) && ( vorbisComment->is_valid() ) ) {
const unsigned char* vendor = vorbisComment->get_vendor_string();
if ( ( nullptr != vendor ) && ( vendor[ 0 ] != 0 ) ) {
tags.insert( Tags::value_type( Tag::Version, reinterpret_cast<const char*>( vendor ) ) );
}
const unsigned int commentCount = vorbisComment->get_num_comments();
for ( unsigned int commentIndex = 0; commentIndex < commentCount; commentIndex++ ) {
const FLAC::Metadata::VorbisComment::Entry entry = vorbisComment->get_comment( commentIndex );
if ( ( entry.is_valid() ) && ( entry.get_field_name_length() != 0 ) && ( entry.get_field_value_length() != 0 ) ) {
const char* field = entry.get_field_name();
if ( 0 == _stricmp( field, "ARTIST" ) ) {
tags.insert( Tags::value_type( Tag::Artist, entry.get_field_value() ) );
} else if ( 0 == _stricmp( field, "TITLE" ) ) {
tags.insert( Tags::value_type( Tag::Title, entry.get_field_value() ) );
} else if ( 0 == _stricmp( field, "ALBUM" ) ) {
tags.insert( Tags::value_type( Tag::Album, entry.get_field_value() ) );
} else if ( 0 == _stricmp( field, "GENRE" ) ) {
tags.insert( Tags::value_type( Tag::Genre, entry.get_field_value() ) );
} else if ( ( 0 == _stricmp( field, "YEAR" ) ) || ( 0 == _stricmp( field, "DATE" ) ) ) {
tags.insert( Tags::value_type( Tag::Year, entry.get_field_value() ) );
} else if ( 0 == _stricmp( field, "COMMENT" ) ) {
tags.insert( Tags::value_type( Tag::Comment, entry.get_field_value() ) );
} else if ( ( 0 == _stricmp( field, "TRACK" ) ) || ( 0 == _stricmp( field, "TRACKNUMBER" ) ) ) {
tags.insert( Tags::value_type( Tag::Track, entry.get_field_value() ) );
} else if ( 0 == _stricmp( field, "REPLAYGAIN_TRACK_GAIN" ) ) {
tags.insert( Tags::value_type( Tag::GainTrack, entry.get_field_value() ) );
} else if ( 0 == _stricmp( field, "REPLAYGAIN_ALBUM_GAIN" ) ) {
tags.insert( Tags::value_type( Tag::GainAlbum, entry.get_field_value() ) );
}
}
}
}
delete block;
block = nullptr;
}
} else if ( FLAC__METADATA_TYPE_PICTURE == blockType ) {
FLAC::Metadata::Prototype* block = iterator.get_block();
if ( nullptr != block ) {
FLAC::Metadata::Picture* picture = dynamic_cast<FLAC::Metadata::Picture*>( block );
if ( ( nullptr != picture ) && ( picture->is_valid() ) && ( FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER == picture->get_type() ) ) {
const int dataLength = static_cast<int>( picture->get_data_length() );
if ( dataLength > 0 ) {
const std::string encodedImage = Base64Encode( reinterpret_cast<const BYTE*>( picture->get_data() ), dataLength );
if ( !encodedImage.empty() ) {
tags.insert( Tags::value_type( Tag::Artwork, encodedImage ) );
}
}
}
delete block;
block = nullptr;
}
}
} while ( iterator.next() );
}
return success;
}
bool HandlerFlac::SetTags( const std::wstring& filename, const Tags& tags ) const
{
bool success = false;
flac_internal_set_utf8_filenames( true );
FLAC::Metadata::Chain chain;
if ( chain.is_valid() && chain.read( WideStringToUTF8( filename ).c_str() ) ) {
FLAC::Metadata::Iterator iterator;
if ( iterator.is_valid() ) {
iterator.init( chain );
bool writeChain = false;
// Determine which metadata blocks need to be updated.
bool updateVorbisComment = false;
bool updatePicture = false;
bool clearPicture = false;
for ( const auto& tagIter : tags ) {
const Tag tag = tagIter.first;
switch ( tag ) {
case Tag::Album :
case Tag::Artist :
case Tag::Comment :
case Tag::Genre :
case Tag::Title :
case Tag::Track :
case Tag::Year :
case Tag::GainAlbum :
case Tag::GainTrack : {
updateVorbisComment = true;
break;
}
case Tag::Artwork : {
updatePicture = true;
clearPicture = tagIter.second.empty();
break;
}
default : {
break;
}
}
}
if ( !updateVorbisComment && !updatePicture ) {
success = true;
} else {
if ( updateVorbisComment ) {
// Insert a vorbis comment block if necessary.
bool vorbisCommentExists = false;
do {
vorbisCommentExists = ( FLAC__METADATA_TYPE_VORBIS_COMMENT == iterator.get_block_type() );
} while ( !vorbisCommentExists && iterator.next() );
if ( !vorbisCommentExists ) {
FLAC::Metadata::VorbisComment* vorbisComment = new FLAC::Metadata::VorbisComment();
iterator.insert_block_after( vorbisComment );
writeChain = true;
}
// Update vorbis comments.
iterator.init( chain );
do {
const FLAC__MetadataType blockType = iterator.get_block_type();
if ( FLAC__METADATA_TYPE_VORBIS_COMMENT == blockType ) {
FLAC::Metadata::Prototype* block = iterator.get_block();
if ( nullptr != block ) {
FLAC::Metadata::VorbisComment* vorbisComment = dynamic_cast<FLAC::Metadata::VorbisComment*>( block );
if ( nullptr != vorbisComment ) {
for ( const auto& tagIter : tags ) {
const Tag tag = tagIter.first;
const std::string& value = tagIter.second;
std::string field;
switch ( tag ) {
case Tag::Album : {
field = "ALBUM";
break;
}
case Tag::Artist : {
field = "ARTIST";
break;
}
case Tag::Comment : {
field = "COMMENT";
break;
}
case Tag::Genre : {
field = "GENRE";
break;
}
case Tag::Title : {
field = "TITLE";
break;
}
case Tag::Track : {
field = "TRACKNUMBER";
break;
}
case Tag::Year : {
field = "DATE";
break;
}
case Tag::GainAlbum : {
field = "REPLAYGAIN_ALBUM_GAIN";
break;
}
case Tag::GainTrack : {
field = "REPLAYGAIN_TRACK_GAIN";
break;
}
default : {
break;
}
}
if ( !field.empty() ) {
if ( value.empty() ) {
vorbisComment->remove_entries_matching( field.c_str() );
writeChain = true;
} else {
FLAC::Metadata::VorbisComment::Entry entry( field.c_str(), value.c_str() );
vorbisComment->replace_comment( entry, true /*all*/ );
writeChain = true;
}
}
}
}
delete block;
block = nullptr;
}
break;
}
} while ( iterator.next() );
}
if ( updatePicture ) {
iterator.init( chain );
// Delete any existing (front cover) picture blocks.
do {
if ( FLAC__METADATA_TYPE_PICTURE == iterator.get_block_type() ) {
FLAC::Metadata::Prototype* block = iterator.get_block();
if ( nullptr != block ) {
FLAC::Metadata::Picture* picture = dynamic_cast<FLAC::Metadata::Picture*>( block );
if ( ( nullptr != picture ) && ( FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER == picture->get_type() ) ) {
iterator.delete_block( false /*replaceWithPadding*/ );
writeChain = true;
}
delete block;
block = nullptr;
}
}
} while ( iterator.next() );
if ( !clearPicture ) {
// Insert (front cover) picture block.
const auto pictureIter = tags.find( Tag::Artwork );
if ( tags.end() != pictureIter ) {
const std::string& encodedImage = pictureIter->second;
const std::vector<BYTE> imageBytes = Base64Decode( encodedImage );
const size_t imageSize = imageBytes.size();
if ( imageSize > 0 ) {
std::string mimeType;
int width = 0;
int height = 0;
int depth = 0;
int colours = 0;
GetImageInformation( encodedImage, mimeType, width, height, depth, colours );
FLAC::Metadata::Picture* picture = new FLAC::Metadata::Picture();
picture->set_mime_type( mimeType.c_str() );
picture->set_width( static_cast<FLAC__uint32>( width ) );
picture->set_height( static_cast<FLAC__uint32>( height ) );
picture->set_depth( static_cast<FLAC__uint32>( depth ) );
picture->set_colors( static_cast<FLAC__uint32>( colours ) );
picture->set_data( imageBytes.data(), static_cast<FLAC__uint32>( imageSize ) );
picture->set_type( FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER );
iterator.insert_block_after( picture );
writeChain = true;
}
}
}
}
if ( writeChain ) {
chain.sort_padding();
// If there is no padding, add an arbitrary amount before writing out the chain.
iterator.init( chain );
bool paddingExists = false;
do {
if ( FLAC__METADATA_TYPE_PADDING == iterator.get_block_type() ) {
FLAC::Metadata::Prototype* block = iterator.get_block();
if ( nullptr != block ) {
FLAC::Metadata::Padding* padding = dynamic_cast<FLAC::Metadata::Padding*>( block );
paddingExists = ( ( nullptr != padding ) && ( padding->get_length() > 0 ) );
delete block;
block = nullptr;
}
}
} while ( !paddingExists && iterator.next() );
if ( !paddingExists ) {
FLAC::Metadata::Padding* padding = new FLAC::Metadata::Padding( kPaddingSize );
iterator.insert_block_after( padding );
chain.sort_padding();
}
success = chain.write();
}
}
}
}
return success;
}
Decoder::Ptr HandlerFlac::OpenDecoder( const std::wstring& filename ) const
{
DecoderFlac* streamFlac = nullptr;
try {
streamFlac = new DecoderFlac( filename );
} catch ( const std::runtime_error& ) {
}
const Decoder::Ptr stream( streamFlac );
return stream;
}
Encoder::Ptr HandlerFlac::OpenEncoder() const
{
Encoder::Ptr encoder( new EncoderFlac() );
return encoder;
}
bool HandlerFlac::IsDecoder() const
{
return true;
}
bool HandlerFlac::IsEncoder() const
{
return true;
}
bool HandlerFlac::CanConfigureEncoder() const
{
return false;
}
bool HandlerFlac::ConfigureEncoder( const HINSTANCE /*instance*/, const HWND /*parent*/, std::string& /*settings*/ ) const
{
return false;
}
void HandlerFlac::SettingsChanged( Settings& /*settings*/ )
{
}