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