1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #ifndef FLAC__FILE_DECODER_H
21 #define FLAC__FILE_DECODER_H
23 #include "seekable_stream_decoder.h"
30 /** \file include/FLAC/file_decoder.h
33 * This module contains the functions which implement the file
36 * See the detailed documentation in the
37 * \link flac_file_decoder file decoder \endlink module.
40 /** \defgroup flac_file_decoder FLAC/file_decoder.h: file decoder interface
41 * \ingroup flac_decoder
44 * This module contains the functions which implement the file
47 * The basic usage of this decoder is as follows:
48 * - The program creates an instance of a decoder using
49 * FLAC__file_decoder_new().
50 * - The program overrides the default settings and sets callbacks for
51 * writing, error reporting, and metadata reporting using
52 * FLAC__file_decoder_set_*() functions.
53 * - The program initializes the instance to validate the settings and
54 * prepare for decoding using FLAC__file_decoder_init().
55 * - The program calls the FLAC__file_decoder_process_*() functions
56 * to decode data, which subsequently calls the callbacks.
57 * - The program finishes the decoding with FLAC__file_decoder_finish(),
58 * which flushes the input and output and resets the decoder to the
59 * uninitialized state.
60 * - The instance may be used again or deleted with
61 * FLAC__file_decoder_delete().
63 * The file decoder is a trivial wrapper around the
64 * \link flac_seekable_stream_decoder seekable stream decoder \endlink
65 * meant to simplfy the process of decoding from a standard file. The
66 * file decoder supplies all but the Write/Metadata/Error callbacks.
67 * The user needs only to provide the path to the file and the file
68 * decoder handles the rest.
70 * Like the seekable stream decoder, seeking is exposed through the
71 * FLAC__file_decoder_seek_absolute() method. At any point after the file
72 * decoder has been initialized, the user can call this function to seek to
73 * an exact sample within the file. Subsequently, the first time the write
74 * callback is called it will be passed a (possibly partial) block starting
77 * The file decoder also inherits MD5 signature checking from the seekable
78 * stream decoder. If this is turned on before initialization,
79 * FLAC__file_decoder_finish() will report when the decoded MD5 signature
80 * does not match the one stored in the STREAMINFO block. MD5 checking is
81 * automatically turned off if there is no signature in the STREAMINFO
82 * block or when a seek is attempted.
84 * Make sure to read the detailed descriptions of the
85 * \link flac_seekable_stream_decoder seekable stream decoder module \endlink
86 * and \link flac_stream_decoder stream decoder module \endlink
87 * since the file decoder inherits much of its behavior from them.
90 * The "set" functions may only be called when the decoder is in the
91 * state FLAC__FILE_DECODER_UNINITIALIZED, i.e. after
92 * FLAC__file_decoder_new() or FLAC__file_decoder_finish(), but
93 * before FLAC__file_decoder_init(). If this is the case they will
94 * return \c true, otherwise \c false.
97 * FLAC__file_decoder_finish() resets all settings to the constructor
98 * defaults, including the callbacks.
104 /** State values for a FLAC__FileDecoder
106 * The decoder's state can be obtained by calling FLAC__file_decoder_get_state().
110 FLAC__FILE_DECODER_OK = 0,
111 /**< The decoder is in the normal OK state. */
113 FLAC__FILE_DECODER_END_OF_FILE,
114 /**< The decoder has reached the end of the file. */
116 FLAC__FILE_DECODER_ERROR_OPENING_FILE,
117 /**< An error occurred opening the input file. */
119 FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR,
120 /**< An error occurred allocating memory. */
122 FLAC__FILE_DECODER_SEEK_ERROR,
123 /**< An error occurred while seeking. */
125 FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR,
126 /**< An error occurred in the underlying seekable stream decoder. */
128 FLAC__FILE_DECODER_ALREADY_INITIALIZED,
129 /**< FLAC__file_decoder_init() was called when the decoder was already
130 * initialized, usually because FLAC__file_decoder_finish() was not
134 FLAC__FILE_DECODER_INVALID_CALLBACK,
135 /**< FLAC__file_decoder_init() was called without all callbacks
139 FLAC__FILE_DECODER_UNINITIALIZED
140 /**< The decoder is in the uninitialized state. */
142 } FLAC__FileDecoderState;
144 /** Maps a FLAC__FileDecoderState to a C string.
146 * Using a FLAC__FileDecoderState as the index to this array
147 * will give the string equivalent. The contents should not be modified.
149 extern const char * const FLAC__FileDecoderStateString[];
152 /***********************************************************************
154 * class FLAC__FileDecoder : public FLAC__StreamDecoder
156 ***********************************************************************/
158 struct FLAC__FileDecoderProtected;
159 struct FLAC__FileDecoderPrivate;
160 /** The opaque structure definition for the file decoder type. See the
161 * \link flac_file_decoder file decoder module \endlink for a detailed
165 struct FLAC__FileDecoderProtected *protected_; /* avoid the C++ keyword 'protected' */
166 struct FLAC__FileDecoderPrivate *private_; /* avoid the C++ keyword 'private' */
170 typedef FLAC__StreamDecoderWriteStatus (*FLAC__FileDecoderWriteCallback)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
171 typedef void (*FLAC__FileDecoderMetadataCallback)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
172 typedef void (*FLAC__FileDecoderErrorCallback)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
175 /***********************************************************************
177 * Class constructor/destructor
179 ***********************************************************************/
181 /** Create a new file decoder instance. The instance is created with
182 * default settings; see the individual FLAC__file_decoder_set_*()
183 * functions for each setting's default.
185 * \retval FLAC__FileDecoder*
186 * \c NULL if there was an error allocating memory, else the new instance.
188 FLAC__FileDecoder *FLAC__file_decoder_new();
190 /** Free a decoder instance. Deletes the object pointed to by \a decoder.
192 * \param decoder A pointer to an existing decoder.
194 * \code decoder != NULL \endcode
196 void FLAC__file_decoder_delete(FLAC__FileDecoder *decoder);
199 /***********************************************************************
201 * Public class method prototypes
203 ***********************************************************************/
205 /** Set the "MD5 signature checking" flag.
206 * This is inherited from FLAC__SeekableStreamDecoder; see
207 * FLAC__seekable_stream_decoder_set_md5_checking().
210 * \param decoder A decoder instance to set.
211 * \param value See above.
213 * \code decoder != NULL \endcode
215 * \c false if the decoder is already initialized, else \c true.
217 FLAC__bool FLAC__file_decoder_set_md5_checking(FLAC__FileDecoder *decoder, FLAC__bool value);
219 /** Set the input file name to decode.
222 * \param decoder A decoder instance to set.
223 * \param value The input file name, or "-" for \c stdin.
225 * \code decoder != NULL \endcode
226 * \code value != NULL \endcode
228 * \c false if the decoder is already initialized, or there was a memory
229 * allocation error, else \c true.
231 FLAC__bool FLAC__file_decoder_set_filename(FLAC__FileDecoder *decoder, const char *value);
233 /** Set the write callback.
234 * This is inherited from FLAC__SeekableStreamDecoder; see
235 * FLAC__seekable_stream_decoder_set_write_callback().
238 * The callback is mandatory and must be set before initialization.
241 * \param decoder A decoder instance to set.
242 * \param value See above.
244 * \code decoder != NULL \endcode
245 * \code value != NULL \endcode
247 * \c false if the decoder is already initialized, else \c true.
249 FLAC__bool FLAC__file_decoder_set_write_callback(FLAC__FileDecoder *decoder, FLAC__FileDecoderWriteCallback value);
251 /** Set the metadata callback.
252 * This is inherited from FLAC__SeekableStreamDecoder; see
253 * FLAC__seekable_stream_decoder_set_metadata_callback().
256 * The callback is mandatory and must be set before initialization.
259 * \param decoder A decoder instance to set.
260 * \param value See above.
262 * \code decoder != NULL \endcode
263 * \code value != NULL \endcode
265 * \c false if the decoder is already initialized, else \c true.
267 FLAC__bool FLAC__file_decoder_set_metadata_callback(FLAC__FileDecoder *decoder, FLAC__FileDecoderMetadataCallback value);
269 /** Set the error callback.
270 * This is inherited from FLAC__SeekableStreamDecoder; see
271 * FLAC__seekable_stream_decoder_set_error_callback().
274 * The callback is mandatory and must be set before initialization.
277 * \param decoder A decoder instance to set.
278 * \param value See above.
280 * \code decoder != NULL \endcode
281 * \code value != NULL \endcode
283 * \c false if the decoder is already initialized, else \c true.
285 FLAC__bool FLAC__file_decoder_set_error_callback(FLAC__FileDecoder *decoder, FLAC__FileDecoderErrorCallback value);
287 /** Set the client data to be passed back to callbacks.
288 * This value will be supplied to callbacks in their \a client_data
292 * \param decoder An decoder instance to set.
293 * \param value See above.
295 * \code decoder != NULL \endcode
297 * \c false if the decoder is already initialized, else \c true.
299 FLAC__bool FLAC__file_decoder_set_client_data(FLAC__FileDecoder *decoder, void *value);
301 /** This is inherited from FLAC__SeekableStreamDecoder; see
302 * FLAC__seekable_stream_decoder_set_metadata_respond().
304 * \default By default, only the \c STREAMINFO block is returned via the
306 * \param decoder A decoder instance to set.
307 * \param type See above.
309 * \code decoder != NULL \endcode
312 * \c false if the decoder is already initialized, else \c true.
314 FLAC__bool FLAC__file_decoder_set_metadata_respond(FLAC__FileDecoder *decoder, FLAC__MetadataType type);
316 /** This is inherited from FLAC__SeekableStreamDecoder; see
317 * FLAC__seekable_stream_decoder_set_metadata_respond_application().
319 * \default By default, only the \c STREAMINFO block is returned via the
321 * \param decoder A decoder instance to set.
322 * \param id See above.
324 * \code decoder != NULL \endcode
325 * \code id != NULL \endcode
327 * \c false if the decoder is already initialized, else \c true.
329 FLAC__bool FLAC__file_decoder_set_metadata_respond_application(FLAC__FileDecoder *decoder, const FLAC__byte id[4]);
331 /** This is inherited from FLAC__SeekableStreamDecoder; see
332 * FLAC__seekable_stream_decoder_set_metadata_respond_all().
334 * \default By default, only the \c STREAMINFO block is returned via the
336 * \param decoder A decoder instance to set.
338 * \code decoder != NULL \endcode
340 * \c false if the decoder is already initialized, else \c true.
342 FLAC__bool FLAC__file_decoder_set_metadata_respond_all(FLAC__FileDecoder *decoder);
344 /** This is inherited from FLAC__SeekableStreamDecoder; see
345 * FLAC__seekable_stream_decoder_set_metadata_ignore().
347 * \default By default, only the \c STREAMINFO block is returned via the
349 * \param decoder A decoder instance to set.
350 * \param type See above.
352 * \code decoder != NULL \endcode
355 * \c false if the decoder is already initialized, else \c true.
357 FLAC__bool FLAC__file_decoder_set_metadata_ignore(FLAC__FileDecoder *decoder, FLAC__MetadataType type);
359 /** This is inherited from FLAC__SeekableStreamDecoder; see
360 * FLAC__seekable_stream_decoder_set_metadata_ignore_application().
362 * \default By default, only the \c STREAMINFO block is returned via the
364 * \param decoder A decoder instance to set.
365 * \param id See above.
367 * \code decoder != NULL \endcode
368 * \code id != NULL \endcode
370 * \c false if the decoder is already initialized, else \c true.
372 FLAC__bool FLAC__file_decoder_set_metadata_ignore_application(FLAC__FileDecoder *decoder, const FLAC__byte id[4]);
374 /** This is inherited from FLAC__SeekableStreamDecoder; see
375 * FLAC__seekable_stream_decoder_set_metadata_ignore_all().
377 * \default By default, only the \c STREAMINFO block is returned via the
379 * \param decoder A decoder instance to set.
381 * \code decoder != NULL \endcode
383 * \c false if the decoder is already initialized, else \c true.
385 FLAC__bool FLAC__file_decoder_set_metadata_ignore_all(FLAC__FileDecoder *decoder);
387 /** Get the current decoder state.
389 * \param decoder A decoder instance to query.
391 * \code decoder != NULL \endcode
392 * \retval FLAC__FileDecoderState
393 * The current decoder state.
395 FLAC__FileDecoderState FLAC__file_decoder_get_state(const FLAC__FileDecoder *decoder);
397 /** Get the state of the underlying seekable stream decoder.
398 * Useful when the file decoder state is
399 * \c FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR.
401 * \param decoder An decoder instance to query.
403 * \code decoder != NULL \endcode
404 * \retval FLAC__SeekableStreamDecoderState
405 * The seekable stream decoder state.
407 FLAC__SeekableStreamDecoderState FLAC__file_decoder_get_seekable_stream_decoder_state(const FLAC__FileDecoder *decoder);
409 /** Get the state of the underlying stream decoder.
410 * Useful when the file decoder state is
411 * \c FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR and the seekable stream
412 * decoder state is \c FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR.
414 * \param decoder An decoder instance to query.
416 * \code decoder != NULL \endcode
417 * \retval FLAC__SeekableStreamDecoderState
418 * The seekable stream decoder state.
420 FLAC__StreamDecoderState FLAC__file_decoder_get_stream_decoder_state(const FLAC__FileDecoder *decoder);
422 /** Get the "MD5 signature checking" flag.
423 * This is inherited from FLAC__SeekableStreamDecoder; see
424 * FLAC__seekable_stream_decoder_get_md5_checking().
426 * \param decoder A decoder instance to query.
428 * \code decoder != NULL \endcode
432 FLAC__bool FLAC__file_decoder_get_md5_checking(const FLAC__FileDecoder *decoder);
434 /** This is inherited from FLAC__SeekableStreamDecoder; see
435 * FLAC__seekable_stream_decoder_get_channels().
437 * \param decoder A decoder instance to query.
439 * \code decoder != NULL \endcode
443 unsigned FLAC__file_decoder_get_channels(const FLAC__FileDecoder *decoder);
445 /** This is inherited from FLAC__SeekableStreamDecoder; see
446 * FLAC__seekable_stream_decoder_get_channel_assignment().
448 * \param decoder A decoder instance to query.
450 * \code decoder != NULL \endcode
451 * \retval FLAC__ChannelAssignment
454 FLAC__ChannelAssignment FLAC__file_decoder_get_channel_assignment(const FLAC__FileDecoder *decoder);
456 /** This is inherited from FLAC__SeekableStreamDecoder; see
457 * FLAC__seekable_stream_decoder_get_bits_per_sample().
459 * \param decoder A decoder instance to query.
461 * \code decoder != NULL \endcode
465 unsigned FLAC__file_decoder_get_bits_per_sample(const FLAC__FileDecoder *decoder);
467 /** This is inherited from FLAC__SeekableStreamDecoder; see
468 * FLAC__seekable_stream_decoder_get_sample_rate().
470 * \param decoder A decoder instance to query.
472 * \code decoder != NULL \endcode
476 unsigned FLAC__file_decoder_get_sample_rate(const FLAC__FileDecoder *decoder);
478 /** This is inherited from FLAC__SeekableStreamDecoder; see
479 * FLAC__seekable_stream_decoder_get_blocksize().
481 * \param decoder A decoder instance to query.
483 * \code decoder != NULL \endcode
487 unsigned FLAC__file_decoder_get_blocksize(const FLAC__FileDecoder *decoder);
489 /** Initialize the decoder instance.
490 * Should be called after FLAC__file_decoder_new() and
491 * FLAC__file_decoder_set_*() but before any of the
492 * FLAC__file_decoder_process_*() functions. Will set and return
493 * the decoder state, which will be FLAC__FILE_DECODER_OK if
494 * initialization succeeded.
496 * \param decoder An uninitialized decoder instance.
498 * \code decoder != NULL \endcode
499 * \retval FLAC__FileDecoderState
500 * \c FLAC__FILE_DECODER_OK if initialization was successful; see
501 * FLAC__FileDecoderState for the meanings of other return values.
503 FLAC__FileDecoderState FLAC__file_decoder_init(FLAC__FileDecoder *decoder);
505 /** Finish the decoding process.
506 * Flushes the decoding buffer, releases resources, resets the decoder
507 * settings to their defaults, and returns the decoder state to
508 * FLAC__FILE_DECODER_UNINITIALIZED.
510 * In the event of a prematurely-terminated decode, it is not strictly
511 * necessary to call this immediately before FLAC__file_decoder_delete()
512 * but it is good practice to match every FLAC__file_decoder_init() with
513 * a FLAC__file_decoder_finish().
515 * \param decoder An uninitialized decoder instance.
517 * \code decoder != NULL \endcode
519 * \c false if MD5 checking is on AND a STREAMINFO block was available
520 * AND the MD5 signature in the STREAMINFO block was non-zero AND the
521 * signature does not match the one computed by the decoder; else
524 FLAC__bool FLAC__file_decoder_finish(FLAC__FileDecoder *decoder);
526 /** This is inherited from FLAC__SeekableStreamDecoder; see
527 * FLAC__seekable_stream_decoder_process_single().
529 * \param decoder A decoder instance.
531 * \code decoder != NULL \endcode
535 FLAC__bool FLAC__file_decoder_process_single(FLAC__FileDecoder *decoder);
537 /** This is inherited from FLAC__SeekableStreamDecoder; see
538 * FLAC__seekable_stream_decoder_process_until_end_of_metadata().
540 * \param decoder A decoder instance.
542 * \code decoder != NULL \endcode
546 FLAC__bool FLAC__file_decoder_process_until_end_of_metadata(FLAC__FileDecoder *decoder);
548 /** This is inherited from FLAC__SeekableStreamDecoder; see
549 * FLAC__seekable_stream_decoder_process_until_end_of_stream().
551 * \param decoder A decoder instance.
553 * \code decoder != NULL \endcode
557 FLAC__bool FLAC__file_decoder_process_until_end_of_file(FLAC__FileDecoder *decoder);
559 /** This is inherited from FLAC__SeekableStreamDecoder; see
560 * FLAC__seekable_stream_decoder_process_remaining_frames().
562 * \param decoder A decoder instance.
564 * \code decoder != NULL \endcode
568 FLAC__bool FLAC__file_decoder_process_remaining_frames(FLAC__FileDecoder *decoder);
570 /** Flush the input and seek to an absolute sample.
571 * This is inherited from FLAC__SeekableStreamDecoder; see
572 * FLAC__seekable_stream_decoder_seek_absolute().
574 * \param decoder A decoder instance.
575 * \param sample The target sample number to seek to.
577 * \code decoder != NULL \endcode
579 * \c true if successful, else \c false.
581 FLAC__bool FLAC__file_decoder_seek_absolute(FLAC__FileDecoder *decoder, FLAC__uint64 sample);