File: ogg.c

package info (click to toggle)
libsndfile 1.2.0-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,616 kB
  • sloc: ansic: 55,313; cpp: 1,085; python: 791; sh: 539; makefile: 539; cs: 122
file content (921 lines) | stat: -rw-r--r-- 27,612 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
/*
** Copyright (C) 2002-2019 Erik de Castro Lopo <erikd@mega-nerd.com>
** Copyright (C) 2007 John ffitch
** Copyright (C) 2018 Arthur Taylor <art@ified.ca>
**
** This program is free software ; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation ; either version 2.1 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY ; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program ; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

/*
** This file contains code based on OpusFile and Opus-Tools, both by
** Xiph.Org. COPYING from each is identical and is as follows:
**
** Copyright (c) 1994-2013 Xiph.Org Foundation and contributors
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** - Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
**
** - Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
**
** - Neither the name of the Xiph.Org Foundation nor the names of its
** contributors may be used to endorse or promote products derived from
** this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION
** OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "sfconfig.h"

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>

#if HAVE_UNISTD_H
#include <unistd.h>
#else
#include "sf_unistd.h"
#endif

#include "sndfile.h"
#include "sfendian.h"
#include "common.h"

#if HAVE_EXTERNAL_XIPH_LIBS

#include <ogg/ogg.h>

#include "ogg.h"

#define OGG_SYNC_READ_SIZE (2048)
#define OGG_PAGE_SIZE_MAX (65307)
#define OGG_CHUNK_SIZE (65536)
#define OGG_CHUNK_SIZE_MAX (1024*1024)

/*
 * The Ogg container may seem overly complicated, particularly when used for a
 * on-disk audio file format. This is probably because Ogg is designed with
 * streaming rather than storage as a priority, and can handle multiple codec
 * payloads multiplexed together, then possibly chained on top of that.
 * Ogg achieves its goals well, but it does lend to a bit of a learning curve,
 * with many internal structures to push data around in compared to most sound
 * file formats which only have a header and raw data.
 *
 * See
 *  - [https://xiph.org/ogg/doc/oggstream.html]
 *  - [https://xiph.org/ogg/doc/framing.html]
 *
 * libogg Memory Management
 * ===========================================================================
 *
 * libOgg's memory management is documented in code, not in headers or external
 * documentation. What follows is not an attempt to completely document it, but
 * an explanation of the basics.
 *
 * libOgg has two data structures which allocate and manage data buffers: The
 * ogg_sync_state structure and the ogg_stream_state structure. The remaining
 * structures of ogg_page and ogg_packet are views into the buffers managed by
 * the previous structures.
 *
 * ogg_sync_state is used for reading purposes. It takes a physical bitstream
 * and searches for, validates, and returns complete Ogg Pages. The
 * ogg_sync_state buffers the returned page data, holding at most one
 * complete page at a time. A returned Ogg page remains valid until any
 * operation other than ogg_sync_check() is called.
 *
 * ogg_stream_state is used for both reading and writing. For reading, the
 * contents of an ogg_page is copied into the stream state. This data is
 * buffered to be split or joined as necessary into complete ogg_packets. If,
 * after copying an ogg_page into an ogg_stream_state, packets are available to
 * be read, then all of those packets remain in memory and valid until either
 * the ogg_stream_state is reset, destroyed, or a new ogg_page is read into it.
 * As the maximum number of packets an Ogg Page may contain is 255, at most 255
 * packets may be available from an ogg_stream_state at one time.
 *
 * For writing, the life cycle of a buffer pointed to by a ogg_packet is the
 * responsibility of the caller. Packets written into an ogg_stream_state are
 * buffered until a complete page is ready for writing. Pages for writing out
 * remain in the ogg_stream_state's buffer and valid until either the
 * ogg_stream_state is reset, cleared, destroyed. Writing another packet into
 * the ogg_stream_state might also invalidate such pages, but writing in
 * packets when a page is ready to be written out is a caller bug anyways.
 */

