massive glob of checkins: improved tests, more tests, bugfixes
[flac.git] / src / libFLAC++ / seekable_stream_decoder.cc
1 /* libFLAC++ - Free Lossless Audio Codec library
2  * Copyright (C) 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 #include "FLAC++/decoder.h"
21 #include "FLAC/assert.h"
22
23 namespace FLAC {
24         namespace Decoder {
25
26                 SeekableStream::SeekableStream():
27                 decoder_(::FLAC__seekable_stream_decoder_new())
28                 { }
29
30                 SeekableStream::~SeekableStream()
31                 {
32                         if(0 != decoder_) {
33                                 (void) ::FLAC__seekable_stream_decoder_finish(decoder_);
34                                 ::FLAC__seekable_stream_decoder_delete(decoder_);
35                         }
36                 }
37
38                 bool SeekableStream::is_valid() const
39                 {
40                         return 0 != decoder_;
41                 }
42
43                 bool SeekableStream::set_md5_checking(bool value)
44                 {
45                         FLAC__ASSERT(is_valid());
46                         return (bool)::FLAC__seekable_stream_decoder_set_md5_checking(decoder_, value);
47                 }
48
49                 bool SeekableStream::set_metadata_respond(::FLAC__MetaDataType type)
50                 {
51                         FLAC__ASSERT(is_valid());
52                         return (bool)::FLAC__seekable_stream_decoder_set_metadata_respond(decoder_, type);
53                 }
54
55                 bool SeekableStream::set_metadata_respond_application(const FLAC__byte id[4])
56                 {
57                         FLAC__ASSERT(is_valid());
58                         return (bool)::FLAC__seekable_stream_decoder_set_metadata_respond_application(decoder_, id);
59                 }
60
61                 bool SeekableStream::set_metadata_respond_all()
62                 {
63                         FLAC__ASSERT(is_valid());
64                         return (bool)::FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder_);
65                 }
66
67                 bool SeekableStream::set_metadata_ignore(::FLAC__MetaDataType type)
68                 {
69                         FLAC__ASSERT(is_valid());
70                         return (bool)::FLAC__seekable_stream_decoder_set_metadata_ignore(decoder_, type);
71                 }
72
73                 bool SeekableStream::set_metadata_ignore_application(const FLAC__byte id[4])
74                 {
75                         FLAC__ASSERT(is_valid());
76                         return (bool)::FLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder_, id);
77                 }
78
79                 bool SeekableStream::set_metadata_ignore_all()
80                 {
81                         FLAC__ASSERT(is_valid());
82                         return (bool)::FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder_);
83                 }
84
85                 SeekableStream::State SeekableStream::get_state() const
86                 {
87                         FLAC__ASSERT(is_valid());
88                         return State(::FLAC__seekable_stream_decoder_get_state(decoder_));
89                 }
90
91                 bool SeekableStream::get_md5_checking() const
92                 {
93                         FLAC__ASSERT(is_valid());
94                         return (bool)::FLAC__seekable_stream_decoder_get_md5_checking(decoder_);
95                 }
96
97                 unsigned SeekableStream::get_channels() const
98                 {
99                         FLAC__ASSERT(is_valid());
100                         return ::FLAC__seekable_stream_decoder_get_channels(decoder_);
101                 }
102
103                 ::FLAC__ChannelAssignment SeekableStream::get_channel_assignment() const
104                 {
105                         FLAC__ASSERT(is_valid());
106                         return ::FLAC__seekable_stream_decoder_get_channel_assignment(decoder_);
107                 }
108
109                 unsigned SeekableStream::get_bits_per_sample() const
110                 {
111                         FLAC__ASSERT(is_valid());
112                         return ::FLAC__seekable_stream_decoder_get_bits_per_sample(decoder_);
113                 }
114
115                 unsigned SeekableStream::get_sample_rate() const
116                 {
117                         FLAC__ASSERT(is_valid());
118                         return ::FLAC__seekable_stream_decoder_get_sample_rate(decoder_);
119                 }
120
121                 unsigned SeekableStream::get_blocksize() const
122                 {
123                         FLAC__ASSERT(is_valid());
124                         return ::FLAC__seekable_stream_decoder_get_blocksize(decoder_);
125                 }
126
127                 SeekableStream::State SeekableStream::init()
128                 {
129                         FLAC__ASSERT(is_valid());
130                         ::FLAC__seekable_stream_decoder_set_read_callback(decoder_, read_callback_);
131                         ::FLAC__seekable_stream_decoder_set_seek_callback(decoder_, seek_callback_);
132                         ::FLAC__seekable_stream_decoder_set_tell_callback(decoder_, tell_callback_);
133                         ::FLAC__seekable_stream_decoder_set_length_callback(decoder_, length_callback_);
134                         ::FLAC__seekable_stream_decoder_set_eof_callback(decoder_, eof_callback_);
135                         ::FLAC__seekable_stream_decoder_set_write_callback(decoder_, write_callback_);
136                         ::FLAC__seekable_stream_decoder_set_metadata_callback(decoder_, metadata_callback_);
137                         ::FLAC__seekable_stream_decoder_set_error_callback(decoder_, error_callback_);
138                         ::FLAC__seekable_stream_decoder_set_client_data(decoder_, (void*)this);
139                         return State(::FLAC__seekable_stream_decoder_init(decoder_));
140                 }
141
142                 bool SeekableStream::finish()
143                 {
144                         FLAC__ASSERT(is_valid());
145                         return (bool)::FLAC__seekable_stream_decoder_finish(decoder_);
146                 }
147
148                 bool SeekableStream::flush()
149                 {
150                         FLAC__ASSERT(is_valid());
151                         return (bool)::FLAC__seekable_stream_decoder_flush(decoder_);
152                 }
153
154                 bool SeekableStream::reset()
155                 {
156                         FLAC__ASSERT(is_valid());
157                         return (bool)::FLAC__seekable_stream_decoder_reset(decoder_);
158                 }
159
160                 bool SeekableStream::process_whole_stream()
161                 {
162                         FLAC__ASSERT(is_valid());
163                         return (bool)::FLAC__seekable_stream_decoder_process_whole_stream(decoder_);
164                 }
165
166                 bool SeekableStream::process_metadata()
167                 {
168                         FLAC__ASSERT(is_valid());
169                         return (bool)::FLAC__seekable_stream_decoder_process_metadata(decoder_);
170                 }
171
172                 bool SeekableStream::process_one_frame()
173                 {
174                         FLAC__ASSERT(is_valid());
175                         return (bool)::FLAC__seekable_stream_decoder_process_one_frame(decoder_);
176                 }
177
178                 bool SeekableStream::process_remaining_frames()
179                 {
180                         FLAC__ASSERT(is_valid());
181                         return (bool)::FLAC__seekable_stream_decoder_process_remaining_frames(decoder_);
182                 }
183
184                 bool SeekableStream::seek_absolute(FLAC__uint64 sample)
185                 {
186                         FLAC__ASSERT(is_valid());
187                         return (bool)::FLAC__seekable_stream_decoder_seek_absolute(decoder_, sample);
188                 }
189
190                 FLAC__SeekableStreamDecoderReadStatus SeekableStream::read_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
191                 {
192                         (void) decoder;
193                         FLAC__ASSERT(0 != client_data);
194                         SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
195                         FLAC__ASSERT(0 != instance);
196                         return instance->read_callback(buffer, bytes);
197                 }
198
199                 FLAC__SeekableStreamDecoderSeekStatus SeekableStream::seek_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
200                 {
201                         (void) decoder;
202                         FLAC__ASSERT(0 != client_data);
203                         SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
204                         FLAC__ASSERT(0 != instance);
205                         return instance->seek_callback(absolute_byte_offset);
206                 }
207
208                 FLAC__SeekableStreamDecoderTellStatus SeekableStream::tell_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
209                 {
210                         (void) decoder;
211                         FLAC__ASSERT(0 != client_data);
212                         SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
213                         FLAC__ASSERT(0 != instance);
214                         return instance->tell_callback(absolute_byte_offset);
215                 }
216
217                 FLAC__SeekableStreamDecoderLengthStatus SeekableStream::length_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
218                 {
219                         (void) decoder;
220                         FLAC__ASSERT(0 != client_data);
221                         SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
222                         FLAC__ASSERT(0 != instance);
223                         return instance->length_callback(stream_length);
224                 }
225
226                 FLAC__bool SeekableStream::eof_callback_(const ::FLAC__SeekableStreamDecoder *decoder, void *client_data)
227                 {
228                         (void) decoder;
229                         FLAC__ASSERT(0 != client_data);
230                         SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
231                         FLAC__ASSERT(0 != instance);
232                         return instance->eof_callback();
233                 }
234
235                 FLAC__StreamDecoderWriteStatus SeekableStream::write_callback_(const ::FLAC__SeekableStreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
236                 {
237                         (void) decoder;
238                         FLAC__ASSERT(0 != client_data);
239                         SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
240                         FLAC__ASSERT(0 != instance);
241                         return instance->write_callback(frame, buffer);
242                 }
243
244                 void SeekableStream::metadata_callback_(const ::FLAC__SeekableStreamDecoder *decoder, const ::FLAC__StreamMetaData *metadata, void *client_data)
245                 {
246                         (void) decoder;
247                         FLAC__ASSERT(0 != client_data);
248                         SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
249                         FLAC__ASSERT(0 != instance);
250                         instance->metadata_callback(metadata);
251                 }
252
253                 void SeekableStream::error_callback_(const ::FLAC__SeekableStreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data)
254                 {
255                         (void) decoder;
256                         FLAC__ASSERT(0 != client_data);
257                         SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
258                         FLAC__ASSERT(0 != instance);
259                         instance->error_callback(status);
260                 }
261
262         };
263 };