Skip to content

Commit b00cfb6

Browse files
committed
Newer file IO
1 parent ca81cb9 commit b00cfb6

File tree

2 files changed

+67
-44
lines changed

2 files changed

+67
-44
lines changed

Example/TestCFHD.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@
4141
#define QBIST_UNIQUE 1 //slower, but each frame get unique art to encode
4242
#define OUTPUT_CFHD 0
4343
#define DO_DECODE 1
44+
45+
#define OUTPUT_3D_TYPE STEREO3D_TYPE_DEFAULT
4446
#if DO_DECODE
4547
#define DO_PSNR 1
46-
#define OUTPUT_3D_TYPE STEREO3D_TYPE_DEFAULT
4748
#if DO_PSNR
4849
#define DECODE_FORMAT pixelFormat // Current encoding format, PSNR needs match pixel formats
4950
#define PPM_EXPORT_BELOW 40
@@ -312,7 +313,8 @@ CFHD_Error DecodeFrame(void **frameDecBuffer,
312313
#endif
313314

314315
CFHD_MetadataTrack metadataTrack;
315-
metadataTrack = METADATATYPE_MODIFIED; //METADATATYPE_ORIGINAL
316+
//metadataTrack = METADATATYPE_MODIFIED; // Use any active metadta changes created in GoPro Studio or CineForm FirstLight
317+
metadataTrack = METADATATYPE_ORIGINAL; // Don't use external active metadata, only internal corrections.
316318
error = CFHD_InitSampleMetadata(metadataDecRef,
317319
metadataTrack,
318320
sampleBuffer,
@@ -409,18 +411,21 @@ CFHD_Error DecodeMOVIE(char *filename, char *ext)
409411
void *frameDecBuffer = NULL;
410412
uint32_t AVI = 0;
411413
float length;
414+
void *handle;
412415

413416
if (0 == stricmp("AVI", ext)) AVI = 1;
414417

415418
if(AVI)
416-
length = OpenAVISource(filename, AVI_TRAK_TYPE, AVI_TRAK_SUBTYPE);
419+
handle = OpenAVISource(filename, AVI_TRAK_TYPE, AVI_TRAK_SUBTYPE);
417420
else
418-
length = OpenMP4Source(filename, MOV_TRAK_TYPE, MOV_TRAK_SUBTYPE);
421+
handle = OpenMP4Source(filename, MOV_TRAK_TYPE, MOV_TRAK_SUBTYPE);
422+
423+
length = GetDuration(handle);
419424

420425
if (length > 0.0)
421426
{
422427
int frame = 0;
423-
uint32_t numframes = GetNumberPayloads();
428+
uint32_t numframes = GetNumberPayloads(handle);
424429

425430
printf("found %.2fs of video (%d frames) within %s\n", length, numframes, filename);
426431

@@ -443,8 +448,8 @@ CFHD_Error DecodeMOVIE(char *filename, char *ext)
443448
uint32_t payloadsize;
444449
CFHD_PixelFormat pixelFormat = TestDecodeOnlyPixelFormat[decmode];
445450

446-
payloadsize = GetPayloadSize(frame);
447-
payload = GetPayload(payload, frame);
451+
payloadsize = GetPayloadSize(handle, frame);
452+
payload = GetPayload(handle, payload, frame);
448453

449454
if (payload == NULL)
450455
{
@@ -502,7 +507,7 @@ CFHD_Error DecodeMOVIE(char *filename, char *ext)
502507
if (decoderRef) CFHD_CloseDecoder(decoderRef);
503508
if (metadataDecRef) CFHD_CloseMetadata(metadataDecRef);
504509

505-
CloseSource();
510+
CloseSource(handle);
506511

507512
return error;
508513
}
@@ -978,9 +983,11 @@ CFHD_Error EncodeDecodeQualityTest()
978983
if(once == 0)
979984
SDKVerion(decoderRef, sampleBuffer, (int)sampleSize), once++;
980985
printf("Resolution: %dx%d\n", FRAME_WIDTH, FRAME_HEIGHT);
986+
#ifdef DECODE_FORMAT
981987
if (pixelFormat != DECODE_FORMAT)
982988
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);
983989
else
990+
#endif
984991
printf("Pixel format: %c%c%c%c\n", (pixelFormat >> 24) & 0xff, (pixelFormat >> 16) & 0xff, (pixelFormat >> 8) & 0xff, (pixelFormat >> 0) & 0xff);
985992
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);
986993
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)
10631070

