1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2002,2003,2004,2005,2006 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.
28 #include "FLAC/assert.h"
29 #include "FLAC/stream_encoder.h"
30 #include "share/grabbag.h"
31 #include "test_libs_common/file_utils_flac.h"
32 #include "test_libs_common/metadata_utils.h"
35 LAYER_STREAM = 0, /* FLAC__stream_encoder_init_[ogg_]stream() without seeking */
36 LAYER_SEEKABLE_STREAM, /* FLAC__stream_encoder_init_[ogg_]stream() with seeking */
37 LAYER_FILE, /* FLAC__stream_encoder_init_[ogg_]FILE() */
38 LAYER_FILENAME /* FLAC__stream_encoder_init_[ogg_]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(FLAC__bool is_ogg)
54 return is_ogg? "metadata.ogg" : "metadata.flac";
57 static FLAC__bool die_(const char *msg)
59 printf("ERROR: %s\n", msg);
63 static FLAC__bool die_s_(const char *msg, const FLAC__StreamEncoder *encoder)
65 FLAC__StreamEncoderState state = FLAC__stream_encoder_get_state(encoder);
68 printf("FAILED, %s", msg);
72 printf(", state = %u (%s)\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
73 if(state == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
74 FLAC__StreamDecoderState dstate = FLAC__stream_encoder_get_verify_decoder_state(encoder);
75 printf(" verify decoder state = %u (%s)\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
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 static FLAC__StreamEncoderReadStatus stream_encoder_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
93 (void)encoder, (void)buffer, (void)bytes, (void)client_data;
94 memset(buffer, 0, *bytes); /* init buffer to avoid valgrind errors */
95 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
98 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)
100 (void)encoder, (void)buffer, (void)bytes, (void)samples, (void)current_frame, (void)client_data;
101 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
104 static FLAC__StreamEncoderSeekStatus stream_encoder_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
106 (void)encoder, (void)absolute_byte_offset, (void)client_data;
107 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
110 static FLAC__StreamEncoderTellStatus stream_encoder_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
112 (void)encoder, (void)client_data;
113 *absolute_byte_offset = 0;
114 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
117 static void stream_encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
119 (void)encoder, (void)metadata, (void)client_data;
122 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)
124 (void)encoder, (void)bytes_written, (void)samples_written, (void)frames_written, (void)total_frames_estimate, (void)client_data;
127 static FLAC__bool test_stream_encoder(Layer layer, FLAC__bool is_ogg)
129 FLAC__StreamEncoder *encoder;
130 FLAC__StreamEncoderInitStatus init_status;
131 FLAC__StreamEncoderState state;
132 FLAC__StreamDecoderState dstate;
134 FLAC__int32 samples[1024];
135 FLAC__int32 *samples_array[1];
138 samples_array[0] = samples;
140 printf("\n+++ libFLAC unit test: FLAC__StreamEncoder (layer: %s, format: %s)\n\n", LayerString[layer], is_ogg? "Ogg FLAC":"FLAC");
142 printf("testing FLAC__stream_encoder_new()... ");
143 encoder = FLAC__stream_encoder_new();
145 printf("FAILED, returned NULL\n");
151 printf("testing FLAC__stream_encoder_set_ogg_serial_number()... ");
152 if(!FLAC__stream_encoder_set_ogg_serial_number(encoder, file_utils__ogg_serial_number))
153 return die_s_("returned false", encoder);
157 printf("testing FLAC__stream_encoder_set_verify()... ");
158 if(!FLAC__stream_encoder_set_verify(encoder, true))
159 return die_s_("returned false", encoder);
162 printf("testing FLAC__stream_encoder_set_streamable_subset()... ");
163 if(!FLAC__stream_encoder_set_streamable_subset(encoder, true))
164 return die_s_("returned false", encoder);
167 printf("testing FLAC__stream_encoder_set_do_mid_side_stereo()... ");
168 if(!FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false))
169 return die_s_("returned false", encoder);
172 printf("testing FLAC__stream_encoder_set_loose_mid_side_stereo()... ");
173 if(!FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false))
174 return die_s_("returned false", encoder);
177 printf("testing FLAC__stream_encoder_set_channels()... ");
178 if(!FLAC__stream_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
179 return die_s_("returned false", encoder);
182 printf("testing FLAC__stream_encoder_set_bits_per_sample()... ");
183 if(!FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample))
184 return die_s_("returned false", encoder);
187 printf("testing FLAC__stream_encoder_set_sample_rate()... ");
188 if(!FLAC__stream_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
189 return die_s_("returned false", encoder);
192 printf("testing FLAC__stream_encoder_set_blocksize()... ");
193 if(!FLAC__stream_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
194 return die_s_("returned false", encoder);
197 printf("testing FLAC__stream_encoder_set_max_lpc_order()... ");
198 if(!FLAC__stream_encoder_set_max_lpc_order(encoder, 0))
199 return die_s_("returned false", encoder);
202 printf("testing FLAC__stream_encoder_set_qlp_coeff_precision()... ");
203 if(!FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0))
204 return die_s_("returned false", encoder);
207 printf("testing FLAC__stream_encoder_set_do_qlp_coeff_prec_search()... ");
208 if(!FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false))
209 return die_s_("returned false", encoder);
212 printf("testing FLAC__stream_encoder_set_do_escape_coding()... ");
213 if(!FLAC__stream_encoder_set_do_escape_coding(encoder, false))
214 return die_s_("returned false", encoder);
217 printf("testing FLAC__stream_encoder_set_do_exhaustive_model_search()... ");
218 if(!FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false))
219 return die_s_("returned false", encoder);
222 printf("testing FLAC__stream_encoder_set_min_residual_partition_order()... ");
223 if(!FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0))
224 return die_s_("returned false", encoder);
227 printf("testing FLAC__stream_encoder_set_max_residual_partition_order()... ");
228 if(!FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0))
229 return die_s_("returned false", encoder);
232 printf("testing FLAC__stream_encoder_set_rice_parameter_search_dist()... ");
233 if(!FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0))
234 return die_s_("returned false", encoder);
237 printf("testing FLAC__stream_encoder_set_total_samples_estimate()... ");
238 if(!FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
239 return die_s_("returned false", encoder);
242 printf("testing FLAC__stream_encoder_set_metadata()... ");
243 if(!FLAC__stream_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
244 return die_s_("returned false", encoder);
249 printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":"");
250 init_status = is_ogg?
251 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=*/0) :
252 FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, stream_encoder_metadata_callback_, /*client_data=*/0);
254 case LAYER_SEEKABLE_STREAM:
255 printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":"");
256 init_status = is_ogg?
257 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=*/0) :
258 FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, stream_encoder_seek_callback_, stream_encoder_tell_callback_, /*metadata_callback=*/0, /*client_data=*/0);
261 printf("opening file for FLAC output... ");
262 file = fopen(flacfilename(is_ogg), "w+b");
264 printf("ERROR (%s)\n", strerror(errno));
269 printf("testing FLAC__stream_encoder_init_%sFILE()... ", is_ogg? "ogg_":"");
270 init_status = is_ogg?
271 FLAC__stream_encoder_init_ogg_FILE(encoder, file, stream_encoder_progress_callback_, /*client_data=*/0) :
272 FLAC__stream_encoder_init_FILE(encoder, file, stream_encoder_progress_callback_, /*client_data=*/0);
275 printf("testing FLAC__stream_encoder_init_%sfile()... ", is_ogg? "ogg_":"");
276 init_status = is_ogg?
277 FLAC__stream_encoder_init_ogg_file(encoder, flacfilename(is_ogg), stream_encoder_progress_callback_, /*client_data=*/0) :
278 FLAC__stream_encoder_init_file(encoder, flacfilename(is_ogg), stream_encoder_progress_callback_, /*client_data=*/0);
281 die_("internal error 001");
284 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
285 return die_s_(0, encoder);
288 printf("testing FLAC__stream_encoder_get_state()... ");
289 state = FLAC__stream_encoder_get_state(encoder);
290 printf("returned state = %u (%s)... OK\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
292 printf("testing FLAC__stream_encoder_get_verify_decoder_state()... ");
293 dstate = FLAC__stream_encoder_get_verify_decoder_state(encoder);
294 printf("returned state = %u (%s)... OK\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
297 FLAC__uint64 absolute_sample;
298 unsigned frame_number;
301 FLAC__int32 expected;
304 printf("testing FLAC__stream_encoder_get_verify_decoder_error_stats()... ");
305 FLAC__stream_encoder_get_verify_decoder_error_stats(encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
309 printf("testing FLAC__stream_encoder_get_verify()... ");
310 if(FLAC__stream_encoder_get_verify(encoder) != true) {
311 printf("FAILED, expected true, got false\n");
316 printf("testing FLAC__stream_encoder_get_streamable_subset()... ");
317 if(FLAC__stream_encoder_get_streamable_subset(encoder) != true) {
318 printf("FAILED, expected true, got false\n");
323 printf("testing FLAC__stream_encoder_get_do_mid_side_stereo()... ");
324 if(FLAC__stream_encoder_get_do_mid_side_stereo(encoder) != false) {
325 printf("FAILED, expected false, got true\n");
330 printf("testing FLAC__stream_encoder_get_loose_mid_side_stereo()... ");
331 if(FLAC__stream_encoder_get_loose_mid_side_stereo(encoder) != false) {
332 printf("FAILED, expected false, got true\n");
337 printf("testing FLAC__stream_encoder_get_channels()... ");
338 if(FLAC__stream_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
339 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, FLAC__stream_encoder_get_channels(encoder));
344 printf("testing FLAC__stream_encoder_get_bits_per_sample()... ");
345 if(FLAC__stream_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
346 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, FLAC__stream_encoder_get_bits_per_sample(encoder));
351 printf("testing FLAC__stream_encoder_get_sample_rate()... ");
352 if(FLAC__stream_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
353 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, FLAC__stream_encoder_get_sample_rate(encoder));
358 printf("testing FLAC__stream_encoder_get_blocksize()... ");
359 if(FLAC__stream_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
360 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, FLAC__stream_encoder_get_blocksize(encoder));
365 printf("testing FLAC__stream_encoder_get_max_lpc_order()... ");
366 if(FLAC__stream_encoder_get_max_lpc_order(encoder) != 0) {
367 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_lpc_order(encoder));
372 printf("testing FLAC__stream_encoder_get_qlp_coeff_precision()... ");
373 (void)FLAC__stream_encoder_get_qlp_coeff_precision(encoder);
374 /* we asked the encoder to auto select this so we accept anything */
377 printf("testing FLAC__stream_encoder_get_do_qlp_coeff_prec_search()... ");
378 if(FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
379 printf("FAILED, expected false, got true\n");
384 printf("testing FLAC__stream_encoder_get_do_escape_coding()... ");
385 if(FLAC__stream_encoder_get_do_escape_coding(encoder) != false) {
386 printf("FAILED, expected false, got true\n");
391 printf("testing FLAC__stream_encoder_get_do_exhaustive_model_search()... ");
392 if(FLAC__stream_encoder_get_do_exhaustive_model_search(encoder) != false) {
393 printf("FAILED, expected false, got true\n");
398 printf("testing FLAC__stream_encoder_get_min_residual_partition_order()... ");
399 if(FLAC__stream_encoder_get_min_residual_partition_order(encoder) != 0) {
400 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_min_residual_partition_order(encoder));
405 printf("testing FLAC__stream_encoder_get_max_residual_partition_order()... ");
406 if(FLAC__stream_encoder_get_max_residual_partition_order(encoder) != 0) {
407 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_residual_partition_order(encoder));
412 printf("testing FLAC__stream_encoder_get_rice_parameter_search_dist()... ");
413 if(FLAC__stream_encoder_get_rice_parameter_search_dist(encoder) != 0) {
414 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_rice_parameter_search_dist(encoder));
419 printf("testing FLAC__stream_encoder_get_total_samples_estimate()... ");
420 if(FLAC__stream_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) {
421 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, FLAC__stream_encoder_get_total_samples_estimate(encoder));
426 /* init the dummy sample buffer */
427 for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
430 printf("testing FLAC__stream_encoder_process()... ");
431 if(!FLAC__stream_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
432 return die_s_("returned false", encoder);
435 printf("testing FLAC__stream_encoder_process_interleaved()... ");
436 if(!FLAC__stream_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
437 return die_s_("returned false", encoder);
440 printf("testing FLAC__stream_encoder_finish()... ");
441 FLAC__stream_encoder_finish(encoder);
444 printf("testing FLAC__stream_encoder_delete()... ");
445 FLAC__stream_encoder_delete(encoder);
448 printf("\nPASSED!\n");
453 FLAC__bool test_encoders()
455 FLAC__bool is_ogg = false;
458 init_metadata_blocks_();
460 if(!test_stream_encoder(LAYER_STREAM, is_ogg))
463 if(!test_stream_encoder(LAYER_SEEKABLE_STREAM, is_ogg))
466 if(!test_stream_encoder(LAYER_FILE, is_ogg))
469 if(!test_stream_encoder(LAYER_FILENAME, is_ogg))
472 (void) grabbag__file_remove_file(flacfilename(is_ogg));
474 free_metadata_blocks_();
476 if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg)