~ubuntu-branches/ubuntu/precise/liboggz/precise

« back to all changes in this revision

Viewing changes to include/oggz/oggz_seek.h

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Wilkinson
  • Date: 2005-04-16 01:19:44 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050416011944-5ipwrrc260ihkpp8
Tags: 0.9.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2003 Commonwealth Scientific and Industrial Research
 
3
   Organisation (CSIRO) Australia
 
4
 
 
5
   Redistribution and use in source and binary forms, with or without
 
6
   modification, are permitted provided that the following conditions
 
7
   are met:
 
8
 
 
9
   - Redistributions of source code must retain the above copyright
 
10
   notice, this list of conditions and the following disclaimer.
 
11
 
 
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.
 
15
 
 
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.
 
19
 
 
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.
 
31
*/
 
32
 
 
33
#ifndef __OGGZ_SEEK_H__
 
34
#define __OGGZ_SEEK_H__
 
35
 
 
36
/** \file
 
37
 * Seeking within files
 
38
 */
 
39
 
 
40
/** \defgroup seek_api OGGZ Seek API
 
41
 *
 
42
 * Oggz can seek on multitrack, multicodec bitstreams.
 
43
 *
 
44
 * \section seek_time Time seeking
 
45
 *
 
46
 * Support is built-in for seeking to time positions in
 
47
 * <a href="http://www.speex.org/">Speex</a>,
 
48
 * <a href="http://www.vorbis.com/">Vorbis</a>,
 
49
 * <a href="http://flac.sourceforge.net/">FLAC</a>,
 
50
 * <a href="http://www.theora.org/">Theora</a>,
 
51
 * and <a href="http://www.annodex.net/">CMML</a>.
 
52
 * Oggz is also compatible with 
 
53
 * <a href="http://www.annodex.net/">Annodex</a> streams, and supports seeking
 
54
 * on all tracks described in an Ogg Skeleton track.
 
55
 *
 
56
 * You need to open the file with the OGGZ_AUTO flag set:
 
57
 *
 
58
 * - Create an OGGZ handle for reading with \a flags = OGGZ_READ | OGGZ_AUTO
 
59
 * - Read data, ensuring that you have received all b_o_s pages before
 
60
 *   attempting to seek.
 
61
 *
 
62
 * Oggz will silently parse known codec headers and associate metrics
 
63
 * appropriately; if you attempt to seek before you have received all
 
64
 * b_o_s pages, Oggz will not have had a chance to parse the codec headers
 
65
 * and associate metrics.
 
66
 * It is safe to seek once you have received a packet with \a b_o_s == 0;
 
67
 * see the \link basics Ogg basics \endlink section for more details.
 
68
 *
 
69
 * \note Oggz parses these codec headers internally, and so liboggz is \b not
 
70
 * linked to libspeex, libvorbis, libflac, libtheora, libcmml or libannodex.
 
71
 *
 
72
 * For other data streams, you will need to provide a metric function;
 
73
 * see the section on \link metric Using OggzMetrics \endlink for details
 
74
 * of setting up and seeking with metrics.
 
75
 *
 
76
 * \section seek_bytes Byte seeking
 
77
 *
 
78
 * oggz_seek() provides low-level seeking to byte positions.
 
79
 *
 
80
 * \section seek_info More detail
 
81
 *
 
82
 * For a full description of the seeking methods possible in Ogg, see
 
83
 * \link seek_semantics Semantics of seeking in Ogg bitstreams \endlink.
 
84
 *
 
85
 * \{
 
86
 */
 
87
 
 
88
/**
 
89
 * Query the current offset in milliseconds, or custom units as
 
90
 * specified by a Metric function you have provided.
 
91
 * \param oggz An OGGZ handle
 
92
 * \returns the offset in milliseconds, or custom units
 
93
 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
 
94
 * \retval OGGZ_ERR_INVALID Operation not suitable for this OGGZ
 
95
 */
 
96
ogg_int64_t oggz_tell_units (OGGZ * oggz);
 