10641071
if (argc == 1)
10651072
{
1066-
//error = EncodeSpeedTest();
1067-
if(error) printf("error code: %d\n", error);
1068-
1073+
#if DO_DECODE
10691074
error = EncodeDecodeQualityTest();
1075+
#else
1076+
error = EncodeSpeedTest();
1077+
#endif
10701078
if (error) printf("error code: %d\n", error);
10711079
}
10721080
else

Example/readavi.cpp

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ int ch,n;
8585
return 0;
8686
}
8787

88-
static int parse_idx1(FILE *in, int chunk_len)
88+
static int parse_idx1(void *hdl, FILE *in, int chunk_len)
8989
{
90+
videoobject *mp4 = (videoobject *)hdl;
91+
if (mp4 == NULL) return 0;
92+
9093
struct index_entry_t index_entry;
9194
int t,framenum = 0;
9295

@@ -105,18 +108,18 @@ int t,framenum = 0;
105108

106109
if (0 == strcmp(index_entry.ckid, "00dc") || 0 == strcmp(index_entry.ckid, "00db")) //video frame compressed or baseband
107110
{
108-
if (metaoffsets)
111+
if (mp4->metaoffsets)
109112
{
110-
metaoffsets[framenum] = movi_offset + 4 + (uint64_t)index_entry.dwChunkOffset;
111-
metasizes[framenum] = (uint64_t)index_entry.dwChunkLength;
113+
mp4->metaoffsets[framenum] = movi_offset + 4 + (uint64_t)index_entry.dwChunkOffset;
114+
mp4->metasizes[framenum] = (uint64_t)index_entry.dwChunkLength;
112115
framenum++;
113116
}
114117
}
115118

116119

117-
indexcount = framenum;
118-
trak_clockcount = meta_clockcount = clockcount = indexcount * basemetadataduration;
119-
videolength = metadatalength = (float)clockcount / (float)clockdemon;
120+
mp4->indexcount = framenum;
121+
mp4->trak_clockcount = mp4->meta_clockcount = mp4->clockcount = mp4->indexcount * mp4->basemetadataduration;
122+
mp4->videolength = mp4->metadatalength = (float)mp4->clockcount / (float)mp4->clockdemon;
120123

121124

122125
#if PRINT_AVI_STRUCTURE
@@ -133,8 +136,11 @@ int t,framenum = 0;
133136
return 0;
134137
}
135138

136-
static int read_avi_header(FILE *in,struct avi_header_t *avi_header)
139+
static int read_avi_header(void *hdl, FILE *in,struct avi_header_t *avi_header)
137140
{
141+
videoobject *mp4 = (videoobject *)hdl;
142+
if (mp4 == NULL) return 0;
143+
138144
avi_header->TimeBetweenFrames=read_long(in);
139145
avi_header->MaximumDataRate=read_long(in);
140146
avi_header->PaddingGranularity=read_long(in);
@@ -169,14 +175,16 @@ static int read_avi_header(FILE *in,struct avi_header_t *avi_header)
169175
#endif
170176

171177

172-
indexcount = avi_header->TotalNumberOfFrames;
173-
basemetadataduration = avi_header->TimeBetweenFrames;
174-
trak_clockdemon = meta_clockdemon = clockdemon = 1000000;
175-
trak_clockcount = meta_clockcount = clockcount = indexcount * basemetadataduration;
176-
videolength = metadatalength = (float)clockcount / (float)clockdemon;
177178

178-
metasizes = (uint32_t *)malloc(indexcount * 4);
179-
metaoffsets = (uint64_t *)malloc(indexcount * 8);
179+
mp4->indexcount = avi_header->TotalNumberOfFrames;
180+
mp4->basemetadataduration = avi_header->TimeBetweenFrames;
181+
mp4->trak_clockdemon = mp4->meta_clockdemon = mp4->clockdemon = 1000000;
182+
mp4->trak_clockcount = mp4->meta_clockcount = mp4->clockcount = mp4->indexcount * mp4->basemetadataduration;
183+
mp4->videolength = mp4->metadatalength = (float)mp4->clockcount / (float)mp4->clockdemon;
184+
185+
mp4->metasizes = (uint32_t *)malloc(mp4->indexcount * 4);
186+
mp4->metasize_count = mp4->indexcount;
187+
mp4->metaoffsets = (uint64_t *)malloc(mp4->indexcount * 8);
180188

181189
return 0;
182190
}
@@ -435,7 +443,7 @@ int stream_type=0; // 0=video 1=sound
435443
return 0;
436444
}
437445

