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