97
 
 
98
/**
 
99
 * Seek to an offset in milliseconds, or custom units as specified
 
100
 * by a Metric function you have provided.
 
101
 * \param oggz An OGGZ handle
 
102
 * \param units A number of milliseconds, or custom units
 
103
 * \param whence As defined in <stdio.h>: SEEK_SET, SEEK_CUR or SEEK_END
 
104
 * \returns the new file offset, or -1 on failure.
 
105
 */
 
106
ogg_int64_t oggz_seek_units (OGGZ * oggz, ogg_int64_t units, int whence);
 
107
 
 
108
/**
 
109
 * Query the file offset in bytes corresponding to the data read.
 
110
 * \param oggz An OGGZ handle
 
111
 * \returns The current offset of oggz.
 
112
 *
 
113
 * \note When reading, the value returned by oggz_tell() reflects the
 
114
 * data offset of the start of the most recent packet processed, so that
 
115
 * when called from an OggzReadPacket callback it reflects the byte
 
116
 * offset of the start of the packet. As OGGZ may have internally read
 
117
 * ahead, this may differ from the current offset of the associated file
 
118
 * descriptor.
 
119
 */
 
120
off_t oggz_tell (OGGZ * oggz);
 
121
 
 
122
/**
 
123
 * Seek to a specific byte offset
 
124
 * \param oggz An OGGZ handle
 
125
 * \param offset a byte offset
 
126
 * \param whence As defined in <stdio.h>: SEEK_SET, SEEK_CUR or SEEK_END
 
127
 * \returns the new file offset, or -1 on failure.
 
128
 */
 
129
off_t oggz_seek (OGGZ * oggz, off_t offset, int whence);
 
130
 
 
131
#ifdef _UNIMPLEMENTED
 
132
long oggz_seek_packets (OGGZ * oggz, long serialno, long packets, int whence);
 
133
#endif
 
134
 
 
135
/** \}
 
136
 */
 
137
 
 
138
/** \defgroup seek_semantics Semantics of seeking in Ogg bitstreams
 
139
 *
 
140
 * \section seek_semantics_intro Introduction
 
141
 *
 
142
 *         [*** This line works around a bug in doxygen ***]
 
143
 *
 
144
 *         [*** This line works around a bug in doxygen ***]
 
145
 *
 
146
 * The seeking semantics of the Ogg file format were outlined by Monty in
 
147
 * <a href="http://www.xiph.org/archives/theora-dev/200209/0040.html">a
 
148
 * post to theora-dev</a> in September 2002. Quoting from that post, we
 
149
 * have the following assumptions:
 
150
 *
 
151
 * - Ogg is not a non-linear format. ... It is a media transport format
 
152
 *   designed to do nothing more than deliver content, in a stream, and
 
153
 *   have all the pieces arrive on time and in sync.
 
154
 * - The Ogg layer does not know the specifics of the codec data it's
 
155
 *   multiplexing into a stream. It knows nothing beyond 'Oooo, packets!',
 
156
 *   that the packets belong to different buckets, that the packets go in
 
157
 *   order, and that packets have position markers. Ogg does not even have
 
158
 *   a concept of 'time'; it only knows about the sequentially increasing,
 
159
 *   unitless position markers. It is up to higher layers which have
 
160
 *   access to the codec APIs to assign and convert units of framing or
 
161
 *   time.
 
162
 *
 
163
 * (For more details on the structure of Ogg streams, see the
 
164
 * \link basics Ogg Basics \endlink section).
 
165
 *
 
166
 * For data such as media, for which it is possible to provide a mapping
 
167
 * such as 'time', OGGZ can efficiently navigate through an Ogg stream
 
168
 * by use of an OggzMetric callback, thus allowing automatic seeking to
 
169
 * points in 'time'.
 
170
 *
 
171
 * For common codecs you can ask Oggz to set this for you automatically by
 
172
 * instantiating the OGGZ handle with the OGGZ_AUTO flag set. For others
 
173
 * you can specify a multiplier with oggz_set_metric_linear(), or a generic
 
174
 * non-linear metric with oggz_set_metric().
 
175
 *
 
176
 */
 
