shithub: opusfile

Download patch

ref: 37f837542f14d50b52bdca5504f376b532a244a0
parent: fd65b94fa24d3e326fe978a9bf422fe8ce029882
author: Timothy B. Terriberry <[email protected]>
date: Fri May 12 17:58:46 EDT 2017

Harmonize op_raw_total() and op_raw_seek()

op_raw_seek() will fail if you try to seek to a byte position
 beyond the end of the file.
However, the "end" is defined in terms of _of->end, which is
 specifically the end of the last Ogg page found in the underlying
 source (excluding any trailing non-Ogg data).
op_raw_total(_of,-1) returns the total size of the stream by using
 _of->end, but it was also subtracting the offset of the first Opus
 page in the first link.
Since there might have been other Ogg streams concurrently
 multiplexed with the first Opus stream whose BOS pages appear
 first, or there might simply be non-Ogg junk at the start, that
 left the caller with no way to determine the valid range of byte
 offsets that could be passed to op_raw_seek().

Instead, make op_raw_total() pretend the first link starts at
 offset 0, and explicitly document that it's what defines the range
 of valid values to op_raw_seek().
This is how our own seeking_example.c was using it, anyway.

--- a/include/opusfile.h
+++ b/include/opusfile.h
@@ -1670,6 +1670,8 @@
     packets out of the tail of the link to which it seeks.
    \param _of          The \c OggOpusFile in which to seek.
    \param _byte_offset The byte position to seek to.
+                       This must be between 0 and #op_raw_total(\a _of,\c -1)
+                        (inclusive).
    \return 0 on success, or a negative error code on failure.
    \retval #OP_EREAD    The underlying seek operation failed.
    \retval #OP_EINVAL   The stream was only partially open, or the target was
--- a/src/opusfile.c
+++ b/src/opusfile.c
@@ -1724,9 +1724,9 @@
    ||OP_UNLIKELY(_li>=_of->nlinks)){
     return OP_EINVAL;
   }
-  if(_li<0)return _of->end-_of->links[0].offset;
+  if(_li<0)return _of->end;
   return (_li+1>=_of->nlinks?_of->end:_of->links[_li+1].offset)
-   -_of->links[_li].offset;
+   -(_li>0?_of->links[_li].offset:0);
 }
 
 ogg_int64_t op_pcm_total(const OggOpusFile *_of,int _li){