1 /* test_libFLAC++ - Unit tester for libFLAC++
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "FLAC/assert.h"
22 #include "FLAC++/encoder.h"
23 #include "share/grabbag.h"
25 #include "test_libs_common/file_utils_flac.h"
26 #include "test_libs_common/metadata_utils.h"
32 #include "share/compat.h"
35 LAYER_STREAM = 0, /* FLAC__stream_encoder_init_stream() without seeking */
36 LAYER_SEEKABLE_STREAM, /* FLAC__stream_encoder_init_stream() with seeking */
37 LAYER_FILE, /* FLAC__stream_encoder_init_FILE() */
38 LAYER_FILENAME /* FLAC__stream_encoder_init_file() */
41 static const char * const LayerString[] = {
48 static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, picture_, unknown_;
49 static ::FLAC__StreamMetadata *metadata_sequence_[] = { &vorbiscomment_, &padding_, &seektable_, &application1_, &application2_, &cuesheet_, &picture_, &unknown_ };
50 static const unsigned num_metadata_ = sizeof(metadata_sequence_) / sizeof(metadata_sequence_[0]);
52 static const char *flacfilename(bool is_ogg)
54 return is_ogg? "metadata.oga" : "metadata.flac";
57 static bool die_(const char *msg)
59 printf("ERROR: %s\n", msg);
63 static bool die_s_(const char *msg, const FLAC::Encoder::Stream *encoder)
65 FLAC::Encoder::Stream::State state = encoder->get_state();
68 printf("FAILED, %s", msg);
72 printf(", state = %u (%s)\n", (unsigned)((::FLAC__StreamEncoderState)state), state.as_cstring());
73 if(state == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
74 FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
75 printf(" verify decoder state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
81 static void init_metadata_blocks_()
83 mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
86 static void free_metadata_blocks_()
88 mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
91 class StreamEncoder : public FLAC::Encoder::Stream {
96 StreamEncoder(Layer layer): FLAC::Encoder::Stream(), layer_(layer), file_(0) { }
99 // from FLAC::Encoder::Stream
100 ::FLAC__StreamEncoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes);
101 ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame);
102 ::FLAC__StreamEncoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
103 ::FLAC__StreamEncoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
104 void metadata_callback(const ::FLAC__StreamMetadata *metadata);
107 ::FLAC__StreamEncoderReadStatus StreamEncoder::read_callback(FLAC__byte buffer[], size_t *bytes)
110 *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, file_);
112 return ::FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
114 return ::FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
116 return ::FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
119 return ::FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
122 ::FLAC__StreamEncoderWriteStatus StreamEncoder::write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame)
124 (void)samples, (void)current_frame;
126 if(fwrite(buffer, 1, bytes, file_) != bytes)
127 return ::FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
129 return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
132 ::FLAC__StreamEncoderSeekStatus StreamEncoder::seek_callback(FLAC__uint64 absolute_byte_offset)
134 if(layer_==LAYER_STREAM)
135 return ::FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED;
136 else if(fseeko(file_, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0)
137 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
139 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
142 ::FLAC__StreamEncoderTellStatus StreamEncoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
145 if(layer_==LAYER_STREAM)
146 return ::FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED;
147 else if((pos = ftello(file_)) < 0)
148 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
150 *absolute_byte_offset = (FLAC__uint64)pos;
151 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
155 void StreamEncoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
160 class FileEncoder : public FLAC::Encoder::File {
164 FileEncoder(Layer layer): FLAC::Encoder::File(), layer_(layer) { }
167 // from FLAC::Encoder::File
168 void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate);
171 void FileEncoder::progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate)
173 (void)bytes_written, (void)samples_written, (void)frames_written, (void)total_frames_estimate;
176 static FLAC::Encoder::Stream *new_by_layer(Layer layer)
178 if(layer < LAYER_FILE)
179 return new StreamEncoder(layer);
181 return new FileEncoder(layer);
184 static bool test_stream_encoder(Layer layer, bool is_ogg)
186 FLAC::Encoder::Stream *encoder;
187 ::FLAC__StreamEncoderInitStatus init_status;
189 FLAC__int32 samples[1024];
190 FLAC__int32 *samples_array[1] = { samples };
193 printf("\n+++ libFLAC++ unit test: FLAC::Encoder::%s (layer: %s, format: %s)\n\n", layer<LAYER_FILE? "Stream":"File", LayerString[layer], is_ogg? "Ogg FLAC":"FLAC");
195 printf("allocating encoder instance... ");
196 encoder = new_by_layer(layer);
198 printf("FAILED, new returned NULL\n");
203 printf("testing is_valid()... ");
204 if(!encoder->is_valid()) {
205 printf("FAILED, returned false\n");
211 printf("testing set_ogg_serial_number()... ");
212 if(!encoder->set_ogg_serial_number(file_utils__ogg_serial_number))
213 return die_s_("returned false", encoder);
217 printf("testing set_verify()... ");
218 if(!encoder->set_verify(true))
219 return die_s_("returned false", encoder);
222 printf("testing set_streamable_subset()... ");
223 if(!encoder->set_streamable_subset(true))
224 return die_s_("returned false", encoder);
227 printf("testing set_channels()... ");
228 if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
229 return die_s_("returned false", encoder);
232 printf("testing set_bits_per_sample()... ");
233 if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample))
234 return die_s_("returned false", encoder);
237 printf("testing set_sample_rate()... ");
238 if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate))
239 return die_s_("returned false", encoder);
242 printf("testing set_compression_level()... ");
243 if(!encoder->set_compression_level((unsigned)(-1)))
244 return die_s_("returned false", encoder);
247 printf("testing set_blocksize()... ");
248 if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
249 return die_s_("returned false", encoder);
252 printf("testing set_do_mid_side_stereo()... ");
253 if(!encoder->set_do_mid_side_stereo(false))
254 return die_s_("returned false", encoder);
257 printf("testing set_loose_mid_side_stereo()... ");
258 if(!encoder->set_loose_mid_side_stereo(false))
259 return die_s_("returned false", encoder);
262 printf("testing set_max_lpc_order()... ");
263 if(!encoder->set_max_lpc_order(0))
264 return die_s_("returned false", encoder);
267 printf("testing set_qlp_coeff_precision()... ");
268 if(!encoder->set_qlp_coeff_precision(0))
269 return die_s_("returned false", encoder);
272 printf("testing set_do_qlp_coeff_prec_search()... ");
273 if(!encoder->set_do_qlp_coeff_prec_search(false))
274 return die_s_("returned false", encoder);
277 printf("testing set_do_escape_coding()... ");
278 if(!encoder->set_do_escape_coding(false))
279 return die_s_("returned false", encoder);
282 printf("testing set_do_exhaustive_model_search()... ");
283 if(!encoder->set_do_exhaustive_model_search(false))
284 return die_s_("returned false", encoder);
287 printf("testing set_min_residual_partition_order()... ");
288 if(!encoder->set_min_residual_partition_order(0))
289 return die_s_("returned false", encoder);
292 printf("testing set_max_residual_partition_order()... ");
293 if(!encoder->set_max_residual_partition_order(0))
294 return die_s_("returned false", encoder);
297 printf("testing set_rice_parameter_search_dist()... ");
298 if(!encoder->set_rice_parameter_search_dist(0))
299 return die_s_("returned false", encoder);
302 printf("testing set_total_samples_estimate()... ");
303 if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples))
304 return die_s_("returned false", encoder);
307 printf("testing set_metadata()... ");
308 if(!encoder->set_metadata(metadata_sequence_, num_metadata_))
309 return die_s_("returned false", encoder);
312 if(layer < LAYER_FILENAME) {
313 printf("opening file for FLAC output... ");
314 file = ::flac_fopen(flacfilename(is_ogg), "w+b");
316 printf("ERROR (%s)\n", strerror(errno));
320 if(layer < LAYER_FILE)
321 dynamic_cast<StreamEncoder*>(encoder)->file_ = file;
326 case LAYER_SEEKABLE_STREAM:
327 printf("testing init%s()... ", is_ogg? "_ogg":"");
328 init_status = is_ogg? encoder->init_ogg() : encoder->init();
331 printf("testing init%s()... ", is_ogg? "_ogg":"");
332 init_status = is_ogg?
333 dynamic_cast<FLAC::Encoder::File*>(encoder)->init_ogg(file) :
334 dynamic_cast<FLAC::Encoder::File*>(encoder)->init(file);
337 printf("testing init%s()... ", is_ogg? "_ogg":"");
338 init_status = is_ogg?
339 dynamic_cast<FLAC::Encoder::File*>(encoder)->init_ogg(flacfilename(is_ogg)) :
340 dynamic_cast<FLAC::Encoder::File*>(encoder)->init(flacfilename(is_ogg));
343 die_("internal error 001");
346 if(init_status != ::FLAC__STREAM_ENCODER_INIT_STATUS_OK)
347 return die_s_(0, encoder);
350 printf("testing get_state()... ");
351 FLAC::Encoder::Stream::State state = encoder->get_state();
352 printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamEncoderState)state), state.as_cstring());
354 printf("testing get_verify_decoder_state()... ");
355 FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
356 printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
359 FLAC__uint64 absolute_sample;
360 unsigned frame_number;
363 FLAC__int32 expected;
366 printf("testing get_verify_decoder_error_stats()... ");
367 encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got);
371 printf("testing get_verify()... ");
372 if(encoder->get_verify() != true) {
373 printf("FAILED, expected true, got false\n");
378 printf("testing get_streamable_subset()... ");
379 if(encoder->get_streamable_subset() != true) {
380 printf("FAILED, expected true, got false\n");
385 printf("testing get_do_mid_side_stereo()... ");
386 if(encoder->get_do_mid_side_stereo() != false) {
387 printf("FAILED, expected false, got true\n");
392 printf("testing get_loose_mid_side_stereo()... ");
393 if(encoder->get_loose_mid_side_stereo() != false) {
394 printf("FAILED, expected false, got true\n");
399 printf("testing get_channels()... ");
400 if(encoder->get_channels() != streaminfo_.data.stream_info.channels) {
401 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, encoder->get_channels());
406 printf("testing get_bits_per_sample()... ");
407 if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) {
408 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample());
413 printf("testing get_sample_rate()... ");
414 if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) {
415 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate());
420 printf("testing get_blocksize()... ");
421 if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) {
422 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize());
427 printf("testing get_max_lpc_order()... ");
428 if(encoder->get_max_lpc_order() != 0) {
429 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_lpc_order());
434 printf("testing get_qlp_coeff_precision()... ");
435 (void)encoder->get_qlp_coeff_precision();
436 /* we asked the encoder to auto select this so we accept anything */
439 printf("testing get_do_qlp_coeff_prec_search()... ");
440 if(encoder->get_do_qlp_coeff_prec_search() != false) {
441 printf("FAILED, expected false, got true\n");
446 printf("testing get_do_escape_coding()... ");
447 if(encoder->get_do_escape_coding() != false) {
448 printf("FAILED, expected false, got true\n");
453 printf("testing get_do_exhaustive_model_search()... ");
454 if(encoder->get_do_exhaustive_model_search() != false) {
455 printf("FAILED, expected false, got true\n");
460 printf("testing get_min_residual_partition_order()... ");
461 if(encoder->get_min_residual_partition_order() != 0) {
462 printf("FAILED, expected %u, got %u\n", 0, encoder->get_min_residual_partition_order());
467 printf("testing get_max_residual_partition_order()... ");
468 if(encoder->get_max_residual_partition_order() != 0) {
469 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_residual_partition_order());
474 printf("testing get_rice_parameter_search_dist()... ");
475 if(encoder->get_rice_parameter_search_dist() != 0) {
476 printf("FAILED, expected %u, got %u\n", 0, encoder->get_rice_parameter_search_dist());
481 printf("testing get_total_samples_estimate()... ");
482 if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
483 printf("FAILED, expected %" PRIu64 ", got %" PRIu64 "\n", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
488 /* init the dummy sample buffer */
489 for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
492 printf("testing process()... ");
493 if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32)))
494 return die_s_("returned false", encoder);
497 printf("testing process_interleaved()... ");
498 if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32)))
499 return die_s_("returned false", encoder);
502 printf("testing finish()... ");
503 if(!encoder->finish()) {
504 state = encoder->get_state();
505 printf("FAILED, returned false, state = %u (%s)\n", (unsigned)((::FLAC__StreamEncoderState)state), state.as_cstring());
510 if(layer < LAYER_FILE)
511 ::fclose(dynamic_cast<StreamEncoder*>(encoder)->file_);
513 printf("freeing encoder instance... ");
517 printf("\nPASSED!\n");
524 FLAC__bool is_ogg = false;
527 init_metadata_blocks_();
529 if(!test_stream_encoder(LAYER_STREAM, is_ogg))
532 if(!test_stream_encoder(LAYER_SEEKABLE_STREAM, is_ogg))
535 if(!test_stream_encoder(LAYER_FILE, is_ogg))
538 if(!test_stream_encoder(LAYER_FILENAME, is_ogg))
541 (void) grabbag__file_remove_file(flacfilename(is_ogg));
543 free_metadata_blocks_();
545 if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg)