1 /* libFLAC++ - Free Lossless Audio Codec library
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
33 #include "FLAC++/decoder.h"
34 #include "FLAC/assert.h"
37 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
38 #pragma warning ( disable : 4800 )
44 // ------------------------------------------------------------
48 // ------------------------------------------------------------
51 decoder_(::FLAC__stream_decoder_new())
57 (void)::FLAC__stream_decoder_finish(decoder_);
58 ::FLAC__stream_decoder_delete(decoder_);
62 bool Stream::is_valid() const
67 bool Stream::set_ogg_serial_number(long value)
69 FLAC__ASSERT(is_valid());
70 return (bool)::FLAC__stream_decoder_set_ogg_serial_number(decoder_, value);
73 bool Stream::set_md5_checking(bool value)
75 FLAC__ASSERT(is_valid());
76 return (bool)::FLAC__stream_decoder_set_md5_checking(decoder_, value);
79 bool Stream::set_metadata_respond(::FLAC__MetadataType type)
81 FLAC__ASSERT(is_valid());
82 return (bool)::FLAC__stream_decoder_set_metadata_respond(decoder_, type);
85 bool Stream::set_metadata_respond_application(const FLAC__byte id[4])
87 FLAC__ASSERT(is_valid());
88 return (bool)::FLAC__stream_decoder_set_metadata_respond_application(decoder_, id);
91 bool Stream::set_metadata_respond_all()
93 FLAC__ASSERT(is_valid());
94 return (bool)::FLAC__stream_decoder_set_metadata_respond_all(decoder_);
97 bool Stream::set_metadata_ignore(::FLAC__MetadataType type)
99 FLAC__ASSERT(is_valid());
100 return (bool)::FLAC__stream_decoder_set_metadata_ignore(decoder_, type);
103 bool Stream::set_metadata_ignore_application(const FLAC__byte id[4])
105 FLAC__ASSERT(is_valid());
106 return (bool)::FLAC__stream_decoder_set_metadata_ignore_application(decoder_, id);
109 bool Stream::set_metadata_ignore_all()
111 FLAC__ASSERT(is_valid());
112 return (bool)::FLAC__stream_decoder_set_metadata_ignore_all(decoder_);
115 Stream::State Stream::get_state() const
117 FLAC__ASSERT(is_valid());
118 return State(::FLAC__stream_decoder_get_state(decoder_));
121 bool Stream::get_md5_checking() const
123 FLAC__ASSERT(is_valid());
124 return (bool)::FLAC__stream_decoder_get_md5_checking(decoder_);
127 FLAC__uint64 Stream::get_total_samples() const
129 FLAC__ASSERT(is_valid());
130 return ::FLAC__stream_decoder_get_total_samples(decoder_);
133 unsigned Stream::get_channels() const
135 FLAC__ASSERT(is_valid());
136 return ::FLAC__stream_decoder_get_channels(decoder_);
139 ::FLAC__ChannelAssignment Stream::get_channel_assignment() const
141 FLAC__ASSERT(is_valid());
142 return ::FLAC__stream_decoder_get_channel_assignment(decoder_);
145 unsigned Stream::get_bits_per_sample() const
147 FLAC__ASSERT(is_valid());
148 return ::FLAC__stream_decoder_get_bits_per_sample(decoder_);
151 unsigned Stream::get_sample_rate() const
153 FLAC__ASSERT(is_valid());
154 return ::FLAC__stream_decoder_get_sample_rate(decoder_);
157 unsigned Stream::get_blocksize() const
159 FLAC__ASSERT(is_valid());
160 return ::FLAC__stream_decoder_get_blocksize(decoder_);
163 bool Stream::get_decode_position(FLAC__uint64 *position) const
165 FLAC__ASSERT(is_valid());
166 return ::FLAC__stream_decoder_get_decode_position(decoder_, position);
169 ::FLAC__StreamDecoderInitStatus Stream::init()
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);
175 ::FLAC__StreamDecoderInitStatus Stream::init_ogg()
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);
181 bool Stream::finish()
183 FLAC__ASSERT(is_valid());
184 return (bool)::FLAC__stream_decoder_finish(decoder_);
189 FLAC__ASSERT(is_valid());
190 return (bool)::FLAC__stream_decoder_flush(decoder_);
195 FLAC__ASSERT(is_valid());
196 return (bool)::FLAC__stream_decoder_reset(decoder_);
199 bool Stream::process_single()
201 FLAC__ASSERT(is_valid());
202 return (bool)::FLAC__stream_decoder_process_single(decoder_);
205 bool Stream::process_until_end_of_metadata()
207 FLAC__ASSERT(is_valid());
208 return (bool)::FLAC__stream_decoder_process_until_end_of_metadata(decoder_);
211 bool Stream::process_until_end_of_stream()
213 FLAC__ASSERT(is_valid());
214 return (bool)::FLAC__stream_decoder_process_until_end_of_stream(decoder_);
217 bool Stream::skip_single_frame()
219 FLAC__ASSERT(is_valid());
220 return (bool)::FLAC__stream_decoder_skip_single_frame(decoder_);
223 bool Stream::seek_absolute(FLAC__uint64 sample)
225 FLAC__ASSERT(is_valid());
226 return (bool)::FLAC__stream_decoder_seek_absolute(decoder_, sample);
229 ::FLAC__StreamDecoderSeekStatus Stream::seek_callback(FLAC__uint64 absolute_byte_offset)
231 (void)absolute_byte_offset;
232 return ::FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
235 ::FLAC__StreamDecoderTellStatus Stream::tell_callback(FLAC__uint64 *absolute_byte_offset)
237 (void)absolute_byte_offset;
238 return ::FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
241 ::FLAC__StreamDecoderLengthStatus Stream::length_callback(FLAC__uint64 *stream_length)
244 return ::FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
247 bool Stream::eof_callback()
252 void Stream::metadata_callback(const ::FLAC__StreamMetadata *metadata)
257 ::FLAC__StreamDecoderReadStatus Stream::read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
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);
266 ::FLAC__StreamDecoderSeekStatus Stream::seek_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
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);
275 ::FLAC__StreamDecoderTellStatus Stream::tell_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
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);
284 ::FLAC__StreamDecoderLengthStatus Stream::length_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
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);
293 FLAC__bool Stream::eof_callback_(const ::FLAC__StreamDecoder *decoder, void *client_data)
296 FLAC__ASSERT(0 != client_data);
297 Stream *instance = reinterpret_cast<Stream *>(client_data);
298 FLAC__ASSERT(0 != instance);
299 return instance->eof_callback();
302 ::FLAC__StreamDecoderWriteStatus Stream::write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
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);
311 void Stream::metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
314 FLAC__ASSERT(0 != client_data);
315 Stream *instance = reinterpret_cast<Stream *>(client_data);
316 FLAC__ASSERT(0 != instance);
317 instance->metadata_callback(metadata);
320 void Stream::error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data)
323 FLAC__ASSERT(0 != client_data);
324 Stream *instance = reinterpret_cast<Stream *>(client_data);
325 FLAC__ASSERT(0 != instance);
326 instance->error_callback(status);
329 // ------------------------------------------------------------
333 // ------------------------------------------------------------
343 ::FLAC__StreamDecoderInitStatus File::init(FILE *file)
345 FLAC__ASSERT(0 != decoder_);
346 return ::FLAC__stream_decoder_init_FILE(decoder_, file, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this);
349 ::FLAC__StreamDecoderInitStatus File::init(const char *filename)
351 FLAC__ASSERT(0 != decoder_);
352 return ::FLAC__stream_decoder_init_file(decoder_, filename, write_callback_, metadata_callback_, error_callback_, /*client_data=*/(void*)this);
355 ::FLAC__StreamDecoderInitStatus File::init(const std::string &filename)
357 return init(filename.c_str());
360 ::FLAC__StreamDecoderInitStatus File::init_ogg(FILE *file)
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);
366 ::FLAC__StreamDecoderInitStatus File::init_ogg(const char *filename)
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);
372 ::FLAC__StreamDecoderInitStatus File::init_ogg(const std::string &filename)
374 return init_ogg(filename.c_str());
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)
384 (void)buffer, (void)bytes;
386 return ::FLAC__STREAM_DECODER_READ_STATUS_ABORT; // double protection