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/stream_encoder.h"
27 static FLAC__StreamMetaData streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_;
28 static FLAC__StreamMetaData *metadata_sequence_[] = { &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_ };
29 static const unsigned num_metadata_ = 5;
30 static const char *flacfilename_ = "metadata.flac";
32 static FLAC__bool die_s_(const char *msg, const FLAC__StreamEncoder *encoder)
34 FLAC__StreamEncoderState state = FLAC__stream_encoder_get_state(encoder);
37 printf("FAILED, %s, state = %u (%s)\n", msg, (unsigned)state, FLAC__StreamEncoderStateString[state]);
39 printf("FAILED, state = %u (%s)\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
44 static void *malloc_or_die_(size_t size)
46 void *x = malloc(size);
48 fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (unsigned)size);
54 static void init_metadata_blocks_()
57 most of the actual numbers and data in the blocks don't matter,
58 we just want to make sure the encoder encodes them correctly
60 remember, the metadata interface gets tested after the encoders,
61 so we do all the metadata manipulation here without it.
64 /* min/max_framesize and md5sum don't get written at first, so we have to leave them 0 */
65 streaminfo_.is_last = false;
66 streaminfo_.type = FLAC__METADATA_TYPE_STREAMINFO;
67 streaminfo_.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
68 streaminfo_.data.stream_info.min_blocksize = 576;
69 streaminfo_.data.stream_info.max_blocksize = 576;
70 streaminfo_.data.stream_info.min_framesize = 0;
71 streaminfo_.data.stream_info.max_framesize = 0;
72 streaminfo_.data.stream_info.sample_rate = 44100;
73 streaminfo_.data.stream_info.channels = 1;
74 streaminfo_.data.stream_info.bits_per_sample = 8;
75 streaminfo_.data.stream_info.total_samples = 0;
76 memset(streaminfo_.data.stream_info.md5sum, 0, 16);
78 padding_.is_last = false;
79 padding_.type = FLAC__METADATA_TYPE_PADDING;
80 padding_.length = 1234;
82 seektable_.is_last = false;
83 seektable_.type = FLAC__METADATA_TYPE_SEEKTABLE;
84 seektable_.data.seek_table.num_points = 2;
85 seektable_.length = seektable_.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
86 seektable_.data.seek_table.points = malloc_or_die_(seektable_.data.seek_table.num_points * sizeof(FLAC__StreamMetaData_SeekPoint));
87 seektable_.data.seek_table.points[0].sample_number = 0;
88 seektable_.data.seek_table.points[0].stream_offset = 0;
89 seektable_.data.seek_table.points[0].frame_samples = streaminfo_.data.stream_info.min_blocksize;
90 seektable_.data.seek_table.points[1].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
91 seektable_.data.seek_table.points[1].stream_offset = 1000;
92 seektable_.data.seek_table.points[1].frame_samples = streaminfo_.data.stream_info.min_blocksize;
94 application1_.is_last = false;
95 application1_.type = FLAC__METADATA_TYPE_APPLICATION;
96 application1_.length = 8;
97 memcpy(application1_.data.application.id, "\xfe\xdc\xba\x98", 4);
98 application1_.data.application.data = malloc_or_die_(4);
99 memcpy(application1_.data.application.data, "\xf0\xe1\xd2\xc3", 4);
101 application2_.is_last = false;
102 application2_.type = FLAC__METADATA_TYPE_APPLICATION;
103 application2_.length = 4;
104 memcpy(application2_.data.application.id, "\x76\x54\x32\x10", 4);
105 application2_.data.application.data = 0;
107 vorbiscomment_.is_last = true;
108 vorbiscomment_.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
109 vorbiscomment_.length = (4 + 8) + 4 + (4 + 5) + (4 + 0);
110 vorbiscomment_.data.vorbis_comment.vendor_string.length = 8;
111 vorbiscomment_.data.vorbis_comment.vendor_string.entry = malloc_or_die_(8);
112 memcpy(vorbiscomment_.data.vorbis_comment.vendor_string.entry, "flac 1.x", 8);
113 vorbiscomment_.data.vorbis_comment.num_comments = 2;
114 vorbiscomment_.data.vorbis_comment.comments = malloc_or_die_(vorbiscomment_.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetaData_VorbisComment_Entry));
115 vorbiscomment_.data.vorbis_comment.comments[0].length = 5;
116 vorbiscomment_.data.vorbis_comment.comments[0].entry = malloc_or_die_(5);
117 memcpy(vorbiscomment_.data.vorbis_comment.comments[0].entry, "ab=cd", 5);
118 vorbiscomment_.data.vorbis_comment.comments[1].length = 0;
119 vorbiscomment_.data.vorbis_comment.comments[1].entry = 0;
122 static void free_metadata_blocks_()
124 free(seektable_.data.seek_table.points);
125 free(application1_.data.application.data);
126 free(vorbiscomment_.data.vorbis_comment.vendor_string.entry);
127 free(vorbiscomment_.data.vorbis_comment.comments[0].entry);
128 free(vorbiscomment_.data.vorbis_comment.comments);
131 static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
133 (void)encoder, (void)buffer, (void)bytes, (void)samples, (void)current_frame, (void)client_data;
134 return FLAC__STREAM_ENCODER_WRITE_OK;
137 static void encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data)
139 (void)encoder, (void)metadata, (void)client_data;
142 static FLAC__bool test_stream_encoder()
144 FLAC__StreamEncoder *encoder;
145 FLAC__StreamEncoderState state;
146 FLAC__int32 samples[1024];
147 FLAC__int32 *samples_array[1] = { samples };
150 printf("\n+++ unit test: FLAC__StreamEncoder\n\n");
152 printf("testing FLAC__stream_encoder_new()... ");
153 encoder = FLAC__stream_encoder_new();
155 printf("FAILED, returned NULL\n");
160 printf("testing FLAC__stream_encoder_set_streamable_subset()... ");
161 if(!FLAC__stream_encoder_set_streamable_subset(encoder, true))
162 return die_s_("returned false", encoder);
165 printf("testing FLAC__stream_encoder_set_do_mid_side_stereo()... ");
166 if(!FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false))
167 return die_s_("returned false", encoder);
170 printf("testing FLAC__stream_encoder_set_loose_mid_side_stereo()... ");
171 if(!FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false))
172 return die_s_("returned false", encoder);
175 printf("testing FLAC__stream_encoder_set_channels()... ");
176 if(!FLAC__stream_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
177 return die_s_("returned false", encoder);
180 printf("testing FLAC__stream_encoder_set_bits_per_sample()... ");
181 if(!FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample))
182 return die_s_("returned false", encoder);
185 printf("testing FLAC__stream_encoder_set_sample_rate()... ");
186 if(!FLAC__stream_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
187 return die_s_("returned false", encoder);
190 printf("testing FLAC__stream_encoder_set_blocksize()... ");
191 if(!FLAC__stream_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
192 return die_s_("returned false", encoder);
195 printf("testing FLAC__stream_encoder_set_max_lpc_order()... ");
196 if(!FLAC__stream_encoder_set_max_lpc_order(encoder, 0))
197 return die_s_("returned false", encoder);
200 printf("testing FLAC__stream_encoder_set_qlp_coeff_precision()... ");
201 if(!FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0))
202 return die_s_("returned false", encoder);
205 printf("testing FLAC__stream_encoder_set_do_qlp_coeff_prec_search()... ");
206 if(!FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false))
207 return die_s_("returned false", encoder);
210 printf("testing FLAC__stream_encoder_set_do_escape_coding()... ");
211 if(!FLAC__stream_encoder_set_do_escape_coding(encoder, false))
212 return die_s_("returned false", encoder);
215 printf("testing FLAC__stream_encoder_set_do_exhaustive_model_search()... ");
216 if(!FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false))
217 return die_s_("returned false", encoder);
220 printf("testing FLAC__stream_encoder_set_min_residual_partition_order()... ");
221 if(!FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0))
222 return die_s_("returned false", encoder);
225 printf("testing FLAC__stream_encoder_set_max_residual_partition_order()... ");
226 if(!FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0))
227 return die_s_("returned false", encoder);
230 printf("testing FLAC__stream_encoder_set_rice_parameter_search_dist()... ");
231 if(!FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0))
232 return die_s_("returned false", encoder);
235 printf("testing FLAC__stream_encoder_set_total_samples_estimate()... ");
236 if(!FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
237 return die_s_("returned false", encoder);
240 printf("testing FLAC__stream_encoder_set_metadata()... ");
241 if(!FLAC__stream_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
242 return die_s_("returned false", encoder);
245 printf("testing FLAC__stream_encoder_set_write_callback()... ");
246 if(!FLAC__stream_encoder_set_write_callback(encoder, encoder_write_callback_))
247 return die_s_("returned false", encoder);
250 printf("testing FLAC__stream_encoder_set_metadata_callback()... ");
251 if(!FLAC__stream_encoder_set_metadata_callback(encoder, encoder_metadata_callback_))
252 return die_s_("returned false", encoder);
255 printf("testing FLAC__stream_encoder_set_client_data()... ");
256 if(!FLAC__stream_encoder_set_client_data(encoder, 0))
257 return die_s_("returned false", encoder);
260 printf("testing FLAC__stream_encoder_init()... ");
261 if(FLAC__stream_encoder_init(encoder) != FLAC__STREAM_ENCODER_OK)
262 return die_s_(0, encoder);
265 printf("testing FLAC__stream_encoder_get_state()... ");
266 state = FLAC__stream_encoder_get_state(encoder);
267 printf("returned state = %u (%s)... OK\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
269 printf("testing FLAC__stream_encoder_get_streamable_subset()... ");
270 if(FLAC__stream_encoder_get_streamable_subset(encoder) != true) {
271 printf("FAILED, expected true, got false\n");
276 printf("testing FLAC__stream_encoder_get_do_mid_side_stereo()... ");
277 if(FLAC__stream_encoder_get_do_mid_side_stereo(encoder) != false) {
278 printf("FAILED, expected false, got true\n");
283 printf("testing FLAC__stream_encoder_get_loose_mid_side_stereo()... ");
284 if(FLAC__stream_encoder_get_loose_mid_side_stereo(encoder) != false) {
285 printf("FAILED, expected false, got true\n");
290 printf("testing FLAC__stream_encoder_get_channels()... ");
291 if(FLAC__stream_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
292 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, FLAC__stream_encoder_get_channels(encoder));
297 printf("testing FLAC__stream_encoder_get_bits_per_sample()... ");
298 if(FLAC__stream_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
299 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, FLAC__stream_encoder_get_bits_per_sample(encoder));
304 printf("testing FLAC__stream_encoder_get_sample_rate()... ");
305 if(FLAC__stream_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
306 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, FLAC__stream_encoder_get_sample_rate(encoder));
311 printf("testing FLAC__stream_encoder_get_blocksize()... ");
312 if(FLAC__stream_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
313 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, FLAC__stream_encoder_get_blocksize(encoder));
318 printf("testing FLAC__stream_encoder_get_max_lpc_order()... ");
319 if(FLAC__stream_encoder_get_max_lpc_order(encoder) != 0) {
320 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_lpc_order(encoder));
325 printf("testing FLAC__stream_encoder_get_qlp_coeff_precision()... ");
326 (void)FLAC__stream_encoder_get_qlp_coeff_precision(encoder);
327 /* we asked the encoder to auto select this so we accept anything */
330 printf("testing FLAC__stream_encoder_get_do_qlp_coeff_prec_search()... ");
331 if(FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
332 printf("FAILED, expected false, got true\n");
337 printf("testing FLAC__stream_encoder_get_do_escape_coding()... ");
338 if(FLAC__stream_encoder_get_do_escape_coding(encoder) != false) {
339 printf("FAILED, expected false, got true\n");
344 printf("testing FLAC__stream_encoder_get_do_exhaustive_model_search()... ");
345 if(FLAC__stream_encoder_get_do_exhaustive_model_search(encoder) != false) {
346 printf("FAILED, expected false, got true\n");
351 printf("testing FLAC__stream_encoder_get_min_residual_partition_order()... ");
352 if(FLAC__stream_encoder_get_min_residual_partition_order(encoder) != 0) {
353 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_min_residual_partition_order(encoder));
358 printf("testing FLAC__stream_encoder_get_max_residual_partition_order()... ");
359 if(FLAC__stream_encoder_get_max_residual_partition_order(encoder) != 0) {
360 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_residual_partition_order(encoder));
365 printf("testing FLAC__stream_encoder_get_rice_parameter_search_dist()... ");
366 if(FLAC__stream_encoder_get_rice_parameter_search_dist(encoder) != 0) {
367 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_rice_parameter_search_dist(encoder));
372 /* init the dummy sample buffer */
373 for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
376 printf("testing FLAC__stream_encoder_process()... ");
377 if(!FLAC__stream_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
378 return die_s_("returned false", encoder);
381 printf("testing FLAC__stream_encoder_process_interleaved()... ");
382 if(!FLAC__stream_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
383 return die_s_("returned false", encoder);
386 printf("testing FLAC__stream_encoder_finish()... ");
387 FLAC__stream_encoder_finish(encoder);
390 printf("testing FLAC__stream_encoder_delete()... ");
391 FLAC__stream_encoder_delete(encoder);
394 printf("\nPASSED!\n");
399 FLAC__bool test_encoders()
401 init_metadata_blocks_();
403 if(!test_stream_encoder())
406 (void) file_utils__remove_file(flacfilename_);
407 free_metadata_blocks_();