libtaginfo / libtaginfo / asfinfo.cc

  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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 * Copyright (C) 2008-2013 J.Rios <anonbeat@gmail.com>
 * Copyright (C) 2013 Jörn Magens
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This Program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file LICENSE.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth 
 * Floor, Boston, MA  02110-1301  USA
 * https://www.gnu.org/licenses/lgpl-2.1.txt
 *
 * Author:
 * 	Jörn Magens <shuerhaaken@googlemail.com>
 * 	Matias De lellis <mati86dl@gmail.com>
 * 	Pavel Vasin <rat4vier@gmail.com>
 */




#include "taginfo.h"
#include "taginfo_internal.h"
#include "taginfo_asftags.h"


#include <asffile.h>
#include <asftag.h>
#include <asfpicture.h>


#define COMPILATION_FLAG         "WM/PartOfACompilation"
#define PICTURE_FLAG             "WM/Picture"
#define TOTAL_TRACKS             "TrackTotal"
#define ALBUM_ARTIST             "WM/AlbumArtist"
#define VOLUME_NUMBER            "WM/PartOfSet"
#define COMPOSER                 "WM/Composer"
#define ORIGINAL_ARTIST          "WM/OriginalArtist"
#define COPYRIGHT                "Copyright"
#define ENCODER                  "WM/EncodedBy"
#define HOMEPAGE                 "URL"
#define SHARED_RATING            "WM/SharedUserRating"
#define TRACK_LABELS             "TRACK_LABELS"
#define ALBUM_LABELS             "ALBUM_LABELS"
#define ARTIST_LABELS            "ARTIST_LABELS"
#define LYRICS                   "WM/Lyrics"


using namespace TagInfo;



inline int wm_rating_to_rating(const int rating) {
    if(rating <= 0)
        return 0;
    if(rating < 25)
        return 1;
    if(rating < 50)
        return 2;
    if(rating < 75)
        return 3;
    if(rating < 99)
        return 4;
    return 5;
}

ImageType get_image_type_from_pic_type(const ASF::Picture::Type& tpe) {
    ImageType itp = IMAGE_TYPE_OTHER;
    switch(tpe) {
        case ASF::Picture::FrontCover :
            itp = IMAGE_TYPE_COVER_FRONT;
            break;
        case ASF::Picture::BackCover :
            itp = IMAGE_TYPE_COVER_BACK;
            break;
        case ASF::Picture::Other :
            itp = IMAGE_TYPE_OTHER;
            break;
        case ASF::Picture::FileIcon :
            itp = IMAGE_TYPE_FILE_ICON;
            break;
        case ASF::Picture::OtherFileIcon :
            itp = IMAGE_TYPE_OTHER_FILE_ICON;
            break;
        case ASF::Picture::LeafletPage :
            itp = IMAGE_TYPE_LEAFLET_PAGE;
            break;
        case ASF::Picture::Media :
            itp = IMAGE_TYPE_MEDIA;
            break;
        case ASF::Picture::LeadArtist :
            itp = IMAGE_TYPE_LEAD_ARTIST;
            break;
        case ASF::Picture::Artist :
            itp = IMAGE_TYPE_ARTIST;
            break;
        case ASF::Picture::Conductor :
            itp = IMAGE_TYPE_CONDUCTOR;
            break;
        case ASF::Picture::Band :
            itp = IMAGE_TYPE_BAND;
            break;
        case ASF::Picture::Composer :
            itp = IMAGE_TYPE_COMPOSER;
            break;
        case ASF::Picture::Lyricist :
            itp = IMAGE_TYPE_LYRICIST;
            break;
        case ASF::Picture::RecordingLocation :
            itp = IMAGE_TYPE_RECORDING_LOCATION;
            break;
        case ASF::Picture::DuringRecording :
            itp = IMAGE_TYPE_DURING_RECORDING;
            break;
        case ASF::Picture::DuringPerformance :
            itp = IMAGE_TYPE_DURING_PERFORMANCE;
            break;
        case ASF::Picture::MovieScreenCapture :
            itp = IMAGE_TYPE_MOVIESCREENCAPTURE;
            break;
        case ASF::Picture::ColouredFish :
            itp = IMAGE_TYPE_COLOURED_FISH;
            break;
        case ASF::Picture::Illustration :
            itp = IMAGE_TYPE_ILLUSTRATION;
            break;
        case ASF::Picture::BandLogo :
            itp = IMAGE_TYPE_ARTIST_LOGO;
            break;
        case ASF::Picture::PublisherLogo :
            itp = IMAGE_TYPE_PUBLISHER_LOGO;
            break;
        default:
            itp = IMAGE_TYPE_OTHER;
            break;
    }
    return itp;
}