177
 
 
178
/** \defgroup metric Using OggzMetric
 
179
 *
 
180
 * \section metric_intro Introduction
 
181
 *
 
182
 * An OggzMetric is a helper function for Oggz's seeking mechanism.
 
183
 *
 
184
 * If every position in an Ogg stream can be described by a metric such as
 
185
 * time, then it is possible to define a function that, given a serialno and
 
186
 * granulepos, returns a measurement in units such as milliseconds. Oggz
 
187
 * will use this function repeatedly while seeking in order to navigate
 
188
 * through the Ogg stream.
 
189
 *
 
190
 * The meaning of the units is arbitrary, but must be consistent across all
 
191
 * logical bitstreams. This allows Oggz to seek accurately through Ogg
 
192
 * bitstreams containing multiple logical bitstreams such as tracks of media.
 
193
 *
 
194
 * \section setting How to set metrics
 
195
 *
 
196
 * You don't need to set metrics for Speex, Vorbis, FLAC, Theora, CMML or
 
197
 * Annodex.
 
198
 * These can be handled \link seek_api automatically \endlink by Oggz.
 
199
 *
 
200
 * For most others it is simply a matter of providing a "granulerate":
 
201
 * a frame or sampling rate, if each packet's granulepos represents a
 
202
 * sample number.
 
203
 *
 
204
 * - Set the \a granule_rate_numerator and \a granule_rate_denominator
 
205
 *   appropriately using oggz_set_granulerate()
 
206
 *
 
207
 * Some codecs use a "granuleshift" to divide a granulepos into two halves;
 
208
 * the first describing a dependency on a previous packet, the second
 
209
 * giving the offset since that packet. This is used to mark keyframes and
 
210
 * intermediate frames.
 
211
 *
 
212
 * - Set the \a granuleshift appropriately using oggz_set_granuleshift()
 
213
 *
 
214
 * \subsection custom Custom Metrics
 
215
 *
 
216
 * For streams with non-linear granulepos, you need to set a custom metric:
 
217
 *
 
218
 * - Implement an OggzMetric callback
 
219
 * - Set the OggzMetric callback using oggz_set_metric()
 
220
 *
 
221
 * \section using Seeking with OggzMetrics
 
222
 *
 
223
 * To seek, use oggz_seek_units(). Oggz will perform a ratio search
 
224
 * through the Ogg bitstream, using the OggzMetric callback to determine
 
225
 * its position relative to the desired unit.
 
226
 *
 
227
 * \note
 
228
 *
 
229
 * Many data streams begin with headers describing such things as codec
 
230
 * setup parameters. One of the assumptions Monty makes is:
 
231
 *
 
232
 * - Given pre-cached decode headers, a player may seek into a stream at
 
233
 *   any point and begin decode.
 
234
 *
 
235
 * Thus, the first action taken by applications dealing with such data is
 
236
 * to read in and cache the decode headers; thereafter the application can
 
237
 * safely seek to arbitrary points in the data.
 
238
 *
 
239
 * This impacts seeking because the portion of the bitstream containing
 
240
 * decode headers should not be considered part of the metric space. To
 
241
 * inform Oggz not to seek earlier than the end of the decode headers,
 
242
 * use oggz_set_data_start().
 
243
 *
 
244
 * \{
 
245
 */
 
246
 
 
247
/**
 
248
 * Retrieve the granuleshift of a logical bitstream.
 
249
 * \param oggz An OGGZ handle
 
250
 * \param serialno Identify the logical bitstream in \a oggz
 
251
 * \returns The granuleshift of the specified logical bitstream.
 
252
 * \retval OGGZ_ERR_BAD_SERIALNO \a serialno does not identify an existing
 
253
 * logical bitstream in \a oggz.
 
254
 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
 
255
 */
 
256
int oggz_get_granuleshift (OGGZ * oggz, long serialno);
 
257
 
 
258
/**
 
259
 * Specify the granuleshift of a logical bitstream.
 
260
 * \param oggz An OGGZ handle
 
261
 * \param serialno Identify the logical bitstream in \a oggz to attach
 
262
 * this granuleshift metric to. A value of -1 indicates that the metric should
 
263
 * be attached to all unattached logical bitstreams in \a oggz.
 
264
 * \param granuleshift The granuleshift
 
265
 * \returns 0 Success
 
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
 
269
 */
 
270
int oggz_set_granuleshift (OGGZ * oggz, long serialno, int granuleshift);
 
