stream_encoder.cpp 9.29 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* libFLAC++ - Free Lossless Audio Codec library
 * Copyright (C) 2002  Josh Coalson
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 */

#include "FLAC++/encoder.h"
#include "FLAC/assert.h"

namespace FLAC {
	namespace Encoder {

26
27
28
29
30
31
32
33
		const char *Stream::State::resolved_as_cstring(const Stream &encoder) const
		{
			if(state_ == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
				return encoder.get_verify_decoder_state().as_cstring();
			else
				return as_cstring();
		}

Josh Coalson's avatar
Josh Coalson committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
		Stream::Stream():
		encoder_(::FLAC__stream_encoder_new())
		{ }

		Stream::~Stream()
		{
			if(0 != encoder_) {
				::FLAC__stream_encoder_finish(encoder_);
				::FLAC__stream_encoder_delete(encoder_);
			}
		}

		bool Stream::is_valid() const
		{
			return 0 != encoder_;
		}

Josh Coalson's avatar
Josh Coalson committed
51
52
53
54
55
56
		bool Stream::set_verify(bool value)
		{
			FLAC__ASSERT(is_valid());
			return (bool)::FLAC__stream_encoder_set_verify(encoder_, value);
		}

Josh Coalson's avatar
Josh Coalson committed
57
58
59
		bool Stream::set_streamable_subset(bool value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
60
			return (bool)::FLAC__stream_encoder_set_streamable_subset(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
61
62
63
64
65
		}

		bool Stream::set_do_mid_side_stereo(bool value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
66
			return (bool)::FLAC__stream_encoder_set_do_mid_side_stereo(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
67
68
69
70
71
		}

		bool Stream::set_loose_mid_side_stereo(bool value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
72
			return (bool)::FLAC__stream_encoder_set_loose_mid_side_stereo(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
73
74
75
76
77
		}

		bool Stream::set_channels(unsigned value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
78
			return (bool)::FLAC__stream_encoder_set_channels(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
79
80
81
82
83
		}

		bool Stream::set_bits_per_sample(unsigned value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
84
			return (bool)::FLAC__stream_encoder_set_bits_per_sample(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
85
86
87
88
89
		}

		bool Stream::set_sample_rate(unsigned value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
90
			return (bool)::FLAC__stream_encoder_set_sample_rate(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
91
92
93
94
95
		}

		bool Stream::set_blocksize(unsigned value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
96
			return (bool)::FLAC__stream_encoder_set_blocksize(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
97
98
99
100
101
		}

		bool Stream::set_max_lpc_order(unsigned value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
102
			return (bool)::FLAC__stream_encoder_set_max_lpc_order(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
103
104
105
106
107
		}

		bool Stream::set_qlp_coeff_precision(unsigned value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
108
			return (bool)::FLAC__stream_encoder_set_qlp_coeff_precision(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
109
110
111
112
113
		}

		bool Stream::set_do_qlp_coeff_prec_search(bool value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
114
			return (bool)::FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
115
116
117
118
119
		}

		bool Stream::set_do_escape_coding(bool value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
120
			return (bool)::FLAC__stream_encoder_set_do_escape_coding(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
121
122
123
124
125
		}

		bool Stream::set_do_exhaustive_model_search(bool value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
126
			return (bool)::FLAC__stream_encoder_set_do_exhaustive_model_search(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
127
128
129
130
131
		}

		bool Stream::set_min_residual_partition_order(unsigned value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
132
			return (bool)::FLAC__stream_encoder_set_min_residual_partition_order(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
133
134
135
136
137
		}

		bool Stream::set_max_residual_partition_order(unsigned value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
138
			return (bool)::FLAC__stream_encoder_set_max_residual_partition_order(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
139
140
141
142
143
		}

		bool Stream::set_rice_parameter_search_dist(unsigned value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
144
			return (bool)::FLAC__stream_encoder_set_rice_parameter_search_dist(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
145
146
147
148
149
		}

		bool Stream::set_total_samples_estimate(FLAC__uint64 value)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
150
			return (bool)::FLAC__stream_encoder_set_total_samples_estimate(encoder_, value);
Josh Coalson's avatar
Josh Coalson committed
151
152
		}

153
		bool Stream::set_metadata(::FLAC__StreamMetadata **metadata, unsigned num_blocks)
Josh Coalson's avatar
Josh Coalson committed
154
155
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
156
			return (bool)::FLAC__stream_encoder_set_metadata(encoder_, metadata, num_blocks);
Josh Coalson's avatar
Josh Coalson committed
157
158
159
160
161
162
163
164
		}

		Stream::State Stream::get_state() const
		{
			FLAC__ASSERT(is_valid());
			return State(::FLAC__stream_encoder_get_state(encoder_));
		}

Josh Coalson's avatar
Josh Coalson committed
165
166
167
168
169
170
		Decoder::Stream::State Stream::get_verify_decoder_state() const
		{
			FLAC__ASSERT(is_valid());
			return Decoder::Stream::State(::FLAC__stream_encoder_get_verify_decoder_state(encoder_));
		}

171
172
173
		void Stream::get_verify_decoder_error_stats(FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got)
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
174
			::FLAC__stream_encoder_get_verify_decoder_error_stats(encoder_, absolute_sample, frame_number, channel, sample, expected, got);
175
176
		}

Josh Coalson's avatar
Josh Coalson committed
177
178
179
180
181
182
		bool Stream::get_verify() const
		{
			FLAC__ASSERT(is_valid());
			return (bool)::FLAC__stream_encoder_get_verify(encoder_);
		}

Josh Coalson's avatar
Josh Coalson committed
183
184
185
		bool Stream::get_streamable_subset() const
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
186
			return (bool)::FLAC__stream_encoder_get_streamable_subset(encoder_);
Josh Coalson's avatar
Josh Coalson committed
187
188
189
190
191
		}

		bool Stream::get_do_mid_side_stereo() const
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
192
			return (bool)::FLAC__stream_encoder_get_do_mid_side_stereo(encoder_);
Josh Coalson's avatar
Josh Coalson committed
193
194
195
196
197
		}

		bool Stream::get_loose_mid_side_stereo() const
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
198
			return (bool)::FLAC__stream_encoder_get_loose_mid_side_stereo(encoder_);
Josh Coalson's avatar
Josh Coalson committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
		}

		unsigned Stream::get_channels() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_encoder_get_channels(encoder_);
		}

		unsigned Stream::get_bits_per_sample() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_encoder_get_bits_per_sample(encoder_);
		}

		unsigned Stream::get_sample_rate() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_encoder_get_sample_rate(encoder_);
		}

		unsigned Stream::get_blocksize() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_encoder_get_blocksize(encoder_);
		}

		unsigned Stream::get_max_lpc_order() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_encoder_get_max_lpc_order(encoder_);
		}

		unsigned Stream::get_qlp_coeff_precision() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_encoder_get_qlp_coeff_precision(encoder_);
		}

		bool Stream::get_do_qlp_coeff_prec_search() const
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
240
			return (bool)::FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder_);
Josh Coalson's avatar
Josh Coalson committed
241
242
243
244
245
		}

		bool Stream::get_do_escape_coding() const
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
246
			return (bool)::FLAC__stream_encoder_get_do_escape_coding(encoder_);
Josh Coalson's avatar
Josh Coalson committed
247
248
249
250
251
		}

