Replaced realloc calls with a std::vector for efficiency.
This commit is contained in:
parent
f47665609b
commit
f10d50fe6b
|
@ -460,15 +460,17 @@ int gwavi_t::peak_chunk( FILE *fp, long int idx, char *fourcc, unsigned int *siz
|
|||
}
|
||||
|
||||
int
|
||||
gwavi_t::write_index(FILE *fp, int count, unsigned int *offsets)
|
||||
gwavi_t::write_index(FILE *fp)
|
||||
{
|
||||
long marker, t;
|
||||
unsigned int offset = 4;
|
||||
unsigned int r;
|
||||
char fourcc[8];
|
||||
|
||||
if (offsets == 0)
|
||||
if (offsets.size() == 0 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (write_chars_bin(fp, "idx1", 4) == -1) {
|
||||
(void)fprintf(stderr, "write_index: write_chars_bin) failed\n");
|
||||
|
@ -481,11 +483,11 @@ gwavi_t::write_index(FILE *fp, int count, unsigned int *offsets)
|
|||
if (write_int(fp, 0) == -1)
|
||||
goto write_int_failed;
|
||||
|
||||
for (t = 0; t < count; t++)
|
||||
for (size_t i = 0; i < offsets.size(); i++)
|
||||
{
|
||||
peak_chunk( fp, offset, fourcc, &r );
|
||||
//peak_chunk( fp, offset, fourcc, &r );
|
||||
|
||||
if ((offsets[t] & 0x80000000) == 0)
|
||||
if ((offsets[i] & 0x80000000) == 0)
|
||||
{
|
||||
write_chars(fp, "00dc");
|
||||
//printf("Index: %u \n", offset );
|
||||
|
@ -493,23 +495,23 @@ gwavi_t::write_index(FILE *fp, int count, unsigned int *offsets)
|
|||
else
|
||||
{
|
||||
write_chars(fp, "01wb");
|
||||
offsets[t] &= 0x7fffffff;
|
||||
offsets[i] &= 0x7fffffff;
|
||||
}
|
||||
if (write_int(fp, 0x10) == -1)
|
||||
goto write_int_failed;
|
||||
if (write_int(fp, offset) == -1)
|
||||
goto write_int_failed;
|
||||
if (write_int(fp, offsets[t]) == -1)
|
||||
if (write_int(fp, offsets[i]) == -1)
|
||||
goto write_int_failed;
|
||||
|
||||
r = offsets[t] % WORD_SIZE;
|
||||
r = offsets[i] % WORD_SIZE;
|
||||
|
||||
if ( r > 0 )
|
||||
{
|
||||
r = WORD_SIZE - r;
|
||||
}
|
||||
|
||||
offset = offset + offsets[t] + 8 + r;
|
||||
offset = offset + offsets[i] + 8 + r;
|
||||
}
|
||||
|
||||
if ((t = ftell(fp)) == -1) {
|
||||
|
|
|
@ -76,12 +76,8 @@ gwavi_t::gwavi_t(void)
|
|||
memset( fourcc, 0, sizeof(fourcc) );
|
||||
marker = 0;
|
||||
movi_fpos = 0;
|
||||
offsets_ptr = 0;
|
||||
offsets_len = 0;
|
||||
offsets_start = 0;
|
||||
offsets = 0;
|
||||
offset_count = 0;
|
||||
bits_per_pixel = 24;
|
||||
|
||||
}
|
||||
|
||||
gwavi_t::~gwavi_t(void)
|
||||
|
@ -262,16 +258,8 @@ gwavi_t::open(const char *filename, unsigned int width, unsigned int height,
|
|||
if (write_chars_bin(out, "movi", 4) == -1)
|
||||
goto write_chars_bin_failed;
|
||||
|
||||
offsets_len = 1024;
|
||||
if ((offsets = (unsigned int *)malloc((size_t)offsets_len *
|
||||
sizeof(unsigned int)))
|
||||
== NULL) {
|
||||
(void)fprintf(stderr, "gwavi_info: could not allocate memory "
|
||||
"for gwavi offsets table\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
offsets_ptr = 0;
|
||||
// Reserve space for about 4 hours of offsets
|
||||
offsets.reserve( 2 * 4 * 3600 );
|
||||
|
||||
return 0;
|
||||
|
||||
|
@ -307,7 +295,6 @@ gwavi_t::add_frame( unsigned char *buffer, size_t len)
|
|||
// (int)len);
|
||||
//}
|
||||
|
||||
offset_count++;
|
||||
stream_header_v.data_length++;
|
||||
|
||||
maxi_pad = len % WORD_SIZE;
|
||||
|
@ -316,27 +303,9 @@ gwavi_t::add_frame( unsigned char *buffer, size_t len)
|
|||
maxi_pad = WORD_SIZE - maxi_pad;
|
||||
}
|
||||
|
||||
if (offset_count >= offsets_len)
|
||||
{
|
||||
void *tmpPtr;
|
||||
offsets_len += 1024;
|
||||
tmpPtr = realloc( offsets,
|
||||
(size_t)offsets_len *
|
||||
sizeof(unsigned int));
|
||||
|
||||
if ( tmpPtr )
|
||||
{
|
||||
offsets = (unsigned int *)tmpPtr;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "gwavi_add_frame: realloc() failed\n");
|
||||
}
|
||||
}
|
||||
|
||||
//printf("Frame Offset: %li\n", ftell(out) - movi_fpos );
|
||||
|
||||
offsets[offsets_ptr++] = (unsigned int)(len);
|
||||
offsets.push_back( (unsigned int)(len) );
|
||||
|
||||
if (write_chars_bin(out, "00dc", 4) == -1) {
|
||||
(void)fprintf(stderr, "gwavi_add_frame: write_chars_bin() "
|
||||
|
@ -386,33 +355,13 @@ gwavi_t::add_audio( unsigned char *buffer, size_t len)
|
|||
return -1;
|
||||
}
|
||||
|
||||
offset_count++;
|
||||
|
||||
maxi_pad = len % WORD_SIZE;
|
||||
if (maxi_pad > 0)
|
||||
{
|
||||
maxi_pad = WORD_SIZE - maxi_pad;
|
||||
}
|
||||
|
||||
if (offset_count >= offsets_len)
|
||||
{
|
||||
void *tmpPtr;
|
||||
offsets_len += 1024;
|
||||
tmpPtr = realloc( offsets,
|
||||
(size_t)offsets_len *
|
||||
sizeof(unsigned int));
|
||||
if ( tmpPtr )
|
||||
{
|
||||
offsets = (unsigned int *)tmpPtr;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "gwavi_add_audio: realloc() failed\n");
|
||||
}
|
||||
}
|
||||
|
||||
offsets[offsets_ptr++] =
|
||||
(unsigned int)((len) | 0x80000000);
|
||||
offsets.push_back( (unsigned int)((len) | 0x80000000) );
|
||||
|
||||
if (write_chars_bin(out,"01wb",4) == -1)
|
||||
{
|
||||
|
@ -472,13 +421,14 @@ gwavi_t::close(void)
|
|||
if (fseek(out,t,SEEK_SET) == -1)
|
||||
goto fseek_failed;
|
||||
|
||||
if (write_index(out, offset_count, offsets) == -1)
|
||||
if (write_index(out) == -1)
|
||||
{
|
||||
(void)fprintf(stderr, "gwavi_close: write_index() failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(offsets);
|
||||
//free(offsets);
|
||||
offsets.clear();
|
||||
|
||||
/* reset some avi header fields */
|
||||
avi_header.number_of_frames = stream_header_v.data_length;
|
||||
|
|
|
@ -35,6 +35,7 @@
|
|||
|
||||
#include <stdint.h> /* for size_t */
|
||||
#include <stddef.h> /* for size_t */
|
||||
#include <vector>
|
||||
|
||||
#pragma pack( push, 2 )
|
||||
|
||||
|
@ -178,12 +179,8 @@ class gwavi_t
|
|||
struct gwavi_stream_header_t stream_header_a;
|
||||
struct gwavi_stream_format_a_t stream_format_a;
|
||||
long marker;
|
||||
int offsets_ptr;
|
||||
int offsets_len;
|
||||
long offsets_start;
|
||||
unsigned int *offsets;
|
||||
std::vector <unsigned int> offsets;
|
||||
long movi_fpos;
|
||||
int offset_count;
|
||||
int bits_per_pixel;
|
||||
char fourcc[8];
|
||||
|
||||
|
@ -196,7 +193,7 @@ class gwavi_t
|
|||
int write_stream_format_a(FILE *fp,
|
||||
struct gwavi_stream_format_a_t *stream_format_a);
|
||||
int write_avi_header_chunk( FILE *fp );
|
||||
int write_index(FILE *fp, int count, unsigned int *offsets);
|
||||
int write_index(FILE *fp);
|
||||
int check_fourcc(const char *fourcc);
|
||||
int write_int(FILE *fp, unsigned int n);
|
||||
int write_short(FILE *fp, unsigned int n);
|
||||
|
|
Loading…
Reference in New Issue