ASF::Picture::Type image_type_to_picture_type(const ImageType& image_type) {
    ASF::Picture::Type itp;
    switch(image_type) {
        case IMAGE_TYPE_COVER_FRONT :
            itp = ASF::Picture::FrontCover;
            break;
        case IMAGE_TYPE_COVER_BACK :
            itp = ASF::Picture::BackCover;
            break;
        case IMAGE_TYPE_OTHER :
            itp = ASF::Picture::Other;
            break;
        case IMAGE_TYPE_FILE_ICON :
            itp = ASF::Picture::FileIcon;
            break;
        case IMAGE_TYPE_OTHER_FILE_ICON :
            itp = ASF::Picture::OtherFileIcon;
            break;
        case IMAGE_TYPE_LEAFLET_PAGE :
            itp = ASF::Picture::LeafletPage;
            break;
        case IMAGE_TYPE_MEDIA  :
            itp = ASF::Picture::Media;
            break;
        case IMAGE_TYPE_LEAD_ARTIST :
            itp = ASF::Picture::LeadArtist;
            break;
        case IMAGE_TYPE_ARTIST :
            itp = ASF::Picture::Artist;
            break;
        case IMAGE_TYPE_CONDUCTOR :
            itp = ASF::Picture::Conductor;
            break;
        case IMAGE_TYPE_BAND :
            itp = ASF::Picture::Band;
            break;
        case IMAGE_TYPE_COMPOSER :
            itp = ASF::Picture::Composer;
            break;
        case IMAGE_TYPE_LYRICIST :
            itp = ASF::Picture::Lyricist;
            break;
        case IMAGE_TYPE_RECORDING_LOCATION :
            itp = ASF::Picture::RecordingLocation;
            break;
        case IMAGE_TYPE_DURING_RECORDING :
            itp = ASF::Picture::DuringRecording;
            break;
        case IMAGE_TYPE_DURING_PERFORMANCE :
            itp = ASF::Picture::DuringPerformance;
            break;
        case IMAGE_TYPE_MOVIESCREENCAPTURE :
            itp = ASF::Picture::MovieScreenCapture;
            break;
        case IMAGE_TYPE_COLOURED_FISH :
            itp = ASF::Picture::ColouredFish;
            break;
        case IMAGE_TYPE_ILLUSTRATION :
            itp = ASF::Picture::Illustration;
            break;
        case IMAGE_TYPE_ARTIST_LOGO :
            itp = ASF::Picture::BandLogo;
            break;
        case IMAGE_TYPE_PUBLISHER_LOGO :
            itp = ASF::Picture::PublisherLogo;
            break;
        default:
            itp = ASF::Picture::Other;
            break;
    }
}

AsfInfo::AsfInfo(const String &filename) : Info(filename) {
    if(file_name.isEmpty()) {
        valid = false;
        printf("File name empty!\n");
    }
    else {
        taglib_file = new TagLib::ASF::File(file_name.toCString(false), true, TagLib::AudioProperties::Fast);
    }
    if(taglib_file) {
        asf_tag = ((TagLib::ASF::File *) taglib_file)->tag();
        if(!asf_tag)
            valid = false;
    }
    else {
        printf("Cant get xiphcomment from '%s'\n", file_name.toCString(false));
        asf_tag = NULL;
        valid = false;
    }
}


AsfInfo::~AsfInfo() {
}


