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 __FISH_SOUND_H__
34 #define __FISH_SOUND_H__
35 
36 #include <fishsound/constants.h>
37 
38 /** \mainpage
39  *
40  * \section intro FishSound, the sound of fish!
41  *
42  * This is the documentation for the FishSound C API. FishSound provides
43  * a simple programming interface for decoding and encoding audio data
44  * using Xiph.Org codecs (FLAC, Speex and Vorbis).
45  *
46  * libfishsound by itself is designed to handle raw codec streams from
47  * a lower level layer such as UDP datagrams.
48  * When these codecs are used in files, they are commonly encapsulated in
49  * <a href="http://www.xiph.org/ogg/">Ogg</a> to produce
50  * <em>Ogg FLAC</em>, <em>Speex</em> and <em>Ogg Vorbis</em> files.
51  * Example C programs using
52  * <a href="http://www.annodex.net/software/liboggz/">liboggz</a> to
53  * read and write these files are provided in the libfishsound sources.
54  *
55  * For more information on the design and history of libfishsound, see the
56  * \link about About \endlink section of this documentation, and the
57  * <a href="http://www.annodex.net/software/libfishsound/">libfishsound</a>
58  * homepage.
59  *
60  * \subsection contents Contents
61  *
62  * - \link fishsound.h fishsound.h \endlink:
63  * Documentation of the FishSound API.
64  *
65  * - \link comments.h Handling comments \endlink:
66  * How to add and retrieve \a name = \a value metadata in Vorbis and Speex
67  * streams.
68  *
69  * - \link decode Decoding audio data \endlink:
70  * How to decode audio data with FishSound, including source for a fully
71  * working Ogg FLAC, Speex and Ogg Vorbis decoder.
72  *
73  * - \link encode Encoding audio data \endlink:
74  * How to encode audio data with FishSound, including source for a fully
75  * working Ogg FLAC, Speex and Ogg Vorbis encoder.
76  *
77  * - \link configuration Configuration \endlink:
78  * Customizing libfishsound to only decode or encode,
79  * or to disable support for a particular codec.
80  *
81  * - \link building Building \endlink:
82  * Information related to building software that uses libfishsound.
83  *
84  * - \link about About \endlink:
85  * Design, motivation, history and acknowledgements.
86  *
87  * \section Licensing
88  *
89  * libfishsound is provided under the following BSD-style open source license:
90  *
91  * \include COPYING
92  *
93  */
94 
95 /** \defgroup about About
96  *
97  * \section design Design
98  * libfishsound provides a simple programming interface for decoding and
99  * encoding audio data using codecs from
100  * <a href="http://www.xiph.org/">Xiph.Org</a>.
101  *
102  * libfishsound by itself is designed to handle raw codec streams from
103  * a lower level layer such as UDP datagrams.
104  * When these codecs are used in files, they are commonly encapsulated in
105  * <a href="http://www.xiph.org/ogg/">Ogg</a> to produce
106  * <em>Ogg FLAC</em>, <em>Speex</em> and <em>Ogg Vorbis</em> files.
107  * Example C programs using
108  * <a href="http://www.annodex.net/software/liboggz/">liboggz</a> to
109  * read and write these files are provided in the libfishsound sources.
110  *
111  * libfishsound is implemented as a wrapper around the existing codec
112  * libraries and provides a consistent, higher-level programming
113  * interface. The motivation for this is twofold: to simplify the task
114  * of developing application software that supports these codecs,
115  * and to ensure that valid codec streams are generated.
116  *
117  * \section history History
118  * libfishsound was designed and developed by Conrad Parker on the
119  * weekend of October 18-19 2003. Previously the author had implemented
120  * Vorbis and Speex support in the following software:
121  * - <a href="http://www.metadecks.org/software/sweep/">Sweep</a>, a
122  * digital audio editor with decoding and GUI control of all encoding
123  * options of Vorbis and Speex
124  * - Speex support in the <a href="http://www.xinehq.org/">xine</a>
125  * multimedia player
126  * - Vorbis and Speex importers for
127  * <a href="http://www.annodex.net/software/libannodex/">libannodex</a>,
128  the basic library for reading and writing
129  * <a href="http://www.annodex.net/">Annodex.net</a> media files.
130  *
131  * The implementation of libfishsound draws heavily on these sources, and
132  * in turn the original example sources of libvorbis and libvorbisenc by
133  * Monty, and libspeex by Jean-Marc Valin.
134  *
135  * The naming of libfishsound reflects both the Xiph.Org logo and
136  * the author's reputation as a dirty, smelly old fish.
137  *
138  * \section limitations Limitations
139  *
140  * libfishsound has been designed to accomodate the various decoding and
141  * encoding styles required by a wide variety of software. However, as it
142  * is an abstraction of the underlying libvorbis, libvorbisenc and libspeex
143  * libraries, it may not be possible to implement some low-level techniques
144  * that these libraries enable, such as parallelization of Vorbis sub-block
145  * decoding. Nevertheless it is expected that libfishsound is a useful
146  * API for most software requiring Vorbis or Speex support, including most
147  * applications the author has encountered.
148  *
149  * \section acknowledgements Acknowledgements
150  * Much of the API design follows the style of
151  * <a href="http://www.zip.com.au/~erikd/libsndfile/">libsndfile</a>.
152  * The author would like to thank Erik de Castro Lopo for feedback on the
153  * design of libfishsound.
154  */
155 
156 /** \defgroup configuration Configuration
157  *
158  * \section platforms Platform-specific configuration
159  *
160  * FishSound can be configured on most platforms using the GNU autoconf
161  * ./configure system described below.
162  *
163  * For Win32, see the \link win32 README.win32 \endlink section. You will
164  * need to edit <tt>win32/config.h</tt> by hand to achieve the customizations
165  * described below.
166  *
167  * \section ./configure ./configure
168  *
169  * It is possible to customize the functionality of libfishsound
170  * by using various ./configure flags when
171  * building it from source; for example you can build a smaller
172  * version of libfishsound to only decode or encode, or and you can
173  * choose to disable support for a particular codec.
174  * By default, both decoding and encoding support is built for all
175  * codecs found on the system.
176  *
177  * For general information about using ./configure, see the file
178  * \link install INSTALL \endlink
179  *
180  * \subsection no_encode Removing encoding support
181  *
182  * Configuring with \a --disable-encode will remove all support for encoding:
183  * - All internal encoding related functions will not be built
184  * - Any attempt to call fish_sound_new() with \a mode == FISH_SOUND_ENCODE
185  *   will fail, returning NULL
186  * - Any attempt to call fish_sound_encode_*() will return
187  *   FISH_SOUND_ERR_DISABLED
188  * - The resulting library will not be linked against libvorbisenc
189  *
190  * \subsection no_decode Removing decoding support
191  *
192  * Configuring with \a --disable-decode will remove all support for decoding:
193  * - All internal decoding related functions will not be built
194  * - Any attempt to call fish_sound_new() with \a mode == FISH_SOUND_DECODE
195  *   will fail, returning NULL
196  * - Any attempt to call fish_sound_decode() will return
197  *   FISH_SOUND_ERR_DISABLED
198  *
199  * \subsection no_flac Removing FLAC support
200  *
201  * Configuring with \a --disable-flac will remove all support for FLAC:
202  * - All internal FLAC related functions will not be built
203  * - Any attempt to call fish_sound_new() with \a mode == FISH_SOUND_ENCODE
204  *   and \a fsinfo->format == FISH_SOUND_FLAC will fail, returning NULL
205  * - The resulting library will not be linked against libFLAC
206  *
207  * \subsection no_speex Removing Speex support
208  *
209  * Configuring with \a --disable-speex will remove all support for Speex:
210  * - All internal Speex related functions will not be built
211  * - Any attempt to call fish_sound_new() with \a mode == FISH_SOUND_ENCODE
212  *   and \a fsinfo->format == FISH_SOUND_SPEEX will fail, returning NULL
213  * - The resulting library will not be linked against libspeex
214  *
215  * \subsection no_vorbis Removing Vorbis support
216  *
217  * Configuring with \a --disable-vorbis will remove all support for Vorbis:
218  * - All internal Vorbis related functions will not be built
219  * - Any attempt to call fish_sound_new() with \a mode == FISH_SOUND_ENCODE
220  *   and \a fsinfo->format == FISH_SOUND_VORBIS will fail, returning NULL
221  * - The resulting library will not be linked against libvorbis or libvorbisenc
222  *
223  * \subsection summary Configuration summary
224  *
225  * Upon successful configuration, you should see something like this:
226 <pre>
227 ------------------------------------------------------------------------
228   libfishsound 0.9.0:  Automatic configuration OK.
229 
230   General configuration:
231 
232     Experimental code: ........... no
233     Decoding support: ............ yes
234     Encoding support: ............ yes
235 
236   Library configuration (./src/libfishsound):
237 
238     FLAC support: ................ yes
239     Speex support: ............... yes (1.1.x)
240     Vorbis support: .............. yes
241 
242   Example programs (./src/examples):
243 
244     fishsound-identify fishsound-decode fishsound-encode
245 
246   Installation paths:
247 
248     libfishsound: ................ /usr/local/lib
249     C header files: .............. /usr/local/include/fishsound
250     Documentation: ............... /usr/local/share/doc/libfishsound
251 
252   Building:
253 
254     Type 'make' to compile libfishsound.
255 
256     Type 'make install' to install libfishsound.
257 
258     Type 'make check' to run test suite (Valgrind testing not enabled)
259 
260   Example programs will be built but not installed.
261 ------------------------------------------------------------------------
262 </pre>
263  */
264 
265 /** \defgroup install Installation
266  * \section install INSTALL
267  *
268  * \include INSTALL
269  */
270 
271 /** \defgroup win32 Building on Win32
272  * \section win32 README.Win32
273  *
274  * \include README.win32
275  */
276 
277 /** \defgroup building Building against libfishsound
278  *
279  *
280  * \section autoconf Using GNU autoconf
281  *
282  * If you are using GNU autoconf, you do not need to call pkg-config
283  * directly. Use the following macro to determine if libfishsound is
284  * available:
285  *
286  <pre>
287  PKG_CHECK_MODULES(FISHSOUND, fishsound >= 0.6.0,
288                    HAVE_FISHSOUND="yes", HAVE_FISHSOUND="no")
289  if test "x$HAVE_FISHSOUND" = "xyes" ; then
290    AC_SUBST(FISHSOUND_CFLAGS)
291    AC_SUBST(FISHSOUND_LIBS)
292  fi
293  </pre>
294  * (Note that if your application requires FLAC support, you should check
295  * for a version of fishsound >= 0.9.0).
296  *
297  * If libfishsound is found, HAVE_FISHSOUND will be set to "yes", and
298  * the autoconf variables FISHSOUND_CFLAGS and FISHSOUND_LIBS will
299  * be set appropriately.
300  *
301  * \section pkg-config Determining compiler options with pkg-config
302  *
303  * If you are not using GNU autoconf in your project, you can use the
304  * pkg-config tool directly to determine the correct compiler options.
305  *
306  <pre>
307  FISHSOUND_CFLAGS=`pkg-config --cflags fishsound`
308 
309  FISHSOUND_LIBS=`pkg-config --libs fishsound`
310  </pre>
311  *
312  */
313 
314 /** \file
315  * The libfishsound C API.
316  *
317  * \section general General usage
318  *
319  * All access is managed via a FishSound* handle. This is instantiated
320  * using fish_sound_new() and should be deleted with fish_sound_delete()
321  * when no longer required. If there is a discontinuity in the input
322  * data (eg. after seeking in an input file), call fish_sound_reset() to
323  * reset the internal codec state.
324  *
325  * \section decoding Decoding
326  *
327  * libfishsound provides callback based decoding: you feed it encoded audio
328  * data, and it will call your callback with decoded PCM. A more detailed
329  * explanation and a full example of decoding Ogg FLAC, Speex and Ogg Vorbis
330  * files is provided in the \link decode Decoding audio data \endlink section.
331  *
332  * \section encoding Encoding
333  *
334  * libfishsound provides callback based encoding: you feed it PCM audio,
335  * and it will call your callback with encoded audio data. A more detailed
336  * explanation and a full example of encoding Ogg FLAC, Speex and Ogg Vorbis
337  * files is provided in the \link encode Encoding audio data \endlink section.
338  */
339 
340 /** \defgroup decode Decoding audio data
341  *
342  * To decode audio data using libfishsound:
343  *
344  * - create a FishSound* object with mode FISH_SOUND_DECODE. fish_sound_new()
345  * will return a new FishSound* object, initialised for decoding, and the
346  * FishSoundInfo structure will be cleared.
347  * - provide a FishSoundDecoded_* callback for libfishsound to call when it has
348  * decoded audio.
349  * - (optionally) specify whether you want to receive interleaved or
350  * per-channel PCM data, using a fish_sound_set_interleave().
351  * The default is for per-channel (non-interleaved) PCM.
352  * - feed encoded audio data to libfishsound via fish_sound_decode().
353  * libfishsound will decode the audio for you, calling the FishSoundDecoded_*
354  * callback you provided earlier each time it has a block of audio ready.
355  * - when finished, call fish_sound_delete().
356  *
357  * This procedure is illustrated in src/examples/fishsound-decode.c.
358  * Note that this example additionally:
359  * - uses <a href="http://www.annodex.net/software/liboggz/">liboggz</a> to
360  * demultiplex audio data from an Ogg encapsulated FLAC, Speex or Vorbis
361  * stream. The step of feeding encoded data to libfishsound is done within
362  * the OggzReadPacket callback.
363  * - uses <a href="http://www.mega-nerd.com/libsndfile/">libsndfile</a> to
364  * write the decoded audio to a WAV file.
365  *
366  * Hence this example code demonstrates all that is needed to decode
367  * Ogg FLAC, Speex or Ogg Vorbis files:
368  *
369  * \include fishsound-decode.c
370  */
371 
372 /** \defgroup encode Encoding audio data
373  *
374  * To encode audio data using libfishsound:
375  *
376  * - create a FishSound* object with mode FISH_SOUND_ENCODE, and with a
377  * FishSoundInfo structure filled in with the required encoding parameters.
378  * fish_sound_new()  will return a new FishSound* object initialised for
379  * encoding.
380  * - provide a FishSoundEncoded callback for libfishsound to call when it
381  * has a block of encoded audio
382  * - feed raw PCM audio data to libfishsound via fish_sound_encode_*().
383  * libfishsound will encode the audio for you, calling the FishSoundEncoded
384  * callback you provided earlier each time it has a block of encoded audio
385  * ready.
386  * - when finished, call fish_sound_delete().
387  *
388  * This procedure is illustrated in src/examples/fishsound-encode.c.
389  * Note that this example additionally:
390  * - uses <a href="http://www.mega-nerd.com/libsndfile/">libsndfile</a> to
391  * read input from a PCM audio file (WAV, AIFF, etc.)
392  * - uses <a href="http://www.annodex.net/software/liboggz/">liboggz</a> to
393  * encapsulate the encoded FLAC, Speex or Vorbis data in an Ogg stream.
394  *
395  * Hence this example code demonstrates all that is needed to encode
396  * Ogg FLAC, Speex and Ogg Vorbis files:
397  *
398  * \include fishsound-encode.c
399  */
400 
401 /**
402  * Info about a particular encoder/decoder instance
403  */
404 typedef struct {
405   /** Sample rate of audio data in Hz */
406   int samplerate;
407 
408   /** Count of channels */
409   int channels;
410 
411   /** FISH_SOUND_VORBIS, FISH_SOUND_SPEEX, FISH_SOUND_FLAC etc. */
412   int format;
413 } FishSoundInfo;
414 
415 /**
416  * Info about a particular sound format
417  */
418 typedef struct {
419   /** FISH_SOUND_VORBIS, FISH_SOUND_SPEEX, FISH_SOUND_FLAC etc. */
420   int format;
421 
422   /** Printable name */
423   const char * name;
424 
425   /** Commonly used file extension */
426   const char * extension;
427 } FishSoundFormat;
428 
429 /**
430  * An opaque handle to a FishSound. This is returned by fishsound_new()
431  * and is passed to all other fish_sound_*() functions.
432  */
433 typedef void * FishSound;
434 
435 #ifdef __cplusplus
436 extern "C" {
437 #endif
438 
439 /**
440  * Identify a codec based on the first few bytes of data.
441  * \param buf A pointer to the first few bytes of the data
442  * \param bytes The count of bytes available at buf
443  * \retval FISH_SOUND_xxxxxx FISH_SOUND_VORBIS, FISH_SOUND_SPEEX or
444  * FISH_SOUND_FLAC if \a buf was identified as the initial bytes of a
445  * supported codec
446  * \retval FISH_SOUND_UNKNOWN if the codec could not be identified
447  * \retval FISH_SOUND_ERR_SHORT_IDENTIFY if \a bytes is less than 8
448  * \note If \a bytes is exactly 8, then only a weak check is performed,
449  * which is fast but may return a false positive.
450  * \note If \a bytes is greater than 8, then a stronger check is performed
451  * in which an attempt is made to decode \a buf as the initial header of
452  * each supported codec. This is unlikely to return a false positive but
453  * is only useful if \a buf is the entire payload of a packet derived from
454  * a lower layer such as Ogg framing or UDP datagrams.
455  */
456 int
457 fish_sound_identify (unsigned char * buf, long bytes);
458 
459 /**
460  * Instantiate a new FishSound* handle
461  * \param mode FISH_SOUND_DECODE or FISH_SOUND_ENCODE
462  * \param fsinfo Encoder configuration, may be NULL for FISH_SOUND_DECODE
463  * \returns A new FishSound* handle, or NULL on error
464  */
465 FishSound * fish_sound_new (int mode, FishSoundInfo * fsinfo);
466 
467 /**
468  * Flush any internally buffered data, forcing encode
469  * \param fsound A FishSound* handle
470  * \returns 0 on success, -1 on failure
471  */
472 long fish_sound_flush (FishSound * fsound);
473 
474 /**
475  * Reset the codec state of a FishSound object.
476  *
477  * When decoding from a seekable file, fish_sound_reset() should be called
478  * after any seek operations. See also fish_sound_set_frameno().
479  *
480  * \param fsound A FishSound* handle
481  * \returns 0 on success, -1 on failure
482  */
483 int fish_sound_reset (FishSound * fsound);
484 
485 /**
486  * Delete a FishSound object
487  * \param fsound A FishSound* handle
488  * \returns 0 on success, -1 on failure
489  */
490 int fish_sound_delete (FishSound * fsound);
491 
492 /**
493  * Command interface
494  * \param fsound A FishSound* handle
495  * \param command The command action
496  * \param data Command data
497  * \param datasize Size of the data in bytes
498  * \returns 0 on success, -1 on failure
499  */
500 int fish_sound_command (FishSound * fsound, int command, void * data,
501 			int datasize);
502 
503 /**
504  * Query whether a FishSound object is using interleaved PCM
505  * \param fsound A FishSound* handle
506  * \retval 0 \a fsound uses non-interleaved PCM
507  * \retval 1 \a fsound uses interleaved PCM
508  * \retval -1 Invalid \a fsound, or out of memory.
509  */
510 int fish_sound_get_interleave (FishSound * fsound);
511 
512 /**
513  * Query the current frame number of a FishSound object.
514  *
515  * For decoding, this is the greatest frame index that has been decoded and
516  * made available to a FishSoundDecoded callback. This function is safe to
517  * call from within a FishSoundDecoded callback, and corresponds to the frame
518  * number of the last frame in the current decoded block.
519  *
520  * For encoding, this is the greatest frame index that has been encoded. This
521  * function is safe to call from within a FishSoundEncoded callback, and
522  * corresponds to the frame number of the last frame encoded in the current
523  * block.
524  *
525  * \param fsound A FishSound* handle
526  * \returns The current frame number
527  * \retval -1 Invalid \a fsound
528  */
529 long fish_sound_get_frameno (FishSound * fsound);
530 
531 /**
532  * Set the current frame number of a FishSound object.
533  *
534  * When decoding from a seekable file, fish_sound_set_frameno() should be
535  * called after any seek operations, otherwise the value returned by
536  * fish_sound_get_frameno() will simply continue to increment. See also
537  * fish_sound_reset().
538  *
539  * \param fsound A FishSound* handle
540  * \param frameno The current frame number.
541  * \retval 0 Success
542  * \retval -1 Invalid \a fsound
543  */
544 int fish_sound_set_frameno (FishSound * fsound, long frameno);
545 
546 /**
547  * Prepare truncation details for the next block of data.
548  * The semantics of these parameters derives directly from Ogg encapsulation
549  * of Vorbis, described
550  * <a href="http://www.xiph.org/ogg/vorbis/doc/Vorbis_I_spec.html#vorbis-over-ogg">here</a>.
551  *
552  * When decoding from Ogg, you should call this function with the \a granulepos
553  * and \a eos of the \a ogg_packet structure. This call should be made before
554  * passing the packet's data to fish_sound_decode(). Failure to do so may
555  * result in minor decode errors on the first and/or last packet of the stream.
556  *
557  * When encoding into Ogg, you should call this function with the \a granulepos
558  * and \a eos that will be used for the \a ogg_packet structure. This call
559  * should be made before passing the block of audio data to
560  * fish_sound_encode_*(). Failure to do so may result in minor encoding errors
561  * on the first and/or last packet of the stream.
562  *
563  * \param fsound A FishSound* handle
564  * \param next_granulepos The "granulepos" for the next block to decode.
565  *        If unknown, set \a next_granulepos to -1. Otherwise,
566  *        \a next_granulepos specifies the frameno of the final frame in the
567  *        block. This is authoritative, hence can be used to indicate
568  *        various forms of truncation at the beginning or end of a stream.
569  *        Mid-stream, a later-than-expected "granulepos" indicates that some
570  *        data was missing.
571  * \param next_eos A boolean indicating whether the next data block will be
572  *        the last in the stream.
573  * \retval 0 Success
574  * \retval -1 Invalid \a fsound
575  */
576 int fish_sound_prepare_truncation (FishSound * fsound, long next_granulepos,
577                                    int next_eos);
578 
579 #ifdef __cplusplus
580 }
581 #endif
582 
583 #include <fishsound/decode.h>
584 #include <fishsound/encode.h>
585 #include <fishsound/comments.h>
586 
587 #include <fishsound/deprecated.h>
588 
589 #endif /* __FISH_SOUND_H__ */
590