271
 
 
272
/**
 
273
 * Retrieve the granulerate of a logical bitstream.
 
274
 * \param oggz An OGGZ handle
 
275
 * \param serialno Identify the logical bitstream in \a oggz
 
276
 * \param granulerate_n Return location for the granulerate numerator
 
277
 * \param granulerate_d Return location for the granulerate denominator
 
278
 * \returns 0 Success
 
279
 * \retval OGGZ_ERR_BAD_SERIALNO \a serialno does not identify an existing
 
280
 * logical bitstream in \a oggz.
 
281
 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
 
282
 *
 
283
 */
 
284
int oggz_get_granulerate (OGGZ * oggz, long serialno,
 
285
                          ogg_int64_t * granulerate_n,
 
286
                          ogg_int64_t * granulerate_d);
 
287
 
 
288
/**
 
289
 * Specify the granulerate of a logical bitstream.
 
290
 * \param oggz An OGGZ handle
 
291
 * \param serialno Identify the logical bitstream in \a oggz to attach
 
292
 * this linear metric to. A value of -1 indicates that the metric should
 
293
 * be attached to all unattached logical bitstreams in \a oggz.
 
294
 * \param granule_rate_numerator The numerator of the granule rate
 
295
 * \param granule_rate_denominator The denominator of the granule rate
 
296
 * \returns 0 Success
 
297
 * \retval OGGZ_ERR_BAD_SERIALNO \a serialno does not identify an existing
 
298
 * logical bitstream in \a oggz.
 
299
 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
 
300
 */
 
301
int oggz_set_granulerate (OGGZ * oggz, long serialno,
 
302
                          ogg_int64_t granule_rate_numerator,
 
303
                          ogg_int64_t granule_rate_denominator);
 
304
 
 
305
/**
 
306
 * This is the signature of a function to correlate Ogg streams.
 
307
 * If every position in an Ogg stream can be described by a metric (eg. time)
 
308
 * then define this function that returns some arbitrary unit value.
 
309
 * This is the normal use of OGGZ for media streams. The meaning of units is
 
310
 * arbitrary, but must be consistent across all logical bitstreams; for
 
311
 * example a conversion of the time offset of a given packet into nanoseconds
 
312
 * or a similar stream-specific subdivision may be appropriate.
 
313
 *
 
314
 * \param oggz An OGGZ handle
 
315
 * \param serialno Identifies a logical bitstream within \a oggz
 
316
 * \param granulepos A granulepos within the logical bitstream identified
 
317
 *                   by \a serialno
 
318
 * \param user_data Arbitrary data you wish to pass to your callback
 
319
 * \returns A conversion of the (serialno, granulepos) pair into a measure
 
320
 * in units which is consistent across all logical bitstreams within \a oggz
 
321
 */
 
322
typedef ogg_int64_t (*OggzMetric) (OGGZ * oggz, long serialno,
 
323
                                   ogg_int64_t granulepos, void * user_data);
 
324
 
 
325
/**
 
326
 * Set the OggzMetric to use for an OGGZ handle
 
327
 *
 
328
 * \param oggz An OGGZ handle
 
329
 * \param serialno Identify the logical bitstream in \a oggz to attach
 
330
 *                 this metric to. A value of -1 indicates that this metric
 
331
 *                 should be attached to all unattached logical bitstreams
 
332
 *                 in \a oggz.
 
333
 * \param metric An OggzMetric callback
 
334
 * \param user_data arbitrary data to pass to the metric callback
 
335
 *
 
336
 * \returns 0 Success
 
337
 * \retval OGGZ_ERR_BAD_SERIALNO \a serialno does not identify an existing
 
338
 *                               logical bitstream in \a oggz, and is not -1
 
339
 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
 
340
 *
 
341
 * \note Specifying values of \a serialno other than -1 allows you to pass
 
342
 *       logical bitstream specific user_data to the same metric.
 
343
 * \note Alternatively, you may use a different \a metric for each
 
344
 *       \a serialno, but all metrics used must return mutually consistent
 
345
 *       unit measurements.
 
346
 */
 
347
int oggz_set_metric (OGGZ * oggz, long serialno, OggzMetric metric,
 
348
                     void * user_data);
 
349
 
 
350
#ifdef _UNIMPLEMENTED
 
