Files
ESP8266lib/ext/sd/main.cpp
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

300 lines
7.5 KiB
C++

#include <stdexcept>
#include <iostream>
//#define debugMod(module, str) {printf("i[%-10s] ", module); printf(str); printf("\n");}
//#define debugMod1(module, str, v1) {printf("i[%-10s] ", module); printf(str, v1); printf("\n");}
//#define debugMod2(module, str, v1, v2) {printf("i[%-10s] ", module); printf(str, v1, v2); printf("\n");}
//#define debugMod3(module, str, v1, v2, v3) {printf("i[%-10s] ", module); printf(str, v1, v2, v3); printf("\n");}
//#define debugMod4(module, str, v1, v2, v3, v4) {printf("i[%-10s] ", module); printf(str, v1, v2, v3, v4); printf("\n");}
//#define debugMod5(module, str, v1, v2, v3, v4, v5) {printf("i[%-10s] ", module); printf(str, v1, v2, v3, v4, v5); printf("\n");}
#define PLATFORM DESKTOP
#include "../../Debug.h"
#include "MBR.h"
#include "fat32/FS.h"
#include "AccessHelper.h"
#include "../../data/formats/DataSourcePosixFile.h"
#include <fstream>
#include <fmt/format.h>
//class Simu {
// FILE* f;
//public:
// Simu(const char* image) {
// f = fopen(image, "rw");
// if (!f) {throw std::runtime_error("failed to open");}
// }
// uint32_t readSingleBlock(LBA512 addr, uint8_t* dst) {
// Log::addInfo("SD", "read512(%d*512)", addr);
// fseek(f, addr*512, SEEK_SET);
// return fread(dst, 512, 1, f) * 512;
// }
// uint32_t writeSingleBlock(LBA512 addr, const uint8_t* src) {
// Log::addInfo("SD", "write512(%d*512)", addr);
// fseek(f, addr*512, SEEK_SET);
// return fwrite(src, 512, 1, f) * 512;
// }
//};
class SimuBlockDev {
uint8_t* data;
public:
SimuBlockDev(const char* image) {
FILE* f = fopen(image, "rw");
if (!f) {throw std::runtime_error("failed to open");}
fseek(f, 0L, SEEK_END);
size_t size = ftell(f);
fseek(f, 0L, SEEK_SET);
data = (uint8_t*) malloc(size);
fread(data, size, 1, f);
fclose(f);
}
uint32_t readBlock(LBA512 addr, uint8_t* dst) {
Log::addInfo("SD", "read512(%d*512)", addr);
memcpy(dst, data+addr*512, 512);
return 512;
}
uint32_t writeBlock(LBA512 addr, const uint8_t* src) {
Log::addInfo("SD", "write512(%d*512)", addr);
memcpy(data+addr*512, src, 512);
return 512;
}
};
#include <gtest/gtest.h>
//#define logInfo(fmt, ...) std::string s = fmt::format(FMT_STRING(fmt), __VA_ARGS__);
#include "/apps/ESP8266lib/data/formats/avi/Demuxer.h"
#define __LINUX__
#include "/apps/ESP8266lib/data/formats/jpeg/JPEGDEC.h"
#include "/apps/ESP8266lib/data/formats/jpeg/JPEGDEC.cpp"
//#include "/apps/ESP8266lib/data/formats/jpeg/jpeg.c"
#define MINIMP3_ONLY_MP3
#define MINIMP3_NO_SIMD
#define MINIMP3_IMPLEMENTATION
#include "/apps/ESP8266lib/data/formats/mp3/minimp3.h"
#include "/apps/ESP8266lib/data/formats/mp3/ID3.h"
int drawJPEG(JPEGDRAW* pDraw) {
return 1;
}
ID3v2::Image mp3Image;
DataSourcePosixFile mp3DS;
int main(int argc, char** argv) {
if (1==1) {
//SimuFile simu("/mnt/ssss/mp3s/TronLegacy/Tron Legacy Remixe/Daft Punk - The Son Of Flynn (Daniel En Test Remix).mp3");
//DataSourcePosixFile f("/mnt/ssss/mp3s/grooveshark/2 Sonic feat. Destiny - Straight To The Light (Bass Up Remix).mp3");
//DataSourcePosixFile f("/mnt/ssss/mp3s/MP3s/Scooter/Scooter - How Much Is The Fish.mp3");
DataSourcePosixFile f("/mnt/ssss/mp3s/newretrowave/Daniel Deluxe - Soul Siphon.mp3");
//DataSourcePosixFile f("/mnt/ssss/mp3s/F-777/Kr1z & F-777/ReMotion Vol.2/Kr1z & F-777 - Frozen Words (Kr1z Remix).mp3");
ID3v2 id3;
ID3v2::Data data = id3.readTags(f, true);
if (data.img.present) {
mp3DS = f;
mp3Image = data.img;
JPEGDEC jpeg;
auto open = [] (const char* filename, int32_t* size) -> void* {
*size = mp3Image.size;
mp3DS.seekTo(mp3Image.pos);
return (void*)1;
};
auto close = [] (void*) {
return;
};
auto read = [] (JPEGFILE* pFile, uint8_t* pBuf, int32_t iLen) -> int32_t {
const uint32_t got = mp3DS.read(iLen, pBuf);
return got;
};
auto seek = [] (JPEGFILE *pFile, int32_t iPosition) {
mp3DS.seekTo(iPosition + mp3Image.pos);
return 1;
};
auto draw = [] (JPEGDRAW *pDraw) {
std::cout << pDraw->x << ":" << pDraw->y << " " << pDraw->iWidth << ":" << pDraw->iHeight << std::endl;
return 1;
};
if (jpeg.open("img", open, close, read, seek, draw)) {
jpeg.setPixelType(RGB565_BIG_ENDIAN);
jpeg.decode(0, 0, 0);//40, 100, JPEG_SCALE_QUARTER | JPEG_EXIF_THUMBNAIL);
jpeg.close();
}
printf("xx");
}
}
if (0==1) {
// ffmpeg -i '/mnt/ssss/anime/Toaru Majutsu no Index/[Eclipse] Toaru Majutsu no Index - 07 (1280x720 h264) [0255D83A].mkv' -r 15 -ss 30 -t 30 -vf scale=480x320 -c:v mjpeg -c:a mp3 -ar 44100 /tmp/ram/1.avi
DataSourcePosixFile f("/tmp/ram/1.avi");
AVI::Demuxer demux(f);
bool valid = demux.isValid();
std::cout << "valid: " << valid << std::endl;
uint8_t buf[64*1024];
std::ofstream outV("/tmp/ram/1.mjpeg");
std::ofstream outA1("/tmp/ram/1.mp3");
std::ofstream outA2("/tmp/ram/1.pcm");
JPEGDEC jpeg;
mp3dec_t mp3d;
mp3dec_frame_info_t info;
info.frame_bytes = 0;
mp3dec_init(&mp3d);
short pcm[MINIMP3_MAX_SAMPLES_PER_FRAME];
// ffplay /tmp/ram/1.mp3
// ffplay -autoexit -f s16le /tmp/ram/1.pcm
for (int i = 0; i < 1024; ++i) {
AVI::NextChunk nc = demux.getNextChunk();
std::cout << (int)nc.type << " : " << nc.size << std::endl;
demux.read(nc, buf);
if (nc.type == AVI::NextChunkType::VIDEO) {
//outV.write((const char*)buf, nc.size);
int res = jpeg.openRAM(buf, nc.size, drawJPEG);
std::cout << "jpeg: " << res << std::endl;
if (res) {
jpeg.setPixelType(RGB565_BIG_ENDIAN);
jpeg.decode(0, 0, 0);//40, 100, JPEG_SCALE_QUARTER | JPEG_EXIF_THUMBNAIL);
jpeg.close();
}
} else if (nc.type == AVI::NextChunkType::AUDIO) {
outA1.write((const char*)buf, nc.size);
uint32_t samples = mp3dec_decode_frame(&mp3d, buf, nc.size, pcm, &info);
std::cout << nc.size << " : " << info.frame_bytes << std::endl;
outA2.write((const char*)pcm, samples*2*sizeof(int16_t));
}
}
return 0;
}
//logInfo("{:s}", "I am not a number")
//std::string s = fmt::format(FMT_STRING("{:s}"), "I am not a number");
//std::string s = fmt::format(FMT_STRING("{:s}"), "I am not a number");
::testing::InitGoogleTest(&argc, argv);
::testing::GTEST_FLAG(filter) = "*TestSeek*";
return RUN_ALL_TESTS();
// diff /tmp/ram/TETRIS.GB /apps/workspace/gbemu/tests/tetris.gb
SimuBlockDev simu("/tmp/ram/1.dat");
AccessHelper<SimuBlockDev> ah(simu);
MBR<AccessHelper<SimuBlockDev>> mbr(ah);
if (mbr.isPresent() && mbr.isValid()) {
for (int i = 0; i < 4; ++i) {
std::cout << (int) mbr.getPartition(i).getType() << " - " << mbr.getPartition(i).getFirstSector() << " - " << mbr.getPartition(i).getNumSectors() << std::endl;
}
using FAT32FS = FAT32::FS<AccessHelper<SimuBlockDev>>;
FAT32FS fat(ah, mbr.getPartition(0).getFirstSector() * 512);
FAT32FS::DirIterator dir = fat.getRoot();
while(false) {
FAT32::DirHandle h = dir.nextUsable();
if (!h.isValid()) {break;}
if (1==0) {
FAT32FS::File f = fat.open(h);
if (1==0) {
uint8_t* bufff = (uint8_t*) malloc(1024*1024);
uint32_t read = f.read(f.size(), bufff);
std::string name = f.getName();
std::ofstream out("/tmp/ram/" + name);
out.write((char*)bufff, read);
out.close();
free(bufff);
break;
}
}
}
// //FAT32::DirEntryAt root = fat.getRoot().cur();
// FAT32FS::File file = fat.getOrCreateFile("tmp1.txt");
// uint8_t src[128];
// file.write(128, src);
// diff /tmp/ram/TETRIS.GB /apps/workspace/gbemu/tests/tetris.gb
// diff /tmp/ram/KIRBY1.GB /apps/workspace/gbemu/tests/Kirby\'s\ Dream\ Land\ \(USA\,\ Europe\).gb
}
}