Files
ESP8266lib/data/formats/mp3/ID3.h
kazu 422610c21c many many small changes
switched to the new logging here and there
some cleanups
worked  on i2S
base class for files
id3 parsing
2021-02-28 20:44:01 +01:00

217 lines
5.2 KiB
C++

#pragma once
#include <cstdint>
#include <vector>
#include <string>
#include <algorithm>
#include <string.h>
#include "../../../Debug.h"
/**
* very basic ID3v2 reader
* https://id3.org/id3v2.3.0#ID3v2_overview
*/
class ID3v2 {
static constexpr const char* NAME = "ID3v2";
/** 4 byte size, used to correct endianness */
struct Size {
uint8_t raw[4];
operator uint32_t () const {return raw[3]<<0 | raw[2]<<8 | raw[1]<<16 | raw[0]<<24;}
};
/** ID3 header at the start of the file (if present) */
struct Header {
char ID3[3]; // "ID3" if present
uint16_t version;
uint8_t flags;
uint8_t sizeBits[4]; // size of content, 4 bytes, the MSB of every byte is 0 -> 28 bits used
uint32_t size() const {return sizeBits[0]<<21 | sizeBits[1]<<14 | sizeBits[2]<<7 | sizeBits[3]<<0;}
} __attribute__((packed));
/** header for every frame within the ID3 data */
struct Frame {
char name[4]; // tag name (e.g. TALB, APIC, ...)
Size size; // size of content
uint16_t flags;
bool operator == (const char* needle) const {return memcmp(name, needle, 4) == 0;}
bool isPadding() const {return name[0] == 0x00 && size == 0;}
} __attribute__((packed));
static constexpr const uint8_t ENC_ISO8859 = 0x00;
static constexpr const uint8_t ENC_UNICODE = 0x01;
static constexpr const uint8_t ENC_UTF8 = 0x03;
// maximum size for a frame to be read
static constexpr const uint32_t MAX_FRM_SIZE = 65536;
public:
struct Image {
bool present = false;
std::string mime;
uint8_t type;
std::string desc;
uint32_t pos; // the absolute position within the data where the image is
uint32_t size; // the length of the image in bytes
};
struct Data {
bool ok = false;
std::string artist;
std::string title;
std::string album;
std::string year;
Image img;
};
public:
ID3v2() {}
template <typename DataSource> Data readTags(DataSource& src, bool readImages) {
Data data;
// start at beginning
src.seekTo(0);
// 10 byte ID3 header, file starts with "ID3" ?
Header head;
src.readType(head);
if (memcmp(head.ID3, "ID3", 3) != 0) {return data;}
// read all tags
while(src.curPos() < head.size()) {
Frame frm;
src.readType(frm);
uint32_t startOfData = src.curPos();
if (frm.isPadding()) {
break; // only (empty) padding blocks will follow -> stop
} else if (frm == "TALB") {
data.album = readString(src, frm.size);
} else if (frm == "TPE1") {
data.artist = readString(src, frm.size);
} else if (frm == "TIT2") {
data.title = readString(src, frm.size);
} else if (frm == "TYER") {
data.year = readString(src, frm.size);
} else if (frm == "APIC" && readImages) {
data.img = parseImage(src, frm.size);
}
Log::addInfo(NAME, "%c%c%c%c:%d", frm.name[0],frm.name[1],frm.name[2],frm.name[3], (uint32_t)frm.size);
// ensure we are positioned after the frame
src.seekTo(startOfData + frm.size);
}
data.ok = true;
return data;
}
private:
/** read a string with the given length, corrects encoding */
template <typename DataSource> std::string readString(DataSource& src, uint32_t size) {
// read no more than 120 chars
uint8_t buf[128];
size = std::min((uint32_t)120, size);
src.read(size, buf);
if (buf[0] == ENC_ISO8859 || buf[0] == ENC_UTF8) {
return std::string((const char*) &buf[1]);
} else if (buf[0] == ENC_UNICODE) { // 16(!) bit unicode, starts with {0x01 0xFF 0xF4}
uint32_t pos = 0;
for (uint32_t i = 3; i < size; i += 2) {
buf[pos++] = buf[i];
}
buf[pos] = 0;
return std::string((const char*)buf);
} else {
return "ENCODING?";
}
}
/** read a string with an unknown length, zero termined, corrects encoding, also returns the number of bytes read */
template <typename DataSource> std::string readStringZeroTerm(DataSource& src, uint8_t encoding, uint32_t& bytesRead) {
std::string res;
bytesRead = 0;
if (encoding == ENC_ISO8859 || encoding == ENC_UTF8) {
char c;
while(true) {
src.readType(c);
if (c == 0) {break;}
res += c;
++bytesRead;
}
} else if (encoding == ENC_UNICODE) { // 16(!) bit unicode, starts with {0x01 0xFF 0xFE}
char c[2];
src.readType(c); // skip 0xFF 0xFE
while(true) {
src.readType(c);
if (c[0] == 0 && c[1] == 0) {break;}
res += c[0];
bytesRead += 2;
}
} else {
return "ENCODING?";
}
return res;
}
/** read X bytes into a vector */
template <typename DataSource> std::vector<uint8_t> readVector(DataSource& src, const uint32_t size) {
std::vector<uint8_t> res;
res.resize(size);
src.read(size, res.data());
return res;
}
/**
* parse an image tag
* image data is preceeded by mime type and description)
* the image is not actually read, but its offset within the data and its size are determined
*/
template <typename DataSource> Image parseImage(DataSource& src, const uint32_t size) {
uint32_t read1, read2;
Image img;
uint8_t encoding;
src.readType(encoding);
img.mime = readStringZeroTerm(src, ENC_ISO8859, read1);
src.readType(img.type);
img.desc = readStringZeroTerm(src, encoding, read2);
// determine the position and byte-size of the image
img.pos = src.curPos();
img.size = size - 1 - read1 - 1 - read2;
img.present = true;
return img;
}
};