351
/** \defgroup order OggzOrder
 
352
 *
 
353
 * - A mechanism to aid seeking across non-metric spaces for which a partial
 
354
 *   order exists (ie. data that is not synchronised by a measure such as time,
 
355
 *   but is nevertheless somehow seekably structured), is also planned.
 
356
 *
 
357
 * \subsection OggzOrder
 
358
 *
 
359
 * Suppose there is a partial order < and a corresponding equivalence
 
360
 * relation = defined on the space of packets in the Ogg stream of 'OGGZ'.
 
361
 * An OggzOrder simply provides a comparison in terms of '<' and '=' for
 
362
 * ogg_packets against a target.
 
363
 *
 
364
 * To use OggzOrder:
 
365
 *
 
366
 * - Implement an OggzOrder callback
 
367
 * - Set the OggzOrder callback for an OGGZ handle with oggz_set_order()
 
368
 * - To seek, use oggz_seek_byorder(). Oggz will use a combination bisection
 
369
 *   search and scan of the Ogg bitstream, using the OggzOrder callback to
 
370
 *   match against the desired 'target'.
 
371
 *
 
372
 * Otherwise, for more general ogg streams for which a partial order can be
 
373
 * defined, define a function matching this specification.
 
374
 *
 
375
 * Parameters:
 
376
 *
 
377
 *     OGGZ: the OGGZ object
 
378
 *     op:  an ogg packet in the stream
 
379
 *     target: a user defined object
 
380
 *
 
381
 * Return values:
 
382
 *
 
383
 *    -1 , if 'op' would occur before the position represented by 'target'
 
384
 *     0 , if the position of 'op' is equivalent to that of 'target'
 
385
 *     1 , if 'op' would occur after the position represented by 'target'
 
386
 *     2 , if the relationship between 'op' and 'target' is undefined.
 
387
 *
 
388
 * Symbolically:
 
389
 *
 
390
 * Suppose there is a partial order < and a corresponding equivalence
 
391
 * relation = defined on the space of packets in the Ogg stream of 'OGGZ'.
 
392
 * Let p represent the position of the packet 'op', and t be the position
 
393
 * represented by 'target'.
 
394
 *
 
395
 * Then a function implementing OggzPacketOrder should return as follows:
 
396
 *
 
397
 *    -1 , p < t
 
398
 *     0 , p = t
 
399
 *     1 , t < p
 
400
 *     2 , otherwise
 
401
 *
 
402
 * Hacker's hint: if there are no circumstances in which you would return
 
403
 * a value of 2, there is a linear order; it may be possible to define a
 
404
 * Metric rather than an Order.
 
405
 *
 
406
 */
 
407
typedef int (*OggzOrder) (OGGZ * oggz, ogg_packet * op, void * target,
 
408
                         void * user_data);
 
409
/**
 
410
 * \retval 0 Success
 
411
 * \retval OGGZ_ERR_BAD_OGGZ \a oggz does not refer to an existing OGGZ
 
412
 * \retval OGGZ_ERR_INVALID Operation not suitable for this OGGZ
 
413
 */
 
414
int oggz_set_order (OGGZ * oggz, long serialno, OggzOrder order,
 
415
                    void * user_data);
 
416
 
 
417
long oggz_seek_byorder (OGGZ * oggz, void * target);
 
418
 
 
419
#endif /* _UNIMPLEMENTED */
 
420
 
 
421
/**
 
422
 * Tell OGGZ to remember the given offset as the start of data.
 
423
 * This informs the seeking mechanism that when seeking back to unit 0,
 
424
 * go to the given offset, not to the start of the file, which is usually
 
425
 * codec headers.
 
426
 * The usual usage is:
 
427
<pre>
 
428
    oggz_set_data_start (oggz, oggz_tell (oggz));
 
429
</pre>
 
430
 * \param oggz An OGGZ handle previously opened for reading
 
431
 * \param offset The offset of the start of data
 
432
 * \returns 0 on success, -1 on failure.
 
433
 */
 
434
int oggz_set_data_start (OGGZ * oggz, off_t offset);
 
435
/** \}
 
436
 */
 
437
 
 
438
#endif /* __OGGZ_SEEK_H__ */