From b72cec1270ca3d71e35fda050513def6d2ef4c76 Mon Sep 17 00:00:00 2001 From: Will Sackfield Date: Tue, 21 Nov 2017 13:40:42 -0500 Subject: [PATCH 1/4] Allow Vorbis Read Size to be set by user * Hooking up Vorbis read callbacks to an HTTP data provider currently yields a lot of overhead due to many read requests at 2kB * Allowing the user to set the vorbis read size lets backend reads become more efficient by reading greater chunks at any given time --- include/vorbis/vorbisfile.h | 4 ++++ lib/vorbisfile.c | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/vorbis/vorbisfile.h b/include/vorbis/vorbisfile.h index de540cf72..cc65ebfbd 100644 --- a/include/vorbis/vorbisfile.h +++ b/include/vorbis/vorbisfile.h @@ -143,6 +143,9 @@ typedef struct OggVorbis_File { ov_callbacks callbacks; + /* The read size of the vorbis file */ + int read_size; + } OggVorbis_File; @@ -185,6 +188,7 @@ extern double ov_time_tell(OggVorbis_File *vf); extern vorbis_info *ov_info(OggVorbis_File *vf,int link); extern vorbis_comment *ov_comment(OggVorbis_File *vf,int link); +extern void ov_set_read_size(OggVorbis_File *vf,int read_size); extern long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int samples, int *bitstream); diff --git a/lib/vorbisfile.c b/lib/vorbisfile.c index 8a0a28015..04dd1e732 100644 --- a/lib/vorbisfile.c +++ b/lib/vorbisfile.c @@ -68,8 +68,8 @@ static long _get_data(OggVorbis_File *vf){ errno=0; if(!(vf->callbacks.read_func))return(-1); if(vf->datasource){ - char *buffer=ogg_sync_buffer(&vf->oy,READSIZE); - long bytes=(vf->callbacks.read_func)(buffer,1,READSIZE,vf->datasource); + char *buffer=ogg_sync_buffer(&vf->oy,vf->read_size); + long bytes=(vf->callbacks.read_func)(buffer,1,vf->read_size,vf->datasource); if(bytes>0)ogg_sync_wrote(&vf->oy,bytes); if(bytes==0 && errno)return(-1); return(bytes); @@ -884,6 +884,7 @@ static int _ov_open1(void *f,OggVorbis_File *vf,const char *initial, memset(vf,0,sizeof(*vf)); vf->datasource=f; vf->callbacks = callbacks; + vf->read_size = READSIZE; /* init the framing state */ ogg_sync_init(&vf->oy); @@ -1911,6 +1912,10 @@ vorbis_comment *ov_comment(OggVorbis_File *vf,int link){ } } +void ov_set_read_size(OggVorbis_File *vf,int read_size) { + vf->read_size = read_size; +} + static int host_is_big_endian() { ogg_int32_t pattern = 0xfeedface; /* deadbeef */ unsigned char *bytewise = (unsigned char *)&pattern; From c760fc1f15aff7e0a735c68c9310d86e6d05f891 Mon Sep 17 00:00:00 2001 From: Will Sackfield Date: Mon, 27 Nov 2017 12:18:32 -0500 Subject: [PATCH 2/4] Add Sanity Checks for setting the read size * Does not allow read sizes < 1 * Does not allow read sizes above chunk sizes --- lib/vorbisfile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vorbisfile.c b/lib/vorbisfile.c index 04dd1e732..b5a35d786 100644 --- a/lib/vorbisfile.c +++ b/lib/vorbisfile.c @@ -1913,7 +1913,7 @@ vorbis_comment *ov_comment(OggVorbis_File *vf,int link){ } void ov_set_read_size(OggVorbis_File *vf,int read_size) { - vf->read_size = read_size; + vf->read_size = MAX(1, MIN(read_size, CHUNKSIZE)); } static int host_is_big_endian() { From 3ef0b16750c0eac0aa687815e8c3c6d3e5682149 Mon Sep 17 00:00:00 2001 From: Will Sackfield Date: Mon, 27 Nov 2017 12:19:12 -0500 Subject: [PATCH 3/4] Add getter for read sizes --- include/vorbis/vorbisfile.h | 1 + lib/vorbisfile.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/vorbis/vorbisfile.h b/include/vorbis/vorbisfile.h index cc65ebfbd..1941c9b1e 100644 --- a/include/vorbis/vorbisfile.h +++ b/include/vorbis/vorbisfile.h @@ -189,6 +189,7 @@ extern double ov_time_tell(OggVorbis_File *vf); extern vorbis_info *ov_info(OggVorbis_File *vf,int link); extern vorbis_comment *ov_comment(OggVorbis_File *vf,int link); extern void ov_set_read_size(OggVorbis_File *vf,int read_size); +extern int ov_get_read_size(OggVorbis_File *vf); extern long ov_read_float(OggVorbis_File *vf,float ***pcm_channels,int samples, int *bitstream); diff --git a/lib/vorbisfile.c b/lib/vorbisfile.c index b5a35d786..3fa29dd28 100644 --- a/lib/vorbisfile.c +++ b/lib/vorbisfile.c @@ -1916,6 +1916,10 @@ void ov_set_read_size(OggVorbis_File *vf,int read_size) { vf->read_size = MAX(1, MIN(read_size, CHUNKSIZE)); } +int ov_get_read_size(OggVorbis_File *vf) { + return vf->read_size; +} + static int host_is_big_endian() { ogg_int32_t pattern = 0xfeedface; /* deadbeef */ unsigned char *bytewise = (unsigned char *)&pattern; From 28b8183a79777c3068953d31b1acd398472d955c Mon Sep 17 00:00:00 2001 From: Will Sackfield Date: Tue, 5 Dec 2017 07:05:15 -0500 Subject: [PATCH 4/4] Do not use MIN/MAX for portability reasons * Does not work on windows so well --- lib/vorbisfile.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/vorbisfile.c b/lib/vorbisfile.c index 3fa29dd28..a97fed8ba 100644 --- a/lib/vorbisfile.c +++ b/lib/vorbisfile.c @@ -1913,7 +1913,9 @@ vorbis_comment *ov_comment(OggVorbis_File *vf,int link){ } void ov_set_read_size(OggVorbis_File *vf,int read_size) { - vf->read_size = MAX(1, MIN(read_size, CHUNKSIZE)); + read_size = read_size > CHUNKSIZE ? CHUNKSIZE : read_size; + read_size = read_size < 1 ? 1 : read_size; + vf->read_size = read_size; } int ov_get_read_size(OggVorbis_File *vf) {