/*-----------------------------------------------------------------------------------------------
** Private function prototypes.
*/

static int		ogg_close (SF_PRIVATE *psf) ;
static int		ogg_stream_classify (SF_PRIVATE *psf, OGG_PRIVATE * odata) ;
static int		ogg_page_classify (SF_PRIVATE * psf, const ogg_page * og) ;
static uint64_t	ogg_page_search_do_rescale (uint64_t x, uint64_t from, uint64_t to) ;
static void		ogg_page_search_continued_data (OGG_PRIVATE *odata, ogg_page *page) ;

/*-----------------------------------------------------------------------------------------------
** Exported functions.
*/

int
ogg_read_first_page (SF_PRIVATE *psf, OGG_PRIVATE *odata)
{	int ret ;
	char *buffer ;

	/*
	** The ogg standard requires that the first pages of a physical ogg
	** bitstream be only the first pages of each logical bitstream. These
	** pages MUST have the Beginning-Of-Stream bit set, and must contain
	** only the stream's relevant header. Currently we only load the first
	** page and check that it contains a codec we support as supporting
	** multiplexed streams (video+audio(en)+audio(fs)+subtitles, etc) is
	** beyond the scope of this library.
	*/

	ret = ogg_sync_fseek (psf, psf->header.indx, SEEK_SET) ;
	if (ret < 0)
		return SFE_NOT_SEEKABLE ;

	buffer = ogg_sync_buffer (&odata->osync, psf->header.indx) ;
	if (buffer == NULL)
		return SFE_MALLOC_FAILED ;
	memcpy (buffer, psf->header.ptr, psf->header.indx) ;
	ogg_sync_wrote (&odata->osync, psf->header.indx) ;

	ret = ogg_sync_next_page (psf, &odata->opage, SF_MAX ((sf_count_t) 0, 4096 - psf->header.indx), NULL) ;

	/* Have we simply run out of data?  If so, we're done. */
	if (ret == 0)
		return 0 ;
	if (ret < 0)
		return psf->error ;

	if (!ogg_page_bos (&odata->opage))
	{	/*
		** Error case. Either must not be an Ogg bitstream, or is in the
		** middle of a bitstream (live capture), or in the middle of a
		** bitstream and no complete page was in the buffer.
		*/
		psf_log_printf (psf, "Input does not appear to be the start of an Ogg bitstream.\n") ;
		return SFE_MALFORMED_FILE ;
		} ;

	/*
	**	Get the serial number and set up the rest of decode.
	**	Serialno first ; use it to set up a logical stream.
	*/
	ogg_stream_reset_serialno (&odata->ostream, ogg_page_serialno (&odata->opage)) ;

	if (ogg_stream_pagein (&odata->ostream, &odata->opage) < 0)
	{	/* Error ; stream version mismatch perhaps. */
		psf_log_printf (psf, "Error reading first page of Ogg bitstream data\n") ;
		return SFE_MALFORMED_FILE ;
		} ;

	if (ogg_stream_packetout (&odata->ostream, &odata->opacket) != 1)
	{	/* No page? */
		psf_log_printf (psf, "Error reading initial header page packet.\n") ;
		return SFE_MALFORMED_FILE ;
		} ;

	return 0 ;
} /* ogg_read_first_page */

int
ogg_write_page (SF_PRIVATE *psf, ogg_page *page)
{	int n ;

	n = psf_fwrite (page->header, 1, page->header_len, psf) ;
	if (n == page->header_len)
		n += psf_fwrite (page->body, 1, page->body_len, psf) ;

	if (n != page->body_len + page->header_len)
		return -1 ;

	return n ;
} /* ogg_write_page */

sf_count_t
ogg_sync_ftell (SF_PRIVATE *psf)
{	OGG_PRIVATE* odata = (OGG_PRIVATE *) psf->container_data ;
	sf_count_t position ;

	position = psf_ftell (psf) ;
	if (position >= 0)
	{	/* success */
		if (position < odata->osync.fill)
		{	/* Really, this should be an assert. */
			psf->error = SFE_INTERNAL ;
			return -1 ;
			}
		position += (sf_count_t) (odata->osync.returned - odata->osync.fill) ;
		}

	return position ;
} /* ogg_sync_ftell */

sf_count_t
ogg_sync_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence)
{	OGG_PRIVATE* odata = (OGG_PRIVATE *) psf->container_data ;
	sf_count_t ret ;

	ret = psf_fseek (psf, offset, whence) ;
	if (ret >= 0)
	{	/* success */
		odata->eos = 0 ;
		ogg_sync_reset (&odata->osync) ;
		}

	return ret ;
} /* ogg_sync_fseek */

int
ogg_sync_next_page (SF_PRIVATE * psf, ogg_page *og, sf_count_t readmax, sf_count_t *offset)
{	OGG_PRIVATE* odata = (OGG_PRIVATE *) psf->container_data ;
	sf_count_t position, nb_read, read_ret ;
	unsigned char *buffer ;
	int synced ;
	int report_hole = 0 ;

	for (position = 0 ; readmax <= 0 || readmax > position ; )
	{	synced = ogg_sync_pageseek (&odata->osync, og) ;
		if (synced < 0)
		{	/*
			** Skipped -synced bytes before finding the start of a page.
			** If seeking, we have just landed in the middle of a page.
			** Otherwise, warn about junk in the bitstream.
			** Page might not yet be ready, hence the continue.
			*/
			if (!offset)
				report_hole = 1 ;
			position -= synced ;
			continue ;
			} ;

		if (report_hole)
		{	psf_log_printf (psf, "Ogg : Skipped %d bytes looking for the next page. Corrupted bitstream?!\n", position) ;
			report_hole = 0 ;
			} ;

		if (synced > 0)
		{	/* Have a page */
			if (offset)
				*offset += position ;
			return og->header_len + og->body_len ;
			} ;

		/*
		** Else readmax == 0, Out of data. Try to read more in without
		** invalidating our boundary (readmax) constraint.
		*/
		if (readmax == 0)
			return 0 ;
		if (readmax > 0)
			nb_read = SF_MIN ((sf_count_t) OGG_SYNC_READ_SIZE, readmax - position) ;
		else
			nb_read = OGG_SYNC_READ_SIZE ;
		buffer = (unsigned char *) ogg_sync_buffer (&odata->osync, nb_read) ;
		if (buffer == NULL)
		{	psf->error = SFE_MALLOC_FAILED ;
			return -1 ;
			}
		read_ret = psf_fread (buffer, 1, nb_read, psf) ;
		if (read_ret == 0)
			return psf->error ? -1 : 0 ;
		ogg_sync_wrote (&odata->osync, read_ret) ;
		} ;
	return 0 ;
} /* ogg_sync_next_page */

int
ogg_stream_next_page (SF_PRIVATE *psf, OGG_PRIVATE *odata)
{	int nn ;

	if (odata->eos)
		return 0 ;

	for ( ; ; )
	{	nn = ogg_sync_next_page (psf, &odata->opage, -1, NULL) ;
		if (nn == 0)
		{	psf_log_printf (psf, "Ogg : File ended unexpectedly without an End-Of-Stream flag set.\n") ;
			odata->eos = 1 ;
			}
		if (nn <= 0)
			return nn ;

		if (ogg_page_serialno (&odata->opage) == odata->ostream.serialno)
			break ;
		} ;

	if (ogg_page_eos (&odata->opage))
		odata->eos = 1 ;

	if (ogg_stream_pagein (&odata->ostream, &odata->opage) < 0)
	{	psf->error = SFE_INTERNAL ;
		return -1 ;
		}

	return 1 ;
} /* ogg_stream_next_page */

