1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 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 along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "FLAC/assert.h"
29 #include "FLAC/stream_encoder.h"
30 #include "share/grabbag.h"
31 #include "share/compat.h"
32 #include "test_libs_common/file_utils_flac.h"
33 #include "test_libs_common/metadata_utils.h"
36 LAYER_STREAM = 0, /* FLAC__stream_encoder_init_[ogg_]stream() without seeking */
37 LAYER_SEEKABLE_STREAM, /* FLAC__stream_encoder_init_[ogg_]stream() with seeking */
38 LAYER_FILE, /* FLAC__stream_encoder_init_[ogg_]FILE() */
39 LAYER_FILENAME /* FLAC__stream_encoder_init_[ogg_]file() */
42 static const char * const LayerString[] = {
49 static FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, picture_, unknown_;
50 static FLAC__StreamMetadata *metadata_sequence_[] = { &vorbiscomment_, &padding_, &seektable_, &application1_, &application2_, &cuesheet_, &picture_, &unknown_ };
51 static const unsigned num_metadata_ = sizeof(metadata_sequence_) / sizeof(metadata_sequence_[0]);
53 static const char *flacfilename(FLAC__bool is_ogg)
55 return is_ogg? "metadata.oga" : "metadata.flac";
58 static FLAC__bool die_(const char *msg)
60 printf("ERROR: %s\n", msg);
64 static FLAC__bool die_s_(const char *msg, const FLAC__StreamEncoder *encoder)
66 FLAC__StreamEncoderState state = FLAC__stream_encoder_get_state(encoder);
69 printf("FAILED, %s", msg);
73 printf(", state = %u (%s)\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
74 if(state == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
75 FLAC__StreamDecoderState dstate = FLAC__stream_encoder_get_verify_decoder_state(encoder);
76 printf(" verify decoder state = %u (%s)\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
82 static void init_metadata_blocks_(void)
84 mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
87 static void free_metadata_blocks_(void)
89 mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
92 static FLAC__StreamEncoderReadStatus stream_encoder_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
94 FILE *f = (FILE*)client_data;
97 *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, f);
99 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
101 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
103 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
106 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
109 static FLAC__StreamEncoderWriteStatus stream_encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
111 FILE *f = (FILE*)client_data;
112 (void)encoder, (void)samples, (void)current_frame;
113 if(fwrite(buffer, 1, bytes, f) != bytes)
114 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
116 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
119 static FLAC__StreamEncoderSeekStatus stream_encoder_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
121 FILE *f = (FILE*)client_data;
123 if(fseeko(f, (long)absolute_byte_offset, SEEK_SET) < 0)
124 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
126 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
129 static FLAC__StreamEncoderTellStatus stream_encoder_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
131 FILE *f = (FILE*)client_data;
134 if((pos = ftello(f)) < 0)
135 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
137 *absolute_byte_offset = (FLAC__uint64)pos;
138 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
142 static void stream_encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
144 (void)encoder, (void)metadata, (void)client_data;
147 static void stream_encoder_progress_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
149 (void)encoder, (void)bytes_written, (void)samples_written, (void)frames_written, (void)total_frames_estimate, (void)client_data;
152 static FLAC__bool test_stream_encoder(Layer layer, FLAC__bool is_ogg)
154 FLAC__StreamEncoder *encoder;
155 FLAC__StreamEncoderInitStatus init_status;
156 FLAC__StreamEncoderState state;
157 FLAC__StreamDecoderState dstate;
159 FLAC__int32 samples[1024];
160 FLAC__int32 *samples_array[1];
163 samples_array[0] = samples;
165 printf("\n+++ libFLAC unit test: FLAC__StreamEncoder (layer: %s, format: %s)\n\n", LayerString[layer], is_ogg? "Ogg FLAC":"FLAC");
167 printf("testing FLAC__stream_encoder_new()... ");
168 encoder = FLAC__stream_encoder_new();
170 printf("FAILED, returned NULL\n");
176 printf("testing FLAC__stream_encoder_set_ogg_serial_number()... ");
177 if(!FLAC__stream_encoder_set_ogg_serial_number(encoder, file_utils__ogg_serial_number))
178 return die_s_("returned false", encoder);
182 printf("testing FLAC__stream_encoder_set_verify()... ");
183 if(!FLAC__stream_encoder_set_verify(encoder, true))
184 return die_s_("returned false", encoder);
187 printf("testing FLAC__stream_encoder_set_streamable_subset()... ");
188 if(!FLAC__stream_encoder_set_streamable_subset(encoder, true))
189 return die_s_("returned false", encoder);
192 printf("testing FLAC__stream_encoder_set_channels()... ");
193 if(!FLAC__stream_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
194 return die_s_("returned false", encoder);
197 printf("testing FLAC__stream_encoder_set_bits_per_sample()... ");
198 if(!FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample))
199 return die_s_("returned false", encoder);
202 printf("testing FLAC__stream_encoder_set_sample_rate()... ");
203 if(!FLAC__stream_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
204 return die_s_("returned false", encoder);
207 printf("testing FLAC__stream_encoder_set_compression_level()... ");
208 if(!FLAC__stream_encoder_set_compression_level(encoder, (unsigned)(-1)))
209 return die_s_("returned false", encoder);
212 printf("testing FLAC__stream_encoder_set_blocksize()... ");
213 if(!FLAC__stream_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
214 return die_s_("returned false", encoder);
217 printf("testing FLAC__stream_encoder_set_do_mid_side_stereo()... ");
218 if(!FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false))
219 return die_s_("returned false", encoder);
222 printf("testing FLAC__stream_encoder_set_loose_mid_side_stereo()... ");
223 if(!FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false))
224 return die_s_("returned false", encoder);
227 printf("testing FLAC__stream_encoder_set_max_lpc_order()... ");
228 if(!FLAC__stream_encoder_set_max_lpc_order(encoder, 0))
229 return die_s_("returned false", encoder);
232 printf("testing FLAC__stream_encoder_set_qlp_coeff_precision()... ");
233 if(!FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0))
234 return die_s_("returned false", encoder);
237 printf("testing FLAC__stream_encoder_set_do_qlp_coeff_prec_search()... ");
238 if(!FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false))
239 return die_s_("returned false", encoder);
242 printf("testing FLAC__stream_encoder_set_do_escape_coding()... ");
243 if(!FLAC__stream_encoder_set_do_escape_coding(encoder, false))
244 return die_s_("returned false", encoder);
247 printf("testing FLAC__stream_encoder_set_do_exhaustive_model_search()... ");
248 if(!FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false))
249 return die_s_("returned false", encoder);
252 printf("testing FLAC__stream_encoder_set_min_residual_partition_order()... ");
253 if(!FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0))
254 return die_s_("returned false", encoder);
257 printf("testing FLAC__stream_encoder_set_max_residual_partition_order()... ");
258 if(!FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0))
259 return die_s_("returned false", encoder);
262 printf("testing FLAC__stream_encoder_set_rice_parameter_search_dist()... ");
263 if(!FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0))
264 return die_s_("returned false", encoder);
267 printf("testing FLAC__stream_encoder_set_total_samples_estimate()... ");
268 if(!FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
269 return die_s_("returned false", encoder);
272 printf("testing FLAC__stream_encoder_set_metadata()... ");
273 if(!FLAC__stream_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
274 return die_s_("returned false", encoder);
277 if(layer < LAYER_FILENAME) {
278 printf("opening file for FLAC output... ");
279 file = flac_fopen(flacfilename(is_ogg), "w+b");
281 printf("ERROR (%s)\n", strerror(errno));
289 printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":"");
290 init_status = is_ogg?
291 FLAC__stream_encoder_init_ogg_stream(encoder, /*read_callback=*/0, stream_encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, stream_encoder_metadata_callback_, /*client_data=*/file) :
292 FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, stream_encoder_metadata_callback_, /*client_data=*/file);
294 case LAYER_SEEKABLE_STREAM:
295 printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":"");
296 init_status = is_ogg?
297 FLAC__stream_encoder_init_ogg_stream(encoder, stream_encoder_read_callback_, stream_encoder_write_callback_, stream_encoder_seek_callback_, stream_encoder_tell_callback_, /*metadata_callback=*/0, /*client_data=*/file) :
298 FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, stream_encoder_seek_callback_, stream_encoder_tell_callback_, /*metadata_callback=*/0, /*client_data=*/file);
301 printf("testing FLAC__stream_encoder_init_%sFILE()... ", is_ogg? "ogg_":"");
302 init_status = is_ogg?
303 FLAC__stream_encoder_init_ogg_FILE(encoder, file, stream_encoder_progress_callback_, /*client_data=*/0) :
304 FLAC__stream_encoder_init_FILE(encoder, file, stream_encoder_progress_callback_, /*client_data=*/0);
307 printf("testing FLAC__stream_encoder_init_%sfile()... ", is_ogg? "ogg_":"");
308 init_status = is_ogg?
309 FLAC__stream_encoder_init_ogg_file(encoder, flacfilename(is_ogg), stream_encoder_progress_callback_, /*client_data=*/0) :
310 FLAC__stream_encoder_init_file(encoder, flacfilename(is_ogg), stream_encoder_progress_callback_, /*client_data=*/0);
313 die_("internal error 001");
316 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
317 return die_s_(0, encoder);
320 printf("testing FLAC__stream_encoder_get_state()... ");
321 state = FLAC__stream_encoder_get_state(encoder);
322 printf("returned state = %u (%s)... OK\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
324 printf("testing FLAC__stream_encoder_get_verify_decoder_state()... ");
325 dstate = FLAC__stream_encoder_get_verify_decoder_state(encoder);
326 printf("returned state = %u (%s)... OK\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
329 FLAC__uint64 absolute_sample;
330 unsigned frame_number;
333 FLAC__int32 expected;
336 printf("testing FLAC__stream_encoder_get_verify_decoder_error_stats()... ");
337 FLAC__stream_encoder_get_verify_decoder_error_stats(encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
341 printf("testing FLAC__stream_encoder_get_verify()... ");
342 if(FLAC__stream_encoder_get_verify(encoder) != true) {
343 printf("FAILED, expected true, got false\n");
348 printf("testing FLAC__stream_encoder_get_streamable_subset()... ");
349 if(FLAC__stream_encoder_get_streamable_subset(encoder) != true) {
350 printf("FAILED, expected true, got false\n");
355 printf("testing FLAC__stream_encoder_get_do_mid_side_stereo()... ");
356 if(FLAC__stream_encoder_get_do_mid_side_stereo(encoder) != false) {
357 printf("FAILED, expected false, got true\n");
362 printf("testing FLAC__stream_encoder_get_loose_mid_side_stereo()... ");
363 if(FLAC__stream_encoder_get_loose_mid_side_stereo(encoder) != false) {
364 printf("FAILED, expected false, got true\n");
369 printf("testing FLAC__stream_encoder_get_channels()... ");
370 if(FLAC__stream_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
371 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, FLAC__stream_encoder_get_channels(encoder));
376 printf("testing FLAC__stream_encoder_get_bits_per_sample()... ");
377 if(FLAC__stream_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
378 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, FLAC__stream_encoder_get_bits_per_sample(encoder));
383 printf("testing FLAC__stream_encoder_get_sample_rate()... ");
384 if(FLAC__stream_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
385 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, FLAC__stream_encoder_get_sample_rate(encoder));
390 printf("testing FLAC__stream_encoder_get_blocksize()... ");
391 if(FLAC__stream_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
392 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, FLAC__stream_encoder_get_blocksize(encoder));
397 printf("testing FLAC__stream_encoder_get_max_lpc_order()... ");
398 if(FLAC__stream_encoder_get_max_lpc_order(encoder) != 0) {
399 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_lpc_order(encoder));
404 printf("testing FLAC__stream_encoder_get_qlp_coeff_precision()... ");
405 (void)FLAC__stream_encoder_get_qlp_coeff_precision(encoder);
406 /* we asked the encoder to auto select this so we accept anything */
409 printf("testing FLAC__stream_encoder_get_do_qlp_coeff_prec_search()... ");
410 if(FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
411 printf("FAILED, expected false, got true\n");
416 printf("testing FLAC__stream_encoder_get_do_escape_coding()... ");
417 if(FLAC__stream_encoder_get_do_escape_coding(encoder) != false) {
418 printf("FAILED, expected false, got true\n");
423 printf("testing FLAC__stream_encoder_get_do_exhaustive_model_search()... ");
424 if(FLAC__stream_encoder_get_do_exhaustive_model_search(encoder) != false) {
425 printf("FAILED, expected false, got true\n");
430 printf("testing FLAC__stream_encoder_get_min_residual_partition_order()... ");
431 if(FLAC__stream_encoder_get_min_residual_partition_order(encoder) != 0) {
432 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_min_residual_partition_order(encoder));
437 printf("testing FLAC__stream_encoder_get_max_residual_partition_order()... ");
438 if(FLAC__stream_encoder_get_max_residual_partition_order(encoder) != 0) {
439 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_residual_partition_order(encoder));
444 printf("testing FLAC__stream_encoder_get_rice_parameter_search_dist()... ");
445 if(FLAC__stream_encoder_get_rice_parameter_search_dist(encoder) != 0) {
446 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_rice_parameter_search_dist(encoder));
451 printf("testing FLAC__stream_encoder_get_total_samples_estimate()... ");
452 if(FLAC__stream_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) {
453 printf("FAILED, expected %" PRIu64 ", got %" PRIu64 "\n", streaminfo_.data.stream_info.total_samples, FLAC__stream_encoder_get_total_samples_estimate(encoder));
458 /* init the dummy sample buffer */
459 for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
462 printf("testing FLAC__stream_encoder_process()... ");
463 if(!FLAC__stream_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
464 return die_s_("returned false", encoder);
467 printf("testing FLAC__stream_encoder_process_interleaved()... ");
468 if(!FLAC__stream_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
469 return die_s_("returned false", encoder);
472 printf("testing FLAC__stream_encoder_finish()... ");
473 if(!FLAC__stream_encoder_finish(encoder))
474 return die_s_("returned false", encoder);
477 if(layer < LAYER_FILE)
480 printf("testing FLAC__stream_encoder_delete()... ");
481 FLAC__stream_encoder_delete(encoder);
484 printf("\nPASSED!\n");
489 FLAC__bool test_encoders(void)
491 FLAC__bool is_ogg = false;
494 init_metadata_blocks_();
496 if(!test_stream_encoder(LAYER_STREAM, is_ogg))
499 if(!test_stream_encoder(LAYER_SEEKABLE_STREAM, is_ogg))
502 if(!test_stream_encoder(LAYER_FILE, is_ogg))
505 if(!test_stream_encoder(LAYER_FILENAME, is_ogg))
508 (void) grabbag__file_remove_file(flacfilename(is_ogg));
510 free_metadata_blocks_();
512 if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg)