test/*.sh : Fix MALLOC_PERTURB initialization.
[platform/upstream/flac.git] / src / libFLAC++ / stream_decoder.cpp
1 /* libFLAC++ - Free Lossless Audio Codec library
2  * Copyright (C) 2002-2009  Josh Coalson
3  * Copyright (C) 2011-2013  Xiph.Org Foundation
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 the Xiph.org Foundation 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
23  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION 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 #include "FLAC++/decoder.h"
34 #include "FLAC/assert.h"
35
36 #ifdef _MSC_VER
37 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
38 #pragma warning ( disable : 4800 )
39 #endif
40
41 namespace FLAC {
42         namespace Decoder {
43
44                 // ------------------------------------------------------------
45                 //
46                 // Stream
47                 //
48                 // ------------------------------------------------------------
49
50                 Stream::Stream():
51                 decoder_(::FLAC__stream_decoder_new())
52                 { }
53
54                 Stream::~Stream()
55                 {
56                         if(0 != decoder_) {
57                                 (void)::FLAC__stream_decoder_finish(decoder_);
58                                 ::FLAC__stream_decoder_delete(decoder_);
59                         }
60                 }
61
62                 bool Stream::is_valid() const
63                 {
64                         return 0 != decoder_;
65                 }
66
67                 bool Stream::set_ogg_serial_number(long value)
68                 {
69                         FLAC__ASSERT(is_valid());
70                         return (bool)::FLAC__stream_decoder_set_ogg_serial_number(decoder_, value);
71                 }
72
73                 bool Stream::set_md5_checking(bool value)
74                 {
75                         FLAC__ASSERT(is_valid());
76                         return (bool)::FLAC__stream_decoder_set_md5_checking(decoder_, value);
77                 }
78
79                 bool Stream::set_metadata_respond(::FLAC__MetadataType type)
80                 {
81                         FLAC__ASSERT(is_valid());
82                         return (bool)::FLAC__stream_decoder_set_metadata_respond(decoder_, type);
83                 }
84
85                 bool Stream::set_metadata_respond_application(const FLAC__byte id[4])
86                 {
87                         FLAC__ASSERT(is_valid());
88                         return (bool)::FLAC__stream_decoder_set_metadata_respond_application(decoder_, id);
89                 }
90
91                 bool Stream::set_metadata_respond_all()
92                 {
93                         FLAC__ASSERT(is_valid());
94                         return (bool)::FLAC__stream_decoder_set_metadata_respond_all(decoder_);
95                 }
96
97                 bool Stream::set_metadata_ignore(::FLAC__MetadataType type)
98                 {
99                         FLAC__ASSERT(is_valid());
100                         return (bool)::FLAC__stream_decoder_set_metadata_ignore(decoder_, type);
101                 }
102
103                 bool Stream::set_metadata_ignore_application(const FLAC__byte id[4])
104                 {
105                         FLAC__ASSERT(is_valid());
106                         return (bool)::FLAC__stream_decoder_set_metadata_ignore_application(decoder_, id);
107                 }
108
109                 bool Stream::set_metadata_ignore_all()
110                 {
111                         FLAC__ASSERT(is_valid());
112                         return (bool)::FLAC__stream_decoder_set_metadata_ignore_all(decoder_);
113                 }
114
115                 Stream::State Stream::get_state() const
116                 {
117                         FLAC__ASSERT(is_valid());
118                         return State(::FLAC__stream_decoder_get_state(decoder_));
119                 }
120
121                 bool Stream::get_md5_checking() const
122                 {
123                         FLAC__ASSERT(is_valid());
124                         return (bool)::FLAC__stream_decoder_get_md5_checking(decoder_);
125                 }
126
127                 FLAC__uint64 Stream::get_total_samples() const
128                 {
129                         FLAC__ASSERT(is_valid());
130                         return ::FLAC__stream_decoder_get_total_samples(decoder_);
131                 }
132
133                 unsigned Stream::get_channels() const
134                 {
135                         FLAC__ASSERT(is_valid());
136                         return ::FLAC__stream_decoder_get_channels(decoder_);
137                 }
138
139                 ::FLAC__ChannelAssignment Stream::get_channel_assignment() const
140                 {
141                         FLAC__ASSERT(is_valid());
142                         return ::FLAC__stream_decoder_get_channel_assignment(decoder_);
143                 }
144
145                 unsigned Stream::get_bits_per_sample() const
146                 {
147                         FLAC__ASSERT(is_valid());
148                         return ::FLAC__stream_decoder_get_bits_per_sample(decoder_);
149                 }
150
151                 unsigned Stream::get_sample_rate() const
152                 {
153                         FLAC__ASSERT(is_valid());
154                         return ::FLAC__stream_decoder_get_sample_rate(decoder_);
155                 }
156
157                 unsigned Stream::get_blocksize() const
158                 {
159                         FLAC__ASSERT(is_valid());
160                         return ::FLAC__stream_decoder_get_blocksize(decoder_);
161                 }
162
163                 bool Stream::get_decode_position(FLAC__uint64 *position) const
164                 {
165                         FLAC__ASSERT(is_valid());
166                         return ::FLAC__stream_decoder_get_decode_position(decoder_, position);
167                 }
168
169                 ::FLAC__StreamDecoderInitStatus Stream::init()
170                 {
171                         FLAC__ASSERT(is_valid());
172                         return ::FLAC__stream_decoder_init_stream(decoder_, read_callback_, seek_callback_, tell_callback_, length_callback_, eof_callback_, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this);
173                 }
174
175                 ::FLAC__StreamDecoderInitStatus Stream::init_ogg()
176                 {
177                         FLAC__ASSERT(is_valid());
178                         return ::FLAC__stream_decoder_init_ogg_stream(decoder_, read_callback_, seek_callback_, tell_callback_, length_callback_, eof_callback_, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this);
179                 }
180
181                 bool Stream::finish()
182                 {
183                         FLAC__ASSERT(is_valid());
184                         return (bool)::FLAC__stream_decoder_finish(decoder_);
185                 }
186
187                 bool Stream::flush()
188                 {
189                         FLAC__ASSERT(is_valid());
190                         return (bool)::FLAC__stream_decoder_flush(decoder_);
191                 }
192
193                 bool Stream::reset()
194                 {
195                         FLAC__ASSERT(is_valid());
196                         return (bool)::FLAC__stream_decoder_reset(decoder_);
197                 }
198
199                 bool Stream::process_single()
200                 {
201                         FLAC__ASSERT(is_valid());
202                         return (bool)::FLAC__stream_decoder_process_single(decoder_);
203                 }
204
205                 bool Stream::process_until_end_of_metadata()
206                 {
207                         FLAC__ASSERT(is_valid());
208                         return (bool)::FLAC__stream_decoder_process_until_end_of_metadata(decoder_);
209                 }
210
211                 bool Stream::process_until_end_of_stream()
212                 {
213                         FLAC__ASSERT(is_valid());
214                         return (bool)::FLAC__stream_decoder_process_until_end_of_stream(decoder_);
215                 }
216
217                 bool Stream::skip_single_frame()
218                 {
219                         FLAC__ASSERT(is_valid());
220                         return (bool)::FLAC__stream_decoder_skip_single_frame(decoder_);
221                 }
222
223                 bool Stream::seek_absolute(FLAC__uint64 sample)
224                 {
225                         FLAC__ASSERT(is_valid());
226                         return (bool)::FLAC__stream_decoder_seek_absolute(decoder_, sample);
227                 }
228
229                 ::FLAC__StreamDecoderSeekStatus Stream::seek_callback(FLAC__uint64 absolute_byte_offset)
230                 {
231                         (void)absolute_byte_offset;
232                         return ::FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
233                 }
234
235                 ::FLAC__StreamDecoderTellStatus Stream::tell_callback(FLAC__uint64 *absolute_byte_offset)
236                 {
237                         (void)absolute_byte_offset;
238                         return ::FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
239                 }
240
241                 ::FLAC__StreamDecoderLengthStatus Stream::length_callback(FLAC__uint64 *stream_length)
242                 {
243                         (void)stream_length;
244                         return ::FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
245                 }
246
247                 bool Stream::eof_callback()
248                 {
249                         return false;
250                 }
251
252                 void Stream::metadata_callback(const ::FLAC__StreamMetadata *metadata)
253                 {
254                         (void)metadata;
255                 }
256
257                 ::FLAC__StreamDecoderReadStatus Stream::read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
258                 {
259                         (void)decoder;
260                         FLAC__ASSERT(0 != client_data);
261                         Stream *instance = reinterpret_cast<Stream *>(client_data);
262                         FLAC__ASSERT(0 != instance);
263                         return instance->read_callback(buffer, bytes);
264                 }
265
266                 ::FLAC__StreamDecoderSeekStatus Stream::seek_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
267                 {
268                         (void) decoder;
269                         FLAC__ASSERT(0 != client_data);
270                         Stream *instance = reinterpret_cast<Stream *>(client_data);
271                         FLAC__ASSERT(0 != instance);
272                         return instance->seek_callback(absolute_byte_offset);
273                 }
274
275                 ::FLAC__StreamDecoderTellStatus Stream::tell_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
276                 {
277                         (void) decoder;
278                         FLAC__ASSERT(0 != client_data);
279                         Stream *instance = reinterpret_cast<Stream *>(client_data);
280                         FLAC__ASSERT(0 != instance);
281                         return instance->tell_callback(absolute_byte_offset);
282                 }
283
284                 ::FLAC__StreamDecoderLengthStatus Stream::length_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
285                 {
286                         (void) decoder;
287                         FLAC__ASSERT(0 != client_data);
288                         Stream *instance = reinterpret_cast<Stream *>(client_data);
289                         FLAC__ASSERT(0 != instance);
290                         return instance->length_callback(stream_length);
291                 }
292
293                 FLAC__bool Stream::eof_callback_(const ::FLAC__StreamDecoder *decoder, void *client_data)
294                 {
295                         (void) decoder;
296                         FLAC__ASSERT(0 != client_data);
297                         Stream *instance = reinterpret_cast<Stream *>(client_data);
298                         FLAC__ASSERT(0 != instance);
299                         return instance->eof_callback();
300                 }
301
302                 ::FLAC__StreamDecoderWriteStatus Stream::write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
303                 {
304                         (void)decoder;
305                         FLAC__ASSERT(0 != client_data);
306                         Stream *instance = reinterpret_cast<Stream *>(client_data);
307                         FLAC__ASSERT(0 != instance);
308                         return instance->write_callback(frame, buffer);
309                 }
310
311                 void Stream::metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
312                 {
313                         (void)decoder;
314                         FLAC__ASSERT(0 != client_data);
315                         Stream *instance = reinterpret_cast<Stream *>(client_data);
316                         FLAC__ASSERT(0 != instance);
317                         instance->metadata_callback(metadata);
318                 }
319
320                 void Stream::error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data)
321                 {
322                         (void)decoder;
323                         FLAC__ASSERT(0 != client_data);
324                         Stream *instance = reinterpret_cast<Stream *>(client_data);
325                         FLAC__ASSERT(0 != instance);
326                         instance->error_callback(status);
327                 }
328
329                 // ------------------------------------------------------------
330                 //
331                 // File
332                 //
333                 // ------------------------------------------------------------
334
335                 File::File():
336                         Stream()
337                 { }
338
339                 File::~File()
340                 {
341                 }
342
343                 ::FLAC__StreamDecoderInitStatus File::init(FILE *file)
344                 {
345                         FLAC__ASSERT(0 != decoder_);
346                         return ::FLAC__stream_decoder_init_FILE(decoder_, file, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this);
347                 }
348
349                 ::FLAC__StreamDecoderInitStatus File::init(const char *filename)
350                 {
351                         FLAC__ASSERT(0 != decoder_);
352                         return ::FLAC__stream_decoder_init_file(decoder_, filename, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this);
353                 }
354
355                 ::FLAC__StreamDecoderInitStatus File::init(const std::string &filename)
356                 {
357                         return init(filename.c_str());
358                 }
359
360                 ::FLAC__StreamDecoderInitStatus File::init_ogg(FILE *file)
361                 {
362                         FLAC__ASSERT(0 != decoder_);
363                         return ::FLAC__stream_decoder_init_ogg_FILE(decoder_, file, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this);
364                 }
365
366                 ::FLAC__StreamDecoderInitStatus File::init_ogg(const char *filename)
367                 {
368                         FLAC__ASSERT(0 != decoder_);
369                         return ::FLAC__stream_decoder_init_ogg_file(decoder_, filename, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this);
370                 }
371
372                 ::FLAC__StreamDecoderInitStatus File::init_ogg(const std::string &filename)
373                 {
374                         return init_ogg(filename.c_str());
375                 }
376
377                 // This is a dummy to satisfy the pure virtual from Stream; the
378                 // read callback will never be called since we are initializing
379                 // with FLAC__stream_decoder_init_FILE() or
380                 // FLAC__stream_decoder_init_file() and those supply the read
381                 // callback internally.
382                 ::FLAC__StreamDecoderReadStatus File::read_callback(FLAC__byte buffer[], size_t *bytes)
383                 {
384                         (void)buffer, (void)bytes;
385                         FLAC__ASSERT(false);
386                         return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT; // double protection
387                 }
388
389         }
390 }