new funcs for getting underlying decoder layer states
[platform/upstream/flac.git] / include / FLAC / file_decoder.h
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifndef FLAC__FILE_DECODER_H
21 #define FLAC__FILE_DECODER_H
22
23 #include "seekable_stream_decoder.h"
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29
30 /** \file include/FLAC/file_decoder.h
31  *
32  *  \brief
33  *  This module contains the functions which implement the file
34  *  decoder.
35  *
36  *  See the detailed documentation in the
37  *  \link flac_file_decoder file decoder \endlink module.
38  */
39
40 /** \defgroup flac_file_decoder FLAC/file_decoder.h: file decoder interface
41  *  \ingroup flac_decoder
42  *
43  *  \brief
44  *  This module contains the functions which implement the file
45  *  decoder.
46  *
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().
62  *
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.
69  *
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
75  * at that sample.
76  *
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.
83  *
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.
88  *
89  * \note
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.
95  *
96  * \note
97  * FLAC__file_decoder_finish() resets all settings to the constructor
98  * defaults, including the callbacks.
99  *
100  * \{
101  */
102
103
104 /** State values for a FLAC__FileDecoder
105  *
106  *  The decoder's state can be obtained by calling FLAC__file_decoder_get_state().
107  */
108 typedef enum {
109
110     FLAC__FILE_DECODER_OK = 0,
111         /**< The decoder is in the normal OK state. */
112
113         FLAC__FILE_DECODER_END_OF_FILE,
114         /**< The decoder has reached the end of the file. */
115
116     FLAC__FILE_DECODER_ERROR_OPENING_FILE,
117         /**< An error occurred opening the input file. */
118
119     FLAC__FILE_DECODER_MEMORY_ALLOCATION_ERROR,
120         /**< An error occurred allocating memory. */
121
122         FLAC__FILE_DECODER_SEEK_ERROR,
123         /**< An error occurred while seeking. */
124
125         FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR,
126         /**< An error occurred in the underlying seekable stream decoder. */
127
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
131      * called.
132          */
133
134     FLAC__FILE_DECODER_INVALID_CALLBACK,
135         /**< FLAC__file_decoder_init() was called without all callbacks
136          * being set.
137          */
138
139     FLAC__FILE_DECODER_UNINITIALIZED
140         /**< The decoder is in the uninitialized state. */
141
142 } FLAC__FileDecoderState;
143
144 /** Maps a FLAC__FileDecoderState to a C string.
145  *
146  *  Using a FLAC__FileDecoderState as the index to this array
147  *  will give the string equivalent.  The contents should not be modified.
148  */
149 extern const char * const FLAC__FileDecoderStateString[];
150
151
152 /***********************************************************************
153  *
154  * class FLAC__FileDecoder : public FLAC__StreamDecoder
155  *
156  ***********************************************************************/
157
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
162  *  description.
163  */
164 typedef struct {
165         struct FLAC__FileDecoderProtected *protected_; /* avoid the C++ keyword 'protected' */
166         struct FLAC__FileDecoderPrivate *private_; /* avoid the C++ keyword 'private' */
167 } FLAC__FileDecoder;
168
169
170 /***********************************************************************
171  *
172  * Class constructor/destructor
173  *
174  ***********************************************************************/
175
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.
179  *
180  * \retval FLAC__FileDecoder*
181  *    \c NULL if there was an error allocating memory, else the new instance.
182  */
183 FLAC__FileDecoder *FLAC__file_decoder_new();
184
185 /** Free a decoder instance.  Deletes the object pointed to by \a decoder.
186  *
187  * \param decoder  A pointer to an existing decoder.
188  * \assert
189  *    \code decoder != NULL \endcode
190  */
191 void FLAC__file_decoder_delete(FLAC__FileDecoder *decoder);
192
193
194 /***********************************************************************
195  *
196  * Public class method prototypes
197  *
198  ***********************************************************************/
199
200 /** Set the "MD5 signature checking" flag.
201  *  This is inherited from FLAC__SeekableStreamDecoder; see
202  *  FLAC__seekable_stream_decoder_set_md5_checking().
203  *
204  * \default \c false
205  * \param  decoder  A decoder instance to set.
206  * \param  value    See above.
207  * \assert
208  *    \code decoder != NULL \endcode
209  * \retval FLAC__bool
210  *    \c false if the decoder is already initialized, else \c true.
211  */
212 FLAC__bool FLAC__file_decoder_set_md5_checking(FLAC__FileDecoder *decoder, FLAC__bool value);
213
214 /** Set the input file name to decode.
215  *
216  * \default \c "-"
217  * \param  decoder  A decoder instance to set.
218  * \param  value    The input file name, or "-" for \c stdin.
219  * \assert
220  *    \code decoder != NULL \endcode
221  *    \code value != NULL \endcode
222  * \retval FLAC__bool
223  *    \c false if the decoder is already initialized, or there was a memory
224  *    allocation error, else \c true.
225  */
226 FLAC__bool FLAC__file_decoder_set_filename(FLAC__FileDecoder *decoder, const char *value);
227
228 /** Set the write callback.
229  *  This is inherited from FLAC__SeekableStreamDecoder; see
230  *  FLAC__seekable_stream_decoder_set_write_callback().
231  *
232  * \note
233  * The callback is mandatory and must be set before initialization.
234  *
235  * \default \c NULL
236  * \param  decoder  A decoder instance to set.
237  * \param  value    See above.
238  * \assert
239  *    \code decoder != NULL \endcode
240  *    \code value != NULL \endcode
241  * \retval FLAC__bool
242  *    \c false if the decoder is already initialized, else \c true.
243  */
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));
245
246 /** Set the metadata callback.
247  *  This is inherited from FLAC__SeekableStreamDecoder; see
248  *  FLAC__seekable_stream_decoder_set_metadata_callback().
249  *
250  * \note
251  * The callback is mandatory and must be set before initialization.
252  *
253  * \default \c NULL
254  * \param  decoder  A decoder instance to set.
255  * \param  value    See above.
256  * \assert
257  *    \code decoder != NULL \endcode
258  *    \code value != NULL \endcode
259  * \retval FLAC__bool
260  *    \c false if the decoder is already initialized, else \c true.
261  */
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));
263
264 /** Set the error callback.
265  *  This is inherited from FLAC__SeekableStreamDecoder; see
266  *  FLAC__seekable_stream_decoder_set_error_callback().
267  *
268  * \note
269  * The callback is mandatory and must be set before initialization.
270  *
271  * \default \c NULL
272  * \param  decoder  A decoder instance to set.
273  * \param  value    See above.
274  * \assert
275  *    \code decoder != NULL \endcode
276  *    \code value != NULL \endcode
277  * \retval FLAC__bool
278  *    \c false if the decoder is already initialized, else \c true.
279  */
280 FLAC__bool FLAC__file_decoder_set_error_callback(FLAC__FileDecoder *decoder, void (*value)(const FLAC__FileDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data));
281
282 /** Set the client data to be passed back to callbacks.
283  *  This value will be supplied to callbacks in their \a client_data
284  *  argument.
285  *
286  * \default \c NULL
287  * \param  decoder  An decoder instance to set.
288  * \param  value    See above.
289  * \assert
290  *    \code decoder != NULL \endcode
291  * \retval FLAC__bool
292  *    \c false if the decoder is already initialized, else \c true.
293  */
294 FLAC__bool FLAC__file_decoder_set_client_data(FLAC__FileDecoder *decoder, void *value);
295
296 /** This is inherited from FLAC__SeekableStreamDecoder; see
297  *  FLAC__seekable_stream_decoder_set_metadata_respond().
298  *
299  * \default By default, only the \c STREAMINFO block is returned via the
300  *          metadata callback.
301  * \param  decoder  A decoder instance to set.
302  * \param  type     See above.
303  * \assert
304  *    \code decoder != NULL \endcode
305  *    \a type is valid
306  * \retval FLAC__bool
307  *    \c false if the decoder is already initialized, else \c true.
308  */
309 FLAC__bool FLAC__file_decoder_set_metadata_respond(FLAC__FileDecoder *decoder, FLAC__MetadataType type);
310
311 /** This is inherited from FLAC__SeekableStreamDecoder; see
312  *  FLAC__seekable_stream_decoder_set_metadata_respond_application().
313  *
314  * \default By default, only the \c STREAMINFO block is returned via the
315  *          metadata callback.
316  * \param  decoder  A decoder instance to set.
317  * \param  id       See above.
318  * \assert
319  *    \code decoder != NULL \endcode
320  *    \code id != NULL \endcode
321  * \retval FLAC__bool
322  *    \c false if the decoder is already initialized, else \c true.
323  */
324 FLAC__bool FLAC__file_decoder_set_metadata_respond_application(FLAC__FileDecoder *decoder, const FLAC__byte id[4]);
325
326 /** This is inherited from FLAC__SeekableStreamDecoder; see
327  *  FLAC__seekable_stream_decoder_set_metadata_respond_all().
328  *
329  * \default By default, only the \c STREAMINFO block is returned via the
330  *          metadata callback.
331  * \param  decoder  A decoder instance to set.
332  * \assert
333  *    \code decoder != NULL \endcode
334  * \retval FLAC__bool
335  *    \c false if the decoder is already initialized, else \c true.
336  */
337 FLAC__bool FLAC__file_decoder_set_metadata_respond_all(FLAC__FileDecoder *decoder);
338
339 /** This is inherited from FLAC__SeekableStreamDecoder; see
340  *  FLAC__seekable_stream_decoder_set_metadata_ignore().
341  *
342  * \default By default, only the \c STREAMINFO block is returned via the
343  *          metadata callback.
344  * \param  decoder  A decoder instance to set.
345  * \param  type     See above.
346  * \assert
347  *    \code decoder != NULL \endcode
348  *    \a type is valid
349  * \retval FLAC__bool
350  *    \c false if the decoder is already initialized, else \c true.
351  */
352 FLAC__bool FLAC__file_decoder_set_metadata_ignore(FLAC__FileDecoder *decoder, FLAC__MetadataType type);
353
354 /** This is inherited from FLAC__SeekableStreamDecoder; see
355  *  FLAC__seekable_stream_decoder_set_metadata_ignore_application().
356  *
357  * \default By default, only the \c STREAMINFO block is returned via the
358  *          metadata callback.
359  * \param  decoder  A decoder instance to set.
360  * \param  id       See above.
361  * \assert
362  *    \code decoder != NULL \endcode
363  *    \code id != NULL \endcode
364  * \retval FLAC__bool
365  *    \c false if the decoder is already initialized, else \c true.
366  */
367 FLAC__bool FLAC__file_decoder_set_metadata_ignore_application(FLAC__FileDecoder *decoder, const FLAC__byte id[4]);
368
369 /** This is inherited from FLAC__SeekableStreamDecoder; see
370  *  FLAC__seekable_stream_decoder_set_metadata_ignore_all().
371  *
372  * \default By default, only the \c STREAMINFO block is returned via the
373  *          metadata callback.
374  * \param  decoder  A decoder instance to set.
375  * \assert
376  *    \code decoder != NULL \endcode
377  * \retval FLAC__bool
378  *    \c false if the decoder is already initialized, else \c true.
379  */
380 FLAC__bool FLAC__file_decoder_set_metadata_ignore_all(FLAC__FileDecoder *decoder);
381
382 /** Get the current decoder state.
383  *
384  * \param  decoder  A decoder instance to query.
385  * \assert
386  *    \code decoder != NULL \endcode
387  * \retval FLAC__FileDecoderState
388  *    The current decoder state.
389  */
390 FLAC__FileDecoderState FLAC__file_decoder_get_state(const FLAC__FileDecoder *decoder);
391
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.
395  *
396  * \param  decoder  An decoder instance to query.
397  * \assert
398  *    \code decoder != NULL \endcode
399  * \retval FLAC__SeekableStreamDecoderState
400  *    The seekable stream decoder state.
401  */
402 FLAC__SeekableStreamDecoderState FLAC__file_decoder_get_seekable_stream_decoder_state(const FLAC__FileDecoder *decoder);
403
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.
408  *
409  * \param  decoder  An decoder instance to query.
410  * \assert
411  *    \code decoder != NULL \endcode
412  * \retval FLAC__SeekableStreamDecoderState
413  *    The seekable stream decoder state.
414  */
415 FLAC__StreamDecoderState FLAC__file_decoder_get_stream_decoder_state(const FLAC__FileDecoder *decoder);
416
417 /** Get the "MD5 signature checking" flag.
418  *  This is inherited from FLAC__SeekableStreamDecoder; see
419  *  FLAC__seekable_stream_decoder_get_md5_checking().
420  *
421  * \param  decoder  A decoder instance to query.
422  * \assert
423  *    \code decoder != NULL \endcode
424  * \retval FLAC__bool
425  *    See above.
426  */
427 FLAC__bool FLAC__file_decoder_get_md5_checking(const FLAC__FileDecoder *decoder);
428
429 /** This is inherited from FLAC__SeekableStreamDecoder; see
430  *  FLAC__seekable_stream_decoder_get_channels().
431  *
432  * \param  decoder  A decoder instance to query.
433  * \assert
434  *    \code decoder != NULL \endcode
435  * \retval unsigned
436  *    See above.
437  */
438 unsigned FLAC__file_decoder_get_channels(const FLAC__FileDecoder *decoder);
439
440 /** This is inherited from FLAC__SeekableStreamDecoder; see
441  *  FLAC__seekable_stream_decoder_get_channel_assignment().
442  *
443  * \param  decoder  A decoder instance to query.
444  * \assert
445  *    \code decoder != NULL \endcode
446  * \retval FLAC__ChannelAssignment
447  *    See above.
448  */
449 FLAC__ChannelAssignment FLAC__file_decoder_get_channel_assignment(const FLAC__FileDecoder *decoder);
450
451 /** This is inherited from FLAC__SeekableStreamDecoder; see
452  *  FLAC__seekable_stream_decoder_get_bits_per_sample().
453  *
454  * \param  decoder  A decoder instance to query.
455  * \assert
456  *    \code decoder != NULL \endcode
457  * \retval unsigned
458  *    See above.
459  */
460 unsigned FLAC__file_decoder_get_bits_per_sample(const FLAC__FileDecoder *decoder);
461
462 /** This is inherited from FLAC__SeekableStreamDecoder; see
463  *  FLAC__seekable_stream_decoder_get_sample_rate().
464  *
465  * \param  decoder  A decoder instance to query.
466  * \assert
467  *    \code decoder != NULL \endcode
468  * \retval unsigned
469  *    See above.
470  */
471 unsigned FLAC__file_decoder_get_sample_rate(const FLAC__FileDecoder *decoder);
472
473 /** This is inherited from FLAC__SeekableStreamDecoder; see
474  *  FLAC__seekable_stream_decoder_get_blocksize().
475  *
476  * \param  decoder  A decoder instance to query.
477  * \assert
478  *    \code decoder != NULL \endcode
479  * \retval unsigned
480  *    See above.
481  */
482 unsigned FLAC__file_decoder_get_blocksize(const FLAC__FileDecoder *decoder);
483
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.
490  *
491  * \param  decoder  An uninitialized decoder instance.
492  * \assert
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.
497  */
498 FLAC__FileDecoderState FLAC__file_decoder_init(FLAC__FileDecoder *decoder);
499
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.
504  *
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().
509  *
510  * \param  decoder  An uninitialized decoder instance.
511  * \assert
512  *    \code decoder != NULL \endcode
513  * \retval FLAC__bool
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
517  *    \c true.
518  */
519 FLAC__bool FLAC__file_decoder_finish(FLAC__FileDecoder *decoder);
520
521 /** This is inherited from FLAC__SeekableStreamDecoder; see
522  *  FLAC__seekable_stream_decoder_process_whole_stream().
523  *
524  * \param  decoder  A decoder instance.
525  * \assert
526  *    \code decoder != NULL \endcode
527  * \retval FLAC__bool
528  *    See above.
529  */
530 FLAC__bool FLAC__file_decoder_process_whole_file(FLAC__FileDecoder *decoder);
531
532 /** This is inherited from FLAC__SeekableStreamDecoder; see
533  *  FLAC__seekable_stream_decoder_process_metadata().
534  *
535  * \param  decoder  A decoder instance.
536  * \assert
537  *    \code decoder != NULL \endcode
538  * \retval FLAC__bool
539  *    See above.
540  */
541 FLAC__bool FLAC__file_decoder_process_metadata(FLAC__FileDecoder *decoder);
542
543 /** This is inherited from FLAC__SeekableStreamDecoder; see
544  *  FLAC__seekable_stream_decoder_process_one_frame().
545  *
546  * \param  decoder  A decoder instance.
547  * \assert
548  *    \code decoder != NULL \endcode
549  * \retval FLAC__bool
550  *    See above.
551  */
552 FLAC__bool FLAC__file_decoder_process_one_frame(FLAC__FileDecoder *decoder);
553
554 /** This is inherited from FLAC__SeekableStreamDecoder; see
555  *  FLAC__seekable_stream_decoder_process_remaining_frames().
556  *
557  * \param  decoder  A decoder instance.
558  * \assert
559  *    \code decoder != NULL \endcode
560  * \retval FLAC__bool
561  *    See above.
562  */
563 FLAC__bool FLAC__file_decoder_process_remaining_frames(FLAC__FileDecoder *decoder);
564
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().
568  *
569  * \param  decoder  A decoder instance.
570  * \param  sample   The target sample number to seek to.
571  * \assert
572  *    \code decoder != NULL \endcode
573  * \retval FLAC__bool
574  *    \c true if successful, else \c false.
575  */
576 FLAC__bool FLAC__file_decoder_seek_absolute(FLAC__FileDecoder *decoder, FLAC__uint64 sample);
577
578 /* \} */
579
580 #ifdef __cplusplus
581 }
582 #endif
583
584 #endif