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