int
ogg_stream_unpack_page (SF_PRIVATE *psf, OGG_PRIVATE *odata)
{	int nn ;
	int i ;
	int found_hole = 0 ;
	ogg_packet *ppkt = odata->pkt ;

	odata->pkt_indx = 0 ;
	nn = ogg_stream_packetout (&odata->ostream, ppkt) ;
	if (nn == 0)
	{	/*
		** Steam is out of packets. Read in more pages until there is one, or
		** the stream ends, or an error occurs.
		*/
		for ( ; nn == 0 ; nn = ogg_stream_packetout (&odata->ostream, ppkt))
		{	nn = ogg_stream_next_page (psf, odata) ;
			if (nn <= 0)
			{	odata->pkt_len = 0 ;
				return nn ;
				}
			}
		/*
		** In the case of the for loop exiting because
		** ogg_stream_packetout() == -1, fall-through.
		*/
		}

	if (nn == -1)
	{	/*
		** libOgg found a hole. That is, the next packet found was out of
		** sequence. As such, "flush" the hole marker by removing the invalid
		** packet, as the valid packets are queued behind it.
		*/
		psf_log_printf (psf, "Ogg : Warning, libogg reports a hole at %d bytes.\n", ogg_sync_ftell (psf)) ;
		nn = ogg_stream_packetout (&odata->ostream, ppkt) ;
		found_hole = 1 ;
		}

	/*
	** Unpack all the packets on the page. It is undocumented (like much of
	** libOgg behavior) but all packets from a page read into the stream are
	** guarenteed to remain valid in memory until a new page is read into the
	** stream.
	*/
	for (i = 1 ; ; i++)
	{	/* Not an off-by-one, there are 255 not 256 packets max. */
		if (i == 255)
		{	if (ogg_stream_packetpeek (&odata->ostream, NULL) == 1)
			{	psf->error = SFE_INTERNAL ;
				return -1 ;
				}
			break ;
			}
		if (ogg_stream_packetout (&odata->ostream, ++ ppkt) != 1)
			break ;
		}
	odata->pkt_len = i ;

	/* 1 = ok, 2 = ok, and found a hole. */
	return 1 + found_hole ;
} /* ogg_stream_unpack_page */

sf_count_t
ogg_sync_last_page_before (SF_PRIVATE *psf, OGG_PRIVATE *odata, uint64_t *gp_out, sf_count_t offset, int32_t serialno)
{	sf_count_t begin, end, original_end, chunk_size, ret ;
	sf_count_t position = 0 ;
	uint64_t gp = -1 ;
	int left_link ;

	/* Based on code from Xiph.org's Opusfile */

	original_end = end = begin = offset ;
	offset = -1 ;
	chunk_size = OGG_CHUNK_SIZE ;
	do
	{	begin = SF_MAX (begin - chunk_size, (sf_count_t) 0) ;
		position = ogg_sync_fseek (psf, begin, SEEK_SET) ;
		if (position < 0)
			return position ;
		left_link = 0 ;
		while (position < end)
		{	ret = ogg_sync_next_page (psf, &odata->opage, end - position, &position) ;
			if (ret <= 0)
				return -1 ;
			if (ogg_page_serialno (&odata->opage) == serialno)
			{	uint64_t page_gp = ogg_page_granulepos (&odata->opage) ;
				if (page_gp != (uint64_t) -1)
				{	offset = position ;
					gp = page_gp ;
					}
				}
			else
				left_link = 1 ;
			position += ret ;
			}

		if ((left_link || !begin) && offset < 0)
		{	psf->error = SFE_MALFORMED_FILE ;
			return -1 ;
			}

		chunk_size = SF_MIN (2 * chunk_size, (sf_count_t) OGG_CHUNK_SIZE_MAX) ;
		end = SF_MIN (begin + OGG_PAGE_SIZE_MAX - 1, original_end) ;
		}
	while (offset < 0) ;

	*gp_out = gp ;
	return offset ;
} /* ogg_sync_last_page_before */

