Add 2003 to copyright notice
[platform/upstream/flac.git] / src / libFLAC++ / stream_encoder.cpp
1 /* libFLAC++ - Free Lossless Audio Codec library
2  * Copyright (C) 2002,2003  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++/encoder.h"
21 #include "FLAC/assert.h"
22
23 namespace FLAC {
24         namespace Encoder {
25
26                 const char *Stream::State::resolved_as_cstring(const Stream &encoder) const
27                 {
28                         if(state_ == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
29                                 return encoder.get_verify_decoder_state().as_cstring();
30                         else
31                                 return as_cstring();
32                 }
33
34                 Stream::Stream():
35                 encoder_(::FLAC__stream_encoder_new())
36                 { }
37
38                 Stream::~Stream()
39                 {
40                         if(0 != encoder_) {
41                                 ::FLAC__stream_encoder_finish(encoder_);
42                                 ::FLAC__stream_encoder_delete(encoder_);
43                         }
44                 }
45
46                 bool Stream::is_valid() const
47                 {
48                         return 0 != encoder_;
49                 }
50
51                 bool Stream::set_verify(bool value)
52                 {
53                         FLAC__ASSERT(is_valid());
54                         return (bool)::FLAC__stream_encoder_set_verify(encoder_, value);
55                 }
56
57                 bool Stream::set_streamable_subset(bool value)
58                 {
59                         FLAC__ASSERT(is_valid());
60                         return (bool)::FLAC__stream_encoder_set_streamable_subset(encoder_, value);
61                 }
62
63                 bool Stream::set_do_mid_side_stereo(bool value)
64                 {
65                         FLAC__ASSERT(is_valid());
66                         return (bool)::FLAC__stream_encoder_set_do_mid_side_stereo(encoder_, value);
67                 }
68
69                 bool Stream::set_loose_mid_side_stereo(bool value)
70                 {
71                         FLAC__ASSERT(is_valid());
72                         return (bool)::FLAC__stream_encoder_set_loose_mid_side_stereo(encoder_, value);
73                 }
74
75                 bool Stream::set_channels(unsigned value)
76                 {
77                         FLAC__ASSERT(is_valid());
78                         return (bool)::FLAC__stream_encoder_set_channels(encoder_, value);
79                 }
80
81                 bool Stream::set_bits_per_sample(unsigned value)
82                 {
83                         FLAC__ASSERT(is_valid());
84                         return (bool)::FLAC__stream_encoder_set_bits_per_sample(encoder_, value);
85                 }
86
87                 bool Stream::set_sample_rate(unsigned value)
88                 {
89                         FLAC__ASSERT(is_valid());
90                         return (bool)::FLAC__stream_encoder_set_sample_rate(encoder_, value);
91                 }
92
93                 bool Stream::set_blocksize(unsigned value)
94                 {
95                         FLAC__ASSERT(is_valid());
96                         return (bool)::FLAC__stream_encoder_set_blocksize(encoder_, value);
97                 }
98
99                 bool Stream::set_max_lpc_order(unsigned value)
100                 {
101                         FLAC__ASSERT(is_valid());
102                         return (bool)::FLAC__stream_encoder_set_max_lpc_order(encoder_, value);
103                 }
104
105                 bool Stream::set_qlp_coeff_precision(unsigned value)
106                 {
107                         FLAC__ASSERT(is_valid());
108                         return (bool)::FLAC__stream_encoder_set_qlp_coeff_precision(encoder_, value);
109                 }
110
111                 bool Stream::set_do_qlp_coeff_prec_search(bool value)
112                 {
113                         FLAC__ASSERT(is_valid());
114                         return (bool)::FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder_, value);
115                 }
116
117                 bool Stream::set_do_escape_coding(bool value)
118                 {
119                         FLAC__ASSERT(is_valid());
120                         return (bool)::FLAC__stream_encoder_set_do_escape_coding(encoder_, value);
121                 }
122
123                 bool Stream::set_do_exhaustive_model_search(bool value)
124                 {
125                         FLAC__ASSERT(is_valid());
126                         return (bool)::FLAC__stream_encoder_set_do_exhaustive_model_search(encoder_, value);
127                 }
128
129                 bool Stream::set_min_residual_partition_order(unsigned value)
130                 {
131                         FLAC__ASSERT(is_valid());
132                         return (bool)::FLAC__stream_encoder_set_min_residual_partition_order(encoder_, value);
133                 }
134
135                 bool Stream::set_max_residual_partition_order(unsigned value)
136                 {
137                         FLAC__ASSERT(is_valid());
138                         return (bool)::FLAC__stream_encoder_set_max_residual_partition_order(encoder_, value);
139                 }
140
141                 bool Stream::set_rice_parameter_search_dist(unsigned value)
142                 {
143                         FLAC__ASSERT(is_valid());
144                         return (bool)::FLAC__stream_encoder_set_rice_parameter_search_dist(encoder_, value);
145                 }
146
147                 bool Stream::set_total_samples_estimate(FLAC__uint64 value)
148                 {
149                         FLAC__ASSERT(is_valid());
150                         return (bool)::FLAC__stream_encoder_set_total_samples_estimate(encoder_, value);
151                 }
152
153                 bool Stream::set_metadata(::FLAC__StreamMetadata **metadata, unsigned num_blocks)
154                 {
155                         FLAC__ASSERT(is_valid());
156                         return (bool)::FLAC__stream_encoder_set_metadata(encoder_, metadata, num_blocks);
157                 }
158
159                 Stream::State Stream::get_state() const
160                 {
161                         FLAC__ASSERT(is_valid());
162                         return State(::FLAC__stream_encoder_get_state(encoder_));
163                 }
164
165                 Decoder::Stream::State Stream::get_verify_decoder_state() const
166                 {
167                         FLAC__ASSERT(is_valid());
168                         return Decoder::Stream::State(::FLAC__stream_encoder_get_verify_decoder_state(encoder_));
169                 }
170
171                 void Stream::get_verify_decoder_error_stats(FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
172                 {
173                         FLAC__ASSERT(is_valid());
174                         ::FLAC__stream_encoder_get_verify_decoder_error_stats(encoder_, absolute_sample, frame_number, channel, sample, expected, got);
175                 }
176
177                 bool Stream::get_verify() const
178                 {
179                         FLAC__ASSERT(is_valid());
180                         return (bool)::FLAC__stream_encoder_get_verify(encoder_);
181                 }
182
183                 bool Stream::get_streamable_subset() const
184                 {
185                         FLAC__ASSERT(is_valid());
186                         return (bool)::FLAC__stream_encoder_get_streamable_subset(encoder_);
187                 }
188
189                 bool Stream::get_do_mid_side_stereo() const
190                 {
191                         FLAC__ASSERT(is_valid());
192                         return (bool)::FLAC__stream_encoder_get_do_mid_side_stereo(encoder_);
193                 }
194
195                 bool Stream::get_loose_mid_side_stereo() const
196                 {
197                         FLAC__ASSERT(is_valid());
198                         return (bool)::FLAC__stream_encoder_get_loose_mid_side_stereo(encoder_);
199                 }
200
201                 unsigned Stream::get_channels() const
202                 {
203                         FLAC__ASSERT(is_valid());
204                         return ::FLAC__stream_encoder_get_channels(encoder_);
205                 }
206
207                 unsigned Stream::get_bits_per_sample() const
208                 {
209                         FLAC__ASSERT(is_valid());
210                         return ::FLAC__stream_encoder_get_bits_per_sample(encoder_);
211                 }
212
213                 unsigned Stream::get_sample_rate() const
214                 {
215                         FLAC__ASSERT(is_valid());
216                         return ::FLAC__stream_encoder_get_sample_rate(encoder_);
217                 }
218
219                 unsigned Stream::get_blocksize() const
220                 {
221                         FLAC__ASSERT(is_valid());
222                         return ::FLAC__stream_encoder_get_blocksize(encoder_);
223                 }
224
225                 unsigned Stream::get_max_lpc_order() const
226                 {
227                         FLAC__ASSERT(is_valid());
228                         return ::FLAC__stream_encoder_get_max_lpc_order(encoder_);
229                 }
230
231                 unsigned Stream::get_qlp_coeff_precision() const
232                 {
233                         FLAC__ASSERT(is_valid());
234                         return ::FLAC__stream_encoder_get_qlp_coeff_precision(encoder_);
235                 }
236
237                 bool Stream::get_do_qlp_coeff_prec_search() const
238                 {
239                         FLAC__ASSERT(is_valid());
240                         return (bool)::FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder_);
241                 }
242
243                 bool Stream::get_do_escape_coding() const
244                 {
245                         FLAC__ASSERT(is_valid());
246                         return (bool)::FLAC__stream_encoder_get_do_escape_coding(encoder_);
247                 }
248
249                 bool Stream::get_do_exhaustive_model_search() const
250                 {
251                         FLAC__ASSERT(is_valid());
252                         return (bool)::FLAC__stream_encoder_get_do_exhaustive_model_search(encoder_);
253                 }
254
255                 unsigned Stream::get_min_residual_partition_order() const
256                 {
257                         FLAC__ASSERT(is_valid());
258                         return ::FLAC__stream_encoder_get_min_residual_partition_order(encoder_);
259                 }
260
261                 unsigned Stream::get_max_residual_partition_order() const
262                 {
263                         FLAC__ASSERT(is_valid());
264                         return ::FLAC__stream_encoder_get_max_residual_partition_order(encoder_);
265                 }
266
267                 unsigned Stream::get_rice_parameter_search_dist() const
268                 {
269                         FLAC__ASSERT(is_valid());
270                         return ::FLAC__stream_encoder_get_rice_parameter_search_dist(encoder_);
271                 }
272
273                 FLAC__uint64 Stream::get_total_samples_estimate() const
274                 {
275                         FLAC__ASSERT(is_valid());
276                         return ::FLAC__stream_encoder_get_total_samples_estimate(encoder_);
277                 }
278
279                 Stream::State Stream::init()
280                 {
281                         FLAC__ASSERT(is_valid());
282                         ::FLAC__stream_encoder_set_write_callback(encoder_, write_callback_);
283                         ::FLAC__stream_encoder_set_metadata_callback(encoder_, metadata_callback_);
284                         ::FLAC__stream_encoder_set_client_data(encoder_, (void*)this);
285                         return State(::FLAC__stream_encoder_init(encoder_));
286                 }
287
288                 void Stream::finish()
289                 {
290                         FLAC__ASSERT(is_valid());
291                         ::FLAC__stream_encoder_finish(encoder_);
292                 }
293
294                 bool Stream::process(const FLAC__int32 * const buffer[], unsigned samples)
295                 {
296                         FLAC__ASSERT(is_valid());
297                         return (bool)::FLAC__stream_encoder_process(encoder_, buffer, samples);
298                 }
299
300                 bool Stream::process_interleaved(const FLAC__int32 buffer[], unsigned samples)
301                 {
302                         FLAC__ASSERT(is_valid());
303                         return (bool)::FLAC__stream_encoder_process_interleaved(encoder_, buffer, samples);
304                 }
305
306                 ::FLAC__StreamEncoderWriteStatus Stream::write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
307                 {
308                         (void)encoder;
309                         FLAC__ASSERT(0 != client_data);
310                         Stream *instance = reinterpret_cast<Stream *>(client_data);
311                         FLAC__ASSERT(0 != instance);
312                         return instance->write_callback(buffer, bytes, samples, current_frame);
313                 }
314
315                 void Stream::metadata_callback_(const ::FLAC__StreamEncoder *encoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
316                 {
317                         (void)encoder;
318                         FLAC__ASSERT(0 != client_data);
319                         Stream *instance = reinterpret_cast<Stream *>(client_data);
320                         FLAC__ASSERT(0 != instance);
321                         instance->metadata_callback(metadata);
322                 }
323
324         };
325 };