bump version number to 1.1.0
[flac.git] / src / test_libOggFLAC++ / encoders.cpp
1 /* test_libOggFLAC++ - Unit tester for libOggFLAC++
2  * Copyright (C) 2002,2003  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 #include "encoders.h"
20 extern "C" {
21 #include "file_utils.h"
22 #include "metadata_utils.h"
23 }
24 #include "FLAC/assert.h"
25 #include "OggFLAC++/encoder.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, unknown_;
31 static ::FLAC__StreamMetadata *metadata_sequence_[] = { &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &unknown_ };
32 static const unsigned num_metadata_ = sizeof(metadata_sequence_) / sizeof(metadata_sequence_[0]);
33
34 static void init_metadata_blocks_()
35 {
36         mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &unknown_);
37 }
38
39 static void free_metadata_blocks_()
40 {
41         mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &unknown_);
42 }
43
44 class StreamEncoder : public OggFLAC::Encoder::Stream {
45 public:
46         StreamEncoder(): OggFLAC::Encoder::Stream() { }
47         ~StreamEncoder() { }
48
49         // from OggFLAC::Encoder::Stream
50         ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame);
51
52         bool die(const char *msg = 0) const;
53 };
54
55 ::FLAC__StreamEncoderWriteStatus StreamEncoder::write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame)
56 {
57         (void)buffer, (void)bytes, (void)samples, (void)current_frame;
58
59         return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
60 }
61
62 bool StreamEncoder::die(const char *msg) const
63 {
64         State state = get_state();
65
66         if(msg)
67                 printf("FAILED, %s", msg);
68         else
69                 printf("FAILED");
70
71         printf(", state = %u (%s)\n", (unsigned)((::OggFLAC__StreamEncoderState)state), state.as_cstring());
72         if(state == ::OggFLAC__STREAM_ENCODER_FLAC_STREAM_ENCODER_ERROR) {
73                 FLAC::Encoder::Stream::State state_ = get_FLAC_stream_encoder_state();
74                 printf("      FLAC stream encoder state = %u (%s)\n", (unsigned)((::FLAC__StreamEncoderState)state_), state_.as_cstring());
75                 if(state_ == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
76                         FLAC::Decoder::Stream::State dstate = get_verify_decoder_state();
77                         printf("      verify decoder state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
78                 }
79         }
80
81         return false;
82 }
83
84 static bool test_stream_encoder()
85 {
86         StreamEncoder *encoder;
87         FLAC__int32 samples[1024];
88         FLAC__int32 *samples_array[1] = { samples };
89         unsigned i;
90
91         printf("\n+++ libOggFLAC++ unit test: OggFLAC::Encoder::Stream\n\n");
92
93         printf("allocating encoder instance... ");
94         encoder = new StreamEncoder();
95         if(0 == encoder) {
96                 printf("FAILED, new returned NULL\n");
97                 return false;
98         }
99         printf("OK\n");
100
101         printf("testing is_valid()... ");
102         if(!encoder->is_valid()) {
103                 printf("FAILED, returned false\n");
104                 return false;
105         }
106         printf("OK\n");
107
108         printf("testing set_serial_number()... ");
109         if(!encoder->set_serial_number(file_utils__serial_number))
110                 return encoder->die("returned false");
111         printf("OK\n");
112
113         printf("testing set_verify()... ");
114         if(!encoder->set_verify(true))
115                 return encoder->die("returned false");
116         printf("OK\n");
117
118         printf("testing set_streamable_subset()... ");
119         if(!encoder->set_streamable_subset(true))
120                 return encoder->die("returned false");
121         printf("OK\n");
122
123         printf("testing set_do_mid_side_stereo()... ");
124         if(!encoder->set_do_mid_side_stereo(false))
125                 return encoder->die("returned false");
126         printf("OK\n");
127
128         printf("testing set_loose_mid_side_stereo()... ");
129         if(!encoder->set_loose_mid_side_stereo(false))
130                 return encoder->die("returned false");
131         printf("OK\n");
132
133         printf("testing set_channels()... ");
134         if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
135                 return encoder->die("returned false");
136         printf("OK\n");
137
138         printf("testing set_bits_per_sample()... ");
139         if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample))
140                 return encoder->die("returned false");
141         printf("OK\n");
142
143         printf("testing set_sample_rate()... ");
144         if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate))
145                 return encoder->die("returned false");
146         printf("OK\n");
147
148         printf("testing set_blocksize()... ");
149         if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
150                 return encoder->die("returned false");
151         printf("OK\n");
152
153         printf("testing set_max_lpc_order()... ");
154         if(!encoder->set_max_lpc_order(0))
155                 return encoder->die("returned false");
156         printf("OK\n");
157
158         printf("testing set_qlp_coeff_precision()... ");
159         if(!encoder->set_qlp_coeff_precision(0))
160                 return encoder->die("returned false");
161         printf("OK\n");
162
163         printf("testing set_do_qlp_coeff_prec_search()... ");
164         if(!encoder->set_do_qlp_coeff_prec_search(false))
165                 return encoder->die("returned false");
166         printf("OK\n");
167
168         printf("testing set_do_escape_coding()... ");
169         if(!encoder->set_do_escape_coding(false))
170                 return encoder->die("returned false");
171         printf("OK\n");
172
173         printf("testing set_do_exhaustive_model_search()... ");
174         if(!encoder->set_do_exhaustive_model_search(false))
175                 return encoder->die("returned false");
176         printf("OK\n");
177
178         printf("testing set_min_residual_partition_order()... ");
179         if(!encoder->set_min_residual_partition_order(0))
180                 return encoder->die("returned false");
181         printf("OK\n");
182
183         printf("testing set_max_residual_partition_order()... ");
184         if(!encoder->set_max_residual_partition_order(0))
185                 return encoder->die("returned false");
186         printf("OK\n");
187
188         printf("testing set_rice_parameter_search_dist()... ");
189         if(!encoder->set_rice_parameter_search_dist(0))
190                 return encoder->die("returned false");
191         printf("OK\n");
192
193         printf("testing set_total_samples_estimate()... ");
194         if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples))
195                 return encoder->die("returned false");
196         printf("OK\n");
197
198         printf("testing set_metadata()... ");
199         if(!encoder->set_metadata(metadata_sequence_, num_metadata_))
200                 return encoder->die("returned false");
201         printf("OK\n");
202
203         printf("testing init()... ");
204         if(encoder->init() != ::OggFLAC__STREAM_ENCODER_OK)
205                 return encoder->die();
206         printf("OK\n");
207
208         printf("testing get_state()... ");
209         OggFLAC::Encoder::Stream::State state = encoder->get_state();
210         printf("returned state = %u (%s)... OK\n", (unsigned)((::OggFLAC__StreamEncoderState)state), state.as_cstring());
211
212         printf("testing get_FLAC_stream_encoder_state()... ");
213         FLAC::Encoder::Stream::State state_ = encoder->get_FLAC_stream_encoder_state();
214         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamEncoderState)state_), state_.as_cstring());
215
216         printf("testing get_verify_decoder_state()... ");
217         FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
218         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
219
220         {
221                 FLAC__uint64 absolute_sample;
222                 unsigned frame_number;
223                 unsigned channel;
224                 unsigned sample;
225                 FLAC__int32 expected;
226                 FLAC__int32 got;
227
228                 printf("testing get_verify_decoder_error_stats()... ");
229                 encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got);
230                 printf("OK\n");
231         }
232
233         printf("testing get_verify()... ");
234         if(encoder->get_verify() != true) {
235                 printf("FAILED, expected true, got false\n");
236                 return false;
237         }
238         printf("OK\n");
239
240         printf("testing get_streamable_subset()... ");
241         if(encoder->get_streamable_subset() != true) {
242                 printf("FAILED, expected true, got false\n");
243                 return false;
244         }
245         printf("OK\n");
246
247         printf("testing get_do_mid_side_stereo()... ");
248         if(encoder->get_do_mid_side_stereo() != false) {
249                 printf("FAILED, expected false, got true\n");
250                 return false;
251         }
252         printf("OK\n");
253
254         printf("testing get_loose_mid_side_stereo()... ");
255         if(encoder->get_loose_mid_side_stereo() != false) {
256                 printf("FAILED, expected false, got true\n");
257                 return false;
258         }
259         printf("OK\n");
260
261         printf("testing get_channels()... ");
262         if(encoder->get_channels() != streaminfo_.data.stream_info.channels) {
263                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, encoder->get_channels());
264                 return false;
265         }
266         printf("OK\n");
267
268         printf("testing get_bits_per_sample()... ");
269         if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) {
270                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample());
271                 return false;
272         }
273         printf("OK\n");
274
275         printf("testing get_sample_rate()... ");
276         if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) {
277                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate());
278                 return false;
279         }
280         printf("OK\n");
281
282         printf("testing get_blocksize()... ");
283         if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) {
284                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize());
285                 return false;
286         }
287         printf("OK\n");
288
289         printf("testing get_max_lpc_order()... ");
290         if(encoder->get_max_lpc_order() != 0) {
291                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_lpc_order());
292                 return false;
293         }
294         printf("OK\n");
295
296         printf("testing get_qlp_coeff_precision()... ");
297         (void)encoder->get_qlp_coeff_precision();
298         /* we asked the encoder to auto select this so we accept anything */
299         printf("OK\n");
300
301         printf("testing get_do_qlp_coeff_prec_search()... ");
302         if(encoder->get_do_qlp_coeff_prec_search() != false) {
303                 printf("FAILED, expected false, got true\n");
304                 return false;
305         }
306         printf("OK\n");
307
308         printf("testing get_do_escape_coding()... ");
309         if(encoder->get_do_escape_coding() != false) {
310                 printf("FAILED, expected false, got true\n");
311                 return false;
312         }
313         printf("OK\n");
314
315         printf("testing get_do_exhaustive_model_search()... ");
316         if(encoder->get_do_exhaustive_model_search() != false) {
317                 printf("FAILED, expected false, got true\n");
318                 return false;
319         }
320         printf("OK\n");
321
322         printf("testing get_min_residual_partition_order()... ");
323         if(encoder->get_min_residual_partition_order() != 0) {
324                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_min_residual_partition_order());
325                 return false;
326         }
327         printf("OK\n");
328
329         printf("testing get_max_residual_partition_order()... ");
330         if(encoder->get_max_residual_partition_order() != 0) {
331                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_residual_partition_order());
332                 return false;
333         }
334         printf("OK\n");
335
336         printf("testing get_rice_parameter_search_dist()... ");
337         if(encoder->get_rice_parameter_search_dist() != 0) {
338                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_rice_parameter_search_dist());
339                 return false;
340         }
341         printf("OK\n");
342
343         printf("testing get_total_samples_estimate()... ");
344         if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
345                 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
346                 return false;
347         }
348         printf("OK\n");
349
350         /* init the dummy sample buffer */
351         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
352                 samples[i] = i & 7;
353
354         printf("testing process()... ");
355         if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32)))
356                 return encoder->die("returned false");
357         printf("OK\n");
358
359         printf("testing process_interleaved()... ");
360         if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32)))
361                 return encoder->die("returned false");
362         printf("OK\n");
363
364         printf("testing finish()... ");
365         encoder->finish();
366         printf("OK\n");
367
368         printf("freeing encoder instance... ");
369         delete encoder;
370         printf("OK\n");
371
372         printf("\nPASSED!\n");
373
374         return true;
375 }
376
377 bool test_encoders()
378 {
379         init_metadata_blocks_();
380
381         if(!test_stream_encoder())
382                 return false;
383
384         free_metadata_blocks_();
385
386         return true;
387 }