int fread_header(Header *header, FILE *file) { int i = 0; unsigned int *namesize = malloc(sizeof(unsigned int)); unsigned short *treesize = malloc(sizeof(unsigned short)); unsigned long long *originalsize = malloc(sizeof(unsigned long long)); unsigned long long *filesize = malloc(sizeof(unsigned long long)); uint32_t *crc = malloc(sizeof(uint32_t)); char *filename; char *tree; if(fread32(namesize, file)) { return 1; } header->namesize = *namesize; filename = malloc(sizeof(char) * *namesize); fread(filename, sizeof(char), header->namesize, file); header->filename = filename; if(fread16(treesize, file)) { return 1; } header->treesize = *treesize; tree = malloc(sizeof(char) * *treesize); fread(tree, sizeof(char), header->treesize, file); header->tree = tree; if(fread32(crc, file)) { return 1; } header->crc = *crc; if(fread64(originalsize, file)) { return 1; } header->originalsize = *originalsize; if(fread64(filesize, file)) { return 1; } header->filesize = *filesize; return 0; }
bool AudioPlaySdAac::setupMp4(void) { _ATOM ftyp = findMp4Atom("ftyp",0, false); if (!ftyp.size) return false; //no mp4/m4a file //go through the boxes to find the interesting atoms: uint32_t moov = findMp4Atom("moov", 0).position; uint32_t trak = findMp4Atom("trak", moov + 8).position; uint32_t mdia = findMp4Atom("mdia", trak + 8).position; //determine duration: uint32_t mdhd = findMp4Atom("mdhd", mdia + 8).position; uint32_t timescale = fread32(mdhd + 8 + 0x0c); duration = 1000.0 * ((float)fread32(mdhd + 8 + 0x10) / (float)timescale); //MP4-data has no aac-frames, so we have to set the parameters by hand. uint32_t minf = findMp4Atom("minf", mdia + 8).position; uint32_t stbl = findMp4Atom("stbl", minf + 8).position; //stsd sample description box: - infos to parametrize the decoder _ATOM stsd = findMp4Atom("stsd", stbl + 8); if (!stsd.size) return false; //something is not ok uint16_t channels = fread16(stsd.position + 8 + 0x20); //uint16_t channels = 1; //uint16_t bits = fread16(stsd.position + 8 + 0x22); //not used uint16_t samplerate = fread32(stsd.position + 8 + 0x26); setupDecoder(channels, samplerate, AAC_PROFILE_LC); //stco - chunk offset atom: uint32_t stco = findMp4Atom("stco", stbl + 8).position; //number of chunks: uint32_t nChunks = fread32(stco + 8 + 0x04); //first entry from chunk table: firstChunk = fread32(stco + 8 + 0x08); //last entry from chunk table: lastChunk = fread32(stco + 8 + 0x04 + nChunks * 4); if (nChunks == 1) { _ATOM mdat = findMp4Atom("mdat", 0); lastChunk = mdat.size; } #if 0 Serial.print("mdhd duration="); Serial.print(duration); Serial.print(" ms, stsd: chan="); Serial.print(channels); Serial.print(" samplerate="); Serial.print(samplerate); Serial.print(" nChunks="); Serial.print(nChunks); Serial.print(" firstChunk="); Serial.println(firstChunk, HEX); Serial.print(" lastChunk="); Serial.println(lastChunk, HEX); #endif return true; }