diff --git a/Makefile b/Makefile index 7d9a18ed..c63aa47b 100644 --- a/Makefile +++ b/Makefile @@ -242,7 +242,7 @@ JPDIR=$(MOUNT_DIR)/jpeg-8c SPEEXDIR=$(MOUNT_DIR)/libspeex OGGDIR=$(MOUNT_DIR)/libogg-1.3.0 OPUSDIR=$(MOUNT_DIR)/opus-1.0.2 -OPUSFILEDIR=$(MOUNT_DIR)/opusfile-0.2 +OPUSFILEDIR=$(MOUNT_DIR)/opusfile-0.5 ZDIR=$(MOUNT_DIR)/zlib Q3ASMDIR=$(MOUNT_DIR)/tools/asm LBURGDIR=$(MOUNT_DIR)/tools/lcc/lburg @@ -1955,7 +1955,8 @@ Q3OBJ += \ $(B)/client/info.o \ $(B)/client/internal.o \ $(B)/client/opusfile.o \ - $(B)/client/stream.o + $(B)/client/stream.o \ + $(B)/client/wincerts.o endif endif diff --git a/code/opusfile-0.2/src/info.c b/code/opusfile-0.2/src/info.c deleted file mode 100644 index 62edecdb..00000000 --- a/code/opusfile-0.2/src/info.c +++ /dev/null @@ -1,286 +0,0 @@ -/******************************************************************** - * * - * THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. * - * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * - * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * - * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * - * * - * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 2012 * - * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * - * * - ********************************************************************/ -#include "internal.h" -#include -#include - -static unsigned op_parse_uint16le(const unsigned char *_data){ - return _data[0]|_data[1]<<8; -} - -static int op_parse_int16le(const unsigned char *_data){ - int ret; - ret=_data[0]|_data[1]<<8; - return (ret^0x8000)-0x8000; -} - -static opus_uint32 op_parse_uint32le(const unsigned char *_data){ - return _data[0]|_data[1]<<8|_data[2]<<16|_data[3]<<24; -} - -int opus_head_parse(OpusHead *_head,const unsigned char *_data,size_t _len){ - OpusHead head; - if(_len<8)return OP_ENOTFORMAT; - if(memcmp(_data,"OpusHead",8)!=0)return OP_ENOTFORMAT; - if(_len<9)return OP_EBADHEADER; - head.version=_data[8]; - if(head.version>15)return OP_EVERSION; - if(_len<19)return OP_EBADHEADER; - head.channel_count=_data[9]; - head.pre_skip=op_parse_uint16le(_data+10); - head.input_sample_rate=op_parse_uint32le(_data+12); - head.output_gain=op_parse_int16le(_data+16); - head.mapping_family=_data[18]; - if(head.mapping_family==0){ - if(head.channel_count<1||head.channel_count>2)return OP_EBADHEADER; - if(head.version<=1&&_len>19)return OP_EBADHEADER; - head.stream_count=1; - head.coupled_count=head.channel_count-1; - if(_head!=NULL){ - _head->mapping[0]=0; - _head->mapping[1]=1; - } - } - else if(head.mapping_family==1){ - size_t size; - int ci; - if(head.channel_count<1||head.channel_count>8)return OP_EBADHEADER; - size=21+head.channel_count; - if(_lensize)return OP_EBADHEADER; - head.stream_count=_data[19]; - if(head.stream_count<1)return OP_EBADHEADER; - head.coupled_count=_data[20]; - if(head.coupled_count>head.stream_count)return OP_EBADHEADER; - for(ci=0;ci=head.stream_count+head.coupled_count - &&_data[21+ci]!=255){ - return OP_EBADHEADER; - } - } - if(_head!=NULL)memcpy(_head->mapping,_data+21,head.channel_count); - } - /*General purpose players should not attempt to play back content with - channel mapping family 255.*/ - else if(head.mapping_family==255)return OP_EIMPL; - /*No other channel mapping families are currently defined.*/ - else return OP_EBADHEADER; - if(_head!=NULL)memcpy(_head,&head,head.mapping-(unsigned char *)&head); - return 0; -} - -void opus_tags_init(OpusTags *_tags){ - memset(_tags,0,sizeof(*_tags)); -} - -void opus_tags_clear(OpusTags *_tags){ - int i; - for(i=_tags->comments;i-->0;)_ogg_free(_tags->user_comments[i]); - _ogg_free(_tags->user_comments); - _ogg_free(_tags->comment_lengths); - _ogg_free(_tags->vendor); -} - -/*The actual implementation of opus_tags_parse(). - Unlike the public API, this function requires _tags to already be - initialized, modifies its contents before success is guaranteed, and assumes - the caller will clear it on error.*/ -int opus_tags_parse_impl(OpusTags *_tags, - const unsigned char *_data,size_t _len){ - opus_uint32 count; - size_t size; - size_t len; - int ncomments; - int i; - len=_len; - if(len<8)return OP_ENOTFORMAT; - if(memcmp(_data,"OpusTags",8)!=0)return OP_ENOTFORMAT; - if(len<16)return OP_EBADHEADER; - _data+=8; - len-=8; - count=op_parse_uint32le(_data); - _data+=4; - len-=4; - if(count>len)return OP_EBADHEADER; - if(_tags!=NULL){ - char *vendor; - size=count+1; - if(sizevendor=vendor; - } - _data+=count; - len-=count; - if(len<4)return OP_EBADHEADER; - count=op_parse_uint32le(_data); - _data+=4; - len-=4; - /*Check to make sure there's minimally sufficient data left in the packet.*/ - if(count>len>>2)return OP_EBADHEADER; - /*Check for overflow (the API limits this to an int).*/ - if(count>(opus_uint32)INT_MAX-1)return OP_EFAULT; - if(_tags!=NULL){ - size=sizeof(*_tags->comment_lengths)*(count+1); - if(size/sizeof(*_tags->comment_lengths)!=count+1)return OP_EFAULT; - _tags->comment_lengths=(int *)_ogg_malloc(size); - size=sizeof(*_tags->user_comments)*(count+1); - if(size/sizeof(*_tags->user_comments)!=count+1)return OP_EFAULT; - _tags->user_comments=(char **)_ogg_malloc(size); - if(_tags->comment_lengths==NULL||_tags->user_comments==NULL){ - return OP_EFAULT; - } - } - ncomments=(int)count; - for(i=0;ilen>>2)return OP_EBADHEADER; - count=op_parse_uint32le(_data); - _data+=4; - len-=4; - if(count>len)return OP_EBADHEADER; - /*Check for overflow (the API limits this to an int).*/ - if(count>(opus_uint32)INT_MAX)return OP_EFAULT; - if(_tags!=NULL){ - _tags->comment_lengths[i]=(int)count; - size=count+1; - if(sizeuser_comments[i]=(char *)_ogg_malloc(size); - if(_tags->user_comments[i]==NULL)return OP_EFAULT; - _tags->comments=i+1; - memcpy(_tags->user_comments[i],_data,count); - _tags->user_comments[i][count]='\0'; - } - _data+=count; - len-=count; - } - if(_tags!=NULL){ - _tags->user_comments[ncomments]=NULL; - _tags->comment_lengths[ncomments]=0; - } - return 0; -} - -int opus_tags_parse(OpusTags *_tags,const unsigned char *_data,size_t _len){ - if(_tags!=NULL){ - OpusTags tags; - int ret; - opus_tags_init(&tags); - ret=opus_tags_parse_impl(&tags,_data,_len); - if(ret<0)opus_tags_clear(&tags); - else *_tags=*&tags; - return ret; - } - else return opus_tags_parse_impl(NULL,_data,_len); -} - -/*Add room for a new comment.*/ -static int op_tags_add_prepare(OpusTags *_tags){ - char **user_comments; - int *comment_lengths; - int ncomments; - ncomments=_tags->comments; - user_comments=_ogg_realloc(_tags->user_comments, - sizeof(*_tags->user_comments)*(ncomments+2)); - if(OP_UNLIKELY(user_comments==NULL))return OP_EFAULT; - _tags->user_comments=user_comments; - comment_lengths=_ogg_realloc(_tags->comment_lengths, - sizeof(*_tags->comment_lengths)*(ncomments+2)); - if(OP_UNLIKELY(comment_lengths==NULL))return OP_EFAULT; - _tags->comment_lengths=comment_lengths; - comment_lengths[ncomments]=comment_lengths[ncomments+1]=0; - /*Our caller will always set user_comments[ncomments].*/ - user_comments[ncomments+1]=NULL; - return 0; -} - -int opus_tags_add(OpusTags *_tags,const char *_tag,const char *_value){ - char *comment; - int tag_len; - int value_len; - int ncomments; - int ret; - ret=op_tags_add_prepare(_tags); - if(OP_UNLIKELY(ret<0))return ret; - tag_len=strlen(_tag); - value_len=strlen(_value); - ncomments=_tags->comments; - /*+2 for '=' and '\0'.*/ - _tags->user_comments[ncomments]=comment= - (char *)_ogg_malloc(sizeof(*comment)*(tag_len+value_len+2)); - if(OP_UNLIKELY(comment==NULL))return OP_EFAULT; - _tags->comment_lengths[ncomments]=tag_len+value_len+1; - memcpy(comment,_tag,sizeof(*comment)*tag_len); - comment[tag_len]='='; - memcpy(comment+tag_len+1,_value,sizeof(*comment)*(value_len+1)); - return 0; -} - -int opus_tags_add_comment(OpusTags *_tags,const char *_comment){ - char *comment; - int ncomments; - int comment_len; - int ret; - ret=op_tags_add_prepare(_tags); - if(OP_UNLIKELY(ret<0))return ret; - comment_len=strlen(_comment); - ncomments=_tags->comments; - _tags->user_comments[ncomments]=comment=(char *) - _ogg_malloc(sizeof(*_tags->user_comments[ncomments])*(comment_len+1)); - if(OP_UNLIKELY(comment==NULL))return OP_EFAULT; - _tags->comment_lengths[ncomments]=comment_len; - memcpy(comment,_comment,sizeof(*comment)*(comment_len+1)); - return 0; -} - -/*Is _a a "tag=value" comment whose tag matches _b? - 0 if it is, a non-zero value otherwise.*/ -static int op_tagcompare(const char *_a,const char *_b,int _n){ - return op_strncasecmp(_a,_b,_n)||_a[_n]!='='; -} - -const char *opus_tags_query(const OpusTags *_tags,const char *_tag,int _count){ - char **user_comments; - int tag_len; - int found; - int ncomments; - int ci; - tag_len=strlen(_tag); - ncomments=_tags->comments; - user_comments=_tags->user_comments; - found=0; - for(ci=0;cicomments; - user_comments=_tags->user_comments; - found=0; - for(ci=0;ci -#include -#include -#include - -typedef struct OpusMemStream OpusMemStream; - -#define OP_MEM_SIZE_MAX (~(size_t)0>>1) -#define OP_MEM_DIFF_MAX ((ptrdiff_t)OP_MEM_SIZE_MAX) - -/*The context information needed to read from a block of memory as if it were a - file.*/ -struct OpusMemStream{ - /*The block of memory to read from.*/ - const unsigned char *data; - /*The total size of the block. - This must be at most OP_MEM_SIZE_MAX to prevent signed overflow while - seeking.*/ - ptrdiff_t size; - /*The current file position. - This is allowed to be set arbitrarily greater than size (i.e., past the end - of the block, though we will not read data past the end of the block), but - is not allowed to be negative (i.e., before the beginning of the block).*/ - ptrdiff_t pos; -}; - -static int op_fread(void *_stream,unsigned char *_ptr,int _buf_size){ - FILE *stream; - size_t ret; - /*Check for empty read.*/ - if(_buf_size<=0)return 0; - stream=(FILE *)_stream; - ret=fread(_ptr,1,_buf_size,stream); - OP_ASSERT(ret<=(size_t)_buf_size); - /*If ret==0 and !feof(stream), there was a read error.*/ - return ret>0||feof(stream)?(int)ret:OP_EREAD; -} - -static int op_fseek(void *_stream,opus_int64 _offset,int _whence){ -#if defined(__MINGW32__) - return fseeko64((FILE *)_stream,_offset,_whence); -#elif defined(_MSC_VER) - return _fseeki64((FILE *)_stream,_offset,_whence); -#else - return fseeko((FILE *)_stream,(off_t)_offset,_whence); -#endif -} - -static opus_int64 op_ftell(void *_stream){ -#if defined(__MINGW32__) - return ftello64((FILE *)_stream); -#elif defined(_MSC_VER) - return _ftelli64((FILE *)_stream); -#else - return ftello((FILE *)_stream); -#endif -} - -static const OpusFileCallbacks OP_FILE_CALLBACKS={ - op_fread, - op_fseek, - op_ftell, - (op_close_func)fclose -}; - -void *op_fopen(OpusFileCallbacks *_cb,const char *_path,const char *_mode){ - FILE *fp; - fp=fopen(_path,_mode); - if(fp!=NULL)*_cb=*&OP_FILE_CALLBACKS; - return fp; -} - -void *op_fdopen(OpusFileCallbacks *_cb,int _fd,const char *_mode){ - FILE *fp; - fp=fdopen(_fd,_mode); - if(fp!=NULL)*_cb=*&OP_FILE_CALLBACKS; - return fp; -} - -void *op_freopen(OpusFileCallbacks *_cb,const char *_path,const char *_mode, - void *_stream){ - FILE *fp; - fp=freopen(_path,_mode,(FILE *)_stream); - if(fp!=NULL)*_cb=*&OP_FILE_CALLBACKS; - return fp; -} - -static int op_mem_read(void *_stream,unsigned char *_ptr,int _buf_size){ - OpusMemStream *stream; - ptrdiff_t size; - ptrdiff_t pos; - stream=(OpusMemStream *)_stream; - /*Check for empty read.*/ - if(_buf_size<=0)return 0; - size=stream->size; - pos=stream->pos; - /*Check for EOF.*/ - if(pos>=size)return 0; - /*Check for a short read.*/ - _buf_size=(int)OP_MAX(size-pos,_buf_size); - memcpy(_ptr,stream->data+pos,_buf_size); - pos+=_buf_size; - stream->pos=pos; - return _buf_size; -} - -static int op_mem_seek(void *_stream,opus_int64 _offset,int _whence){ - OpusMemStream *stream; - ptrdiff_t pos; - stream=(OpusMemStream *)_stream; - pos=stream->pos; - switch(_whence){ - case SEEK_SET:{ - /*Check for overflow:*/ - if(_offset<0||_offset>OP_MEM_DIFF_MAX)return -1; - pos=(ptrdiff_t)_offset; - }break; - case SEEK_CUR:{ - /*Check for overflow:*/ - if(_offset<-pos||_offset>OP_MEM_DIFF_MAX-pos)return -1; - pos=(ptrdiff_t)(pos+_offset); - }break; - case SEEK_END:{ - ptrdiff_t size; - size=stream->size; - OP_ASSERT(size>=0); - /*Check for overflow:*/ - if(_offset>size||_offsetpos=pos; - return 0; -} - -static opus_int64 op_mem_tell(void *_stream){ - OpusMemStream *stream; - stream=(OpusMemStream *)_stream; - return (ogg_int64_t)stream->pos; -} - -static int op_mem_close(void *_stream){ - _ogg_free(_stream); - return 0; -} - -static const OpusFileCallbacks OP_MEM_CALLBACKS={ - op_mem_read, - op_mem_seek, - op_mem_tell, - op_mem_close -}; - -void *op_mem_stream_create(OpusFileCallbacks *_cb, - const unsigned char *_data,size_t _size){ - OpusMemStream *stream; - if(_size>OP_MEM_SIZE_MAX)return NULL; - stream=(OpusMemStream *)_ogg_malloc(sizeof(*stream)); - if(stream!=NULL){ - *_cb=*&OP_MEM_CALLBACKS; - stream->data=_data; - stream->size=_size; - stream->pos=0; - } - return stream; -} diff --git a/code/opusfile-0.2/include/opusfile.h b/code/opusfile-0.5/include/opusfile.h similarity index 76% rename from code/opusfile-0.2/include/opusfile.h rename to code/opusfile-0.5/include/opusfile.h index bc7c7384..850cd6b9 100644 --- a/code/opusfile-0.2/include/opusfile.h +++ b/code/opusfile-0.5/include/opusfile.h @@ -16,7 +16,6 @@ ********************************************************************/ #if !defined(_opusfile_h) # define _opusfile_h (1) -# include /**\mainpage \section Introduction @@ -50,17 +49,68 @@ Several additional sections are not tied to the main API. - \ref stream_callbacks - \ref header_info - - \ref error_codes*/ + - \ref error_codes + \section Overview + + The libopusfile API always decodes files to 48 kHz. + The original sample rate is not preserved by the lossy compression, though + it is stored in the header to allow you to resample to it after decoding + (the libopusfile API does not currently provide a resampler, + but the + the + Speex resampler is a good choice if you need one). + In general, if you are playing back the audio, you should leave it at + 48 kHz, provided your audio hardware supports it. + When decoding to a file, it may be worth resampling back to the original + sample rate, so as not to surprise users who might not expect the sample + rate to change after encoding to Opus and decoding. + + Opus files can contain anywhere from 1 to 255 channels of audio. + The channel mappings for up to 8 channels are the same as the + Vorbis + mappings. + A special stereo API can convert everything to 2 channels, making it simple + to support multichannel files in an application which only has stereo + output. + Although the libopusfile ABI provides support for the theoretical + maximum number of channels, the current implementation does not support + files with more than 8 channels, as they do not have well-defined channel + mappings. + + Like all Ogg files, Opus files may be "chained". + That is, multiple Opus files may be combined into a single, longer file just + by concatenating the original files. + This is commonly done in internet radio streaming, as it allows the title + and artist to be updated each time the song changes, since each link in the + chain includes its own set of metadata. + + libopusfile fully supports chained files. + It will decode the first Opus stream found in each link of a chained file + (ignoring any other streams that might be concurrently multiplexed with it, + such as a video stream). + + The channel count can also change between links. + If your application is not prepared to deal with this, it can use the stereo + API to ensure the audio from all links will always get decoded into a + common format. + Since libopusfile always decodes to 48 kHz, you do not have to + worry about the sample rate changing between links (as was possible with + Vorbis). + This makes application support for chained files with libopusfile + very easy.*/ # if defined(__cplusplus) extern "C" { # endif +# include # include # include # include +/**@cond PRIVATE*/ + /*Enable special features for gcc and gcc-compatible compilers.*/ # if !defined(OP_GNUC_PREREQ) # if defined(__GNUC__)&&defined(__GNUC_MINOR__) @@ -75,9 +125,12 @@ extern "C" { # pragma GCC visibility push(default) # endif -typedef struct OpusHead OpusHead; -typedef struct OpusTags OpusTags; -typedef struct OggOpusFile OggOpusFile; +typedef struct OpusHead OpusHead; +typedef struct OpusTags OpusTags; +typedef struct OpusPictureTag OpusPictureTag; +typedef struct OpusServerInfo OpusServerInfo; +typedef struct OpusFileCallbacks OpusFileCallbacks; +typedef struct OggOpusFile OggOpusFile; /*Warning attributes for libopusfile functions.*/ # if OP_GNUC_PREREQ(3,4) @@ -91,6 +144,8 @@ typedef struct OggOpusFile OggOpusFile; # define OP_ARG_NONNULL(_x) # endif +/**@endcond*/ + /**\defgroup error_codes Error Codes*/ /*@{*/ /**\name List of possible error codes @@ -182,8 +237,9 @@ struct OpusHead{ opus_uint32 input_sample_rate; /**The gain to apply to the decoded output, in dB, as a Q8 value in the range -32768...32767. - The decoder will automatically scale the output by - pow(10,output_gain/(20.0*256)).*/ + The libopusfile API will automatically apply this gain to the + decoded output before returning it, scaling it by + pow(10,output_gain/(20.0*256)).*/ int output_gain; /**The channel mapping family, in the range 0...255. Channel mapping family 0 covers mono or stereo in a single stream. @@ -253,6 +309,87 @@ struct OpusTags{ char *vendor; }; +/**\name Picture tag image formats*/ +/*@{*/ + +/**The MIME type was not recognized, or the image data did not match the + declared MIME type.*/ +#define OP_PIC_FORMAT_UNKNOWN (-1) +/**The MIME type indicates the image data is really a URL.*/ +#define OP_PIC_FORMAT_URL (0) +/**The image is a JPEG.*/ +#define OP_PIC_FORMAT_JPEG (1) +/**The image is a PNG.*/ +#define OP_PIC_FORMAT_PNG (2) +/**The image is a GIF.*/ +#define OP_PIC_FORMAT_GIF (3) + +/*@}*/ + +/**The contents of a METADATA_BLOCK_PICTURE tag.*/ +struct OpusPictureTag{ + /**The picture type according to the ID3v2 APIC frame: +
    +
  1. Other
  2. +
  3. 32x32 pixels 'file icon' (PNG only)
  4. +
  5. Other file icon
  6. +
  7. Cover (front)
  8. +
  9. Cover (back)
  10. +
  11. Leaflet page
  12. +
  13. Media (e.g. label side of CD)
  14. +
  15. Lead artist/lead performer/soloist
  16. +
  17. Artist/performer
  18. +
  19. Conductor
  20. +
  21. Band/Orchestra
  22. +
  23. Composer
  24. +
  25. Lyricist/text writer
  26. +
  27. Recording Location
  28. +
  29. During recording
  30. +
  31. During performance
  32. +
  33. Movie/video screen capture
  34. +
  35. A bright colored fish
  36. +
  37. Illustration
  38. +
  39. Band/artist logotype
  40. +
  41. Publisher/Studio logotype
  42. +
