Skip to content
Myr

Merge branch 'vorbis_seek_fixes' of https://github.com/dougallj/stb into work2

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sean Barrett · committed by Sean Barrett · parents fd4f1f7, da79a21 · Browse files · 1 file+45 added−31 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+45 added−31 removed

  • other:+45 added−31 removed

1 file+45 added−31 removed

modified:stb_vorbis.c+45 added-31 removed
772 unmodified lines
773 uint32 stream_len;774775 uint8 push_mode;776777 // the page to seek to when seeking to start, may be zero778 uint32 first_audio_page_offset;779780 // p_first is the page on which the first audio packet ends781 // (but not necessarily the page on which it starts)782 ProbedPage p_first, p_last;783784 // memory management785 stb_vorbis_alloc alloc;
620 unmodified lines
14061407static int start_page_no_capturepattern(vorb *f)1408{1409 uint32 loc0,loc1,n;1410 if (f->first_decode && !IS_PUSH_MODE(f)) {1411 f->p_first.page_start = stb_vorbis_get_file_offset(f) - 4;1412 }1413 // stream structure version1414 if (0 != get8(f)) return error(f, VORBIS_invalid_stream_structure_version);1415 // header flag1416 f->page_flag = get8(f);
28 unmodified lines
1445 }1446 }1447 if (f->first_decode) {1448 int i,len;1443 ProbedPage p;1449 len = 0;1450 for (i=0; i < f->segment_count; ++i)1451 len += f->segments[i];1452 len += 27 + f->segment_count;1448 p.page_start = f->first_audio_page_offset;1449 p.page_end = p.page_start + len;1450 p.last_decoded_sample = loc0;1453 f->p_first.page_end = f->p_first.page_start + len;1454 f->p_first.last_decoded_sample = loc0;1451 f->p_first = p;1455 }1456 f->next_seg = 0;1457 return TRUE;1458}
2047 unmodified lines
3506 return res;3507}35083509#ifndef STB_VORBIS_NO_PUSHDATA_API3507static int is_whole_packet_present(stb_vorbis *f, int end_page)3510static int is_whole_packet_present(stb_vorbis *f)3511{3512 // make sure that we have the packet available before continuing...3513 // this requires a full ogg parse, but we know we can fetch from f->stream3514
11 unmodified lines
3526 if (f->segments[s] < 255) // stop at first short segment3527 break;3528 }3529 // either this continues, or it ends it...3527 if (end_page)3528 if (s < f->segment_count-1) return error(f, VORBIS_invalid_stream);3530 if (s == f->segment_count)3531 s = -1; // set 'crosses page' flag3532 if (p > f->stream_end) return error(f, VORBIS_need_more_data);3533 first = FALSE;
24 unmodified lines
3558 p += q[s];3559 if (q[s] < 255)3560 break;3561 }3561 if (end_page)3562 if (s < n-1) return error(f, VORBIS_invalid_stream);3562 if (s == n)3563 s = -1; // set 'crosses page' flag3564 if (p > f->stream_end) return error(f, VORBIS_need_more_data);3565 first = FALSE;
8 unmodified lines
3574 int len,i,j,k, max_submaps = 0;3575 int longest_floorlist=0;35763577 // first page, first packet3578 f->first_decode = TRUE;35793580 if (!start_page(f)) return FALSE;3581 // validate page flag3582 if (!(f->page_flag & PAGEFLAG_first_page)) return error(f, VORBIS_invalid_first_page);
60 unmodified lines
3643 if (!start_packet(f)) return FALSE;36443645 #ifndef STB_VORBIS_NO_PUSHDATA_API3646 if (IS_PUSH_MODE(f)) {3647 if (!is_whole_packet_present(f, TRUE)) {3647 if (!is_whole_packet_present(f)) {3648 // convert error in ogg header to write type3649 if (f->error == VORBIS_invalid_stream)3650 f->error = VORBIS_invalid_setup;3651 return FALSE;
479 unmodified lines
4131 if (imdct_mem > f->temp_memory_required)4132 f->temp_memory_required = imdct_mem;4133 }41344135 f->first_decode = TRUE;41354136 if (f->alloc.alloc_buffer) {4137 assert(f->temp_offset == f->alloc.alloc_buffer_length_in_bytes);4138 // check if there's enough temp memory so we don't error later4139 if (f->setup_offset + sizeof(*f) + f->temp_memory_required > (unsigned) f->temp_offset)4140 return error(f, VORBIS_outofmem);4141 }41424143 // @TODO: stb_vorbis_seek_start expects first_audio_page_offset to point to a page4144 // without PAGEFLAG_continued_packet, so this either points to the first page, or4145 // the page after the end of the headers. It might be cleaner to point to a page4146 // in the middle of the headers, when that's the page where the first audio packet4147 // starts, but we'd have to also correctly skip the end of any continued packet in4148 // stb_vorbis_seek_start.4149 if (f->next_seg == -1) {4144 f->first_audio_page_offset = stb_vorbis_get_file_offset(f);4150 f->first_audio_page_offset = stb_vorbis_get_file_offset(f);4151 } else {4152 f->first_audio_page_offset = 0;4153 }41544155 return TRUE;4156}4157
239 unmodified lines
4397 f->stream_end = (uint8 *) data + data_len;4398 f->error = VORBIS__no_error;43994400 // check that we have the entire packet in memory4392 if (!is_whole_packet_present(f, FALSE)) {4401 if (!is_whole_packet_present(f)) {4402 *samples = 0;4403 return 0;4404 }4405
227 unmodified lines
4633static int seek_to_sample_coarse(stb_vorbis *f, uint32 sample_number)4634{4635 ProbedPage left, right, mid;4636 int i, start_seg_with_known_loc, end_pos, page_start;4628 uint32 delta, stream_length, padding;4637 uint32 delta, stream_length, padding, last_sample_limit;4638 double offset, bytes_per_sample;4639 int probe = 0;46404641 // find the last page and validate the target sample
5 unmodified lines
4647 // actual granule position value), and the right-start (which the spec4648 // indicates should be the granule position (give or take one)).4649 padding = ((f->blocksize_1 - f->blocksize_0) >> 2);4650 if (sample_number < padding)4642 sample_number = 0;4651 last_sample_limit = 0;4652 else4644 sample_number -= padding;4653 last_sample_limit = sample_number - padding;46544655 left = f->p_first;4656 while (left.last_decoded_sample == ~0U) {4657 // (untested) the first page does not have a 'last_decoded_sample'
4 unmodified lines
4662 right = f->p_last;4663 assert(right.last_decoded_sample != ~0U);46644665 // starting from the start is handled differently4657 if (sample_number <= left.last_decoded_sample) {4658 if (stb_vorbis_seek_start(f))4666 if (last_sample_limit <= left.last_decoded_sample) {4667 if (stb_vorbis_seek_start(f)) {4668 if (f->current_loc > sample_number)4669 return error(f, VORBIS_seek_failed);4670 return 1;4671 }4672 return 0;4673 }46744675 while (left.page_end != right.page_start) {
8 unmodified lines
4684 if (probe == 0) {4685 // first probe (interpolate)4686 double data_bytes = right.page_end - left.page_start;4687 bytes_per_sample = data_bytes / right.last_decoded_sample;4676 offset = left.page_start + bytes_per_sample * (sample_number - left.last_decoded_sample);4688 offset = left.page_start + bytes_per_sample * (last_sample_limit - left.last_decoded_sample);4689 } else {4690 // second probe (try to bound the other side)4679 double error = ((double) sample_number - mid.last_decoded_sample) * bytes_per_sample;4691 double error = ((double) last_sample_limit - mid.last_decoded_sample) * bytes_per_sample;4692 if (error >= 0 && error < 8000) error = 8000;4693 if (error < 0 && error > -8000) error = -8000;4694 offset += error * 2;4695 }
22 unmodified lines
4718 assert(mid.page_start < right.page_start);4719 }47204721 // if we've just found the last page again then we're in a tricky file,4710 // and we're close enough.4711 if (mid.page_start == right.page_start)4712 break;4722 // and we're close enough (if it wasn't an interpolation probe).4723 if (mid.page_start == right.page_start) {4724 if (probe >= 2 || delta <= 65536)4725 break;4726 } else {4727 if (last_sample_limit < mid.last_decoded_sample)4728 right = mid;4729 else4730 left = mid;4731 }47324714 if (sample_number < mid.last_decoded_sample)4715 right = mid;4716 else4717 left = mid;47184733 ++probe;4734 }47354736 // seek back to start of the last packet
106 unmodified lines
4843 maybe_start_packet(f);4844 flush_packet(f);4845 }4846 }4833 // the next frame will start with the sample4834 assert(f->current_loc == sample_number);4847 // the next frame should start with the sample4848 if (f->current_loc != sample_number) return error(f, VORBIS_seek_failed);4849 return 1;4850}48514852int stb_vorbis_seek(stb_vorbis *f, unsigned int sample_number)
664 unmodified lines