*::State::resolved_as_cstring() is now inlined
[platform/upstream/flac.git] / src / libFLAC++ / stream_decoder.cpp
1 /* libFLAC++ - Free Lossless Audio Codec library
2  * Copyright (C) 2002,2003  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the Xiph.org Foundation nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "FLAC++/decoder.h"
33 #include "FLAC/assert.h"
34
35 namespace FLAC {
36         namespace Decoder {
37
38                 Stream::Stream():
39                 decoder_(::FLAC__stream_decoder_new())
40                 { }
41
42                 Stream::~Stream()
43                 {
44                         if(0 != decoder_) {
45                                 ::FLAC__stream_decoder_finish(decoder_);
46                                 ::FLAC__stream_decoder_delete(decoder_);
47                         }
48                 }
49
50                 bool Stream::is_valid() const
51                 {
52                         return 0 != decoder_;
53                 }
54
55                 bool Stream::set_metadata_respond(::FLAC__MetadataType type)
56                 {
57                         FLAC__ASSERT(is_valid());
58                         return (bool)::FLAC__stream_decoder_set_metadata_respond(decoder_, type);
59                 }
60
61                 bool Stream::set_metadata_respond_application(const FLAC__byte id[4])
62                 {
63                         FLAC__ASSERT(is_valid());
64                         return (bool)::FLAC__stream_decoder_set_metadata_respond_application(decoder_, id);
65                 }
66
67                 bool Stream::set_metadata_respond_all()
68                 {
69                         FLAC__ASSERT(is_valid());
70                         return (bool)::FLAC__stream_decoder_set_metadata_respond_all(decoder_);
71                 }
72
73                 bool Stream::set_metadata_ignore(::FLAC__MetadataType type)
74                 {
75                         FLAC__ASSERT(is_valid());
76                         return (bool)::FLAC__stream_decoder_set_metadata_ignore(decoder_, type);
77                 }
78
79                 bool Stream::set_metadata_ignore_application(const FLAC__byte id[4])
80                 {
81                         FLAC__ASSERT(is_valid());
82                         return (bool)::FLAC__stream_decoder_set_metadata_ignore_application(decoder_, id);
83                 }
84
85                 bool Stream::set_metadata_ignore_all()
86                 {
87                         FLAC__ASSERT(is_valid());
88                         return (bool)::FLAC__stream_decoder_set_metadata_ignore_all(decoder_);
89                 }
90
91                 Stream::State Stream::get_state() const
92                 {
93                         FLAC__ASSERT(is_valid());
94                         return State(::FLAC__stream_decoder_get_state(decoder_));
95                 }
96
97                 unsigned Stream::get_channels() const
98                 {
99                         FLAC__ASSERT(is_valid());
100                         return ::FLAC__stream_decoder_get_channels(decoder_);
101                 }
102
103                 ::FLAC__ChannelAssignment Stream::get_channel_assignment() const
104                 {
105                         FLAC__ASSERT(is_valid());
106                         return ::FLAC__stream_decoder_get_channel_assignment(decoder_);
107                 }
108
109                 unsigned Stream::get_bits_per_sample() const
110                 {
111                         FLAC__ASSERT(is_valid());
112                         return ::FLAC__stream_decoder_get_bits_per_sample(decoder_);
113                 }
114
115                 unsigned Stream::get_sample_rate() const
116                 {
117                         FLAC__ASSERT(is_valid());
118                         return ::FLAC__stream_decoder_get_sample_rate(decoder_);
119                 }
120
121                 unsigned Stream::get_blocksize() const
122                 {
123                         FLAC__ASSERT(is_valid());
124                         return ::FLAC__stream_decoder_get_blocksize(decoder_);
125                 }
126
127                 Stream::State Stream::init()
128                 {
129                         FLAC__ASSERT(is_valid());
130                         ::FLAC__stream_decoder_set_read_callback(decoder_, read_callback_);
131                         ::FLAC__stream_decoder_set_write_callback(decoder_, write_callback_);
132                         ::FLAC__stream_decoder_set_metadata_callback(decoder_, metadata_callback_);
133                         ::FLAC__stream_decoder_set_error_callback(decoder_, error_callback_);
134                         ::FLAC__stream_decoder_set_client_data(decoder_, (void*)this);
135                         return State(::FLAC__stream_decoder_init(decoder_));
136                 }
137
138                 void Stream::finish()
139                 {
140                         FLAC__ASSERT(is_valid());
141                         ::FLAC__stream_decoder_finish(decoder_);
142                 }
143
144                 bool Stream::flush()
145                 {
146                         FLAC__ASSERT(is_valid());
147                         return (bool)::FLAC__stream_decoder_flush(decoder_);
148                 }
149
150                 bool Stream::reset()
151                 {
152                         FLAC__ASSERT(is_valid());
153                         return (bool)::FLAC__stream_decoder_reset(decoder_);
154                 }
155
156                 bool Stream::process_single()
157                 {
158                         FLAC__ASSERT(is_valid());
159                         return (bool)::FLAC__stream_decoder_process_single(decoder_);
160                 }
161
162                 bool Stream::process_until_end_of_metadata()
163                 {
164                         FLAC__ASSERT(is_valid());
165                         return (bool)::FLAC__stream_decoder_process_until_end_of_metadata(decoder_);
166                 }
167
168                 bool Stream::process_until_end_of_stream()
169                 {
170                         FLAC__ASSERT(is_valid());
171                         return (bool)::FLAC__stream_decoder_process_until_end_of_stream(decoder_);
172                 }
173
174                 ::FLAC__StreamDecoderReadStatus Stream::read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
175                 {
176                         (void)decoder;
177                         FLAC__ASSERT(0 != client_data);
178                         Stream *instance = reinterpret_cast<Stream *>(client_data);
179                         FLAC__ASSERT(0 != instance);
180                         return instance->read_callback(buffer, bytes);
181                 }
182
183                 ::FLAC__StreamDecoderWriteStatus Stream::write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
184                 {
185                         (void)decoder;
186                         FLAC__ASSERT(0 != client_data);
187                         Stream *instance = reinterpret_cast<Stream *>(client_data);
188                         FLAC__ASSERT(0 != instance);
189                         return instance->write_callback(frame, buffer);
190                 }
191
192                 void Stream::metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
193                 {
194                         (void)decoder;
195                         FLAC__ASSERT(0 != client_data);
196                         Stream *instance = reinterpret_cast<Stream *>(client_data);
197                         FLAC__ASSERT(0 != instance);
198                         instance->metadata_callback(metadata);
199                 }
200
201                 void Stream::error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data)
202                 {
203                         (void)decoder;
204                         FLAC__ASSERT(0 != client_data);
205                         Stream *instance = reinterpret_cast<Stream *>(client_data);
206                         FLAC__ASSERT(0 != instance);
207                         instance->error_callback(status);
208                 }
209
210         };
211 };