From d2de2be10fdb1cf459e194b40302af8f6a8a0c7b Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Fri, 3 Mar 2017 05:48:37 -0800 Subject: [PATCH] vorbis: propagate errors better on seek failure (e.g. when coarse seek fails) --- stb_vorbis.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/stb_vorbis.c b/stb_vorbis.c index c4f24d5..dd364c8 100644 --- a/stb_vorbis.c +++ b/stb_vorbis.c @@ -275,7 +275,7 @@ extern int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number); // do not need to seek to EXACTLY the target sample when using get_samples_*, // you can also use seek_frame(). -extern void stb_vorbis_seek_start(stb_vorbis *f); +extern int stb_vorbis_seek_start(stb_vorbis *f); // this function is equivalent to stb_vorbis_seek(f,0) extern unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f); @@ -3484,11 +3484,13 @@ static int vorbis_finish_frame(stb_vorbis *f, int len, int left, int right) return right - left; } -static void vorbis_pump_first_frame(stb_vorbis *f) +static int vorbis_pump_first_frame(stb_vorbis *f) { - int len, right, left; - if (vorbis_decode_packet(f, &len, &left, &right)) + int len, right, left, res; + res = vorbis_decode_packet(f, &len, &left, &right); + if (res) vorbis_finish_frame(f, len, left, right); + return res; } #ifndef STB_VORBIS_NO_PUSHDATA_API @@ -4615,8 +4617,9 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) // starting from the start is handled differently if (sample_number <= left.last_decoded_sample) { - stb_vorbis_seek_start(f); - return 1; + if (stb_vorbis_seek_start(f)) + return 1; + return 0; } while (left.page_end != right.page_start) { @@ -4717,7 +4720,10 @@ static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number) skip(f, f->segments[i]); // start decoding (optimizable - this frame is generally discarded) - vorbis_pump_first_frame(f); + if (!vorbis_pump_first_frame(f)) + return 0; + if (f->current_loc > sample_number) + return error(f, VORBIS_seek_failed); return 1; error: @@ -4808,14 +4814,14 @@ int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number) return 1; } -void stb_vorbis_seek_start(stb_vorbis *f) +int stb_vorbis_seek_start(stb_vorbis *f) { - if (IS_PUSH_MODE(f)) { error(f, VORBIS_invalid_api_mixing); return; } + if (IS_PUSH_MODE(f)) { return error(f, VORBIS_invalid_api_mixing); } set_file_offset(f, f->first_audio_page_offset); f->previous_length = 0; f->first_decode = TRUE; f->next_seg = -1; - vorbis_pump_first_frame(f); + return vorbis_pump_first_frame(f); } unsigned int stb_vorbis_stream_length_in_samples(stb_vorbis *f)