minor verbiage
[platform/upstream/flac.git] / src / test_libFLAC++ / encoders.cpp
1 /* test_libFLAC++ - Unit tester for libFLAC++
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 "FLAC++/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_;
31 static ::FLAC__StreamMetadata *metadata_sequence_[] = { &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_ };
32 static const unsigned num_metadata_ = sizeof(metadata_sequence_) / sizeof(metadata_sequence_[0]);
33 static const char *flacfilename_ = "metadata.flac";
34
35 static void init_metadata_blocks_()
36 {
37         mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_);
38 }
39
40 static void free_metadata_blocks_()
41 {
42         mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_);
43 }
44
45 class StreamEncoder : public FLAC::Encoder::Stream {
46 public:
47         StreamEncoder(): FLAC::Encoder::Stream() { }
48         ~StreamEncoder() { }
49
50         // from FLAC::Encoder::Stream
51         ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame);
52         void metadata_callback(const ::FLAC__StreamMetadata *metadata);
53
54         bool die(const char *msg = 0) const;
55 };
56
57 ::FLAC__StreamEncoderWriteStatus StreamEncoder::write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame)
58 {
59         (void)buffer, (void)bytes, (void)samples, (void)current_frame;
60
61         return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
62 }
63
64 void StreamEncoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
65 {
66         (void)metadata;
67 }
68
69 bool StreamEncoder::die(const char *msg) const
70 {
71         State state = get_state();
72
73         if(msg)
74                 printf("FAILED, %s", msg);
75         else
76                 printf("FAILED");
77
78         printf(", state = %u (%s)\n", (unsigned)((::FLAC__StreamEncoderState)state), state.as_cstring());
79         if(state == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
80                 FLAC::Decoder::Stream::State dstate = get_verify_decoder_state();
81                 printf("      verify decoder state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
82         }
83
84         return false;
85 }
86
87 static bool test_stream_encoder()
88 {
89         StreamEncoder *encoder;
90         FLAC__int32 samples[1024];
91         FLAC__int32 *samples_array[1] = { samples };
92         unsigned i;
93
94         printf("\n+++ libFLAC++ unit test: FLAC::Encoder::Stream\n\n");
95
96         printf("allocating encoder instance... ");
97         encoder = new StreamEncoder();
98         if(0 == encoder) {
99                 printf("FAILED, new returned NULL\n");
100                 return false;
101         }
102         printf("OK\n");
103
104         printf("testing is_valid()... ");
105         if(!encoder->is_valid()) {
106                 printf("FAILED, returned false\n");
107                 return false;
108         }
109         printf("OK\n");
110
111         printf("testing set_verify()... ");
112         if(!encoder->set_verify(true))
113                 return encoder->die("returned false");
114         printf("OK\n");
115
116         printf("testing set_streamable_subset()... ");
117         if(!encoder->set_streamable_subset(true))
118                 return encoder->die("returned false");
119         printf("OK\n");
120
121         printf("testing set_do_mid_side_stereo()... ");
122         if(!encoder->set_do_mid_side_stereo(false))
123                 return encoder->die("returned false");
124         printf("OK\n");
125
126         printf("testing set_loose_mid_side_stereo()... ");
127         if(!encoder->set_loose_mid_side_stereo(false))
128                 return encoder->die("returned false");
129         printf("OK\n");
130
131         printf("testing set_channels()... ");
132         if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
133                 return encoder->die("returned false");
134         printf("OK\n");
135
136         printf("testing set_bits_per_sample()... ");
137         if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample))
138                 return encoder->die("returned false");
139         printf("OK\n");
140
141         printf("testing set_sample_rate()... ");
142         if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate))
143                 return encoder->die("returned false");
144         printf("OK\n");
145
146         printf("testing set_blocksize()... ");
147         if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
148                 return encoder->die("returned false");
149         printf("OK\n");
150
151         printf("testing set_max_lpc_order()... ");
152         if(!encoder->set_max_lpc_order(0))
153                 return encoder->die("returned false");
154         printf("OK\n");
155
156         printf("testing set_qlp_coeff_precision()... ");
157         if(!encoder->set_qlp_coeff_precision(0))
158                 return encoder->die("returned false");
159         printf("OK\n");
160
161         printf("testing set_do_qlp_coeff_prec_search()... ");
162         if(!encoder->set_do_qlp_coeff_prec_search(false))
163                 return encoder->die("returned false");
164         printf("OK\n");
165
166         printf("testing set_do_escape_coding()... ");
167         if(!encoder->set_do_escape_coding(false))
168                 return encoder->die("returned false");
169         printf("OK\n");
170
171         printf("testing set_do_exhaustive_model_search()... ");
172         if(!encoder->set_do_exhaustive_model_search(false))
173                 return encoder->die("returned false");
174         printf("OK\n");
175
176         printf("testing set_min_residual_partition_order()... ");
177         if(!encoder->set_min_residual_partition_order(0))
178                 return encoder->die("returned false");
179         printf("OK\n");
180
181         printf("testing set_max_residual_partition_order()... ");
182         if(!encoder->set_max_residual_partition_order(0))
183                 return encoder->die("returned false");
184         printf("OK\n");
185
186         printf("testing set_rice_parameter_search_dist()... ");
187         if(!encoder->set_rice_parameter_search_dist(0))
188                 return encoder->die("returned false");
189         printf("OK\n");
190
191         printf("testing set_total_samples_estimate()... ");
192         if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples))
193                 return encoder->die("returned false");
194         printf("OK\n");
195
196         printf("testing set_metadata()... ");
197         if(!encoder->set_metadata(metadata_sequence_, num_metadata_))
198                 return encoder->die("returned false");
199         printf("OK\n");
200
201         printf("testing init()... ");
202         if(encoder->init() != ::FLAC__STREAM_ENCODER_OK)
203                 return encoder->die();
204         printf("OK\n");
205
206         printf("testing get_state()... ");
207         FLAC::Encoder::Stream::State state = encoder->get_state();
208         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamEncoderState)state), state.as_cstring());
209
210         printf("testing get_verify_decoder_state()... ");
211         FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
212         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
213
214         {
215                 FLAC__uint64 absolute_sample;
216                 unsigned frame_number;
217                 unsigned channel;
218                 unsigned sample;
219                 FLAC__int32 expected;
220                 FLAC__int32 got;
221
222                 printf("testing get_verify_decoder_error_stats()... ");
223                 encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got);
224                 printf("OK\n");
225         }
226
227         printf("testing get_verify()... ");
228         if(encoder->get_verify() != true) {
229                 printf("FAILED, expected true, got false\n");
230                 return false;
231         }
232         printf("OK\n");
233
234         printf("testing get_streamable_subset()... ");
235         if(encoder->get_streamable_subset() != true) {
236                 printf("FAILED, expected true, got false\n");
237                 return false;
238         }
239         printf("OK\n");
240
241         printf("testing get_do_mid_side_stereo()... ");
242         if(encoder->get_do_mid_side_stereo() != false) {
243                 printf("FAILED, expected false, got true\n");
244                 return false;
245         }
246         printf("OK\n");
247
248         printf("testing get_loose_mid_side_stereo()... ");
249         if(encoder->get_loose_mid_side_stereo() != false) {
250                 printf("FAILED, expected false, got true\n");
251                 return false;
252         }
253         printf("OK\n");
254
255         printf("testing get_channels()... ");
256         if(encoder->get_channels() != streaminfo_.data.stream_info.channels) {
257                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, encoder->get_channels());
258                 return false;
259         }
260         printf("OK\n");
261
262         printf("testing get_bits_per_sample()... ");
263         if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) {
264                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample());
265                 return false;
266         }
267         printf("OK\n");
268
269         printf("testing get_sample_rate()... ");
270         if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) {
271                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate());
272                 return false;
273         }
274         printf("OK\n");
275
276         printf("testing get_blocksize()... ");
277         if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) {
278                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize());
279                 return false;
280         }
281         printf("OK\n");
282
283         printf("testing get_max_lpc_order()... ");
284         if(encoder->get_max_lpc_order() != 0) {
285                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_lpc_order());
286                 return false;
287         }
288         printf("OK\n");
289
290         printf("testing get_qlp_coeff_precision()... ");
291         (void)encoder->get_qlp_coeff_precision();
292         /* we asked the encoder to auto select this so we accept anything */
293         printf("OK\n");
294
295         printf("testing get_do_qlp_coeff_prec_search()... ");
296         if(encoder->get_do_qlp_coeff_prec_search() != false) {
297                 printf("FAILED, expected false, got true\n");
298                 return false;
299         }
300         printf("OK\n");
301
302         printf("testing get_do_escape_coding()... ");
303         if(encoder->get_do_escape_coding() != false) {
304                 printf("FAILED, expected false, got true\n");
305                 return false;
306         }
307         printf("OK\n");
308
309         printf("testing get_do_exhaustive_model_search()... ");
310         if(encoder->get_do_exhaustive_model_search() != false) {
311                 printf("FAILED, expected false, got true\n");
312                 return false;
313         }
314         printf("OK\n");
315
316         printf("testing get_min_residual_partition_order()... ");
317         if(encoder->get_min_residual_partition_order() != 0) {
318                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_min_residual_partition_order());
319                 return false;
320         }
321         printf("OK\n");
322
323         printf("testing get_max_residual_partition_order()... ");
324         if(encoder->get_max_residual_partition_order() != 0) {
325                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_residual_partition_order());
326                 return false;
327         }
328         printf("OK\n");
329
330         printf("testing get_rice_parameter_search_dist()... ");
331         if(encoder->get_rice_parameter_search_dist() != 0) {
332                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_rice_parameter_search_dist());
333                 return false;
334         }
335         printf("OK\n");
336
337         printf("testing get_total_samples_estimate()... ");
338         if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
339                 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
340                 return false;
341         }
342         printf("OK\n");
343
344         /* init the dummy sample buffer */
345         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
346                 samples[i] = i & 7;
347
348         printf("testing process()... ");
349         if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32)))
350                 return encoder->die("returned false");
351         printf("OK\n");
352
353         printf("testing process_interleaved()... ");
354         if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32)))
355                 return encoder->die("returned false");
356         printf("OK\n");
357
358         printf("testing finish()... ");
359         encoder->finish();
360         printf("OK\n");
361
362         printf("freeing encoder instance... ");
363         delete encoder;
364         printf("OK\n");
365
366         printf("\nPASSED!\n");
367
368         return true;
369 }
370
371 class SeekableStreamEncoder : public FLAC::Encoder::SeekableStream {
372 public:
373         SeekableStreamEncoder(): FLAC::Encoder::SeekableStream() { }
374         ~SeekableStreamEncoder() { }
375
376         // from FLAC::Encoder::SeekableStream
377         ::FLAC__SeekableStreamEncoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
378         ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame);
379
380         bool die(const char *msg = 0) const;
381 };
382
383 ::FLAC__SeekableStreamEncoderSeekStatus SeekableStreamEncoder::seek_callback(FLAC__uint64 absolute_byte_offset)
384 {
385         (void)absolute_byte_offset;
386
387         return ::FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK;
388 }
389
390 ::FLAC__StreamEncoderWriteStatus SeekableStreamEncoder::write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame)
391 {
392         (void)buffer, (void)bytes, (void)samples, (void)current_frame;
393
394         return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
395 }
396
397 bool SeekableStreamEncoder::die(const char *msg) const
398 {
399         State state = get_state();
400
401         if(msg)
402                 printf("FAILED, %s", msg);
403         else
404                 printf("FAILED");
405
406         printf(", state = %u (%s)\n", (unsigned)((::FLAC__SeekableStreamEncoderState)state), state.as_cstring());
407         if(state == ::FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR) {
408                 FLAC::Encoder::Stream::State state_ = get_stream_encoder_state();
409                 printf("      stream encoder state = %u (%s)\n", (unsigned)((::FLAC__StreamEncoderState)state_), state_.as_cstring());
410                 if(state_ == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
411                         FLAC::Decoder::Stream::State dstate = get_verify_decoder_state();
412                         printf("      verify decoder state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
413                 }
414         }
415
416         return false;
417 }
418
419 static bool test_seekable_stream_encoder()
420 {
421         SeekableStreamEncoder *encoder;
422         FLAC__int32 samples[1024];
423         FLAC__int32 *samples_array[1] = { samples };
424         unsigned i;
425
426         printf("\n+++ libFLAC++ unit test: FLAC::Encoder::SeekableStream\n\n");
427
428         printf("allocating encoder instance... ");
429         encoder = new SeekableStreamEncoder();
430         if(0 == encoder) {
431                 printf("FAILED, new returned NULL\n");
432                 return false;
433         }
434         printf("OK\n");
435
436         printf("testing is_valid()... ");
437         if(!encoder->is_valid()) {
438                 printf("FAILED, returned false\n");
439                 return false;
440         }
441         printf("OK\n");
442
443         printf("testing set_verify()... ");
444         if(!encoder->set_verify(true))
445                 return encoder->die("returned false");
446         printf("OK\n");
447
448         printf("testing set_streamable_subset()... ");
449         if(!encoder->set_streamable_subset(true))
450                 return encoder->die("returned false");
451         printf("OK\n");
452
453         printf("testing set_do_mid_side_stereo()... ");
454         if(!encoder->set_do_mid_side_stereo(false))
455                 return encoder->die("returned false");
456         printf("OK\n");
457
458         printf("testing set_loose_mid_side_stereo()... ");
459         if(!encoder->set_loose_mid_side_stereo(false))
460                 return encoder->die("returned false");
461         printf("OK\n");
462
463         printf("testing set_channels()... ");
464         if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
465                 return encoder->die("returned false");
466         printf("OK\n");
467
468         printf("testing set_bits_per_sample()... ");
469         if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample))
470                 return encoder->die("returned false");
471         printf("OK\n");
472
473         printf("testing set_sample_rate()... ");
474         if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate))
475                 return encoder->die("returned false");
476         printf("OK\n");
477
478         printf("testing set_blocksize()... ");
479         if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
480                 return encoder->die("returned false");
481         printf("OK\n");
482
483         printf("testing set_max_lpc_order()... ");
484         if(!encoder->set_max_lpc_order(0))
485                 return encoder->die("returned false");
486         printf("OK\n");
487
488         printf("testing set_qlp_coeff_precision()... ");
489         if(!encoder->set_qlp_coeff_precision(0))
490                 return encoder->die("returned false");
491         printf("OK\n");
492
493         printf("testing set_do_qlp_coeff_prec_search()... ");
494         if(!encoder->set_do_qlp_coeff_prec_search(false))
495                 return encoder->die("returned false");
496         printf("OK\n");
497
498         printf("testing set_do_escape_coding()... ");
499         if(!encoder->set_do_escape_coding(false))
500                 return encoder->die("returned false");
501         printf("OK\n");
502
503         printf("testing set_do_exhaustive_model_search()... ");
504         if(!encoder->set_do_exhaustive_model_search(false))
505                 return encoder->die("returned false");
506         printf("OK\n");
507
508         printf("testing set_min_residual_partition_order()... ");
509         if(!encoder->set_min_residual_partition_order(0))
510                 return encoder->die("returned false");
511         printf("OK\n");
512
513         printf("testing set_max_residual_partition_order()... ");
514         if(!encoder->set_max_residual_partition_order(0))
515                 return encoder->die("returned false");
516         printf("OK\n");
517
518         printf("testing set_rice_parameter_search_dist()... ");
519         if(!encoder->set_rice_parameter_search_dist(0))
520                 return encoder->die("returned false");
521         printf("OK\n");
522
523         printf("testing set_total_samples_estimate()... ");
524         if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples))
525                 return encoder->die("returned false");
526         printf("OK\n");
527
528         printf("testing set_metadata()... ");
529         if(!encoder->set_metadata(metadata_sequence_, num_metadata_))
530                 return encoder->die("returned false");
531         printf("OK\n");
532
533         printf("testing init()... ");
534         if(encoder->init() != ::FLAC__SEEKABLE_STREAM_ENCODER_OK)
535                 return encoder->die();
536         printf("OK\n");
537
538         printf("testing get_state()... ");
539         FLAC::Encoder::SeekableStream::State state = encoder->get_state();
540         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__SeekableStreamEncoderState)state), state.as_cstring());
541
542         printf("testing get_stream_encoder_state()... ");
543         FLAC::Encoder::Stream::State state_ = encoder->get_stream_encoder_state();
544         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamEncoderState)state_), state_.as_cstring());
545
546         printf("testing get_verify_decoder_state()... ");
547         FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
548         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
549
550         {
551                 FLAC__uint64 absolute_sample;
552                 unsigned frame_number;
553                 unsigned channel;
554                 unsigned sample;
555                 FLAC__int32 expected;
556                 FLAC__int32 got;
557
558                 printf("testing get_verify_decoder_error_stats()... ");
559                 encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got);
560                 printf("OK\n");
561         }
562
563         printf("testing get_verify()... ");
564         if(encoder->get_verify() != true) {
565                 printf("FAILED, expected true, got false\n");
566                 return false;
567         }
568         printf("OK\n");
569
570         printf("testing get_streamable_subset()... ");
571         if(encoder->get_streamable_subset() != true) {
572                 printf("FAILED, expected true, got false\n");
573                 return false;
574         }
575         printf("OK\n");
576
577         printf("testing get_do_mid_side_stereo()... ");
578         if(encoder->get_do_mid_side_stereo() != false) {
579                 printf("FAILED, expected false, got true\n");
580                 return false;
581         }
582         printf("OK\n");
583
584         printf("testing get_loose_mid_side_stereo()... ");
585         if(encoder->get_loose_mid_side_stereo() != false) {
586                 printf("FAILED, expected false, got true\n");
587                 return false;
588         }
589         printf("OK\n");
590
591         printf("testing get_channels()... ");
592         if(encoder->get_channels() != streaminfo_.data.stream_info.channels) {
593                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, encoder->get_channels());
594                 return false;
595         }
596         printf("OK\n");
597
598         printf("testing get_bits_per_sample()... ");
599         if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) {
600                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample());
601                 return false;
602         }
603         printf("OK\n");
604
605         printf("testing get_sample_rate()... ");
606         if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) {
607                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate());
608                 return false;
609         }
610         printf("OK\n");
611
612         printf("testing get_blocksize()... ");
613         if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) {
614                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize());
615                 return false;
616         }
617         printf("OK\n");
618
619         printf("testing get_max_lpc_order()... ");
620         if(encoder->get_max_lpc_order() != 0) {
621                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_lpc_order());
622                 return false;
623         }
624         printf("OK\n");
625
626         printf("testing get_qlp_coeff_precision()... ");
627         (void)encoder->get_qlp_coeff_precision();
628         /* we asked the encoder to auto select this so we accept anything */
629         printf("OK\n");
630
631         printf("testing get_do_qlp_coeff_prec_search()... ");
632         if(encoder->get_do_qlp_coeff_prec_search() != false) {
633                 printf("FAILED, expected false, got true\n");
634                 return false;
635         }
636         printf("OK\n");
637
638         printf("testing get_do_escape_coding()... ");
639         if(encoder->get_do_escape_coding() != false) {
640                 printf("FAILED, expected false, got true\n");
641                 return false;
642         }
643         printf("OK\n");
644
645         printf("testing get_do_exhaustive_model_search()... ");
646         if(encoder->get_do_exhaustive_model_search() != false) {
647                 printf("FAILED, expected false, got true\n");
648                 return false;
649         }
650         printf("OK\n");
651
652         printf("testing get_min_residual_partition_order()... ");
653         if(encoder->get_min_residual_partition_order() != 0) {
654                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_min_residual_partition_order());
655                 return false;
656         }
657         printf("OK\n");
658
659         printf("testing get_max_residual_partition_order()... ");
660         if(encoder->get_max_residual_partition_order() != 0) {
661                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_residual_partition_order());
662                 return false;
663         }
664         printf("OK\n");
665
666         printf("testing get_rice_parameter_search_dist()... ");
667         if(encoder->get_rice_parameter_search_dist() != 0) {
668                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_rice_parameter_search_dist());
669                 return false;
670         }
671         printf("OK\n");
672
673         printf("testing get_total_samples_estimate()... ");
674         if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
675                 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
676                 return false;
677         }
678         printf("OK\n");
679
680         /* init the dummy sample buffer */
681         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
682                 samples[i] = i & 7;
683
684         printf("testing process()... ");
685         if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32)))
686                 return encoder->die("returned false");
687         printf("OK\n");
688
689         printf("testing process_interleaved()... ");
690         if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32)))
691                 return encoder->die("returned false");
692         printf("OK\n");
693
694         printf("testing finish()... ");
695         encoder->finish();
696         printf("OK\n");
697
698         printf("freeing encoder instance... ");
699         delete encoder;
700         printf("OK\n");
701
702         printf("\nPASSED!\n");
703
704         return true;
705 }
706
707 class FileEncoder : public FLAC::Encoder::File {
708 public:
709         FileEncoder(): FLAC::Encoder::File() { }
710         ~FileEncoder() { }
711
712         // from FLAC::Encoder::File
713         void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate);
714
715         bool die(const char *msg = 0) const;
716 };
717
718 void FileEncoder::progress_callback(FLAC__uint64, FLAC__uint64, unsigned, unsigned)
719 {
720 }
721
722 bool FileEncoder::die(const char *msg) const
723 {
724         State state = get_state();
725
726         if(msg)
727                 printf("FAILED, %s", msg);
728         else
729                 printf("FAILED");
730
731         printf(", state = %u (%s)\n", (unsigned)((::FLAC__FileEncoderState)state), state.as_cstring());
732         if(state == ::FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR) {
733                 FLAC::Encoder::SeekableStream::State state_ = get_seekable_stream_encoder_state();
734                 printf("      seekable stream encoder state = %u (%s)\n", (unsigned)((::FLAC__SeekableStreamEncoderState)state_), state_.as_cstring());
735                 if(state_ == ::FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR) {
736                         FLAC::Encoder::Stream::State state__ = get_stream_encoder_state();
737                         printf("      stream encoder state = %u (%s)\n", (unsigned)((::FLAC__StreamEncoderState)state__), state__.as_cstring());
738                         if(state__ == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
739                                 FLAC::Decoder::Stream::State dstate = get_verify_decoder_state();
740                                 printf("      verify decoder state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
741                         }
742                 }
743         }
744
745         return false;
746 }
747
748 static bool test_file_encoder()
749 {
750         FileEncoder *encoder;
751         FLAC__int32 samples[1024];
752         FLAC__int32 *samples_array[1] = { samples };
753         unsigned i;
754
755         printf("\n+++ libFLAC++ unit test: FLAC::Encoder::File\n\n");
756
757         printf("allocating encoder instance... ");
758         encoder = new FileEncoder();
759         if(0 == encoder) {
760                 printf("FAILED, new returned NULL\n");
761                 return false;
762         }
763         printf("OK\n");
764
765         printf("testing is_valid()... ");
766         if(!encoder->is_valid()) {
767                 printf("FAILED, returned false\n");
768                 return false;
769         }
770         printf("OK\n");
771
772         printf("testing set_verify()... ");
773         if(!encoder->set_verify(true))
774                 return encoder->die("returned false");
775         printf("OK\n");
776
777         printf("testing set_streamable_subset()... ");
778         if(!encoder->set_streamable_subset(true))
779                 return encoder->die("returned false");
780         printf("OK\n");
781
782         printf("testing set_do_mid_side_stereo()... ");
783         if(!encoder->set_do_mid_side_stereo(false))
784                 return encoder->die("returned false");
785         printf("OK\n");
786
787         printf("testing set_loose_mid_side_stereo()... ");
788         if(!encoder->set_loose_mid_side_stereo(false))
789                 return encoder->die("returned false");
790         printf("OK\n");
791
792         printf("testing set_channels()... ");
793         if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
794                 return encoder->die("returned false");
795         printf("OK\n");
796
797         printf("testing set_bits_per_sample()... ");
798         if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample))
799                 return encoder->die("returned false");
800         printf("OK\n");
801
802         printf("testing set_sample_rate()... ");
803         if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate))
804                 return encoder->die("returned false");
805         printf("OK\n");
806
807         printf("testing set_blocksize()... ");
808         if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
809                 return encoder->die("returned false");
810         printf("OK\n");
811
812         printf("testing set_max_lpc_order()... ");
813         if(!encoder->set_max_lpc_order(0))
814                 return encoder->die("returned false");
815         printf("OK\n");
816
817         printf("testing set_qlp_coeff_precision()... ");
818         if(!encoder->set_qlp_coeff_precision(0))
819                 return encoder->die("returned false");
820         printf("OK\n");
821
822         printf("testing set_do_qlp_coeff_prec_search()... ");
823         if(!encoder->set_do_qlp_coeff_prec_search(false))
824                 return encoder->die("returned false");
825         printf("OK\n");
826
827         printf("testing set_do_escape_coding()... ");
828         if(!encoder->set_do_escape_coding(false))
829                 return encoder->die("returned false");
830         printf("OK\n");
831
832         printf("testing set_do_exhaustive_model_search()... ");
833         if(!encoder->set_do_exhaustive_model_search(false))
834                 return encoder->die("returned false");
835         printf("OK\n");
836
837         printf("testing set_min_residual_partition_order()... ");
838         if(!encoder->set_min_residual_partition_order(0))
839                 return encoder->die("returned false");
840         printf("OK\n");
841
842         printf("testing set_max_residual_partition_order()... ");
843         if(!encoder->set_max_residual_partition_order(0))
844                 return encoder->die("returned false");
845         printf("OK\n");
846
847         printf("testing set_rice_parameter_search_dist()... ");
848         if(!encoder->set_rice_parameter_search_dist(0))
849                 return encoder->die("returned false");
850         printf("OK\n");
851
852         printf("testing set_total_samples_estimate()... ");
853         if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples))
854                 return encoder->die("returned false");
855         printf("OK\n");
856
857         printf("testing set_metadata()... ");
858         if(!encoder->set_metadata(metadata_sequence_, num_metadata_))
859                 return encoder->die("returned false");
860         printf("OK\n");
861
862         printf("testing set_filename()... ");
863         if(!encoder->set_filename(flacfilename_))
864                 return encoder->die("returned false");
865         printf("OK\n");
866
867         printf("testing init()... ");
868         if(encoder->init() != ::FLAC__FILE_ENCODER_OK)
869                 return encoder->die();
870         printf("OK\n");
871
872         printf("testing get_state()... ");
873         FLAC::Encoder::File::State state = encoder->get_state();
874         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__FileEncoderState)state), state.as_cstring());
875
876         printf("testing get_seekable_stream_encoder_state()... ");
877         FLAC::Encoder::SeekableStream::State state_ = encoder->get_seekable_stream_encoder_state();
878         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__SeekableStreamEncoderState)state_), state_.as_cstring());
879
880         printf("testing get_stream_encoder_state()... ");
881         FLAC::Encoder::Stream::State state__ = encoder->get_stream_encoder_state();
882         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamEncoderState)state__), state__.as_cstring());
883
884         printf("testing get_verify_decoder_state()... ");
885         FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
886         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
887
888         {
889                 FLAC__uint64 absolute_sample;
890                 unsigned frame_number;
891                 unsigned channel;
892                 unsigned sample;
893                 FLAC__int32 expected;
894                 FLAC__int32 got;
895
896                 printf("testing get_verify_decoder_error_stats()... ");
897                 encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got);
898                 printf("OK\n");
899         }
900
901         printf("testing get_verify()... ");
902         if(encoder->get_verify() != true) {
903                 printf("FAILED, expected true, got false\n");
904                 return false;
905         }
906         printf("OK\n");
907
908         printf("testing get_streamable_subset()... ");
909         if(encoder->get_streamable_subset() != true) {
910                 printf("FAILED, expected true, got false\n");
911                 return false;
912         }
913         printf("OK\n");
914
915         printf("testing get_do_mid_side_stereo()... ");
916         if(encoder->get_do_mid_side_stereo() != false) {
917                 printf("FAILED, expected false, got true\n");
918                 return false;
919         }
920         printf("OK\n");
921
922         printf("testing get_loose_mid_side_stereo()... ");
923         if(encoder->get_loose_mid_side_stereo() != false) {
924                 printf("FAILED, expected false, got true\n");
925                 return false;
926         }
927         printf("OK\n");
928
929         printf("testing get_channels()... ");
930         if(encoder->get_channels() != streaminfo_.data.stream_info.channels) {
931                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, encoder->get_channels());
932                 return false;
933         }
934         printf("OK\n");
935
936         printf("testing get_bits_per_sample()... ");
937         if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) {
938                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample());
939                 return false;
940         }
941         printf("OK\n");
942
943         printf("testing get_sample_rate()... ");
944         if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) {
945                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate());
946                 return false;
947         }
948         printf("OK\n");
949
950         printf("testing get_blocksize()... ");
951         if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) {
952                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize());
953                 return false;
954         }
955         printf("OK\n");
956
957         printf("testing get_max_lpc_order()... ");
958         if(encoder->get_max_lpc_order() != 0) {
959                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_lpc_order());
960                 return false;
961         }
962         printf("OK\n");
963
964         printf("testing get_qlp_coeff_precision()... ");
965         (void)encoder->get_qlp_coeff_precision();
966         /* we asked the encoder to auto select this so we accept anything */
967         printf("OK\n");
968
969         printf("testing get_do_qlp_coeff_prec_search()... ");
970         if(encoder->get_do_qlp_coeff_prec_search() != false) {
971                 printf("FAILED, expected false, got true\n");
972                 return false;
973         }
974         printf("OK\n");
975
976         printf("testing get_do_escape_coding()... ");
977         if(encoder->get_do_escape_coding() != false) {
978                 printf("FAILED, expected false, got true\n");
979                 return false;
980         }
981         printf("OK\n");
982
983         printf("testing get_do_exhaustive_model_search()... ");
984         if(encoder->get_do_exhaustive_model_search() != false) {
985                 printf("FAILED, expected false, got true\n");
986                 return false;
987         }
988         printf("OK\n");
989
990         printf("testing get_min_residual_partition_order()... ");
991         if(encoder->get_min_residual_partition_order() != 0) {
992                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_min_residual_partition_order());
993                 return false;
994         }
995         printf("OK\n");
996
997         printf("testing get_max_residual_partition_order()... ");
998         if(encoder->get_max_residual_partition_order() != 0) {
999                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_residual_partition_order());
1000                 return false;
1001         }
1002         printf("OK\n");
1003
1004         printf("testing get_rice_parameter_search_dist()... ");
1005         if(encoder->get_rice_parameter_search_dist() != 0) {
1006                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_rice_parameter_search_dist());
1007                 return false;
1008         }
1009         printf("OK\n");
1010
1011         printf("testing get_total_samples_estimate()... ");
1012         if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
1013                 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
1014                 return false;
1015         }
1016         printf("OK\n");
1017
1018         /* init the dummy sample buffer */
1019         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
1020                 samples[i] = i & 7;
1021
1022         printf("testing process()... ");
1023         if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32)))
1024                 return encoder->die("returned false");
1025         printf("OK\n");
1026
1027         printf("testing process_interleaved()... ");
1028         if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32)))
1029                 return encoder->die("returned false");
1030         printf("OK\n");
1031
1032         printf("testing finish()... ");
1033         encoder->finish();
1034         printf("OK\n");
1035
1036         printf("freeing encoder instance... ");
1037         delete encoder;
1038         printf("OK\n");
1039
1040         printf("\nPASSED!\n");
1041
1042         return true;
1043 }
1044
1045 bool test_encoders()
1046 {
1047         init_metadata_blocks_();
1048
1049         if(!test_stream_encoder())
1050                 return false;
1051
1052         if(!test_seekable_stream_encoder())
1053                 return false;
1054
1055         if(!test_file_encoder())
1056                 return false;
1057
1058         free_metadata_blocks_();
1059
1060         return true;
1061 }