		bool Stream::get_do_exhaustive_model_search() const
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
252
			return (bool)::FLAC__stream_encoder_get_do_exhaustive_model_search(encoder_);
Josh Coalson's avatar
Josh Coalson committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
		}

		unsigned Stream::get_min_residual_partition_order() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_encoder_get_min_residual_partition_order(encoder_);
		}

		unsigned Stream::get_max_residual_partition_order() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_encoder_get_max_residual_partition_order(encoder_);
		}

		unsigned Stream::get_rice_parameter_search_dist() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_encoder_get_rice_parameter_search_dist(encoder_);
		}

273
274
275
276
277
278
		FLAC__uint64 Stream::get_total_samples_estimate() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_encoder_get_total_samples_estimate(encoder_);
		}

Josh Coalson's avatar
Josh Coalson committed
279
280
281
		Stream::State Stream::init()
		{
			FLAC__ASSERT(is_valid());
282
283
284
			::FLAC__stream_encoder_set_write_callback(encoder_, write_callback_);
			::FLAC__stream_encoder_set_metadata_callback(encoder_, metadata_callback_);
			::FLAC__stream_encoder_set_client_data(encoder_, (void*)this);
Josh Coalson's avatar
Josh Coalson committed
285
286
287
288
289
290
291
292
293
			return State(::FLAC__stream_encoder_init(encoder_));
		}

		void Stream::finish()
		{
			FLAC__ASSERT(is_valid());
			::FLAC__stream_encoder_finish(encoder_);
		}

294
		bool Stream::process(const FLAC__int32 * const buffer[], unsigned samples)
Josh Coalson's avatar
Josh Coalson committed
295
296
		{
			FLAC__ASSERT(is_valid());
297
			return (bool)::FLAC__stream_encoder_process(encoder_, buffer, samples);
Josh Coalson's avatar
Josh Coalson committed
298
299
		}

300
		bool Stream::process_interleaved(const FLAC__int32 buffer[], unsigned samples)
Josh Coalson's avatar
Josh Coalson committed
301
302
		{
			FLAC__ASSERT(is_valid());
303
			return (bool)::FLAC__stream_encoder_process_interleaved(encoder_, buffer, samples);
Josh Coalson's avatar
Josh Coalson committed
304
305
306
307
308
309
310
311
312
313
314
		}

		::FLAC__StreamEncoderWriteStatus Stream::write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
		{
			(void)encoder;
			FLAC__ASSERT(0 != client_data);
			Stream *instance = reinterpret_cast<Stream *>(client_data);
			FLAC__ASSERT(0 != instance);
			return instance->write_callback(buffer, bytes, samples, current_frame);
		}

315
		void Stream::metadata_callback_(const ::FLAC__StreamEncoder *encoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
Josh Coalson's avatar
Josh Coalson committed
316
317
318
319
320
321
322
323
324
325
		{
			(void)encoder;
			FLAC__ASSERT(0 != client_data);
			Stream *instance = reinterpret_cast<Stream *>(client_data);
			FLAC__ASSERT(0 != instance);
			instance->metadata_callback(metadata);
		}

	};
};