Bug 1077086 - Update nestegg from upstream. r=giles
authorMatthew Gregan <kinetik@flim.org>
Wed, 08 Oct 2014 13:08:36 +1300
changeset 209246 c78b949b21da028939c2dac178e06fce5984cb8c
parent 209245 97068dca449ef5b8afa16bc54975d6aab02ec168
child 209247 43c963b56723f6d318bdda0eb906d3cb5b08717a
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersgiles
bugs1077086
milestone35.0a1
Bug 1077086 - Update nestegg from upstream. r=giles
media/libnestegg/README_MOZILLA
media/libnestegg/include/nestegg.h
media/libnestegg/src/nestegg.c
--- a/media/libnestegg/README_MOZILLA
+++ b/media/libnestegg/README_MOZILLA
@@ -1,8 +1,8 @@
 The source from this directory was copied from the nestegg
 git repository using the update.sh script.  The only changes
 made were those applied by update.sh and the addition of
 Makefile.in build files for the Mozilla build system.
 
 The nestegg git repository is: git://github.com/kinetiknz/nestegg.git
 
-The git commit ID used was 59220ae3e801cbad0f8160129c4df315469af671.
+The git commit ID used was fa610276b66c2a62415fc957483e9adc0a55de5e.
--- a/media/libnestegg/include/nestegg.h
+++ b/media/libnestegg/include/nestegg.h
@@ -166,18 +166,18 @@ void nestegg_destroy(nestegg * context);
 /** Query the duration of the media stream in nanoseconds.
     @param context  Stream context initialized by #nestegg_init.
     @param duration Storage for the queried duration.
     @retval  0 Success.
     @retval -1 Error. */
 int nestegg_duration(nestegg * context, uint64_t * duration);
 
 /** Query the tstamp scale of the media stream in nanoseconds.
-    Timecodes presented by nestegg have been scaled by this value
-    before presentation to the caller.
+    @note Timecodes presented by nestegg have been scaled by this value
+          before presentation to the caller.
     @param context Stream context initialized by #nestegg_init.
     @param scale   Storage for the queried scale factor.
     @retval  0 Success.
     @retval -1 Error. */
 int nestegg_tstamp_scale(nestegg * context, uint64_t * scale);
 
 /** Query the number of tracks in the media stream.
     @param context Stream context initialized by #nestegg_init.
@@ -196,18 +196,18 @@ int nestegg_track_count(nestegg * contex
     @param tstamp      Starting timestamp of the cluster.
     @retval  0 Success.
     @retval -1 Error. */
 int nestegg_get_cue_point(nestegg * context, unsigned int cluster_num,
                           int64_t max_offset, int64_t * start_pos,
                           int64_t * end_pos, uint64_t * tstamp);
 
 /** Seek to @a offset.  Stream will seek directly to offset.
-    Should be used to seek to the start of a resync point, i.e. cluster; the
-    parser will not be able to understand other offsets.
+    Must be used to seek to the start of a cluster; the parser will not be
+    able to understand other offsets.
     @param context Stream context initialized by #nestegg_init.
     @param offset  Absolute offset in bytes.
     @retval  0 Success.
     @retval -1 Error. */
 int nestegg_offset_seek(nestegg * context, uint64_t offset);
 
 /** Seek @a track to @a tstamp.  Stream seek will terminate at the earliest
     key point in the stream at or before @a tstamp.  Other tracks in the
@@ -363,32 +363,27 @@ int nestegg_packet_discard_padding(neste
                                    int64_t * discard_padding);
 
 /** Query the presence of cues.
     @param context  Stream context initialized by #nestegg_init.
     @retval 0 The media has no cues.
     @retval 1 The media has cues. */
 int nestegg_has_cues(nestegg * context);
 
-/**
- * Try to determine if the buffer looks like the beginning of a WebM file.
- *
- * @param buffer A buffer containing the beginning of a media file.
- * @param length The size of the buffer.
- * @retval 0 The file is not a WebM file.
- * @retval 1 The file is a WebM file. */
+/** Try to determine if the buffer looks like the beginning of a WebM file.
+    @param buffer A buffer containing the beginning of a media file.
+    @param length The size of the buffer.
+    @retval 0 The file is not a WebM file.
+    @retval 1 The file is a WebM file. */
 int nestegg_sniff(unsigned char const * buffer, size_t length);
 
-/**
- * Set the underlying allocation function for library allocations.
- *
- * @param realloc_func The desired function.
- * @retval 0 realloc_func(p, 0) does not act as free()
- * @retval 1 realloc_func(p, 0) acts as free()
- * @retval -1 malloc failed during realloc_func test
- */
+/** Set the underlying allocation function for library allocations.
+    @param realloc_func The desired function.
+    @retval 1 Success.  realloc_func(p, 0) acts as free()
+    @retval 0 Failure. realloc_func(p, 0) does not act as free()
+    @retval -1 Failure. realloc_func(NULL, 1) failed. */
 int nestegg_set_halloc_func(void * (* realloc_func)(void *, size_t));
 
 #if defined(__cplusplus)
 }
 #endif
 
 #endif /* NESTEGG_671cac2a_365d_ed69_d7a3_4491d3538d79 */
--- 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;
     }