mp3: Fix some cover arts not being loaded

This commit is contained in:
Joel16 2021-12-20 01:07:35 -05:00
parent 274f6e0d55
commit 00c847e0bc

View File

@ -11,74 +11,84 @@ namespace MP3 {
static u64 frames_read = 0, total_samples = 0; static u64 frames_read = 0, total_samples = 0;
static int channels = 0; static int channels = 0;
static long sample_rate = 0; static long sample_rate = 0;
static inline void ReadID3v1Field(const char* field, size_t length, std::string& s) { /* Helper for v1 printing, get these strings their zero byte. */
char buf[length + 1]; void SafePrint(std::string &data, char *value, size_t size) {
char safe[31];
// copy the data into a temporary buffer to add a zero byte at the end of the text if (size > 30)
std::memcpy(buf, field, length); return;
buf[length] = 0;
s = buf;
}
static void ProcessID3v1(AudioMetadata& info, const mpg123_id3v1& id3) {
std::string temp;
if (info.title.empty()) {
MP3::ReadID3v1Field(id3.title, 30, temp);
info.title = temp;
}
if (info.album.empty()) {
MP3::ReadID3v1Field(id3.album, 30, temp);
info.album = temp;
}
if (info.artist.empty()) {
MP3::ReadID3v1Field(id3.artist, 30, temp);
info.artist = temp;
}
if (info.year.empty()) {
MP3::ReadID3v1Field(id3.year, 30, temp);
info.year = temp;
}
}
void ProcessID3v2(AudioMetadata& info, const mpg123_id3v2& id3) {
if (id3.title && id3.title->p)
info.title = id3.title->p;
if (id3.album && id3.album->p)
info.album = id3.album->p;
if (id3.artist && id3.artist->p)
info.artist = id3.artist->p;
if (id3.genre && id3.genre->p)
info.genre = id3.genre->p;
if (id3.year && id3.year->p)
info.year = id3.year->p;
size_t i = 0;
for (i = 0; i < id3.pictures; ++i) {
const mpg123_picture& pic = id3.picture[i];
printf("\ncount: %d, pic.type: %d, pic.mime_type.p: %s\n", i, pic.type, pic.mime_type.p);
if ((pic.type == mpg123_id3_pic_front_cover) || (pic.type == mpg123_id3_pic_back_cover)) { std::memcpy(safe, value, size);
if ((!strcasecmp(pic.mime_type.p, "image/jpg")) || (!strcasecmp(pic.mime_type.p, "image/jpeg"))) { safe[size] = 0;
metadata.cover_image = Textures::LoadImageBufferJPEG(pic.data, pic.size); data = std::string(safe);
break; }
}
else if (!strcasecmp(pic.mime_type.p, "image/png")) { /* Print out ID3v1 info. */
metadata.cover_image = Textures::LoadImageBufferPNG(pic.data, pic.size); void ProcessID3v1(AudioMetadata &tag, mpg123_id3v1 *v1) {
break; MP3::SafePrint(tag.title, v1->title, sizeof(v1->title));
MP3::SafePrint(tag.artist, v1->artist, sizeof(v1->artist));
MP3::SafePrint(tag.album, v1->album, sizeof(v1->album));
MP3::SafePrint(tag.year, v1->year, sizeof(v1->year));
MP3::SafePrint(tag.comment, v1->comment, sizeof(v1->comment));
}
/* Split up a number of lines separated by \n, \r, both or just zero byte
and print out each line. */
void PrintLines(std::string &data, mpg123_string *inlines) {
int hadcr = 0, hadlf = 0;
char *lines = nullptr;
char *line = nullptr;
size_t len = 0;
if (inlines != nullptr && inlines->fill) {
lines = inlines->p;
len = inlines->fill;
}
else
return;
line = lines;
for(size_t i = 0; i < len; ++i) {
if (lines[i] == '\n' || lines[i] == '\r' || lines[i] == 0) {
char save = lines[i]; /* saving, changing, restoring a byte in the data */
if (save == '\n')
++hadlf;
if (save == '\r')
++hadcr;
if ((hadcr || hadlf) && hadlf % 2 == 0 && hadcr % 2 == 0)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
line = "";
#pragma GCC diagnostic pop
if (line) {
lines[i] = 0;
data = line;
line = nullptr;
lines[i] = save;
} }
} }
else {
hadlf = hadcr = 0;
if (line == nullptr)
line = lines + i;
}
} }
} }
/* Print out the named ID3v2 fields. */
void ProcessID3v2(AudioMetadata &tag, mpg123_id3v2 *v2) {
MP3::PrintLines(tag.title, v2->title);
MP3::PrintLines(tag.artist, v2->artist);
MP3::PrintLines(tag.album, v2->album);
MP3::PrintLines(tag.year, v2->year);
MP3::PrintLines(tag.comment, v2->comment);
MP3::PrintLines(tag.genre, v2->genre);
}
int Init(const std::string &path) { int Init(const std::string &path) {
int error = 0; int error = 0;
@ -116,9 +126,30 @@ namespace MP3 {
mpg123_id3v2 *id3v2; mpg123_id3v2 *id3v2;
if (metadata.has_meta & MPG123_ID3 && mpg123_id3(mp3, &id3v1, &id3v2) == MPG123_OK) { if (metadata.has_meta & MPG123_ID3 && mpg123_id3(mp3, &id3v1, &id3v2) == MPG123_OK) {
if (id3v1) if (id3v1)
MP3::ProcessID3v1(metadata, *id3v1); MP3::ProcessID3v1(metadata, id3v1);
if (id3v2) if (id3v2)
MP3::ProcessID3v2(metadata, *id3v2); MP3::ProcessID3v2(metadata, id3v2);
for (size_t i = 0; i < id3v2->pictures; ++i) {
mpg123_picture *pic = &id3v2->picture[i];
// Front cover or other
if ((pic->type == 3)) {
if ((!strcasecmp(pic->mime_type.p, "image/jpeg")) || (!strcasecmp(pic->mime_type.p, "image/jpg"))) {
metadata.cover_image = Textures::LoadImageBufferJPEG(pic->data, pic->size);
break;
}
else if (!strcasecmp(pic->mime_type.p, "image/png")) {
metadata.cover_image = Textures::LoadImageBufferPNG(pic->data, pic->size);
// I have trust issues
if (!metadata.cover_image)
metadata.cover_image = Textures::LoadImageBufferJPEG(pic->data, pic->size);
break;
}
}
}
} }
mpg123_getformat(mp3, &sample_rate, &channels, nullptr); mpg123_getformat(mp3, &sample_rate, &channels, nullptr);