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 /***********************************************************************
172 * Class constructor/destructor
174 ***********************************************************************/
176 /** Create a new file decoder instance. The instance is created with
177 * default settings; see the individual FLAC__file_decoder_set_*()
178 * functions for each setting's default.
180 * \retval FLAC__FileDecoder*
181 * \c NULL if there was an error allocating memory, else the new instance.
183 FLAC__FileDecoder *FLAC__file_decoder_new();
185 /** Free a decoder instance. Deletes the object pointed to by \a decoder.
187 * \param decoder A pointer to an existing decoder.
189 * \code decoder != NULL \endcode
191 void FLAC__file_decoder_delete(FLAC__FileDecoder *decoder);
194 /***********************************************************************
196 * Public class method prototypes
198 ***********************************************************************/
200 /** Set the "MD5 signature checking" flag.
201 * This is inherited from FLAC__SeekableStreamDecoder; see
202 * FLAC__seekable_stream_decoder_set_md5_checking().
205 * \param decoder A decoder instance to set.
206 * \param value See above.
208 * \code decoder != NULL \endcode
210 * \c false if the decoder is already initialized, else \c true.
212 FLAC__bool FLAC__file_decoder_set_md5_checking(FLAC__FileDecoder *decoder, FLAC__bool value);
214 /** Set the input file name to decode.
217 * \param decoder A decoder instance to set.
218 * \param value The input file name, or "-" for \c stdin.
220 * \code decoder != NULL \endcode
221 * \code value != NULL \endcode
223 * \c false if the decoder is already initialized, or there was a memory
224 * allocation error, else \c true.
226 FLAC__bool FLAC__file_decoder_set_filename(FLAC__FileDecoder *decoder, const char *value);
228 /** Set the write callback.
229 * This is inherited from FLAC__SeekableStreamDecoder; see
230 * FLAC__seekable_stream_decoder_set_write_callback().
233 * The callback is mandatory and must be set before initialization.
236 * \param decoder A decoder instance to set.
237 * \param value See above.
239 * \code decoder != NULL \endcode
240 * \code value != NULL \endcode
242 * \c false if the decoder is already initialized, else \c true.
244 FLAC__bool FLAC__file_decoder_set_write_callback(FLAC__FileDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__FileDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data));
246 /** Set the metadata callback.
247 * This is inherited from FLAC__SeekableStreamDecoder; see
248 * FLAC__seekable_stream_decoder_set_metadata_callback().
251 * The callback is mandatory and must be set before initialization.
254 * \param decoder A decoder instance to set.
255 * \param value See above.
257 * \code decoder != NULL \endcode
258 * \code value != NULL \endcode
260 * \c false if the decoder is already initialized, else \c true.
262 FLAC__bool FLAC__file_decoder_set_metadata_callback(FLAC__FileDecoder *decoder, void (*value)(const FLAC__FileDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data));
264 /** Set the error callback.
265 * This is inherited from FLAC__SeekableStreamDecoder; see
266 * FLAC__seekable_stream_decoder_set_error_callback().
269 * The callback is mandatory and must be set before initialization.
272 * \param decoder A decoder instance to set.
273 * \param value See above.
275 * \code decoder != NULL \endcode
276 * \code value != NULL \endcode
278 * \c false if the decoder is already initialized, else \c true.
280 FLAC__bool FLAC__file_decoder_set_error_callback(FLAC__FileDecoder *decoder, void (*value)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data));
282 /** Set the client data to be passed back to callbacks.
283 * This value will be supplied to callbacks in their \a client_data
287 * \param decoder An decoder instance to set.
288 * \param value See above.
290 * \code decoder != NULL \endcode
292 * \c false if the decoder is already initialized, else \c true.
294 FLAC__bool FLAC__file_decoder_set_client_data(FLAC__FileDecoder *decoder, void *value);
296 /** This is inherited from FLAC__SeekableStreamDecoder; see
297 * FLAC__seekable_stream_decoder_set_metadata_respond().
299 * \default By default, only the \c STREAMINFO block is returned via the
301 * \param decoder A decoder instance to set.
302 * \param type See above.
304 * \code decoder != NULL \endcode
307 * \c false if the decoder is already initialized, else \c true.
309 FLAC__bool FLAC__file_decoder_set_metadata_respond(FLAC__FileDecoder *decoder, FLAC__MetadataType type);
311 /** This is inherited from FLAC__SeekableStreamDecoder; see
312 * FLAC__seekable_stream_decoder_set_metadata_respond_application().
314 * \default By default, only the \c STREAMINFO block is returned via the
316 * \param decoder A decoder instance to set.
317 * \param id See above.
319 * \code decoder != NULL \endcode
320 * \code id != NULL \endcode
322 * \c false if the decoder is already initialized, else \c true.
324 FLAC__bool FLAC__file_decoder_set_metadata_respond_application(FLAC__FileDecoder *decoder, const FLAC__byte id[4]);
326 /** This is inherited from FLAC__SeekableStreamDecoder; see
327 * FLAC__seekable_stream_decoder_set_metadata_respond_all().
329 * \default By default, only the \c STREAMINFO block is returned via the
331 * \param decoder A decoder instance to set.
333 * \code decoder != NULL \endcode
335 * \c false if the decoder is already initialized, else \c true.
337 FLAC__bool FLAC__file_decoder_set_metadata_respond_all(FLAC__FileDecoder *decoder);
339 /** This is inherited from FLAC__SeekableStreamDecoder; see
340 * FLAC__seekable_stream_decoder_set_metadata_ignore().
342 * \default By default, only the \c STREAMINFO block is returned via the
344 * \param decoder A decoder instance to set.
345 * \param type See above.
347 * \code decoder != NULL \endcode
350 * \c false if the decoder is already initialized, else \c true.
352 FLAC__bool FLAC__file_decoder_set_metadata_ignore(FLAC__FileDecoder *decoder, FLAC__MetadataType type);
354 /** This is inherited from FLAC__SeekableStreamDecoder; see
355 * FLAC__seekable_stream_decoder_set_metadata_ignore_application().
357 * \default By default, only the \c STREAMINFO block is returned via the
359 * \param decoder A decoder instance to set.
360 * \param id See above.
362 * \code decoder != NULL \endcode
363 * \code id != NULL \endcode
365 * \c false if the decoder is already initialized, else \c true.
367 FLAC__bool FLAC__file_decoder_set_metadata_ignore_application(FLAC__FileDecoder *decoder, const FLAC__byte id[4]);
369 /** This is inherited from FLAC__SeekableStreamDecoder; see
370 * FLAC__seekable_stream_decoder_set_metadata_ignore_all().
372 * \default By default, only the \c STREAMINFO block is returned via the
374 * \param decoder A decoder instance to set.
376 * \code decoder != NULL \endcode
378 * \c false if the decoder is already initialized, else \c true.
380 FLAC__bool FLAC__file_decoder_set_metadata_ignore_all(FLAC__FileDecoder *decoder);
382 /** Get the current decoder state.
384 * \param decoder A decoder instance to query.
386 * \code decoder != NULL \endcode
387 * \retval FLAC__FileDecoderState
388 * The current decoder state.
390 FLAC__FileDecoderState FLAC__file_decoder_get_state(const FLAC__FileDecoder *decoder);
392 /** Get the state of the underlying seekable stream decoder.
393 * Useful when the file decoder state is
394 * \c FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR.
396 * \param decoder An decoder instance to query.
398 * \code decoder != NULL \endcode
399 * \retval FLAC__SeekableStreamDecoderState
400 * The seekable stream decoder state.
402 FLAC__SeekableStreamDecoderState FLAC__file_decoder_get_seekable_stream_decoder_state(const FLAC__FileDecoder *decoder);
404 /** Get the state of the underlying stream decoder.
405 * Useful when the file decoder state is
406 * \c FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR and the seekable stream
407 * decoder state is \c FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR.
409 * \param decoder An decoder instance to query.
411 * \code decoder != NULL \endcode
412 * \retval FLAC__SeekableStreamDecoderState
413 * The seekable stream decoder state.
415 FLAC__StreamDecoderState FLAC__file_decoder_get_stream_decoder_state(const FLAC__FileDecoder *decoder);
417 /** Get the "MD5 signature checking" flag.
418 * This is inherited from FLAC__SeekableStreamDecoder; see
419 * FLAC__seekable_stream_decoder_get_md5_checking().
421 * \param decoder A decoder instance to query.
423 * \code decoder != NULL \endcode
427 FLAC__bool FLAC__file_decoder_get_md5_checking(const FLAC__FileDecoder *decoder);
429 /** This is inherited from FLAC__SeekableStreamDecoder; see
430 * FLAC__seekable_stream_decoder_get_channels().
432 * \param decoder A decoder instance to query.
434 * \code decoder != NULL \endcode
438 unsigned FLAC__file_decoder_get_channels(const FLAC__FileDecoder *decoder);
440 /** This is inherited from FLAC__SeekableStreamDecoder; see
441 * FLAC__seekable_stream_decoder_get_channel_assignment().
443 * \param decoder A decoder instance to query.
445 * \code decoder != NULL \endcode
446 * \retval FLAC__ChannelAssignment
449 FLAC__ChannelAssignment FLAC__file_decoder_get_channel_assignment(const FLAC__FileDecoder *decoder);
451 /** This is inherited from FLAC__SeekableStreamDecoder; see
452 * FLAC__seekable_stream_decoder_get_bits_per_sample().
454 * \param decoder A decoder instance to query.
456 * \code decoder != NULL \endcode
460 unsigned FLAC__file_decoder_get_bits_per_sample(const FLAC__FileDecoder *decoder);
462 /** This is inherited from FLAC__SeekableStreamDecoder; see
463 * FLAC__seekable_stream_decoder_get_sample_rate().
465 * \param decoder A decoder instance to query.
467 * \code decoder != NULL \endcode
471 unsigned FLAC__file_decoder_get_sample_rate(const FLAC__FileDecoder *decoder);
473 /** This is inherited from FLAC__SeekableStreamDecoder; see
474 * FLAC__seekable_stream_decoder_get_blocksize().
476 * \param decoder A decoder instance to query.
478 * \code decoder != NULL \endcode
482 unsigned FLAC__file_decoder_get_blocksize(const FLAC__FileDecoder *decoder);
484 /** Initialize the decoder instance.
485 * Should be called after FLAC__file_decoder_new() and
486 * FLAC__file_decoder_set_*() but before any of the
487 * FLAC__file_decoder_process_*() functions. Will set and return
488 * the decoder state, which will be FLAC__FILE_DECODER_OK if
489 * initialization succeeded.
491 * \param decoder An uninitialized decoder instance.
493 * \code decoder != NULL \endcode
494 * \retval FLAC__FileDecoderState
495 * \c FLAC__FILE_DECODER_OK if initialization was successful; see
496 * FLAC__FileDecoderState for the meanings of other return values.
498 FLAC__FileDecoderState FLAC__file_decoder_init(FLAC__FileDecoder *decoder);
500 /** Finish the decoding process.
501 * Flushes the decoding buffer, releases resources, resets the decoder
502 * settings to their defaults, and returns the decoder state to
503 * FLAC__FILE_DECODER_UNINITIALIZED.
505 * In the event of a prematurely-terminated decode, it is not strictly
506 * necessary to call this immediately before FLAC__file_decoder_delete()
507 * but it is good practice to match every FLAC__file_decoder_init() with
508 * a FLAC__file_decoder_finish().
510 * \param decoder An uninitialized decoder instance.
512 * \code decoder != NULL \endcode
514 * \c false if MD5 checking is on AND a STREAMINFO block was available
515 * AND the MD5 signature in the STREAMINFO block was non-zero AND the
516 * signature does not match the one computed by the decoder; else
519 FLAC__bool FLAC__file_decoder_finish(FLAC__FileDecoder *decoder);
521 /** This is inherited from FLAC__SeekableStreamDecoder; see
522 * FLAC__seekable_stream_decoder_process_whole_stream().
524 * \param decoder A decoder instance.
526 * \code decoder != NULL \endcode
530 FLAC__bool FLAC__file_decoder_process_whole_file(FLAC__FileDecoder *decoder);
532 /** This is inherited from FLAC__SeekableStreamDecoder; see
533 * FLAC__seekable_stream_decoder_process_metadata().
535 * \param decoder A decoder instance.
537 * \code decoder != NULL \endcode
541 FLAC__bool FLAC__file_decoder_process_metadata(FLAC__FileDecoder *decoder);
543 /** This is inherited from FLAC__SeekableStreamDecoder; see
544 * FLAC__seekable_stream_decoder_process_one_frame().
546 * \param decoder A decoder instance.
548 * \code decoder != NULL \endcode
552 FLAC__bool FLAC__file_decoder_process_one_frame(FLAC__FileDecoder *decoder);
554 /** This is inherited from FLAC__SeekableStreamDecoder; see
555 * FLAC__seekable_stream_decoder_process_remaining_frames().
557 * \param decoder A decoder instance.
559 * \code decoder != NULL \endcode
563 FLAC__bool FLAC__file_decoder_process_remaining_frames(FLAC__FileDecoder *decoder);
565 /** Flush the input and seek to an absolute sample.
566 * This is inherited from FLAC__SeekableStreamDecoder; see
567 * FLAC__seekable_stream_decoder_seek_absolute().
569 * \param decoder A decoder instance.
570 * \param sample The target sample number to seek to.
572 * \code decoder != NULL \endcode
574 * \c true if successful, else \c false.
576 FLAC__bool FLAC__file_decoder_seek_absolute(FLAC__FileDecoder *decoder, FLAC__uint64 sample);