565a7059648f45d3edfeba60907c998bb492e5e6
[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 /* @@@@ document */
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);
173
174
175 /***********************************************************************
176  *
177  * Class constructor/destructor
178  *
179  ***********************************************************************/
180
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.
184  *
185  * \retval FLAC__FileDecoder*
186  *    \c NULL if there was an error allocating memory, else the new instance.
187  */
188 FLAC__FileDecoder *FLAC__file_decoder_new();
189
190 /** Free a decoder instance.  Deletes the object pointed to by \a decoder.
191  *
192  * \param decoder  A pointer to an existing decoder.
193  * \assert
194  *    \code decoder != NULL \endcode
195  */
196 void FLAC__file_decoder_delete(FLAC__FileDecoder *decoder);
197
198
199 /***********************************************************************
200  *
201  * Public class method prototypes
202  *
203  ***********************************************************************/
204
205 /** Set the "MD5 signature checking" flag.
206  *  This is inherited from FLAC__SeekableStreamDecoder; see
207  *  FLAC__seekable_stream_decoder_set_md5_checking().
208  *
209  * \default \c false
210  * \param  decoder  A decoder instance to set.
211  * \param  value    See above.
212  * \assert
213  *    \code decoder != NULL \endcode
214  * \retval FLAC__bool
215  *    \c false if the decoder is already initialized, else \c true.
216  */
217 FLAC__bool FLAC__file_decoder_set_md5_checking(FLAC__FileDecoder *decoder, FLAC__bool value);
218
219 /** Set the input file name to decode.
220  *
221  * \default \c "-"
222  * \param  decoder  A decoder instance to set.
223  * \param  value    The input file name, or "-" for \c stdin.
224  * \assert
225  *    \code decoder != NULL \endcode
226  *    \code value != NULL \endcode
227  * \retval FLAC__bool
228  *    \c false if the decoder is already initialized, or there was a memory
229  *    allocation error, else \c true.
230  */
231 FLAC__bool FLAC__file_decoder_set_filename(FLAC__FileDecoder *decoder, const char *value);
232
233 /** Set the write callback.
234  *  This is inherited from FLAC__SeekableStreamDecoder; see
235  *  FLAC__seekable_stream_decoder_set_write_callback().
236  *
237  * \note
238  * The callback is mandatory and must be set before initialization.
239  *
240  * \default \c NULL
241  * \param  decoder  A decoder instance to set.
242  * \param  value    See above.
243  * \assert
244  *    \code decoder != NULL \endcode
245  *    \code value != NULL \endcode
246  * \retval FLAC__bool
247  *    \c false if the decoder is already initialized, else \c true.
248  */
249 FLAC__bool FLAC__file_decoder_set_write_callback(FLAC__FileDecoder *decoder, FLAC__FileDecoderWriteCallback value);
250
251 /** Set the metadata callback.
252  *  This is inherited from FLAC__SeekableStreamDecoder; see
253  *  FLAC__seekable_stream_decoder_set_metadata_callback().
254  *
255  * \note
256  * The callback is mandatory and must be set before initialization.
257  *
258  * \default \c NULL
259  * \param  decoder  A decoder instance to set.
260  * \param  value    See above.
261  * \assert
262  *    \code decoder != NULL \endcode
263  *    \code value != NULL \endcode
264  * \retval FLAC__bool
265  *    \c false if the decoder is already initialized, else \c true.
266  */
267 FLAC__bool FLAC__file_decoder_set_metadata_callback(FLAC__FileDecoder *decoder, FLAC__FileDecoderMetadataCallback value);
268
269 /** Set the error callback.
270  *  This is inherited from FLAC__SeekableStreamDecoder; see
271  *  FLAC__seekable_stream_decoder_set_error_callback().
272  *
273  * \note
274  * The callback is mandatory and must be set before initialization.
275  *
276  * \default \c NULL
277  * \param  decoder  A decoder instance to set.
278  * \param  value    See above.
279  * \assert
280  *    \code decoder != NULL \endcode
281  *    \code value != NULL \endcode
282  * \retval FLAC__bool
283  *    \c false if the decoder is already initialized, else \c true.
284  */
285 FLAC__bool FLAC__file_decoder_set_error_callback(FLAC__FileDecoder *decoder, FLAC__FileDecoderErrorCallback value);
286
287 /** Set the client data to be passed back to callbacks.
288  *  This value will be supplied to callbacks in their \a client_data
289  *  argument.
290  *
291  * \default \c NULL
292  * \param  decoder  An decoder instance to set.
293  * \param  value    See above.
294  * \assert
295  *    \code decoder != NULL \endcode
296  * \retval FLAC__bool
297  *    \c false if the decoder is already initialized, else \c true.
298  */
299 FLAC__bool FLAC__file_decoder_set_client_data(FLAC__FileDecoder *decoder, void *value);
300
301 /** This is inherited from FLAC__SeekableStreamDecoder; see
302  *  FLAC__seekable_stream_decoder_set_metadata_respond().
303  *
304  * \default By default, only the \c STREAMINFO block is returned via the
305  *          metadata callback.
306  * \param  decoder  A decoder instance to set.
307  * \param  type     See above.
308  * \assert
309  *    \code decoder != NULL \endcode
310  *    \a type is valid
311  * \retval FLAC__bool
312  *    \c false if the decoder is already initialized, else \c true.
313  */
314 FLAC__bool FLAC__file_decoder_set_metadata_respond(FLAC__FileDecoder *decoder, FLAC__MetadataType type);
315
316 /** This is inherited from FLAC__SeekableStreamDecoder; see
317  *  FLAC__seekable_stream_decoder_set_metadata_respond_application().
318  *
319  * \default By default, only the \c STREAMINFO block is returned via the
320  *          metadata callback.
321  * \param  decoder  A decoder instance to set.
322  * \param  id       See above.
323  * \assert
324  *    \code decoder != NULL \endcode
325  *    \code id != NULL \endcode
326  * \retval FLAC__bool
327  *    \c false if the decoder is already initialized, else \c true.
328  */
329 FLAC__bool FLAC__file_decoder_set_metadata_respond_application(FLAC__FileDecoder *decoder, const FLAC__byte id[4]);
330
331 /** This is inherited from FLAC__SeekableStreamDecoder; see
332  *  FLAC__seekable_stream_decoder_set_metadata_respond_all().
333  *
334  * \default By default, only the \c STREAMINFO block is returned via the
335  *          metadata callback.
336  * \param  decoder  A decoder instance to set.
337  * \assert
338  *    \code decoder != NULL \endcode
339  * \retval FLAC__bool
340  *    \c false if the decoder is already initialized, else \c true.
341  */
342 FLAC__bool FLAC__file_decoder_set_metadata_respond_all(FLAC__FileDecoder *decoder);
343
344 /** This is inherited from FLAC__SeekableStreamDecoder; see
345  *  FLAC__seekable_stream_decoder_set_metadata_ignore().
346  *
347  * \default By default, only the \c STREAMINFO block is returned via the
348  *          metadata callback.
349  * \param  decoder  A decoder instance to set.
350  * \param  type     See above.
351  * \assert
352  *    \code decoder != NULL \endcode
353  *    \a type is valid
354  * \retval FLAC__bool
355  *    \c false if the decoder is already initialized, else \c true.
356  */
357 FLAC__bool FLAC__file_decoder_set_metadata_ignore(FLAC__FileDecoder *decoder, FLAC__MetadataType type);
358
359 /** This is inherited from FLAC__SeekableStreamDecoder; see
360  *  FLAC__seekable_stream_decoder_set_metadata_ignore_application().
361  *
362  * \default By default, only the \c STREAMINFO block is returned via the
363  *          metadata callback.
364  * \param  decoder  A decoder instance to set.
365  * \param  id       See above.
366  * \assert
367  *    \code decoder != NULL \endcode
368  *    \code id != NULL \endcode
369  * \retval FLAC__bool
370  *    \c false if the decoder is already initialized, else \c true.
371  */
372 FLAC__bool FLAC__file_decoder_set_metadata_ignore_application(FLAC__FileDecoder *decoder, const FLAC__byte id[4]);
373
374 /** This is inherited from FLAC__SeekableStreamDecoder; see
375  *  FLAC__seekable_stream_decoder_set_metadata_ignore_all().
376  *
377  * \default By default, only the \c STREAMINFO block is returned via the
378  *          metadata callback.
379  * \param  decoder  A decoder instance to set.
380  * \assert
381  *    \code decoder != NULL \endcode
382  * \retval FLAC__bool
383  *    \c false if the decoder is already initialized, else \c true.
384  */
385 FLAC__bool FLAC__file_decoder_set_metadata_ignore_all(FLAC__FileDecoder *decoder);
386
387 /** Get the current decoder state.
388  *
389  * \param  decoder  A decoder instance to query.
390  * \assert
391  *    \code decoder != NULL \endcode
392  * \retval FLAC__FileDecoderState
393  *    The current decoder state.
394  */
395 FLAC__FileDecoderState FLAC__file_decoder_get_state(const FLAC__FileDecoder *decoder);
396
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.
400  *
401  * \param  decoder  An decoder instance to query.
402  * \assert
403  *    \code decoder != NULL \endcode
404  * \retval FLAC__SeekableStreamDecoderState
405  *    The seekable stream decoder state.
406  */
407 FLAC__SeekableStreamDecoderState FLAC__file_decoder_get_seekable_stream_decoder_state(const FLAC__FileDecoder *decoder);
408
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.
413  *
414  * \param  decoder  An decoder instance to query.
415  * \assert
416  *    \code decoder != NULL \endcode
417  * \retval FLAC__SeekableStreamDecoderState
418  *    The seekable stream decoder state.
419  */
420 FLAC__StreamDecoderState FLAC__file_decoder_get_stream_decoder_state(const FLAC__FileDecoder *decoder);
421
422 /** Get the "MD5 signature checking" flag.
423  *  This is inherited from FLAC__SeekableStreamDecoder; see
424  *  FLAC__seekable_stream_decoder_get_md5_checking().
425  *
426  * \param  decoder  A decoder instance to query.
427  * \assert
428  *    \code decoder != NULL \endcode
429  * \retval FLAC__bool
430  *    See above.
431  */
432 FLAC__bool FLAC__file_decoder_get_md5_checking(const FLAC__FileDecoder *decoder);
433
434 /** This is inherited from FLAC__SeekableStreamDecoder; see
435  *  FLAC__seekable_stream_decoder_get_channels().
436  *
437  * \param  decoder  A decoder instance to query.
438  * \assert
439  *    \code decoder != NULL \endcode
440  * \retval unsigned
441  *    See above.
442  */
443 unsigned FLAC__file_decoder_get_channels(const FLAC__FileDecoder *decoder);
444
445 /** This is inherited from FLAC__SeekableStreamDecoder; see
446  *  FLAC__seekable_stream_decoder_get_channel_assignment().
447  *
448  * \param  decoder  A decoder instance to query.
449  * \assert
450  *    \code decoder != NULL \endcode
451  * \retval FLAC__ChannelAssignment
452  *    See above.
453  */
454 FLAC__ChannelAssignment FLAC__file_decoder_get_channel_assignment(const FLAC__FileDecoder *decoder);
455
456 /** This is inherited from FLAC__SeekableStreamDecoder; see
457  *  FLAC__seekable_stream_decoder_get_bits_per_sample().
458  *
459  * \param  decoder  A decoder instance to query.
460  * \assert
461  *    \code decoder != NULL \endcode
462  * \retval unsigned
463  *    See above.
464  */
465 unsigned FLAC__file_decoder_get_bits_per_sample(const FLAC__FileDecoder *decoder);
466
467 /** This is inherited from FLAC__SeekableStreamDecoder; see
468  *  FLAC__seekable_stream_decoder_get_sample_rate().
469  *
470  * \param  decoder  A decoder instance to query.
471  * \assert
472  *    \code decoder != NULL \endcode
473  * \retval unsigned
474  *    See above.
475  */
476 unsigned FLAC__file_decoder_get_sample_rate(const FLAC__FileDecoder *decoder);
477
478 /** This is inherited from FLAC__SeekableStreamDecoder; see
479  *  FLAC__seekable_stream_decoder_get_blocksize().
480  *
481  * \param  decoder  A decoder instance to query.
482  * \assert
483  *    \code decoder != NULL \endcode
484  * \retval unsigned
485  *    See above.
486  */
487 unsigned FLAC__file_decoder_get_blocksize(const FLAC__FileDecoder *decoder);
488
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.
495  *
496  * \param  decoder  An uninitialized decoder instance.
497  * \assert
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.
502  */
503 FLAC__FileDecoderState FLAC__file_decoder_init(FLAC__FileDecoder *decoder);
504
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.
509  *
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().
514  *
515  * \param  decoder  An uninitialized decoder instance.
516  * \assert
517  *    \code decoder != NULL \endcode
518  * \retval FLAC__bool
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
522  *    \c true.
523  */
524 FLAC__bool FLAC__file_decoder_finish(FLAC__FileDecoder *decoder);
525
526 /** This is inherited from FLAC__SeekableStreamDecoder; see
527  *  FLAC__seekable_stream_decoder_process_single().
528  *
529  * \param  decoder  A decoder instance.
530  * \assert
531  *    \code decoder != NULL \endcode
532  * \retval FLAC__bool
533  *    See above.
534  */
535 FLAC__bool FLAC__file_decoder_process_single(FLAC__FileDecoder *decoder);
536
537 /** This is inherited from FLAC__SeekableStreamDecoder; see
538  *  FLAC__seekable_stream_decoder_process_until_end_of_metadata().
539  *
540  * \param  decoder  A decoder instance.
541  * \assert
542  *    \code decoder != NULL \endcode
543  * \retval FLAC__bool
544  *    See above.
545  */
546 FLAC__bool FLAC__file_decoder_process_until_end_of_metadata(FLAC__FileDecoder *decoder);
547
548 /** This is inherited from FLAC__SeekableStreamDecoder; see
549  *  FLAC__seekable_stream_decoder_process_until_end_of_stream().
550  *
551  * \param  decoder  A decoder instance.
552  * \assert
553  *    \code decoder != NULL \endcode
554  * \retval FLAC__bool
555  *    See above.
556  */
557 FLAC__bool FLAC__file_decoder_process_until_end_of_file(FLAC__FileDecoder *decoder);
558
559 /** This is inherited from FLAC__SeekableStreamDecoder; see
560  *  FLAC__seekable_stream_decoder_process_remaining_frames().
561  *
562  * \param  decoder  A decoder instance.
563  * \assert
564  *    \code decoder != NULL \endcode
565  * \retval FLAC__bool
566  *    See above.
567  */
568 FLAC__bool FLAC__file_decoder_process_remaining_frames(FLAC__FileDecoder *decoder);
569
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().
573  *
574  * \param  decoder  A decoder instance.
575  * \param  sample   The target sample number to seek to.
576  * \assert
577  *    \code decoder != NULL \endcode
578  * \retval FLAC__bool
579  *    \c true if successful, else \c false.
580  */
581 FLAC__bool FLAC__file_decoder_seek_absolute(FLAC__FileDecoder *decoder, FLAC__uint64 sample);
582
583 /* \} */
584
585 #ifdef __cplusplus
586 }
587 #endif
588
589 #endif