many many small changes
switched to the new logging here and there some cleanups worked on i2S base class for files id3 parsing
This commit is contained in:
@@ -20,9 +20,9 @@ namespace AVI {
|
||||
NextChunk(NextChunkType type, uint32_t size) : type(type), size(size) {}
|
||||
};
|
||||
|
||||
template <typename Source> class Demuxer {
|
||||
template <typename DataSource> class Demuxer {
|
||||
|
||||
Source& src;
|
||||
DataSource& src;
|
||||
|
||||
bool valid = true;
|
||||
//AVITypeHeader riff;
|
||||
@@ -52,7 +52,7 @@ namespace AVI {
|
||||
|
||||
public:
|
||||
|
||||
Demuxer(Source& src) : src(src) {
|
||||
Demuxer(DataSource& src) : src(src) {
|
||||
|
||||
RIFFHeader riff;
|
||||
read(riff);
|
||||
@@ -180,8 +180,8 @@ namespace AVI {
|
||||
|
||||
|
||||
void dumpState() {
|
||||
std::cout << "video @" << state.video.streamID << ": " << state.video.fmt.bmi_header.biWidth << "x" << state.video.fmt.bmi_header.biHeight << " Format: " << state.streamHeaders[state.video.streamID].fccHandler.chars << std::endl;
|
||||
std::cout << "audio @" << state.audio.streamID << ": " << state.audio.fmt.channels << " channels @ " << state.audio.fmt.samples_per_sec << " Hz" << " Format: " << state.audio.fmt.format_tag << std::endl;
|
||||
//std::cout << "video @" << state.video.streamID << ": " << state.video.fmt.bmi_header.biWidth << "x" << state.video.fmt.bmi_header.biHeight << " Format: " << state.streamHeaders[state.video.streamID].fccHandler.chars << std::endl;
|
||||
//std::cout << "audio @" << state.audio.streamID << ": " << state.audio.fmt.channels << " channels @ " << state.audio.fmt.samples_per_sec << " Hz" << " Format: " << state.audio.fmt.format_tag << std::endl;
|
||||
}
|
||||
|
||||
AVIList readUntilList(const char* content) {
|
||||
|
||||
@@ -14,7 +14,7 @@ union FOURCC {
|
||||
bool operator == (const FOURCC other) const {return u32 == other.u32;}
|
||||
bool operator != (const FOURCC other) const {return u32 != other.u32;}
|
||||
|
||||
};
|
||||
} __attribute__((__packed__));
|
||||
|
||||
|
||||
//struct AVICommonHeader {
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "../../../Debug.h"
|
||||
|
||||
/**
|
||||
* very basic ID3v2 reader
|
||||
* https://id3.org/id3v2.3.0#ID3v2_overview
|
||||
*/
|
||||
template <typename File> class ID3v2 {
|
||||
class ID3v2 {
|
||||
|
||||
static constexpr const char* NAME = "ID3v2";
|
||||
|
||||
/** 4 byte size, used to correct endianness */
|
||||
struct Size {
|
||||
@@ -34,173 +41,176 @@ template <typename File> class ID3v2 {
|
||||
bool isPadding() const {return name[0] == 0x00 && size == 0;}
|
||||
} __attribute__((packed));
|
||||
|
||||
File& f;
|
||||
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:
|
||||
|
||||
/** ctor, define whether to read contained images */
|
||||
ID3v2(File& f, bool includeImages) : f(f) {
|
||||
read(includeImages);
|
||||
}
|
||||
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
|
||||
};
|
||||
|
||||
/** parsed data */
|
||||
struct Data {
|
||||
std::string album;
|
||||
bool ok = false;
|
||||
std::string artist;
|
||||
std::string title;
|
||||
std::string album;
|
||||
std::string year;
|
||||
std::vector<uint8_t> image;
|
||||
} data;
|
||||
Image img;
|
||||
};
|
||||
|
||||
private:
|
||||
public:
|
||||
|
||||
bool read(bool includeImages) {
|
||||
ID3v2() {}
|
||||
|
||||
// start at beginning of file
|
||||
seekTo(0);
|
||||
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;
|
||||
read(head);
|
||||
if (memcmp(head.ID3, "ID3", 3) != 0) {return false;}
|
||||
std::cout << head.size() << std::endl;
|
||||
src.readType(head);
|
||||
if (memcmp(head.ID3, "ID3", 3) != 0) {return data;}
|
||||
|
||||
// read all tags
|
||||
while(curPos() < head.size()) {
|
||||
while(src.curPos() < head.size()) {
|
||||
|
||||
Frame frm;
|
||||
read(frm);
|
||||
uint32_t startOfData = curPos();
|
||||
|
||||
std::cout << frm.name << ":" << frm.size << std::endl;
|
||||
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(frm.size);
|
||||
data.album = readString(src, frm.size);
|
||||
} else if (frm == "TPE1") {
|
||||
data.artist = readString(frm.size);
|
||||
data.artist = readString(src, frm.size);
|
||||
} else if (frm == "TIT2") {
|
||||
data.title = readString(frm.size);
|
||||
data.title = readString(src, frm.size);
|
||||
} else if (frm == "TYER") {
|
||||
data.year = readString(frm.size);
|
||||
} else if (frm == "APIC" && includeImages) {
|
||||
data.image = readImage(frm.size);
|
||||
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
|
||||
seekTo(startOfData + frm.size);
|
||||
src.seekTo(startOfData + frm.size);
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
data.ok = true;
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/** read a string with the given length, also corrects encoding */
|
||||
std::string readString(uint32_t size) {
|
||||
/** 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(120u, size);
|
||||
f.read(size, buf);
|
||||
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 */
|
||||
std::vector<uint8_t> readVector(const uint32_t size) {
|
||||
template <typename DataSource> std::vector<uint8_t> readVector(DataSource& src, const uint32_t size) {
|
||||
std::vector<uint8_t> res;
|
||||
res.resize(size);
|
||||
read(size, res.data());
|
||||
src.read(size, res.data());
|
||||
return res;
|
||||
}
|
||||
|
||||
/** read an image (is preceeded by mime type and description) */
|
||||
std::vector<uint8_t> readImage(const uint32_t size) {
|
||||
std::vector<uint8_t> tmp = readVector(size);
|
||||
/**
|
||||
* 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;
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
// class StringWrapper {
|
||||
|
||||
// const uint8_t* src;
|
||||
// const uint32_t len;
|
||||
|
||||
// public:
|
||||
|
||||
// StringWrapper(const uint8_t* src, const uint8_t len) : src(src), len(len) {}
|
||||
|
||||
// StringWrapper(const std::vector<uint8_t>& vec, uint32_t offset = 0) : src(vec.data()+offset), len(vec.size()-offset) {}
|
||||
|
||||
// std::string get(uint32_t& outPos) const {
|
||||
|
||||
// // encoding?
|
||||
// if (src[0] == 0x00) { // ISO8859-15
|
||||
// return std::string((const char*) &buf[1]);
|
||||
|
||||
// } else if (buf[0] == 0x01) { // 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];
|
||||
// ++pos;
|
||||
// }
|
||||
// buf[pos] = 0;
|
||||
// return std::string((const char*)buf);
|
||||
|
||||
// } else {
|
||||
// return "ENCODING?";
|
||||
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// /** determine the length of the string */
|
||||
// uint32_t len() const {
|
||||
|
||||
// if (src[0] == 0x00) { // ISO8859-15
|
||||
|
||||
|
||||
// } else if (buf[0] == 0x01) { // 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];
|
||||
// ++pos;
|
||||
// }
|
||||
// buf[pos] = 0;
|
||||
// return std::string((const char*)buf);
|
||||
|
||||
// } else {
|
||||
|
||||
// }
|
||||
|
||||
// };
|
||||
|
||||
/** current position within the file */
|
||||
uint32_t curPos() {
|
||||
return f.curPos();
|
||||
}
|
||||
|
||||
/** seek to a new position within the file */
|
||||
void seekTo(uint32_t pos) {
|
||||
f.seekTo(pos);
|
||||
}
|
||||
|
||||
template <typename T> void read(T& dst) {
|
||||
read(sizeof(T), (uint8_t*) &dst);
|
||||
}
|
||||
|
||||
void read(uint32_t size, uint8_t* dst) {
|
||||
f.read(size, dst);
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user