438-
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)
446+
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)
439447
{
440448
char chunk_id[5];
441449
int chunk_size;
@@ -456,7 +464,7 @@ long offset=ftell(in);
456464
end_of_chunk=end_of_chunk+(4-(end_of_chunk%4));
457465
}
458466

459-
read_avi_header(in,avi_header);
467+
read_avi_header(hdl, in,avi_header);
460468

461469
#if PRINT_AVI_STRUCTURE
462470
printf(" }\n");
@@ -471,16 +479,25 @@ long offset=ftell(in);
471479
return 0;
472480
}
473481

474-
static int parse_riff(FILE *in)
482+
static int parse_riff(void *hdl)
475483
{
484+
FILE *in;
476485
char chunk_id[5];
477486
int chunk_size;
478487
char chunk_type[5];
479488
int end_of_chunk,end_of_subchunk;
480489
struct avi_header_t avi_header;
481490
struct stream_header_t stream_header;
482491
struct stream_format_t stream_format={0};
483-
long offset=ftell(in);
492+
long offset;
493+
494+
videoobject *mp4 = (videoobject *)hdl;
495+
if (mp4 == NULL) return 0;
496+
497+
in = mp4->mediafp;
498+
offset= ftell(in);
499+
500+
484501

485502
read_chars(in,chunk_id,4);
486503
chunk_size=read_long(in);
@@ -546,7 +563,7 @@ long offset=ftell(in);
546563
else
547564
if (STRINGCASECOMPARE("hdrl",chunk_type)==0)
548565
{
549-
parse_hdrl(in,&avi_header,&stream_header,&stream_format, chunk_size);
566+
parse_hdrl(hdl, in,&avi_header,&stream_header,&stream_format, chunk_size);
550567
/* skip_chunk(in); */
551568
}
552569
else
@@ -567,7 +584,7 @@ long offset=ftell(in);
567584
if (STRINGCASECOMPARE("idx1",chunk_id)==0)
568585
{
569586
fseek(in,ftell(in)-4,SEEK_SET);
570-
parse_idx1(in,chunk_size);
587+
parse_idx1(hdl, in,chunk_size);
571588
}
572589
else
573590
{
@@ -596,30 +613,28 @@ long offset=ftell(in);
596613
return 0;
597614
}
598615

599-
float OpenAVISource(char *filename, uint32_t traktype, uint32_t subtype)
616+
void *OpenAVISource(char *filename, uint32_t traktype, uint32_t subtype)
600617
{
601-
metasizes = NULL;
602-
metaoffsets = NULL;
603-
indexcount = 0;
604-
videolength = 0.0;
605-
metadatalength = 0.0;
606-
basemetadataduration = 0;
607-
basemetadataoffset = 0;
618+
videoobject *mp4 = (videoobject *)malloc(sizeof(videoobject));
619+
if (mp4 == NULL) return NULL;
620+
621+
memset(mp4, 0, sizeof(videoobject));
622+
608623

609624
#ifdef _WINDOWS
610-
fopen_s(&mediafp, filename, "rb");
625+
fopen_s(&mp4->mediafp, filename, "rb");
611626
#else
612627
mediafp = fopen(filename, "rb");
613628
#endif
614629

615-
if (mediafp == 0)
630+
if (mp4->mediafp == 0)
616631
{
617632
printf("Could not open %s for input\n", filename);
618633
exit(1);
619634
}
620635

621-
parse_riff(mediafp);
636+
parse_riff((void *)mp4);
622637

623-
return videolength;
638+
return (void *)mp4;
624639
}
625640

0 commit comments

Comments
 (0)