bool AsfInfo::load(void) {
    if(Info::load()) {
        if(asf_tag) {
            load_base_tags((TagLib::Tag *)asf_tag);
            
            if(asf_tag->attributeListMap().contains(VOLUME_NUMBER) && !asf_tag->attributeListMap()[ VOLUME_NUMBER ].isEmpty()) {
                StringList list = asf_tag->attributeListMap()[ VOLUME_NUMBER ].front().toString().split("/");
                if(list.size() == 2) {
                    volume_number = atol(list.front().toCString(false));
                    volume_count  = atol(list.back().toCString(false));
                }
                else if(list.size() == 1) {
                    volume_number = atol(list.front().toCString(false));
                }
            }
            if(asf_tag->attributeListMap().contains(COMPOSER)) {
                composer = asf_tag->attributeListMap()[ COMPOSER ].front().toString();
            }
            if(asf_tag->attributeListMap().contains(ORIGINAL_ARTIST)) {
                original_artist = asf_tag->attributeListMap()[ ORIGINAL_ARTIST ].front().toString();
            }
            if(asf_tag->attributeListMap().contains(TOTAL_TRACKS)) {// seems not to be official
                track_count = 
                    atol(asf_tag->attributeListMap()[ TOTAL_TRACKS ].front().toString().toCString(false));
            }
            if(asf_tag->attributeListMap().contains(COPYRIGHT)) {
                copyright = asf_tag->attributeListMap()[ COPYRIGHT ].front().toString();
            }
            if(asf_tag->attributeListMap().contains(ENCODER)) {
                encoder = asf_tag->attributeListMap()[ ENCODER ].front().toString();
            }
            if(asf_tag->attributeListMap().contains(HOMEPAGE)) {
                homepage = asf_tag->attributeListMap()[ HOMEPAGE ].front().toString();
            }
            if(asf_tag->attributeListMap().contains(COMPILATION_FLAG)) {
                is_compilation =
                    asf_tag->attributeListMap()[COMPILATION_FLAG].front().toString() == String("1");
            }
            if(asf_tag->attributeListMap().contains(ALBUM_ARTIST)) {
                album_artist = asf_tag->attributeListMap()[ ALBUM_ARTIST ].front().toString();
            }
            long rat = 0;
            if(asf_tag->attributeListMap().contains(SHARED_RATING)) {
                rat = atol(asf_tag->attributeListMap()[ SHARED_RATING ].front().toString().toCString(false));
            }
            if(!rat && asf_tag->attributeListMap().contains("Rating")) {
                rat = atol(asf_tag->attributeListMap()[ "Rating" ].front().toString().toCString(false));
            }
            if(rat) {
                if(rat > 5)
                    rating = wm_rating_to_rating(rat);
                else
                    rating = rat;
            }
            if(track_labels.size() == 0) {
                if(asf_tag->attributeListMap().contains(TRACK_LABELS)) {
                    track_labels_string = asf_tag->attributeListMap()[ TRACK_LABELS ].front().toString();
                    track_labels = split(track_labels_string, "|");
                }
            }
            if(artist_labels.size() == 0) {
                if(asf_tag->attributeListMap().contains(ARTIST_LABELS)) {
                    artist_labels_string = asf_tag->attributeListMap()[ ARTIST_LABELS ].front().toString();
                    artist_labels = split(artist_labels_string, "|");
                }
            }
            if(album_labels.size() == 0) {
                if(asf_tag->attributeListMap().contains(ALBUM_LABELS)) {
                    album_labels_string = asf_tag->attributeListMap()[ ALBUM_LABELS ].front().toString();
                    album_labels = split(album_labels_string, "|");
                }
            }
        }
    }
    else {
        printf("Error: Could not load tags from file '%s'\n", file_name.toCString(true));
        return false; 
    }
    return true;
}


void check_asf_label_frame(ASF::Tag * asftag, const char * description, const String &value) {
    if(asftag->attributeListMap().contains(description))
        asftag->removeItem(description);
    if(!value.isEmpty()) {
            asftag->setAttribute(description, value);
    }
}


bool AsfInfo::save(void) {
    if(asf_tag) {
        if(changedflag) {
            if(changedflag & CHANGED_DATA_VOL_NUM || changedflag & CHANGED_DATA_VOL_CNT) {
                asf_tag->removeItem(VOLUME_NUMBER);
                if(volume_count != 0) {
                    if(volume_number != 0)
                        asf_tag->setAttribute(VOLUME_NUMBER, format("%u/%u", volume_number, volume_count));
                    else
                        asf_tag->setAttribute(VOLUME_NUMBER, format("%u", volume_number));
                }
            }
            if(changedflag & CHANGED_TRACK_COUNT) {
                asf_tag->removeItem(TOTAL_TRACKS);
                asf_tag->setAttribute(TOTAL_TRACKS, format("%d", track_count));
            }
            if(changedflag & CHANGED_COMPOSER_TAG) {
                asf_tag->removeItem(COMPOSER);
                asf_tag->setAttribute(COMPOSER, composer);
            }
            if(changedflag & CHANGED_ORIGINALARTIST_TAG) {
                asf_tag->removeItem(ORIGINAL_ARTIST);
                asf_tag->setAttribute(ORIGINAL_ARTIST, original_artist);
            }
            if(changedflag & CHANGED_COPYRIGHT_TAG) {
                asf_tag->removeItem(COPYRIGHT);
                asf_tag->setAttribute(COPYRIGHT, copyright);
            }
            if(changedflag & CHANGED_ENCODER_TAG) {
                asf_tag->removeItem(ENCODER);
                asf_tag->setAttribute(ENCODER, encoder);
            }
            if(changedflag & CHANGED_HOMEPAGE_TAG) {
                asf_tag->removeItem(HOMEPAGE);
                asf_tag->setAttribute(HOMEPAGE, homepage);
            }
            if(changedflag & CHANGED_IS_COMPILATION_TAG) {
                asf_tag->removeItem(COMPILATION_FLAG);
                if(is_compilation)
                    asf_tag->setAttribute(COMPILATION_FLAG, String("1"));
                else
                    asf_tag->setAttribute(COMPILATION_FLAG, String("0"));
            }
            if(changedflag & CHANGED_DATA_ALBUMARTIST) {
                asf_tag->removeItem(ALBUM_ARTIST);
                asf_tag->setAttribute(ALBUM_ARTIST, album_artist);
            }
            if(changedflag & CHANGED_DATA_RATING) {
                asf_tag->removeItem(SHARED_RATING);
                int WMRatings[] = { 0, 0, 1, 25, 50, 75, 99 };
                asf_tag->setAttribute(SHARED_RATING, format("%i", WMRatings[ rating + 1 ]));
            }
            // The Labels
            if(changedflag & CHANGED_TRACK_LABELS)
                check_asf_label_frame(asf_tag, TRACK_LABELS, track_labels_string);
            if(changedflag & CHANGED_ARTIST_LABELS)
                check_asf_label_frame(asf_tag, ARTIST_LABELS, artist_labels_string);
            if(changedflag & CHANGED_ALBUM_LABELS)
                check_asf_label_frame(asf_tag, ALBUM_LABELS, album_labels_string);
            save_base_tags((TagLib::Tag *)asf_tag);
        }
    }
    return Info::save();
}




