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