--- a/media/libnestegg/src/nestegg.c
+++ b/media/libnestegg/src/nestegg.c
@@ -316,28 +316,19 @@ struct frame {
struct block_additional {
unsigned int id;
unsigned char * data;
size_t length;
struct block_additional * next;
};
-#define NE_IO_BUFSZ 16384
-
-struct nestegg_io_buf {
- nestegg_io io;
- unsigned char buffer[NE_IO_BUFSZ];
- size_t bufsz;
- int offset;
-};
-
/* Public (opaque) Structures */
struct nestegg {
- struct nestegg_io_buf * io;
+ nestegg_io * io;
nestegg_log log;
struct pool_ctx * alloc_pool;
uint64_t last_id;
uint64_t last_size;
int last_valid;
struct list_node * ancestor;
struct ebml ebml;
struct segment segment;
@@ -550,127 +541,53 @@ ne_pool_alloc(size_t size, struct pool_c
static void *
ne_alloc(size_t size)
{
return calloc(1, size);
}
static int
-ne_io_read(struct nestegg_io_buf * io, void * buffer, size_t length)
+ne_io_read(nestegg_io * io, void * buffer, size_t length)
{
- int64_t off;
- int r;
- size_t avail;
-
- assert(io->offset == -1 || (io->offset >= 0 && (unsigned int) io->offset < io->bufsz));
-
- /* Too big to buffer, invalidate buffer and read through */
- if (length > io->bufsz) {
- if (io->offset != -1) {
- r = io->io.seek(-(io->bufsz - io->offset), NESTEGG_SEEK_CUR, io->io.userdata);
- if (r != 0) {
- return -1;
- }
- }
- io->offset = -1;
- return io->io.read(buffer, length, io->io.userdata);
- }
-
- /* Buffer invalid */
- if (io->offset == -1) {
- off = io->io.tell(io->io.userdata);
- if (off == -1) {
- return -1;
- }
- /* Refill buffer */
- r = io->io.read(io->buffer, io->bufsz, io->io.userdata);
- if (r != 1) {
- /* Read truncated due to being within io->bufsz of EOS, reset read
- position and switch to read through mode */
- io->offset = -1;
- io->bufsz = 0;
- if (r == 0) {
- r = io->io.seek(off, NESTEGG_SEEK_SET, io->io.userdata);
- }
- if (r == 0) {
- return io->io.read(buffer, length, io->io.userdata);
- }
- return -1;
- }
- if (r == 1) {
- io->offset = 0;
- }
- }
-
- /* Service request with what we have */
- avail = length;
- if (io->bufsz - io->offset < length) {
- avail = io->bufsz - io->offset;
- }
- memcpy(buffer, io->buffer + io->offset, avail);
- io->offset += avail;
-
- if ((unsigned int) io->offset == io->bufsz) {
- io->offset = -1;
- }
-
- /* Still more to read, invalidate buffer and read more */
- if (length - avail > 0) {
- return ne_io_read(io, (char *) buffer + avail, length - avail);
- }
-
- return 1;
+ return io->read(buffer, length, io->userdata);
}
static int
-ne_io_seek(struct nestegg_io_buf * io, int64_t offset, int whence)
-{
- /* Invalidate buffer */
- io->offset = -1;
-
- return io->io.seek(offset, whence, io->io.userdata);
-}
-
-static int64_t
-ne_io_tell(struct nestegg_io_buf * io)
+ne_io_seek(nestegg_io * io, int64_t offset, int whence)
{
- int64_t off;
-
- off = io->io.tell(io->io.userdata);
- if (off == -1) {
- return -1;
- }
- if (io->offset == -1) {
- return off;
- }
- assert(off >= (int64_t) io->bufsz - io->offset);
- return off - io->bufsz + (unsigned int) io->offset;
+ return io->seek(offset, whence, io->userdata);
}
static int
-ne_io_read_skip(struct nestegg_io_buf * io, size_t length)
+ne_io_read_skip(nestegg_io * io, size_t length)
{
size_t get;
unsigned char buf[8192];
int r = 1;
while (length > 0) {
get = length < sizeof(buf) ? length : sizeof(buf);
r = ne_io_read(io, buf, get);
if (r != 1)
break;
length -= get;
}
return r;
}
+static int64_t
+ne_io_tell(nestegg_io * io)
+{
+ return io->tell(io->userdata);
+}
+
static int
-ne_bare_read_vint(struct nestegg_io_buf * io, uint64_t * value, uint64_t * length, enum vint_mask maskflag)
+ne_bare_read_vint(nestegg_io * io, uint64_t * value, uint64_t * length, enum vint_mask maskflag)
{
int r;
unsigned char b;
size_t maxlen = 8;
unsigned int count = 1, mask = 1 << 7;
r = ne_io_read(io, &b, 1);
if (r != 1)
@@ -697,29 +614,29 @@ ne_bare_read_vint(struct nestegg_io_buf
*value <<= 8;
*value |= b;
}
return 1;
}
static int
-ne_read_id(struct nestegg_io_buf * io, uint64_t * value, uint64_t * length)
+ne_read_id(nestegg_io * io, uint64_t * value, uint64_t * length)
{
return ne_bare_read_vint(io, value, length, MASK_NONE);
}
static int
-ne_read_vint(struct nestegg_io_buf * io, uint64_t * value, uint64_t * length)
+ne_read_vint(nestegg_io * io, uint64_t * value, uint64_t * length)
{
return ne_bare_read_vint(io, value, length, MASK_FIRST_BIT);
}
static int
-ne_read_svint(struct nestegg_io_buf * io, int64_t * value, uint64_t * length)
+ne_read_svint(nestegg_io * io, int64_t * value, uint64_t * length)
{
int r;
uint64_t uvalue;
uint64_t ulength;
int64_t svint_subtr[] = {
0x3f, 0x1fff,
0xfffff, 0x7ffffff,
0x3ffffffffLL, 0x1ffffffffffLL,
@@ -731,17 +648,17 @@ ne_read_svint(struct nestegg_io_buf * io
return r;
*value = uvalue - svint_subtr[ulength - 1];
if (length)
*length = ulength;
return r;
}
static int
-ne_read_uint(struct nestegg_io_buf * io, uint64_t * val, uint64_t length)
+ne_read_uint(nestegg_io * io, uint64_t * val, uint64_t length)
{
unsigned char b;
int r;
if (length == 0 || length > 8)
return -1;
r = ne_io_read(io, &b, 1);
if (r != 1)
@@ -753,17 +670,17 @@ ne_read_uint(struct nestegg_io_buf * io,
return r;
*val <<= 8;
*val |= b;
}
return 1;
}
static int
-ne_read_int(struct nestegg_io_buf * io, int64_t * val, uint64_t length)
+ne_read_int(nestegg_io * io, int64_t * val, uint64_t length)
{
int r;
uint64_t uval, base;
r = ne_read_uint(io, &uval, length);
if (r != 1)
return r;
@@ -780,17 +697,17 @@ ne_read_int(struct nestegg_io_buf * io,
} else {
*val = (int64_t) uval;
}
return 1;
}
static int
-ne_read_float(struct nestegg_io_buf * io, double * val, uint64_t length)
+ne_read_float(nestegg_io * io, double * val, uint64_t length)
{
union {
uint64_t u;
float f;
double d;
} value;
int r;
@@ -1215,17 +1132,17 @@ ne_xiph_lace_value(unsigned char ** np)
}
*np = p;
return value;
}
static int
-ne_read_xiph_lace_value(struct nestegg_io_buf * io, uint64_t * value, size_t * consumed)
+ne_read_xiph_lace_value(nestegg_io * io, uint64_t * value, size_t * consumed)
{
int r;
uint64_t lace;
r = ne_read_uint(io, &lace, 1);
if (r != 1)
return r;
*consumed += 1;
@@ -1238,17 +1155,17 @@ ne_read_xiph_lace_value(struct nestegg_i
*consumed += 1;
*value += lace;
}
return 1;
}
static int
-ne_read_xiph_lacing(struct nestegg_io_buf * io, size_t block, size_t * read, uint64_t n, uint64_t * sizes)
+ne_read_xiph_lacing(nestegg_io * io, size_t block, size_t * read, uint64_t n, uint64_t * sizes)
{
int r;
size_t i = 0;
uint64_t sum = 0;
while (--n) {
r = ne_read_xiph_lace_value(io, &sizes[i], read);
if (r != 1)
@@ -1261,17 +1178,17 @@ ne_read_xiph_lacing(struct nestegg_io_bu
return -1;
/* Last frame is the remainder of the block. */
sizes[i] = block - *read - sum;
return 1;
}
static int
-ne_read_ebml_lacing(struct nestegg_io_buf * io, size_t block, size_t * read, uint64_t n, uint64_t * sizes)
+ne_read_ebml_lacing(nestegg_io * io, size_t block, size_t * read, uint64_t n, uint64_t * sizes)
{
int r;
uint64_t lace, sum, length;
int64_t slace;
size_t i = 0;
r = ne_read_vint(io, &lace, &length);
if (r != 1)
@@ -1939,19 +1856,17 @@ ne_match_webm(nestegg_io io, int64_t max
if (!ctx)
return -1;
ctx->io = ne_alloc(sizeof(*ctx->io));
if (!ctx->io) {
nestegg_destroy(ctx);
return -1;
}
- ctx->io->io = io;
- ctx->io->bufsz = NE_IO_BUFSZ;
- ctx->io->offset = -1;
+ *ctx->io = io;
ctx->alloc_pool = ne_pool_init();
if (!ctx->alloc_pool) {
nestegg_destroy(ctx);
return -1;
}
ctx->log = ne_null_log_callback;
r = ne_peek_element(ctx, &id, NULL);
@@ -1999,19 +1914,17 @@ nestegg_init(nestegg ** context, nestegg
if (!ctx)
return -1;
ctx->io = ne_alloc(sizeof(*ctx->io));
if (!ctx->io) {
nestegg_destroy(ctx);
return -1;
}
- ctx->io->io = io;
- ctx->io->bufsz = NE_IO_BUFSZ;
- ctx->io->offset = -1;
+ *ctx->io = io;
ctx->log = callback;
ctx->alloc_pool = ne_pool_init();
if (!ctx->alloc_pool) {
nestegg_destroy(ctx);
return -1;
}
if (!ctx->log)
@@ -2152,33 +2065,33 @@ nestegg_get_cue_point(nestegg * ctx, uns
while (cues_node && !range_obtained) {
assert(cues_node->id == ID_CUE_POINT);
cue_point = cues_node->data;
cue_pos_node = cue_point->cue_track_positions.head;
while (cue_pos_node) {
assert(cue_pos_node->id == ID_CUE_TRACK_POSITIONS);
pos = cue_pos_node->data;
- for (track = 0; track < track_count; track++) {
+ for (track = 0; track < track_count; ++track) {
if (ne_get_uint(pos->track, &track_number) != 0)
return -1;
if (ne_map_track_number_to_index(ctx, track_number, &track_index) != 0)
return -1;
if (track_index == track) {
if (ne_get_uint(pos->cluster_position, &seek_pos) != 0)
return -1;
if (cluster_count == cluster_num) {
- *start_pos = ctx->segment_offset+seek_pos;
+ *start_pos = ctx->segment_offset + seek_pos;
if (ne_get_uint(cue_point->time, &time) != 0)
return -1;
*tstamp = time * tc_scale;
- } else if (cluster_count == cluster_num+1) {
- *end_pos = (ctx->segment_offset+seek_pos)-1;
+ } else if (cluster_count == cluster_num + 1) {
+ *end_pos = ctx->segment_offset + seek_pos - 1;
range_obtained = 1;
break;
}
cluster_count++;
}
}
cue_pos_node = cue_pos_node->next;
}