AVI file load efficiency fix. Read entire chunks at a time to increase disk read performance.

This commit is contained in:
mjbudd77 2021-10-03 11:19:21 -04:00
parent 674e3dc8f3
commit edebc11048
2 changed files with 55 additions and 24 deletions

View File

@ -84,6 +84,8 @@ gwavi_t::gwavi_t(void)
avi_std = 2; avi_std = 2;
audioEnabled = false; audioEnabled = false;
riffWalkCallback = NULL; riffWalkCallback = NULL;
readBuf = NULL;
readBufSize = 0;
} }
gwavi_t::~gwavi_t(void) gwavi_t::~gwavi_t(void)
@ -96,7 +98,11 @@ gwavi_t::~gwavi_t(void)
{ {
fclose(out); out = NULL; fclose(out); out = NULL;
} }
if ( readBuf )
{
free(readBuf); readBuf = NULL;
readBufSize = 0;
}
} }
int int
@ -667,6 +673,12 @@ int gwavi_t::riffwalk(void)
{ {
return -1; return -1;
} }
if ( readBuf == NULL )
{
readBufSize = 64 * 1024;
readBuf = (unsigned char*)malloc( readBufSize );
}
fpos = ftell(in); fpos = ftell(in);
if (read_chars_bin(in, fourcc, 4) == -1) if (read_chars_bin(in, fourcc, 4) == -1)
@ -851,7 +863,7 @@ unsigned int gwavi_t::readList(int lvl)
unsigned int gwavi_t::readChunk(const char *id, int lvl) unsigned int gwavi_t::readChunk(const char *id, int lvl)
{ {
unsigned int r, size, chunkSize, bytesRead=0; unsigned int r, ret, size, chunkSize, bytesRead=0;
unsigned short dataWord; unsigned short dataWord;
char indent[256]; char indent[256];
long long int fpos; long long int fpos;
@ -889,6 +901,23 @@ unsigned int gwavi_t::readChunk(const char *id, int lvl)
size += r; size += r;
} }
while ( size > 0 )
{
if ( size > readBufSize )
{
ret = fread( readBuf, 1, readBufSize, in );
}
else
{
ret = fread( readBuf, 1, size, in );
}
if ( ret == 0 )
{
return 0;
}
size -= ret;
bytesRead += ret;
}
//if ( strcmp( id, "avih") == 0 ) //if ( strcmp( id, "avih") == 0 )
//{ //{
// ret = readAviHeader(); // ret = readAviHeader();
@ -923,30 +952,30 @@ unsigned int gwavi_t::readChunk(const char *id, int lvl)
// bytesRead += ret; // bytesRead += ret;
//} //}
while ( size >= WORD_SIZE ) //while ( size >= WORD_SIZE )
{ //{
if (read_ushort(in, dataWord) == -1) // if (read_ushort(in, dataWord) == -1)
{ // {
(void)fprintf(stderr, "readChunk: read_int() failed\n"); // (void)fprintf(stderr, "readChunk: read_int() failed\n");
return 0; // return 0;
} // }
size -= WORD_SIZE; // size -= WORD_SIZE;
bytesRead += WORD_SIZE; // bytesRead += WORD_SIZE;
} //}
if ( size > 0 ) //if ( size > 0 )
{ //{
char pad[4]; // char pad[4];
int r = size % WORD_SIZE; // int r = size % WORD_SIZE;
if (read_chars_bin(in, pad, r) == -1) // if (read_chars_bin(in, pad, r) == -1)
{ // {
(void)fprintf(stderr, "readChunk: read_int() failed\n"); // (void)fprintf(stderr, "readChunk: read_int() failed\n");
return 0; // return 0;
} // }
size -= r; // size -= r;
bytesRead += r; // bytesRead += r;
} //}
//printf("%sChunk End: %s %u\n", indent, id, bytesRead); //printf("%sChunk End: %s %u\n", indent, id, bytesRead);

View File

@ -252,6 +252,8 @@ class gwavi_t
int avi_std; int avi_std;
char fourcc[8]; char fourcc[8];
char audioEnabled; char audioEnabled;
unsigned char *readBuf;
size_t readBufSize;
// helper functions // helper functions
long long ftell(FILE *fp); long long ftell(FILE *fp);