int
ogg_stream_seek_page_search (SF_PRIVATE *psf, OGG_PRIVATE *odata,
	uint64_t target_gp, uint64_t pcm_start, uint64_t pcm_end, uint64_t *best_gp,
	sf_count_t begin, sf_count_t end, uint64_t gp_rate)
{	ogg_page page ;
	uint64_t gp ;
	sf_count_t d0, d1, d2 ;
	sf_count_t best ;
	sf_count_t best_start ;
	sf_count_t boundary ;
	sf_count_t next_boundary ;
	sf_count_t page_offset = -1 ;
	sf_count_t seek_pos = -1 ;
	sf_count_t bisect ;
	sf_count_t chunk_size ;
	int buffering = SF_FALSE ;
	int force_bisect = SF_FALSE ;
	int ret ;
	int has_packets ;

	*best_gp = pcm_start ;
	best = best_start = begin ;
	boundary = end ;

	ogg_stream_reset_serialno (&odata->ostream, odata->ostream.serialno) ;

	/*
	** This code is based on op_pcm_seek_page() from Opusfile, which is in turn
	** based on "new search algorithm by Nicholas Vinen" from libvorbisfile.
	*/

	d2 = d1 = d0 = end - begin ;
	while (begin < end)
	{	/*
		** Figure out if and where to try and seek in the file.
		*/
		if (end - begin < OGG_CHUNK_SIZE)
			bisect = begin ;
		else
		{	/* Update the interval size history */
			d0 = d1 >> 1 ;
			d1 = d2 >> 1 ;
			d2 = (end - begin) >> 1 ;
			if (force_bisect == SF_TRUE)
				bisect = begin + ((end - begin) >> 1) ;
			else
			{	/* Take a decent guess. */
				bisect = begin + ogg_page_search_do_rescale (target_gp - pcm_start, pcm_end - pcm_start, end - begin) ;
				}
			if (bisect - OGG_CHUNK_SIZE < begin)
				bisect = begin ;
			else
				bisect -= OGG_CHUNK_SIZE ;
			force_bisect = SF_FALSE ;
			}

		/*
		** Avoid an actual fseek if we can (common for final iterations.)
		*/
		if (seek_pos != bisect)
		{	if (buffering == SF_TRUE)
				ogg_stream_reset (&odata->ostream) ;
			buffering = SF_FALSE ;
			page_offset = -1 ;
			seek_pos = ogg_sync_fseek (psf, bisect, SEEK_SET) ;
			if (seek_pos < 0)
				return seek_pos ;
			}

		chunk_size = OGG_CHUNK_SIZE ;
		next_boundary = boundary ;

		/*
		** Scan forward, figure out where we landed.
		** The ideal case is we see a page that ends before our target followed
		** by a page that ends after our target.
		** If we are too far before or after, breaking out will bisect what we
		** have found so far.
		*/
		while (begin < end)
		{	ret = ogg_sync_next_page (psf, &page, boundary - seek_pos, &seek_pos) ;
			if (ret <= 0)
				return ret ;
			page_offset = seek_pos ;
			if (ret == 0)
			{	/*
				** There are no more pages in this interval from our stream
				** with a granulepos less than our target.
				*/
				if (bisect <= begin + 1)
				{	/* Scanned the whole interval, so we are done. */
					end = begin ;
					}
				else
				{	/*
					** Otherwise, back up one chunk. First discard any data
					** from a continued packet.
					*/
					if (buffering)
						ogg_stream_reset (&odata->ostream) ;
					buffering = SF_FALSE ;
					bisect = SF_MAX (bisect - chunk_size, begin) ;
					seek_pos = ogg_sync_fseek (psf, bisect, SEEK_SET) ;
					if (seek_pos < 0)
						return seek_pos ;
					/* Bump up the chunk size. */
					chunk_size = SF_MIN (2 * chunk_size, (sf_count_t) OGG_CHUNK_SIZE_MAX) ;
					/*
					** If we did find a page from another stream or without a
					** timestamp, don't read past it.
					*/
					boundary = next_boundary ;
					}
				continue ;
				}

			/* Found a page. Advance seek_pos past it */
			seek_pos += page.header_len + page.body_len ;
			/*
			** Save the offset of the first page we found after the seek,
			** regardless of the stream it came from or whether or not it has a
			** timestamp.
			*/
			next_boundary = SF_MIN (page_offset, next_boundary) ;

			/* If not from our stream, continue. */
			if (odata->ostream.serialno != ogg_page_serialno (&page))
				continue ;

			/*
			** The Ogg spec says that a page with a granule pos of -1 must not
			** contain and packets which complete, but the lack of biconditional
			** wording means that /technically/ a packet which does not complete
			** any packets can have a granule pos other than -1. To make matters
			** worse, older versions of libogg did just that.
			*/
			has_packets = ogg_page_packets (&page) > 0 ;
			gp = has_packets ? ogg_page_granulepos (&page) : -1 ;
			if (gp == (uint64_t) -1)
			{	if (buffering == SF_TRUE)
				{	if (!has_packets)
						ogg_stream_pagein (&odata->ostream, &page) ;
					else
					{	/*
						** If packets did end on this page, but we still didn't
						** have a valid granule position (in violation of the
						** spec!), stop buffering continued packet data.
						** Otherwise we might continue past the packet we
						** actually wanted.
						*/
						ogg_stream_reset (&odata->ostream) ;
						buffering = SF_FALSE ;
						}
					}
				continue ;
				}

			if (gp < target_gp)
			{	/*
				** We found a page that ends before our target. Advance to
				** the raw offset of the next page.
				*/
				begin = seek_pos ;
				if (pcm_start > gp || pcm_end < gp)
					break ;
				/* Save the byte offset of after this page. */
				best = best_start = begin ;
				if (buffering)
					ogg_stream_reset (&odata->ostream) ;
				/* Check to see if the last packet continues. */
				if (ogg_page_continues (&page))
				{	ogg_page_search_continued_data (odata, &page) ;
					/*
					** If we have a continued packet, remember the offset of
					** this page's start, so that if we do wind up having to
					** seek back here later, we can prime the stream with the
					** continued packet data. With no continued packet, we
					** remember the end of the page.
					*/
					best_start = page_offset ;
					/*
					** Then force buffering on, so that if a packet starts (but
					** does not end) on the next page, we still avoid the extra
					** seek back.
					*/
					buffering = SF_TRUE ;
				} ;
				*best_gp = pcm_start = gp ;
				if (target_gp - gp > gp_rate)
				{	/* Out by over a second. Try another bisection. */
					break ;
					}
				/* Otherwise, keep scanning forward (do NOT use begin+1). */
				bisect = begin ;
				}
			else
			{	/*
				** Found a page that ends after our target. If we had just
				** scanned the whole interval before we found it, we're good.
				*/
				if (bisect <= begin + 1)
					end = begin ;
				else
				{	end = bisect ;
					/*
					** In later iterations, don't read past the first page we
					** found.
					*/
					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 > d0 * 2 ? SF_TRUE : SF_FALSE ;
					/*
					** Don't let pcm_end get out of range! That could happen
					** with an invalid timestamp.
					*/
					if (pcm_end > gp && pcm_start <= gp)
						pcm_end = gp ;
					}
				break ;
				}
			}
		}

	/*
	** If we are buffering, the page we want is currently buffered in the
	** Ogg stream structure, or in the Ogg page which has not been submitted.
	** If not, we need to seek back and load it again.
	*/
	if (buffering == SF_FALSE)
	{	if (best_start != page_offset)
		{	page_offset = -1 ;
			seek_pos = ogg_sync_fseek (psf, best_start, SEEK_SET) ;
			if (seek_pos < 0)
				return seek_pos ;
			}
		if (best_start < best)
		{	if (page_offset < 0)
			{	ret = ogg_sync_next_page (psf, &page, -1, &seek_pos) ;
				if (seek_pos != best_start)
					return -1 ;
				}
			ogg_page_search_continued_data (odata, &page) ;
			page_offset = -1 ;
			}
		} ;

	if (page_offset >= 0)
		ogg_stream_pagein (&odata->ostream, &page) ;

	return 0 ;
} /* ogg_stream_seek_page_search */

