Set version to 1.3.1pre1.
[flac.git] / src / test_libFLAC / encoders.c
blob17318dca19247b6e0c085d572668d5d1c2270346
1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2014 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.
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "encoders.h"
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"
36 typedef enum {
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() */
41 } Layer;
43 static const char * const LayerString[] = {
44 "Stream",
45 "Seekable Stream",
46 "FILE*",
47 "Filename"
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 unsigned 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);
62 return false;
65 static FLAC__bool die_s_(const char *msg, const FLAC__StreamEncoder *encoder)
67 FLAC__StreamEncoderState state = FLAC__stream_encoder_get_state(encoder);
69 if(msg)
70 printf("FAILED, %s", msg);
71 else
72 printf("FAILED");
74 printf(", state = %u (%s)\n", (unsigned)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", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
80 return false;
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;
96 (void)encoder;
97 if(*bytes > 0) {
98 *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, f);
99 if(ferror(f))
100 return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
101 else if(*bytes == 0)
102 return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
103 else
104 return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
106 else
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, unsigned samples, unsigned 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;
116 else
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;
123 (void)encoder;
124 if(fseeko(f, (long)absolute_byte_offset, SEEK_SET) < 0)
125 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
126 else
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;
133 FLAC__off_t pos;
134 (void)encoder;
135 if((pos = ftello(f)) < 0)
136 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
137 else {
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, unsigned frames_written, unsigned 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;
159 FILE *file = 0;
160 FLAC__int32 samples[1024];
161 FLAC__int32 *samples_array[1];
162 unsigned i;
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();
170 if(0 == encoder) {
171 printf("FAILED, returned NULL\n");
172 return false;
174 printf("OK\n");
176 if(is_ogg) {
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);
180 printf("OK\n");
183 printf("testing FLAC__stream_encoder_set_verify()... ");
184 if(!FLAC__stream_encoder_set_verify(encoder, true))
185 return die_s_("returned false", encoder);
186 printf("OK\n");
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);
191 printf("OK\n");
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);
196 printf("OK\n");
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);
201 printf("OK\n");
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);
206 printf("OK\n");
208 printf("testing FLAC__stream_encoder_set_compression_level()... ");
209 if(!FLAC__stream_encoder_set_compression_level(encoder, (unsigned)(-1)))
210 return die_s_("returned false", encoder);
211 printf("OK\n");
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);
216 printf("OK\n");
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);
221 printf("OK\n");
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);
226 printf("OK\n");
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);
231 printf("OK\n");
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);
236 printf("OK\n");
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);
241 printf("OK\n");
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);
246 printf("OK\n");
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);
251 printf("OK\n");
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);
256 printf("OK\n");
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);
261 printf("OK\n");
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);
266 printf("OK\n");
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);
271 printf("OK\n");
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);
276 printf("OK\n");
278 if(layer < LAYER_FILENAME) {
279 printf("opening file for FLAC output... ");
280 file = flac_fopen(flacfilename(is_ogg), "w+b");
281 if(0 == file) {
282 printf("ERROR (%s)\n", strerror(errno));
283 return false;
285 printf("OK\n");
288 switch(layer) {
289 case LAYER_STREAM:
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);
294 break;
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);
300 break;
301 case LAYER_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);
306 break;
307 case LAYER_FILENAME:
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);
312 break;
313 default:
314 die_("internal error 001");
315 return false;
317 if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
318 return die_s_(0, encoder);
319 printf("OK\n");
321 printf("testing FLAC__stream_encoder_get_state()... ");
322 state = FLAC__stream_encoder_get_state(encoder);
323 printf("returned state = %u (%s)... OK\n", (unsigned)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", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
330 FLAC__uint64 absolute_sample;
331 unsigned frame_number;
332 unsigned channel;
333 unsigned sample;
334 FLAC__int32 expected;
335 FLAC__int32 got;
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);
339 printf("OK\n");
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");
345 return false;
347 printf("OK\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");
352 return false;
354 printf("OK\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");
359 return false;
361 printf("OK\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");
366 return false;
368 printf("OK\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));
373 return false;
375 printf("OK\n");
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));
380 return false;
382 printf("OK\n");
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));
387 return false;
389 printf("OK\n");
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));
394 return false;
396 printf("OK\n");
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 %u, got %u\n", 0, FLAC__stream_encoder_get_max_lpc_order(encoder));
401 return false;
403 printf("OK\n");
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 */
408 printf("OK\n");
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");
413 return false;
415 printf("OK\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");
420 return false;
422 printf("OK\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");
427 return false;
429 printf("OK\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 %u, got %u\n", 0, FLAC__stream_encoder_get_min_residual_partition_order(encoder));
434 return false;
436 printf("OK\n");
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 %u, got %u\n", 0, FLAC__stream_encoder_get_max_residual_partition_order(encoder));
441 return false;
443 printf("OK\n");
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 %u, got %u\n", 0, FLAC__stream_encoder_get_rice_parameter_search_dist(encoder));
448 return false;
450 printf("OK\n");
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));
455 return false;
457 printf("OK\n");
459 /* init the dummy sample buffer */
460 for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
461 samples[i] = i & 7;
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);
466 printf("OK\n");
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);
471 printf("OK\n");
473 printf("testing FLAC__stream_encoder_finish()... ");
474 if(!FLAC__stream_encoder_finish(encoder))
475 return die_s_("returned false", encoder);
476 printf("OK\n");
478 if(layer < LAYER_FILE)
479 fclose(file);
481 printf("testing FLAC__stream_encoder_delete()... ");
482 FLAC__stream_encoder_delete(encoder);
483 printf("OK\n");
485 printf("\nPASSED!\n");
487 return true;
490 FLAC__bool test_encoders(void)
492 FLAC__bool is_ogg = false;
494 while(1) {
495 init_metadata_blocks_();
497 if(!test_stream_encoder(LAYER_STREAM, is_ogg))
498 return false;
500 if(!test_stream_encoder(LAYER_SEEKABLE_STREAM, is_ogg))
501 return false;
503 if(!test_stream_encoder(LAYER_FILE, is_ogg))
504 return false;
506 if(!test_stream_encoder(LAYER_FILENAME, is_ogg))
507 return false;
509 (void) grabbag__file_remove_file(flacfilename(is_ogg));
511 free_metadata_blocks_();
513 if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg)
514 break;
515 is_ogg = true;
518 return true;