1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2002 Josh Coalson
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.
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.
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.
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"
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";
34 static FLAC__bool die_s_(const char *msg, const FLAC__StreamEncoder *encoder)
36 FLAC__StreamEncoderState state = FLAC__stream_encoder_get_state(encoder);
39 printf("FAILED, %s", msg);
43 printf(", state = %u (%s)\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
44 if(state == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
45 FLAC__StreamDecoderState dstate = FLAC__stream_encoder_get_verify_decoder_state(encoder);
46 printf(" verify decoder state = %u (%s)\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
52 static FLAC__bool die_ss_(const char *msg, const FLAC__SeekableStreamEncoder *encoder)
54 FLAC__SeekableStreamEncoderState state = FLAC__seekable_stream_encoder_get_state(encoder);
57 printf("FAILED, %s", msg);
61 printf(", state = %u (%s)\n", (unsigned)state, FLAC__SeekableStreamEncoderStateString[state]);
62 if(state == FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR) {
63 FLAC__StreamEncoderState state_ = FLAC__seekable_stream_encoder_get_stream_encoder_state(encoder);
64 printf(" stream encoder state = %u (%s)\n", (unsigned)state_, FLAC__StreamEncoderStateString[state_]);
65 if(state_ == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
66 FLAC__StreamDecoderState dstate = FLAC__seekable_stream_encoder_get_verify_decoder_state(encoder);
67 printf(" verify decoder state = %u (%s)\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
74 static FLAC__bool die_f_(const char *msg, const FLAC__FileEncoder *encoder)
76 FLAC__FileEncoderState state = FLAC__file_encoder_get_state(encoder);
79 printf("FAILED, %s", msg);
83 printf(", state = %u (%s)\n", (unsigned)state, FLAC__FileEncoderStateString[state]);
84 if(state == FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR) {
85 FLAC__SeekableStreamEncoderState state_ = FLAC__file_encoder_get_seekable_stream_encoder_state(encoder);
86 printf(" seekable stream encoder state = %u (%s)\n", (unsigned)state_, FLAC__SeekableStreamEncoderStateString[state_]);
87 if(state_ == FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR) {
88 FLAC__StreamEncoderState state__ = FLAC__file_encoder_get_stream_encoder_state(encoder);
89 printf(" stream encoder state = %u (%s)\n", (unsigned)state__, FLAC__StreamEncoderStateString[state__]);
90 if(state__ == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
91 FLAC__StreamDecoderState dstate = FLAC__file_encoder_get_verify_decoder_state(encoder);
92 printf(" verify decoder state = %u (%s)\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
100 static void *malloc_or_die_(size_t size)
102 void *x = malloc(size);
104 fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (unsigned)size);
110 static void init_metadata_blocks_()
113 most of the actual numbers and data in the blocks don't matter,
114 we just want to make sure the encoder encodes them correctly
116 remember, the metadata interface gets tested after the encoders,
117 so we do all the metadata manipulation here without it.
120 /* min/max_framesize and md5sum don't get written at first, so we have to leave them 0 */
121 streaminfo_.is_last = false;
122 streaminfo_.type = FLAC__METADATA_TYPE_STREAMINFO;
123 streaminfo_.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
124 streaminfo_.data.stream_info.min_blocksize = 576;
125 streaminfo_.data.stream_info.max_blocksize = 576;
126 streaminfo_.data.stream_info.min_framesize = 0;
127 streaminfo_.data.stream_info.max_framesize = 0;
128 streaminfo_.data.stream_info.sample_rate = 44100;
129 streaminfo_.data.stream_info.channels = 1;
130 streaminfo_.data.stream_info.bits_per_sample = 8;
131 streaminfo_.data.stream_info.total_samples = 0;
132 memset(streaminfo_.data.stream_info.md5sum, 0, 16);
134 padding_.is_last = false;
135 padding_.type = FLAC__METADATA_TYPE_PADDING;
136 padding_.length = 1234;
138 seektable_.is_last = false;
139 seektable_.type = FLAC__METADATA_TYPE_SEEKTABLE;
140 seektable_.data.seek_table.num_points = 2;
141 seektable_.length = seektable_.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
142 seektable_.data.seek_table.points = malloc_or_die_(seektable_.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
143 seektable_.data.seek_table.points[0].sample_number = 0;
144 seektable_.data.seek_table.points[0].stream_offset = 0;
145 seektable_.data.seek_table.points[0].frame_samples = streaminfo_.data.stream_info.min_blocksize;
146 seektable_.data.seek_table.points[1].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
147 seektable_.data.seek_table.points[1].stream_offset = 1000;
148 seektable_.data.seek_table.points[1].frame_samples = streaminfo_.data.stream_info.min_blocksize;
150 application1_.is_last = false;
151 application1_.type = FLAC__METADATA_TYPE_APPLICATION;
152 application1_.length = 8;
153 memcpy(application1_.data.application.id, "\xfe\xdc\xba\x98", 4);
154 application1_.data.application.data = malloc_or_die_(4);
155 memcpy(application1_.data.application.data, "\xf0\xe1\xd2\xc3", 4);
157 application2_.is_last = false;
158 application2_.type = FLAC__METADATA_TYPE_APPLICATION;
159 application2_.length = 4;
160 memcpy(application2_.data.application.id, "\x76\x54\x32\x10", 4);
161 application2_.data.application.data = 0;
163 vorbiscomment_.is_last = true;
164 vorbiscomment_.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
165 vorbiscomment_.length = (4 + 8) + 4 + (4 + 5) + (4 + 0);
166 vorbiscomment_.data.vorbis_comment.vendor_string.length = 8;
167 vorbiscomment_.data.vorbis_comment.vendor_string.entry = malloc_or_die_(8);
168 memcpy(vorbiscomment_.data.vorbis_comment.vendor_string.entry, "flac 1.x", 8);
169 vorbiscomment_.data.vorbis_comment.num_comments = 2;
170 vorbiscomment_.data.vorbis_comment.comments = malloc_or_die_(vorbiscomment_.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
171 vorbiscomment_.data.vorbis_comment.comments[0].length = 5;
172 vorbiscomment_.data.vorbis_comment.comments[0].entry = malloc_or_die_(5);
173 memcpy(vorbiscomment_.data.vorbis_comment.comments[0].entry, "ab=cd", 5);
174 vorbiscomment_.data.vorbis_comment.comments[1].length = 0;
175 vorbiscomment_.data.vorbis_comment.comments[1].entry = 0;
178 static void free_metadata_blocks_()
180 free(seektable_.data.seek_table.points);
181 free(application1_.data.application.data);
182 free(vorbiscomment_.data.vorbis_comment.vendor_string.entry);
183 free(vorbiscomment_.data.vorbis_comment.comments[0].entry);
184 free(vorbiscomment_.data.vorbis_comment.comments);
187 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)
189 (void)encoder, (void)buffer, (void)bytes, (void)samples, (void)current_frame, (void)client_data;
190 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
193 static void stream_encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
195 (void)encoder, (void)metadata, (void)client_data;
198 static FLAC__bool test_stream_encoder()
200 FLAC__StreamEncoder *encoder;
201 FLAC__StreamEncoderState state;
202 FLAC__StreamDecoderState dstate;
203 FLAC__int32 samples[1024];
204 FLAC__int32 *samples_array[1] = { samples };
207 printf("\n+++ libFLAC unit test: FLAC__StreamEncoder\n\n");
209 printf("testing FLAC__stream_encoder_new()... ");
210 encoder = FLAC__stream_encoder_new();
212 printf("FAILED, returned NULL\n");
217 printf("testing FLAC__stream_encoder_set_verify()... ");
218 if(!FLAC__stream_encoder_set_verify(encoder, true))
219 return die_s_("returned false", encoder);
222 printf("testing FLAC__stream_encoder_set_streamable_subset()... ");
223 if(!FLAC__stream_encoder_set_streamable_subset(encoder, true))
224 return die_s_("returned false", encoder);
227 printf("testing FLAC__stream_encoder_set_do_mid_side_stereo()... ");
228 if(!FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false))
229 return die_s_("returned false", encoder);
232 printf("testing FLAC__stream_encoder_set_loose_mid_side_stereo()... ");
233 if(!FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false))
234 return die_s_("returned false", encoder);
237 printf("testing FLAC__stream_encoder_set_channels()... ");
238 if(!FLAC__stream_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
239 return die_s_("returned false", encoder);
242 printf("testing FLAC__stream_encoder_set_bits_per_sample()... ");
243 if(!FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample))
244 return die_s_("returned false", encoder);
247 printf("testing FLAC__stream_encoder_set_sample_rate()... ");
248 if(!FLAC__stream_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
249 return die_s_("returned false", encoder);
252 printf("testing FLAC__stream_encoder_set_blocksize()... ");
253 if(!FLAC__stream_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
254 return die_s_("returned false", encoder);
257 printf("testing FLAC__stream_encoder_set_max_lpc_order()... ");
258 if(!FLAC__stream_encoder_set_max_lpc_order(encoder, 0))
259 return die_s_("returned false", encoder);
262 printf("testing FLAC__stream_encoder_set_qlp_coeff_precision()... ");
263 if(!FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0))
264 return die_s_("returned false", encoder);
267 printf("testing FLAC__stream_encoder_set_do_qlp_coeff_prec_search()... ");
268 if(!FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false))
269 return die_s_("returned false", encoder);
272 printf("testing FLAC__stream_encoder_set_do_escape_coding()... ");
273 if(!FLAC__stream_encoder_set_do_escape_coding(encoder, false))
274 return die_s_("returned false", encoder);
277 printf("testing FLAC__stream_encoder_set_do_exhaustive_model_search()... ");
278 if(!FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false))
279 return die_s_("returned false", encoder);
282 printf("testing FLAC__stream_encoder_set_min_residual_partition_order()... ");
283 if(!FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0))
284 return die_s_("returned false", encoder);
287 printf("testing FLAC__stream_encoder_set_max_residual_partition_order()... ");
288 if(!FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0))
289 return die_s_("returned false", encoder);
292 printf("testing FLAC__stream_encoder_set_rice_parameter_search_dist()... ");
293 if(!FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0))
294 return die_s_("returned false", encoder);
297 printf("testing FLAC__stream_encoder_set_total_samples_estimate()... ");
298 if(!FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
299 return die_s_("returned false", encoder);
302 printf("testing FLAC__stream_encoder_set_metadata()... ");
303 if(!FLAC__stream_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
304 return die_s_("returned false", encoder);
307 printf("testing FLAC__stream_encoder_set_write_callback()... ");
308 if(!FLAC__stream_encoder_set_write_callback(encoder, stream_encoder_write_callback_))
309 return die_s_("returned false", encoder);
312 printf("testing FLAC__stream_encoder_set_metadata_callback()... ");
313 if(!FLAC__stream_encoder_set_metadata_callback(encoder, stream_encoder_metadata_callback_))
314 return die_s_("returned false", encoder);
317 printf("testing FLAC__stream_encoder_set_client_data()... ");
318 if(!FLAC__stream_encoder_set_client_data(encoder, 0))
319 return die_s_("returned false", encoder);
322 printf("testing FLAC__stream_encoder_init()... ");
323 if(FLAC__stream_encoder_init(encoder) != FLAC__STREAM_ENCODER_OK)
324 return die_s_(0, encoder);
327 printf("testing FLAC__stream_encoder_get_state()... ");
328 state = FLAC__stream_encoder_get_state(encoder);
329 printf("returned state = %u (%s)... OK\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
331 printf("testing FLAC__stream_encoder_get_verify_decoder_state()... ");
332 dstate = FLAC__stream_encoder_get_verify_decoder_state(encoder);
333 printf("returned state = %u (%s)... OK\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
336 FLAC__uint64 absolute_sample;
337 unsigned frame_number;
340 FLAC__int32 expected;
343 printf("testing FLAC__stream_encoder_get_verify_decoder_error_stats()... ");
344 FLAC__stream_encoder_get_verify_decoder_error_stats(encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
348 printf("testing FLAC__stream_encoder_get_verify()... ");
349 if(FLAC__stream_encoder_get_verify(encoder) != true) {
350 printf("FAILED, expected true, got false\n");
355 printf("testing FLAC__stream_encoder_get_streamable_subset()... ");
356 if(FLAC__stream_encoder_get_streamable_subset(encoder) != true) {
357 printf("FAILED, expected true, got false\n");
362 printf("testing FLAC__stream_encoder_get_do_mid_side_stereo()... ");
363 if(FLAC__stream_encoder_get_do_mid_side_stereo(encoder) != false) {
364 printf("FAILED, expected false, got true\n");
369 printf("testing FLAC__stream_encoder_get_loose_mid_side_stereo()... ");
370 if(FLAC__stream_encoder_get_loose_mid_side_stereo(encoder) != false) {
371 printf("FAILED, expected false, got true\n");
376 printf("testing FLAC__stream_encoder_get_channels()... ");
377 if(FLAC__stream_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
378 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, FLAC__stream_encoder_get_channels(encoder));
383 printf("testing FLAC__stream_encoder_get_bits_per_sample()... ");
384 if(FLAC__stream_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
385 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, FLAC__stream_encoder_get_bits_per_sample(encoder));
390 printf("testing FLAC__stream_encoder_get_sample_rate()... ");
391 if(FLAC__stream_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
392 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, FLAC__stream_encoder_get_sample_rate(encoder));
397 printf("testing FLAC__stream_encoder_get_blocksize()... ");
398 if(FLAC__stream_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
399 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, FLAC__stream_encoder_get_blocksize(encoder));
404 printf("testing FLAC__stream_encoder_get_max_lpc_order()... ");
405 if(FLAC__stream_encoder_get_max_lpc_order(encoder) != 0) {
406 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_lpc_order(encoder));
411 printf("testing FLAC__stream_encoder_get_qlp_coeff_precision()... ");
412 (void)FLAC__stream_encoder_get_qlp_coeff_precision(encoder);
413 /* we asked the encoder to auto select this so we accept anything */
416 printf("testing FLAC__stream_encoder_get_do_qlp_coeff_prec_search()... ");
417 if(FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
418 printf("FAILED, expected false, got true\n");
423 printf("testing FLAC__stream_encoder_get_do_escape_coding()... ");
424 if(FLAC__stream_encoder_get_do_escape_coding(encoder) != false) {
425 printf("FAILED, expected false, got true\n");
430 printf("testing FLAC__stream_encoder_get_do_exhaustive_model_search()... ");
431 if(FLAC__stream_encoder_get_do_exhaustive_model_search(encoder) != false) {
432 printf("FAILED, expected false, got true\n");
437 printf("testing FLAC__stream_encoder_get_min_residual_partition_order()... ");
438 if(FLAC__stream_encoder_get_min_residual_partition_order(encoder) != 0) {
439 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_min_residual_partition_order(encoder));
444 printf("testing FLAC__stream_encoder_get_max_residual_partition_order()... ");
445 if(FLAC__stream_encoder_get_max_residual_partition_order(encoder) != 0) {
446 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_residual_partition_order(encoder));
451 printf("testing FLAC__stream_encoder_get_rice_parameter_search_dist()... ");
452 if(FLAC__stream_encoder_get_rice_parameter_search_dist(encoder) != 0) {
453 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_rice_parameter_search_dist(encoder));
458 printf("testing FLAC__stream_encoder_get_total_samples_estimate()... ");
459 if(FLAC__stream_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) {
460 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, FLAC__stream_encoder_get_total_samples_estimate(encoder));
465 /* init the dummy sample buffer */
466 for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
469 printf("testing FLAC__stream_encoder_process()... ");
470 if(!FLAC__stream_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
471 return die_s_("returned false", encoder);
474 printf("testing FLAC__stream_encoder_process_interleaved()... ");
475 if(!FLAC__stream_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
476 return die_s_("returned false", encoder);
479 printf("testing FLAC__stream_encoder_finish()... ");
480 FLAC__stream_encoder_finish(encoder);
483 printf("testing FLAC__stream_encoder_delete()... ");
484 FLAC__stream_encoder_delete(encoder);
487 printf("\nPASSED!\n");
492 FLAC__SeekableStreamEncoderSeekStatus seekable_stream_encoder_seek_callback_(const FLAC__SeekableStreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
494 (void)encoder, (void)absolute_byte_offset, (void)client_data;
495 return FLAC__SEEKABLE_STREAM_ENCODER_SEEK_STATUS_OK;
498 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)
500 (void)encoder, (void)buffer, (void)bytes, (void)samples, (void)current_frame, (void)client_data;
501 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
504 static FLAC__bool test_seekable_stream_encoder()
506 FLAC__SeekableStreamEncoder *encoder;
507 FLAC__SeekableStreamEncoderState state;
508 FLAC__StreamEncoderState state_;
509 FLAC__StreamDecoderState dstate;
510 FLAC__int32 samples[1024];
511 FLAC__int32 *samples_array[1] = { samples };
514 printf("\n+++ libFLAC unit test: FLAC__SeekableStreamEncoder\n\n");
516 printf("testing FLAC__seekable_stream_encoder_new()... ");
517 encoder = FLAC__seekable_stream_encoder_new();
519 printf("FAILED, returned NULL\n");
524 printf("testing FLAC__seekable_stream_encoder_set_verify()... ");
525 if(!FLAC__seekable_stream_encoder_set_verify(encoder, true))
526 return die_ss_("returned false", encoder);
529 printf("testing FLAC__seekable_stream_encoder_set_streamable_subset()... ");
530 if(!FLAC__seekable_stream_encoder_set_streamable_subset(encoder, true))
531 return die_ss_("returned false", encoder);
534 printf("testing FLAC__seekable_stream_encoder_set_do_mid_side_stereo()... ");
535 if(!FLAC__seekable_stream_encoder_set_do_mid_side_stereo(encoder, false))
536 return die_ss_("returned false", encoder);
539 printf("testing FLAC__seekable_stream_encoder_set_loose_mid_side_stereo()... ");
540 if(!FLAC__seekable_stream_encoder_set_loose_mid_side_stereo(encoder, false))
541 return die_ss_("returned false", encoder);
544 printf("testing FLAC__seekable_stream_encoder_set_channels()... ");
545 if(!FLAC__seekable_stream_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
546 return die_ss_("returned false", encoder);
549 printf("testing FLAC__seekable_stream_encoder_set_bits_per_sample()... ");
550 if(!FLAC__seekable_stream_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample))
551 return die_ss_("returned false", encoder);
554 printf("testing FLAC__seekable_stream_encoder_set_sample_rate()... ");
555 if(!FLAC__seekable_stream_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
556 return die_ss_("returned false", encoder);
559 printf("testing FLAC__seekable_stream_encoder_set_blocksize()... ");
560 if(!FLAC__seekable_stream_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
561 return die_ss_("returned false", encoder);
564 printf("testing FLAC__seekable_stream_encoder_set_max_lpc_order()... ");
565 if(!FLAC__seekable_stream_encoder_set_max_lpc_order(encoder, 0))
566 return die_ss_("returned false", encoder);
569 printf("testing FLAC__seekable_stream_encoder_set_qlp_coeff_precision()... ");
570 if(!FLAC__seekable_stream_encoder_set_qlp_coeff_precision(encoder, 0))
571 return die_ss_("returned false", encoder);
574 printf("testing FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search()... ");
575 if(!FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search(encoder, false))
576 return die_ss_("returned false", encoder);
579 printf("testing FLAC__seekable_stream_encoder_set_do_escape_coding()... ");
580 if(!FLAC__seekable_stream_encoder_set_do_escape_coding(encoder, false))
581 return die_ss_("returned false", encoder);
584 printf("testing FLAC__seekable_stream_encoder_set_do_exhaustive_model_search()... ");
585 if(!FLAC__seekable_stream_encoder_set_do_exhaustive_model_search(encoder, false))
586 return die_ss_("returned false", encoder);
589 printf("testing FLAC__seekable_stream_encoder_set_min_residual_partition_order()... ");
590 if(!FLAC__seekable_stream_encoder_set_min_residual_partition_order(encoder, 0))
591 return die_ss_("returned false", encoder);
594 printf("testing FLAC__seekable_stream_encoder_set_max_residual_partition_order()... ");
595 if(!FLAC__seekable_stream_encoder_set_max_residual_partition_order(encoder, 0))
596 return die_ss_("returned false", encoder);
599 printf("testing FLAC__seekable_stream_encoder_set_rice_parameter_search_dist()... ");
600 if(!FLAC__seekable_stream_encoder_set_rice_parameter_search_dist(encoder, 0))
601 return die_ss_("returned false", encoder);
604 printf("testing FLAC__seekable_stream_encoder_set_total_samples_estimate()... ");
605 if(!FLAC__seekable_stream_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
606 return die_ss_("returned false", encoder);
609 printf("testing FLAC__seekable_stream_encoder_set_metadata()... ");
610 if(!FLAC__seekable_stream_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
611 return die_ss_("returned false", encoder);
614 printf("testing FLAC__seekable_stream_encoder_set_seek_callback()... ");
615 if(!FLAC__seekable_stream_encoder_set_seek_callback(encoder, seekable_stream_encoder_seek_callback_))
616 return die_ss_("returned false", encoder);
619 printf("testing FLAC__seekable_stream_encoder_set_write_callback()... ");
620 if(!FLAC__seekable_stream_encoder_set_write_callback(encoder, seekable_stream_encoder_write_callback_))
621 return die_ss_("returned false", encoder);
624 printf("testing FLAC__seekable_stream_encoder_set_client_data()... ");
625 if(!FLAC__seekable_stream_encoder_set_client_data(encoder, 0))
626 return die_ss_("returned false", encoder);
629 printf("testing FLAC__seekable_stream_encoder_init()... ");
630 if(FLAC__seekable_stream_encoder_init(encoder) != FLAC__SEEKABLE_STREAM_ENCODER_OK)
631 return die_ss_(0, encoder);
634 printf("testing FLAC__seekable_stream_encoder_get_state()... ");
635 state = FLAC__seekable_stream_encoder_get_state(encoder);
636 printf("returned state = %u (%s)... OK\n", (unsigned)state, FLAC__SeekableStreamEncoderStateString[state]);
638 printf("testing FLAC__seekable_stream_encoder_get_stream_encoder_state()... ");
639 state_ = FLAC__seekable_stream_encoder_get_stream_encoder_state(encoder);
640 printf("returned state = %u (%s)... OK\n", (unsigned)state_, FLAC__StreamEncoderStateString[state_]);
642 printf("testing FLAC__seekable_stream_encoder_get_verify_decoder_state()... ");
643 dstate = FLAC__seekable_stream_encoder_get_verify_decoder_state(encoder);
644 printf("returned state = %u (%s)... OK\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
647 FLAC__uint64 absolute_sample;
648 unsigned frame_number;
651 FLAC__int32 expected;
654 printf("testing FLAC__seekable_stream_encoder_get_verify_decoder_error_stats()... ");
655 FLAC__seekable_stream_encoder_get_verify_decoder_error_stats(encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
659 printf("testing FLAC__seekable_stream_encoder_get_verify()... ");
660 if(FLAC__seekable_stream_encoder_get_verify(encoder) != true) {
661 printf("FAILED, expected true, got false\n");
666 printf("testing FLAC__seekable_stream_encoder_get_streamable_subset()... ");
667 if(FLAC__seekable_stream_encoder_get_streamable_subset(encoder) != true) {
668 printf("FAILED, expected true, got false\n");
673 printf("testing FLAC__seekable_stream_encoder_get_do_mid_side_stereo()... ");
674 if(FLAC__seekable_stream_encoder_get_do_mid_side_stereo(encoder) != false) {
675 printf("FAILED, expected false, got true\n");
680 printf("testing FLAC__seekable_stream_encoder_get_loose_mid_side_stereo()... ");
681 if(FLAC__seekable_stream_encoder_get_loose_mid_side_stereo(encoder) != false) {
682 printf("FAILED, expected false, got true\n");
687 printf("testing FLAC__seekable_stream_encoder_get_channels()... ");
688 if(FLAC__seekable_stream_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
689 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, FLAC__seekable_stream_encoder_get_channels(encoder));
694 printf("testing FLAC__seekable_stream_encoder_get_bits_per_sample()... ");
695 if(FLAC__seekable_stream_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
696 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, FLAC__seekable_stream_encoder_get_bits_per_sample(encoder));
701 printf("testing FLAC__seekable_stream_encoder_get_sample_rate()... ");
702 if(FLAC__seekable_stream_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
703 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, FLAC__seekable_stream_encoder_get_sample_rate(encoder));
708 printf("testing FLAC__seekable_stream_encoder_get_blocksize()... ");
709 if(FLAC__seekable_stream_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
710 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, FLAC__seekable_stream_encoder_get_blocksize(encoder));
715 printf("testing FLAC__seekable_stream_encoder_get_max_lpc_order()... ");
716 if(FLAC__seekable_stream_encoder_get_max_lpc_order(encoder) != 0) {
717 printf("FAILED, expected %u, got %u\n", 0, FLAC__seekable_stream_encoder_get_max_lpc_order(encoder));
722 printf("testing FLAC__seekable_stream_encoder_get_qlp_coeff_precision()... ");
723 (void)FLAC__seekable_stream_encoder_get_qlp_coeff_precision(encoder);
724 /* we asked the encoder to auto select this so we accept anything */
727 printf("testing FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search()... ");
728 if(FLAC__seekable_stream_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
729 printf("FAILED, expected false, got true\n");
734 printf("testing FLAC__seekable_stream_encoder_get_do_escape_coding()... ");
735 if(FLAC__seekable_stream_encoder_get_do_escape_coding(encoder) != false) {
736 printf("FAILED, expected false, got true\n");
741 printf("testing FLAC__seekable_stream_encoder_get_do_exhaustive_model_search()... ");
742 if(FLAC__seekable_stream_encoder_get_do_exhaustive_model_search(encoder) != false) {
743 printf("FAILED, expected false, got true\n");
748 printf("testing FLAC__seekable_stream_encoder_get_min_residual_partition_order()... ");
749 if(FLAC__seekable_stream_encoder_get_min_residual_partition_order(encoder) != 0) {
750 printf("FAILED, expected %u, got %u\n", 0, FLAC__seekable_stream_encoder_get_min_residual_partition_order(encoder));
755 printf("testing FLAC__seekable_stream_encoder_get_max_residual_partition_order()... ");
756 if(FLAC__seekable_stream_encoder_get_max_residual_partition_order(encoder) != 0) {
757 printf("FAILED, expected %u, got %u\n", 0, FLAC__seekable_stream_encoder_get_max_residual_partition_order(encoder));
762 printf("testing FLAC__seekable_stream_encoder_get_rice_parameter_search_dist()... ");
763 if(FLAC__seekable_stream_encoder_get_rice_parameter_search_dist(encoder) != 0) {
764 printf("FAILED, expected %u, got %u\n", 0, FLAC__seekable_stream_encoder_get_rice_parameter_search_dist(encoder));
769 printf("testing FLAC__seekable_stream_encoder_get_total_samples_estimate()... ");
770 if(FLAC__seekable_stream_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) {
771 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, FLAC__seekable_stream_encoder_get_total_samples_estimate(encoder));
776 /* init the dummy sample buffer */
777 for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
780 printf("testing FLAC__seekable_stream_encoder_process()... ");
781 if(!FLAC__seekable_stream_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
782 return die_ss_("returned false", encoder);
785 printf("testing FLAC__seekable_stream_encoder_process_interleaved()... ");
786 if(!FLAC__seekable_stream_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
787 return die_ss_("returned false", encoder);
790 printf("testing FLAC__seekable_stream_encoder_finish()... ");
791 FLAC__seekable_stream_encoder_finish(encoder);
794 printf("testing FLAC__seekable_stream_encoder_delete()... ");
795 FLAC__seekable_stream_encoder_delete(encoder);
798 printf("\nPASSED!\n");
803 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)
805 (void)encoder, (void)bytes_written, (void)samples_written, (void)frames_written, (void)total_frames_estimate, (void)client_data;
808 static FLAC__bool test_file_encoder()
810 FLAC__FileEncoder *encoder;
811 FLAC__FileEncoderState state;
812 FLAC__SeekableStreamEncoderState state_;
813 FLAC__StreamEncoderState state__;
814 FLAC__StreamDecoderState dstate;
815 FLAC__int32 samples[1024];
816 FLAC__int32 *samples_array[1] = { samples };
819 printf("\n+++ libFLAC unit test: FLAC__FileEncoder\n\n");
821 printf("testing FLAC__file_encoder_new()... ");
822 encoder = FLAC__file_encoder_new();
824 printf("FAILED, returned NULL\n");
829 printf("testing FLAC__file_encoder_set_verify()... ");
830 if(!FLAC__file_encoder_set_verify(encoder, true))
831 return die_f_("returned false", encoder);
834 printf("testing FLAC__file_encoder_set_streamable_subset()... ");
835 if(!FLAC__file_encoder_set_streamable_subset(encoder, true))
836 return die_f_("returned false", encoder);
839 printf("testing FLAC__file_encoder_set_do_mid_side_stereo()... ");
840 if(!FLAC__file_encoder_set_do_mid_side_stereo(encoder, false))
841 return die_f_("returned false", encoder);
844 printf("testing FLAC__file_encoder_set_loose_mid_side_stereo()... ");
845 if(!FLAC__file_encoder_set_loose_mid_side_stereo(encoder, false))
846 return die_f_("returned false", encoder);
849 printf("testing FLAC__file_encoder_set_channels()... ");
850 if(!FLAC__file_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
851 return die_f_("returned false", encoder);
854 printf("testing FLAC__file_encoder_set_bits_per_sample()... ");
855 if(!FLAC__file_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample))
856 return die_f_("returned false", encoder);
859 printf("testing FLAC__file_encoder_set_sample_rate()... ");
860 if(!FLAC__file_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
861 return die_f_("returned false", encoder);
864 printf("testing FLAC__file_encoder_set_blocksize()... ");
865 if(!FLAC__file_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
866 return die_f_("returned false", encoder);
869 printf("testing FLAC__file_encoder_set_max_lpc_order()... ");
870 if(!FLAC__file_encoder_set_max_lpc_order(encoder, 0))
871 return die_f_("returned false", encoder);
874 printf("testing FLAC__file_encoder_set_qlp_coeff_precision()... ");
875 if(!FLAC__file_encoder_set_qlp_coeff_precision(encoder, 0))
876 return die_f_("returned false", encoder);
879 printf("testing FLAC__file_encoder_set_do_qlp_coeff_prec_search()... ");
880 if(!FLAC__file_encoder_set_do_qlp_coeff_prec_search(encoder, false))
881 return die_f_("returned false", encoder);
884 printf("testing FLAC__file_encoder_set_do_escape_coding()... ");
885 if(!FLAC__file_encoder_set_do_escape_coding(encoder, false))
886 return die_f_("returned false", encoder);
889 printf("testing FLAC__file_encoder_set_do_exhaustive_model_search()... ");
890 if(!FLAC__file_encoder_set_do_exhaustive_model_search(encoder, false))
891 return die_f_("returned false", encoder);
894 printf("testing FLAC__file_encoder_set_min_residual_partition_order()... ");
895 if(!FLAC__file_encoder_set_min_residual_partition_order(encoder, 0))
896 return die_f_("returned false", encoder);
899 printf("testing FLAC__file_encoder_set_max_residual_partition_order()... ");
900 if(!FLAC__file_encoder_set_max_residual_partition_order(encoder, 0))
901 return die_f_("returned false", encoder);
904 printf("testing FLAC__file_encoder_set_rice_parameter_search_dist()... ");
905 if(!FLAC__file_encoder_set_rice_parameter_search_dist(encoder, 0))
906 return die_f_("returned false", encoder);
909 printf("testing FLAC__file_encoder_set_total_samples_estimate()... ");
910 if(!FLAC__file_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
911 return die_f_("returned false", encoder);
914 printf("testing FLAC__file_encoder_set_metadata()... ");
915 if(!FLAC__file_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
916 return die_f_("returned false", encoder);
919 printf("testing FLAC__file_encoder_set_filename()... ");
920 if(!FLAC__file_encoder_set_filename(encoder, flacfilename_))
921 return die_f_("returned false", encoder);
924 printf("testing FLAC__file_encoder_set_progress_callback()... ");
925 if(!FLAC__file_encoder_set_progress_callback(encoder, file_encoder_progress_callback_))
926 return die_f_("returned false", encoder);
929 printf("testing FLAC__file_encoder_set_client_data()... ");
930 if(!FLAC__file_encoder_set_client_data(encoder, 0))
931 return die_f_("returned false", encoder);
934 printf("testing FLAC__file_encoder_init()... ");
935 if(FLAC__file_encoder_init(encoder) != FLAC__FILE_ENCODER_OK)
936 return die_f_(0, encoder);
939 printf("testing FLAC__file_encoder_get_state()... ");
940 state = FLAC__file_encoder_get_state(encoder);
941 printf("returned state = %u (%s)... OK\n", (unsigned)state, FLAC__FileEncoderStateString[state]);
943 printf("testing FLAC__file_encoder_get_seekable_stream_encoder_state()... ");
944 state_ = FLAC__file_encoder_get_seekable_stream_encoder_state(encoder);
945 printf("returned state = %u (%s)... OK\n", (unsigned)state_, FLAC__SeekableStreamEncoderStateString[state_]);
947 printf("testing FLAC__file_encoder_get_stream_encoder_state()... ");
948 state__ = FLAC__file_encoder_get_stream_encoder_state(encoder);
949 printf("returned state = %u (%s)... OK\n", (unsigned)state__, FLAC__StreamEncoderStateString[state__]);
951 printf("testing FLAC__file_encoder_get_verify_decoder_state()... ");
952 dstate = FLAC__file_encoder_get_verify_decoder_state(encoder);
953 printf("returned state = %u (%s)... OK\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
956 FLAC__uint64 absolute_sample;
957 unsigned frame_number;
960 FLAC__int32 expected;
963 printf("testing FLAC__file_encoder_get_verify_decoder_error_stats()... ");
964 FLAC__file_encoder_get_verify_decoder_error_stats(encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
968 printf("testing FLAC__file_encoder_get_verify()... ");
969 if(FLAC__file_encoder_get_verify(encoder) != true) {
970 printf("FAILED, expected true, got false\n");
975 printf("testing FLAC__file_encoder_get_streamable_subset()... ");
976 if(FLAC__file_encoder_get_streamable_subset(encoder) != true) {
977 printf("FAILED, expected true, got false\n");
982 printf("testing FLAC__file_encoder_get_do_mid_side_stereo()... ");
983 if(FLAC__file_encoder_get_do_mid_side_stereo(encoder) != false) {
984 printf("FAILED, expected false, got true\n");
989 printf("testing FLAC__file_encoder_get_loose_mid_side_stereo()... ");
990 if(FLAC__file_encoder_get_loose_mid_side_stereo(encoder) != false) {
991 printf("FAILED, expected false, got true\n");
996 printf("testing FLAC__file_encoder_get_channels()... ");
997 if(FLAC__file_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
998 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, FLAC__file_encoder_get_channels(encoder));
1003 printf("testing FLAC__file_encoder_get_bits_per_sample()... ");
1004 if(FLAC__file_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
1005 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, FLAC__file_encoder_get_bits_per_sample(encoder));
1010 printf("testing FLAC__file_encoder_get_sample_rate()... ");
1011 if(FLAC__file_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
1012 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, FLAC__file_encoder_get_sample_rate(encoder));
1017 printf("testing FLAC__file_encoder_get_blocksize()... ");
1018 if(FLAC__file_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
1019 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, FLAC__file_encoder_get_blocksize(encoder));
1024 printf("testing FLAC__file_encoder_get_max_lpc_order()... ");
1025 if(FLAC__file_encoder_get_max_lpc_order(encoder) != 0) {
1026 printf("FAILED, expected %u, got %u\n", 0, FLAC__file_encoder_get_max_lpc_order(encoder));
1031 printf("testing FLAC__file_encoder_get_qlp_coeff_precision()... ");
1032 (void)FLAC__file_encoder_get_qlp_coeff_precision(encoder);
1033 /* we asked the encoder to auto select this so we accept anything */
1036 printf("testing FLAC__file_encoder_get_do_qlp_coeff_prec_search()... ");
1037 if(FLAC__file_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
1038 printf("FAILED, expected false, got true\n");
1043 printf("testing FLAC__file_encoder_get_do_escape_coding()... ");
1044 if(FLAC__file_encoder_get_do_escape_coding(encoder) != false) {
1045 printf("FAILED, expected false, got true\n");
1050 printf("testing FLAC__file_encoder_get_do_exhaustive_model_search()... ");
1051 if(FLAC__file_encoder_get_do_exhaustive_model_search(encoder) != false) {
1052 printf("FAILED, expected false, got true\n");
1057 printf("testing FLAC__file_encoder_get_min_residual_partition_order()... ");
1058 if(FLAC__file_encoder_get_min_residual_partition_order(encoder) != 0) {
1059 printf("FAILED, expected %u, got %u\n", 0, FLAC__file_encoder_get_min_residual_partition_order(encoder));
1064 printf("testing FLAC__file_encoder_get_max_residual_partition_order()... ");
1065 if(FLAC__file_encoder_get_max_residual_partition_order(encoder) != 0) {
1066 printf("FAILED, expected %u, got %u\n", 0, FLAC__file_encoder_get_max_residual_partition_order(encoder));
1071 printf("testing FLAC__file_encoder_get_rice_parameter_search_dist()... ");
1072 if(FLAC__file_encoder_get_rice_parameter_search_dist(encoder) != 0) {
1073 printf("FAILED, expected %u, got %u\n", 0, FLAC__file_encoder_get_rice_parameter_search_dist(encoder));
1078 printf("testing FLAC__file_encoder_get_total_samples_estimate()... ");
1079 if(FLAC__file_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) {
1080 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, FLAC__file_encoder_get_total_samples_estimate(encoder));
1085 /* init the dummy sample buffer */
1086 for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
1089 printf("testing FLAC__file_encoder_process()... ");
1090 if(!FLAC__file_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
1091 return die_f_("returned false", encoder);
1094 printf("testing FLAC__file_encoder_process_interleaved()... ");
1095 if(!FLAC__file_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
1096 return die_f_("returned false", encoder);
1099 printf("testing FLAC__file_encoder_finish()... ");
1100 FLAC__file_encoder_finish(encoder);
1103 printf("testing FLAC__file_encoder_delete()... ");
1104 FLAC__file_encoder_delete(encoder);
1107 printf("\nPASSED!\n");
1112 FLAC__bool test_encoders()
1114 init_metadata_blocks_();
1116 if(!test_stream_encoder())
1119 if(!test_seekable_stream_encoder())
1122 if(!test_file_encoder())
1125 (void) file_utils__remove_file(flacfilename_);
1126 free_metadata_blocks_();