From b00cfb6cee57fe96fcc46fde0ff9649ee97bab95 Mon Sep 17 00:00:00 2001 From: dnewman-gpsw Date: Sat, 21 Oct 2017 18:11:01 -0700 Subject: [PATCH] Newer file IO --- Example/TestCFHD.cpp | 30 ++++++++++------ Example/readavi.cpp | 81 ++++++++++++++++++++++++++------------------ 2 files changed, 67 insertions(+), 44 deletions(-) diff --git a/Example/TestCFHD.cpp b/Example/TestCFHD.cpp index 0eb8b42..608b2e1 100644 --- a/Example/TestCFHD.cpp +++ b/Example/TestCFHD.cpp @@ -41,9 +41,10 @@ #define QBIST_UNIQUE 1 //slower, but each frame get unique art to encode #define OUTPUT_CFHD 0 #define DO_DECODE 1 + +#define OUTPUT_3D_TYPE STEREO3D_TYPE_DEFAULT #if DO_DECODE #define DO_PSNR 1 - #define OUTPUT_3D_TYPE STEREO3D_TYPE_DEFAULT #if DO_PSNR #define DECODE_FORMAT pixelFormat // Current encoding format, PSNR needs match pixel formats #define PPM_EXPORT_BELOW 40 @@ -312,7 +313,8 @@ CFHD_Error DecodeFrame(void **frameDecBuffer, #endif CFHD_MetadataTrack metadataTrack; - metadataTrack = METADATATYPE_MODIFIED; //METADATATYPE_ORIGINAL + //metadataTrack = METADATATYPE_MODIFIED; // Use any active metadta changes created in GoPro Studio or CineForm FirstLight + metadataTrack = METADATATYPE_ORIGINAL; // Don't use external active metadata, only internal corrections. error = CFHD_InitSampleMetadata(metadataDecRef, metadataTrack, sampleBuffer, @@ -409,18 +411,21 @@ CFHD_Error DecodeMOVIE(char *filename, char *ext) void *frameDecBuffer = NULL; uint32_t AVI = 0; float length; + void *handle; if (0 == stricmp("AVI", ext)) AVI = 1; if(AVI) - length = OpenAVISource(filename, AVI_TRAK_TYPE, AVI_TRAK_SUBTYPE); + handle = OpenAVISource(filename, AVI_TRAK_TYPE, AVI_TRAK_SUBTYPE); else - length = OpenMP4Source(filename, MOV_TRAK_TYPE, MOV_TRAK_SUBTYPE); + handle = OpenMP4Source(filename, MOV_TRAK_TYPE, MOV_TRAK_SUBTYPE); + + length = GetDuration(handle); if (length > 0.0) { int frame = 0; - uint32_t numframes = GetNumberPayloads(); + uint32_t numframes = GetNumberPayloads(handle); printf("found %.2fs of video (%d frames) within %s\n", length, numframes, filename); @@ -443,8 +448,8 @@ CFHD_Error DecodeMOVIE(char *filename, char *ext) uint32_t payloadsize; CFHD_PixelFormat pixelFormat = TestDecodeOnlyPixelFormat[decmode]; - payloadsize = GetPayloadSize(frame); - payload = GetPayload(payload, frame); + payloadsize = GetPayloadSize(handle, frame); + payload = GetPayload(handle, payload, frame); if (payload == NULL) { @@ -502,7 +507,7 @@ CFHD_Error DecodeMOVIE(char *filename, char *ext) if (decoderRef) CFHD_CloseDecoder(decoderRef); if (metadataDecRef) CFHD_CloseMetadata(metadataDecRef); - CloseSource(); + CloseSource(handle); return error; } @@ -978,9 +983,11 @@ CFHD_Error EncodeDecodeQualityTest() if(once == 0) SDKVerion(decoderRef, sampleBuffer, (int)sampleSize), once++; printf("Resolution: %dx%d\n", FRAME_WIDTH, FRAME_HEIGHT); +#ifdef DECODE_FORMAT if (pixelFormat != DECODE_FORMAT) printf("Pixel format: %c%c%c%c <--> %c%c%c%c\n", (pixelFormat >> 24) & 0xff, (pixelFormat >> 16) & 0xff, (pixelFormat >> 8) & 0xff, (pixelFormat >> 0) & 0xff, (DECODE_FORMAT >> 24) & 0xff, (DECODE_FORMAT >> 16) & 0xff, (DECODE_FORMAT >> 8) & 0xff, (DECODE_FORMAT >> 0) & 0xff); else +#endif printf("Pixel format: %c%c%c%c\n", (pixelFormat >> 24) & 0xff, (pixelFormat >> 16) & 0xff, (pixelFormat >> 8) & 0xff, (pixelFormat >> 0) & 0xff); printf("Encode: %d\n", encodedFormat == CFHD_ENCODED_FORMAT_YUV_422 ? 422 : encodedFormat == CFHD_ENCODED_FORMAT_RGB_444 ? 444 : encodedFormat == CFHD_ENCODED_FORMAT_RGBA_4444 ? 4444 : 0); printf("Decode: %s\n", decode_res == 1 ? "Full res" : decode_res == 2 ? "Half res" : decode_res == 3 ? "Quarter res" : "none"); @@ -1063,10 +1070,11 @@ int main(int argc, char **argv) if (argc == 1) { - //error = EncodeSpeedTest(); - if(error) printf("error code: %d\n", error); - +#if DO_DECODE error = EncodeDecodeQualityTest(); +#else + error = EncodeSpeedTest(); +#endif if (error) printf("error code: %d\n", error); } else diff --git a/Example/readavi.cpp b/Example/readavi.cpp index c33d492..7aa13f4 100644 --- a/Example/readavi.cpp +++ b/Example/readavi.cpp @@ -85,8 +85,11 @@ int ch,n; return 0; } -static int parse_idx1(FILE *in, int chunk_len) +static int parse_idx1(void *hdl, FILE *in, int chunk_len) { + videoobject *mp4 = (videoobject *)hdl; + if (mp4 == NULL) return 0; + struct index_entry_t index_entry; int t,framenum = 0; @@ -105,18 +108,18 @@ int t,framenum = 0; if (0 == strcmp(index_entry.ckid, "00dc") || 0 == strcmp(index_entry.ckid, "00db")) //video frame compressed or baseband { - if (metaoffsets) + if (mp4->metaoffsets) { - metaoffsets[framenum] = movi_offset + 4 + (uint64_t)index_entry.dwChunkOffset; - metasizes[framenum] = (uint64_t)index_entry.dwChunkLength; + mp4->metaoffsets[framenum] = movi_offset + 4 + (uint64_t)index_entry.dwChunkOffset; + mp4->metasizes[framenum] = (uint64_t)index_entry.dwChunkLength; framenum++; } } - indexcount = framenum; - trak_clockcount = meta_clockcount = clockcount = indexcount * basemetadataduration; - videolength = metadatalength = (float)clockcount / (float)clockdemon; + mp4->indexcount = framenum; + mp4->trak_clockcount = mp4->meta_clockcount = mp4->clockcount = mp4->indexcount * mp4->basemetadataduration; + mp4->videolength = mp4->metadatalength = (float)mp4->clockcount / (float)mp4->clockdemon; #if PRINT_AVI_STRUCTURE @@ -133,8 +136,11 @@ int t,framenum = 0; return 0; } -static int read_avi_header(FILE *in,struct avi_header_t *avi_header) +static int read_avi_header(void *hdl, FILE *in,struct avi_header_t *avi_header) { + videoobject *mp4 = (videoobject *)hdl; + if (mp4 == NULL) return 0; + avi_header->TimeBetweenFrames=read_long(in); avi_header->MaximumDataRate=read_long(in); avi_header->PaddingGranularity=read_long(in); @@ -169,14 +175,16 @@ static int read_avi_header(FILE *in,struct avi_header_t *avi_header) #endif - indexcount = avi_header->TotalNumberOfFrames; - basemetadataduration = avi_header->TimeBetweenFrames; - trak_clockdemon = meta_clockdemon = clockdemon = 1000000; - trak_clockcount = meta_clockcount = clockcount = indexcount * basemetadataduration; - videolength = metadatalength = (float)clockcount / (float)clockdemon; - metasizes = (uint32_t *)malloc(indexcount * 4); - metaoffsets = (uint64_t *)malloc(indexcount * 8); + mp4->indexcount = avi_header->TotalNumberOfFrames; + mp4->basemetadataduration = avi_header->TimeBetweenFrames; + mp4->trak_clockdemon = mp4->meta_clockdemon = mp4->clockdemon = 1000000; + mp4->trak_clockcount = mp4->meta_clockcount = mp4->clockcount = mp4->indexcount * mp4->basemetadataduration; + mp4->videolength = mp4->metadatalength = (float)mp4->clockcount / (float)mp4->clockdemon; + + mp4->metasizes = (uint32_t *)malloc(mp4->indexcount * 4); + mp4->metasize_count = mp4->indexcount; + mp4->metaoffsets = (uint64_t *)malloc(mp4->indexcount * 8); return 0; } @@ -435,7 +443,7 @@ int stream_type=0; // 0=video 1=sound return 0; } -static int parse_hdrl(FILE *in,struct avi_header_t *avi_header, struct stream_header_t *stream_header, struct stream_format_t *stream_format, unsigned int size) +static int parse_hdrl(void *hdl, FILE *in,struct avi_header_t *avi_header, struct stream_header_t *stream_header, struct stream_format_t *stream_format, unsigned int size) { char chunk_id[5]; int chunk_size; @@ -456,7 +464,7 @@ long offset=ftell(in); end_of_chunk=end_of_chunk+(4-(end_of_chunk%4)); } - read_avi_header(in,avi_header); + read_avi_header(hdl, in,avi_header); #if PRINT_AVI_STRUCTURE printf(" }\n"); @@ -471,8 +479,9 @@ long offset=ftell(in); return 0; } -static int parse_riff(FILE *in) +static int parse_riff(void *hdl) { +FILE *in; char chunk_id[5]; int chunk_size; char chunk_type[5]; @@ -480,7 +489,15 @@ int end_of_chunk,end_of_subchunk; struct avi_header_t avi_header; struct stream_header_t stream_header; struct stream_format_t stream_format={0}; -long offset=ftell(in); +long offset; + +videoobject *mp4 = (videoobject *)hdl; +if (mp4 == NULL) return 0; + +in = mp4->mediafp; +offset= ftell(in); + + read_chars(in,chunk_id,4); chunk_size=read_long(in); @@ -546,7 +563,7 @@ long offset=ftell(in); else if (STRINGCASECOMPARE("hdrl",chunk_type)==0) { - parse_hdrl(in,&avi_header,&stream_header,&stream_format, chunk_size); + parse_hdrl(hdl, in,&avi_header,&stream_header,&stream_format, chunk_size); /* skip_chunk(in); */ } else @@ -567,7 +584,7 @@ long offset=ftell(in); if (STRINGCASECOMPARE("idx1",chunk_id)==0) { fseek(in,ftell(in)-4,SEEK_SET); - parse_idx1(in,chunk_size); + parse_idx1(hdl, in,chunk_size); } else { @@ -596,30 +613,28 @@ long offset=ftell(in); return 0; } -float OpenAVISource(char *filename, uint32_t traktype, uint32_t subtype) +void *OpenAVISource(char *filename, uint32_t traktype, uint32_t subtype) { - metasizes = NULL; - metaoffsets = NULL; - indexcount = 0; - videolength = 0.0; - metadatalength = 0.0; - basemetadataduration = 0; - basemetadataoffset = 0; + videoobject *mp4 = (videoobject *)malloc(sizeof(videoobject)); + if (mp4 == NULL) return NULL; + + memset(mp4, 0, sizeof(videoobject)); + #ifdef _WINDOWS - fopen_s(&mediafp, filename, "rb"); + fopen_s(&mp4->mediafp, filename, "rb"); #else mediafp = fopen(filename, "rb"); #endif - if (mediafp == 0) + if (mp4->mediafp == 0) { printf("Could not open %s for input\n", filename); exit(1); } - parse_riff(mediafp); + parse_riff((void *)mp4); - return videolength; + return (void *)mp4; }