1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2016 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.
29 #include "FLAC/assert.h"
30 #include "FLAC/stream_encoder.h"
31 #include "share/grabbag.h"
32 #include "share/compat.h"
33 #include "test_libs_common/file_utils_flac.h"
34 #include "test_libs_common/metadata_utils.h"
37 LAYER_STREAM
= 0, /* FLAC__stream_encoder_init_[ogg_]stream() without seeking */
38 LAYER_SEEKABLE_STREAM
, /* FLAC__stream_encoder_init_[ogg_]stream() with seeking */
39 LAYER_FILE
, /* FLAC__stream_encoder_init_[ogg_]FILE() */
40 LAYER_FILENAME
/* FLAC__stream_encoder_init_[ogg_]file() */
43 static const char * const LayerString
[] = {
50 static FLAC__StreamMetadata streaminfo_
, padding_
, seektable_
, application1_
, application2_
, vorbiscomment_
, cuesheet_
, picture_
, unknown_
;
51 static FLAC__StreamMetadata
*metadata_sequence_
[] = { &vorbiscomment_
, &padding_
, &seektable_
, &application1_
, &application2_
, &cuesheet_
, &picture_
, &unknown_
};
52 static const uint32_t num_metadata_
= sizeof(metadata_sequence_
) / sizeof(metadata_sequence_
[0]);
54 static const char *flacfilename(FLAC__bool is_ogg
)
56 return is_ogg
? "metadata.oga" : "metadata.flac";
59 static FLAC__bool
die_(const char *msg
)
61 printf("ERROR: %s\n", msg
);
65 static FLAC__bool
die_s_(const char *msg
, const FLAC__StreamEncoder
*encoder
)
67 FLAC__StreamEncoderState state
= FLAC__stream_encoder_get_state(encoder
);
70 printf("FAILED, %s", msg
);
74 printf(", state = %u (%s)\n", (uint32_t)state
, FLAC__StreamEncoderStateString
[state
]);
75 if(state
== FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR
) {
76 FLAC__StreamDecoderState dstate
= FLAC__stream_encoder_get_verify_decoder_state(encoder
);
77 printf(" verify decoder state = %u (%s)\n", (uint32_t)dstate
, FLAC__StreamDecoderStateString
[dstate
]);
83 static void init_metadata_blocks_(void)
85 mutils__init_metadata_blocks(&streaminfo_
, &padding_
, &seektable_
, &application1_
, &application2_
, &vorbiscomment_
, &cuesheet_
, &picture_
, &unknown_
);
88 static void free_metadata_blocks_(void)
90 mutils__free_metadata_blocks(&streaminfo_
, &padding_
, &seektable_
, &application1_
, &application2_
, &vorbiscomment_
, &cuesheet_
, &picture_
, &unknown_
);
93 static FLAC__StreamEncoderReadStatus
stream_encoder_read_callback_(const FLAC__StreamEncoder
*encoder
, FLAC__byte buffer
[], size_t *bytes
, void *client_data
)
95 FILE *f
= (FILE*)client_data
;
98 *bytes
= fread(buffer
, sizeof(FLAC__byte
), *bytes
, f
);
100 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT
;
102 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM
;
104 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE
;
107 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT
;
110 static FLAC__StreamEncoderWriteStatus
stream_encoder_write_callback_(const FLAC__StreamEncoder
*encoder
, const FLAC__byte buffer
[], size_t bytes
, uint32_t samples
, uint32_t current_frame
, void *client_data
)
112 FILE *f
= (FILE*)client_data
;
113 (void)encoder
, (void)samples
, (void)current_frame
;
114 if(fwrite(buffer
, 1, bytes
, f
) != bytes
)
115 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR
;
117 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK
;
120 static FLAC__StreamEncoderSeekStatus
stream_encoder_seek_callback_(const FLAC__StreamEncoder
*encoder
, FLAC__uint64 absolute_byte_offset
, void *client_data
)
122 FILE *f
= (FILE*)client_data
;
124 if(fseeko(f
, (long)absolute_byte_offset
, SEEK_SET
) < 0)
125 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR
;
127 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK
;
130 static FLAC__StreamEncoderTellStatus
stream_encoder_tell_callback_(const FLAC__StreamEncoder
*encoder
, FLAC__uint64
*absolute_byte_offset
, void *client_data
)
132 FILE *f
= (FILE*)client_data
;
135 if((pos
= ftello(f
)) < 0)
136 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR
;
138 *absolute_byte_offset
= (FLAC__uint64
)pos
;
139 return FLAC__STREAM_ENCODER_TELL_STATUS_OK
;
143 static void stream_encoder_metadata_callback_(const FLAC__StreamEncoder
*encoder
, const FLAC__StreamMetadata
*metadata
, void *client_data
)
145 (void)encoder
, (void)metadata
, (void)client_data
;
148 static void stream_encoder_progress_callback_(const FLAC__StreamEncoder
*encoder
, FLAC__uint64 bytes_written
, FLAC__uint64 samples_written
, uint32_t frames_written
, uint32_t total_frames_estimate
, void *client_data
)
150 (void)encoder
, (void)bytes_written
, (void)samples_written
, (void)frames_written
, (void)total_frames_estimate
, (void)client_data
;
153 static FLAC__bool
test_stream_encoder(Layer layer
, FLAC__bool is_ogg
)
155 FLAC__StreamEncoder
*encoder
;
156 FLAC__StreamEncoderInitStatus init_status
;
157 FLAC__StreamEncoderState state
;
158 FLAC__StreamDecoderState dstate
;
160 FLAC__int32 samples
[1024];
161 FLAC__int32
*samples_array
[1];
164 samples_array
[0] = samples
;
166 printf("\n+++ libFLAC unit test: FLAC__StreamEncoder (layer: %s, format: %s)\n\n", LayerString
[layer
], is_ogg
? "Ogg FLAC":"FLAC");
168 printf("testing FLAC__stream_encoder_new()... ");
169 encoder
= FLAC__stream_encoder_new();
171 printf("FAILED, returned NULL\n");
177 printf("testing FLAC__stream_encoder_set_ogg_serial_number()... ");
178 if(!FLAC__stream_encoder_set_ogg_serial_number(encoder
, file_utils__ogg_serial_number
))
179 return die_s_("returned false", encoder
);
183 printf("testing FLAC__stream_encoder_set_verify()... ");
184 if(!FLAC__stream_encoder_set_verify(encoder
, true))
185 return die_s_("returned false", encoder
);
188 printf("testing FLAC__stream_encoder_set_streamable_subset()... ");
189 if(!FLAC__stream_encoder_set_streamable_subset(encoder
, true))
190 return die_s_("returned false", encoder
);
193 printf("testing FLAC__stream_encoder_set_channels()... ");
194 if(!FLAC__stream_encoder_set_channels(encoder
, streaminfo_
.data
.stream_info
.channels
))
195 return die_s_("returned false", encoder
);
198 printf("testing FLAC__stream_encoder_set_bits_per_sample()... ");
199 if(!FLAC__stream_encoder_set_bits_per_sample(encoder
, streaminfo_
.data
.stream_info
.bits_per_sample
))
200 return die_s_("returned false", encoder
);
203 printf("testing FLAC__stream_encoder_set_sample_rate()... ");
204 if(!FLAC__stream_encoder_set_sample_rate(encoder
, streaminfo_
.data
.stream_info
.sample_rate
))
205 return die_s_("returned false", encoder
);
208 printf("testing FLAC__stream_encoder_set_compression_level()... ");
209 if(!FLAC__stream_encoder_set_compression_level(encoder
, (uint32_t)(-1)))
210 return die_s_("returned false", encoder
);
213 printf("testing FLAC__stream_encoder_set_blocksize()... ");
214 if(!FLAC__stream_encoder_set_blocksize(encoder
, streaminfo_
.data
.stream_info
.min_blocksize
))
215 return die_s_("returned false", encoder
);
218 printf("testing FLAC__stream_encoder_set_do_mid_side_stereo()... ");
219 if(!FLAC__stream_encoder_set_do_mid_side_stereo(encoder
, false))
220 return die_s_("returned false", encoder
);
223 printf("testing FLAC__stream_encoder_set_loose_mid_side_stereo()... ");
224 if(!FLAC__stream_encoder_set_loose_mid_side_stereo(encoder
, false))
225 return die_s_("returned false", encoder
);
228 printf("testing FLAC__stream_encoder_set_max_lpc_order()... ");
229 if(!FLAC__stream_encoder_set_max_lpc_order(encoder
, 0))
230 return die_s_("returned false", encoder
);
233 printf("testing FLAC__stream_encoder_set_qlp_coeff_precision()... ");
234 if(!FLAC__stream_encoder_set_qlp_coeff_precision(encoder
, 0))
235 return die_s_("returned false", encoder
);
238 printf("testing FLAC__stream_encoder_set_do_qlp_coeff_prec_search()... ");
239 if(!FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder
, false))
240 return die_s_("returned false", encoder
);
243 printf("testing FLAC__stream_encoder_set_do_escape_coding()... ");
244 if(!FLAC__stream_encoder_set_do_escape_coding(encoder
, false))
245 return die_s_("returned false", encoder
);
248 printf("testing FLAC__stream_encoder_set_do_exhaustive_model_search()... ");
249 if(!FLAC__stream_encoder_set_do_exhaustive_model_search(encoder
, false))
250 return die_s_("returned false", encoder
);
253 printf("testing FLAC__stream_encoder_set_min_residual_partition_order()... ");
254 if(!FLAC__stream_encoder_set_min_residual_partition_order(encoder
, 0))
255 return die_s_("returned false", encoder
);
258 printf("testing FLAC__stream_encoder_set_max_residual_partition_order()... ");
259 if(!FLAC__stream_encoder_set_max_residual_partition_order(encoder
, 0))
260 return die_s_("returned false", encoder
);
263 printf("testing FLAC__stream_encoder_set_rice_parameter_search_dist()... ");
264 if(!FLAC__stream_encoder_set_rice_parameter_search_dist(encoder
, 0))
265 return die_s_("returned false", encoder
);
268 printf("testing FLAC__stream_encoder_set_total_samples_estimate()... ");
269 if(!FLAC__stream_encoder_set_total_samples_estimate(encoder
, streaminfo_
.data
.stream_info
.total_samples
))
270 return die_s_("returned false", encoder
);
273 printf("testing FLAC__stream_encoder_set_metadata()... ");
274 if(!FLAC__stream_encoder_set_metadata(encoder
, metadata_sequence_
, num_metadata_
))
275 return die_s_("returned false", encoder
);
278 if(layer
< LAYER_FILENAME
) {
279 printf("opening file for FLAC output... ");
280 file
= flac_fopen(flacfilename(is_ogg
), "w+b");
282 printf("ERROR (%s)\n", strerror(errno
));
290 printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg
? "ogg_":"");
291 init_status
= is_ogg
?
292 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
) :
293 FLAC__stream_encoder_init_stream(encoder
, stream_encoder_write_callback_
, /*seek_callback=*/0, /*tell_callback=*/0, stream_encoder_metadata_callback_
, /*client_data=*/file
);
295 case LAYER_SEEKABLE_STREAM
:
296 printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg
? "ogg_":"");
297 init_status
= is_ogg
?
298 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
) :
299 FLAC__stream_encoder_init_stream(encoder
, stream_encoder_write_callback_
, stream_encoder_seek_callback_
, stream_encoder_tell_callback_
, /*metadata_callback=*/0, /*client_data=*/file
);
302 printf("testing FLAC__stream_encoder_init_%sFILE()... ", is_ogg
? "ogg_":"");
303 init_status
= is_ogg
?
304 FLAC__stream_encoder_init_ogg_FILE(encoder
, file
, stream_encoder_progress_callback_
, /*client_data=*/0) :
305 FLAC__stream_encoder_init_FILE(encoder
, file
, stream_encoder_progress_callback_
, /*client_data=*/0);
308 printf("testing FLAC__stream_encoder_init_%sfile()... ", is_ogg
? "ogg_":"");
309 init_status
= is_ogg
?
310 FLAC__stream_encoder_init_ogg_file(encoder
, flacfilename(is_ogg
), stream_encoder_progress_callback_
, /*client_data=*/0) :
311 FLAC__stream_encoder_init_file(encoder
, flacfilename(is_ogg
), stream_encoder_progress_callback_
, /*client_data=*/0);
314 die_("internal error 001");
317 if(init_status
!= FLAC__STREAM_ENCODER_INIT_STATUS_OK
)
318 return die_s_(0, encoder
);
321 printf("testing FLAC__stream_encoder_get_state()... ");
322 state
= FLAC__stream_encoder_get_state(encoder
);
323 printf("returned state = %u (%s)... OK\n", (uint32_t)state
, FLAC__StreamEncoderStateString
[state
]);
325 printf("testing FLAC__stream_encoder_get_verify_decoder_state()... ");
326 dstate
= FLAC__stream_encoder_get_verify_decoder_state(encoder
);
327 printf("returned state = %u (%s)... OK\n", (uint32_t)dstate
, FLAC__StreamDecoderStateString
[dstate
]);
330 FLAC__uint64 absolute_sample
;
331 uint32_t frame_number
;
334 FLAC__int32 expected
;
337 printf("testing FLAC__stream_encoder_get_verify_decoder_error_stats()... ");
338 FLAC__stream_encoder_get_verify_decoder_error_stats(encoder
, &absolute_sample
, &frame_number
, &channel
, &sample
, &expected
, &got
);
342 printf("testing FLAC__stream_encoder_get_verify()... ");
343 if(FLAC__stream_encoder_get_verify(encoder
) != true) {
344 printf("FAILED, expected true, got false\n");
349 printf("testing FLAC__stream_encoder_get_streamable_subset()... ");
350 if(FLAC__stream_encoder_get_streamable_subset(encoder
) != true) {
351 printf("FAILED, expected true, got false\n");
356 printf("testing FLAC__stream_encoder_get_do_mid_side_stereo()... ");
357 if(FLAC__stream_encoder_get_do_mid_side_stereo(encoder
) != false) {
358 printf("FAILED, expected false, got true\n");
363 printf("testing FLAC__stream_encoder_get_loose_mid_side_stereo()... ");
364 if(FLAC__stream_encoder_get_loose_mid_side_stereo(encoder
) != false) {
365 printf("FAILED, expected false, got true\n");
370 printf("testing FLAC__stream_encoder_get_channels()... ");
371 if(FLAC__stream_encoder_get_channels(encoder
) != streaminfo_
.data
.stream_info
.channels
) {
372 printf("FAILED, expected %u, got %u\n", streaminfo_
.data
.stream_info
.channels
, FLAC__stream_encoder_get_channels(encoder
));
377 printf("testing FLAC__stream_encoder_get_bits_per_sample()... ");
378 if(FLAC__stream_encoder_get_bits_per_sample(encoder
) != streaminfo_
.data
.stream_info
.bits_per_sample
) {
379 printf("FAILED, expected %u, got %u\n", streaminfo_
.data
.stream_info
.bits_per_sample
, FLAC__stream_encoder_get_bits_per_sample(encoder
));
384 printf("testing FLAC__stream_encoder_get_sample_rate()... ");
385 if(FLAC__stream_encoder_get_sample_rate(encoder
) != streaminfo_
.data
.stream_info
.sample_rate
) {
386 printf("FAILED, expected %u, got %u\n", streaminfo_
.data
.stream_info
.sample_rate
, FLAC__stream_encoder_get_sample_rate(encoder
));
391 printf("testing FLAC__stream_encoder_get_blocksize()... ");
392 if(FLAC__stream_encoder_get_blocksize(encoder
) != streaminfo_
.data
.stream_info
.min_blocksize
) {
393 printf("FAILED, expected %u, got %u\n", streaminfo_
.data
.stream_info
.min_blocksize
, FLAC__stream_encoder_get_blocksize(encoder
));
398 printf("testing FLAC__stream_encoder_get_max_lpc_order()... ");
399 if(FLAC__stream_encoder_get_max_lpc_order(encoder
) != 0) {
400 printf("FAILED, expected %d, got %u\n", 0, FLAC__stream_encoder_get_max_lpc_order(encoder
));
405 printf("testing FLAC__stream_encoder_get_qlp_coeff_precision()... ");
406 (void)FLAC__stream_encoder_get_qlp_coeff_precision(encoder
);
407 /* we asked the encoder to auto select this so we accept anything */
410 printf("testing FLAC__stream_encoder_get_do_qlp_coeff_prec_search()... ");
411 if(FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder
) != false) {
412 printf("FAILED, expected false, got true\n");
417 printf("testing FLAC__stream_encoder_get_do_escape_coding()... ");
418 if(FLAC__stream_encoder_get_do_escape_coding(encoder
) != false) {
419 printf("FAILED, expected false, got true\n");
424 printf("testing FLAC__stream_encoder_get_do_exhaustive_model_search()... ");
425 if(FLAC__stream_encoder_get_do_exhaustive_model_search(encoder
) != false) {
426 printf("FAILED, expected false, got true\n");
431 printf("testing FLAC__stream_encoder_get_min_residual_partition_order()... ");
432 if(FLAC__stream_encoder_get_min_residual_partition_order(encoder
) != 0) {
433 printf("FAILED, expected %d, got %u\n", 0, FLAC__stream_encoder_get_min_residual_partition_order(encoder
));
438 printf("testing FLAC__stream_encoder_get_max_residual_partition_order()... ");
439 if(FLAC__stream_encoder_get_max_residual_partition_order(encoder
) != 0) {
440 printf("FAILED, expected %d, got %u\n", 0, FLAC__stream_encoder_get_max_residual_partition_order(encoder
));
445 printf("testing FLAC__stream_encoder_get_rice_parameter_search_dist()... ");
446 if(FLAC__stream_encoder_get_rice_parameter_search_dist(encoder
) != 0) {
447 printf("FAILED, expected %d, got %u\n", 0, FLAC__stream_encoder_get_rice_parameter_search_dist(encoder
));
452 printf("testing FLAC__stream_encoder_get_total_samples_estimate()... ");
453 if(FLAC__stream_encoder_get_total_samples_estimate(encoder
) != streaminfo_
.data
.stream_info
.total_samples
) {
454 printf("FAILED, expected %" PRIu64
", got %" PRIu64
"\n", streaminfo_
.data
.stream_info
.total_samples
, FLAC__stream_encoder_get_total_samples_estimate(encoder
));
459 /* init the dummy sample buffer */
460 for(i
= 0; i
< sizeof(samples
) / sizeof(FLAC__int32
); i
++)
463 printf("testing FLAC__stream_encoder_process()... ");
464 if(!FLAC__stream_encoder_process(encoder
, (const FLAC__int32
* const *)samples_array
, sizeof(samples
) / sizeof(FLAC__int32
)))
465 return die_s_("returned false", encoder
);
468 printf("testing FLAC__stream_encoder_process_interleaved()... ");
469 if(!FLAC__stream_encoder_process_interleaved(encoder
, samples
, sizeof(samples
) / sizeof(FLAC__int32
)))
470 return die_s_("returned false", encoder
);
473 printf("testing FLAC__stream_encoder_finish()... ");
474 if(!FLAC__stream_encoder_finish(encoder
))
475 return die_s_("returned false", encoder
);
478 if(layer
< LAYER_FILE
)
481 printf("testing FLAC__stream_encoder_delete()... ");
482 FLAC__stream_encoder_delete(encoder
);
485 printf("\nPASSED!\n");
490 FLAC__bool
test_encoders(void)
492 FLAC__bool is_ogg
= false;
495 init_metadata_blocks_();
497 if(!test_stream_encoder(LAYER_STREAM
, is_ogg
))
500 if(!test_stream_encoder(LAYER_SEEKABLE_STREAM
, is_ogg
))
503 if(!test_stream_encoder(LAYER_FILE
, is_ogg
))
506 if(!test_stream_encoder(LAYER_FILENAME
, is_ogg
))
509 (void) grabbag__file_remove_file(flacfilename(is_ogg
));
511 free_metadata_blocks_();
513 if(!FLAC_API_SUPPORTS_OGG_FLAC
|| is_ogg
)