/src/libsndfile/src/ogg.c
Line | Count | Source |
1 | | /* |
2 | | ** Copyright (C) 2002-2019 Erik de Castro Lopo <erikd@mega-nerd.com> |
3 | | ** Copyright (C) 2007 John ffitch |
4 | | ** Copyright (C) 2018 Arthur Taylor <art@ified.ca> |
5 | | ** |
6 | | ** This program is free software ; you can redistribute it and/or modify |
7 | | ** it under the terms of the GNU Lesser General Public License as published by |
8 | | ** the Free Software Foundation ; either version 2.1 of the License, or |
9 | | ** (at your option) any later version. |
10 | | ** |
11 | | ** This program is distributed in the hope that it will be useful, |
12 | | ** but WITHOUT ANY WARRANTY ; without even the implied warranty of |
13 | | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | ** GNU Lesser General Public License for more details. |
15 | | ** |
16 | | ** You should have received a copy of the GNU Lesser General Public License |
17 | | ** along with this program ; if not, write to the Free Software |
18 | | ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 | | */ |
20 | | |
21 | | /* |
22 | | ** This file contains code based on OpusFile and Opus-Tools, both by |
23 | | ** Xiph.Org. COPYING from each is identical and is as follows: |
24 | | ** |
25 | | ** Copyright (c) 1994-2013 Xiph.Org Foundation and contributors |
26 | | ** |
27 | | ** Redistribution and use in source and binary forms, with or without |
28 | | ** modification, are permitted provided that the following conditions |
29 | | ** are met: |
30 | | ** |
31 | | ** - Redistributions of source code must retain the above copyright |
32 | | ** notice, this list of conditions and the following disclaimer. |
33 | | ** |
34 | | ** - Redistributions in binary form must reproduce the above copyright |
35 | | ** notice, this list of conditions and the following disclaimer in the |
36 | | ** documentation and/or other materials provided with the distribution. |
37 | | ** |
38 | | ** - Neither the name of the Xiph.Org Foundation nor the names of its |
39 | | ** contributors may be used to endorse or promote products derived from |
40 | | ** this software without specific prior written permission. |
41 | | ** |
42 | | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
43 | | ** ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
44 | | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
45 | | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION |
46 | | ** OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
47 | | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
48 | | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
49 | | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
50 | | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
51 | | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
52 | | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
53 | | */ |
54 | | |
55 | | #include "sfconfig.h" |
56 | | |
57 | | #include <stdio.h> |
58 | | #include <fcntl.h> |
59 | | #include <string.h> |
60 | | #include <ctype.h> |
61 | | #include <time.h> |
62 | | #include <math.h> |
63 | | |
64 | | #if HAVE_UNISTD_H |
65 | | #include <unistd.h> |
66 | | #else |
67 | | #include "sf_unistd.h" |
68 | | #endif |
69 | | |
70 | | #include "sndfile.h" |
71 | | #include "sfendian.h" |
72 | | #include "common.h" |
73 | | |
74 | | #if HAVE_EXTERNAL_XIPH_LIBS |
75 | | |
76 | | #include <ogg/ogg.h> |
77 | | |
78 | | #include "ogg.h" |
79 | | |
80 | | #define OGG_SYNC_READ_SIZE (2048) |
81 | | #define OGG_PAGE_SIZE_MAX (65307) |
82 | | #define OGG_CHUNK_SIZE (65536) |
83 | | #define OGG_CHUNK_SIZE_MAX (1024*1024) |
84 | | |
85 | | /* |
86 | | * The Ogg container may seem overly complicated, particularly when used for a |
87 | | * on-disk audio file format. This is probably because Ogg is designed with |
88 | | * streaming rather than storage as a priority, and can handle multiple codec |
89 | | * payloads multiplexed together, then possibly chained on top of that. |
90 | | * Ogg achieves its goals well, but it does lend to a bit of a learning curve, |
91 | | * with many internal structures to push data around in compared to most sound |
92 | | * file formats which only have a header and raw data. |
93 | | * |
94 | | * See |
95 | | * - [https://xiph.org/ogg/doc/oggstream.html] |
96 | | * - [https://xiph.org/ogg/doc/framing.html] |
97 | | * |
98 | | * libogg Memory Management |
99 | | * =========================================================================== |
100 | | * |
101 | | * libOgg's memory management is documented in code, not in headers or external |
102 | | * documentation. What follows is not an attempt to completely document it, but |
103 | | * an explanation of the basics. |
104 | | * |
105 | | * libOgg has two data structures which allocate and manage data buffers: The |
106 | | * ogg_sync_state structure and the ogg_stream_state structure. The remaining |
107 | | * structures of ogg_page and ogg_packet are views into the buffers managed by |
108 | | * the previous structures. |
109 | | * |
110 | | * ogg_sync_state is used for reading purposes. It takes a physical bitstream |
111 | | * and searches for, validates, and returns complete Ogg Pages. The |
112 | | * ogg_sync_state buffers the returned page data, holding at most one |
113 | | * complete page at a time. A returned Ogg page remains valid until any |
114 | | * operation other than ogg_sync_check() is called. |
115 | | * |
116 | | * ogg_stream_state is used for both reading and writing. For reading, the |
117 | | * contents of an ogg_page is copied into the stream state. This data is |
118 | | * buffered to be split or joined as necessary into complete ogg_packets. If, |
119 | | * after copying an ogg_page into an ogg_stream_state, packets are available to |
120 | | * be read, then all of those packets remain in memory and valid until either |
121 | | * the ogg_stream_state is reset, destroyed, or a new ogg_page is read into it. |
122 | | * As the maximum number of packets an Ogg Page may contain is 255, at most 255 |
123 | | * packets may be available from an ogg_stream_state at one time. |
124 | | * |
125 | | * For writing, the life cycle of a buffer pointed to by a ogg_packet is the |
126 | | * responsibility of the caller. Packets written into an ogg_stream_state are |
127 | | * buffered until a complete page is ready for writing. Pages for writing out |
128 | | * remain in the ogg_stream_state's buffer and valid until either the |
129 | | * ogg_stream_state is reset, cleared, destroyed. Writing another packet into |
130 | | * the ogg_stream_state might also invalidate such pages, but writing in |
131 | | * packets when a page is ready to be written out is a caller bug anyways. |
132 | | */ |
133 | | |
134 | | /*----------------------------------------------------------------------------------------------- |
135 | | ** Private function prototypes. |
136 | | */ |
137 | | |
138 | | static int ogg_close (SF_PRIVATE *psf) ; |
139 | | static int ogg_stream_classify (SF_PRIVATE *psf, OGG_PRIVATE * odata) ; |
140 | | static int ogg_page_classify (SF_PRIVATE * psf, const ogg_page * og) ; |
141 | | static uint64_t ogg_page_search_do_rescale (uint64_t x, uint64_t from, uint64_t to) ; |
142 | | static void ogg_page_search_continued_data (OGG_PRIVATE *odata, ogg_page *page) ; |
143 | | |
144 | | /*----------------------------------------------------------------------------------------------- |
145 | | ** Exported functions. |
146 | | */ |
147 | | |
148 | | int |
149 | | ogg_read_first_page (SF_PRIVATE *psf, OGG_PRIVATE *odata) |
150 | | { int ret ; |
151 | | char *buffer ; |
152 | | |
153 | | /* |
154 | | ** The ogg standard requires that the first pages of a physical ogg |
155 | | ** bitstream be only the first pages of each logical bitstream. These |
156 | | ** pages MUST have the Beginning-Of-Stream bit set, and must contain |
157 | | ** only the stream's relevant header. Currently we only load the first |
158 | | ** page and check that it contains a codec we support as supporting |
159 | | ** multiplexed streams (video+audio(en)+audio(fs)+subtitles, etc) is |
160 | | ** beyond the scope of this library. |
161 | | */ |
162 | | |
163 | | ret = ogg_sync_fseek (psf, psf->header.indx, SEEK_SET) ; |
164 | | if (ret < 0) |
165 | | return SFE_NOT_SEEKABLE ; |
166 | | |
167 | | buffer = ogg_sync_buffer (&odata->osync, psf->header.indx) ; |
168 | | if (buffer == NULL) |
169 | | return SFE_MALLOC_FAILED ; |
170 | | memcpy (buffer, psf->header.ptr, psf->header.indx) ; |
171 | | ogg_sync_wrote (&odata->osync, psf->header.indx) ; |
172 | | |
173 | | ret = ogg_sync_next_page (psf, &odata->opage, SF_MAX ((sf_count_t) 0, 4096 - psf->header.indx), NULL) ; |
174 | | |
175 | | /* Have we simply run out of data? If so, we're done. */ |
176 | | if (ret == 0) |
177 | | return 0 ; |
178 | | if (ret < 0) |
179 | | return psf->error ; |
180 | | |
181 | | if (!ogg_page_bos (&odata->opage)) |
182 | | { /* |
183 | | ** Error case. Either must not be an Ogg bitstream, or is in the |
184 | | ** middle of a bitstream (live capture), or in the middle of a |
185 | | ** bitstream and no complete page was in the buffer. |
186 | | */ |
187 | | psf_log_printf (psf, "Input does not appear to be the start of an Ogg bitstream.\n") ; |
188 | | return SFE_MALFORMED_FILE ; |
189 | | } ; |
190 | | |
191 | | /* |
192 | | ** Get the serial number and set up the rest of decode. |
193 | | ** Serialno first ; use it to set up a logical stream. |
194 | | */ |
195 | | ogg_stream_reset_serialno (&odata->ostream, ogg_page_serialno (&odata->opage)) ; |
196 | | |
197 | | if (ogg_stream_pagein (&odata->ostream, &odata->opage) < 0) |
198 | | { /* Error ; stream version mismatch perhaps. */ |
199 | | psf_log_printf (psf, "Error reading first page of Ogg bitstream data\n") ; |
200 | | return SFE_MALFORMED_FILE ; |
201 | | } ; |
202 | | |
203 | | if (ogg_stream_packetout (&odata->ostream, &odata->opacket) != 1) |
204 | | { /* No page? */ |
205 | | psf_log_printf (psf, "Error reading initial header page packet.\n") ; |
206 | | return SFE_MALFORMED_FILE ; |
207 | | } ; |
208 | | |
209 | | return 0 ; |
210 | | } /* ogg_read_first_page */ |
211 | | |
212 | | int |
213 | | ogg_write_page (SF_PRIVATE *psf, ogg_page *page) |
214 | | { int n ; |
215 | | |
216 | | n = psf_fwrite (page->header, 1, page->header_len, psf) ; |
217 | | if (n == page->header_len) |
218 | | n += psf_fwrite (page->body, 1, page->body_len, psf) ; |
219 | | |
220 | | if (n != page->body_len + page->header_len) |
221 | | return -1 ; |
222 | | |
223 | | return n ; |
224 | | } /* ogg_write_page */ |
225 | | |
226 | | sf_count_t |
227 | | ogg_sync_ftell (SF_PRIVATE *psf) |
228 | | { OGG_PRIVATE* odata = (OGG_PRIVATE *) psf->container_data ; |
229 | | sf_count_t position ; |
230 | | |
231 | | position = psf_ftell (psf) ; |
232 | | if (position >= 0) |
233 | | { /* success */ |
234 | | if (position < odata->osync.fill) |
235 | | { /* Really, this should be an assert. */ |
236 | | psf->error = SFE_INTERNAL ; |
237 | | return -1 ; |
238 | | } |
239 | | position += (sf_count_t) (odata->osync.returned - odata->osync.fill) ; |
240 | | } |
241 | | |
242 | | return position ; |
243 | | } /* ogg_sync_ftell */ |
244 | | |
245 | | sf_count_t |
246 | | ogg_sync_fseek (SF_PRIVATE *psf, sf_count_t offset, int whence) |
247 | | { OGG_PRIVATE* odata = (OGG_PRIVATE *) psf->container_data ; |
248 | | sf_count_t ret ; |
249 | | |
250 | | ret = psf_fseek (psf, offset, whence) ; |
251 | | if (ret >= 0) |
252 | | { /* success */ |
253 | | odata->eos = 0 ; |
254 | | ogg_sync_reset (&odata->osync) ; |
255 | | } |
256 | | |
257 | | return ret ; |
258 | | } /* ogg_sync_fseek */ |
259 | | |
260 | | int |
261 | | ogg_sync_next_page (SF_PRIVATE * psf, ogg_page *og, sf_count_t readmax, sf_count_t *offset) |
262 | | { OGG_PRIVATE* odata = (OGG_PRIVATE *) psf->container_data ; |
263 | | sf_count_t position, nb_read, read_ret ; |
264 | | unsigned char *buffer ; |
265 | | int synced ; |
266 | | int report_hole = 0 ; |
267 | | |
268 | | for (position = 0 ; readmax <= 0 || readmax > position ; ) |
269 | | { synced = ogg_sync_pageseek (&odata->osync, og) ; |
270 | | if (synced < 0) |
271 | | { /* |
272 | | ** Skipped -synced bytes before finding the start of a page. |
273 | | ** If seeking, we have just landed in the middle of a page. |
274 | | ** Otherwise, warn about junk in the bitstream. |
275 | | ** Page might not yet be ready, hence the continue. |
276 | | */ |
277 | | if (!offset) |
278 | | report_hole = 1 ; |
279 | | position -= synced ; |
280 | | continue ; |
281 | | } ; |
282 | | |
283 | | if (report_hole) |
284 | | { psf_log_printf (psf, "Ogg : Skipped %d bytes looking for the next page. Corrupted bitstream?!\n", position) ; |
285 | | report_hole = 0 ; |
286 | | } ; |
287 | | |
288 | | if (synced > 0) |
289 | | { /* Have a page */ |
290 | | if (offset) |
291 | | *offset += position ; |
292 | | return og->header_len + og->body_len ; |
293 | | } ; |
294 | | |
295 | | /* |
296 | | ** Else readmax == 0, Out of data. Try to read more in without |
297 | | ** invalidating our boundary (readmax) constraint. |
298 | | */ |
299 | | if (readmax == 0) |
300 | | return 0 ; |
301 | | if (readmax > 0) |
302 | | nb_read = SF_MIN ((sf_count_t) OGG_SYNC_READ_SIZE, readmax - position) ; |
303 | | else |
304 | | nb_read = OGG_SYNC_READ_SIZE ; |
305 | | buffer = (unsigned char *) ogg_sync_buffer (&odata->osync, nb_read) ; |
306 | | if (buffer == NULL) |
307 | | { psf->error = SFE_MALLOC_FAILED ; |
308 | | return -1 ; |
309 | | } |
310 | | read_ret = psf_fread (buffer, 1, nb_read, psf) ; |
311 | | if (read_ret == 0) |
312 | | return psf->error ? -1 : 0 ; |
313 | | ogg_sync_wrote (&odata->osync, read_ret) ; |
314 | | } ; |
315 | | return 0 ; |
316 | | } /* ogg_sync_next_page */ |
317 | | |
318 | | int |
319 | | ogg_stream_next_page (SF_PRIVATE *psf, OGG_PRIVATE *odata) |
320 | | { int nn ; |
321 | | |
322 | | if (odata->eos) |
323 | | return 0 ; |
324 | | |
325 | | for ( ; ; ) |
326 | | { nn = ogg_sync_next_page (psf, &odata->opage, -1, NULL) ; |
327 | | if (nn == 0) |
328 | | { psf_log_printf (psf, "Ogg : File ended unexpectedly without an End-Of-Stream flag set.\n") ; |
329 | | odata->eos = 1 ; |
330 | | } |
331 | | if (nn <= 0) |
332 | | return nn ; |
333 | | |
334 | | if (ogg_page_serialno (&odata->opage) == odata->ostream.serialno) |
335 | | break ; |
336 | | } ; |
337 | | |
338 | | if (ogg_page_eos (&odata->opage)) |
339 | | odata->eos = 1 ; |
340 | | |
341 | | if (ogg_stream_pagein (&odata->ostream, &odata->opage) < 0) |
342 | | { psf->error = SFE_INTERNAL ; |
343 | | return -1 ; |
344 | | } |
345 | | |
346 | | return 1 ; |
347 | | } /* ogg_stream_next_page */ |
348 | | |
349 | | int |
350 | | ogg_stream_unpack_page (SF_PRIVATE *psf, OGG_PRIVATE *odata) |
351 | | { int nn ; |
352 | | int i ; |
353 | | int found_hole = 0 ; |
354 | | ogg_packet *ppkt = odata->pkt ; |
355 | | |
356 | | odata->pkt_indx = 0 ; |
357 | | nn = ogg_stream_packetout (&odata->ostream, ppkt) ; |
358 | | if (nn == 0) |
359 | | { /* |
360 | | ** Steam is out of packets. Read in more pages until there is one, or |
361 | | ** the stream ends, or an error occurs. |
362 | | */ |
363 | | for ( ; nn == 0 ; nn = ogg_stream_packetout (&odata->ostream, ppkt)) |
364 | | { nn = ogg_stream_next_page (psf, odata) ; |
365 | | if (nn <= 0) |
366 | | { odata->pkt_len = 0 ; |
367 | | return nn ; |
368 | | } |
369 | | } |
370 | | /* |
371 | | ** In the case of the for loop exiting because |
372 | | ** ogg_stream_packetout() == -1, fall-through. |
373 | | */ |
374 | | } |
375 | | |
376 | | if (nn == -1) |
377 | | { /* |
378 | | ** libOgg found a hole. That is, the next packet found was out of |
379 | | ** sequence. As such, "flush" the hole marker by removing the invalid |
380 | | ** packet, as the valid packets are queued behind it. |
381 | | */ |
382 | | psf_log_printf (psf, "Ogg : Warning, libogg reports a hole at %d bytes.\n", ogg_sync_ftell (psf)) ; |
383 | | nn = ogg_stream_packetout (&odata->ostream, ppkt) ; |
384 | | found_hole = 1 ; |
385 | | } |
386 | | |
387 | | /* |
388 | | ** Unpack all the packets on the page. It is undocumented (like much of |
389 | | ** libOgg behavior) but all packets from a page read into the stream are |
390 | | ** guaranteed to remain valid in memory until a new page is read into the |
391 | | ** stream. |
392 | | */ |
393 | | for (i = 1 ; ; i++) |
394 | | { /* Not an off-by-one, there are 255 not 256 packets max. */ |
395 | | if (i == 255) |
396 | | { if (ogg_stream_packetpeek (&odata->ostream, NULL) == 1) |
397 | | { psf->error = SFE_INTERNAL ; |
398 | | return -1 ; |
399 | | } |
400 | | break ; |
401 | | } |
402 | | if (ogg_stream_packetout (&odata->ostream, ++ ppkt) != 1) |
403 | | break ; |
404 | | } |
405 | | odata->pkt_len = i ; |
406 | | |
407 | | /* 1 = ok, 2 = ok, and found a hole. */ |
408 | | return 1 + found_hole ; |
409 | | } /* ogg_stream_unpack_page */ |
410 | | |
411 | | sf_count_t |
412 | | ogg_sync_last_page_before (SF_PRIVATE *psf, OGG_PRIVATE *odata, uint64_t *gp_out, sf_count_t offset, int32_t serialno) |
413 | | { sf_count_t begin, end, original_end, chunk_size, ret ; |
414 | | sf_count_t position = 0 ; |
415 | | uint64_t gp = -1 ; |
416 | | int left_link ; |
417 | | |
418 | | /* Based on code from Xiph.org's Opusfile */ |
419 | | |
420 | | original_end = end = begin = offset ; |
421 | | offset = -1 ; |
422 | | chunk_size = OGG_CHUNK_SIZE ; |
423 | | do |
424 | | { begin = SF_MAX (begin - chunk_size, (sf_count_t) 0) ; |
425 | | position = ogg_sync_fseek (psf, begin, SEEK_SET) ; |
426 | | if (position < 0) |
427 | | return position ; |
428 | | left_link = 0 ; |
429 | | while (position < end) |
430 | | { ret = ogg_sync_next_page (psf, &odata->opage, end - position, &position) ; |
431 | | if (ret < 0) |
432 | | return -1 ; |
433 | | else if (ret == 0) |
434 | | { // Hit EOF before EOS |
435 | | break ; |
436 | | } |
437 | | if (ogg_page_serialno (&odata->opage) == serialno) |
438 | | { uint64_t page_gp = ogg_page_granulepos (&odata->opage) ; |
439 | | if (page_gp != (uint64_t) -1) |
440 | | { offset = position ; |
441 | | gp = page_gp ; |
442 | | } |
443 | | } |
444 | | else |
445 | | left_link = 1 ; |
446 | | position += ret ; |
447 | | } |
448 | | |
449 | | if ((left_link || !begin) && offset < 0) |
450 | | { psf->error = SFE_MALFORMED_FILE ; |
451 | | return -1 ; |
452 | | } |
453 | | |
454 | | chunk_size = SF_MIN (2 * chunk_size, (sf_count_t) OGG_CHUNK_SIZE_MAX) ; |
455 | | end = SF_MIN (begin + OGG_PAGE_SIZE_MAX - 1, original_end) ; |
456 | | } |
457 | | while (offset < 0) ; |
458 | | |
459 | | *gp_out = gp ; |
460 | | return offset ; |
461 | | } /* ogg_sync_last_page_before */ |
462 | | |
463 | | int |
464 | | ogg_stream_seek_page_search (SF_PRIVATE *psf, OGG_PRIVATE *odata, |
465 | | uint64_t target_gp, uint64_t pcm_start, uint64_t pcm_end, uint64_t *best_gp, |
466 | | sf_count_t begin, sf_count_t end, uint64_t gp_rate) |
467 | | { ogg_page page ; |
468 | | uint64_t gp ; |
469 | | sf_count_t d0, d1, d2 ; |
470 | | sf_count_t best ; |
471 | | sf_count_t best_start ; |
472 | | sf_count_t boundary ; |
473 | | sf_count_t next_boundary ; |
474 | | sf_count_t page_offset = -1 ; |
475 | | sf_count_t seek_pos = -1 ; |
476 | | sf_count_t bisect ; |
477 | | sf_count_t chunk_size ; |
478 | | int buffering = SF_FALSE ; |
479 | | int force_bisect = SF_FALSE ; |
480 | | int ret ; |
481 | | int has_packets ; |
482 | | |
483 | | *best_gp = pcm_start ; |
484 | | best = best_start = begin ; |
485 | | boundary = end ; |
486 | | |
487 | | ogg_stream_reset_serialno (&odata->ostream, odata->ostream.serialno) ; |
488 | | |
489 | | /* |
490 | | ** This code is based on op_pcm_seek_page() from Opusfile, which is in turn |
491 | | ** based on "new search algorithm by Nicholas Vinen" from libvorbisfile. |
492 | | */ |
493 | | |
494 | | d2 = d1 = d0 = end - begin ; |
495 | | while (begin < end) |
496 | | { /* |
497 | | ** Figure out if and where to try and seek in the file. |
498 | | */ |
499 | | if (end - begin < OGG_CHUNK_SIZE) |
500 | | bisect = begin ; |
501 | | else |
502 | | { /* Update the interval size history */ |
503 | | d0 = d1 >> 1 ; |
504 | | d1 = d2 >> 1 ; |
505 | | d2 = (end - begin) >> 1 ; |
506 | | if (force_bisect == SF_TRUE) |
507 | | bisect = begin + ((end - begin) >> 1) ; |
508 | | else |
509 | | { /* Take a decent guess. */ |
510 | | bisect = begin + ogg_page_search_do_rescale (target_gp - pcm_start, pcm_end - pcm_start, end - begin) ; |
511 | | } |
512 | | if (bisect - OGG_CHUNK_SIZE < begin) |
513 | | bisect = begin ; |
514 | | else |
515 | | bisect -= OGG_CHUNK_SIZE ; |
516 | | force_bisect = SF_FALSE ; |
517 | | } |
518 | | |
519 | | /* |
520 | | ** Avoid an actual fseek if we can (common for final iterations.) |
521 | | */ |
522 | | if (seek_pos != bisect) |
523 | | { if (buffering == SF_TRUE) |
524 | | ogg_stream_reset (&odata->ostream) ; |
525 | | buffering = SF_FALSE ; |
526 | | page_offset = -1 ; |
527 | | seek_pos = ogg_sync_fseek (psf, bisect, SEEK_SET) ; |
528 | | if (seek_pos < 0) |
529 | | return seek_pos ; |
530 | | } |
531 | | |
532 | | chunk_size = OGG_CHUNK_SIZE ; |
533 | | next_boundary = boundary ; |
534 | | |
535 | | /* |
536 | | ** Scan forward, figure out where we landed. |
537 | | ** The ideal case is we see a page that ends before our target followed |
538 | | ** by a page that ends after our target. |
539 | | ** If we are too far before or after, breaking out will bisect what we |
540 | | ** have found so far. |
541 | | */ |
542 | | while (begin < end) |
543 | | { ret = ogg_sync_next_page (psf, &page, boundary - seek_pos, &seek_pos) ; |
544 | | if (ret <= 0) |
545 | | return ret ; |
546 | | page_offset = seek_pos ; |
547 | | if (ret == 0) |
548 | | { /* |
549 | | ** There are no more pages in this interval from our stream |
550 | | ** with a granulepos less than our target. |
551 | | */ |
552 | | if (bisect <= begin + 1) |
553 | | { /* Scanned the whole interval, so we are done. */ |
554 | | end = begin ; |
555 | | } |
556 | | else |
557 | | { /* |
558 | | ** Otherwise, back up one chunk. First discard any data |
559 | | ** from a continued packet. |
560 | | */ |
561 | | if (buffering) |
562 | | ogg_stream_reset (&odata->ostream) ; |
563 | | buffering = SF_FALSE ; |
564 | | bisect = SF_MAX (bisect - chunk_size, begin) ; |
565 | | seek_pos = ogg_sync_fseek (psf, bisect, SEEK_SET) ; |
566 | | if (seek_pos < 0) |
567 | | return seek_pos ; |
568 | | /* Bump up the chunk size. */ |
569 | | chunk_size = SF_MIN (2 * chunk_size, (sf_count_t) OGG_CHUNK_SIZE_MAX) ; |
570 | | /* |
571 | | ** If we did find a page from another stream or without a |
572 | | ** timestamp, don't read past it. |
573 | | */ |
574 | | boundary = next_boundary ; |
575 | | } |
576 | | continue ; |
577 | | } |
578 | | |
579 | | /* Found a page. Advance seek_pos past it */ |
580 | | seek_pos += page.header_len + page.body_len ; |
581 | | /* |
582 | | ** Save the offset of the first page we found after the seek, |
583 | | ** regardless of the stream it came from or whether or not it has a |
584 | | ** timestamp. |
585 | | */ |
586 | | next_boundary = SF_MIN (page_offset, next_boundary) ; |
587 | | |
588 | | /* If not from our stream, continue. */ |
589 | | if (odata->ostream.serialno != ogg_page_serialno (&page)) |
590 | | continue ; |
591 | | |
592 | | /* |
593 | | ** The Ogg spec says that a page with a granule pos of -1 must not |
594 | | ** contain and packets which complete, but the lack of biconditional |
595 | | ** wording means that /technically/ a packet which does not complete |
596 | | ** any packets can have a granule pos other than -1. To make matters |
597 | | ** worse, older versions of libogg did just that. |
598 | | */ |
599 | | has_packets = ogg_page_packets (&page) > 0 ; |
600 | | gp = has_packets ? ogg_page_granulepos (&page) : -1 ; |
601 | | if (gp == (uint64_t) -1) |
602 | | { if (buffering == SF_TRUE) |
603 | | { if (!has_packets) |
604 | | ogg_stream_pagein (&odata->ostream, &page) ; |
605 | | else |
606 | | { /* |
607 | | ** If packets did end on this page, but we still didn't |
608 | | ** have a valid granule position (in violation of the |
609 | | ** spec!), stop buffering continued packet data. |
610 | | ** Otherwise we might continue past the packet we |
611 | | ** actually wanted. |
612 | | */ |
613 | | ogg_stream_reset (&odata->ostream) ; |
614 | | buffering = SF_FALSE ; |
615 | | } |
616 | | } |
617 | | continue ; |
618 | | } |
619 | | |
620 | | if (gp < target_gp) |
621 | | { /* |
622 | | ** We found a page that ends before our target. Advance to |
623 | | ** the raw offset of the next page. |
624 | | */ |
625 | | begin = seek_pos ; |
626 | | if (pcm_start > gp || pcm_end < gp) |
627 | | break ; |
628 | | /* Save the byte offset of after this page. */ |
629 | | best = best_start = begin ; |
630 | | if (buffering) |
631 | | ogg_stream_reset (&odata->ostream) ; |
632 | | /* Check to see if the last packet continues. */ |
633 | | if (ogg_page_continues (&page)) |
634 | | { ogg_page_search_continued_data (odata, &page) ; |
635 | | /* |
636 | | ** If we have a continued packet, remember the offset of |
637 | | ** this page's start, so that if we do wind up having to |
638 | | ** seek back here later, we can prime the stream with the |
639 | | ** continued packet data. With no continued packet, we |
640 | | ** remember the end of the page. |
641 | | */ |
642 | | best_start = page_offset ; |
643 | | /* |
644 | | ** Then force buffering on, so that if a packet starts (but |
645 | | ** does not end) on the next page, we still avoid the extra |
646 | | ** seek back. |
647 | | */ |
648 | | buffering = SF_TRUE ; |
649 | | } ; |
650 | | *best_gp = pcm_start = gp ; |
651 | | if (target_gp - gp > gp_rate) |
652 | | { /* Out by over a second. Try another bisection. */ |
653 | | break ; |
654 | | } |
655 | | /* Otherwise, keep scanning forward (do NOT use begin+1). */ |
656 | | bisect = begin ; |
657 | | } |
658 | | else |
659 | | { /* |
660 | | ** Found a page that ends after our target. If we had just |
661 | | ** scanned the whole interval before we found it, we're good. |
662 | | */ |
663 | | if (bisect <= begin + 1) |
664 | | end = begin ; |
665 | | else |
666 | | { end = bisect ; |
667 | | /* |
668 | | ** In later iterations, don't read past the first page we |
669 | | ** found. |
670 | | */ |
671 | | boundary = next_boundary ; |
672 | | /* |
673 | | ** If we're not making much progress shrinking the interval |
674 | | ** size, start forcing straight bisection to limit the |
675 | | ** worst case. |
676 | | */ |
677 | | force_bisect = end - begin > d0 * 2 ? SF_TRUE : SF_FALSE ; |
678 | | /* |
679 | | ** Don't let pcm_end get out of range! That could happen |
680 | | ** with an invalid timestamp. |
681 | | */ |
682 | | if (pcm_end > gp && pcm_start <= gp) |
683 | | pcm_end = gp ; |
684 | | } |
685 | | break ; |
686 | | } |
687 | | } |
688 | | } |
689 | | |
690 | | /* |
691 | | ** If we are buffering, the page we want is currently buffered in the |
692 | | ** Ogg stream structure, or in the Ogg page which has not been submitted. |
693 | | ** If not, we need to seek back and load it again. |
694 | | */ |
695 | | if (buffering == SF_FALSE) |
696 | | { if (best_start != page_offset) |
697 | | { page_offset = -1 ; |
698 | | seek_pos = ogg_sync_fseek (psf, best_start, SEEK_SET) ; |
699 | | if (seek_pos < 0) |
700 | | return seek_pos ; |
701 | | } |
702 | | if (best_start < best) |
703 | | { if (page_offset < 0) |
704 | | { ret = ogg_sync_next_page (psf, &page, -1, &seek_pos) ; |
705 | | if (seek_pos != best_start) |
706 | | return -1 ; |
707 | | } |
708 | | ogg_page_search_continued_data (odata, &page) ; |
709 | | page_offset = -1 ; |
710 | | } |
711 | | } ; |
712 | | |
713 | | if (page_offset >= 0) |
714 | | ogg_stream_pagein (&odata->ostream, &page) ; |
715 | | |
716 | | return 0 ; |
717 | | } /* ogg_stream_seek_page_search */ |
718 | | |
719 | | int |
720 | | ogg_open (SF_PRIVATE *psf) |
721 | | { OGG_PRIVATE* odata = calloc (1, sizeof (OGG_PRIVATE)) ; |
722 | | sf_count_t pos = psf_ftell (psf) ; |
723 | | int error = 0 ; |
724 | | |
725 | | psf->container_data = odata ; |
726 | | psf->container_close = ogg_close ; |
727 | | |
728 | | if (psf->file.mode == SFM_RDWR) |
729 | | return SFE_BAD_MODE_RW ; |
730 | | |
731 | | if (psf->file.mode == SFM_READ) |
732 | | if ((error = ogg_stream_classify (psf, odata)) != 0) |
733 | | return error ; |
734 | | |
735 | | if (SF_ENDIAN (psf->sf.format) != 0) |
736 | | return SFE_BAD_ENDIAN ; |
737 | | |
738 | | switch (psf->sf.format) |
739 | | { case SF_FORMAT_OGG | SF_FORMAT_VORBIS : |
740 | | return ogg_vorbis_open (psf) ; |
741 | | |
742 | | case SF_FORMAT_OGGFLAC : |
743 | | /* Reset everything to an initial state. */ |
744 | | ogg_sync_clear (&odata->osync) ; |
745 | | ogg_stream_clear (&odata->ostream) ; |
746 | | psf_fseek (psf, pos, SEEK_SET) ; |
747 | | free (psf->container_data) ; |
748 | | psf->container_data = NULL ; |
749 | | psf->container_close = NULL ; |
750 | | return flac_open (psf) ; |
751 | | |
752 | | case SF_FORMAT_OGG | SF_FORMAT_OPUS : |
753 | | return ogg_opus_open (psf) ; |
754 | | |
755 | | #if ENABLE_EXPERIMENTAL_CODE |
756 | | case SF_FORMAT_OGG | SF_FORMAT_SPEEX : |
757 | | return ogg_speex_open (psf) ; |
758 | | |
759 | | case SF_FORMAT_OGG | SF_FORMAT_PCM_16 : |
760 | | case SF_FORMAT_OGG | SF_FORMAT_PCM_24 : |
761 | | return ogg_pcm_open (psf) ; |
762 | | #endif |
763 | | |
764 | | default : |
765 | | break ; |
766 | | } ; |
767 | | |
768 | | psf_log_printf (psf, "%s : bad psf->sf.format 0x%x.\n", __func__, psf->sf.format) ; |
769 | | return SFE_INTERNAL ; |
770 | | } /* ogg_open */ |
771 | | |
772 | | /*============================================================================== |
773 | | ** Private functions. |
774 | | */ |
775 | | |
776 | | static int |
777 | | ogg_close (SF_PRIVATE *psf) |
778 | | { OGG_PRIVATE* odata = psf->container_data ; |
779 | | |
780 | | ogg_sync_clear (&odata->osync) ; |
781 | | ogg_stream_clear (&odata->ostream) ; |
782 | | |
783 | | return 0 ; |
784 | | } /* ogg_close */ |
785 | | |
786 | | static int |
787 | | ogg_stream_classify (SF_PRIVATE *psf, OGG_PRIVATE* odata) |
788 | | { int error ; |
789 | | |
790 | | /* Call this here so it only gets called once, so no memory is leaked. */ |
791 | | ogg_sync_init (&odata->osync) ; |
792 | | ogg_stream_init (&odata->ostream, 0) ; |
793 | | |
794 | | /* Load the first page in the physical bitstream. */ |
795 | | if ((error = ogg_read_first_page (psf, odata)) != 0) |
796 | | return error ; |
797 | | |
798 | | odata->codec = ogg_page_classify (psf, &odata->opage) ; |
799 | | |
800 | | switch (odata->codec) |
801 | | { case OGG_VORBIS : |
802 | | psf->sf.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ; |
803 | | return 0 ; |
804 | | |
805 | | case OGG_FLAC : |
806 | | case OGG_FLAC0 : |
807 | | psf->sf.format = SF_FORMAT_OGGFLAC ; |
808 | | return 0 ; |
809 | | |
810 | | case OGG_SPEEX : |
811 | | psf->sf.format = SF_FORMAT_OGG | SF_FORMAT_SPEEX ; |
812 | | return 0 ; |
813 | | |
814 | | case OGG_OPUS : |
815 | | psf->sf.format = SF_FORMAT_OGG | SF_FORMAT_OPUS ; |
816 | | return 0 ; |
817 | | |
818 | | case OGG_PCM : |
819 | | psf_log_printf (psf, "Detected Ogg/PCM data. This is not supported yet.\n") ; |
820 | | return SFE_UNIMPLEMENTED ; |
821 | | |
822 | | default : |
823 | | break ; |
824 | | } ; |
825 | | |
826 | | psf_log_printf (psf, "This Ogg bitstream contains some unknown data type.\n") ; |
827 | | return SFE_UNIMPLEMENTED ; |
828 | | } /* ogg_stream_classify */ |
829 | | |
830 | | /*============================================================================== |
831 | | */ |
832 | | |
833 | | static struct |
834 | | { const char *str, *name ; |
835 | | int len, codec ; |
836 | | } codec_lookup [] = |
837 | | { { "Annodex", "Annodex", 8, OGG_ANNODEX }, |
838 | | { "AnxData", "AnxData", 7, OGG_ANXDATA }, |
839 | | { "\177FLAC", "Flac1", 5, OGG_FLAC }, |
840 | | { "fLaC", "Flac0", 4, OGG_FLAC0 }, |
841 | | { "PCM ", "PCM", 8, OGG_PCM }, |
842 | | { "Speex", "Speex", 5, OGG_SPEEX }, |
843 | | { "\001vorbis", "Vorbis", 7, OGG_VORBIS }, |
844 | | { "OpusHead", "Opus", 8, OGG_OPUS }, |
845 | | } ; |
846 | | |
847 | | static int |
848 | | ogg_page_classify (SF_PRIVATE * psf, const ogg_page * og) |
849 | | { int k, len ; |
850 | | |
851 | | for (k = 0 ; k < ARRAY_LEN (codec_lookup) ; k++) |
852 | | { if (codec_lookup [k].len > og->body_len) |
853 | | continue ; |
854 | | |
855 | | if (memcmp (og->body, codec_lookup [k].str, codec_lookup [k].len) == 0) |
856 | | { psf_log_printf (psf, "Ogg stream data : %s\n", codec_lookup [k].name) ; |
857 | | psf_log_printf (psf, "Stream serialno : %u\n", (uint32_t) ogg_page_serialno (og)) ; |
858 | | return codec_lookup [k].codec ; |
859 | | } ; |
860 | | } ; |
861 | | |
862 | | len = og->body_len < 8 ? og->body_len : 8 ; |
863 | | |
864 | | psf_log_printf (psf, "Ogg_stream data : '") ; |
865 | | for (k = 0 ; k < len ; k++) |
866 | | psf_log_printf (psf, "%c", isprint (og->body [k]) ? og->body [k] : '.') ; |
867 | | psf_log_printf (psf, "' ") ; |
868 | | for (k = 0 ; k < len ; k++) |
869 | | psf_log_printf (psf, " %02x", og->body [k] & 0xff) ; |
870 | | psf_log_printf (psf, "\n") ; |
871 | | |
872 | | return 0 ; |
873 | | } /* ogg_page_classify */ |
874 | | |
875 | | /* |
876 | | ** Scale x from the range [0, from] to the range [0, to] |
877 | | */ |
878 | | static uint64_t |
879 | | ogg_page_search_do_rescale (uint64_t x, uint64_t from, uint64_t to) |
880 | | { uint64_t frac ; |
881 | | uint64_t ret ; |
882 | | int i ; |
883 | | |
884 | | /* I should have paid more attention in CSc 349A: Numerical Analysis */ |
885 | | if (x >= from) |
886 | | return to ; |
887 | | if (x == 0) |
888 | | return 0 ; |
889 | | frac = 0 ; |
890 | | for (i = 0 ; i < 63 ; i++) |
891 | | { frac <<= 1 ; |
892 | | if (x >= from >> 1) |
893 | | { x -= from - x ; |
894 | | frac |= 1 ; |
895 | | } |
896 | | else |
897 | | x <<= 1 ; |
898 | | } |
899 | | ret = 0 ; |
900 | | for (i = 0 ; i < 63 ; i++) |
901 | | { if (frac & 1) |
902 | | ret = (ret & to & 1) + (ret >> 1) + (to >> 1) ; |
903 | | else |
904 | | ret >>= 1 ; |
905 | | frac >>= 1 ; |
906 | | } |
907 | | return ret ; |
908 | | } /* ogg_page_search_do_rescale */ |
909 | | |
910 | | static void |
911 | | ogg_page_search_continued_data (OGG_PRIVATE *odata, ogg_page *page) |
912 | | { ogg_stream_pagein (&odata->ostream, page) ; |
913 | | while (ogg_stream_packetout (&odata->ostream, &odata->opacket)) ; |
914 | | } /* ogg_page_search_continued_data */ |
915 | | |
916 | | #else /* HAVE_EXTERNAL_XIPH_LIBS */ |
917 | | |
918 | | int |
919 | | ogg_open (SF_PRIVATE *psf) |
920 | 1 | { |
921 | 1 | psf_log_printf (psf, "This version of libsndfile was compiled without Ogg/Vorbis support.\n") ; |
922 | 1 | return SFE_UNIMPLEMENTED ; |
923 | 1 | } /* ogg_open */ |
924 | | |
925 | | #endif |