int
ogg_open (SF_PRIVATE *psf)
{	OGG_PRIVATE* odata = calloc (1, sizeof (OGG_PRIVATE)) ;
	sf_count_t pos = psf_ftell (psf) ;
	int	error = 0 ;

	psf->container_data = odata ;
	psf->container_close = ogg_close ;

	if (psf->file.mode == SFM_RDWR)
		return SFE_BAD_MODE_RW ;

	if (psf->file.mode == SFM_READ)
		if ((error = ogg_stream_classify (psf, odata)) != 0)
			return error ;

	if (SF_ENDIAN (psf->sf.format) != 0)
		return SFE_BAD_ENDIAN ;

	switch (psf->sf.format)
	{	case SF_FORMAT_OGG | SF_FORMAT_VORBIS :
			return ogg_vorbis_open (psf) ;

		case SF_FORMAT_OGGFLAC :
			/* Reset everything to an initial state. */
			ogg_sync_clear (&odata->osync) ;
			ogg_stream_clear (&odata->ostream) ;
			psf_fseek (psf, pos, SEEK_SET) ;
			free (psf->container_data) ;
			psf->container_data = NULL ;
			psf->container_close = NULL ;
			return flac_open (psf) ;

		case SF_FORMAT_OGG | SF_FORMAT_OPUS :
			return ogg_opus_open (psf) ;

#if ENABLE_EXPERIMENTAL_CODE
		case SF_FORMAT_OGG | SF_FORMAT_SPEEX :
			return ogg_speex_open (psf) ;

		case SF_FORMAT_OGG | SF_FORMAT_PCM_16 :
		case SF_FORMAT_OGG | SF_FORMAT_PCM_24 :
			return ogg_pcm_open (psf) ;
#endif

		default :
			break ;
		} ;

	psf_log_printf (psf, "%s : bad psf->sf.format 0x%x.\n", __func__, psf->sf.format) ;
	return SFE_INTERNAL ;
} /* ogg_open */