+ Others are reserved and should not be used. + There may only be one each of picture type 1 and 2 in a file.*/ + opus_int32 type; + /**The MIME type of the picture, in printable ASCII characters 0x20-0x7E. + The MIME type may also be "-->" to signify that the data part + is a URL pointing to the picture instead of the picture data itself. + In this case, a terminating NUL is appended to the URL string in #data, + but #data_length is set to the length of the string excluding that + terminating NUL.*/ + char *mime_type; + /**The description of the picture, in UTF-8.*/ + char *description; + /**The width of the picture in pixels.*/ + opus_uint32 width; + /**The height of the picture in pixels.*/ + opus_uint32 height; + /**The color depth of the picture in bits-per-pixel (not + bits-per-channel).*/ + opus_uint32 depth; + /**For indexed-color pictures (e.g., GIF), the number of colors used, or 0 + for non-indexed pictures.*/ + opus_uint32 colors; + /**The length of the picture data in bytes.*/ + opus_uint32 data_length; + /**The binary picture data.*/ + unsigned char *data; + /**The format of the picture data, if known. + One of +
    +
  • #OP_PIC_FORMAT_UNKNOWN,
  • +
  • #OP_PIC_FORMAT_URL,
  • +
  • #OP_PIC_FORMAT_JPEG,
  • +
  • #OP_PIC_FORMAT_PNG, or
  • +
  • #OP_PIC_FORMAT_GIF.
  • +
*/ + int format; +}; + /**\name Functions for manipulating header data These functions manipulate the #OpusHead and #OpusTags structures, @@ -315,7 +452,7 @@ ogg_int64_t opus_granule_sample(const OpusHead *_head,ogg_int64_t _gp) for validity. \param[in] _data The contents of the 'comment' header packet. \param _len The number of bytes of data in the 'info' header packet. - \retval 0 Success. + \retval 0 Success. \retval #OP_ENOTFORMAT If the data does not start with the "OpusTags" string. \retval #OP_EBADHEADER If the contents of the packet otherwise violate the @@ -324,6 +461,15 @@ ogg_int64_t opus_granule_sample(const OpusHead *_head,ogg_int64_t _gp) OP_WARN_UNUSED_RESULT int opus_tags_parse(OpusTags *_tags, const unsigned char *_data,size_t _len) OP_ARG_NONNULL(2); +/**Performs a deep copy of an #OpusTags structure. + \param _dst The #OpusTags structure to copy into. + If this function fails, the contents of this structure remain + untouched. + \param _src The #OpusTags structure to copy from. + \retval 0 Success. + \retval #OP_EFAULT If there wasn't enough memory to copy the tags.*/ +int opus_tags_copy(OpusTags *_dst,const OpusTags *_src) OP_ARG_NONNULL(1); + /**Initializes an #OpusTags structure. This should be called on a freshly allocated #OpusTags structure before attempting to use it. @@ -385,6 +531,24 @@ const char *opus_tags_query(const OpusTags *_tags,const char *_tag,int _count) int opus_tags_query_count(const OpusTags *_tags,const char *_tag) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2); +/**Get the track gain from an R128_TRACK_GAIN tag, if one was specified. + This searches for the first R128_TRACK_GAIN tag with a valid signed, + 16-bit decimal integer value and returns the value. + This routine is exposed merely for convenience for applications which wish + to do something special with the track gain (i.e., display it). + If you simply wish to apply the track gain instead of the header gain, you + can use op_set_gain_offset() with an #OP_TRACK_GAIN type and no offset. + \param _tags An initialized #OpusTags structure. + \param[out] _gain_q8 The track gain, in 1/256ths of a dB. + This will lie in the range [-32768,32767], and should + be applied in addition to the header gain. + On error, no value is returned, and the previous + contents remain unchanged. + \return 0 on success, or a negative value on error. + \retval #OP_FALSE There was no track gain available in the given tags.*/ +int opus_tags_get_track_gain(const OpusTags *_tags,int *_gain_q8) + OP_ARG_NONNULL(1) OP_ARG_NONNULL(2); + /**Clears the #OpusTags structure. This should be called on an #OpusTags structure after it is no longer needed. @@ -392,6 +556,78 @@ int opus_tags_query_count(const OpusTags *_tags,const char *_tag) \param _tags The #OpusTags structure to clear.*/ void opus_tags_clear(OpusTags *_tags) OP_ARG_NONNULL(1); +/**Check if \a _comment is an instance of a \a _tag_name tag. + \see opus_tagncompare + \param _tag_name A NUL-terminated, case-insensitive, ASCII string containing + the name of the tag to check for (without the terminating + '=' character). + \param _comment The comment string to check. + \return An integer less than, equal to, or greater than zero if \a _comment + is found respectively, to be less than, to match, or be greater + than a "tag=value" string whose tag matches \a _tag_name.*/ +int opus_tagcompare(const char *_tag_name,const char *_comment); + +/**Check if \a _comment is an instance of a \a _tag_name tag. + This version is slightly more efficient than opus_tagcompare() if the length + of the tag name is already known (e.g., because it is a constant). + \see opus_tagcompare + \param _tag_name A case-insensitive ASCII string containing the name of the + tag to check for (without the terminating '=' character). + \param _tag_len The number of characters in the tag name. + This must be non-negative. + \param _comment The comment string to check. + \return An integer less than, equal to, or greater than zero if \a _comment + is found respectively, to be less than, to match, or be greater + than a "tag=value" string whose tag matches the first \a _tag_len + characters of \a _tag_name.*/ +int opus_tagncompare(const char *_tag_name,int _tag_len,const char *_comment); + +/**Parse a single METADATA_BLOCK_PICTURE tag. + This decodes the BASE64-encoded content of the tag and returns a structure + with the MIME type, description, image parameters (if known), and the + compressed image data. + If the MIME type indicates the presence of an image format we recognize + (JPEG, PNG, or GIF) and the actual image data contains the magic signature + associated with that format, then the OpusPictureTag::format field will be + set to the corresponding format. + This is provided as a convenience to avoid requiring applications to parse + the MIME type and/or do their own format detection for the commonly used + formats. + In this case, we also attempt to extract the image parameters directly from + the image data (overriding any that were present in the tag, which the + specification says applications are not meant to rely on). + The application must still provide its own support for actually decoding the + image data and, if applicable, retrieving that data from URLs. + \param[out] _pic Returns the parsed picture data. + No sanitation is done on the type, MIME type, or + description fields, so these might return invalid values. + The contents of this structure are left unmodified on + failure. + \param _tag The METADATA_BLOCK_PICTURE tag contents. + The leading "METADATA_BLOCK_PICTURE=" portion is optional, + to allow the function to be used on either directly on the + values in OpusTags::user_comments or on the return value + of opus_tags_query(). + \return 0 on success or a negative value on error. + \retval #OP_ENOTFORMAT The METADATA_BLOCK_PICTURE contents were not valid. + \retval #OP_EFAULT There was not enough memory to store the picture tag + contents.*/ +OP_WARN_UNUSED_RESULT int opus_picture_tag_parse(OpusPictureTag *_pic, + const char *_tag) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2); + +/**Initializes an #OpusPictureTag structure. + This should be called on a freshly allocated #OpusPictureTag structure + before attempting to use it. + \param _pic The #OpusPictureTag structure to initialize.*/ +void opus_picture_tag_init(OpusPictureTag *_pic) OP_ARG_NONNULL(1); + +/**Clears the #OpusPictureTag structure. + This should be called on an #OpusPictureTag structure after it is no longer + needed. + It will free all memory used by the structure members. + \param _pic The #OpusPictureTag structure to clear.*/ +void opus_picture_tag_clear(OpusPictureTag *_pic) OP_ARG_NONNULL(1); + /*@}*/ /*@}*/ @@ -408,6 +644,8 @@ void opus_tags_clear(OpusTags *_tags) OP_ARG_NONNULL(1); They may be expanded in the future.*/ /*@{*/ +/**@cond PRIVATE*/ + /*These are the raw numbers used to define the request codes. They should not be used directly.*/ #define OP_SSL_SKIP_CERTIFICATE_CHECK_REQUEST (6464) @@ -415,6 +653,7 @@ void opus_tags_clear(OpusTags *_tags) OP_ARG_NONNULL(1); #define OP_HTTP_PROXY_PORT_REQUEST (6592) #define OP_HTTP_PROXY_USER_REQUEST (6656) #define OP_HTTP_PROXY_PASS_REQUEST (6720) +#define OP_GET_SERVER_INFO_REQUEST (6784) #define OP_URL_OPT(_request) ((_request)+(char *)0) @@ -422,6 +661,64 @@ void opus_tags_clear(OpusTags *_tags) OP_ARG_NONNULL(1); provided to one of the URL options.*/ #define OP_CHECK_INT(_x) ((void)((_x)==(opus_int32)0),(opus_int32)(_x)) #define OP_CHECK_CONST_CHAR_PTR(_x) ((_x)+((_x)-(const char *)(_x))) +#define OP_CHECK_SERVER_INFO_PTR(_x) ((_x)+((_x)-(OpusServerInfo *)(_x))) + +/**@endcond*/ + +/**HTTP/Shoutcast/Icecast server information associated with a URL.*/ +struct OpusServerInfo{ + /**The name of the server (icy-name/ice-name). + This is NULL if there was no icy-name or + ice-name header.*/ + char *name; + /**A short description of the server (icy-description/ice-description). + This is NULL if there was no icy-description or + ice-description header.*/ + char *description; + /**The genre the server falls under (icy-genre/ice-genre). + This is NULL if there was no icy-genre or + ice-genre header.*/ + char *genre; + /**The homepage for the server (icy-url/ice-url). + This is NULL if there was no icy-url or + ice-url header.*/ + char *url; + /**The software used by the origin server (Server). + This is NULL if there was no Server header.*/ + char *server; + /**The media type of the entity sent to the recepient (Content-Type). + This is NULL if there was no Content-Type + header.*/ + char *content_type; + /**The nominal stream bitrate in kbps (icy-br/ice-bitrate). + This is -1 if there was no icy-br or + ice-bitrate header.*/ + opus_int32 bitrate_kbps; + /**Flag indicating whether the server is public (1) or not + (0) (icy-pub/ice-public). + This is -1 if there was no icy-pub or + ice-public header.*/ + int is_public; + /**Flag indicating whether the server is using HTTPS instead of HTTP. + This is 0 unless HTTPS is being used. + This may not match the protocol used in the original URL if there were + redirections.*/ + int is_ssl; +}; + +/**Initializes an #OpusServerInfo structure. + All fields are set as if the corresponding header was not available. + \param _info The #OpusServerInfo structure to initialize. + \note If you use this function, you must link against libopusurl.*/ +void opus_server_info_init(OpusServerInfo *_info) OP_ARG_NONNULL(1); + +/**Clears the #OpusServerInfo structure. + This should be called on an #OpusServerInfo structure after it is no longer + needed. + It will free all memory used by the structure members. + \param _info The #OpusServerInfo structure to clear. + \note If you use this function, you must link against libopusurl.*/ +void opus_server_info_clear(OpusServerInfo *_info) OP_ARG_NONNULL(1); /**Skip the certificate check when connecting via TLS/SSL (https). \param _b opus_int32: Whether or not to skip the certificate @@ -467,7 +764,7 @@ void opus_tags_clear(OpusTags *_tags) OP_ARG_NONNULL(1); arguments. \hideinitializer*/ #define OP_HTTP_PROXY_USER(_user) \ - OP_URL_OPT(OP_HTTP_PROXY_USER_REQUEST),OP_CHECK_CONST_CHAR_PTR(_host) + OP_URL_OPT(OP_HTTP_PROXY_USER_REQUEST),OP_CHECK_CONST_CHAR_PTR(_user) /**Use the given password for authentication when proxying connections. All proxy parameters are ignored for non-http and non-https URLs. @@ -480,7 +777,28 @@ void opus_tags_clear(OpusTags *_tags) OP_ARG_NONNULL(1); arguments. \hideinitializer*/ #define OP_HTTP_PROXY_PASS(_pass) \ - OP_URL_OPT(OP_HTTP_PROXY_PASS_REQUEST),OP_CHECK_CONST_CHAR_PTR(_host) + OP_URL_OPT(OP_HTTP_PROXY_PASS_REQUEST),OP_CHECK_CONST_CHAR_PTR(_pass) + +/**Parse information about the streaming server (if any) and return it. + Very little validation is done. + In particular, OpusServerInfo::url may not be a valid URL, + OpusServerInfo::bitrate_kbps may not really be in kbps, and + OpusServerInfo::content_type may not be a valid MIME type. + The character set of the string fields is not specified anywhere, and should + not be assumed to be valid UTF-8. + \param _info OpusServerInfo *: Returns information about the server. + If there is any error opening the stream, the + contents of this structure remain + unmodified. + On success, fills in the structure with the + server information that was available, if + any. + After a successful return, the contents of + this structure should be freed by calling + opus_server_info_clear(). + \hideinitializer*/ +#define OP_GET_SERVER_INFO(_info) \ + OP_URL_OPT(OP_GET_SERVER_INFO_REQUEST),OP_CHECK_SERVER_INFO_PTR(_info) /*@}*/ /*@}*/ @@ -497,8 +815,6 @@ void opus_tags_clear(OpusTags *_tags) OP_ARG_NONNULL(1); block of memory, or URLs.*/ /*@{*/ -typedef struct OpusFileCallbacks OpusFileCallbacks; - /**Reads up to \a _nbytes bytes of data from \a _stream. \param _stream The stream to read from. \param[out] _ptr The buffer to store the data in. @@ -564,6 +880,10 @@ struct OpusFileCallbacks{ If there is an error opening the file, nothing will be filled in here. \param _path The path to the file to open. + On Windows, this string must be UTF-8 (to allow access to + files whose names cannot be represented in the current + MBCS code page). + All other systems use the native character encoding. \param _mode The mode to open the file in. \return A stream handle to use with the callbacks, or NULL on error.*/ @@ -597,6 +917,10 @@ OP_WARN_UNUSED_RESULT void *op_fdopen(OpusFileCallbacks *_cb, If there is an error opening the file, nothing will be filled in here. \param _path The path to the file to open. + On Windows, this string must be UTF-8 (to allow access + to files whose names cannot be represented in the + current MBCS code page). + All other systems use the native character encoding. \param _mode The mode to open the file in. \param _stream A stream previously returned by op_fopen(), op_fdopen(), or op_freopen(). @@ -624,6 +948,7 @@ OP_WARN_UNUSED_RESULT void *op_mem_stream_create(OpusFileCallbacks *_cb, takes a va_list instead of a variable number of arguments. It does not call the va_end macro, and because it invokes the va_arg macro, the value of \a _ap is undefined after the call. + \note If you use this function, you must link against libopusurl. \param[out] _cb The callbacks to use for this stream. If there is an error creating the stream, nothing will be filled in here. @@ -632,6 +957,10 @@ OP_WARN_UNUSED_RESULT void *op_mem_stream_create(OpusFileCallbacks *_cb, schemes are supported. Both and may be disabled at compile time, in which case opening such URLs will always fail. + Currently this only supports URIs. + IRIs should be converted to UTF-8 and URL-escaped, with + internationalized domain names encoded in punycode, + before passing them to this function. \param[in,out] _ap A list of the \ref url_options "optional flags" to use. This is a variable-length list of options terminated with NULL. @@ -640,7 +969,8 @@ OP_WARN_UNUSED_RESULT void *op_mem_stream_create(OpusFileCallbacks *_cb, OP_WARN_UNUSED_RESULT void *op_url_stream_vcreate(OpusFileCallbacks *_cb, const char *_url,va_list _ap) OP_ARG_NONNULL(1) OP_ARG_NONNULL(2); -/**Creates a stream that reads from the given URL using the specified proxy. +/**Creates a stream that reads from the given URL. + \note If you use this function, you must link against libopusurl. \param[out] _cb The callbacks to use for this stream. If there is an error creating the stream, nothing will be filled in here. @@ -649,6 +979,10 @@ OP_WARN_UNUSED_RESULT void *op_url_stream_vcreate(OpusFileCallbacks *_cb, are supported. Both and may be disabled at compile time, in which case opening such URLs will always fail. + Currently this only supports URIs. + IRIs should be converted to UTF-8 and URL-escaped, with + internationalized domain names encoded in punycode, before + passing them to this function. \param ... The \ref url_options "optional flags" to use. This is a variable-length list of options terminated with NULL. @@ -731,12 +1065,17 @@ OP_WARN_UNUSED_RESULT OggOpusFile *op_open_memory(const unsigned char *_data, takes a va_list instead of a variable number of arguments. It does not call the va_end macro, and because it invokes the va_arg macro, the value of \a _ap is undefined after the call. + \note If you use this function, you must link against libopusurl. \param _url The URL to open. Currently only the , , and schemes are supported. Both and may be disabled at compile time, in which case opening such URLs will always fail. + Currently this only supports URIs. + IRIs should be converted to UTF-8 and URL-escaped, + with internationalized domain names encoded in + punycode, before passing them to this function. \param[out] _error Returns 0 on success, or a failure code on error. You may pass in NULL if you don't want the failure code. @@ -751,13 +1090,16 @@ OP_WARN_UNUSED_RESULT OggOpusFile *op_vopen_url(const char *_url, int *_error,va_list _ap) OP_ARG_NONNULL(1); /**Open a stream from a URL. - However, this approach will not work for live streams or HTTP/1.0 servers - (which do not support Range requets). + \note If you use this function, you must link against libopusurl. \param _url The URL to open. Currently only the , , and schemes are supported. Both and may be disabled at compile time, in which case opening such URLs will always fail. + Currently this only supports URIs. + IRIs should be converted to UTF-8 and URL-escaped, with + internationalized domain names encoded in punycode, + before passing them to this function. \param[out] _error Returns 0 on success, or a failure code on error. You may pass in NULL if you don't want the failure code. @@ -841,7 +1183,11 @@ OP_WARN_UNUSED_RESULT OggOpusFile *op_open_url(const char *_url,
The first or last timestamp in a link failed basic validity checks.
- \return A freshly opened \c OggOpusFile, or NULL on error.*/ + \return A freshly opened \c OggOpusFile, or NULL on error. + libopusfile does not take ownership of the source + if the call fails. + The calling application is responsible for closing the source if + this call returns an error.*/ OP_WARN_UNUSED_RESULT OggOpusFile *op_open_callbacks(void *_source, const OpusFileCallbacks *_cb,const unsigned char *_initial_data, size_t _initial_bytes,int *_error) OP_ARG_NONNULL(2); @@ -876,6 +1222,7 @@ OP_WARN_UNUSED_RESULT OggOpusFile *op_test_memory(const unsigned char *_data, takes a va_list instead of a variable number of arguments. It does not call the va_end macro, and because it invokes the va_arg macro, the value of \a _ap is undefined after the call. + \note If you use this function, you must link against libopusurl. \see op_test_url \see op_test_callbacks \param _url The URL to open. @@ -884,6 +1231,10 @@ OP_WARN_UNUSED_RESULT OggOpusFile *op_test_memory(const unsigned char *_data, Both and may be disabled at compile time, in which case opening such URLs will always fail. + Currently this only supports URIs. + IRIs should be converted to UTF-8 and URL-escaped, + with internationalized domain names encoded in + punycode, before passing them to this function. \param[out] _error Returns 0 on success, or a failure code on error. You may pass in NULL if you don't want the failure code. @@ -898,12 +1249,17 @@ OP_WARN_UNUSED_RESULT OggOpusFile *op_vtest_url(const char *_url, int *_error,va_list _ap) OP_ARG_NONNULL(1); /**Partially open a stream from a URL. + \note If you use this function, you must link against libopusurl. \see op_test_callbacks \param _url The URL to open. Currently only the , , and schemes are supported. Both and may be disabled at compile time, in which case opening such URLs will always fail. + Currently this only supports URIs. + IRIs should be converted to UTF-8 and URL-escaped, with + internationalized domain names encoded in punycode, + before passing them to this function. \param[out] _error Returns 0 on success, or a failure code on error. You may pass in NULL if you don't want the failure code. @@ -974,7 +1330,11 @@ OP_WARN_UNUSED_RESULT OggOpusFile *op_test_url(const char *_url, the failure code. See op_open_callbacks() for a full list of failure codes. - \return A partially opened \c OggOpusFile, or NULL on error.*/ + \return A partially opened \c OggOpusFile, or NULL on error. + libopusfile does not take ownership of the source + if the call fails. + The calling application is responsible for closing the source if + this call returns an error.*/ OP_WARN_UNUSED_RESULT OggOpusFile *op_test_callbacks(void *_source, const OpusFileCallbacks *_cb,const unsigned char *_initial_data, size_t _initial_bytes,int *_error) OP_ARG_NONNULL(2); @@ -1046,7 +1406,7 @@ void op_free(OggOpusFile *_of); This function may be called on partially-opened streams. \param _of The \c OggOpusFile whose seekable status is to be returned. \return A non-zero value if seekable, and 0 if unseekable.*/ -int op_seekable(OggOpusFile *_of) OP_ARG_NONNULL(1); +int op_seekable(const OggOpusFile *_of) OP_ARG_NONNULL(1); /**Returns the number of links in this chained stream. This function may be called on partially-opened streams, but it will always @@ -1054,9 +1414,9 @@ int op_seekable(OggOpusFile *_of) OP_ARG_NONNULL(1); The actual number of links is not known until the stream is fully opened. \param _of The \c OggOpusFile from which to retrieve the link count. \return For fully-open seekable sources, this returns the total number of - links in the whole stream. + links in the whole stream, which will be at least 1. For partially-open or unseekable sources, this always returns 1.*/ -int op_link_count(OggOpusFile *_of) OP_ARG_NONNULL(1); +int op_link_count(const OggOpusFile *_of) OP_ARG_NONNULL(1); /**Get the serial number of the given link in a (possibly-chained) Ogg Opus stream. @@ -1071,7 +1431,7 @@ int op_link_count(OggOpusFile *_of) OP_ARG_NONNULL(1); the serial number of the last link. If the source is not seekable, this always returns the serial number of the current link.*/ -opus_uint32 op_serialno(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); +opus_uint32 op_serialno(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); /**Get the channel count of the given link in a (possibly-chained) Ogg Opus stream. @@ -1088,7 +1448,7 @@ opus_uint32 op_serialno(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); the channel count of the last link. If the source is not seekable, this always returns the channel count of the current link.*/ -int op_channel_count(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); +int op_channel_count(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); /**Get the total (compressed) size of the stream, or of an individual link in a (possibly-chained) Ogg Opus stream, including all headers and Ogg muxing @@ -1106,7 +1466,7 @@ int op_channel_count(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); \retval #OP_EINVAL The source is not seekable (so we can't know the length), \a _li wasn't less than the total number of links in the stream, or the stream was only partially open.*/ -opus_int64 op_raw_total(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); +opus_int64 op_raw_total(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); /**Get the total PCM length (number of samples at 48 kHz) of the stream, or of an individual link in a (possibly-chained) Ogg Opus stream. @@ -1124,7 +1484,7 @@ opus_int64 op_raw_total(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); \retval #OP_EINVAL The source is not seekable (so we can't know the length), \a _li wasn't less than the total number of links in the stream, or the stream was only partially open.*/ -ogg_int64_t op_pcm_total(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); +ogg_int64_t op_pcm_total(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); /**Get the ID header information for the given link in a (possibly chained) Ogg Opus stream. @@ -1140,7 +1500,7 @@ ogg_int64_t op_pcm_total(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); information for the current link is always returned, if available. \return The contents of the ID header for the given link.*/ -const OpusHead *op_head(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); +const OpusHead *op_head(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); /**Get the comment header information for the given link in a (possibly chained) Ogg Opus stream. @@ -1158,7 +1518,7 @@ const OpusHead *op_head(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); \return The contents of the comment header for the given link, or NULL if this is an unseekable stream that encountered an invalid link.*/ -const OpusTags *op_tags(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); +const OpusTags *op_tags(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); /**Retrieve the index of the current link. This is the link that produced the data most recently read by @@ -1175,7 +1535,7 @@ const OpusTags *op_tags(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); each time a new link is encountered (even though op_link_count() always returns 1). \retval #OP_EINVAL The stream was only partially open.*/ -int op_current_link(OggOpusFile *_of) OP_ARG_NONNULL(1); +int op_current_link(const OggOpusFile *_of) OP_ARG_NONNULL(1); /**Computes the bitrate for a given link in a (possibly chained) Ogg Opus stream. @@ -1188,7 +1548,7 @@ int op_current_link(OggOpusFile *_of) OP_ARG_NONNULL(1); \retval #OP_EINVAL The stream was only partially open, the stream was not seekable, or \a _li was larger than the number of links.*/ -opus_int32 op_bitrate(OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); +opus_int32 op_bitrate(const OggOpusFile *_of,int _li) OP_ARG_NONNULL(1); /**Compute the instantaneous bitrate, measured as the ratio of bits to playable samples decoded since a) the last call to op_bitrate_instant(), b) the last @@ -1207,7 +1567,7 @@ opus_int32 op_bitrate_instant(OggOpusFile *_of) OP_ARG_NONNULL(1); \param _of The \c OggOpusFile from which to retrieve the position indicator. \return The byte position that is currently being read from. \retval #OP_EINVAL The stream was only partially open.*/ -opus_int64 op_raw_tell(OggOpusFile *_of) OP_ARG_NONNULL(1); +opus_int64 op_raw_tell(const OggOpusFile *_of) OP_ARG_NONNULL(1); /**Obtain the PCM offset of the next sample to be read. If the stream is not properly timestamped, this might not increment by the @@ -1216,7 +1576,7 @@ opus_int64 op_raw_tell(OggOpusFile *_of) OP_ARG_NONNULL(1); \param _of The \c OggOpusFile from which to retrieve the PCM offset. \return The PCM offset of the next sample to be read. \retval #OP_EINVAL The stream was only partially open.*/ -ogg_int64_t op_pcm_tell(OggOpusFile *_of) OP_ARG_NONNULL(1); +ogg_int64_t op_pcm_tell(const OggOpusFile *_of) OP_ARG_NONNULL(1); /*@}*/ /*@}*/ @@ -1303,11 +1663,12 @@ int op_pcm_seek(OggOpusFile *_of,ogg_int64_t _pcm_offset) OP_ARG_NONNULL(1); clipping and other issues which might be avoided entirely if, e.g., you scale down the volume at some other stage. However, if you intend to direct consume 16-bit samples, the conversion in - libopusfile provides noise-shaping dithering API. + libopusfile provides noise-shaping dithering and, if compiled + against libopus 1.1 or later, soft-clipping prevention. libopusfile can also be configured at compile time to use the fixed-point libopus API. - If so, the floating-point API may also be disabled. + If so, libopusfile's floating-point API may also be disabled. In that configuration, nothing in libopusfile will use any floating-point operations, to simplify support on devices without an adequate FPU. @@ -1316,20 +1677,125 @@ int op_pcm_seek(OggOpusFile *_of,ogg_int64_t _pcm_offset) OP_ARG_NONNULL(1); not check the error return code from op_read_float() or its associated functions. If the remote peer does not close the connection gracefully (with a TLS - "close notify" message), these functions will return OP_EREAD instead of 0 + "close notify" message), these functions will return #OP_EREAD instead of 0 when they reach the end of the file. If you are reading from an URL (particularly if seeking is not supported), you should make sure to check for this error and warn the user appropriately.*/ /*@{*/ +/**Indicates that the decoding callback should produce signed 16-bit + native-endian output samples.*/ +#define OP_DEC_FORMAT_SHORT (7008) +/**Indicates that the decoding callback should produce 32-bit native-endian + float samples.*/ +#define OP_DEC_FORMAT_FLOAT (7040) + +/**Indicates that the decoding callback did not decode anything, and that + libopusfile should decode normally instead.*/ +#define OP_DEC_USE_DEFAULT (6720) + +/**Called to decode an Opus packet. + This should invoke the functional equivalent of opus_multistream_decode() or + opus_multistream_decode_float(), except that it returns 0 on success + instead of the number of decoded samples (which is known a priori). + \param _ctx The application-provided callback context. + \param _decoder The decoder to use to decode the packet. + \param[out] _pcm The buffer to decode into. + This will always have enough room for \a _nchannels of + \a _nsamples samples, which should be placed into this + buffer interleaved. + \param _op The packet to decode. + This will always have its granule position set to a valid + value. + \param _nsamples The number of samples expected from the packet. + \param _nchannels The number of channels expected from the packet. + \param _format The desired sample output format. + This is either #OP_DEC_FORMAT_SHORT or + #OP_DEC_FORMAT_FLOAT. + \param _li The index of the link from which this packet was decoded. + \return A non-negative value on success, or a negative value on error. + The error codes should be the same as those returned by + opus_multistream_decode() or opus_multistream_decode_float(). + \retval 0 Decoding was successful. + The application has filled the buffer with + exactly \a _nsamples*\a + _nchannels samples in the requested + format. + \retval #OP_DEC_USE_DEFAULT No decoding was done. + libopusfile should decode normally + instead.*/ +typedef int (*op_decode_cb_func)(void *_ctx,OpusMSDecoder *_decoder,void *_pcm, + const ogg_packet *_op,int _nsamples,int _nchannels,int _format,int _li); + +/**Sets the packet decode callback function. + This is called once for each packet that needs to be decoded. + A call to this function is no guarantee that the audio will eventually be + delivered to the application. + Some or all of the data from the packet may be discarded (i.e., at the + beginning or end of a link, or after a seek), however the callback is + required to provide all of it. + \param _of The \c OggOpusFile on which to set the decode callback. + \param _decode_cb The callback function to call. + This may be NULL to disable calling the + callback. + \param _ctx The application-provided context pointer to pass to the + callback on each call.*/ +void op_set_decode_callback(OggOpusFile *_of, + op_decode_cb_func _decode_cb,void *_ctx) OP_ARG_NONNULL(1); + +/**Gain offset type that indicates that the provided offset is relative to the + header gain. + This is the default.*/ +#define OP_HEADER_GAIN (0) + +/**Gain offset type that indicates that the provided offset is relative to the + R128_TRACK_GAIN value (if any), in addition to the header gain.*/ +#define OP_TRACK_GAIN (3008) + +/**Gain offset type that indicates that the provided offset should be used as + the gain directly, without applying any the header or track gains.*/ +#define OP_ABSOLUTE_GAIN (3009) + +/**Sets the gain to be used for decoded output. + By default, the gain in the header is applied with no additional offset. + The total gain (including header gain and/or track gain, if applicable, and + this offset), will be clamped to [-32768,32767]/256 dB. + This is more than enough to saturate or underflow 16-bit PCM. + \note The new gain will not be applied to any already buffered, decoded + output. + This means you cannot change it sample-by-sample, as at best it will be + updated packet-by-packet. + It is meant for setting a target volume level, rather than applying smooth + fades, etc. + \param _of The \c OggOpusFile on which to set the gain offset. + \param _gain_type One of #OP_HEADER_GAIN, #OP_TRACK_GAIN, or + #OP_ABSOLUTE_GAIN. + \param _gain_offset_q8 The gain offset to apply, in 1/256ths of a dB. + \return 0 on success or a negative value on error. + \retval #OP_EINVAL The \a _gain_type was unrecognized.*/ +int op_set_gain_offset(OggOpusFile *_of, + int _gain_type,opus_int32 _gain_offset_q8) OP_ARG_NONNULL(1); + +/**Sets whether or not dithering is enabled for 16-bit decoding. + By default, when libopusfile is compiled to use floating-point + internally, calling op_read() or op_read_stereo() will first decode to + float, and then convert to fixed-point using noise-shaping dithering. + This flag can be used to disable that dithering. + When the application uses op_read_float() or op_read_float_stereo(), or when + the library has been compiled to decode directly to fixed point, this flag + has no effect. + \param _of The \c OggOpusFile on which to enable or disable dithering. + \param _enabled A non-zero value to enable dithering, or 0 to disable it.*/ +void op_set_dither_enabled(OggOpusFile *_of,int _enabled) OP_ARG_NONNULL(1); + /**Reads more samples from the stream. \note Although \a _buf_size must indicate the total number of values that can be stored in \a _pcm, the return value is the number of samples per channel. This is done because
    -
  1. The channel count cannot be known a prior (reading more samples might +
  2. The channel count cannot be known a priori (reading more samples might advance us into the next link, with a different channel count), so \a _buf_size cannot also be in units of samples per channel,
  3. Returning the samples per channel matches the libopus API @@ -1346,14 +1812,14 @@ int op_pcm_seek(OggOpusFile *_of,ogg_int64_t _pcm_offset) OP_ARG_NONNULL(1);
\param _of The \c OggOpusFile from which to read. \param[out] _pcm A buffer in which to store the output PCM samples, as - signed native-endian 16-bit values with a nominal - range of [-32768,32767). + signed native-endian 16-bit values at 48 kHz + with a nominal range of [-32768,32767). Multiple channels are interleaved using the Vorbis channel ordering. This must have room for at least \a _buf_size values. \param _buf_size The number of values that can be stored in \a _pcm. - It is reccommended that this be large enough for at + It is recommended that this be large enough for at least 120 ms of data at 48 kHz per channel (5760 values per channel). Smaller buffers will simply return less data, possibly @@ -1411,7 +1877,7 @@ OP_WARN_UNUSED_RESULT int op_read(OggOpusFile *_of, can be stored in \a _pcm, the return value is the number of samples per channel.
    -
  1. The channel count cannot be known a prior (reading more samples might +
  2. The channel count cannot be known a priori (reading more samples might advance us into the next link, with a different channel count), so \a _buf_size cannot also be in units of samples per channel,
  3. Returning the samples per channel matches the libopus API @@ -1428,14 +1894,14 @@ OP_WARN_UNUSED_RESULT int op_read(OggOpusFile *_of,
\param _of The \c OggOpusFile from which to read. \param[out] _pcm A buffer in which to store the output PCM samples as - signed floats with a nominal range of + signed floats at 48 kHz with a nominal range of [-1.0,1.0]. Multiple channels are interleaved using the Vorbis channel ordering. This must have room for at least \a _buf_size floats. \param _buf_size The number of floats that can be stored in \a _pcm. - It is reccommended that this be large enough for at + It is recommended that this be large enough for at least 120 ms of data at 48 kHz per channel (5760 samples per channel). Smaller buffers will simply return less data, possibly @@ -1497,13 +1963,13 @@ OP_WARN_UNUSED_RESULT int op_read_float(OggOpusFile *_of, op_read(). \param _of The \c OggOpusFile from which to read. \param[out] _pcm A buffer in which to store the output PCM samples, as - signed native-endian 16-bit values with a nominal - range of [-32768,32767). + signed native-endian 16-bit values at 48 kHz + with a nominal range of [-32768,32767). The left and right channels are interleaved in the buffer. This must have room for at least \a _buf_size values. \param _buf_size The number of values that can be stored in \a _pcm. - It is reccommended that this be large enough for at + It is recommended that this be large enough for at least 120 ms of data at 48 kHz per channel (11520 values total). Smaller buffers will simply return less data, possibly @@ -1558,13 +2024,13 @@ OP_WARN_UNUSED_RESULT int op_read_stereo(OggOpusFile *_of, op_read_float(). \param _of The \c OggOpusFile from which to read. \param[out] _pcm A buffer in which to store the output PCM samples, as - signed floats with a nominal range of + signed floats at 48 kHz with a nominal range of [-1.0,1.0]. The left and right channels are interleaved in the buffer. This must have room for at least \a _buf_size values. \param _buf_size The number of values that can be stored in \a _pcm. - It is reccommended that this be large enough for at + It is recommended that this be large enough for at least 120 ms of data at 48 kHz per channel (11520 values total). Smaller buffers will simply return less data, possibly diff --git a/code/opusfile-0.2/src/http.c b/code/opusfile-0.5/src/http.c similarity index 86% rename from code/opusfile-0.2/src/http.c rename to code/opusfile-0.5/src/http.c index 09580c6f..4a9eaf59 100644 --- a/code/opusfile-0.2/src/http.c +++ b/code/opusfile-0.5/src/http.c @@ -9,6 +9,10 @@ * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * * * ********************************************************************/ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include "internal.h" #include #include @@ -40,7 +44,8 @@ RFC 6066: Transport Layer Security (TLS) Extensions: Extension Definitions RFC 6125: Representation and Verification of Domain-Based Application Service Identity within Internet Public Key Infrastructure Using X.509 (PKIX) - Certificates in the Context of Transport Layer Security (TLS)*/ + Certificates in the Context of Transport Layer Security (TLS) + RFC 6555: Happy Eyeballs: Success with Dual-Stack Hosts*/ typedef struct OpusParsedURL OpusParsedURL; typedef struct OpusStringBuf OpusStringBuf; @@ -56,7 +61,7 @@ static char *op_string_range_dup(const char *_start,const char *_end){ if(OP_UNLIKELY(len>=INT_MAX))return NULL; ret=(char *)_ogg_malloc(sizeof(*ret)*(len+1)); if(OP_LIKELY(ret!=NULL)){ - memcpy(ret,_start,sizeof(*ret)*(len)); + ret=(char *)memcpy(ret,_start,sizeof(*ret)*(len)); ret[len]='\0'; } return ret; @@ -205,18 +210,144 @@ static const char *op_parse_file_url(const char *_src){ } #if defined(OP_ENABLE_HTTP) -# include -# include -# include +# if defined(_WIN32) +# include +# include +# include +# include "winerrno.h" + +typedef SOCKET op_sock; + +# define OP_INVALID_SOCKET (INVALID_SOCKET) + +/*Vista and later support WSAPoll(), but we don't want to rely on that. + Instead we re-implement it badly using select(). + Unfortunately, they define a conflicting struct pollfd, so we only define our + own if it looks like that one has not already been defined.*/ +# if !defined(POLLIN) +/*Equivalent to POLLIN.*/ +# define POLLRDNORM (0x0100) +/*Priority band data can be read.*/ +# define POLLRDBAND (0x0200) +/*There is data to read.*/ +# define POLLIN (POLLRDNORM|POLLRDBAND) +/* There is urgent data to read.*/ +# define POLLPRI (0x0400) +/*Equivalent to POLLOUT.*/ +# define POLLWRNORM (0x0010) +/*Writing now will not block.*/ +# define POLLOUT (POLLWRNORM) +/*Priority data may be written.*/ +# define POLLWRBAND (0x0020) +/*Error condition (output only).*/ +# define POLLERR (0x0001) +/*Hang up (output only).*/ +# define POLLHUP (0x0002) +/*Invalid request: fd not open (output only).*/ +# define POLLNVAL (0x0004) + +struct pollfd{ + /*File descriptor.*/ + op_sock fd; + /*Requested events.*/ + short events; + /*Returned events.*/ + short revents; +}; +# endif + +/*But Winsock never defines nfds_t (it's simply hard-coded to ULONG).*/ +typedef unsigned long nfds_t; + +/*The usage of FD_SET() below is O(N^2). + This is okay because select() is limited to 64 sockets in Winsock, anyway. + In practice, we only ever call it with one or two sockets.*/ +static int op_poll_win32(struct pollfd *_fds,nfds_t _nfds,int _timeout){ + struct timeval tv; + fd_set ifds; + fd_set ofds; + fd_set efds; + nfds_t i; + int ret; + FD_ZERO(&ifds); + FD_ZERO(&ofds); + FD_ZERO(&efds); + for(i=0;i<_nfds;i++){ + _fds[i].revents=0; + if(_fds[i].events&POLLIN)FD_SET(_fds[i].fd,&ifds); + if(_fds[i].events&POLLOUT)FD_SET(_fds[i].fd,&ofds); + FD_SET(_fds[i].fd,&efds); + } + if(_timeout>=0){ + tv.tv_sec=_timeout/1000; + tv.tv_usec=(_timeout%1000)*1000; + } + ret=select(-1,&ifds,&ofds,&efds,_timeout<0?NULL:&tv); + if(ret>0){ + for(i=0;i<_nfds;i++){ + if(FD_ISSET(_fds[i].fd,&ifds))_fds[i].revents|=POLLIN; + if(FD_ISSET(_fds[i].fd,&ofds))_fds[i].revents|=POLLOUT; + /*This isn't correct: there are several different things that might have + happened to a fd in efds, but I don't know a good way to distinguish + them without more context from the caller. + It's okay, because we don't actually check any of these bits, we just + need _some_ bit set.*/ + if(FD_ISSET(_fds[i].fd,&efds))_fds[i].revents|=POLLHUP; + } + } + return ret; +} + +/*We define op_errno() to make it clear that it's not an l-value like normal + errno is.*/ +# define op_errno() (WSAGetLastError()?WSAGetLastError()-WSABASEERR:0) +# define op_reset_errno() (WSASetLastError(0)) + +/*The remaining functions don't get an op_ prefix even though they only + operate on sockets, because we don't use non-socket I/O here, and this + minimizes the changes needed to deal with Winsock.*/ +# define close(_fd) closesocket(_fd) +/*This relies on sizeof(u_long)==sizeof(int), which is always true on both + Win32 and Win64.*/ +# define ioctl(_fd,_req,_arg) ioctlsocket(_fd,_req,(u_long *)(_arg)) +# define getsockopt(_fd,_level,_name,_val,_len) \ + getsockopt(_fd,_level,_name,(char *)(_val),_len) +# define setsockopt(_fd,_level,_name,_val,_len) \ + setsockopt(_fd,_level,_name,(const char *)(_val),_len) +# define poll(_fds,_nfds,_timeout) op_poll_win32(_fds,_nfds,_timeout) + +# if defined(_MSC_VER) +typedef ptrdiff_t ssize_t; +# endif + +/*Load certificates from the built-in certificate store.*/ +int SSL_CTX_set_default_verify_paths_win32(SSL_CTX *_ssl_ctx); +# define SSL_CTX_set_default_verify_paths \ + SSL_CTX_set_default_verify_paths_win32 + +# else +/*Normal Berkeley sockets.*/ +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +typedef int op_sock; + +# define OP_INVALID_SOCKET (-1) + +# define op_errno() (errno) +# define op_reset_errno() (errno=0) + +# endif # include -# include -# include -# include -# include -# include -# include -# include -# include # include /*The maximum number of simultaneous connections. @@ -228,6 +359,11 @@ static const char *op_parse_file_url(const char *_src){ when seeking, and time out rapidly.*/ # define OP_NCONNS_MAX (4) +/*The amount of time before we attempt to re-resolve the host. + This is 10 minutes, as recommended in RFC 6555 for expiring cached connection + results for dual-stack hosts.*/ +# define OP_RESOLVE_CACHE_TIMEOUT_MS (10*60*(opus_int32)1000) + /*The number of redirections at which we give up. The value here is the current default in Firefox. RFC 2068 mandated a maximum of 5, but RFC 2616 relaxed that to "a client @@ -486,6 +622,8 @@ static void op_sb_clear(OpusStringBuf *_sb){ _ogg_free(_sb->buf); } +/*Make sure we have room for at least _capacity characters (plus 1 more for the + terminating NUL).*/ static int op_sb_ensure_capacity(OpusStringBuf *_sb,int _capacity){ char *buf; int cbuf; @@ -503,6 +641,8 @@ static int op_sb_ensure_capacity(OpusStringBuf *_sb,int _capacity){ return 0; } +/*Increase the capacity of the buffer, but not to more than _max_size + characters (plus 1 more for the terminating NUL).*/ static int op_sb_grow(OpusStringBuf *_sb,int _max_size){ char *buf; int cbuf; @@ -581,27 +721,33 @@ static struct addrinfo *op_resolve(const char *_host,unsigned _port){ char service[6]; memset(&hints,0,sizeof(hints)); hints.ai_socktype=SOCK_STREAM; +#if !defined(_WIN32) hints.ai_flags=AI_NUMERICSERV; +#endif OP_ASSERT(_port<=65535U); sprintf(service,"%u",_port); if(OP_LIKELY(!getaddrinfo(_host,service,&hints,&addrs)))return addrs; return NULL; } -static int op_sock_set_nonblocking(int _fd,int _nonblocking){ +static int op_sock_set_nonblocking(op_sock _fd,int _nonblocking){ +#if !defined(_WIN32) int flags; flags=fcntl(_fd,F_GETFL); if(OP_UNLIKELY(flags<0))return flags; if(_nonblocking)flags|=O_NONBLOCK; else flags&=~O_NONBLOCK; return fcntl(_fd,F_SETFL,flags); +#else + return ioctl(_fd,FIONBIO,&_nonblocking); +#endif } /*Disable/enable write coalescing if we can. We always send whole requests at once and always parse the response headers before sending another one, so normally write coalescing just causes added delay.*/ -static void op_sock_set_tcp_nodelay(int _fd,int _nodelay){ +static void op_sock_set_tcp_nodelay(op_sock _fd,int _nodelay){ # if defined(TCP_NODELAY)&&(defined(IPPROTO_TCP)||defined(SOL_TCP)) # if defined(IPPROTO_TCP) # define OP_SO_LEVEL IPPROTO_TCP @@ -615,6 +761,14 @@ static void op_sock_set_tcp_nodelay(int _fd,int _nodelay){ # endif } +#if defined(_WIN32) +static void op_init_winsock(){ + static LONG count; + static WSADATA wsadata; + if(InterlockedIncrement(&count)==1)WSAStartup(0x0202,&wsadata); +} +#endif + /*A single physical connection to an HTTP server. We may have several of these open at once.*/ struct OpusHTTPConn{ @@ -640,7 +794,7 @@ struct OpusHTTPConn{ /*The estimated throughput of this connection, in bytes/s.*/ opus_int64 read_rate; /*The socket we're reading from.*/ - int fd; + op_sock fd; /*The number of remaining requests we are allowed on this connection.*/ int nrequests_left; /*The chunk size to use for pipelining requests.*/ @@ -651,13 +805,13 @@ static void op_http_conn_init(OpusHTTPConn *_conn){ _conn->next_pos=-1; _conn->ssl_conn=NULL; _conn->next=NULL; - _conn->fd=-1; + _conn->fd=OP_INVALID_SOCKET; } static void op_http_conn_clear(OpusHTTPConn *_conn){ if(_conn->ssl_conn!=NULL)SSL_free(_conn->ssl_conn); /*SSL frees the BIO for us.*/ - if(_conn->fd>=0)close(_conn->fd); + if(_conn->fd!=OP_INVALID_SOCKET)close(_conn->fd); } /*The global stream state.*/ @@ -683,6 +837,8 @@ struct OpusHTTPStream{ struct sockaddr_in v4; struct sockaddr_in6 v6; } addr; + /*The last time we re-resolved the host.*/ + struct timeb resolve_time; /*A buffer used to build HTTP requests.*/ OpusStringBuf request; /*A buffer used to build proxy CONNECT requests.*/ @@ -694,6 +850,10 @@ struct OpusHTTPStream{ opus_int64 content_length; /*The position indicator used when no connection is active.*/ opus_int64 pos; + /*The host we actually connected to.*/ + char *connect_host; + /*The port we actually connected to.*/ + unsigned connect_port; /*The connection we're currently reading from. This can be -1 if no connection is active.*/ int cur_conni; @@ -713,7 +873,7 @@ struct OpusHTTPStream{ static void op_http_stream_init(OpusHTTPStream *_stream){ OpusHTTPConn **pnext; - int ci; + int ci; pnext=&_stream->free_head; for(ci=0;ciconns+ci); @@ -727,6 +887,7 @@ static void op_http_stream_init(OpusHTTPStream *_stream){ op_sb_init(&_stream->request); op_sb_init(&_stream->proxy_connect); op_sb_init(&_stream->response); + _stream->connect_host=NULL; _stream->seekable=0; } @@ -744,19 +905,13 @@ static void op_http_conn_close(OpusHTTPStream *_stream,OpusHTTPConn *_conn, However, we will not wait if this would block (it's not worth the savings from session resumption to do so). Clients (that's us) MAY resume a TLS session that ended with an incomplete - close, according to RFC 2818, so that's no reason to make sure the server - shut things down gracefully. - It also says "client implementations MUST treat any premature closes as - errors and the data received as potentially truncated," but libopusfile - treats errors and potentially truncated data in unseekable streams just - like a normal EOF. - We warn about this in the docs, and give some suggestions if you truly want - to avoid truncation attacks.*/ + close, according to RFC 2818, so there's no reason to make sure the server + shut things down gracefully.*/ if(_gracefully&&_conn->ssl_conn!=NULL)SSL_shutdown(_conn->ssl_conn); op_http_conn_clear(_conn); _conn->next_pos=-1; _conn->ssl_conn=NULL; - _conn->fd=-1; + _conn->fd=OP_INVALID_SOCKET; OP_ASSERT(*_pnext==_conn); *_pnext=_conn->next; _conn->next=_stream->free_head; @@ -772,6 +927,7 @@ static void op_http_stream_clear(OpusHTTPStream *_stream){ op_sb_clear(&_stream->response); op_sb_clear(&_stream->proxy_connect); op_sb_clear(&_stream->request); + if(_stream->connect_host!=_stream->url.host)_ogg_free(_stream->connect_host); op_parsed_url_clear(&_stream->url); } @@ -802,14 +958,14 @@ static int op_http_conn_write_fully(OpusHTTPConn *_conn, } else{ ssize_t ret; - errno=0; - ret=write(fd.fd,_buf,_buf_size); + op_reset_errno(); + ret=send(fd.fd,_buf,_buf_size,0); if(ret>0){ _buf+=ret; _buf_size-=ret; continue; } - err=errno; + err=op_errno(); if(err!=EAGAIN&&err!=EWOULDBLOCK)return OP_FALSE; fd.events=POLLOUT; } @@ -839,11 +995,11 @@ static int op_http_conn_estimate_available(OpusHTTPConn *_conn){ static opus_int32 op_time_diff_ms(const struct timeb *_end, const struct timeb *_start){ opus_int64 dtime; - dtime=_end->time-_start->time; + dtime=_end->time-(opus_int64)_start->time; OP_ASSERT(_end->millitm<1000); OP_ASSERT(_start->millitm<1000); - if(OP_UNLIKELY(dtime>(0x7FFFFFFF-1000)/1000))return 0x7FFFFFFF; - if(OP_UNLIKELY(dtime<(-0x7FFFFFFF+999)/1000))return -0x7FFFFFFF-1; + if(OP_UNLIKELY(dtime>(OP_INT32_MAX-1000)/1000))return OP_INT32_MAX; + if(OP_UNLIKELY(dtime<(OP_INT32_MIN+1000)/1000))return OP_INT32_MIN; return (opus_int32)dtime*1000+_end->millitm-_start->millitm; } @@ -855,7 +1011,7 @@ static void op_http_conn_read_rate_update(OpusHTTPConn *_conn){ opus_int64 read_rate; read_delta_bytes=_conn->read_bytes; if(read_delta_bytes<=0)return; - OP_ALWAYS_TRUE(!ftime(&read_time)); + ftime(&read_time); read_delta_ms=op_time_diff_ms(&read_time,&_conn->read_time); read_rate=_conn->read_rate; read_delta_ms=OP_MAX(read_delta_ms,1); @@ -881,6 +1037,9 @@ static int op_http_conn_read(OpusHTTPConn *_conn, fd.fd=_conn->fd; ssl_conn=_conn->ssl_conn; nread=nread_unblocked=0; + /*RFC 2818 says "client implementations MUST treat any premature closes as + errors and the data received as potentially truncated," so we make very + sure to report read errors upwards.*/ do{ int err; if(ssl_conn!=NULL){ @@ -912,8 +1071,8 @@ static int op_http_conn_read(OpusHTTPConn *_conn, } else{ ssize_t ret; - errno=0; - ret=read(fd.fd,_buf+nread,_buf_size-nread); + op_reset_errno(); + ret=recv(fd.fd,_buf+nread,_buf_size-nread,0); OP_ASSERT(ret<=_buf_size-nread); if(ret>0){ /*Read some data. @@ -925,7 +1084,7 @@ static int op_http_conn_read(OpusHTTPConn *_conn, /*If we already read some data or the connection was closed, return right now.*/ if(ret==0||nread>0)break; - err=errno; + err=op_errno(); if(err!=EAGAIN&&err!=EWOULDBLOCK)return OP_EREAD; fd.events=POLLIN; } @@ -944,8 +1103,7 @@ static int op_http_conn_read(OpusHTTPConn *_conn, /*Tries to look at the pending data for a connection without consuming it. [out] _buf: Returns the data at which we're peeking. _buf_size: The size of the buffer.*/ -static int op_http_conn_peek(OpusHTTPConn *_conn, - char *_buf,int _buf_size){ +static int op_http_conn_peek(OpusHTTPConn *_conn,char *_buf,int _buf_size){ struct pollfd fd; SSL *ssl_conn; int ret; @@ -964,11 +1122,11 @@ static int op_http_conn_peek(OpusHTTPConn *_conn, else return 0; } else{ - errno=0; + op_reset_errno(); ret=(int)recv(fd.fd,_buf,_buf_size,MSG_PEEK); /*Either saw some data or the connection was closed.*/ if(ret>=0)return ret; - err=errno; + err=op_errno(); if(err!=EAGAIN&&err!=EWOULDBLOCK)return 0; fd.events=POLLIN; } @@ -1192,24 +1350,22 @@ static int op_http_get_next_header(char **_header,char **_cdr,char **_s){ static opus_int64 op_http_parse_nonnegative_int64(const char **_next, const char *_cdr){ const char *next; - opus_int64 content_length; + opus_int64 ret; int i; next=_cdr+strspn(_cdr,OP_HTTP_DIGIT); *_next=next; if(OP_UNLIKELY(next<=_cdr))return OP_FALSE; while(*_cdr=='0')_cdr++; if(OP_UNLIKELY(next-_cdr>19))return OP_EIMPL; - content_length=0; + ret=0; for(i=0;i(OP_INT64_MAX-9)/10+(digit<=7))){ - return OP_EIMPL; - } - content_length=content_length*10+digit; + if(OP_UNLIKELY(ret>(OP_INT64_MAX-9)/10+(digit<=7)))return OP_EIMPL; + ret=ret*10+digit; } - return content_length; + return ret; } static opus_int64 op_http_parse_content_length(const char *_cdr){ @@ -1295,7 +1451,7 @@ static int op_http_parse_connection(char *_cdr){ typedef int (*op_ssl_step_func)(SSL *_ssl_conn); /*Try to run an SSL function to completion (blocking if necessary).*/ -static int op_do_ssl_step(SSL *_ssl_conn,int _fd,op_ssl_step_func _step){ +static int op_do_ssl_step(SSL *_ssl_conn,op_sock _fd,op_ssl_step_func _step){ struct pollfd fd; fd.fd=_fd; for(;;){ @@ -1389,8 +1545,8 @@ static BIO_METHOD op_bio_retry_method={ /*Establish a CONNECT tunnel and pipeline the start of the TLS handshake for proxying https URL requests.*/ -int op_http_conn_establish_tunnel(OpusHTTPStream *_stream, - OpusHTTPConn *_conn,int _fd,SSL *_ssl_conn,BIO *_ssl_bio){ +static int op_http_conn_establish_tunnel(OpusHTTPStream *_stream, + OpusHTTPConn *_conn,op_sock _fd,SSL *_ssl_conn,BIO *_ssl_bio){ BIO *retry_bio; char *status_code; char *next; @@ -1507,8 +1663,7 @@ static struct addrinfo *op_inet_pton(const char *_host){ /*Verify the server's hostname matches the certificate they presented using the procedure from Section 6 of RFC 6125. Return: 0 if the certificate doesn't match, and a non-zero value if it does.*/ -static int op_http_verify_hostname(OpusHTTPStream *_stream, - SSL *_ssl_conn){ +static int op_http_verify_hostname(OpusHTTPStream *_stream,SSL *_ssl_conn){ X509 *peer_cert; STACK_OF(GENERAL_NAME) *san_names; char *host; @@ -1576,7 +1731,7 @@ static int op_http_verify_hostname(OpusHTTPStream *_stream, equivalent) of a URI or deriving the application service type from the scheme of a URI) ..." We don't have a way to check (without relying on DNS records, which might - be subverted), if this address is fully-qualified. + be subverted) if this address is fully-qualified. This is particularly problematic when using a CONNECT tunnel, as it is the server that does DNS lookup, not us. However, we are certain that if the hostname has no '.', it is definitely @@ -1659,8 +1814,8 @@ static int op_http_verify_hostname(OpusHTTPStream *_stream, } /*Perform the TLS handshake on a new connection.*/ -int op_http_conn_start_tls(OpusHTTPStream *_stream,OpusHTTPConn *_conn, - int _fd,SSL *_ssl_conn){ +static int op_http_conn_start_tls(OpusHTTPStream *_stream,OpusHTTPConn *_conn, + op_sock _fd,SSL *_ssl_conn){ SSL_SESSION *ssl_session; BIO *ssl_bio; int skip_certificate_check; @@ -1724,9 +1879,10 @@ int op_http_conn_start_tls(OpusHTTPStream *_stream,OpusHTTPConn *_conn, OP_FALSE If the connection failed and there were no more addresses left to try. *_addr will be set to NULL in this case.*/ -static int op_sock_connect_next(int _fd, - struct addrinfo **_addr,int _ai_family){ - struct addrinfo *addr; +static int op_sock_connect_next(op_sock _fd, + const struct addrinfo **_addr,int _ai_family){ + const struct addrinfo *addr; + int err; addr=*_addr; for(;;){ /*Move to the next address of the requested type.*/ @@ -1735,7 +1891,9 @@ static int op_sock_connect_next(int _fd, /*No more: failure.*/ if(addr==NULL)return OP_FALSE; if(connect(_fd,addr->ai_addr,addr->ai_addrlen)>=0)return 1; - if(OP_LIKELY(errno==EINPROGRESS))return 0; + err=op_errno(); + /*Winsock will set WSAEWOULDBLOCK.*/ + if(OP_LIKELY(err==EINPROGRESS||err==EWOULDBLOCK))return 0; addr=addr->ai_next; } } @@ -1743,36 +1901,30 @@ static int op_sock_connect_next(int _fd, /*The number of address families to try connecting to simultaneously.*/ # define OP_NPROTOS (2) -static int op_http_connect(OpusHTTPStream *_stream,OpusHTTPConn *_conn, - struct addrinfo *_addrs,struct timeb *_start_time){ - struct addrinfo *addr; - struct addrinfo *addrs[OP_NPROTOS]; - struct pollfd fds[OP_NPROTOS]; - int ai_family; - int nprotos; - int ret; - int pi; - int pj; +static int op_http_connect_impl(OpusHTTPStream *_stream,OpusHTTPConn *_conn, + const struct addrinfo *_addrs,struct timeb *_start_time){ + const struct addrinfo *addr; + const struct addrinfo *addrs[OP_NPROTOS]; + struct pollfd fds[OP_NPROTOS]; + int ai_family; + int nprotos; + int ret; + int pi; + int pj; for(pi=0;piai_next){ - /*Give IPv6 a slight edge by putting it first in the list.*/ - if(addr->ai_family==AF_INET6){ + one that succeeds. + Start by finding the first address from each family. + We order the first connection attempts in the same order the address + families were returned in the DNS records in accordance with RFC 6555.*/ + for(addr=_addrs,nprotos=0;addr!=NULL&&nprotosai_next){ + if(addr->ai_family==AF_INET6||addr->ai_family==AF_INET){ OP_ASSERT(addr->ai_addrlen<=sizeof(struct sockaddr_in6)); - if(addrs[0]==NULL)addrs[0]=addr; - } - else if(addr->ai_family==AF_INET){ OP_ASSERT(addr->ai_addrlen<=sizeof(struct sockaddr_in)); - if(addrs[1]==NULL)addrs[1]=addr; - } - } - /*Consolidate the list of addresses.*/ - for(pi=nprotos=0;piai_family==addr->ai_family)break; + if(pifree_head=_conn->next; _conn->next=_stream->lru_head; _stream->lru_head=_conn; - OP_ALWAYS_TRUE(!ftime(_start_time)); + ftime(_start_time); *&_conn->read_time=*_start_time; _conn->read_bytes=0; _conn->read_rate=0; - /*Try to start a connection to each protocol.*/ + /*Try to start a connection to each protocol. + RFC 6555 says it is RECOMMENDED that connection attempts be paced + 150...250 ms apart "to balance human factors against network load", but + that "stateful algorithms" (that's us) "are expected to be more + aggressive". + We are definitely more aggressive: we don't pace at all.*/ for(pi=0;piai_family; fds[pi].fd=socket(ai_family,SOCK_STREAM,addrs[pi]->ai_protocol); fds[pi].events=POLLOUT; - if(OP_LIKELY(fds[pi].fd>=0)){ + if(OP_LIKELY(fds[pi].fd!=OP_INVALID_SOCKET)){ if(OP_LIKELY(op_sock_set_nonblocking(fds[pi].fd,1)>=0)){ ret=op_sock_connect_next(fds[pi].fd,addrs+pi,ai_family); if(OP_UNLIKELY(ret>0)){ @@ -1820,7 +1977,7 @@ static int op_http_connect(OpusHTTPStream *_stream,OpusHTTPConn *_conn, /*Some platforms will return the pending error in &err and return 0. Others will put it in errno and return -1.*/ ret=getsockopt(fds[pi].fd,SOL_SOCKET,SO_ERROR,&err,&errlen); - if(ret<0)err=errno; + if(ret<0)err=op_errno(); /*Success!*/ if(err==0||err==EISCONN)break; /*Move on to the next address for this protocol.*/ @@ -1862,7 +2019,7 @@ static int op_http_connect(OpusHTTPStream *_stream,OpusHTTPConn *_conn, SSL_free(ssl_conn); } close(fds[pi].fd); - _conn->fd=-1; + _conn->fd=OP_INVALID_SOCKET; return OP_FALSE; } /*Just a normal non-SSL connection.*/ @@ -1876,6 +2033,29 @@ static int op_http_connect(OpusHTTPStream *_stream,OpusHTTPConn *_conn, return 0; } +static int op_http_connect(OpusHTTPStream *_stream,OpusHTTPConn *_conn, + const struct addrinfo *_addrs,struct timeb *_start_time){ + struct timeb resolve_time; + struct addrinfo *new_addrs; + int ret; + /*Re-resolve the host if we need to (RFC 6555 says we MUST do so + occasionally).*/ + new_addrs=NULL; + ftime(&resolve_time); + if(_addrs!=&_stream->addr_info||op_time_diff_ms(&resolve_time, + &_stream->resolve_time)>=OP_RESOLVE_CACHE_TIMEOUT_MS){ + new_addrs=op_resolve(_stream->connect_host,_stream->connect_port); + if(OP_LIKELY(new_addrs!=NULL)){ + _addrs=new_addrs; + *&_stream->resolve_time=*&resolve_time; + } + else if(OP_LIKELY(_addrs==NULL))return OP_FALSE; + } + ret=op_http_connect_impl(_stream,_conn,_addrs,_start_time); + if(new_addrs!=NULL)freeaddrinfo(new_addrs); + return ret; +} + # define OP_BASE64_LENGTH(_len) (((_len)+2)/3*4) static const char BASE64_TABLE[64]={ @@ -1897,15 +2077,15 @@ static char *op_base64_encode(char *_dst,const char *_src,int _len){ s1=_src[3*i+1]; s2=_src[3*i+2]; _dst[4*i+0]=BASE64_TABLE[s0>>2]; - _dst[4*i+1]=BASE64_TABLE[s0&3<<4|s1>>4]; - _dst[4*i+2]=BASE64_TABLE[s1&15<<2|s2>>6]; + _dst[4*i+1]=BASE64_TABLE[(s0&3)<<4|s1>>4]; + _dst[4*i+2]=BASE64_TABLE[(s1&15)<<2|s2>>6]; _dst[4*i+3]=BASE64_TABLE[s2&63]; } _len-=3*i; if(_len==1){ s0=_src[3*i+0]; _dst[4*i+0]=BASE64_TABLE[s0>>2]; - _dst[4*i+1]=BASE64_TABLE[s0&3<<4]; + _dst[4*i+1]=BASE64_TABLE[(s0&3)<<4]; _dst[4*i+2]='='; _dst[4*i+3]='='; i++; @@ -1914,8 +2094,8 @@ static char *op_base64_encode(char *_dst,const char *_src,int _len){ s0=_src[3*i+0]; s1=_src[3*i+1]; _dst[4*i+0]=BASE64_TABLE[s0>>2]; - _dst[4*i+1]=BASE64_TABLE[s0&3<<4|s1>>4]; - _dst[4*i+2]=BASE64_TABLE[s1&15<<2]; + _dst[4*i+1]=BASE64_TABLE[(s0&3)<<4|s1>>4]; + _dst[4*i+2]=BASE64_TABLE[(s1&15)<<2]; _dst[4*i+3]='='; i++; } @@ -1990,50 +2170,33 @@ static int op_http_allow_pipelining(const char *_server){ static int op_http_stream_open(OpusHTTPStream *_stream,const char *_url, int _skip_certificate_check,const char *_proxy_host,unsigned _proxy_port, - const char *_proxy_user,const char *_proxy_pass){ + const char *_proxy_user,const char *_proxy_pass,OpusServerInfo *_info){ struct addrinfo *addrs; - const char *last_host; - unsigned last_port; int nredirs; int ret; - if(_proxy_host!=NULL&&OP_UNLIKELY(_proxy_port>65535U))return OP_EINVAL; - last_host=NULL; - /*We shouldn't have to initialize last_port, but gcc is too dumb to figure - out that last_host!=NULL implies we've already taken one trip through the - loop.*/ - last_port=0; +#if defined(_WIN32) + op_init_winsock(); +#endif ret=op_parse_url(&_stream->url,_url); if(OP_UNLIKELY(ret<0))return ret; + if(_proxy_host!=NULL){ + if(OP_UNLIKELY(_proxy_port>65535U))return OP_EINVAL; + _stream->connect_host=op_string_dup(_proxy_host); + _stream->connect_port=_proxy_port; + } + else{ + _stream->connect_host=_stream->url.host; + _stream->connect_port=_stream->url.port; + } + addrs=NULL; for(nredirs=0;nredirsurl.host; - port=_stream->url.port; - } - else{ - host=_proxy_host; - port=_proxy_port; - } - /*If connecting to the same place as last time, don't re-resolve it.*/ - addrs=NULL; - if(last_host!=NULL){ - if(strcmp(last_host,host)==0&&last_port==port)addrs=&_stream->addr_info; - else if(_stream->ssl_session!=NULL){ - /*Forget any cached SSL session from the last host.*/ - SSL_SESSION_free(_stream->ssl_session); - _stream->ssl_session=NULL; - } - if(last_host!=_proxy_host)_ogg_free((void *)last_host); - } - last_host=host; - last_port=port; + OpusParsedURL next_url; + struct timeb start_time; + struct timeb end_time; + char *next; + char *status_code; + int minor_version_pos; + int v1_1_compat; /*Initialize the SSL library if necessary.*/ if(OP_URL_IS_SSL(&_stream->url)&&_stream->ssl_ctx==NULL){ SSL_CTX *ssl_ctx; @@ -2069,6 +2232,7 @@ static int op_http_stream_open(OpusHTTPStream *_stream,const char *_url, if(_proxy_host!=NULL){ /*We need to establish a CONNECT tunnel to handle https proxying. Build the request we'll send to do so.*/ + _stream->proxy_connect.nbuf=0; ret=op_sb_append(&_stream->proxy_connect,"CONNECT ",8); ret|=op_sb_append_string(&_stream->proxy_connect,_stream->url.host); ret|=op_sb_append_port(&_stream->proxy_connect,_stream->url.port); @@ -2096,12 +2260,7 @@ static int op_http_stream_open(OpusHTTPStream *_stream,const char *_url, } } /*Actually make the connection.*/ - if(addrs!=&_stream->addr_info){ - addrs=op_resolve(host,port); - if(OP_UNLIKELY(addrs==NULL))return OP_FALSE; - } ret=op_http_connect(_stream,_stream->conns+0,addrs,&start_time); - if(addrs!=&_stream->addr_info)freeaddrinfo(addrs); if(OP_UNLIKELY(ret<0))return ret; /*Build the request to send.*/ _stream->request.nbuf=0; @@ -2162,20 +2321,22 @@ static int op_http_stream_open(OpusHTTPStream *_stream,const char *_url, if(OP_UNLIKELY(ret<0))return ret; ret=op_http_conn_read_response(_stream->conns+0,&_stream->response); if(OP_UNLIKELY(ret<0))return ret; - OP_ALWAYS_TRUE(!ftime(&end_time)); + ftime(&end_time); next=op_http_parse_status_line(&v1_1_compat,&status_code, _stream->response.buf); if(OP_UNLIKELY(next==NULL))return OP_FALSE; if(status_code[0]=='2'){ opus_int64 content_length; opus_int64 range_length; - int pipeline; + int pipeline_supported; + int pipeline_disabled; /*We only understand 20x codes.*/ if(status_code[1]!='0')return OP_FALSE; content_length=-1; range_length=-1; - /*Pipelining is disabled by default.*/ - pipeline=0; + /*Pipelining must be explicitly enabled.*/ + pipeline_supported=0; + pipeline_disabled=0; for(;;){ char *header; char *cdr; @@ -2200,7 +2361,7 @@ static int op_http_stream_open(OpusHTTPStream *_stream,const char *_url, ret=op_http_parse_content_range(&range_first,&range_last, &range_length,cdr); if(OP_UNLIKELY(ret<0))return ret; - /*"A response with satus code 206 (Partial Content) MUST NOTE + /*"A response with satus code 206 (Partial Content) MUST NOT include a Content-Range field with a byte-range-resp-spec of '*'."*/ if(status_code[2]=='6' @@ -2233,9 +2394,9 @@ static int op_http_stream_open(OpusHTTPStream *_stream,const char *_url, error out and reconnect, adding lots of latency.*/ ret=op_http_parse_connection(cdr); if(OP_UNLIKELY(ret<0))return ret; - pipeline-=ret; + pipeline_disabled|=ret; } - else if(strcmp(header,"server")){ + else if(strcmp(header,"server")==0){ /*If we got a Server response header, and it wasn't from a known-bad server, enable pipelining, as long as it's at least HTTP/1.1. According to RFC 2145, the server is supposed to respond with the @@ -2243,7 +2404,51 @@ static int op_http_stream_open(OpusHTTPStream *_stream,const char *_url, suspected that we incorrectly implement the HTTP specification. So it should send back at least HTTP/1.1, despite our HTTP/1.0 request.*/ - pipeline+=v1_1_compat&&op_http_allow_pipelining(cdr); + pipeline_supported=v1_1_compat; + if(v1_1_compat)pipeline_disabled|=!op_http_allow_pipelining(cdr); + if(_info!=NULL&&_info->server==NULL)_info->server=op_string_dup(cdr); + } + /*Collect station information headers if the caller requested it. + If there's more than one copy of a header, the first one wins.*/ + else if(_info!=NULL){ + if(strcmp(header,"content-type")==0){ + if(_info->content_type==NULL){ + _info->content_type=op_string_dup(cdr); + } + } + else if(header[0]=='i'&&header[1]=='c' + &&(header[2]=='e'||header[2]=='y')&&header[3]=='-'){ + if(strcmp(header+4,"name")==0){ + if(_info->name==NULL)_info->name=op_string_dup(cdr); + } + else if(strcmp(header+4,"description")==0){ + if(_info->description==NULL)_info->description=op_string_dup(cdr); + } + else if(strcmp(header+4,"genre")==0){ + if(_info->genre==NULL)_info->genre=op_string_dup(cdr); + } + else if(strcmp(header+4,"url")==0){ + if(_info->url==NULL)_info->url=op_string_dup(cdr); + } + else if(strcmp(header,"icy-br")==0 + ||strcmp(header,"ice-bitrate")==0){ + if(_info->bitrate_kbps<0){ + opus_int64 bitrate_kbps; + /*Just re-using this function to parse a random unsigned + integer field.*/ + bitrate_kbps=op_http_parse_content_length(cdr); + if(bitrate_kbps>=0&&bitrate_kbps<=OP_INT32_MAX){ + _info->bitrate_kbps=(opus_int32)bitrate_kbps; + } + } + } + else if(strcmp(header,"icy-pub")==0 + ||strcmp(header,"ice-public")==0){ + if(_info->is_public<0&&(cdr[0]=='0'||cdr[0]=='1')&&cdr[1]=='\0'){ + _info->is_public=cdr[0]-'0'; + } + } + } } } switch(status_code[2]){ @@ -2278,15 +2483,16 @@ static int op_http_stream_open(OpusHTTPStream *_stream,const char *_url, default:return OP_FALSE; } _stream->content_length=content_length; - _stream->pipeline=pipeline>0; + _stream->pipeline=pipeline_supported&&!pipeline_disabled; /*Pipelining requires HTTP/1.1 persistent connections.*/ - if(pipeline)_stream->request.buf[minor_version_pos]='1'; + if(_stream->pipeline)_stream->request.buf[minor_version_pos]='1'; _stream->conns[0].pos=0; _stream->conns[0].end_pos=_stream->seekable?content_length:-1; _stream->conns[0].chunk_size=-1; _stream->cur_conni=0; _stream->connect_rate=op_time_diff_ms(&end_time,&start_time); _stream->connect_rate=OP_MAX(_stream->connect_rate,1); + if(_info!=NULL)_info->is_ssl=OP_URL_IS_SSL(&_stream->url); /*The URL has been successfully opened.*/ return 0; } @@ -2306,7 +2512,9 @@ static int op_http_stream_open(OpusHTTPStream *_stream,const char *_url, /*302 Found*/ case '2': /*307 Temporary Redirect*/ - case '7':break; + case '7': + /*308 Permanent Redirect (defined by draft-reschke-http-status-308-07).*/ + case '8':break; /*305 Use Proxy: "The Location field gives the URI of the proxy." TODO: This shouldn't actually be that hard to do.*/ case '5':return OP_EIMPL; @@ -2314,7 +2522,7 @@ static int op_http_stream_open(OpusHTTPStream *_stream,const char *_url, originally requested resource." 304 Not Modified: "The 304 response MUST NOT contain a message-body." 306 (Unused) - 308...309 are not yet defined, so we don't know how to handle them.*/ + 309 is not yet defined, so we don't know how to handle it.*/ default:return OP_FALSE; } _url=NULL; @@ -2327,15 +2535,33 @@ static int op_http_stream_open(OpusHTTPStream *_stream,const char *_url, if(strcmp(header,"location")==0&&OP_LIKELY(_url==NULL))_url=cdr; } if(OP_UNLIKELY(_url==NULL))return OP_FALSE; - /*Don't free last_host if it came from the last URL.*/ - if(last_host!=_proxy_host)_stream->url.host=NULL; - op_parsed_url_clear(&_stream->url); - ret=op_parse_url(&_stream->url,_url); - if(OP_UNLIKELY(ret<0)){ - if(ret==OP_EINVAL)ret=OP_FALSE; - if(last_host!=_proxy_host)_ogg_free((void *)last_host); - return ret; + ret=op_parse_url(&next_url,_url); + if(OP_UNLIKELY(ret<0))return ret; + if(_proxy_host==NULL||_stream->ssl_session!=NULL){ + if(strcmp(_stream->url.host,next_url.host)==0 + &&_stream->url.port==next_url.port){ + /*Try to skip re-resolve when connecting to the same host.*/ + addrs=&_stream->addr_info; + } + else{ + if(_stream->ssl_session!=NULL){ + /*Forget any cached SSL session from the last host.*/ + SSL_SESSION_free(_stream->ssl_session); + _stream->ssl_session=NULL; + } + } } + if(_proxy_host==NULL){ + OP_ASSERT(_stream->connect_host==_stream->url.host); + _stream->connect_host=next_url.host; + _stream->connect_port=next_url.port; + } + /*Always try to skip re-resolve for proxy connections.*/ + else addrs=&_stream->addr_info; + op_parsed_url_clear(&_stream->url); + *&_stream->url=*&next_url; + /*TODO: On servers/proxies that support pipelining, we might be able to + re-use this connection.*/ op_http_conn_close(_stream,_stream->conns+0,&_stream->lru_head,1); } /*Redirection limit reached.*/ @@ -2500,7 +2726,7 @@ static int op_http_conn_handle_response(OpusHTTPStream *_stream, } /*Open a new connection that will start reading at byte offset _pos. - _pos: The byte offset to start readiny from. + _pos: The byte offset to start reading from. _chunk_size: The number of bytes to ask for in the initial request, or -1 to request the rest of the resource. This may be more bytes than remain, in which case it will be @@ -2518,7 +2744,7 @@ static int op_http_conn_open_pos(OpusHTTPStream *_stream, if(OP_UNLIKELY(ret<0))return ret; ret=op_http_conn_handle_response(_stream,_conn); if(OP_UNLIKELY(ret!=0))return OP_FALSE; - OP_ALWAYS_TRUE(!ftime(&end_time)); + ftime(&end_time); _stream->cur_conni=_conn-_stream->conns; OP_ASSERT(_stream->cur_conni>=0&&_stream->cur_conni=0&&next_end-next_pos<=0x7FFFFFFF); + ||next_end-next_pos>=0&&next_end-next_pos<=OP_INT32_MAX); ret=op_http_conn_open_pos(_stream,_conn,next_pos, next_end<0?-1:(opus_int32)(next_end-next_pos)); if(OP_UNLIKELY(ret<0))return OP_EREAD; @@ -2811,7 +3037,7 @@ static int op_http_stream_seek(void *_stream,opus_int64 _offset,int _whence){ op_http_conn_read_rate_update(stream->conns+ci); *&seek_time=*&stream->conns[ci].read_time; } - else OP_ALWAYS_TRUE(!ftime(&seek_time)); + else ftime(&seek_time); /*If we seeked past the end of the stream, just disable the active connection.*/ if(pos>=content_length){ @@ -2979,12 +3205,33 @@ static const OpusFileCallbacks OP_HTTP_CALLBACKS={ }; #endif +void opus_server_info_init(OpusServerInfo *_info){ + _info->name=NULL; + _info->description=NULL; + _info->genre=NULL; + _info->url=NULL; + _info->server=NULL; + _info->content_type=NULL; + _info->bitrate_kbps=-1; + _info->is_public=-1; + _info->is_ssl=0; +} + +void opus_server_info_clear(OpusServerInfo *_info){ + _ogg_free(_info->content_type); + _ogg_free(_info->server); + _ogg_free(_info->url); + _ogg_free(_info->genre); + _ogg_free(_info->description); + _ogg_free(_info->name); +} + /*The actual URL stream creation function. This one isn't extensible like the application-level interface, but because it isn't public, we're free to change it in the future.*/ static void *op_url_stream_create_impl(OpusFileCallbacks *_cb,const char *_url, int _skip_certificate_check,const char *_proxy_host,unsigned _proxy_port, - const char *_proxy_user,const char *_proxy_pass){ + const char *_proxy_user,const char *_proxy_pass,OpusServerInfo *_info){ const char *path; /*Check to see if this is a valid file: URL.*/ path=op_parse_file_url(_url); @@ -3006,7 +3253,7 @@ static void *op_url_stream_create_impl(OpusFileCallbacks *_cb,const char *_url, if(OP_UNLIKELY(stream==NULL))return NULL; op_http_stream_init(stream); ret=op_http_stream_open(stream,_url,_skip_certificate_check, - _proxy_host,_proxy_port,_proxy_user,_proxy_pass); + _proxy_host,_proxy_port,_proxy_user,_proxy_pass,_info); if(OP_UNLIKELY(ret<0)){ op_http_stream_clear(stream); _ogg_free(stream); @@ -3021,22 +3268,25 @@ static void *op_url_stream_create_impl(OpusFileCallbacks *_cb,const char *_url, (void)_proxy_port; (void)_proxy_user; (void)_proxy_pass; + (void)_info; return NULL; #endif } void *op_url_stream_vcreate(OpusFileCallbacks *_cb, const char *_url,va_list _ap){ - int skip_certificate_check; - const char *proxy_host; - opus_int32 proxy_port; - const char *proxy_user; - const char *proxy_pass; + int skip_certificate_check; + const char *proxy_host; + opus_int32 proxy_port; + const char *proxy_user; + const char *proxy_pass; + OpusServerInfo *pinfo; skip_certificate_check=0; proxy_host=NULL; proxy_port=8080; proxy_user=NULL; proxy_pass=NULL; + pinfo=NULL; for(;;){ ptrdiff_t request; request=va_arg(_ap,char *)-(char *)NULL; @@ -3059,17 +3309,83 @@ void *op_url_stream_vcreate(OpusFileCallbacks *_cb, case OP_HTTP_PROXY_PASS_REQUEST:{ proxy_pass=va_arg(_ap,const char *); }break; + case OP_GET_SERVER_INFO_REQUEST:{ + pinfo=va_arg(_ap,OpusServerInfo *); + }break; /*Some unknown option.*/ default:return NULL; } } + /*If the caller has requested server information, proxy it to a local copy to + simplify error handling.*/ + if(pinfo!=NULL){ + OpusServerInfo info; + void *ret; + opus_server_info_init(&info); + ret=op_url_stream_create_impl(_cb,_url,skip_certificate_check, + proxy_host,proxy_port,proxy_user,proxy_pass,&info); + if(ret!=NULL)*pinfo=*&info; + else opus_server_info_clear(&info); + return ret; + } return op_url_stream_create_impl(_cb,_url,skip_certificate_check, - proxy_host,proxy_port,proxy_user,proxy_pass); + proxy_host,proxy_port,proxy_user,proxy_pass,NULL); } void *op_url_stream_create(OpusFileCallbacks *_cb, const char *_url,...){ - va_list ap; + va_list ap; + void *ret; va_start(ap,_url); - return op_url_stream_vcreate(_cb,_url,ap); + ret=op_url_stream_vcreate(_cb,_url,ap); + va_end(ap); + return ret; +} + +/*Convenience routines to open/test URLs in a single step.*/ + +OggOpusFile *op_vopen_url(const char *_url,int *_error,va_list _ap){ + OpusFileCallbacks cb; + OggOpusFile *of; + void *source; + source=op_url_stream_vcreate(&cb,_url,_ap); + if(OP_UNLIKELY(source==NULL)){ + if(_error!=NULL)*_error=OP_EFAULT; + return NULL; + } + of=op_open_callbacks(source,&cb,NULL,0,_error); + if(OP_UNLIKELY(of==NULL))(*cb.close)(source); + return of; +} + +OggOpusFile *op_open_url(const char *_url,int *_error,...){ + OggOpusFile *ret; + va_list ap; + va_start(ap,_error); + ret=op_vopen_url(_url,_error,ap); + va_end(ap); + return ret; +} + +OggOpusFile *op_vtest_url(const char *_url,int *_error,va_list _ap){ + OpusFileCallbacks cb; + OggOpusFile *of; + void *source; + source=op_url_stream_vcreate(&cb,_url,_ap); + if(OP_UNLIKELY(source==NULL)){ + if(_error!=NULL)*_error=OP_EFAULT; + return NULL; + } + of=op_test_callbacks(source,&cb,NULL,0,_error); + if(OP_UNLIKELY(of==NULL))(*cb.close)(source); + return of; +} + +OggOpusFile *op_test_url(const char *_url,int *_error,...){ + OggOpusFile *ret; + va_list ap; + va_start(ap,_error); + ret=op_vtest_url(_url,_error,ap); + va_end(ap); + return ret; } diff --git a/code/opusfile-0.5/src/info.c b/code/opusfile-0.5/src/info.c new file mode 100644 index 00000000..6cf98516 --- /dev/null +++ b/code/opusfile-0.5/src/info.c @@ -0,0 +1,683 @@ +/******************************************************************** + * * + * THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. * + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * + * * + * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 2012 * + * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * + * * + ********************************************************************/ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "internal.h" +#include +#include + +static unsigned op_parse_uint16le(const unsigned char *_data){ + return _data[0]|_data[1]<<8; +} + +static int op_parse_int16le(const unsigned char *_data){ + int ret; + ret=_data[0]|_data[1]<<8; + return (ret^0x8000)-0x8000; +} + +static opus_uint32 op_parse_uint32le(const unsigned char *_data){ + return _data[0]|_data[1]<<8|_data[2]<<16|_data[3]<<24; +} + +static opus_uint32 op_parse_uint32be(const unsigned char *_data){ + return _data[3]|_data[2]<<8|_data[1]<<16|_data[0]<<24; +} + +int opus_head_parse(OpusHead *_head,const unsigned char *_data,size_t _len){ + OpusHead head; + if(_len<8)return OP_ENOTFORMAT; + if(memcmp(_data,"OpusHead",8)!=0)return OP_ENOTFORMAT; + if(_len<9)return OP_EBADHEADER; + head.version=_data[8]; + if(head.version>15)return OP_EVERSION; + if(_len<19)return OP_EBADHEADER; + head.channel_count=_data[9]; + head.pre_skip=op_parse_uint16le(_data+10); + head.input_sample_rate=op_parse_uint32le(_data+12); + head.output_gain=op_parse_int16le(_data+16); + head.mapping_family=_data[18]; + if(head.mapping_family==0){ + if(head.channel_count<1||head.channel_count>2)return OP_EBADHEADER; + if(head.version<=1&&_len>19)return OP_EBADHEADER; + head.stream_count=1; + head.coupled_count=head.channel_count-1; + if(_head!=NULL){ + _head->mapping[0]=0; + _head->mapping[1]=1; + } + } + else if(head.mapping_family==1){ + size_t size; + int ci; + if(head.channel_count<1||head.channel_count>8)return OP_EBADHEADER; + size=21+head.channel_count; + if(_lensize)return OP_EBADHEADER; + head.stream_count=_data[19]; + if(head.stream_count<1)return OP_EBADHEADER; + head.coupled_count=_data[20]; + if(head.coupled_count>head.stream_count)return OP_EBADHEADER; + for(ci=0;ci=head.stream_count+head.coupled_count + &&_data[21+ci]!=255){ + return OP_EBADHEADER; + } + } + if(_head!=NULL)memcpy(_head->mapping,_data+21,head.channel_count); + } + /*General purpose players should not attempt to play back content with + channel mapping family 255.*/ + else if(head.mapping_family==255)return OP_EIMPL; + /*No other channel mapping families are currently defined.*/ + else return OP_EBADHEADER; + if(_head!=NULL)memcpy(_head,&head,head.mapping-(unsigned char *)&head); + return 0; +} + +void opus_tags_init(OpusTags *_tags){ + memset(_tags,0,sizeof(*_tags)); +} + +void opus_tags_clear(OpusTags *_tags){ + int ci; + for(ci=_tags->comments;ci-->0;)_ogg_free(_tags->user_comments[ci]); + _ogg_free(_tags->user_comments); + _ogg_free(_tags->comment_lengths); + _ogg_free(_tags->vendor); +} + +/*Ensure there's room for up to _ncomments comments.*/ +static int op_tags_ensure_capacity(OpusTags *_tags,size_t _ncomments){ + char **user_comments; + int *comment_lengths; + size_t size; + if(OP_UNLIKELY(_ncomments>=(size_t)INT_MAX))return OP_EFAULT; + size=sizeof(*_tags->comment_lengths)*(_ncomments+1); + if(size/sizeof(*_tags->comment_lengths)!=_ncomments+1)return OP_EFAULT; + comment_lengths=(int *)_ogg_realloc(_tags->comment_lengths,size); + if(OP_UNLIKELY(comment_lengths==NULL))return OP_EFAULT; + comment_lengths[_ncomments]=0; + _tags->comment_lengths=comment_lengths; + size=sizeof(*_tags->user_comments)*(_ncomments+1); + if(size/sizeof(*_tags->user_comments)!=_ncomments+1)return OP_EFAULT; + user_comments=(char **)_ogg_realloc(_tags->user_comments,size); + if(OP_UNLIKELY(user_comments==NULL))return OP_EFAULT; + user_comments[_ncomments]=NULL; + _tags->user_comments=user_comments; + return 0; +} + +/*Duplicate a (possibly non-NUL terminated) string with a known length.*/ +static char *op_strdup_with_len(const char *_s,size_t _len){ + size_t size; + char *ret; + size=sizeof(*ret)*(_len+1); + if(OP_UNLIKELY(size<_len))return NULL; + ret=(char *)_ogg_malloc(size); + if(OP_LIKELY(ret!=NULL)){ + ret=(char *)memcpy(ret,_s,sizeof(*ret)*_len); + ret[_len]='\0'; + } + return ret; +} + +/*The actual implementation of opus_tags_parse(). + Unlike the public API, this function requires _tags to already be + initialized, modifies its contents before success is guaranteed, and assumes + the caller will clear it on error.*/ +static int opus_tags_parse_impl(OpusTags *_tags, + const unsigned char *_data,size_t _len){ + opus_uint32 count; + size_t len; + int ncomments; + int ci; + len=_len; + if(len<8)return OP_ENOTFORMAT; + if(memcmp(_data,"OpusTags",8)!=0)return OP_ENOTFORMAT; + if(len<16)return OP_EBADHEADER; + _data+=8; + len-=8; + count=op_parse_uint32le(_data); + _data+=4; + len-=4; + if(count>len)return OP_EBADHEADER; + if(_tags!=NULL){ + _tags->vendor=op_strdup_with_len((char *)_data,count); + if(_tags->vendor==NULL)return OP_EFAULT; + } + _data+=count; + len-=count; + if(len<4)return OP_EBADHEADER; + count=op_parse_uint32le(_data); + _data+=4; + len-=4; + /*Check to make sure there's minimally sufficient data left in the packet.*/ + if(count>len>>2)return OP_EBADHEADER; + /*Check for overflow (the API limits this to an int).*/ + if(count>(opus_uint32)INT_MAX-1)return OP_EFAULT; + if(_tags!=NULL){ + int ret; + ret=op_tags_ensure_capacity(_tags,count); + if(ret<0)return ret; + } + ncomments=(int)count; + for(ci=0;cilen>>2)return OP_EBADHEADER; + count=op_parse_uint32le(_data); + _data+=4; + len-=4; + if(count>len)return OP_EBADHEADER; + /*Check for overflow (the API limits this to an int).*/ + if(count>(opus_uint32)INT_MAX)return OP_EFAULT; + if(_tags!=NULL){ + _tags->user_comments[ci]=op_strdup_with_len((char *)_data,count); + if(_tags->user_comments[ci]==NULL)return OP_EFAULT; + _tags->comment_lengths[ci]=(int)count; + _tags->comments=ci+1; + } + _data+=count; + len-=count; + } + return 0; +} + +int opus_tags_parse(OpusTags *_tags,const unsigned char *_data,size_t _len){ + if(_tags!=NULL){ + OpusTags tags; + int ret; + opus_tags_init(&tags); + ret=opus_tags_parse_impl(&tags,_data,_len); + if(ret<0)opus_tags_clear(&tags); + else *_tags=*&tags; + return ret; + } + else return opus_tags_parse_impl(NULL,_data,_len); +} + +/*The actual implementation of opus_tags_copy(). + Unlike the public API, this function requires _dst to already be + initialized, modifies its contents before success is guaranteed, and assumes + the caller will clear it on error.*/ +static int opus_tags_copy_impl(OpusTags *_dst,const OpusTags *_src){ + char *vendor; + int ncomments; + int ret; + int ci; + vendor=_src->vendor; + _dst->vendor=op_strdup_with_len(vendor,strlen(vendor)); + if(OP_UNLIKELY(_dst->vendor==NULL))return OP_EFAULT; + ncomments=_src->comments; + ret=op_tags_ensure_capacity(_dst,ncomments); + if(OP_UNLIKELY(ret<0))return ret; + for(ci=0;cicomment_lengths[ci]; + OP_ASSERT(len>=0); + _dst->user_comments[ci]=op_strdup_with_len(_src->user_comments[ci],len); + if(OP_UNLIKELY(_dst->user_comments[ci]==NULL))return OP_EFAULT; + _dst->comment_lengths[ci]=len; + _dst->comments=ci+1; + } + return 0; +} + +int opus_tags_copy(OpusTags *_dst,const OpusTags *_src){ + OpusTags dst; + int ret; + opus_tags_init(&dst); + ret=opus_tags_copy_impl(&dst,_src); + if(OP_UNLIKELY(ret<0))opus_tags_clear(&dst); + else *_dst=*&dst; + return 0; +} + +int opus_tags_add(OpusTags *_tags,const char *_tag,const char *_value){ + char *comment; + int tag_len; + int value_len; + int ncomments; + int ret; + ncomments=_tags->comments; + ret=op_tags_ensure_capacity(_tags,ncomments+1); + if(OP_UNLIKELY(ret<0))return ret; + tag_len=strlen(_tag); + value_len=strlen(_value); + /*+2 for '=' and '\0'.*/ + _tags->comment_lengths[ncomments]=0; + _tags->user_comments[ncomments]=comment= + (char *)_ogg_malloc(sizeof(*comment)*(tag_len+value_len+2)); + if(OP_UNLIKELY(comment==NULL))return OP_EFAULT; + _tags->comment_lengths[ncomments]=tag_len+value_len+1; + memcpy(comment,_tag,sizeof(*comment)*tag_len); + comment[tag_len]='='; + memcpy(comment+tag_len+1,_value,sizeof(*comment)*(value_len+1)); + return 0; +} + +int opus_tags_add_comment(OpusTags *_tags,const char *_comment){ + int comment_len; + int ncomments; + int ret; + ncomments=_tags->comments; + ret=op_tags_ensure_capacity(_tags,ncomments+1); + if(OP_UNLIKELY(ret<0))return ret; + comment_len=(int)strlen(_comment); + _tags->comment_lengths[ncomments]=0; + _tags->user_comments[ncomments]=op_strdup_with_len(_comment,comment_len); + if(OP_UNLIKELY(_tags->user_comments[ncomments]==NULL))return OP_EFAULT; + _tags->comment_lengths[ncomments]=comment_len; + return 0; +} + +int opus_tagcompare(const char *_tag_name,const char *_comment){ + return opus_tagncompare(_tag_name,strlen(_tag_name),_comment); +} + +int opus_tagncompare(const char *_tag_name,int _tag_len,const char *_comment){ + int ret; + OP_ASSERT(_tag_len>=0); + ret=op_strncasecmp(_tag_name,_comment,_tag_len); + return ret?ret:'='-_comment[_tag_len]; +} + +const char *opus_tags_query(const OpusTags *_tags,const char *_tag,int _count){ + char **user_comments; + int tag_len; + int found; + int ncomments; + int ci; + tag_len=strlen(_tag); + ncomments=_tags->comments; + user_comments=_tags->user_comments; + found=0; + for(ci=0;cicomments; + user_comments=_tags->user_comments; + found=0; + for(ci=0;ciuser_comments; + ncomments=_tags->comments; + /*Look for the first valid R128_TRACK_GAIN tag and use that.*/ + for(ci=0;ci='0'&&*p<='9'){ + gain_q8=10*gain_q8+*p-'0'; + if(gain_q8>32767-negative)break; + p++; + } + /*This didn't look like a signed 16-bit decimal integer. + Not a valid R128_TRACK_GAIN tag.*/ + if(*p!='\0')continue; + *_gain_q8=(int)(gain_q8+negative^negative); + return 0; + } + } + return OP_FALSE; +} + +static int op_is_jpeg(const unsigned char *_buf,size_t _buf_sz){ + return _buf_sz>=11&&memcmp(_buf,"\xFF\xD8\xFF\xE0",4)==0 + &&(_buf[4]<<8|_buf[5])>=16&&memcmp(_buf+6,"JFIF",5)==0; +} + +/*Tries to extract the width, height, bits per pixel, and palette size of a + JPEG. + On failure, simply leaves its outputs unmodified.*/ +static void op_extract_jpeg_params(const unsigned char *_buf,size_t _buf_sz, + opus_uint32 *_width,opus_uint32 *_height, + opus_uint32 *_depth,opus_uint32 *_colors,int *_has_palette){ + if(op_is_jpeg(_buf,_buf_sz)){ + size_t offs; + offs=2; + for(;;){ + size_t segment_len; + int marker; + while(offs<_buf_sz&&_buf[offs]!=0xFF)offs++; + while(offs<_buf_sz&&_buf[offs]==0xFF)offs++; + marker=_buf[offs]; + offs++; + /*If we hit EOI* (end of image), or another SOI* (start of image), + or SOS (start of scan), then stop now.*/ + if(offs>=_buf_sz||(marker>=0xD8&&marker<=0xDA))break; + /*RST* (restart markers): skip (no segment length).*/ + else if(marker>=0xD0&&marker<=0xD7)continue; + /*Read the length of the marker segment.*/ + if(_buf_sz-offs<2)break; + segment_len=_buf[offs]<<8|_buf[offs+1]; + if(segment_len<2||_buf_sz-offs0xC0&&marker<0xD0&&(marker&3)!=0)){ + /*Found a SOFn (start of frame) marker segment:*/ + if(segment_len>=8){ + *_height=_buf[offs+3]<<8|_buf[offs+4]; + *_width=_buf[offs+5]<<8|_buf[offs+6]; + *_depth=_buf[offs+2]*_buf[offs+7]; + *_colors=0; + *_has_palette=0; + } + break; + } + /*Other markers: skip the whole marker segment.*/ + offs+=segment_len; + } + } +} + +static int op_is_png(const unsigned char *_buf,size_t _buf_sz){ + return _buf_sz>=8&&memcmp(_buf,"\x89PNG\x0D\x0A\x1A\x0A",8)==0; +} + +/*Tries to extract the width, height, bits per pixel, and palette size of a + PNG. + On failure, simply leaves its outputs unmodified.*/ +static void op_extract_png_params(const unsigned char *_buf,size_t _buf_sz, + opus_uint32 *_width,opus_uint32 *_height, + opus_uint32 *_depth,opus_uint32 *_colors,int *_has_palette){ + if(op_is_png(_buf,_buf_sz)){ + size_t offs; + offs=8; + while(_buf_sz-offs>=12){ + ogg_uint32_t chunk_len; + chunk_len=op_parse_uint32be(_buf+offs); + if(chunk_len>_buf_sz-(offs+12))break; + else if(chunk_len==13&&memcmp(_buf+offs+4,"IHDR",4)==0){ + int color_type; + *_width=op_parse_uint32be(_buf+offs+8); + *_height=op_parse_uint32be(_buf+offs+12); + color_type=_buf[offs+17]; + if(color_type==3){ + *_depth=24; + *_has_palette=1; + } + else{ + int sample_depth; + sample_depth=_buf[offs+16]; + if(color_type==0)*_depth=sample_depth; + else if(color_type==2)*_depth=sample_depth*3; + else if(color_type==4)*_depth=sample_depth*2; + else if(color_type==6)*_depth=sample_depth*4; + *_colors=0; + *_has_palette=0; + break; + } + } + else if(*_has_palette>0&&memcmp(_buf+offs+4,"PLTE",4)==0){ + *_colors=chunk_len/3; + break; + } + offs+=12+chunk_len; + } + } +} + +static int op_is_gif(const unsigned char *_buf,size_t _buf_sz){ + return _buf_sz>=6&&(memcmp(_buf,"GIF87a",6)==0||memcmp(_buf,"GIF89a",6)==0); +} + +/*Tries to extract the width, height, bits per pixel, and palette size of a + GIF. + On failure, simply leaves its outputs unmodified.*/ +static void op_extract_gif_params(const unsigned char *_buf,size_t _buf_sz, + opus_uint32 *_width,opus_uint32 *_height, + opus_uint32 *_depth,opus_uint32 *_colors,int *_has_palette){ + if(op_is_gif(_buf,_buf_sz)&&_buf_sz>=14){ + *_width=_buf[6]|_buf[7]<<8; + *_height=_buf[8]|_buf[9]<<8; + /*libFLAC hard-codes the depth to 24.*/ + *_depth=24; + *_colors=1<<((_buf[10]&7)+1); + *_has_palette=1; + } +} + +/*The actual implementation of opus_picture_tag_parse(). + Unlike the public API, this function requires _pic to already be + initialized, modifies its contents before success is guaranteed, and assumes + the caller will clear it on error.*/ +static int opus_picture_tag_parse_impl(OpusPictureTag *_pic,const char *_tag, + unsigned char *_buf,size_t _buf_sz,size_t _base64_sz){ + opus_int32 picture_type; + opus_uint32 mime_type_length; + char *mime_type; + opus_uint32 description_length; + char *description; + opus_uint32 width; + opus_uint32 height; + opus_uint32 depth; + opus_uint32 colors; + opus_uint32 data_length; + opus_uint32 file_width; + opus_uint32 file_height; + opus_uint32 file_depth; + opus_uint32 file_colors; + int format; + int has_palette; + int colors_set; + size_t i; + /*Decode the BASE64 data.*/ + for(i=0;i<_base64_sz;i++){ + opus_uint32 value; + int j; + value=0; + for(j=0;j<4;j++){ + unsigned c; + unsigned d; + c=(unsigned char)_tag[4*i+j]; + if(c=='+')d=62; + else if(c=='/')d=63; + else if(c>='0'&&c<='9')d=52+c-'0'; + else if(c>='a'&&c<='z')d=26+c-'a'; + else if(c>='A'&&c<='Z')d=c-'A'; + else if(c=='='&&3*i+j>_buf_sz)d=0; + else return OP_ENOTFORMAT; + value=value<<6|d; + } + _buf[3*i]=(unsigned char)(value>>16); + if(3*i+1<_buf_sz){ + _buf[3*i+1]=(unsigned char)(value>>8); + if(3*i+2<_buf_sz)_buf[3*i+2]=(unsigned char)value; + } + } + i=0; + picture_type=op_parse_uint32be(_buf+i); + i+=4; + /*Extract the MIME type.*/ + mime_type_length=op_parse_uint32be(_buf+i); + i+=4; + if(mime_type_length>_buf_sz-32)return OP_ENOTFORMAT; + mime_type=(char *)_ogg_malloc(sizeof(*_pic->mime_type)*(mime_type_length+1)); + if(mime_type==NULL)return OP_EFAULT; + memcpy(mime_type,_buf+i,sizeof(*mime_type)*mime_type_length); + mime_type[mime_type_length]='\0'; + _pic->mime_type=mime_type; + i+=mime_type_length; + /*Extract the description string.*/ + description_length=op_parse_uint32be(_buf+i); + i+=4; + if(description_length>_buf_sz-mime_type_length-32)return OP_ENOTFORMAT; + description= + (char *)_ogg_malloc(sizeof(*_pic->mime_type)*(description_length+1)); + if(description==NULL)return OP_EFAULT; + memcpy(description,_buf+i,sizeof(*description)*description_length); + description[description_length]='\0'; + _pic->description=description; + i+=description_length; + /*Extract the remaining fields.*/ + width=op_parse_uint32be(_buf+i); + i+=4; + height=op_parse_uint32be(_buf+i); + i+=4; + depth=op_parse_uint32be(_buf+i); + i+=4; + colors=op_parse_uint32be(_buf+i); + i+=4; + /*If one of these is set, they all must be, but colors==0 is a valid value.*/ + colors_set=width!=0||height!=0||depth!=0||colors!=0; + if(width==0||height==0||depth==0&&colors_set)return OP_ENOTFORMAT; + data_length=op_parse_uint32be(_buf+i); + i+=4; + if(data_length>_buf_sz-i)return OP_ENOTFORMAT; + /*Trim extraneous data so we don't copy it below.*/ + _buf_sz=i+data_length; + /*Attempt to determine the image format.*/ + format=OP_PIC_FORMAT_UNKNOWN; + if(mime_type_length==3&&strcmp(mime_type,"-->")==0){ + format=OP_PIC_FORMAT_URL; + /*Picture type 1 must be a 32x32 PNG.*/ + if(picture_type==1&&(width!=0||height!=0)&&(width!=32||height!=32)){ + return OP_ENOTFORMAT; + } + /*Append a terminating NUL for the convenience of our callers.*/ + _buf[_buf_sz++]='\0'; + } + else{ + if(mime_type_length==10 + &&op_strncasecmp(mime_type,"image/jpeg",mime_type_length)==0){ + if(op_is_jpeg(_buf+i,data_length))format=OP_PIC_FORMAT_JPEG; + } + else if(mime_type_length==9 + &&op_strncasecmp(mime_type,"image/png",mime_type_length)==0){ + if(op_is_png(_buf+i,data_length))format=OP_PIC_FORMAT_PNG; + } + else if(mime_type_length==9 + &&op_strncasecmp(mime_type,"image/gif",mime_type_length)==0){ + if(op_is_gif(_buf+i,data_length))format=OP_PIC_FORMAT_GIF; + } + else if(mime_type_length==0||(mime_type_length==6 + &&op_strncasecmp(mime_type,"image/",mime_type_length)==0)){ + if(op_is_jpeg(_buf+i,data_length))format=OP_PIC_FORMAT_JPEG; + else if(op_is_png(_buf+i,data_length))format=OP_PIC_FORMAT_PNG; + else if(op_is_gif(_buf+i,data_length))format=OP_PIC_FORMAT_GIF; + } + file_width=file_height=file_depth=file_colors=0; + has_palette=-1; + switch(format){ + case OP_PIC_FORMAT_JPEG:{ + op_extract_jpeg_params(_buf+i,data_length, + &file_width,&file_height,&file_depth,&file_colors,&has_palette); + }break; + case OP_PIC_FORMAT_PNG:{ + op_extract_png_params(_buf+i,data_length, + &file_width,&file_height,&file_depth,&file_colors,&has_palette); + }break; + case OP_PIC_FORMAT_GIF:{ + op_extract_gif_params(_buf+i,data_length, + &file_width,&file_height,&file_depth,&file_colors,&has_palette); + }break; + } + if(has_palette>=0){ + /*If we successfully extracted these parameters from the image, override + any declared values.*/ + width=file_width; + height=file_height; + depth=file_depth; + colors=file_colors; + } + /*Picture type 1 must be a 32x32 PNG.*/ + if(picture_type==1&&(format!=OP_PIC_FORMAT_PNG||width!=32||height!=32)){ + return OP_ENOTFORMAT; + } + } + /*Adjust _buf_sz instead of using data_length to capture the terminating NUL + for URLs.*/ + _buf_sz-=i; + memmove(_buf,_buf+i,sizeof(*_buf)*_buf_sz); + _buf=(unsigned char *)_ogg_realloc(_buf,_buf_sz); + if(_buf_sz>0&&_buf==NULL)return OP_EFAULT; + _pic->type=picture_type; + _pic->width=width; + _pic->height=height; + _pic->depth=depth; + _pic->colors=colors; + _pic->data_length=data_length; + _pic->data=_buf; + _pic->format=format; + return 0; +} + +int opus_picture_tag_parse(OpusPictureTag *_pic,const char *_tag){ + OpusPictureTag pic; + unsigned char *buf; + size_t base64_sz; + size_t buf_sz; + size_t tag_length; + int ret; + if(opus_tagncompare("METADATA_BLOCK_PICTURE",22,_tag)==0)_tag+=23; + /*Figure out how much BASE64-encoded data we have.*/ + tag_length=strlen(_tag); + if(tag_length&3)return OP_ENOTFORMAT; + base64_sz=tag_length>>2; + buf_sz=3*base64_sz; + if(buf_sz<32)return OP_ENOTFORMAT; + if(_tag[tag_length-1]=='=')buf_sz--; + if(_tag[tag_length-2]=='=')buf_sz--; + if(buf_sz<32)return OP_ENOTFORMAT; + /*Allocate an extra byte to allow appending a terminating NUL to URL data.*/ + buf=(unsigned char *)_ogg_malloc(sizeof(*buf)*(buf_sz+1)); + if(buf==NULL)return OP_EFAULT; + opus_picture_tag_init(&pic); + ret=opus_picture_tag_parse_impl(&pic,_tag,buf,buf_sz,base64_sz); + if(ret<0){ + opus_picture_tag_clear(&pic); + _ogg_free(buf); + } + else *_pic=*&pic; + return ret; +} + +void opus_picture_tag_init(OpusPictureTag *_pic){ + memset(_pic,0,sizeof(*_pic)); +} + +void opus_picture_tag_clear(OpusPictureTag *_pic){ + _ogg_free(_pic->description); + _ogg_free(_pic->mime_type); + _ogg_free(_pic->data); +} diff --git a/code/opusfile-0.2/src/internal.c b/code/opusfile-0.5/src/internal.c similarity index 96% rename from code/opusfile-0.2/src/internal.c rename to code/opusfile-0.5/src/internal.c index 2d2e3c85..96c80def 100644 --- a/code/opusfile-0.2/src/internal.c +++ b/code/opusfile-0.5/src/internal.c @@ -9,6 +9,10 @@ * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * * * ********************************************************************/ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include "internal.h" #if defined(OP_ENABLE_ASSERTIONS) diff --git a/code/opusfile-0.2/src/internal.h b/code/opusfile-0.5/src/internal.h similarity index 85% rename from code/opusfile-0.2/src/internal.h rename to code/opusfile-0.5/src/internal.h index 79416ae3..08114919 100644 --- a/code/opusfile-0.2/src/internal.h +++ b/code/opusfile-0.5/src/internal.h @@ -32,10 +32,22 @@ # include typedef struct OggOpusLink OggOpusLink; + # if defined(OP_FIXED_POINT) + typedef opus_int16 op_sample; + # else + typedef float op_sample; + +/*We're using this define to test for libopus 1.1 or later until libopus + provides a better mechanism.*/ +# if defined(OPUS_GET_EXPERT_FRAME_DURATION_REQUEST) +/*Enable soft clipping prevention in 16-bit decodes.*/ +# define OP_SOFT_CLIP (1) +# endif + # endif # if OP_GNUC_PREREQ(4,2) @@ -80,8 +92,10 @@ void op_fatal_impl(const char *_str,const char *_file,int _line); # define OP_ALWAYS_TRUE(_cond) ((void)(_cond)) # endif -# define OP_INT64_MAX ((ogg_int64_t)0x7FFFFFFFFFFFFFFFLL) +# define OP_INT64_MAX (2*(((ogg_int64_t)1<<62)-1)|1) # define OP_INT64_MIN (-OP_INT64_MAX-1) +# define OP_INT32_MAX (2*(((ogg_int32_t)1<<30)-1)|1) +# define OP_INT32_MIN (-OP_INT32_MAX-1) # define OP_MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) # define OP_MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) @@ -90,7 +104,7 @@ void op_fatal_impl(const char *_str,const char *_file,int _line); /*Advance a file offset by the given amount, clamping against OP_INT64_MAX. This is used to advance a known offset by things like OP_CHUNK_SIZE or OP_PAGE_SIZE_MAX, while making sure to avoid signed overflow. - It assumes that both _offset and _amount are positive.*/ + It assumes that both _offset and _amount are non-negative.*/ #define OP_ADV_OFFSET(_offset,_amount) \ (OP_MIN(_offset,OP_INT64_MAX-(_amount))+(_amount)) @@ -189,6 +203,10 @@ struct OggOpusFile{ int op_count; /*Central working state for the packet-to-PCM decoder.*/ OpusMSDecoder *od; + /*The application-provided packet decode callback.*/ + op_decode_cb_func decode_cb; + /*The application-provided packet decode callback context.*/ + void *decode_cb_ctx; /*The stream count used to initialize the decoder.*/ int od_stream_count; /*The coupled stream count used to initialize the decoder.*/ @@ -203,12 +221,26 @@ struct OggOpusFile{ int od_buffer_pos; /*The number of valid samples in the decoded buffer.*/ int od_buffer_size; - /*Internal state for dithering float->short output.*/ + /*The type of gain offset to apply. + One of OP_HEADER_GAIN, OP_TRACK_GAIN, or OP_ABSOLUTE_GAIN.*/ + int gain_type; + /*The offset to apply to the gain.*/ + opus_int32 gain_offset_q8; + /*Internal state for soft clipping and dithering float->short output.*/ #if !defined(OP_FIXED_POINT) +# if defined(OP_SOFT_CLIP) + float clip_state[OP_NCHANNELS_MAX]; +# endif float dither_a[OP_NCHANNELS_MAX*4]; float dither_b[OP_NCHANNELS_MAX*4]; - int dither_mute; opus_uint32 dither_seed; + int dither_mute; + int dither_disabled; + /*The number of channels represented by the internal state. + This gets set to 0 whenever anything that would prevent state propagation + occurs (switching between the float/short APIs, or between the + stereo/multistream APIs).*/ + int state_channel_count; #endif }; diff --git a/code/opusfile-0.2/src/opusfile.c b/code/opusfile-0.5/src/opusfile.c similarity index 86% rename from code/opusfile-0.2/src/opusfile.c rename to code/opusfile-0.5/src/opusfile.c index 812c2c45..392ddb29 100644 --- a/code/opusfile-0.2/src/opusfile.c +++ b/code/opusfile-0.5/src/opusfile.c @@ -14,6 +14,10 @@ last mod: $Id: vorbisfile.c 17573 2010-10-27 14:53:59Z xiphmont $ ********************************************************************/ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include "internal.h" #include #include @@ -163,7 +167,7 @@ static int op_seek_helper(OggOpusFile *_of,opus_int64 _offset){ /*Get the current position indicator of the underlying source. This should be the same as the value reported by tell().*/ -static opus_int64 op_position(OggOpusFile *_of){ +static opus_int64 op_position(const OggOpusFile *_of){ /*The current position indicator is _not_ simply offset. We may also have unprocessed, buffered data in the sync state.*/ return _of->offset+_of->oy.fill-_of->oy.returned; @@ -182,9 +186,8 @@ static opus_int64 op_position(OggOpusFile *_of){ OP_BADLINK: We hit end-of-file before reaching _boundary.*/ static opus_int64 op_get_next_page(OggOpusFile *_of,ogg_page *_og, opus_int64 _boundary){ - for(;;){ + while(_boundary<=0||_of->offset<_boundary){ int more; - if(_boundary>0&&_of->offset>=_boundary)return OP_FALSE; more=ogg_sync_pageseek(&_of->oy,_og); /*Skipped (-more) bytes.*/ if(OP_UNLIKELY(more<0))_of->offset-=more; @@ -211,17 +214,19 @@ static opus_int64 op_get_next_page(OggOpusFile *_of,ogg_page *_og, } else{ /*Got a page. - Return the offset at the page beginning, advance the internal offset - past the page end.*/ + Return the page start offset and advance the internal offset past the + page end.*/ opus_int64 page_offset; page_offset=_of->offset; _of->offset+=more; + OP_ASSERT(page_offset>=0); return page_offset; } } + return OP_FALSE; } -static int op_add_serialno(ogg_page *_og, +static int op_add_serialno(const ogg_page *_og, ogg_uint32_t **_serialnos,int *_nserialnos,int *_cserialnos){ ogg_uint32_t *serialnos; int nserialnos; @@ -235,7 +240,8 @@ static int op_add_serialno(ogg_page *_og, if(OP_UNLIKELY(cserialnos>INT_MAX-1>>1))return OP_EFAULT; cserialnos=2*cserialnos+1; OP_ASSERT(nserialnos=0 implies we entered the if(page_gp!=-1) block at least once.*/ + gp=-1; chunk_size=OP_CHUNK_SIZE; do{ int left_link; @@ -422,7 +431,7 @@ static opus_int64 op_get_last_page(OggOpusFile *_of,ogg_int64_t *_gp, opus_int64 llret; ogg_uint32_t serialno; llret=op_get_next_page(_of,&og,end); - if(OP_UNLIKELY(llretop[op_count].packet, _of->op[op_count].bytes); if(OP_LIKELY(_durations[op_count]>0)){ @@ -990,7 +1003,7 @@ static int op_find_final_pcm_offset(OggOpusFile *_of, /*Rescale the number _x from the range [0,_from] to [0,_to]. _from and _to must be positive.*/ -opus_int64 op_rescale64(opus_int64 _x,opus_int64 _from,opus_int64 _to){ +static opus_int64 op_rescale64(opus_int64 _x,opus_int64 _from,opus_int64 _to){ opus_int64 frac; opus_int64 ret; int i; @@ -1124,7 +1137,7 @@ static int op_bisect_forward_serialno(OggOpusFile *_of, if(OP_UNLIKELY(clinks>INT_MAX-1>>1))return OP_EFAULT; clinks=2*clinks+1; OP_ASSERT(nlinkslinks=links; } @@ -1134,7 +1147,7 @@ static int op_bisect_forward_serialno(OggOpusFile *_of, (potentially not a page we care about).*/ /*Scan the seek records we already have to save us some bisection.*/ for(sri=0;srilinks=links; /*We also don't need these anymore.*/ _ogg_free(*_serialnos); @@ -1282,12 +1295,49 @@ static int op_bisect_forward_serialno(OggOpusFile *_of, return 0; } +static void op_update_gain(OggOpusFile *_of){ + OpusHead *head; + opus_int32 gain_q8; + int li; + /*If decode isn't ready, then we'll apply the gain when we initialize the + decoder.*/ + if(_of->ready_stategain_offset_q8; + li=_of->seekable?_of->cur_link:0; + head=&_of->links[li].head; + /*We don't have to worry about overflow here because the header gain and + track gain must lie in the range [-32768,32767], and the user-supplied + offset has been pre-clamped to [-98302,98303].*/ + switch(_of->gain_type){ + case OP_TRACK_GAIN:{ + int track_gain_q8; + track_gain_q8=0; + opus_tags_get_track_gain(&_of->links[li].tags,&track_gain_q8); + gain_q8+=track_gain_q8; + } + /*Fall through.*/ + case OP_HEADER_GAIN:gain_q8+=head->output_gain;break; + case OP_ABSOLUTE_GAIN:break; + default:OP_ASSERT(0); + } + gain_q8=OP_CLAMP(-32768,gain_q8,32767); + OP_ASSERT(_of->od!=NULL); +#if defined(OPUS_SET_GAIN) + opus_multistream_decoder_ctl(_of->od,OPUS_SET_GAIN(gain_q8)); +#else +/*A fallback that works with both float and fixed-point is a bunch of work, + so just force people to use a sufficiently new version. + This is deployed well enough at this point that this shouldn't be a burden.*/ +# error "libopus 1.0.1 or later required" +#endif +} + static int op_make_decode_ready(OggOpusFile *_of){ - OpusHead *head; - int li; - int stream_count; - int coupled_count; - int channel_count; + const OpusHead *head; + int li; + int stream_count; + int coupled_count; + int channel_count; if(_of->ready_state>OP_STREAMSET)return 0; if(OP_UNLIKELY(_of->ready_stateseekable?_of->cur_link:0; @@ -1313,23 +1363,16 @@ static int op_make_decode_ready(OggOpusFile *_of){ _of->od_channel_count=channel_count; memcpy(_of->od_mapping,head->mapping,sizeof(*head->mapping)*channel_count); } -#if defined(OPUS_SET_GAIN) - opus_multistream_decoder_ctl(_of->od,OPUS_SET_GAIN(head->output_gain)); -#else -/*A fallback that works with both float and fixed-point is a bunch of work, - so just force people to use a sufficiently new version. - This is deployed well enough at this point that this shouldn't be a burden.*/ -# error "libopus 1.0.1 or later required" -#endif _of->ready_state=OP_INITSET; _of->bytes_tracked=0; _of->samples_tracked=0; #if !defined(OP_FIXED_POINT) - _of->dither_mute=65; + _of->state_channel_count=0; /*Use the serial number for the PRNG seed to get repeatable output for straight play-throughs.*/ _of->dither_seed=_of->links[li].serialno; #endif + op_update_gain(_of); return 0; } @@ -1368,7 +1411,15 @@ static int op_open_seekable2(OggOpusFile *_of){ int start_op_count; int ret; /*We're partially open and have a first link header state in storage in _of. - Save off that stream state so we can come back to it.*/ + Save off that stream state so we can come back to it. + It would be simpler to just dump all this state and seek back to + links[0].data_offset when we're done. + But we do the extra work to allow us to seek back to _exactly_ the same + stream position we're at now. + This allows, e.g., the HTTP backend to continue reading from the original + connection (if it's still available), instead of opening a new one. + This means we can open and start playing a normal Opus file with a single + link and reasonable packet sizes using only two HTTP requests.*/ start_op_count=_of->op_count; /*This is a bit too large to put on the stack unconditionally.*/ op_start=(ogg_packet *)_ogg_malloc(sizeof(*op_start)*start_op_count); @@ -1468,14 +1519,12 @@ static int op_open1(OggOpusFile *_of, seekable=_cb->seek!=NULL&&(*_cb->seek)(_source,0,SEEK_CUR)!=-1; /*If seek is implemented, tell must also be implemented.*/ if(seekable){ + opus_int64 pos; if(OP_UNLIKELY(_of->callbacks.tell==NULL))return OP_EINVAL; - else{ - opus_int64 pos; - pos=(*_of->callbacks.tell)(_of->source); - /*If the current position is not equal to the initial bytes consumed, - absolute seeking will not work.*/ - if(OP_UNLIKELY(pos!=(opus_int64)_initial_bytes))return OP_EINVAL; - } + pos=(*_of->callbacks.tell)(_of->source); + /*If the current position is not equal to the initial bytes consumed, + absolute seeking will not work.*/ + if(OP_UNLIKELY(pos!=(opus_int64)_initial_bytes))return OP_EINVAL; } _of->seekable=seekable; /*Don't seek yet. @@ -1506,12 +1555,7 @@ static int op_open1(OggOpusFile *_of, if(!seekable)_of->cur_link++; pog=&og; } - if(OP_UNLIKELY(ret<0)){ - /*Don't auto-close the stream on failure.*/ - _of->callbacks.close=NULL; - op_clear(_of); - } - else _of->ready_state=OP_PARTOPEN; + if(OP_LIKELY(ret>=0))_of->ready_state=OP_PARTOPEN; return ret; } @@ -1548,6 +1592,9 @@ OggOpusFile *op_test_callbacks(void *_source,const OpusFileCallbacks *_cb, if(_error!=NULL)*_error=0; return of; } + /*Don't auto-close the stream on failure.*/ + of->callbacks.close=NULL; + op_clear(of); _ogg_free(of); } if(_error!=NULL)*_error=ret; @@ -1594,18 +1641,6 @@ OggOpusFile *op_open_memory(const unsigned char *_data,size_t _size, _error); } -OggOpusFile *op_vopen_url(const char *_url,int *_error,va_list _ap){ - OpusFileCallbacks cb; - return op_open_close_on_failure(op_url_stream_vcreate(&cb,_url,_ap),&cb, - _error); -} - -OggOpusFile *op_open_url(const char *_url,int *_error,...){ - va_list ap; - va_start(ap,_error); - return op_vopen_url(_url,_error,ap); -} - /*Convenience routine to clean up from failure for the open functions that create their own streams.*/ static OggOpusFile *op_test_close_on_failure(void *_source, @@ -1632,18 +1667,6 @@ OggOpusFile *op_test_memory(const unsigned char *_data,size_t _size, _error); } -OggOpusFile *op_vtest_url(const char *_url,int *_error,va_list _ap){ - OpusFileCallbacks cb; - return op_test_close_on_failure(op_url_stream_vcreate(&cb,_url,_ap),&cb, - _error); -} - -OggOpusFile *op_test_url(const char *_url,int *_error,...){ - va_list ap; - va_start(ap,_error); - return op_vtest_url(_url,_error,ap); -} - int op_test_open(OggOpusFile *_of){ int ret; if(OP_UNLIKELY(_of->ready_state!=OP_PARTOPEN))return OP_EINVAL; @@ -1661,25 +1684,25 @@ void op_free(OggOpusFile *_of){ } } -int op_seekable(OggOpusFile *_of){ +int op_seekable(const OggOpusFile *_of){ return _of->seekable; } -int op_link_count(OggOpusFile *_of){ +int op_link_count(const OggOpusFile *_of){ return _of->nlinks; } -ogg_uint32_t op_serialno(OggOpusFile *_of,int _li){ +ogg_uint32_t op_serialno(const OggOpusFile *_of,int _li){ if(OP_UNLIKELY(_li>=_of->nlinks))_li=_of->nlinks-1; if(!_of->seekable)_li=0; return _of->links[_li<0?_of->cur_link:_li].serialno; } -int op_channel_count(OggOpusFile *_of,int _li){ +int op_channel_count(const OggOpusFile *_of,int _li){ return op_head(_of,_li)->channel_count; } -opus_int64 op_raw_total(OggOpusFile *_of,int _li){ +opus_int64 op_raw_total(const OggOpusFile *_of,int _li){ if(OP_UNLIKELY(_of->ready_stateseekable) ||OP_UNLIKELY(_li>=_of->nlinks)){ @@ -1690,7 +1713,7 @@ opus_int64 op_raw_total(OggOpusFile *_of,int _li){ -_of->links[_li].offset; } -ogg_int64_t op_pcm_total(OggOpusFile *_of,int _li){ +ogg_int64_t op_pcm_total(const OggOpusFile *_of,int _li){ OggOpusLink *links; ogg_int64_t diff; int nlinks; @@ -1720,13 +1743,13 @@ ogg_int64_t op_pcm_total(OggOpusFile *_of,int _li){ return diff-links[_li].head.pre_skip; } -const OpusHead *op_head(OggOpusFile *_of,int _li){ +const OpusHead *op_head(const OggOpusFile *_of,int _li){ if(OP_UNLIKELY(_li>=_of->nlinks))_li=_of->nlinks-1; if(!_of->seekable)_li=0; return &_of->links[_li<0?_of->cur_link:_li].head; } -const OpusTags *op_tags(OggOpusFile *_of,int _li){ +const OpusTags *op_tags(const OggOpusFile *_of,int _li){ if(OP_UNLIKELY(_li>=_of->nlinks))_li=_of->nlinks-1; if(!_of->seekable){ if(_of->ready_stateready_state!=OP_PARTOPEN){ @@ -1738,7 +1761,7 @@ const OpusTags *op_tags(OggOpusFile *_of,int _li){ return &_of->links[_li].tags; } -int op_current_link(OggOpusFile *_of){ +int op_current_link(const OggOpusFile *_of){ if(OP_UNLIKELY(_of->ready_statecur_link; } @@ -1749,11 +1772,13 @@ static opus_int32 op_calc_bitrate(opus_int64 _bytes,ogg_int64_t _samples){ /*These rates are absurd, but let's handle them anyway.*/ if(OP_UNLIKELY(_bytes>(OP_INT64_MAX-(_samples>>1))/(48000*8))){ ogg_int64_t den; - if(OP_UNLIKELY(_bytes/(0x7FFFFFFFF/(48000*8))>=_samples))return 0x7FFFFFFF; + if(OP_UNLIKELY(_bytes/(OP_INT32_MAX/(48000*8))>=_samples)){ + return OP_INT32_MAX; + } den=_samples/(48000*8); return (opus_int32)((_bytes+(den>>1))/den); } - if(OP_UNLIKELY(_samples<=0))return 0x7FFFFFFF; + if(OP_UNLIKELY(_samples<=0))return OP_INT32_MAX; /*This can't actually overflow in normal operation: even with a pre-skip of 545 2.5 ms frames with 8 streams running at 1282*8+1 bytes per packet (1275 byte frames + Opus framing overhead + Ogg lacing values), that all @@ -1761,10 +1786,11 @@ static opus_int32 op_calc_bitrate(opus_int64 _bytes,ogg_int64_t _samples){ The only way to get bitrates larger than that is with excessive Opus padding, more encoded streams than output channels, or lots and lots of Ogg pages with no packets on them.*/ - return (opus_int32)OP_MIN((_bytes*48000*8+(_samples>>1))/_samples,0x7FFFFFFF); + return (opus_int32)OP_MIN((_bytes*48000*8+(_samples>>1))/_samples, + OP_INT32_MAX); } -opus_int32 op_bitrate(OggOpusFile *_of,int _li){ +opus_int32 op_bitrate(const OggOpusFile *_of,int _li){ if(OP_UNLIKELY(_of->ready_stateseekable) ||OP_UNLIKELY(_li>=_of->nlinks)){ return OP_EINVAL; @@ -1800,11 +1826,8 @@ static int op_fetch_and_process_page(OggOpusFile *_of, int seekable; int cur_link; int ret; - if(OP_LIKELY(_of->ready_state>=OP_INITSET) - &&OP_LIKELY(_of->op_pos<_of->op_count)){ - /*We're ready to decode and have at least one packet available already.*/ - return 1; - } + /*We shouldn't get here if we have unprocessed packets.*/ + OP_ASSERT(_of->ready_stateop_pos>=_of->op_count); if(!_readp)return 0; seekable=_of->seekable; links=_of->links; @@ -2059,7 +2082,7 @@ static int op_fetch_and_process_page(OggOpusFile *_of, } int op_raw_seek(OggOpusFile *_of,opus_int64 _pos){ - int ret; + int ret; if(OP_UNLIKELY(_of->ready_stateseekable))return OP_ENOSEEK; @@ -2090,10 +2113,10 @@ int op_raw_seek(OggOpusFile *_of,opus_int64 _pos){ position in an individual link.*/ static ogg_int64_t op_get_granulepos(const OggOpusFile *_of, ogg_int64_t _pcm_offset,int *_li){ - OggOpusLink *links; - ogg_int64_t duration; - int nlinks; - int li; + const OggOpusLink *links; + ogg_int64_t duration; + int nlinks; + int li; OP_ASSERT(_pcm_offset>=0); nlinks=_of->nlinks; links=_of->links; @@ -2128,6 +2151,12 @@ static ogg_int64_t op_get_granulepos(const OggOpusFile *_of, Two minutes seems to be a good default.*/ #define OP_CUR_TIME_THRESH (120*48*(opus_int32)1000) +/*Note: The OP_SMALL_FOOTPRINT #define doesn't (currently) save much code size, + but it's meant to serve as documentation for portions of the seeking + algorithm that are purely optional, to aid others learning from/porting this + code to other contexts.*/ +/*#define OP_SMALL_FOOTPRINT (1)*/ + /*Search within link _li for the page with the highest granule position preceding (or equal to) _target_gp. There is a danger here: missing pages or incorrect frame number information @@ -2135,27 +2164,27 @@ static ogg_int64_t op_get_granulepos(const OggOpusFile *_of, Account for that (and report it as an error condition).*/ static int op_pcm_seek_page(OggOpusFile *_of, ogg_int64_t _target_gp,int _li){ - OggOpusLink *link; - ogg_page og; - ogg_int64_t pcm_pre_skip; - ogg_int64_t pcm_start; - ogg_int64_t pcm_end; - ogg_int64_t best_gp; - ogg_int64_t diff; - ogg_uint32_t serialno; - opus_int32 pre_skip; - opus_int32 cur_discard_count; - opus_int64 begin; - opus_int64 end; - opus_int64 boundary; - opus_int64 best; - opus_int64 page_offset; - opus_int64 d[3]; - int force_bisect; - int ret; + const OggOpusLink *link; + ogg_page og; + ogg_int64_t pcm_pre_skip; + ogg_int64_t pcm_start; + ogg_int64_t pcm_end; + ogg_int64_t best_gp; + ogg_int64_t diff; + ogg_uint32_t serialno; + opus_int32 pre_skip; + opus_int64 begin; + opus_int64 end; + opus_int64 boundary; + opus_int64 best; + opus_int64 page_offset; + opus_int64 d0; + opus_int64 d1; + opus_int64 d2; + int force_bisect; + int ret; _of->bytes_tracked=0; _of->samples_tracked=0; - /*New search algorithm by HB (Nicholas Vinen).*/ link=_of->links+_li; best_gp=pcm_start=link->pcm_start; pcm_end=link->pcm_end; @@ -2165,7 +2194,8 @@ static int op_pcm_seek_page(OggOpusFile *_of, /*We discard the first 80 ms of data after a seek, so seek back that much farther. If we can't, simply seek to the beginning of the link.*/ - if(OP_UNLIKELY(op_granpos_add(&_target_gp,_target_gp,-80*48)<0)){ + if(OP_UNLIKELY(op_granpos_add(&_target_gp,_target_gp,-80*48)<0) + ||OP_UNLIKELY(op_granpos_cmp(_target_gp,pcm_start)<0)){ _target_gp=pcm_start; } /*Special case seeking to the start of the link.*/ @@ -2174,6 +2204,7 @@ static int op_pcm_seek_page(OggOpusFile *_of, if(op_granpos_cmp(_target_gp,pcm_pre_skip)<0)end=boundary=begin; else{ end=boundary=link->end_offset; +#if !defined(OP_SMALL_FOOTPRINT) /*If we were decoding from this link, we can narrow the range a bit.*/ if(_li==_of->cur_link&&_of->ready_state>=OP_INITSET){ opus_int64 offset; @@ -2187,15 +2218,15 @@ static int op_pcm_seek_page(OggOpusFile *_of, offset=_of->offset; if(op_count>0&&OP_LIKELY(offset<=end)){ ogg_int64_t gp; - gp=_of->op[op_count-1].granulepos; /*Make sure the timestamp is valid. The granule position might be -1 if we collected the packets from a page without a granule position after reporting a hole.*/ + gp=_of->op[op_count-1].granulepos; if(OP_LIKELY(gp!=-1)&&OP_LIKELY(op_granpos_cmp(pcm_start,gp)<0) &&OP_LIKELY(op_granpos_cmp(pcm_end,gp)>0)){ OP_ALWAYS_TRUE(!op_granpos_diff(&diff,gp,_target_gp)); /*We only actually use the current time if either - a) We can cut off more than half the range, or + a) We can cut off at least half the range, or b) We're seeking sufficiently close to the current position that it's likely to be informative. Otherwise it appears using the whole link range to estimate the @@ -2207,18 +2238,44 @@ static int op_pcm_seek_page(OggOpusFile *_of, best_gp=pcm_start=gp; } } - else if(offset-begin<=end-begin>>1||diffop[0].granulepos, + op_get_packet_duration(_of->op[0].packet,_of->op[0].bytes))); + if(op_granpos_cmp(prev_page_gp,_target_gp)<=0){ + /*Don't call op_decode_clear(), because it will dump our + packets.*/ + _of->op_pos=0; + _of->od_buffer_size=0; + _of->prev_packet_gp=prev_page_gp; + _of->ready_state=OP_STREAMSET; + return op_make_decode_ready(_of); + } + /*No such luck. + Check if we can cut off at least half the range, though.*/ + if(offset-begin<=end-begin>>1||diff>1; - d[1]=d[2]>>1; - d[2]=end-begin>>1; + d0=d1>>1; + d1=d2>>1; + d2=end-begin>>1; if(force_bisect)bisect=begin+(end-begin>>1); else{ ogg_int64_t diff2; @@ -2307,7 +2364,7 @@ static int op_pcm_seek_page(OggOpusFile *_of, boundary=next_boundary; /*If we're not making much progress shrinking the interval size, start forcing straight bisection to limit the worst case.*/ - force_bisect=end-begin>d[0]*2; + force_bisect=end-begin>d0*2; /*Don't let pcm_end get out of range! That could happen with an invalid timestamp.*/ if(OP_LIKELY(op_granpos_cmp(pcm_end,gp)>0) @@ -2321,7 +2378,8 @@ static int op_pcm_seek_page(OggOpusFile *_of, } } /*Found our page. - Seek right after it and update prev_packet_gp and cur_discard_count. + Seek to the end of it and update prev_packet_gp. + Our caller will set cur_discard_count. This is an easier case than op_raw_seek(), as we don't need to keep any packets from the page we found.*/ /*Seek, if necessary.*/ @@ -2330,21 +2388,10 @@ static int op_pcm_seek_page(OggOpusFile *_of, ret=op_seek_helper(_of,best); if(OP_UNLIKELY(ret<0))return ret; } - /*By default, discard 80 ms of data after a seek, unless we seek - into the pre-skip region.*/ - cur_discard_count=80*48; - OP_ALWAYS_TRUE(!op_granpos_diff(&diff,best_gp,pcm_start)); - OP_ASSERT(diff>=0); - /*If we start at the beginning of the pre-skip region, or we're at least - 80 ms from the end of the pre-skip region, we discard to the end of the - pre-skip region. - Otherwise, we still use the 80 ms default, which will discard past the end - of the pre-skip region.*/ - if(diff<=OP_MAX(0,pre_skip-80*48))cur_discard_count=pre_skip-(int)diff; + OP_ASSERT(op_granpos_cmp(best_gp,pcm_start)>=0); _of->cur_link=_li; _of->ready_state=OP_STREAMSET; _of->prev_packet_gp=best_gp; - _of->cur_discard_count=cur_discard_count; ogg_stream_reset_serialno(&_of->os,serialno); ret=op_fetch_and_process_page(_of,page_offset<0?NULL:&og,page_offset,1,0,1); if(OP_UNLIKELY(ret<=0))return OP_EBADLINK; @@ -2356,31 +2403,60 @@ static int op_pcm_seek_page(OggOpusFile *_of, } int op_pcm_seek(OggOpusFile *_of,ogg_int64_t _pcm_offset){ - OggOpusLink *link; - ogg_int64_t pcm_start; - ogg_int64_t target_gp; - ogg_int64_t prev_packet_gp; - ogg_int64_t skip; - ogg_int64_t diff; - int op_count; - int op_pos; - int ret; - int li; + const OggOpusLink *link; + ogg_int64_t pcm_start; + ogg_int64_t target_gp; + ogg_int64_t prev_packet_gp; + ogg_int64_t skip; + ogg_int64_t diff; + int op_count; + int op_pos; + int ret; + int li; if(OP_UNLIKELY(_of->ready_stateseekable))return OP_ENOSEEK; if(OP_UNLIKELY(_pcm_offset<0))return OP_EINVAL; target_gp=op_get_granulepos(_of,_pcm_offset,&li); if(OP_UNLIKELY(target_gp==-1))return OP_EINVAL; - ret=op_pcm_seek_page(_of,target_gp,li); - /*Now skip samples until we actually get to our target.*/ link=_of->links+li; pcm_start=link->pcm_start; OP_ALWAYS_TRUE(!op_granpos_diff(&_pcm_offset,target_gp,pcm_start)); +#if !defined(OP_SMALL_FOOTPRINT) + /*For small (90 ms or less) forward seeks within the same link, just decode + forward. + This also optimizes the case of seeking to the current position.*/ + if(li==_of->cur_link&&_of->ready_state>=OP_INITSET){ + ogg_int64_t gp; + gp=_of->prev_packet_gp; + if(OP_LIKELY(gp!=-1)){ + int nbuffered; + nbuffered=OP_MAX(_of->od_buffer_size-_of->od_buffer_pos,0); + OP_ALWAYS_TRUE(!op_granpos_add(&gp,gp,-nbuffered)); + /*We do _not_ add cur_discard_count to gp. + Otherwise the total amount to discard could grow without bound, and it + would be better just to do a full seek.*/ + if(OP_LIKELY(!op_granpos_diff(&diff,gp,pcm_start))){ + ogg_int64_t discard_count; + discard_count=_pcm_offset-diff; + /*We use a threshold of 90 ms instead of 80, since 80 ms is the + _minimum_ we would have discarded after a full seek. + Assuming 20 ms frames (the default), we'd discard 90 ms on average.*/ + if(discard_count>=0&&OP_UNLIKELY(discard_count<90*48)){ + _of->cur_discard_count=(opus_int32)discard_count; + return 0; + } + } + } + } +#endif + ret=op_pcm_seek_page(_of,target_gp,li); + if(OP_UNLIKELY(ret<0))return ret; + /*Now skip samples until we actually get to our target.*/ /*Figure out where we should skip to.*/ if(_pcm_offset<=link->head.pre_skip)skip=0; else skip=OP_MAX(_pcm_offset-80*48,0); OP_ASSERT(_pcm_offset-skip>=0); - OP_ASSERT(_pcm_offset-skip<0x7FFFFFFF-120*48); + OP_ASSERT(_pcm_offset-skipop_count; @@ -2406,7 +2482,7 @@ int op_pcm_seek(OggOpusFile *_of,ogg_int64_t _pcm_offset){ /*We skipped too far. Either the timestamps were illegal or there was a hole in the data.*/ if(diff>skip)return OP_EBADLINK; - OP_ASSERT(_pcm_offset-diff<0x7FFFFFFF); + OP_ASSERT(_pcm_offset-diffready_stateoffset; } @@ -2425,10 +2501,10 @@ opus_int64 op_raw_tell(OggOpusFile *_of){ For unseekable sources, this gets reset to 0 at the beginning of each link.*/ static ogg_int64_t op_get_pcm_offset(const OggOpusFile *_of, ogg_int64_t _gp,int _li){ - OggOpusLink *links; - ogg_int64_t pcm_offset; - ogg_int64_t delta; - int li; + const OggOpusLink *links; + ogg_int64_t pcm_offset; + ogg_int64_t delta; + int li; links=_of->links; pcm_offset=0; OP_ASSERT(_li<_of->nlinks); @@ -2443,15 +2519,23 @@ static ogg_int64_t op_get_pcm_offset(const OggOpusFile *_of, _gp=links[_li].pcm_end; } if(OP_LIKELY(op_granpos_cmp(_gp,links[_li].pcm_start)>0)){ - OP_ALWAYS_TRUE(!op_granpos_diff(&delta,_gp,links[_li].pcm_start)); + if(OP_UNLIKELY(op_granpos_diff(&delta,_gp,links[_li].pcm_start)<0)){ + /*This means an unseekable stream claimed to have a page from more than + 2 billion days after we joined.*/ + OP_ASSERT(!_of->seekable); + return OP_INT64_MAX; + } if(deltadecode_cb=_decode_cb; + _of->decode_cb_ctx=_ctx; +} + +int op_set_gain_offset(OggOpusFile *_of, + int _gain_type,opus_int32 _gain_offset_q8){ + if(_gain_type!=OP_HEADER_GAIN&&_gain_type!=OP_TRACK_GAIN + &&_gain_type!=OP_ABSOLUTE_GAIN){ + return OP_EINVAL; + } + _of->gain_type=_gain_type; + /*The sum of header gain and track gain lies in the range [-65536,65534]. + These bounds allow the offset to set the final value to anywhere in the + range [-32768,32767], which is what we'll clamp it to before applying.*/ + _of->gain_offset_q8=OP_CLAMP(-98302,_gain_offset_q8,98303); + op_update_gain(_of); + return 0; +} + +void op_set_dither_enabled(OggOpusFile *_of,int _enabled){ +#if !defined(OP_FIXED_POINT) + _of->dither_disabled=!_enabled; + if(!_enabled)_of->dither_mute=65; +#endif +} + /*Allocate the decoder scratch buffer. This is done lazily, since if the user provides large enough buffers, we'll never need it.*/ static int op_init_buffer(OggOpusFile *_of){ int nchannels_max; if(_of->seekable){ - OggOpusLink *links; - int nlinks; - int li; + const OggOpusLink *links; + int nlinks; + int li; links=_of->links; nlinks=_of->nlinks; nchannels_max=1; @@ -2490,6 +2602,39 @@ static int op_init_buffer(OggOpusFile *_of){ return 0; } +/*Decode a single packet into the target buffer.*/ +static int op_decode(OggOpusFile *_of,op_sample *_pcm, + const ogg_packet *_op,int _nsamples,int _nchannels){ + int ret; + /*First we try using the application-provided decode callback.*/ + if(_of->decode_cb!=NULL){ +#if defined(OP_FIXED_POINT) + ret=(*_of->decode_cb)(_of->decode_cb_ctx,_of->od,_pcm,_op, + _nsamples,_nchannels,OP_DEC_FORMAT_SHORT,_of->cur_link); +#else + ret=(*_of->decode_cb)(_of->decode_cb_ctx,_of->od,_pcm,_op, + _nsamples,_nchannels,OP_DEC_FORMAT_FLOAT,_of->cur_link); +#endif + } + else ret=OP_DEC_USE_DEFAULT; + /*If the application didn't want to handle decoding, do it ourselves.*/ + if(ret==OP_DEC_USE_DEFAULT){ +#if defined(OP_FIXED_POINT) + ret=opus_multistream_decode(_of->od, + _op->packet,_op->bytes,_pcm,_nsamples,0); +#else + ret=opus_multistream_decode_float(_of->od, + _op->packet,_op->bytes,_pcm,_nsamples,0); +#endif + OP_ASSERT(ret<0||ret==_nsamples); + } + /*If the application returned a positive value other than 0 or + OP_DEC_USE_DEFAULT, fail.*/ + else if(OP_UNLIKELY(ret>0))return OP_EBADPACKET; + if(OP_UNLIKELY(ret<0))return OP_EBADPACKET; + return ret; +} + /*Read more samples from the stream, using the same API as op_read() or op_read_float().*/ static int op_read_native(OggOpusFile *_of, @@ -2506,10 +2651,8 @@ static int op_read_native(OggOpusFile *_of, od_buffer_pos=_of->od_buffer_pos; nsamples=_of->od_buffer_size-od_buffer_pos; /*If we have buffered samples, return them.*/ - if(OP_UNLIKELY(nsamples>0)){ - if(OP_UNLIKELY(nsamples*nchannels>_buf_size)){ - nsamples=_buf_size/nchannels; - } + if(nsamples>0){ + if(nsamples*nchannels>_buf_size)nsamples=_buf_size/nchannels; memcpy(_pcm,_of->od_buffer+nchannels*od_buffer_pos, sizeof(*_pcm)*nchannels*nsamples); od_buffer_pos+=nsamples; @@ -2520,11 +2663,11 @@ static int op_read_native(OggOpusFile *_of, /*If we have buffered packets, decode one.*/ op_pos=_of->op_pos; if(OP_LIKELY(op_pos<_of->op_count)){ - ogg_packet *pop; - ogg_int64_t diff; - opus_int32 cur_discard_count; - int duration; - int trimmed_duration; + const ogg_packet *pop; + ogg_int64_t diff; + opus_int32 cur_discard_count; + int duration; + int trimmed_duration; pop=_of->op+op_pos++; _of->op_pos=op_pos; cur_discard_count=_of->cur_discard_count; @@ -2553,15 +2696,8 @@ static int op_read_native(OggOpusFile *_of, if(OP_UNLIKELY(ret<0))return ret; buf=_of->od_buffer; } -#if defined(OP_FIXED_POINT) - ret=opus_multistream_decode(_of->od, - pop->packet,pop->bytes,buf,120*48,0); -#else - ret=opus_multistream_decode_float(_of->od, - pop->packet,pop->bytes,buf,120*48,0); -#endif - if(OP_UNLIKELY(ret<0))return OP_EBADPACKET; - OP_ASSERT(ret==duration); + ret=op_decode(_of,buf,pop,duration,nchannels); + if(OP_UNLIKELY(ret<0))return ret; /*Perform pre-skip/pre-roll.*/ od_buffer_pos=(int)OP_MIN(trimmed_duration,cur_discard_count); cur_discard_count-=od_buffer_pos; @@ -2572,31 +2708,22 @@ static int op_read_native(OggOpusFile *_of, what was decoded.*/ _of->bytes_tracked+=pop->bytes; _of->samples_tracked+=trimmed_duration-od_buffer_pos; - /*Don't grab another page yet.*/ - if(OP_LIKELY(od_buffer_posod,pop->packet,pop->bytes, - _pcm,_buf_size/nchannels,0); -#else - ret=opus_multistream_decode_float(_of->od,pop->packet,pop->bytes, - _pcm,_buf_size/nchannels,0); -#endif - if(OP_UNLIKELY(ret<0))return OP_EBADPACKET; - OP_ASSERT(ret==duration); + ret=op_decode(_of,_pcm,pop,duration,nchannels); + if(OP_UNLIKELY(ret<0))return ret; if(OP_LIKELY(trimmed_duration>0)){ /*Perform pre-skip/pre-roll.*/ od_buffer_pos=(int)OP_MIN(trimmed_duration,cur_discard_count); cur_discard_count-=od_buffer_pos; _of->cur_discard_count=cur_discard_count; - if(OP_UNLIKELY(od_buffer_pos>0) - &&OP_LIKELY(od_buffer_pos0) + &&OP_UNLIKELY(od_buffer_pos>0)){ + memmove(_pcm,_pcm+od_buffer_pos*nchannels, + sizeof(*_pcm)*trimmed_duration*nchannels); + } /*Update bitrate tracking based on the actual samples we used from what was decoded.*/ _of->bytes_tracked+=pop->bytes; @@ -2607,6 +2734,9 @@ static int op_read_native(OggOpusFile *_of, } } } + /*Don't grab another page yet. + This one might have more packets, or might have buffered data now.*/ + continue; } } /*Suck in another page.*/ @@ -2619,12 +2749,15 @@ static int op_read_native(OggOpusFile *_of, } } +/*A generic filter to apply to the decoded audio data. + _src is non-const because we will destructively modify the contents of the + source buffer that we consume in some cases.*/ typedef int (*op_read_filter_func)(OggOpusFile *_of,void *_dst,int _dst_sz, op_sample *_src,int _nsamples,int _nchannels); /*Decode some samples and then apply a custom filter to them. This is used to convert to different output formats.*/ -static int op_read_native_filter(OggOpusFile *_of,void *_dst,int _dst_sz, +static int op_filter_read_native(OggOpusFile *_of,void *_dst,int _dst_sz, op_read_filter_func _filter,int *_li){ int ret; /*Ensure we have some decoded samples in our buffer.*/ @@ -2648,11 +2781,46 @@ static int op_read_native_filter(OggOpusFile *_of,void *_dst,int _dst_sz, return ret; } -#if defined(OP_FIXED_POINT) +#if !defined(OP_FIXED_POINT)||!defined(OP_DISABLE_FLOAT_API) -int op_read(OggOpusFile *_of,opus_int16 *_pcm,int _buf_size,int *_li){ - return op_read_native(_of,_pcm,_buf_size,_li); -} +/*Matrices for downmixing from the supported channel counts to stereo. + The matrices with 5 or more channels are normalized to a total volume of 2.0, + since most mixes sound too quiet if normalized to 1.0 (as there is generally + little volume in the side/rear channels).*/ +static const float OP_STEREO_DOWNMIX[OP_NCHANNELS_MAX-2][OP_NCHANNELS_MAX][2]={ + /*3.0*/ + { + {0.5858F,0.0F},{0.4142F,0.4142F},{0.0F,0.5858F} + }, + /*quadrophonic*/ + { + {0.4226F,0.0F},{0.0F,0.4226F},{0.366F,0.2114F},{0.2114F,0.336F} + }, + /*5.0*/ + { + {0.651F,0.0F},{0.46F,0.46F},{0.0F,0.651F},{0.5636F,0.3254F}, + {0.3254F,0.5636F} + }, + /*5.1*/ + { + {0.529F,0.0F},{0.3741F,0.3741F},{0.0F,0.529F},{0.4582F,0.2645F}, + {0.2645F,0.4582F},{0.3741F,0.3741F} + }, + /*6.1*/ + { + {0.4553F,0.0F},{0.322F,0.322F},{0.0F,0.4553F},{0.3943F,0.2277F}, + {0.2277F,0.3943F},{0.2788F,0.2788F},{0.322F,0.322F} + }, + /*7.1*/ + { + {0.3886F,0.0F},{0.2748F,0.2748F},{0.0F,0.3886F},{0.3366F,0.1943F}, + {0.1943F,0.3366F},{0.3366F,0.1943F},{0.1943F,0.3366F},{0.2748F,0.2748F} + } +}; + +#endif + +#if defined(OP_FIXED_POINT) /*Matrices for downmixing from the supported channel counts to stereo. The matrices with 5 or more channels are normalized to a total volume of 2.0, @@ -2690,9 +2858,13 @@ static const opus_int16 OP_STEREO_DOWNMIX_Q14 } }; +int op_read(OggOpusFile *_of,opus_int16 *_pcm,int _buf_size,int *_li){ + return op_read_native(_of,_pcm,_buf_size,_li); +} + static int op_stereo_filter(OggOpusFile *_of,void *_dst,int _dst_sz, op_sample *_src,int _nsamples,int _nchannels){ - _of=_of; + (void)_of; _nsamples=OP_MIN(_nsamples,_dst_sz>>1); if(_nchannels==2)memcpy(_dst,_src,_nsamples*2*sizeof(*_src)); else{ @@ -2714,6 +2886,7 @@ static int op_stereo_filter(OggOpusFile *_of,void *_dst,int _dst_sz, l+=OP_STEREO_DOWNMIX_Q14[_nchannels-3][ci][0]*s; r+=OP_STEREO_DOWNMIX_Q14[_nchannels-3][ci][1]*s; } + /*TODO: For 5 or more channels, we should do soft clipping here.*/ dst[2*i+0]=(opus_int16)OP_CLAMP(-32768,l+8192>>14,32767); dst[2*i+1]=(opus_int16)OP_CLAMP(-32768,r+8192>>14,32767); } @@ -2723,7 +2896,7 @@ static int op_stereo_filter(OggOpusFile *_of,void *_dst,int _dst_sz, } int op_read_stereo(OggOpusFile *_of,opus_int16 *_pcm,int _buf_size){ - return op_read_native_filter(_of,_pcm,_buf_size,op_stereo_filter,NULL); + return op_filter_read_native(_of,_pcm,_buf_size,op_stereo_filter,NULL); } # if !defined(OP_DISABLE_FLOAT_API) @@ -2732,7 +2905,7 @@ static int op_short2float_filter(OggOpusFile *_of,void *_dst,int _dst_sz, op_sample *_src,int _nsamples,int _nchannels){ float *dst; int i; - _of=_of; + (void)_of; dst=(float *)_dst; if(OP_UNLIKELY(_nsamples*_nchannels>_dst_sz))_nsamples=_dst_sz/_nchannels; _dst_sz=_nsamples*_nchannels; @@ -2741,33 +2914,51 @@ static int op_short2float_filter(OggOpusFile *_of,void *_dst,int _dst_sz, } int op_read_float(OggOpusFile *_of,float *_pcm,int _buf_size,int *_li){ - return op_read_native_filter(_of,_pcm,_buf_size,op_short2float_filter,_li); + return op_filter_read_native(_of,_pcm,_buf_size,op_short2float_filter,_li); } static int op_short2float_stereo_filter(OggOpusFile *_of, void *_dst,int _dst_sz,op_sample *_src,int _nsamples,int _nchannels){ float *dst; + int i; dst=(float *)_dst; _nsamples=OP_MIN(_nsamples,_dst_sz>>1); if(_nchannels==1){ - int i; _nsamples=op_short2float_filter(_of,dst,_nsamples,_src,_nsamples,1); for(i=_nsamples;i-->0;)dst[2*i+0]=dst[2*i+1]=dst[i]; - return _nsamples; } - /*It would be better to convert to floats and then downmix (so that we don't - risk clipping with more than 5 channels), but that would require a large - stack buffer, which is probably not a good idea if you're using the - fixed-point build.*/ - if(_nchannels>2){ - _nsamples=op_stereo_filter(_of,_src,_nsamples*2, - _src,_nsamples,_nchannels); + else if(_nchannels<5){ + /*For 3 or 4 channels, we can downmix in fixed point without risk of + clipping.*/ + if(_nchannels>2){ + _nsamples=op_stereo_filter(_of,_src,_nsamples*2, + _src,_nsamples,_nchannels); + } + return op_short2float_filter(_of,dst,_dst_sz,_src,_nsamples,2); } - return op_short2float_filter(_of,dst,_dst_sz,_src,_nsamples,2); + else{ + /*For 5 or more channels, we convert to floats and then downmix (so that we + don't risk clipping).*/ + for(i=0;i<_nsamples;i++){ + float l; + float r; + int ci; + l=r=0; + for(ci=0;ci<_nchannels;ci++){ + float s; + s=(1.0F/32768)*_src[_nchannels*i+ci]; + l+=OP_STEREO_DOWNMIX[_nchannels-3][ci][0]*s; + r+=OP_STEREO_DOWNMIX[_nchannels-3][ci][1]*s; + } + dst[2*i+0]=l; + dst[2*i+1]=r; + } + } + return _nsamples; } int op_read_float_stereo(OggOpusFile *_of,float *_pcm,int _buf_size){ - return op_read_native_filter(_of,_pcm,_buf_size, + return op_filter_read_native(_of,_pcm,_buf_size, op_short2float_stereo_filter,NULL); } @@ -2801,7 +2992,7 @@ static opus_uint32 op_rand(opus_uint32 _seed){ suppression. This process can increase the peak level of the signal (in theory by the peak error of 1.5 +20 dB, though that is unobservably rare). - To avoid clipping, the signal is attenuated by a couple thousands of a dB. + To avoid clipping, the signal is attenuated by a couple thousandths of a dB. Initially, the approach taken here was to only attenuate by the 99.9th percentile, making clipping rare but not impossible (like SoX), but the limited gain of the filter means that the worst case was only two @@ -2809,9 +3000,9 @@ static opus_uint32 op_rand(opus_uint32 _seed){ The attenuation is probably also helpful to prevent clipping in the DAC reconstruction filters or downstream resampling, in any case.*/ -#define OP_GAIN (32753.0F) +# define OP_GAIN (32753.0F) -#define OP_PRNG_GAIN (1.0F/0xFFFFFFFF) +# define OP_PRNG_GAIN (1.0F/0xFFFFFFFF) /*48 kHz noise shaping filter, sd=2.34.*/ @@ -2823,118 +3014,95 @@ static const float OP_FCOEF_A[4]={ 0.9030F,0.0116F,-0.5853F,-0.2571F }; -static void op_shaped_dither16(OggOpusFile *_of,opus_int16 *_dst, - const float *_src,int _nsamples,int _nchannels){ - opus_uint32 seed; - int mute; - int i; - mute=_of->dither_mute; - seed=_of->dither_seed; - /*In order to avoid replacing digital silence with quiet dither noise, we - mute if the output has been silent for a while.*/ - if(mute>64)memset(_of->dither_a,0,sizeof(*_of->dither_a)*4*_nchannels); - for(i=0;i<_nsamples;i++){ - int silent; - int ci; - silent=1; - for(ci=0;ci<_nchannels;ci++){ - float r; - float s; - float err; - int si; - int j; - s=_src[_nchannels*i+ci]; - silent&=s==0; - s*=OP_GAIN; - err=0; - for(j=0;j<4;j++){ - err+=OP_FCOEF_B[j]*_of->dither_b[ci*4+j] - -OP_FCOEF_A[j]*_of->dither_a[ci*4+j]; - } - for(j=3;j-->0;)_of->dither_a[ci*4+j+1]=_of->dither_a[ci*4+j]; - for(j=3;j-->0;)_of->dither_b[ci*4+j+1]=_of->dither_b[ci*4+j]; - _of->dither_a[ci*4]=err; - s-=err; - if(mute>16)r=0; - else{ - seed=op_rand(seed); - r=seed*OP_PRNG_GAIN; - seed=op_rand(seed); - r-=seed*OP_PRNG_GAIN; - } - /*Clamp in float out of paranoia that the input will be > 96 dBFS and - wrap if the integer is clamped.*/ - si=op_float2int(OP_CLAMP(-32768,s+r,32767)); - _dst[_nchannels*i+ci]=(opus_int16)si; - /*Including clipping in the noise shaping is generally disastrous: the - futile effort to restore the clipped energy results in more clipping. - However, small amounts---at the level which could normally be created - by dither and rounding---are harmless and can even reduce clipping - somewhat due to the clipping sometimes reducing the dither + rounding - error.*/ - _of->dither_b[ci*4]=mute>16?0:OP_CLAMP(-1.5F,si-s,1.5F); - } - mute++; - if(!silent)mute=0; - } - _of->dither_mute=OP_MIN(mute,65); - _of->dither_seed=seed; -} - static int op_float2short_filter(OggOpusFile *_of,void *_dst,int _dst_sz, - op_sample *_src,int _nsamples,int _nchannels){ + float *_src,int _nsamples,int _nchannels){ opus_int16 *dst; + int ci; + int i; dst=(opus_int16 *)_dst; if(OP_UNLIKELY(_nsamples*_nchannels>_dst_sz))_nsamples=_dst_sz/_nchannels; - op_shaped_dither16(_of,dst,_src,_nsamples,_nchannels); +# if defined(OP_SOFT_CLIP) + if(_of->state_channel_count!=_nchannels){ + for(ci=0;ci<_nchannels;ci++)_of->clip_state[ci]=0; + } + opus_pcm_soft_clip(_src,_nsamples,_nchannels,_of->clip_state); +# endif + if(_of->dither_disabled){ + for(i=0;i<_nchannels*_nsamples;i++){ + dst[i]=op_float2int(OP_CLAMP(-32768,32768.0F*_src[i],32767)); + } + } + else{ + opus_uint32 seed; + int mute; + seed=_of->dither_seed; + mute=_of->dither_mute; + if(_of->state_channel_count!=_nchannels)mute=65; + /*In order to avoid replacing digital silence with quiet dither noise, we + mute if the output has been silent for a while.*/ + if(mute>64)memset(_of->dither_a,0,sizeof(*_of->dither_a)*4*_nchannels); + for(i=0;i<_nsamples;i++){ + int silent; + silent=1; + for(ci=0;ci<_nchannels;ci++){ + float r; + float s; + float err; + int si; + int j; + s=_src[_nchannels*i+ci]; + silent&=s==0; + s*=OP_GAIN; + err=0; + for(j=0;j<4;j++){ + err+=OP_FCOEF_B[j]*_of->dither_b[ci*4+j] + -OP_FCOEF_A[j]*_of->dither_a[ci*4+j]; + } + for(j=3;j-->0;)_of->dither_a[ci*4+j+1]=_of->dither_a[ci*4+j]; + for(j=3;j-->0;)_of->dither_b[ci*4+j+1]=_of->dither_b[ci*4+j]; + _of->dither_a[ci*4]=err; + s-=err; + if(mute>16)r=0; + else{ + seed=op_rand(seed); + r=seed*OP_PRNG_GAIN; + seed=op_rand(seed); + r-=seed*OP_PRNG_GAIN; + } + /*Clamp in float out of paranoia that the input will be > 96 dBFS and + wrap if the integer is clamped.*/ + si=op_float2int(OP_CLAMP(-32768,s+r,32767)); + dst[_nchannels*i+ci]=(opus_int16)si; + /*Including clipping in the noise shaping is generally disastrous: the + futile effort to restore the clipped energy results in more clipping. + However, small amounts---at the level which could normally be created + by dither and rounding---are harmless and can even reduce clipping + somewhat due to the clipping sometimes reducing the dither + rounding + error.*/ + _of->dither_b[ci*4]=mute>16?0:OP_CLAMP(-1.5F,si-s,1.5F); + } + mute++; + if(!silent)mute=0; + } + _of->dither_mute=OP_MIN(mute,65); + _of->dither_seed=seed; + } + _of->state_channel_count=_nchannels; return _nsamples; } int op_read(OggOpusFile *_of,opus_int16 *_pcm,int _buf_size,int *_li){ - return op_read_native_filter(_of,_pcm,_buf_size,op_float2short_filter,_li); + return op_filter_read_native(_of,_pcm,_buf_size,op_float2short_filter,_li); } int op_read_float(OggOpusFile *_of,float *_pcm,int _buf_size,int *_li){ + _of->state_channel_count=0; return op_read_native(_of,_pcm,_buf_size,_li); } -/*Matrices for downmixing from the supported channel counts to stereo. - The matrices with 5 or more channels are normalized to a total volume of 2.0, - since most mixes sound too quiet if normalized to 1.0 (as there is generally - little volume in the side/rear channels).*/ -static const float OP_STEREO_DOWNMIX[OP_NCHANNELS_MAX-2][OP_NCHANNELS_MAX][2]={ - /*3.0*/ - { - {0.5858F,0.0F},{0.4142F,0.4142F},{0.0F,0.5858F} - }, - /*quadrophonic*/ - { - {0.4226F,0.0F},{0.0F,0.4226F},{0.366F,0.2114F},{0.2114F,0.336F} - }, - /*5.0*/ - { - {0.651F,0.0F},{0.46F,0.46F},{0.0F,0.651F},{0.5636F,0.3254F}, - {0.3254F,0.5636F} - }, - /*5.1*/ - { - {0.529F,0.0F},{0.3741F,0.3741F},{0.0F,0.529F},{0.4582F,0.2645F}, - {0.2645F,0.4582F},{0.3741F,0.3741F} - }, - /*6.1*/ - { - {0.4553F,0.0F},{0.322F,0.322F},{0.0F,0.4553F},{0.3943F,0.2277F}, - {0.2277F,0.3943F},{0.2788F,0.2788F},{0.322F,0.322F} - }, - /*7.1*/ - { - {0.3886F,0.0F},{0.2748F,0.2748F},{0.0F,0.3886F},{0.3366F,0.1943F}, - {0.1943F,0.3366F},{0.3366F,0.1943F},{0.1943F,0.3366F},{0.2748F,0.2748F} - } -}; - static int op_stereo_filter(OggOpusFile *_of,void *_dst,int _dst_sz, op_sample *_src,int _nsamples,int _nchannels){ + (void)_of; _nsamples=OP_MIN(_nsamples,_dst_sz>>1); if(_nchannels==2)memcpy(_dst,_src,_nsamples*2*sizeof(*_src)); else{ @@ -2966,29 +3134,30 @@ static int op_float2short_stereo_filter(OggOpusFile *_of, void *_dst,int _dst_sz,op_sample *_src,int _nsamples,int _nchannels){ opus_int16 *dst; dst=(opus_int16 *)_dst; - _nsamples=OP_MIN(_nsamples,_dst_sz>>1); if(_nchannels==1){ int i; - op_shaped_dither16(_of,dst,_src,_nsamples,1); + _nsamples=op_float2short_filter(_of,dst,_dst_sz>>1,_src,_nsamples,1); for(i=_nsamples;i-->0;)dst[2*i+0]=dst[2*i+1]=dst[i]; } else{ if(_nchannels>2){ + _nsamples=OP_MIN(_nsamples,_dst_sz>>1); _nsamples=op_stereo_filter(_of,_src,_nsamples*2, _src,_nsamples,_nchannels); } - op_shaped_dither16(_of,dst,_src,_nsamples,_nchannels); + _nsamples=op_float2short_filter(_of,dst,_dst_sz,_src,_nsamples,2); } return _nsamples; } int op_read_stereo(OggOpusFile *_of,opus_int16 *_pcm,int _buf_size){ - return op_read_native_filter(_of,_pcm,_buf_size, + return op_filter_read_native(_of,_pcm,_buf_size, op_float2short_stereo_filter,NULL); } int op_read_float_stereo(OggOpusFile *_of,float *_pcm,int _buf_size){ - return op_read_native_filter(_of,_pcm,_buf_size,op_stereo_filter,NULL); + _of->state_channel_count=0; + return op_filter_read_native(_of,_pcm,_buf_size,op_stereo_filter,NULL); } #endif diff --git a/code/opusfile-0.5/src/stream.c b/code/opusfile-0.5/src/stream.c new file mode 100644 index 00000000..0238a6b3 --- /dev/null +++ b/code/opusfile-0.5/src/stream.c @@ -0,0 +1,366 @@ +/******************************************************************** + * * + * THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. * + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * + * * + * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 1994-2012 * + * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * + * * + ******************************************************************** + + function: stdio-based convenience library for opening/seeking/decoding + last mod: $Id: vorbisfile.c 17573 2010-10-27 14:53:59Z xiphmont $ + + ********************************************************************/ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "internal.h" +#include +#include +#include +#include +#include +#if defined(_WIN32) +# include +#endif + +typedef struct OpusMemStream OpusMemStream; + +#define OP_MEM_SIZE_MAX (~(size_t)0>>1) +#define OP_MEM_DIFF_MAX ((ptrdiff_t)OP_MEM_SIZE_MAX) + +/*The context information needed to read from a block of memory as if it were a + file.*/ +struct OpusMemStream{ + /*The block of memory to read from.*/ + const unsigned char *data; + /*The total size of the block. + This must be at most OP_MEM_SIZE_MAX to prevent signed overflow while + seeking.*/ + ptrdiff_t size; + /*The current file position. + This is allowed to be set arbitrarily greater than size (i.e., past the end + of the block, though we will not read data past the end of the block), but + is not allowed to be negative (i.e., before the beginning of the block).*/ + ptrdiff_t pos; +}; + +static int op_fread(void *_stream,unsigned char *_ptr,int _buf_size){ + FILE *stream; + size_t ret; + /*Check for empty read.*/ + if(_buf_size<=0)return 0; + stream=(FILE *)_stream; + ret=fread(_ptr,1,_buf_size,stream); + OP_ASSERT(ret<=(size_t)_buf_size); + /*If ret==0 and !feof(stream), there was a read error.*/ + return ret>0||feof(stream)?(int)ret:OP_EREAD; +} + +static int op_fseek(void *_stream,opus_int64 _offset,int _whence){ +#if defined(_WIN32) + /*_fseeki64() is not exposed until MSCVCRT80. + This is the default starting with MSVC 2005 (_MSC_VER>=1400), but we want + to allow linking against older MSVCRT versions for compatibility back to + XP without installing extra runtime libraries. + i686-pc-mingw32 does not have fseeko() and requires + __MSVCRT_VERSION__>=0x800 for _fseeki64(), which screws up linking with + other libraries (that don't use MSVCRT80 from MSVC 2005 by default). + i686-w64-mingw32 does have fseeko() and respects _FILE_OFFSET_BITS, but I + don't know how to detect that at compile time. + We could just use fseeko64() (which is available in both), but its + implemented using fgetpos()/fsetpos() just like this code, except without + the overflow checking, so we prefer our version.*/ + opus_int64 pos; + /*We don't use fpos_t directly because it might be a struct if __STDC__ is + non-zero or _INTEGRAL_MAX_BITS < 64. + I'm not certain when the latter is true, but someone could in theory set + the former. + Either way, it should be binary compatible with a normal 64-bit int (this + assumption is not portable, but I believe it is true for MSVCRT).*/ + OP_ASSERT(sizeof(pos)==sizeof(fpos_t)); + /*Translate the seek to an absolute one.*/ + if(_whence==SEEK_CUR){ + int ret; + ret=fgetpos((FILE *)_stream,(fpos_t *)&pos); + if(ret)return ret; + } + else if(_whence==SEEK_END)pos=_filelengthi64(_fileno((FILE *)_stream)); + else if(_whence==SEEK_SET)pos=0; + else return -1; + /*Check for errors or overflow.*/ + if(pos<0||_offset<-pos||_offset>OP_INT64_MAX-pos)return -1; + pos+=_offset; + return fsetpos((FILE *)_stream,(fpos_t *)&pos); +#else + /*This function actually conforms to the SUSv2 and POSIX.1-2001, so we prefer + it except on Windows.*/ + return fseeko((FILE *)_stream,(off_t)_offset,_whence); +#endif +} + +static opus_int64 op_ftell(void *_stream){ +#if defined(_WIN32) + /*_ftelli64() is not exposed until MSCVCRT80, and ftello()/ftello64() have + the same problems as fseeko()/fseeko64() in MingW. + See above for a more detailed explanation.*/ + opus_int64 pos; + OP_ASSERT(sizeof(pos)==sizeof(fpos_t)); + return fgetpos((FILE *)_stream,(fpos_t *)&pos)?-1:pos; +#else + /*This function actually conforms to the SUSv2 and POSIX.1-2001, so we prefer + it except on Windows.*/ + return ftello((FILE *)_stream); +#endif +} + +static const OpusFileCallbacks OP_FILE_CALLBACKS={ + op_fread, + op_fseek, + op_ftell, + (op_close_func)fclose +}; + +#if defined(_WIN32) +# include +# include + +/*Windows doesn't accept UTF-8 by default, and we don't have a wchar_t API, + so if we just pass the path to fopen(), then there'd be no way for a user + of our API to open a Unicode filename. + Instead, we translate from UTF-8 to UTF-16 and use Windows' wchar_t API. + This makes this API more consistent with platforms where the character set + used by fopen is the same as used on disk, which is generally UTF-8, and + with our metadata API, which always uses UTF-8.*/ +static wchar_t *op_utf8_to_utf16(const char *_src){ + wchar_t *dst; + size_t len; + len=strlen(_src); + /*Worst-case output is 1 wide character per 1 input character.*/ + dst=(wchar_t *)_ogg_malloc(sizeof(*dst)*(len+1)); + if(dst!=NULL){ + size_t si; + size_t di; + for(di=si=0;si=0x80U){ + /*This is a 2-byte sequence that is not overlong.*/ + dst[di++]=w; + si++; + continue; + } + } + else{ + int c2; + /*This is safe, because c1 was not 0 and _src is NUL-terminated.*/ + c2=(unsigned char)_src[si+2]; + if((c2&0xC0)==0x80){ + /*Found at least two continuation bytes.*/ + if((c0&0xF0)==0xE0){ + wchar_t w; + /*Start byte says this is a 3-byte sequence.*/ + w=(c0&0xF)<<12|(c1&0x3F)<<6|c2&0x3F; + if(w>=0x800U&&(w<0xD800||w>=0xE000)&&w<0xFFFE){ + /*This is a 3-byte sequence that is not overlong, not a + UTF-16 surrogate pair value, and not a 'not a character' + value.*/ + dst[di++]=w; + si+=2; + continue; + } + } + else{ + int c3; + /*This is safe, because c2 was not 0 and _src is + NUL-terminated.*/ + c3=(unsigned char)_src[si+3]; + if((c3&0xC0)==0x80){ + /*Found at least three continuation bytes.*/ + if((c0&0xF8)==0xF0){ + opus_uint32 w; + /*Start byte says this is a 4-byte sequence.*/ + w=(c0&7)<<18|(c1&0x3F)<<12|(c2&0x3F)<<6&(c3&0x3F); + if(w>=0x10000U&&w<0x110000U){ + /*This is a 4-byte sequence that is not overlong and not + greater than the largest valid Unicode code point. + Convert it to a surrogate pair.*/ + w-=0x10000; + dst[di++]=(wchar_t)(0xD800+(w>>10)); + dst[di++]=(wchar_t)(0xDC00+(w&0x3FF)); + si+=3; + continue; + } + } + } + } + } + } + } + } + /*If we got here, we encountered an illegal UTF-8 sequence.*/ + _ogg_free(dst); + return NULL; + } + OP_ASSERT(di<=len); + dst[di]='\0'; + } + return dst; +} + +#endif + +void *op_fopen(OpusFileCallbacks *_cb,const char *_path,const char *_mode){ + FILE *fp; +#if !defined(_WIN32) + fp=fopen(_path,_mode); +#else + fp=NULL; + if(_path==NULL||_mode==NULL)errno=EINVAL; + else{ + wchar_t *wpath; + wchar_t *wmode; + wpath=op_utf8_to_utf16(_path); + wmode=op_utf8_to_utf16(_mode); + if(wmode==NULL)errno=EINVAL; + else if(wpath==NULL)errno=ENOENT; + else fp=_wfopen(wpath,wmode); + _ogg_free(wmode); + _ogg_free(wpath); + } +#endif + if(fp!=NULL)*_cb=*&OP_FILE_CALLBACKS; + return fp; +} + +void *op_fdopen(OpusFileCallbacks *_cb,int _fd,const char *_mode){ + FILE *fp; + fp=fdopen(_fd,_mode); + if(fp!=NULL)*_cb=*&OP_FILE_CALLBACKS; + return fp; +} + +void *op_freopen(OpusFileCallbacks *_cb,const char *_path,const char *_mode, + void *_stream){ + FILE *fp; +#if !defined(_WIN32) + fp=freopen(_path,_mode,(FILE *)_stream); +#else + fp=NULL; + if(_path==NULL||_mode==NULL)errno=EINVAL; + else{ + wchar_t *wpath; + wchar_t *wmode; + wpath=op_utf8_to_utf16(_path); + wmode=op_utf8_to_utf16(_mode); + if(wmode==NULL)errno=EINVAL; + else if(wpath==NULL)errno=ENOENT; + else fp=_wfreopen(wpath,wmode,(FILE *)_stream); + _ogg_free(wmode); + _ogg_free(wpath); + } +#endif + if(fp!=NULL)*_cb=*&OP_FILE_CALLBACKS; + return fp; +} + +static int op_mem_read(void *_stream,unsigned char *_ptr,int _buf_size){ + OpusMemStream *stream; + ptrdiff_t size; + ptrdiff_t pos; + stream=(OpusMemStream *)_stream; + /*Check for empty read.*/ + if(_buf_size<=0)return 0; + size=stream->size; + pos=stream->pos; + /*Check for EOF.*/ + if(pos>=size)return 0; + /*Check for a short read.*/ + _buf_size=(int)OP_MIN(size-pos,_buf_size); + memcpy(_ptr,stream->data+pos,_buf_size); + pos+=_buf_size; + stream->pos=pos; + return _buf_size; +} + +static int op_mem_seek(void *_stream,opus_int64 _offset,int _whence){ + OpusMemStream *stream; + ptrdiff_t pos; + stream=(OpusMemStream *)_stream; + pos=stream->pos; + OP_ASSERT(pos>=0); + switch(_whence){ + case SEEK_SET:{ + /*Check for overflow:*/ + if(_offset<0||_offset>OP_MEM_DIFF_MAX)return -1; + pos=(ptrdiff_t)_offset; + }break; + case SEEK_CUR:{ + /*Check for overflow:*/ + if(_offset<-pos||_offset>OP_MEM_DIFF_MAX-pos)return -1; + pos=(ptrdiff_t)(pos+_offset); + }break; + case SEEK_END:{ + ptrdiff_t size; + size=stream->size; + OP_ASSERT(size>=0); + /*Check for overflow:*/ + if(_offset>size||_offsetpos=pos; + return 0; +} + +static opus_int64 op_mem_tell(void *_stream){ + OpusMemStream *stream; + stream=(OpusMemStream *)_stream; + return (ogg_int64_t)stream->pos; +} + +static int op_mem_close(void *_stream){ + _ogg_free(_stream); + return 0; +} + +static const OpusFileCallbacks OP_MEM_CALLBACKS={ + op_mem_read, + op_mem_seek, + op_mem_tell, + op_mem_close +}; + +void *op_mem_stream_create(OpusFileCallbacks *_cb, + const unsigned char *_data,size_t _size){ + OpusMemStream *stream; + if(_size>OP_MEM_SIZE_MAX)return NULL; + stream=(OpusMemStream *)_ogg_malloc(sizeof(*stream)); + if(stream!=NULL){ + *_cb=*&OP_MEM_CALLBACKS; + stream->data=_data; + stream->size=_size; + stream->pos=0; + } + return stream; +} diff --git a/code/opusfile-0.5/src/wincerts.c b/code/opusfile-0.5/src/wincerts.c new file mode 100644 index 00000000..b0e35aa3 --- /dev/null +++ b/code/opusfile-0.5/src/wincerts.c @@ -0,0 +1,171 @@ +/******************************************************************** + * * + * THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. * + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * + * * + * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 2013 * + * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * + * * + ********************************************************************/ + +/*This should really be part of OpenSSL, but there's been a patch [1] sitting + in their bugtracker for over two years that implements this, without any + action, so I'm giving up and re-implementing it locally. + + [1] */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "internal.h" +#if defined(OP_ENABLE_HTTP)&&defined(_WIN32) +/*You must include windows.h before wincrypt.h and x509.h.*/ +# define WIN32_LEAN_AND_MEAN +# define WIN32_EXTRA_LEAN +# include +/*You must include wincrypt.h before x509.h, too, or X509_NAME doesn't get + defined properly.*/ +# include +# include +# include +# include + +static int op_capi_new(X509_LOOKUP *_lu){ + HCERTSTORE h_store; + h_store=CertOpenStore(CERT_STORE_PROV_SYSTEM_A,0,0, + CERT_STORE_OPEN_EXISTING_FLAG|CERT_STORE_READONLY_FLAG| + CERT_SYSTEM_STORE_CURRENT_USER|CERT_STORE_SHARE_CONTEXT_FLAG,"ROOT"); + if(h_store!=NULL){ + _lu->method_data=(char *)h_store; + return 1; + } + return 0; +} + +static void op_capi_free(X509_LOOKUP *_lu){ + HCERTSTORE h_store; + h_store=(HCERTSTORE)_lu->method_data; +# if defined(OP_ENABLE_ASSERTIONS) + OP_ALWAYS_TRUE(CertCloseStore(h_store,CERT_CLOSE_STORE_CHECK_FLAG)); +# else + CertCloseStore(h_store,0); +# endif +} + +static int op_capi_retrieve_by_subject(X509_LOOKUP *_lu,int _type, + X509_NAME *_name,X509_OBJECT *_ret){ + X509_OBJECT *obj; + CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE); + obj=X509_OBJECT_retrieve_by_subject(_lu->store_ctx->objs,_type,_name); + CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE); + if(obj!=NULL){ + _ret->type=obj->type; + memcpy(&_ret->data,&obj->data,sizeof(_ret->data)); + return 1; + } + return 0; +} + +static int op_capi_get_by_subject(X509_LOOKUP *_lu,int _type,X509_NAME *_name, + X509_OBJECT *_ret){ + HCERTSTORE h_store; + if(_name==NULL)return 0; + if(_name->bytes==NULL||_name->bytes->length<=0||_name->modified){ + if(i2d_X509_NAME(_name,NULL)<0)return 0; + OP_ASSERT(_name->bytes->length>0); + } + h_store=(HCERTSTORE)_lu->method_data; + switch(_type){ + case X509_LU_X509:{ + CERT_NAME_BLOB find_para; + PCCERT_CONTEXT cert; + X509 *x; + int ret; + /*Although X509_NAME contains a canon_enc field, that "canonical" [1] + encoding was just made up by OpenSSL. + It doesn't correspond to any actual standard, and since it drops the + initial sequence header, won't be recognized by the Crypto API. + The assumption here is that CertFindCertificateInStore() will allow any + appropriate variations in the encoding when it does its comparison. + This is, however, emphatically not true under Wine, which just compares + the encodings with memcmp(). + Most of the time things work anyway, though, and there isn't really + anything we can do to make the situation better. + + [1] A "canonical form" is defined as the one where, if you locked 10 + mathematicians in a room and asked them to come up with a + representation for something, it's the answer that 9 of them would + give you back. + I don't think OpenSSL's encoding qualifies.*/ + find_para.cbData=_name->bytes->length; + find_para.pbData=(unsigned char *)_name->bytes->data; + cert=CertFindCertificateInStore(h_store,X509_ASN_ENCODING,0, + CERT_FIND_SUBJECT_NAME,&find_para,NULL); + if(cert==NULL)return 0; + x=d2i_X509(NULL,(const unsigned char **)&cert->pbCertEncoded, + cert->cbCertEncoded); + CertFreeCertificateContext(cert); + if(x==NULL)return 0; + ret=X509_STORE_add_cert(_lu->store_ctx,x); + X509_free(x); + if(ret)return op_capi_retrieve_by_subject(_lu,_type,_name,_ret); + }break; + case X509_LU_CRL:{ + CERT_INFO cert_info; + CERT_CONTEXT find_para; + PCCRL_CONTEXT crl; + X509_CRL *x; + int ret; + ret=op_capi_retrieve_by_subject(_lu,_type,_name,_ret); + if(ret>0)return ret; + memset(&cert_info,0,sizeof(cert_info)); + cert_info.Issuer.cbData=_name->bytes->length; + cert_info.Issuer.pbData=(unsigned char *)_name->bytes->data; + memset(&find_para,0,sizeof(find_para)); + find_para.pCertInfo=&cert_info; + crl=CertFindCRLInStore(h_store,0,0,CRL_FIND_ISSUED_BY,&find_para,NULL); + if(crl==NULL)return 0; + x=d2i_X509_CRL(NULL,(const unsigned char **)&crl->pbCrlEncoded, + crl->cbCrlEncoded); + CertFreeCRLContext(crl); + if(x==NULL)return 0; + ret=X509_STORE_add_crl(_lu->store_ctx,x); + X509_CRL_free(x); + if(ret)return op_capi_retrieve_by_subject(_lu,_type,_name,_ret); + }break; + } + return 0; +} + +/*This is not const because OpenSSL doesn't allow it, even though it won't + write to it.*/ +static X509_LOOKUP_METHOD X509_LOOKUP_CAPI={ + "Load Crypto API store into cache", + op_capi_new, + op_capi_free, + NULL, + NULL, + NULL, + op_capi_get_by_subject, + NULL, + NULL, + NULL +}; + +int SSL_CTX_set_default_verify_paths_win32(SSL_CTX *_ssl_ctx){ + X509_STORE *store; + X509_LOOKUP *lu; + /*We intentionally do not add the normal default paths, as they are usually + wrong, and are just asking to be used as an exploit vector.*/ + store=SSL_CTX_get_cert_store(_ssl_ctx); + OP_ASSERT(store!=NULL); + lu=X509_STORE_add_lookup(store,&X509_LOOKUP_CAPI); + if(lu==NULL)return 0; + ERR_clear_error(); + return 1; +} + +#endif diff --git a/code/opusfile-0.5/src/winerrno.h b/code/opusfile-0.5/src/winerrno.h new file mode 100644 index 00000000..32a90b4e --- /dev/null +++ b/code/opusfile-0.5/src/winerrno.h @@ -0,0 +1,90 @@ +/******************************************************************** + * * + * THIS FILE IS PART OF THE libopusfile SOFTWARE CODEC SOURCE CODE. * + * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * + * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * + * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * + * * + * THE libopusfile SOURCE CODE IS (C) COPYRIGHT 2012 * + * by the Xiph.Org Foundation and contributors http://www.xiph.org/ * + * * + ********************************************************************/ +#if !defined(_opusfile_winerrno_h) +# define _opusfile_winerrno_h (1) + +# include +# include + +/*These conflict with the MSVC errno.h definitions, but we don't need to use + the original ones in any file that deals with sockets. + We could map the WSA errors to the errno.h ones (most of which are only + available on sufficiently new versions of MSVC), but they aren't ordered the + same, and given how rarely we actually look at the values, I don't think + it's worth a lookup table.*/ +# undef EWOULDBLOCK +# undef EINPROGRESS +# undef EALREADY +# undef ENOTSOCK +# undef EDESTADDRREQ +# undef EMSGSIZE +# undef EPROTOTYPE +# undef ENOPROTOOPT +# undef EPROTONOSUPPORT +# undef EOPNOTSUPP +# undef EAFNOSUPPORT +# undef EADDRINUSE +# undef EADDRNOTAVAIL +# undef ENETDOWN +# undef ENETUNREACH +# undef ENETRESET +# undef ECONNABORTED +# undef ECONNRESET +# undef ENOBUFS +# undef EISCONN +# undef ENOTCONN +# undef ETIMEDOUT +# undef ECONNREFUSED +# undef ELOOP +# undef ENAMETOOLONG +# undef EHOSTUNREACH +# undef ENOTEMPTY + +# define EWOULDBLOCK (WSAEWOULDBLOCK-WSABASEERR) +# define EINPROGRESS (WSAEINPROGRESS-WSABASEERR) +# define EALREADY (WSAEALREADY-WSABASEERR) +# define ENOTSOCK (WSAENOTSOCK-WSABASEERR) +# define EDESTADDRREQ (WSAEDESTADDRREQ-WSABASEERR) +# define EMSGSIZE (WSAEMSGSIZE-WSABASEERR) +# define EPROTOTYPE (WSAEPROTOTYPE-WSABASEERR) +# define ENOPROTOOPT (WSAENOPROTOOPT-WSABASEERR) +# define EPROTONOSUPPORT (WSAEPROTONOSUPPORT-WSABASEERR) +# define ESOCKTNOSUPPORT (WSAESOCKTNOSUPPORT-WSABASEERR) +# define EOPNOTSUPP (WSAEOPNOTSUPP-WSABASEERR) +# define EPFNOSUPPORT (WSAEPFNOSUPPORT-WSABASEERR) +# define EAFNOSUPPORT (WSAEAFNOSUPPORT-WSABASEERR) +# define EADDRINUSE (WSAEADDRINUSE-WSABASEERR) +# define EADDRNOTAVAIL (WSAEADDRNOTAVAIL-WSABASEERR) +# define ENETDOWN (WSAENETDOWN-WSABASEERR) +# define ENETUNREACH (WSAENETUNREACH-WSABASEERR) +# define ENETRESET (WSAENETRESET-WSABASEERR) +# define ECONNABORTED (WSAECONNABORTED-WSABASEERR) +# define ECONNRESET (WSAECONNRESET-WSABASEERR) +# define ENOBUFS (WSAENOBUFS-WSABASEERR) +# define EISCONN (WSAEISCONN-WSABASEERR) +# define ENOTCONN (WSAENOTCONN-WSABASEERR) +# define ESHUTDOWN (WSAESHUTDOWN-WSABASEERR) +# define ETOOMANYREFS (WSAETOOMANYREFS-WSABASEERR) +# define ETIMEDOUT (WSAETIMEDOUT-WSABASEERR) +# define ECONNREFUSED (WSAECONNREFUSED-WSABASEERR) +# define ELOOP (WSAELOOP-WSABASEERR) +# define ENAMETOOLONG (WSAENAMETOOLONG-WSABASEERR) +# define EHOSTDOWN (WSAEHOSTDOWN-WSABASEERR) +# define EHOSTUNREACH (WSAEHOSTUNREACH-WSABASEERR) +# define ENOTEMPTY (WSAENOTEMPTY-WSABASEERR) +# define EPROCLIM (WSAEPROCLIM-WSABASEERR) +# define EUSERS (WSAEUSERS-WSABASEERR) +# define EDQUOT (WSAEDQUOT-WSABASEERR) +# define ESTALE (WSAESTALE-WSABASEERR) +# define EREMOTE (WSAEREMOTE-WSABASEERR) + +#endif