lcm_eventlog_event_t *lcm_eventlog_read_next_event(lcm_eventlog_t *l) { lcm_eventlog_event_t *le = (lcm_eventlog_event_t*) calloc(1, sizeof(lcm_eventlog_event_t)); int32_t magic = 0; int r; do { r = fgetc(l->f); if (r < 0) goto eof; magic = (magic << 8) | r; } while( magic != MAGIC ); if (0 != fread64(l->f, &le->eventnum) || 0 != fread64(l->f, &le->timestamp) || 0 != fread32(l->f, &le->channellen) || 0 != fread32(l->f, &le->datalen)) { return NULL; } // Sanity check the channel length and data length if (le->channellen < 0 || le->channellen >= 1000) { fprintf(stderr, "Log event has invalid channel length: %d\n", le->channellen); return NULL; } if (le->datalen < 0) { fprintf(stderr, "Log event has invalid data length: %d\n", le->datalen); return NULL; } le->channel = (char *) calloc(1, le->channellen+1); if (fread(le->channel, 1, le->channellen, l->f) != (size_t) le->channellen) goto eof; le->data = calloc(1, le->datalen+1); if (fread(le->data, 1, le->datalen, l->f) != (size_t) le->datalen) goto eof; // Check that there's a valid event or the EOF after this event. int32_t next_magic; if (0 == fread32(l->f, &next_magic)) { if (next_magic != MAGIC) { fprintf(stderr, "Invalid header after log data\n"); return NULL; } fseeko (l->f, -4, SEEK_CUR); } return le; eof: free(le->channel); free(le->data); free(le); return NULL; }
lcm_eventlog_event_t *lcm_eventlog_read_next_event(lcm_eventlog_t *l) { lcm_eventlog_event_t *le = (lcm_eventlog_event_t*) calloc(1, sizeof(lcm_eventlog_event_t)); int32_t magic = 0; int r; do { r = fgetc(l->f); if (r < 0) goto eof; magic = (magic << 8) | r; } while( magic != MAGIC ); fread64(l->f, &le->eventnum); fread64(l->f, &le->timestamp); fread32(l->f, &le->channellen); fread32(l->f, &le->datalen); assert (le->channellen < 1000); if (l->eventcount != le->eventnum) { // these warnings will spew unnecessarily for log files that have been // filtered through lcm-logplayer-gui since it preserves event numbers // printf ("Mismatch: eventcount %"PRId64" eventnum %"PRId64"\n", // l->eventcount, le->eventnum); // printf ("file offset %"PRId64"\n", ftello (l->f)); l->eventcount = le->eventnum; } le->channel = (char *) calloc(1, le->channellen+1); if (fread(le->channel, 1, le->channellen, l->f) != (size_t) le->channellen) goto eof; le->data = calloc(1, le->datalen+1); if (fread(le->data, 1, le->datalen, l->f) != (size_t) le->datalen) goto eof; l->eventcount++; return le; eof: return NULL; }
eventlog_event_t *eventlog_read_next_event(eventlog_t *l) { eventlog_event_t *le = (eventlog_event_t*) calloc(1, sizeof(eventlog_event_t)); int32_t magic = 0; int r; do { r = fgetc(l->f); if (r < 0) goto eof; magic = (magic << 8) | r; } while( magic != MAGIC ); fread64(l->f, &le->eventnum); fread64(l->f, &le->timestamp); fread32(l->f, &le->channellen); fread32(l->f, &le->datalen); assert (le->channellen < 1000); assert (le->datalen < 65536); if (l->eventcount != le->eventnum) { printf ("Mismatch: eventcount %"PRId64" eventnum %"PRId64"\n", l->eventcount, le->eventnum); printf ("file offset %"PRId64"\n", ftello (l->f)); l->eventcount = le->eventnum; } le->channel = calloc(1, le->channellen+1); if (fread(le->channel, 1, le->channellen, l->f) != (size_t) le->channellen) goto eof; le->data = calloc(1, le->datalen+1); if (fread(le->data, 1, le->datalen, l->f) != (size_t) le->datalen) goto eof; l->eventcount++; return le; eof: return NULL; }
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; }
/** * Read frames from a file and saves them into decompressed BMP files */ static int decompress_frames(FILE* in, const V2UDecompressParams* dp) { int result = STATUS_OK; /* Allocate enough memory for maximum supported resolution */ const V2U_UINT32 buflen = 2048*2048*3; void* compressed = malloc(buflen); void* decompressed = malloc(buflen); int framenum = 0; if (compressed && decompressed) { /* Initialize decompression library */ V2U_DECOMPRESSION_LIB_CONTEXT libctx = v2udec_init_context(); if (libctx) { /* Statistics */ int PFramesCount = 0; int KeyFramesCount = 0; V2U_UINT32 PFramesSum = 0; V2U_UINT32 KeyFramesSum = 0; V2U_UINT32 framelen = 0; while (fread32(in, &framelen)) { if (framelen <= buflen && fread(compressed, framelen, 1, in) == 1) { V2URect rect; V2U_VideoMode mode; V2U_UINT32 palette = v2udec_get_palette(libctx, compressed, framelen); V2U_TIME timestamp = v2udec_get_timestamp(libctx, compressed, framelen); if (dp->format) { palette = dp->format->value; if (!v2udec_set_palette(libctx, compressed, framelen, palette)) { printf("Incompatible decompression format\n"); result = STATUS_IOERR; break; } } if (!framenum) { V2U_UINT32 cpalette = v2udec_get_cpalette(libctx, compressed, framelen); const V2UFormat* src = format_by_value(cpalette, v2u_cformats, N(v2u_cformats)); const V2UFormat* dest = format_by_value(palette, v2u_formats, N(v2u_formats)); if (src && dest) { printf("Decompressing %s -> %s\n", src->name, dest->name); } } v2udec_get_frameres(libctx, compressed, framelen, &rect); v2udec_get_videomode(libctx, compressed, framelen, &mode); if (v2udec_decompress_frame(libctx, compressed, framelen, decompressed, buflen) > 0) { /* Save decompressed frame as a bitmap file */ FILE* out; char bmpname[1024]; printf("Frame #%04d: timestamp=%u framesize=%d width=%d height=%d (%dx%d-%f)\n", framenum, (unsigned int)timestamp, framelen, rect.width, rect.height, mode.width, mode.height, mode.vfreq/1000.0); if (dp->format) { sprintf(bmpname,"%s-%s.%04d.bmp", dp->format->name, dp->fname, framenum); } else { sprintf(bmpname,"%s.%04d.bmp", dp->fname, framenum); } out = fopen(bmpname, "wb"); if (out) { v2u_write_bmp(out, rect.width, rect.height, palette, decompressed); fclose(out); framenum++; /* Collect statistics */ if (v2udec_is_keyframe(libctx, compressed, framelen)) { KeyFramesSum += framelen; KeyFramesCount++; } else { PFramesSum += framelen; PFramesCount++; } } else { printf("Failed to create file %s\n", bmpname); result = STATUS_IOERR; break; } } else { printf("Frame decode error\n"); result = STATUS_DECERR; break; } } else { printf("File format error\n"); result = STATUS_DECERR; break; } } v2udec_deinit_context(libctx); /* Calculating average decompressed frame sizes */ if (KeyFramesCount) { printf("Average key frame size: %d bytes\n", KeyFramesSum/KeyFramesCount); } if (PFramesCount) { printf("Average inner frame size: %d bytes\n", PFramesSum/PFramesCount); } } else { printf("Failed to allocate decompression context\n"); result = STATUS_FAIL; } free(compressed); free(decompressed); } return result; }