/*==============================================================================
** Private functions.
*/

static int
ogg_close (SF_PRIVATE *psf)
{	OGG_PRIVATE* odata = psf->container_data ;

	ogg_sync_clear (&odata->osync) ;
	ogg_stream_clear (&odata->ostream) ;

	return 0 ;
} /* ogg_close */

static int
ogg_stream_classify (SF_PRIVATE *psf, OGG_PRIVATE* odata)
{	int error ;

	/* Call this here so it only gets called once, so no memory is leaked. */
	ogg_sync_init (&odata->osync) ;
	ogg_stream_init (&odata->ostream, 0) ;

	/* Load the first page in the physical bitstream. */
	if ((error = ogg_read_first_page (psf, odata)) != 0)
		return error ;

	odata->codec = ogg_page_classify (psf, &odata->opage) ;

	switch (odata->codec)
	{	case OGG_VORBIS :
			psf->sf.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ;
			return 0 ;

		case OGG_FLAC :
		case OGG_FLAC0 :
			psf->sf.format = SF_FORMAT_OGGFLAC ;
			return 0 ;

		case OGG_SPEEX :
			psf->sf.format = SF_FORMAT_OGG | SF_FORMAT_SPEEX ;
			return 0 ;

		case OGG_OPUS :
			psf->sf.format = SF_FORMAT_OGG | SF_FORMAT_OPUS ;
			return 0 ;

		case OGG_PCM :
			psf_log_printf (psf, "Detected Ogg/PCM data. This is not supported yet.\n") ;
			return SFE_UNIMPLEMENTED ;

		default :
			break ;
		} ;

	psf_log_printf (psf, "This Ogg bitstream contains some unknown data type.\n") ;
	return SFE_UNIMPLEMENTED ;
} /* ogg_stream_classify */

