c0ab1931734c9128eb595103e62e1caab83fe33a
[flac.git] / src / test_libFLAC / encoders.c
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 #include "file_utils.h"
21 #include "FLAC/assert.h"
22 #include "FLAC/file_encoder.h"
23 #include "FLAC/seekable_stream_encoder.h"
24 #include "FLAC/stream_encoder.h"
25 #include "share/grabbag.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 static FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_;
31 static FLAC__StreamMetadata *metadata_sequence_[] = { &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_ };
32 static const unsigned num_metadata_ = 6;
33 static const char *flacfilename_ = "metadata.flac";
34
35 static FLAC__bool die_s_(const char *msg, const FLAC__StreamEncoder *encoder)
36 {
37         FLAC__StreamEncoderState state = FLAC__stream_encoder_get_state(encoder);
38
39         if(msg)
40                 printf("FAILED, %s", msg);
41         else
42                 printf("FAILED");
43
44         printf(", state = %u (%s)\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
45         if(state == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
46                 FLAC__StreamDecoderState dstate = FLAC__stream_encoder_get_verify_decoder_state(encoder);
47                 printf("      verify decoder state = %u (%s)\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
48         }
49
50         return false;
51 }
52
53 static FLAC__bool die_ss_(const char *msg, const FLAC__SeekableStreamEncoder *encoder)
54 {
55         FLAC__SeekableStreamEncoderState state = FLAC__seekable_stream_encoder_get_state(encoder);
56
57         if(msg)
58                 printf("FAILED, %s", msg);
59         else
60                 printf("FAILED");
61
62         printf(", state = %u (%s)\n", (unsigned)state, FLAC__SeekableStreamEncoderStateString[state]);
63         if(state == FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR) {
64                 FLAC__StreamEncoderState state_ = FLAC__seekable_stream_encoder_get_stream_encoder_state(encoder);
65                 printf("      stream encoder state = %u (%s)\n", (unsigned)state_, FLAC__StreamEncoderStateString[state_]);
66                 if(state_ == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
67                         FLAC__StreamDecoderState dstate = FLAC__seekable_stream_encoder_get_verify_decoder_state(encoder);
68                         printf("      verify decoder state = %u (%s)\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
69                 }
70         }
71
72         return false;
73 }
74
75 static FLAC__bool die_f_(const char *msg, const FLAC__FileEncoder *encoder)
76 {
77         FLAC__FileEncoderState state = FLAC__file_encoder_get_state(encoder);
78
79         if(msg)
80                 printf("FAILED, %s", msg);
81         else
82                 printf("FAILED");
83
84         printf(", state = %u (%s)\n", (unsigned)state, FLAC__FileEncoderStateString[state]);
85         if(state == FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR) {
86                 FLAC__SeekableStreamEncoderState state_ = FLAC__file_encoder_get_seekable_stream_encoder_state(encoder);
87                 printf("      seekable stream encoder state = %u (%s)\n", (unsigned)state_, FLAC__SeekableStreamEncoderStateString[state_]);
88                 if(state_ == FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR) {
89                         FLAC__StreamEncoderState state__ = FLAC__file_encoder_get_stream_encoder_state(encoder);
90                         printf("      stream encoder state = %u (%s)\n", (unsigned)state__, FLAC__StreamEncoderStateString[state__]);
91                         if(state__ == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
92                                 FLAC__StreamDecoderState dstate = FLAC__file_encoder_get_verify_decoder_state(encoder);
93                                 printf("      verify decoder state = %u (%s)\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
94                         }
95                 }
96         }
97
98         return false;
99 }
100
101 static void *malloc_or_die_(size_t size)
102 {
103         void *x = malloc(size);
104         if(0 == x) {
105                 fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (unsigned)size);
106                 exit(1);
107         }
108         return x;
109 }
110
111 static void init_metadata_blocks_()
112 {
113         /*
114                 most of the actual numbers and data in the blocks don't matter,
115                 we just want to make sure the encoder encodes them correctly
116
117                 remember, the metadata interface gets tested after the encoders,
118                 so we do all the metadata manipulation here without it.
119         */
120
121         /* min/max_framesize and md5sum don't get written at first, so we have to leave them 0 */
122         streaminfo_.is_last = false;
123         streaminfo_.type = FLAC__METADATA_TYPE_STREAMINFO;
124         streaminfo_.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
125         streaminfo_.data.stream_info.min_blocksize = 576;
126         streaminfo_.data.stream_info.max_blocksize = 576;
127         streaminfo_.data.stream_info.min_framesize = 0;
128         streaminfo_.data.stream_info.max_framesize = 0;
129         streaminfo_.data.stream_info.sample_rate = 44100;
130         streaminfo_.data.stream_info.channels = 1;
131         streaminfo_.data.stream_info.bits_per_sample = 8;
132         streaminfo_.data.stream_info.total_samples = 0;
133         memset(streaminfo_.data.stream_info.md5sum, 0, 16);
134
135         padding_.is_last = false;
136         padding_.type = FLAC__METADATA_TYPE_PADDING;
137         padding_.length = 1234;
138
139         seektable_.is_last = false;
140         seektable_.type = FLAC__METADATA_TYPE_SEEKTABLE;
141         seektable_.data.seek_table.num_points = 2;
142         seektable_.length = seektable_.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
143         seektable_.data.seek_table.points = malloc_or_die_(seektable_.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
144         seektable_.data.seek_table.points[0].sample_number = 0;
145         seektable_.data.seek_table.points[0].stream_offset = 0;
146         seektable_.data.seek_table.points[0].frame_samples = streaminfo_.data.stream_info.min_blocksize;
147         seektable_.data.seek_table.points[1].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
148         seektable_.data.seek_table.points[1].stream_offset = 1000;
149         seektable_.data.seek_table.points[1].frame_samples = streaminfo_.data.stream_info.min_blocksize;
150
151         application1_.is_last = false;
152         application1_.type = FLAC__METADATA_TYPE_APPLICATION;
153         application1_.length = 8;
154         memcpy(application1_.data.application.id, "\xfe\xdc\xba\x98", 4);
155         application1_.data.application.data = malloc_or_die_(4);
156         memcpy(application1_.data.application.data, "\xf0\xe1\xd2\xc3", 4);
157
158         application2_.is_last = false;
159         application2_.type = FLAC__METADATA_TYPE_APPLICATION;
160         application2_.length = 4;
161         memcpy(application2_.data.application.id, "\x76\x54\x32\x10", 4);
162         application2_.data.application.data = 0;
163
164         vorbiscomment_.is_last = false;
165         vorbiscomment_.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
166         vorbiscomment_.length = (4 + 8) + 4 + (4 + 5) + (4 + 0);
167         vorbiscomment_.data.vorbis_comment.vendor_string.length = 8;
168         vorbiscomment_.data.vorbis_comment.vendor_string.entry = malloc_or_die_(8);
169         memcpy(vorbiscomment_.data.vorbis_comment.vendor_string.entry, "flac 1.x", 8);
170         vorbiscomment_.data.vorbis_comment.num_comments = 2;
171         vorbiscomment_.data.vorbis_comment.comments = malloc_or_die_(vorbiscomment_.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
172         vorbiscomment_.data.vorbis_comment.comments[0].length = 5;
173         vorbiscomment_.data.vorbis_comment.comments[0].entry = malloc_or_die_(5);
174         memcpy(vorbiscomment_.data.vorbis_comment.comments[0].entry, "ab=cd", 5);
175         vorbiscomment_.data.vorbis_comment.comments[1].length = 0;
176         vorbiscomment_.data.vorbis_comment.comments[1].entry = 0;
177
178         cuesheet_.is_last = true;
179         cuesheet_.type = FLAC__METADATA_TYPE_CUESHEET;
180         cuesheet_.length =
181                 /* cuesheet guts */
182                 (
183                         FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN +
184                         FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN +
185                         FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN +
186                         FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN +
187                         FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN
188                 ) / 8 +
189                 /* 2 tracks */
190                 3 * (
191                         FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN +
192                         FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN +
193                         FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN +
194                         FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN +
195                         FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN +
196                         FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN +
197                         FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN
198                 ) / 8 +
199                 /* 3 index points */
200                 3 * (
201                         FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN +
202                         FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN +
203                         FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN
204                 ) / 8
205         ;
206         memset(cuesheet_.data.cue_sheet.media_catalog_number, 0, sizeof(cuesheet_.data.cue_sheet.media_catalog_number));
207         cuesheet_.data.cue_sheet.media_catalog_number[0] = 'j';
208         cuesheet_.data.cue_sheet.media_catalog_number[1] = 'C';
209         cuesheet_.data.cue_sheet.lead_in = 2 * 44100;
210         cuesheet_.data.cue_sheet.is_cd = true;
211         cuesheet_.data.cue_sheet.num_tracks = 3;
212         cuesheet_.data.cue_sheet.tracks = malloc_or_die_(cuesheet_.data.cue_sheet.num_tracks * sizeof(FLAC__StreamMetadata_CueSheet_Track));
213         cuesheet_.data.cue_sheet.tracks[0].offset = 0;
214         cuesheet_.data.cue_sheet.tracks[0].number = 1;
215         memcpy(cuesheet_.data.cue_sheet.tracks[0].isrc, "ACBDE1234567", sizeof(cuesheet_.data.cue_sheet.tracks[0].isrc));
216         cuesheet_.data.cue_sheet.tracks[0].type = 0;
217         cuesheet_.data.cue_sheet.tracks[0].pre_emphasis = 1;
218         cuesheet_.data.cue_sheet.tracks[0].num_indices = 2;
219         cuesheet_.data.cue_sheet.tracks[0].indices = malloc_or_die_(cuesheet_.data.cue_sheet.tracks[0].num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
220         cuesheet_.data.cue_sheet.tracks[0].indices[0].offset = 0;
221         cuesheet_.data.cue_sheet.tracks[0].indices[0].number = 0;
222         cuesheet_.data.cue_sheet.tracks[0].indices[1].offset = 123 * 588;
223         cuesheet_.data.cue_sheet.tracks[0].indices[1].number = 1;
224         cuesheet_.data.cue_sheet.tracks[1].offset = 1234 * 588;
225         cuesheet_.data.cue_sheet.tracks[1].number = 2;
226         memcpy(cuesheet_.data.cue_sheet.tracks[1].isrc, "ACBDE7654321", sizeof(cuesheet_.data.cue_sheet.tracks[1].isrc));
227         cuesheet_.data.cue_sheet.tracks[1].type = 1;
228         cuesheet_.data.cue_sheet.tracks[1].pre_emphasis = 0;
229         cuesheet_.data.cue_sheet.tracks[1].num_indices = 1;
230         cuesheet_.data.cue_sheet.tracks[1].indices = malloc_or_die_(cuesheet_.data.cue_sheet.tracks[1].num_indices * sizeof(FLAC__StreamMetadata_CueSheet_Index));
231         cuesheet_.data.cue_sheet.tracks[1].indices[0].offset = 0;
232         cuesheet_.data.cue_sheet.tracks[1].indices[0].number = 1;
233         cuesheet_.data.cue_sheet.tracks[2].offset = 12345 * 588;
234         cuesheet_.data.cue_sheet.tracks[2].number = 170;
235         cuesheet_.data.cue_sheet.tracks[2].num_indices = 0;
236 }
237
238 static void free_metadata_blocks_()
239 {
240         free(seektable_.data.seek_table.points);
241         free(application1_.data.application.data);
242         free(vorbiscomment_.data.vorbis_comment.vendor_string.entry);
243         free(vorbiscomment_.data.vorbis_comment.comments[0].entry);
244         free(vorbiscomment_.data.vorbis_comment.comments);
245         free(cuesheet_.data.cue_sheet.tracks[0].indices);
246         free(cuesheet_.data.cue_sheet.tracks[1].indices);
247         free(cuesheet_.data.cue_sheet.tracks);
248 }
249
250 static FLAC__StreamEncoderWriteStatus stream_encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
251 {
252         (void)encoder, (void)buffer, (void)bytes, (void)samples, (void)current_frame, (void)client_data;
253         return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
254 }
255
256 static void stream_encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
257 {
258         (void)encoder, (void)metadata, (void)client_data;
259 }
260
261 static FLAC__bool test_stream_encoder()
262 {
263         FLAC__StreamEncoder *encoder;
264         FLAC__StreamEncoderState state;
265         FLAC__StreamDecoderState dstate;
266         FLAC__int32 samples[1024];
267         FLAC__int32 *samples_array[1] = { samples };
268         unsigned i;
269
270         printf("\n+++ libFLAC unit test: FLAC__StreamEncoder\n\n");
271
272         printf("testing FLAC__stream_encoder_new()... ");
273         encoder = FLAC__stream_encoder_new();
274         if(0 == encoder) {
275                 printf("FAILED, returned NULL\n");
276                 return false;
277         }
278         printf("OK\n");
279
280         printf("testing FLAC__stream_encoder_set_verify()... ");
281         if(!FLAC__stream_encoder_set_verify(encoder, true))
282                 return die_s_("returned false", encoder);
283         printf("OK\n");
284
285         printf("testing FLAC__stream_encoder_set_streamable_subset()... ");
286         if(!FLAC__stream_encoder_set_streamable_subset(encoder, true))
287                 return die_s_("returned false", encoder);
288         printf("OK\n");
289
290         printf("testing FLAC__stream_encoder_set_do_mid_side_stereo()... ");
291         if(!FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false))
292                 return die_s_("returned false", encoder);
293         printf("OK\n");
294
295         printf("testing FLAC__stream_encoder_set_loose_mid_side_stereo()... ");
296         if(!FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false))
297                 return die_s_("returned false", encoder);
298         printf("OK\n");
299
300         printf("testing FLAC__stream_encoder_set_channels()... ");
301         if(!FLAC__stream_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
302                 return die_s_("returned false", encoder);
303         printf("OK\n");
304
305         printf("testing FLAC__stream_encoder_set_bits_per_sample()... ");
306         if(!FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample))
307                 return die_s_("returned false", encoder);
308         printf("OK\n");
309
310         printf("testing FLAC__stream_encoder_set_sample_rate()... ");
311         if(!FLAC__stream_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
312                 return die_s_("returned false", encoder);
313         printf("OK\n");
314
315         printf("testing FLAC__stream_encoder_set_blocksize()... ");
316         if(!FLAC__stream_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
317                 return die_s_("returned false", encoder);
318         printf("OK\n");
319
320         printf("testing FLAC__stream_encoder_set_max_lpc_order()... ");
321         if(!FLAC__stream_encoder_set_max_lpc_order(encoder, 0))
322                 return die_s_("returned false", encoder);
323         printf("OK\n");
324
325         printf("testing FLAC__stream_encoder_set_qlp_coeff_precision()... ");
326         if(!FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0))
327                 return die_s_("returned false", encoder);
328         printf("OK\n");
329
330         printf("testing FLAC__stream_encoder_set_do_qlp_coeff_prec_search()... ");
331         if(!FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false))
332                 return die_s_("returned false", encoder);
333         printf("OK\n");
334
335         printf("testing FLAC__stream_encoder_set_do_escape_coding()... ");
336         if(!FLAC__stream_encoder_set_do_escape_coding(encoder, false))
337                 return die_s_("returned false", encoder);
338         printf("OK\n");
339
340         printf("testing FLAC__stream_encoder_set_do_exhaustive_model_search()... ");
341         if(!FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false))
342                 return die_s_("returned false", encoder);
343         printf("OK\n");
344
345         printf("testing FLAC__stream_encoder_set_min_residual_partition_order()... ");
346         if(!FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0))
347                 return die_s_("returned false", encoder);
348         printf("OK\n");
349
350         printf("testing FLAC__stream_encoder_set_max_residual_partition_order()... ");
351         if(!FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0))
352                 return die_s_("returned false", encoder);
353         printf("OK\n");
354
355         printf("testing FLAC__stream_encoder_set_rice_parameter_search_dist()... ");
356         if(!FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0))
357                 return die_s_("returned false", encoder);
358         printf("OK\n");
359
360         printf("testing FLAC__stream_encoder_set_total_samples_estimate()... ");
361         if(!FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
362                 return die_s_("returned false", encoder);
363         printf("OK\n");
364
365         printf("testing FLAC__stream_encoder_set_metadata()... ");
366         if(!FLAC__stream_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
367                 return die_s_("returned false", encoder);
368         printf("OK\n");
369
370         printf("testing FLAC__stream_encoder_set_write_callback()... ");
371         if(!FLAC__stream_encoder_set_write_callback(encoder, stream_encoder_write_callback_))
372                 return die_s_("returned false", encoder);
373         printf("OK\n");
374
375         printf("testing FLAC__stream_encoder_set_metadata_callback()... ");
376         if(!FLAC__stream_encoder_set_metadata_callback(encoder, stream_encoder_metadata_callback_))
377                 return die_s_("returned false", encoder);
378         printf("OK\n");
379
380         printf("testing FLAC__stream_encoder_set_client_data()... ");
381         if(!FLAC__stream_encoder_set_client_data(encoder, 0))
382                 return die_s_("returned false", encoder);
383         printf("OK\n");
384
385         printf("testing FLAC__stream_encoder_init()... ");
386         if(FLAC__stream_encoder_init(encoder) != FLAC__STREAM_ENCODER_OK)
387                 return die_s_(0, encoder);
388         printf("OK\n");
389
390         printf("testing FLAC__stream_encoder_get_state()... ");
391         state = FLAC__stream_encoder_get_state(encoder);
392         printf("returned state = %u (%s)... OK\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
393
394         printf("testing FLAC__stream_encoder_get_verify_decoder_state()... ");
395         dstate = FLAC__stream_encoder_get_verify_decoder_state(encoder);
396         printf("returned state = %u (%s)... OK\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
397
398         {
399                 FLAC__uint64 absolute_sample;
400                 unsigned frame_number;
401                 unsigned channel;
402                 unsigned sample;
403                 FLAC__int32 expected;
404                 FLAC__int32 got;
405
406                 printf("testing FLAC__stream_encoder_get_verify_decoder_error_stats()... ");
407                 FLAC__stream_encoder_get_verify_decoder_error_stats(encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
408                 printf("OK\n");
409         }
410
411         printf("testing FLAC__stream_encoder_get_verify()... ");
412         if(FLAC__stream_encoder_get_verify(encoder) != true) {
413                 printf("FAILED, expected true, got false\n");
414                 return false;
415         }
416         printf("OK\n");
417
418         printf("testing FLAC__stream_encoder_get_streamable_subset()... ");
419         if(FLAC__stream_encoder_get_streamable_subset(encoder) != true) {
420                 printf("FAILED, expected true, got false\n");
421                 return false;
422         }
423         printf("OK\n");
424
425         printf("testing FLAC__stream_encoder_get_do_mid_side_stereo()... ");
426         if(FLAC__stream_encoder_get_do_mid_side_stereo(encoder) != false) {
427                 printf("FAILED, expected false, got true\n");
428                 return false;
429         }
430         printf("OK\n");
431
432         printf("testing FLAC__stream_encoder_get_loose_mid_side_stereo()... ");
433         if(FLAC__stream_encoder_get_loose_mid_side_stereo(encoder) != false) {
434                 printf("FAILED, expected false, got true\n");
435                 return false;
436         }
437         printf("OK\n");
438
439         printf("testing FLAC__stream_encoder_get_channels()... ");
440         if(FLAC__stream_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
441                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, FLAC__stream_encoder_get_channels(encoder));
442                 return false;
443         }
444         printf("OK\n");
445
446         printf("testing FLAC__stream_encoder_get_bits_per_sample()... ");
447         if(FLAC__stream_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
448                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, FLAC__stream_encoder_get_bits_per_sample(encoder));
449                 return false;
450         }
451         printf("OK\n");
452
453         printf("testing FLAC__stream_encoder_get_sample_rate()... ");
454         if(FLAC__stream_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
455                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, FLAC__stream_encoder_get_sample_rate(encoder));
456                 return false;
457         }
458         printf("OK\n");
459
460         printf("testing FLAC__stream_encoder_get_blocksize()... ");
461         if(FLAC__stream_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
462                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, FLAC__stream_encoder_get_blocksize(encoder));
463                 return false;
464         }
465         printf("OK\n");
466
467         printf("testing FLAC__stream_encoder_get_max_lpc_order()... ");
468         if(FLAC__stream_encoder_get_max_lpc_order(encoder) != 0) {
469                 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_lpc_order(encoder));
470                 return false;
471         }
472         printf("OK\n");
473
474         printf("testing FLAC__stream_encoder_get_qlp_coeff_precision()... ");
475         (void)FLAC__stream_encoder_get_qlp_coeff_precision(encoder);
476         /* we asked the encoder to auto select this so we accept anything */
477         printf("OK\n");
478
479         printf("testing FLAC__stream_encoder_get_do_qlp_coeff_prec_search()... ");
480         if(FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
481                 printf("FAILED, expected false, got true\n");
482                 return false;
483         }
484         printf("OK\n");
485
486         printf("testing FLAC__stream_encoder_get_do_escape_coding()... ");
487         if(FLAC__stream_encoder_get_do_escape_coding(encoder) != false) {
488                 printf("FAILED, expected false, got true\n");
489                 return false;
490         }
491         printf("OK\n");
492
493         printf("testing FLAC__stream_encoder_get_do_exhaustive_model_search()... ");
494         if(FLAC__stream_encoder_get_do_exhaustive_model_search(encoder) != false) {
495                 printf("FAILED, expected false, got true\n");
496                 return false;
497         }
498         printf("OK\n");
499
500         printf("testing FLAC__stream_encoder_get_min_residual_partition_order()... ");
501         if(FLAC__stream_encoder_get_min_residual_partition_order(encoder) != 0) {
502                 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_min_residual_partition_order(encoder));
503                 return false;
504         }
505         printf("OK\n");
506
507         printf("testing FLAC__stream_encoder_get_max_residual_partition_order()... ");
508         if(FLAC__stream_encoder_get_max_residual_partition_order(encoder) != 0) {
509                 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_residual_partition_order(encoder));
510                 return false;
511         }
512         printf("OK\n");
513
514         printf("testing FLAC__stream_encoder_get_rice_parameter_search_dist()... ");
515         if(FLAC__stream_encoder_get_rice_parameter_search_dist(encoder) != 0) {
516                 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_rice_parameter_search_dist(encoder));
517                 return false;
518         }
519         printf("OK\n");
520
521         printf("testing FLAC__stream_encoder_get_total_samples_estimate()... ");
522         if(FLAC__stream_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) {
523                 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, FLAC__stream_encoder_get_total_samples_estimate(encoder));
524                 return false;
525         }
526         printf("OK\n");
527
528         /* init the dummy sample buffer */
529         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
530                 samples[i] = i & 7;
531
532         printf("testing FLAC__stream_encoder_process()... ");
533         if(!FLAC__stream_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
534                 return die_s_("returned false", encoder);
535         printf("OK\n");
536
537         printf("testing FLAC__stream_encoder_process_interleaved()... ");
538         if(!FLAC__stream_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
539                 return die_s_("returned false", encoder);
540         printf("OK\n");
541
542         printf("testing FLAC__stream_encoder_finish()... ");
543         FLAC__stream_encoder_finish(encoder);
544         printf("OK\n");
545
546         printf("testing FLAC__stream_encoder_delete()... ");
547         FLAC__stream_encoder_delete(encoder);
548         printf("OK\n");
549
550         printf("\nPASSED!\n");
551
552         return true;
553 }
554
555 FLAC__SeekableStreamEncoderSeekStatus seekable_stream_encoder_seek_callback_(const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
556 {
557         (void)encoder, (void)absolute_byte_offset, (void)client_data;
558         return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK;
559 }
560
561 FLAC__StreamEncoderWriteStatus seekable_stream_encoder_write_callback_(const FLAC__SeekableStreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
562 {
563         (void)encoder, (void)buffer, (void)bytes, (void)samples, (void)current_frame, (void)client_data;
564         return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
565 }
566
567 static FLAC__bool test_seekable_stream_encoder()
568 {
569         FLAC__SeekableStreamEncoder *encoder;
570         FLAC__SeekableStreamEncoderState state;
571         FLAC__StreamEncoderState state_;
572         FLAC__StreamDecoderState dstate;
573         FLAC__int32 samples[1024];
574         FLAC__int32 *samples_array[1] = { samples };
575         unsigned i;
576
577         printf("\n+++ libFLAC unit test: FLAC__SeekableStreamEncoder\n\n");
578
579         printf("testing FLAC__seekable_stream_encoder_new()... ");
580         encoder = FLAC__seekable_stream_encoder_new();
581         if(0 == encoder) {
582                 printf("FAILED, returned NULL\n");
583                 return false;
584         }
585         printf("OK\n");
586
587         printf("testing FLAC__seekable_stream_encoder_set_verify()... ");
588         if(!FLAC__seekable_stream_encoder_set_verify(encoder, true))
589                 return die_ss_("returned false", encoder);
590         printf("OK\n");
591
592         printf("testing FLAC__seekable_stream_encoder_set_streamable_subset()... ");
593         if(!FLAC__seekable_stream_encoder_set_streamable_subset(encoder, true))
594                 return die_ss_("returned false", encoder);
595         printf("OK\n");
596
597         printf("testing FLAC__seekable_stream_encoder_set_do_mid_side_stereo()... ");
598         if(!FLAC__seekable_stream_encoder_set_do_mid_side_stereo(encoder, false))
599                 return die_ss_("returned false", encoder);
600         printf("OK\n");
601
602         printf("testing FLAC__seekable_stream_encoder_set_loose_mid_side_stereo()... ");
603         if(!FLAC__seekable_stream_encoder_set_loose_mid_side_stereo(encoder, false))
604                 return die_ss_("returned false", encoder);
605         printf("OK\n");
606
607         printf("testing FLAC__seekable_stream_encoder_set_channels()... ");
608         if(!FLAC__seekable_stream_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
609                 return die_ss_("returned false", encoder);
610         printf("OK\n");
611
612         printf("testing FLAC__seekable_stream_encoder_set_bits_per_sample()... ");
613         if(!FLAC__seekable_stream_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample))
614                 return die_ss_("returned false", encoder);
615         printf("OK\n");
616
617         printf("testing FLAC__seekable_stream_encoder_set_sample_rate()... ");
618         if(!FLAC__seekable_stream_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
619                 return die_ss_("returned false", encoder);
620         printf("OK\n");
621
622         printf("testing FLAC__seekable_stream_encoder_set_blocksize()... ");
623         if(!FLAC__seekable_stream_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
624                 return die_ss_("returned false", encoder);
625         printf("OK\n");
626
627         printf("testing FLAC__seekable_stream_encoder_set_max_lpc_order()... ");
628         if(!FLAC__seekable_stream_encoder_set_max_lpc_order(encoder, 0))
629                 return die_ss_("returned false", encoder);
630         printf("OK\n");
631
632         printf("testing FLAC__seekable_stream_encoder_set_qlp_coeff_precision()... ");
633         if(!FLAC__seekable_stream_encoder_set_qlp_coeff_precision(encoder, 0))
634                 return die_ss_("returned false", encoder);
635         printf("OK\n");
636
637         printf("testing FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search()... ");
638         if(!FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search(encoder, false))
639                 return die_ss_("returned false", encoder);
640         printf("OK\n");
641
642         printf("testing FLAC__seekable_stream_encoder_set_do_escape_coding()... ");
643         if(!FLAC__seekable_stream_encoder_set_do_escape_coding(encoder, false))
644                 return die_ss_("returned false", encoder);
645         printf("OK\n");
646
647         printf("testing FLAC__seekable_stream_encoder_set_do_exhaustive_model_search()... ");
648         if(!FLAC__seekable_stream_encoder_set_do_exhaustive_model_search(encoder, false))
649                 return die_ss_("returned false", encoder);
650         printf("OK\n");
651
652         printf("testing FLAC__seekable_stream_encoder_set_min_residual_partition_order()... ");
653         if(!FLAC__seekable_stream_encoder_set_min_residual_partition_order(encoder, 0))
654                 return die_ss_("returned false", encoder);
655         printf("OK\n");
656
657         printf("testing FLAC__seekable_stream_encoder_set_max_residual_partition_order()... ");
658         if(!FLAC__seekable_stream_encoder_set_max_residual_partition_order(encoder, 0))
659                 return die_ss_("returned false", encoder);
660         printf("OK\n");
661
662         printf("testing FLAC__seekable_stream_encoder_set_rice_parameter_search_dist()... ");
663         if(!FLAC__seekable_stream_encoder_set_rice_parameter_search_dist(encoder, 0))
664                 return die_ss_("returned false", encoder);
665         printf("OK\n");
666
667         printf("testing FLAC__seekable_stream_encoder_set_total_samples_estimate()... ");
668         if(!FLAC__seekable_stream_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
669                 return die_ss_("returned false", encoder);
670         printf("OK\n");
671
672         printf("testing FLAC__seekable_stream_encoder_set_metadata()... ");
673         if(!FLAC__seekable_stream_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
674                 return die_ss_("returned false", encoder);
675         printf("OK\n");
676
677         printf("testing FLAC__seekable_stream_encoder_set_seek_callback()... ");
678         if(!FLAC__seekable_stream_encoder_set_seek_callback(encoder, seekable_stream_encoder_seek_callback_))
679                 return die_ss_("returned false", encoder);
680         printf("OK\n");
681
682         printf("testing FLAC__seekable_stream_encoder_set_write_callback()... ");
683         if(!FLAC__seekable_stream_encoder_set_write_callback(encoder, seekable_stream_encoder_write_callback_))
684                 return die_ss_("returned false", encoder);
685         printf("OK\n");
686
687         printf("testing FLAC__seekable_stream_encoder_set_client_data()... ");
688         if(!FLAC__seekable_stream_encoder_set_client_data(encoder, 0))
689                 return die_ss_("returned false", encoder);
690         printf("OK\n");
691
692         printf("testing FLAC__seekable_stream_encoder_init()... ");
693         if(FLAC__seekable_stream_encoder_init(encoder) != FLAC__SEEKABLE_STREAM_ENCODER_OK)
694                 return die_ss_(0, encoder);
695         printf("OK\n");
696
697         printf("testing FLAC__seekable_stream_encoder_get_state()... ");
698         state = FLAC__seekable_stream_encoder_get_state(encoder);
699         printf("returned state = %u (%s)... OK\n", (unsigned)state, FLAC__SeekableStreamEncoderStateString[state]);
700
701         printf("testing FLAC__seekable_stream_encoder_get_stream_encoder_state()... ");
702         state_ = FLAC__seekable_stream_encoder_get_stream_encoder_state(encoder);
703         printf("returned state = %u (%s)... OK\n", (unsigned)state_, FLAC__StreamEncoderStateString[state_]);
704
705         printf("testing FLAC__seekable_stream_encoder_get_verify_decoder_state()... ");
706         dstate = FLAC__seekable_stream_encoder_get_verify_decoder_state(encoder);
707         printf("returned state = %u (%s)... OK\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
708
709         {
710                 FLAC__uint64 absolute_sample;
711                 unsigned frame_number;
712                 unsigned channel;
713                 unsigned sample;
714                 FLAC__int32 expected;
715                 FLAC__int32 got;
716
717                 printf("testing FLAC__seekable_stream_encoder_get_verify_decoder_error_stats()... ");
718                 FLAC__seekable_stream_encoder_get_verify_decoder_error_stats(encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
719                 printf("OK\n");
720         }
721
722         printf("testing FLAC__seekable_stream_encoder_get_verify()... ");
723         if(FLAC__seekable_stream_encoder_get_verify(encoder) != true) {
724                 printf("FAILED, expected true, got false\n");
725                 return false;
726         }
727         printf("OK\n");
728
729         printf("testing FLAC__seekable_stream_encoder_get_streamable_subset()... ");
730         if(FLAC__seekable_stream_encoder_get_streamable_subset(encoder) != true) {
731                 printf("FAILED, expected true, got false\n");
732                 return false;
733         }
734         printf("OK\n");
735
736         printf("testing FLAC__seekable_stream_encoder_get_do_mid_side_stereo()... ");
737         if(FLAC__seekable_stream_encoder_get_do_mid_side_stereo(encoder) != false) {
738                 printf("FAILED, expected false, got true\n");
739                 return false;
740         }
741         printf("OK\n");
742
743         printf("testing FLAC__seekable_stream_encoder_get_loose_mid_side_stereo()... ");
744         if(FLAC__seekable_stream_encoder_get_loose_mid_side_stereo(encoder) != false) {
745                 printf("FAILED, expected false, got true\n");
746                 return false;
747         }
748         printf("OK\n");
749
750         printf("testing FLAC__seekable_stream_encoder_get_channels()... ");
751         if(FLAC__seekable_stream_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
752                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, FLAC__seekable_stream_encoder_get_channels(encoder));
753                 return false;
754         }
755         printf("OK\n");
756
757         printf("testing FLAC__seekable_stream_encoder_get_bits_per_sample()... ");
758         if(FLAC__seekable_stream_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
759                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, FLAC__seekable_stream_encoder_get_bits_per_sample(encoder));
760                 return false;
761         }
762         printf("OK\n");
763
764         printf("testing FLAC__seekable_stream_encoder_get_sample_rate()... ");
765         if(FLAC__seekable_stream_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
766                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, FLAC__seekable_stream_encoder_get_sample_rate(encoder));
767                 return false;
768         }
769         printf("OK\n");
770
771         printf("testing FLAC__seekable_stream_encoder_get_blocksize()... ");
772         if(FLAC__seekable_stream_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
773                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, FLAC__seekable_stream_encoder_get_blocksize(encoder));
774                 return false;
775         }
776         printf("OK\n");
777
778         printf("testing FLAC__seekable_stream_encoder_get_max_lpc_order()... ");
779         if(FLAC__seekable_stream_encoder_get_max_lpc_order(encoder) != 0) {
780                 printf("FAILED, expected %u, got %u\n", 0, FLAC__seekable_stream_encoder_get_max_lpc_order(encoder));
781                 return false;
782         }
783         printf("OK\n");
784
785         printf("testing FLAC__seekable_stream_encoder_get_qlp_coeff_precision()... ");
786         (void)FLAC__seekable_stream_encoder_get_qlp_coeff_precision(encoder);
787         /* we asked the encoder to auto select this so we accept anything */
788         printf("OK\n");
789
790         printf("testing FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search()... ");
791         if(FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
792                 printf("FAILED, expected false, got true\n");
793                 return false;
794         }
795         printf("OK\n");
796
797         printf("testing FLAC__seekable_stream_encoder_get_do_escape_coding()... ");
798         if(FLAC__seekable_stream_encoder_get_do_escape_coding(encoder) != false) {
799                 printf("FAILED, expected false, got true\n");
800                 return false;
801         }
802         printf("OK\n");
803
804         printf("testing FLAC__seekable_stream_encoder_get_do_exhaustive_model_search()... ");
805         if(FLAC__seekable_stream_encoder_get_do_exhaustive_model_search(encoder) != false) {
806                 printf("FAILED, expected false, got true\n");
807                 return false;
808         }
809         printf("OK\n");
810
811         printf("testing FLAC__seekable_stream_encoder_get_min_residual_partition_order()... ");
812         if(FLAC__seekable_stream_encoder_get_min_residual_partition_order(encoder) != 0) {
813                 printf("FAILED, expected %u, got %u\n", 0, FLAC__seekable_stream_encoder_get_min_residual_partition_order(encoder));
814                 return false;
815         }
816         printf("OK\n");
817
818         printf("testing FLAC__seekable_stream_encoder_get_max_residual_partition_order()... ");
819         if(FLAC__seekable_stream_encoder_get_max_residual_partition_order(encoder) != 0) {
820                 printf("FAILED, expected %u, got %u\n", 0, FLAC__seekable_stream_encoder_get_max_residual_partition_order(encoder));
821                 return false;
822         }
823         printf("OK\n");
824
825         printf("testing FLAC__seekable_stream_encoder_get_rice_parameter_search_dist()... ");
826         if(FLAC__seekable_stream_encoder_get_rice_parameter_search_dist(encoder) != 0) {
827                 printf("FAILED, expected %u, got %u\n", 0, FLAC__seekable_stream_encoder_get_rice_parameter_search_dist(encoder));
828                 return false;
829         }
830         printf("OK\n");
831
832         printf("testing FLAC__seekable_stream_encoder_get_total_samples_estimate()... ");
833         if(FLAC__seekable_stream_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) {
834                 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, FLAC__seekable_stream_encoder_get_total_samples_estimate(encoder));
835                 return false;
836         }
837         printf("OK\n");
838
839         /* init the dummy sample buffer */
840         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
841                 samples[i] = i & 7;
842
843         printf("testing FLAC__seekable_stream_encoder_process()... ");
844         if(!FLAC__seekable_stream_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
845                 return die_ss_("returned false", encoder);
846         printf("OK\n");
847
848         printf("testing FLAC__seekable_stream_encoder_process_interleaved()... ");
849         if(!FLAC__seekable_stream_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
850                 return die_ss_("returned false", encoder);
851         printf("OK\n");
852
853         printf("testing FLAC__seekable_stream_encoder_finish()... ");
854         FLAC__seekable_stream_encoder_finish(encoder);
855         printf("OK\n");
856
857         printf("testing FLAC__seekable_stream_encoder_delete()... ");
858         FLAC__seekable_stream_encoder_delete(encoder);
859         printf("OK\n");
860
861         printf("\nPASSED!\n");
862
863         return true;
864 }
865
866 static void file_encoder_progress_callback_(const FLAC__FileEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
867 {
868         (void)encoder, (void)bytes_written, (void)samples_written, (void)frames_written, (void)total_frames_estimate, (void)client_data;
869 }
870
871 static FLAC__bool test_file_encoder()
872 {
873         FLAC__FileEncoder *encoder;
874         FLAC__FileEncoderState state;
875         FLAC__SeekableStreamEncoderState state_;
876         FLAC__StreamEncoderState state__;
877         FLAC__StreamDecoderState dstate;
878         FLAC__int32 samples[1024];
879         FLAC__int32 *samples_array[1] = { samples };
880         unsigned i;
881
882         printf("\n+++ libFLAC unit test: FLAC__FileEncoder\n\n");
883
884         printf("testing FLAC__file_encoder_new()... ");
885         encoder = FLAC__file_encoder_new();
886         if(0 == encoder) {
887                 printf("FAILED, returned NULL\n");
888                 return false;
889         }
890         printf("OK\n");
891
892         printf("testing FLAC__file_encoder_set_verify()... ");
893         if(!FLAC__file_encoder_set_verify(encoder, true))
894                 return die_f_("returned false", encoder);
895         printf("OK\n");
896
897         printf("testing FLAC__file_encoder_set_streamable_subset()... ");
898         if(!FLAC__file_encoder_set_streamable_subset(encoder, true))
899                 return die_f_("returned false", encoder);
900         printf("OK\n");
901
902         printf("testing FLAC__file_encoder_set_do_mid_side_stereo()... ");
903         if(!FLAC__file_encoder_set_do_mid_side_stereo(encoder, false))
904                 return die_f_("returned false", encoder);
905         printf("OK\n");
906
907         printf("testing FLAC__file_encoder_set_loose_mid_side_stereo()... ");
908         if(!FLAC__file_encoder_set_loose_mid_side_stereo(encoder, false))
909                 return die_f_("returned false", encoder);
910         printf("OK\n");
911
912         printf("testing FLAC__file_encoder_set_channels()... ");
913         if(!FLAC__file_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
914                 return die_f_("returned false", encoder);
915         printf("OK\n");
916
917         printf("testing FLAC__file_encoder_set_bits_per_sample()... ");
918         if(!FLAC__file_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample))
919                 return die_f_("returned false", encoder);
920         printf("OK\n");
921
922         printf("testing FLAC__file_encoder_set_sample_rate()... ");
923         if(!FLAC__file_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
924                 return die_f_("returned false", encoder);
925         printf("OK\n");
926
927         printf("testing FLAC__file_encoder_set_blocksize()... ");
928         if(!FLAC__file_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
929                 return die_f_("returned false", encoder);
930         printf("OK\n");
931
932         printf("testing FLAC__file_encoder_set_max_lpc_order()... ");
933         if(!FLAC__file_encoder_set_max_lpc_order(encoder, 0))
934                 return die_f_("returned false", encoder);
935         printf("OK\n");
936
937         printf("testing FLAC__file_encoder_set_qlp_coeff_precision()... ");
938         if(!FLAC__file_encoder_set_qlp_coeff_precision(encoder, 0))
939                 return die_f_("returned false", encoder);
940         printf("OK\n");
941
942         printf("testing FLAC__file_encoder_set_do_qlp_coeff_prec_search()... ");
943         if(!FLAC__file_encoder_set_do_qlp_coeff_prec_search(encoder, false))
944                 return die_f_("returned false", encoder);
945         printf("OK\n");
946
947         printf("testing FLAC__file_encoder_set_do_escape_coding()... ");
948         if(!FLAC__file_encoder_set_do_escape_coding(encoder, false))
949                 return die_f_("returned false", encoder);
950         printf("OK\n");
951
952         printf("testing FLAC__file_encoder_set_do_exhaustive_model_search()... ");
953         if(!FLAC__file_encoder_set_do_exhaustive_model_search(encoder, false))
954                 return die_f_("returned false", encoder);
955         printf("OK\n");
956
957         printf("testing FLAC__file_encoder_set_min_residual_partition_order()... ");
958         if(!FLAC__file_encoder_set_min_residual_partition_order(encoder, 0))
959                 return die_f_("returned false", encoder);
960         printf("OK\n");
961
962         printf("testing FLAC__file_encoder_set_max_residual_partition_order()... ");
963         if(!FLAC__file_encoder_set_max_residual_partition_order(encoder, 0))
964                 return die_f_("returned false", encoder);
965         printf("OK\n");
966
967         printf("testing FLAC__file_encoder_set_rice_parameter_search_dist()... ");
968         if(!FLAC__file_encoder_set_rice_parameter_search_dist(encoder, 0))
969                 return die_f_("returned false", encoder);
970         printf("OK\n");
971
972         printf("testing FLAC__file_encoder_set_total_samples_estimate()... ");
973         if(!FLAC__file_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
974                 return die_f_("returned false", encoder);
975         printf("OK\n");
976
977         printf("testing FLAC__file_encoder_set_metadata()... ");
978         if(!FLAC__file_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
979                 return die_f_("returned false", encoder);
980         printf("OK\n");
981
982         printf("testing FLAC__file_encoder_set_filename()... ");
983         if(!FLAC__file_encoder_set_filename(encoder, flacfilename_))
984                 return die_f_("returned false", encoder);
985         printf("OK\n");
986
987         printf("testing FLAC__file_encoder_set_progress_callback()... ");
988         if(!FLAC__file_encoder_set_progress_callback(encoder, file_encoder_progress_callback_))
989                 return die_f_("returned false", encoder);
990         printf("OK\n");
991
992         printf("testing FLAC__file_encoder_set_client_data()... ");
993         if(!FLAC__file_encoder_set_client_data(encoder, 0))
994                 return die_f_("returned false", encoder);
995         printf("OK\n");
996
997         printf("testing FLAC__file_encoder_init()... ");
998         if(FLAC__file_encoder_init(encoder) != FLAC__FILE_ENCODER_OK)
999                 return die_f_(0, encoder);
1000         printf("OK\n");
1001
1002         printf("testing FLAC__file_encoder_get_state()... ");
1003         state = FLAC__file_encoder_get_state(encoder);
1004         printf("returned state = %u (%s)... OK\n", (unsigned)state, FLAC__FileEncoderStateString[state]);
1005
1006         printf("testing FLAC__file_encoder_get_seekable_stream_encoder_state()... ");
1007         state_ = FLAC__file_encoder_get_seekable_stream_encoder_state(encoder);
1008         printf("returned state = %u (%s)... OK\n", (unsigned)state_, FLAC__SeekableStreamEncoderStateString[state_]);
1009
1010         printf("testing FLAC__file_encoder_get_stream_encoder_state()... ");
1011         state__ = FLAC__file_encoder_get_stream_encoder_state(encoder);
1012         printf("returned state = %u (%s)... OK\n", (unsigned)state__, FLAC__StreamEncoderStateString[state__]);
1013
1014         printf("testing FLAC__file_encoder_get_verify_decoder_state()... ");
1015         dstate = FLAC__file_encoder_get_verify_decoder_state(encoder);
1016         printf("returned state = %u (%s)... OK\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
1017
1018         {
1019                 FLAC__uint64 absolute_sample;
1020                 unsigned frame_number;
1021                 unsigned channel;
1022                 unsigned sample;
1023                 FLAC__int32 expected;
1024                 FLAC__int32 got;
1025
1026                 printf("testing FLAC__file_encoder_get_verify_decoder_error_stats()... ");
1027                 FLAC__file_encoder_get_verify_decoder_error_stats(encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
1028                 printf("OK\n");
1029         }
1030
1031         printf("testing FLAC__file_encoder_get_verify()... ");
1032         if(FLAC__file_encoder_get_verify(encoder) != true) {
1033                 printf("FAILED, expected true, got false\n");
1034                 return false;
1035         }
1036         printf("OK\n");
1037
1038         printf("testing FLAC__file_encoder_get_streamable_subset()... ");
1039         if(FLAC__file_encoder_get_streamable_subset(encoder) != true) {
1040                 printf("FAILED, expected true, got false\n");
1041                 return false;
1042         }
1043         printf("OK\n");
1044
1045         printf("testing FLAC__file_encoder_get_do_mid_side_stereo()... ");
1046         if(FLAC__file_encoder_get_do_mid_side_stereo(encoder) != false) {
1047                 printf("FAILED, expected false, got true\n");
1048                 return false;
1049         }
1050         printf("OK\n");
1051
1052         printf("testing FLAC__file_encoder_get_loose_mid_side_stereo()... ");
1053         if(FLAC__file_encoder_get_loose_mid_side_stereo(encoder) != false) {
1054                 printf("FAILED, expected false, got true\n");
1055                 return false;
1056         }
1057         printf("OK\n");
1058
1059         printf("testing FLAC__file_encoder_get_channels()... ");
1060         if(FLAC__file_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
1061                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, FLAC__file_encoder_get_channels(encoder));
1062                 return false;
1063         }
1064         printf("OK\n");
1065
1066         printf("testing FLAC__file_encoder_get_bits_per_sample()... ");
1067         if(FLAC__file_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
1068                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, FLAC__file_encoder_get_bits_per_sample(encoder));
1069                 return false;
1070         }
1071         printf("OK\n");
1072
1073         printf("testing FLAC__file_encoder_get_sample_rate()... ");
1074         if(FLAC__file_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
1075                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, FLAC__file_encoder_get_sample_rate(encoder));
1076                 return false;
1077         }
1078         printf("OK\n");
1079
1080         printf("testing FLAC__file_encoder_get_blocksize()... ");
1081         if(FLAC__file_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
1082                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, FLAC__file_encoder_get_blocksize(encoder));
1083                 return false;
1084         }
1085         printf("OK\n");
1086
1087         printf("testing FLAC__file_encoder_get_max_lpc_order()... ");
1088         if(FLAC__file_encoder_get_max_lpc_order(encoder) != 0) {
1089                 printf("FAILED, expected %u, got %u\n", 0, FLAC__file_encoder_get_max_lpc_order(encoder));
1090                 return false;
1091         }
1092         printf("OK\n");
1093
1094         printf("testing FLAC__file_encoder_get_qlp_coeff_precision()... ");
1095         (void)FLAC__file_encoder_get_qlp_coeff_precision(encoder);
1096         /* we asked the encoder to auto select this so we accept anything */
1097         printf("OK\n");
1098
1099         printf("testing FLAC__file_encoder_get_do_qlp_coeff_prec_search()... ");
1100         if(FLAC__file_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
1101                 printf("FAILED, expected false, got true\n");
1102                 return false;
1103         }
1104         printf("OK\n");
1105
1106         printf("testing FLAC__file_encoder_get_do_escape_coding()... ");
1107         if(FLAC__file_encoder_get_do_escape_coding(encoder) != false) {
1108                 printf("FAILED, expected false, got true\n");
1109                 return false;
1110         }
1111         printf("OK\n");
1112
1113         printf("testing FLAC__file_encoder_get_do_exhaustive_model_search()... ");
1114         if(FLAC__file_encoder_get_do_exhaustive_model_search(encoder) != false) {
1115                 printf("FAILED, expected false, got true\n");
1116                 return false;
1117         }
1118         printf("OK\n");
1119
1120         printf("testing FLAC__file_encoder_get_min_residual_partition_order()... ");
1121         if(FLAC__file_encoder_get_min_residual_partition_order(encoder) != 0) {
1122                 printf("FAILED, expected %u, got %u\n", 0, FLAC__file_encoder_get_min_residual_partition_order(encoder));
1123                 return false;
1124         }
1125         printf("OK\n");
1126
1127         printf("testing FLAC__file_encoder_get_max_residual_partition_order()... ");
1128         if(FLAC__file_encoder_get_max_residual_partition_order(encoder) != 0) {
1129                 printf("FAILED, expected %u, got %u\n", 0, FLAC__file_encoder_get_max_residual_partition_order(encoder));
1130                 return false;
1131         }
1132         printf("OK\n");
1133
1134         printf("testing FLAC__file_encoder_get_rice_parameter_search_dist()... ");
1135         if(FLAC__file_encoder_get_rice_parameter_search_dist(encoder) != 0) {
1136                 printf("FAILED, expected %u, got %u\n", 0, FLAC__file_encoder_get_rice_parameter_search_dist(encoder));
1137                 return false;
1138         }
1139         printf("OK\n");
1140
1141         printf("testing FLAC__file_encoder_get_total_samples_estimate()... ");
1142         if(FLAC__file_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) {
1143                 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, FLAC__file_encoder_get_total_samples_estimate(encoder));
1144                 return false;
1145         }
1146         printf("OK\n");
1147
1148         /* init the dummy sample buffer */
1149         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
1150                 samples[i] = i & 7;
1151
1152         printf("testing FLAC__file_encoder_process()... ");
1153         if(!FLAC__file_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
1154                 return die_f_("returned false", encoder);
1155         printf("OK\n");
1156
1157         printf("testing FLAC__file_encoder_process_interleaved()... ");
1158         if(!FLAC__file_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
1159                 return die_f_("returned false", encoder);
1160         printf("OK\n");
1161
1162         printf("testing FLAC__file_encoder_finish()... ");
1163         FLAC__file_encoder_finish(encoder);
1164         printf("OK\n");
1165
1166         printf("testing FLAC__file_encoder_delete()... ");
1167         FLAC__file_encoder_delete(encoder);
1168         printf("OK\n");
1169
1170         printf("\nPASSED!\n");
1171
1172         return true;
1173 }
1174
1175 FLAC__bool test_encoders()
1176 {
1177         init_metadata_blocks_();
1178
1179         if(!test_stream_encoder())
1180                 return false;
1181
1182         if(!test_seekable_stream_encoder())
1183                 return false;
1184
1185         if(!test_file_encoder())
1186                 return false;
1187
1188         (void) grabbag__file_remove_file(flacfilename_);
1189         free_metadata_blocks_();
1190
1191         return true;
1192 }