Image ** AsfInfo::get_images(int &image_count) const {
    image_count = 0;
    if(asf_tag) {
        Image** images;
        if(asf_tag->attributeListMap().contains(PICTURE_FLAG)) {
            ASF::AttributeList list = asf_tag->attributeListMap()[ PICTURE_FLAG ];
            if(0 >= list.size())
                return NULL;
            int i = 0;
            images = new Image*[list.size()];
            for(ASF::AttributeList::ConstIterator it = list.begin(); it != list.end(); it++) {
                //ASF::Attribute attr = (*it);
                ASF::Picture pict = (*it).toPicture();
                if(!pict.isValid())
                    continue;
                ByteVector pict_data = pict.picture();
                Image * image = new Image();
                if(pict_data.size() > 0) {
                    image->data_length = pict_data.size();
                    image->data = new char[image->data_length];
                    memcpy(image->data, pict_data.data(), pict_data.size());
                    
                    String mimetype = pict.mimeType();
                    if(mimetype.find("/jpeg") != -1 || mimetype.find("/jpg") != -1)
                        image->image_file_type = IMAGE_FILE_TYPE_JPEG;
                    else if(mimetype.find("/png") != -1)
                        image->image_file_type = IMAGE_FILE_TYPE_PNG;
                    else if(mimetype.find("/bmp") != -1)
                        image->image_file_type = IMAGE_FILE_TYPE_BMP;
                    else if(mimetype.find("/gif") != -1)
                        image->image_file_type = IMAGE_FILE_TYPE_GIF;
                    
                    image->image_type = get_image_type_from_pic_type(pict.type()); 
                                        
                    if(image->data && image->data_length > 0) {
                        images[i] = image;
                        image_count++;
                        i++;
                    }
                    else {
                        delete image;
                    }
                }
            }
            return images;
        }
    }
    return NULL;
}

void AsfInfo::set_images(const Image ** images, const int image_count) {
    if(asf_tag) {
        if(asf_tag->attributeListMap().contains(PICTURE_FLAG))
            asf_tag->removeItem(PICTURE_FLAG);
        
        
        for(int p = 0; p < image_count; p++) {
            const Image * image = images[p];
            if(!image->data || image->data_length == 0)
                continue;
            ASF::Picture picture = ASF::Picture();
            if(image->image_file_type == IMAGE_FILE_TYPE_JPEG || 
               image->image_file_type == IMAGE_FILE_TYPE_UNKNOWN) //default to jpeg
                picture.setMimeType("image/jpeg");
            else if(image->image_file_type == IMAGE_FILE_TYPE_PNG)
                picture.setMimeType("image/png");
            else if(image->image_file_type == IMAGE_FILE_TYPE_GIF)
                picture.setMimeType("image/gif");
            else if(image->image_file_type == IMAGE_FILE_TYPE_BMP)
                picture.setMimeType("image/bmp");
            
            ByteVector ImgData = ByteVector((TagLib::uint) image->data_length);
            memcpy(ImgData.data(), image->data, image->data_length);
            
            picture.setPicture(ImgData);
            picture.setType(image_type_to_picture_type(image->image_type));
            ASF::Attribute attr = ASF::Attribute(picture);
            asf_tag->addAttribute(PICTURE_FLAG, attr);
        }
    }
}


String AsfInfo::get_lyrics(void) const {
    if(asf_tag) {
            if(asf_tag->attributeListMap().contains(LYRICS)) {
            return asf_tag->attributeListMap()[ LYRICS ].front().toString();
        }
    }
    return String();
}


bool AsfInfo::set_lyrics(const String &lyrics) {
    if(asf_tag) {
        asf_tag->removeItem(LYRICS);
        asf_tag->setAttribute(LYRICS, lyrics);
        return true;
    }
    return false;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.