/*==============================================================================
*/

static struct
{	const char *str, *name ;
	int len, codec ;
} codec_lookup [] =
{	{	"Annodex",		"Annodex",	8, OGG_ANNODEX },
	{	"AnxData",		"AnxData",	7, OGG_ANXDATA },
	{	"\177FLAC",		"Flac1",	5, OGG_FLAC },
	{	"fLaC",			"Flac0",	4, OGG_FLAC0 },
	{	"PCM     ",		"PCM",		8, OGG_PCM },
	{	"Speex",		"Speex",	5, OGG_SPEEX },
	{	"\001vorbis",	"Vorbis",	7, OGG_VORBIS },
	{	"OpusHead",		"Opus",		8, OGG_OPUS },
} ;

static int
ogg_page_classify (SF_PRIVATE * psf, const ogg_page * og)
{	int k, len ;

	for (k = 0 ; k < ARRAY_LEN (codec_lookup) ; k++)
	{	if (codec_lookup [k].len > og->body_len)
			continue ;

		if (memcmp (og->body, codec_lookup [k].str, codec_lookup [k].len) == 0)
		{	psf_log_printf (psf, "Ogg stream data : %s\n", codec_lookup [k].name) ;
			psf_log_printf (psf, "Stream serialno : %u\n", (uint32_t) ogg_page_serialno (og)) ;
			return codec_lookup [k].codec ;
			} ;
		} ;

	len = og->body_len < 8 ? og->body_len : 8 ;

	psf_log_printf (psf, "Ogg_stream data : '") ;
	for (k = 0 ; k < len ; k++)
		psf_log_printf (psf, "%c", isprint (og->body [k]) ? og->body [k] : '.') ;
	psf_log_printf (psf, "'     ") ;
	for (k = 0 ; k < len ; k++)
		psf_log_printf (psf, " %02x", og->body [k] & 0xff) ;
	psf_log_printf (psf, "\n") ;

	return 0 ;
} /* ogg_page_classify */

/*
** Scale x from the range [0, from] to the range [0, to]
*/
static uint64_t
ogg_page_search_do_rescale (uint64_t x, uint64_t from, uint64_t to)
{	uint64_t frac ;
	uint64_t ret ;
	int i ;

	/* I should have paid more attention in CSc 349A: Numerical Analysis */
	if (x >= from)
		return to ;
	if (x == 0)
		return 0 ;
	frac = 0 ;
	for (i = 0 ; i < 63 ; i++)
	{	frac <<= 1 ;
		if (x >= from >> 1)
		{	x -= from - x ;
			frac |= 1 ;
			}
		else
			x <<= 1 ;
		}
	ret = 0 ;
	for (i = 0 ; i < 63 ; i++)
	{	if (frac & 1)
			ret = (ret & to & 1) + (ret >> 1) + (to >> 1) ;
		else
			ret >>= 1 ;
		frac >>= 1 ;
		}
	return ret ;
} /* ogg_page_search_do_rescale */

static void
ogg_page_search_continued_data (OGG_PRIVATE *odata, ogg_page *page)
{	ogg_stream_pagein (&odata->ostream, page) ;
	while (ogg_stream_packetout (&odata->ostream, &odata->opacket)) ;
} /* ogg_page_search_continued_data */

#else /* HAVE_EXTERNAL_XIPH_LIBS */

int
ogg_open	(SF_PRIVATE *psf)
{
	psf_log_printf (psf, "This version of libsndfile was compiled without Ogg/Vorbis support.\n") ;
	return SFE_UNIMPLEMENTED ;
} /* ogg_open */

#endif