Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / media / liboggz / include / oggz / oggz_seek.h
blobbc3c5485532be38fe963d883edcf4b0e092cf3e8
1 /*
2 Copyright (C) 2003 Commonwealth Scientific and Industrial Research
3 Organisation (CSIRO) Australia
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 - Neither the name of CSIRO Australia nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #ifndef __OGGZ_SEEK_H__
34 #define __OGGZ_SEEK_H__
36 /** \file
37 * Seeking within files
40 /** \defgroup seek_api Oggz Seek API
42 * Oggz can seek on multitrack, multicodec bitstreams.
44 * \section seek_time Time seeking
46 * Support is built-in for seeking to time positions in
47 * <a href="http://www.celt-codec.org/">CELT</a>,
48 * <a href="http://www.annodex.net/">CMML</a>.
49 * <a href="http://flac.sourceforge.net/">FLAC</a>,
50 * <a href="http://wiki.xiph.org/OggPCM">OggPCM</a>,
51 * <a href="http://www.speex.org/">Speex</a>,
52 * <a href="http://www.theora.org/">Theora</a>
53 * and <a href="http://www.vorbis.com/">Vorbis</a>.
54 * Oggz is also compatible with
55 * <a href="http://www.annodex.net/">Annodex</a> streams, and supports seeking
56 * on all tracks described in an
57 * <a href="http://wiki.xiph.org/OggSkeleton">Ogg Skeleton</a> track.
59 * You need to open the file with the OGGZ_AUTO flag set:
61 * - Create an OGGZ handle for reading with \a flags = OGGZ_READ | OGGZ_AUTO
62 * - Read data, ensuring that you have received all b_o_s pages before
63 * attempting to seek.
65 * Oggz will silently parse known codec headers and associate metrics
66 * appropriately; if you attempt to seek before you have received all
67 * b_o_s pages, Oggz will not have had a chance to parse the codec headers
68 * and associate metrics.
69 * It is safe to seek once you have received a packet with \a b_o_s == 0;
70 * see the \link basics Ogg basics \endlink section for more details.
72 * \note Oggz parses these codec headers internally, and so liboggz is \b not
73 * linked to libspeex, libvorbis, libflac, libtheora, libcmml or libannodex.
75 * For other data streams, you will need to provide a metric function;
76 * see the section on \link metric Using OggzMetrics \endlink for details
77 * of setting up and seeking with metrics.
79 * \section seek_bytes Byte seeking
81 * oggz_seek() provides low-level seeking to byte positions.
83 * \section seek_info More detail
85 * For a full description of the seeking methods possible in Ogg, see
86 * \link seek_semantics Semantics of seeking in Ogg bitstreams \endlink.
88 * \{
91 /**
92 * Query the current offset in milliseconds, or custom units as
93 * specified by a Metric function you have provided.
94 * \param oggz An OGGZ handle
95 * \returns the offset in milliseconds, or custom units
96 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
97 * \retval OGGZ_ERR_INVALID Operation not suitable for this OGGZ
99 ogg_int64_t oggz_tell_units (OGGZ * oggz);
102 * Seek to an offset in milliseconds, or custom units as specified
103 * by a Metric function you have provided.
104 * \param oggz An OGGZ handle
105 * \param units A number of milliseconds, or custom units
106 * \param whence As defined in <stdio.h>: SEEK_SET, SEEK_CUR or SEEK_END
107 * \returns the new file offset, or -1 on failure.
109 ogg_int64_t oggz_seek_units (OGGZ * oggz, ogg_int64_t units, int whence);
112 * Provide the exact stored granulepos (from the page header) if relevant to
113 * the current packet, or a constructed granulepos if the stored granulepos
114 * does not belong to this packet, or -1 if this codec does not have support
115 * for granulepos interpolation
116 * \param oggz An OGGZ handle
117 * \returns the granulepos of the \a current packet (if available)
119 ogg_int64_t
120 oggz_tell_granulepos (OGGZ * oggz);
123 * Query the file offset in bytes corresponding to the data read.
124 * \param oggz An OGGZ handle
125 * \returns The current offset of oggz.
127 * \note When reading, the value returned by oggz_tell() reflects the
128 * data offset of the start of the most recent packet processed, so that
129 * when called from an OggzReadPacket callback it reflects the byte
130 * offset of the start of the packet. As Oggz may have internally read
131 * ahead, this may differ from the current offset of the associated file
132 * descriptor.
134 oggz_off_t oggz_tell (OGGZ * oggz);
137 * Seek to a specific byte offset
138 * \param oggz An OGGZ handle
139 * \param offset a byte offset
140 * \param whence As defined in <stdio.h>: SEEK_SET, SEEK_CUR or SEEK_END
141 * \returns the new file offset, or -1 on failure.
143 oggz_off_t oggz_seek (OGGZ * oggz, oggz_off_t offset, int whence);
145 #ifdef _UNIMPLEMENTED
146 long oggz_seek_packets (OGGZ * oggz, long serialno, long packets, int whence);
147 #endif
149 /** \}
152 /** \defgroup seek_semantics Semantics of seeking in Ogg bitstreams
154 * \section seek_semantics_intro Introduction
156 * [*** This line works around a bug in doxygen ***]
158 * [*** This line works around a bug in doxygen ***]
160 * The seeking semantics of the Ogg file format were outlined by Monty in
161 * <a href="http://web.archive.org/web/20031201054855/http://www.xiph.org/archives/theora-dev/200209/0040.html"> a
162 * post to theora-dev</a> in September 2002. Quoting from that post, we
163 * have the following assumptions:
165 * - Ogg is not a non-linear format. ... It is a media transport format
166 * designed to do nothing more than deliver content, in a stream, and
167 * have all the pieces arrive on time and in sync.
168 * - The Ogg layer does not know the specifics of the codec data it's
169 * multiplexing into a stream. It knows nothing beyond 'Oooo, packets!',
170 * that the packets belong to different buckets, that the packets go in
171 * order, and that packets have position markers. Ogg does not even have
172 * a concept of 'time'; it only knows about the sequentially increasing,
173 * unitless position markers. It is up to higher layers which have
174 * access to the codec APIs to assign and convert units of framing or
175 * time.
177 * (For more details on the structure of Ogg streams, see the
178 * \link basics Ogg Basics \endlink section).
180 * For data such as media, for which it is possible to provide a mapping
181 * such as 'time', Oggz can efficiently navigate through an Ogg stream
182 * by use of an OggzMetric callback, thus allowing automatic seeking to
183 * points in 'time'.
185 * For common codecs you can ask Oggz to set this for you automatically by
186 * instantiating the OGGZ handle with the OGGZ_AUTO flag set. For others
187 * you can specify a multiplier with oggz_set_metric_linear(), or a generic
188 * non-linear metric with oggz_set_metric().
192 /** \defgroup metric Using OggzMetric
194 * \section metric_intro Introduction
196 * An OggzMetric is a helper function for Oggz's seeking mechanism.
198 * If every position in an Ogg stream can be described by a metric such as
199 * time, then it is possible to define a function that, given a serialno and
200 * granulepos, returns a measurement in units such as milliseconds. Oggz
201 * will use this function repeatedly while seeking in order to navigate
202 * through the Ogg stream.
204 * The meaning of the units is arbitrary, but must be consistent across all
205 * logical bitstreams. This allows Oggz to seek accurately through Ogg
206 * bitstreams containing multiple logical bitstreams such as tracks of media.
208 * \section setting How to set metrics
210 * You don't need to set metrics for Speex, Vorbis, FLAC, Theora, CMML or
211 * Annodex.
212 * These can be handled \link seek_api automatically \endlink by Oggz.
214 * For most others it is simply a matter of providing a "granulerate":
215 * a frame or sampling rate, if each packet's granulepos represents a
216 * sample number.
218 * - Set the \a granule_rate_numerator and \a granule_rate_denominator
219 * appropriately using oggz_set_granulerate()
221 * Some codecs use a "granuleshift" to divide a granulepos into two halves;
222 * the first describing a dependency on a previous packet, the second
223 * giving the offset since that packet. This is used to mark keyframes and
224 * intermediate frames.
226 * - Set the \a granuleshift appropriately using oggz_set_granuleshift()
228 * \subsection custom Custom Metrics
230 * For streams with non-linear granulepos, you need to set a custom metric:
232 * - Implement an OggzMetric callback
233 * - Set the OggzMetric callback using oggz_set_metric()
235 * \section using Seeking with OggzMetrics
237 * To seek, use oggz_seek_units(). Oggz will perform a ratio search
238 * through the Ogg bitstream, using the OggzMetric callback to determine
239 * its position relative to the desired unit.
241 * \note
243 * Many data streams begin with headers describing such things as codec
244 * setup parameters. One of the assumptions Monty makes is:
246 * - Given pre-cached decode headers, a player may seek into a stream at
247 * any point and begin decode.
249 * Thus, the first action taken by applications dealing with such data is
250 * to read in and cache the decode headers; thereafter the application can
251 * safely seek to arbitrary points in the data.
253 * This impacts seeking because the portion of the bitstream containing
254 * decode headers should not be considered part of the metric space. To
255 * inform Oggz not to seek earlier than the end of the decode headers,
256 * use oggz_set_data_start().
258 * \{
262 * Retrieve the granuleshift of a logical bitstream.
263 * \param oggz An OGGZ handle
264 * \param serialno Identify the logical bitstream in \a oggz
265 * \returns The granuleshift of the specified logical bitstream.
266 * \retval OGGZ_ERR_BAD_SERIALNO \a serialno does not identify an existing
267 * logical bitstream in \a oggz.
268 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
270 int oggz_get_granuleshift (OGGZ * oggz, long serialno);
273 * Specify the granuleshift of a logical bitstream.
274 * \param oggz An OGGZ handle
275 * \param serialno Identify the logical bitstream in \a oggz to attach
276 * this granuleshift metric to. A value of -1 indicates that the metric should
277 * be attached to all unattached logical bitstreams in \a oggz.
278 * \param granuleshift The granuleshift
279 * \returns 0 Success
280 * \retval OGGZ_ERR_BAD_SERIALNO \a serialno does not identify an existing
281 * logical bitstream in \a oggz.
282 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
284 int oggz_set_granuleshift (OGGZ * oggz, long serialno, int granuleshift);
287 * Retrieve the granulerate of a logical bitstream.
288 * \param oggz An OGGZ handle
289 * \param serialno Identify the logical bitstream in \a oggz
290 * \param granulerate_n Return location for the granulerate numerator
291 * \param granulerate_d Return location for the granulerate denominator
292 * \returns 0 Success
293 * \retval OGGZ_ERR_BAD_SERIALNO \a serialno does not identify an existing
294 * logical bitstream in \a oggz.
295 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
298 int oggz_get_granulerate (OGGZ * oggz, long serialno,
299 ogg_int64_t * granulerate_n,
300 ogg_int64_t * granulerate_d);
303 * Specify the granulerate of a logical bitstream.
304 * \param oggz An OGGZ handle
305 * \param serialno Identify the logical bitstream in \a oggz to attach
306 * this linear metric to. A value of -1 indicates that the metric should
307 * be attached to all unattached logical bitstreams in \a oggz.
308 * \param granule_rate_numerator The numerator of the granule rate
309 * \param granule_rate_denominator The denominator of the granule rate
310 * \returns 0 Success
311 * \retval OGGZ_ERR_BAD_SERIALNO \a serialno does not identify an existing
312 * logical bitstream in \a oggz.
313 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
315 int oggz_set_granulerate (OGGZ * oggz, long serialno,
316 ogg_int64_t granule_rate_numerator,
317 ogg_int64_t granule_rate_denominator);
320 * This is the signature of a function to correlate Ogg streams.
321 * If every position in an Ogg stream can be described by a metric (eg. time)
322 * then define this function that returns some arbitrary unit value.
323 * This is the normal use of Oggz for media streams. The meaning of units is
324 * arbitrary, but must be consistent across all logical bitstreams; for
325 * example a conversion of the time offset of a given packet into nanoseconds
326 * or a similar stream-specific subdivision may be appropriate.
328 * \param oggz An OGGZ handle
329 * \param serialno Identifies a logical bitstream within \a oggz
330 * \param granulepos A granulepos within the logical bitstream identified
331 * by \a serialno
332 * \param user_data Arbitrary data you wish to pass to your callback
333 * \returns A conversion of the (serialno, granulepos) pair into a measure
334 * in units which is consistent across all logical bitstreams within \a oggz
336 typedef ogg_int64_t (*OggzMetric) (OGGZ * oggz, long serialno,
337 ogg_int64_t granulepos, void * user_data);
340 * Set the OggzMetric to use for an OGGZ handle
342 * \param oggz An OGGZ handle
343 * \param serialno Identify the logical bitstream in \a oggz to attach
344 * this metric to. A value of -1 indicates that this metric
345 * should be attached to all unattached logical bitstreams
346 * in \a oggz.
347 * \param metric An OggzMetric callback
348 * \param user_data arbitrary data to pass to the metric callback
350 * \returns 0 Success
351 * \retval OGGZ_ERR_BAD_SERIALNO \a serialno does not identify an existing
352 * logical bitstream in \a oggz, and is not -1
353 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
355 * \note Specifying values of \a serialno other than -1 allows you to pass
356 * logical bitstream specific user_data to the same metric.
357 * \note Alternatively, you may use a different \a metric for each
358 * \a serialno, but all metrics used must return mutually consistent
359 * unit measurements.
361 int oggz_set_metric (OGGZ * oggz, long serialno, OggzMetric metric,
362 void * user_data);
364 #ifdef _UNIMPLEMENTED
365 /** \defgroup order OggzOrder
367 * - A mechanism to aid seeking across non-metric spaces for which a partial
368 * order exists (ie. data that is not synchronised by a measure such as time,
369 * but is nevertheless somehow seekably structured), is also planned.
371 * \subsection OggzOrder
373 * Suppose there is a partial order < and a corresponding equivalence
374 * relation = defined on the space of packets in the Ogg stream of 'OGGZ'.
375 * An OggzOrder simply provides a comparison in terms of '<' and '=' for
376 * ogg_packets against a target.
378 * To use OggzOrder:
380 * - Implement an OggzOrder callback
381 * - Set the OggzOrder callback for an OGGZ handle with oggz_set_order()
382 * - To seek, use oggz_seek_byorder(). Oggz will use a combination bisection
383 * search and scan of the Ogg bitstream, using the OggzOrder callback to
384 * match against the desired 'target'.
386 * Otherwise, for more general ogg streams for which a partial order can be
387 * defined, define a function matching this specification.
389 * Parameters:
391 * OGGZ: the OGGZ object
392 * op: an ogg packet in the stream
393 * target: a user defined object
395 * Return values:
397 * -1 , if 'op' would occur before the position represented by 'target'
398 * 0 , if the position of 'op' is equivalent to that of 'target'
399 * 1 , if 'op' would occur after the position represented by 'target'
400 * 2 , if the relationship between 'op' and 'target' is undefined.
402 * Symbolically:
404 * Suppose there is a partial order < and a corresponding equivalence
405 * relation = defined on the space of packets in the Ogg stream of 'OGGZ'.
406 * Let p represent the position of the packet 'op', and t be the position
407 * represented by 'target'.
409 * Then a function implementing OggzPacketOrder should return as follows:
411 * -1 , p < t
412 * 0 , p = t
413 * 1 , t < p
414 * 2 , otherwise
416 * Hacker's hint: if there are no circumstances in which you would return
417 * a value of 2, there is a linear order; it may be possible to define a
418 * Metric rather than an Order.
421 typedef int (*OggzOrder) (OGGZ * oggz, ogg_packet * op, void * target,
422 void * user_data);
424 * \retval 0 Success
425 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
426 * \retval OGGZ_ERR_INVALID Operation not suitable for this OGGZ
428 int oggz_set_order (OGGZ * oggz, long serialno, OggzOrder order,
429 void * user_data);
431 long oggz_seek_byorder (OGGZ * oggz, void * target);
433 #endif /* _UNIMPLEMENTED */
436 * Tell Oggz to remember the given offset as the start of data.
437 * This informs the seeking mechanism that when seeking back to unit 0,
438 * go to the given offset, not to the start of the file, which is usually
439 * codec headers.
440 * The usual usage is:
441 <pre>
442 oggz_set_data_start (oggz, oggz_tell (oggz));
443 </pre>
444 * \param oggz An OGGZ handle previously opened for reading
445 * \param offset The offset of the start of data
446 * \returns 0 on success, -1 on failure.
448 int oggz_set_data_start (OGGZ * oggz, oggz_off_t offset);
449 /** \}
452 #endif /* __OGGZ_SEEK_H__ */