ddd5a6e70dc2e098be2cf991e2eabfe0355087b4
[flac.git] / src / test_libFLAC++ / encoders.cpp
1 /* test_libFLAC++ - Unit tester for libFLAC++
2  * Copyright (C) 2002-2009  Josh Coalson
3  * Copyright (C) 2011-2014  Xiph.Org Foundation
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "encoders.h"
25 #include "FLAC/assert.h"
26 #include "FLAC++/encoder.h"
27 #include "share/grabbag.h"
28 extern "C" {
29 #include "test_libs_common/file_utils_flac.h"
30 #include "test_libs_common/metadata_utils.h"
31 }
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "share/compat.h"
37
38 #ifdef _MSC_VER
39 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
40 #pragma warning ( disable : 4800 )
41 #endif
42
43 typedef enum {
44         LAYER_STREAM = 0, /* FLAC__stream_encoder_init_stream() without seeking */
45         LAYER_SEEKABLE_STREAM, /* FLAC__stream_encoder_init_stream() with seeking */
46         LAYER_FILE, /* FLAC__stream_encoder_init_FILE() */
47         LAYER_FILENAME /* FLAC__stream_encoder_init_file() */
48 } Layer;
49
50 static const char * const LayerString[] = {
51         "Stream",
52         "Seekable Stream",
53         "FILE*",
54         "Filename"
55 };
56
57 static ::FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, picture_, unknown_;
58 static ::FLAC__StreamMetadata *metadata_sequence_[] = { &vorbiscomment_, &padding_, &seektable_, &application1_, &application2_, &cuesheet_, &picture_, &unknown_ };
59 static const unsigned num_metadata_ = sizeof(metadata_sequence_) / sizeof(metadata_sequence_[0]);
60
61 static const char *flacfilename(bool is_ogg)
62 {
63         return is_ogg? "metadata.oga" : "metadata.flac";
64 }
65
66 static bool die_(const char *msg)
67 {
68         printf("ERROR: %s\n", msg);
69         return false;
70 }
71
72 static bool die_s_(const char *msg, const FLAC::Encoder::Stream *encoder)
73 {
74         FLAC::Encoder::Stream::State state = encoder->get_state();
75
76         if(msg)
77                 printf("FAILED, %s", msg);
78         else
79                 printf("FAILED");
80
81         printf(", state = %u (%s)\n", (unsigned)((::FLAC__StreamEncoderState)state), state.as_cstring());
82         if(state == ::FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
83                 FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
84                 printf("      verify decoder state = %u (%s)\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
85         }
86
87         return false;
88 }
89
90 static void init_metadata_blocks_()
91 {
92         mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
93 }
94
95 static void free_metadata_blocks_()
96 {
97         mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
98 }
99
100 class StreamEncoder : public FLAC::Encoder::Stream {
101 public:
102         Layer layer_;
103         FILE *file_;
104
105         StreamEncoder(Layer layer): FLAC::Encoder::Stream(), layer_(layer), file_(0) { }
106         ~StreamEncoder() { }
107
108         // from FLAC::Encoder::Stream
109         ::FLAC__StreamEncoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes);
110         ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame);
111         ::FLAC__StreamEncoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset);
112         ::FLAC__StreamEncoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset);
113         void metadata_callback(const ::FLAC__StreamMetadata *metadata);
114 private:
115         StreamEncoder(const StreamEncoder&);
116         StreamEncoder&operator=(const StreamEncoder&);
117 };
118
119 ::FLAC__StreamEncoderReadStatus StreamEncoder::read_callback(FLAC__byte buffer[], size_t *bytes)
120 {
121         if(*bytes > 0) {
122                 *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, file_);
123                 if(ferror(file_))
124                         return ::FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
125                 else if(*bytes == 0)
126                         return ::FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
127                 else
128                         return ::FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
129         }
130         else
131                 return ::FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
132 }
133
134 ::FLAC__StreamEncoderWriteStatus StreamEncoder::write_callback(const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame)
135 {
136         (void)samples, (void)current_frame;
137
138         if(fwrite(buffer, 1, bytes, file_) != bytes)
139                 return ::FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
140         else
141                 return ::FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
142 }
143
144 ::FLAC__StreamEncoderSeekStatus StreamEncoder::seek_callback(FLAC__uint64 absolute_byte_offset)
145 {
146         if(layer_==LAYER_STREAM)
147                 return ::FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED;
148         else if(fseeko(file_, (FLAC__off_t)absolute_byte_offset, SEEK_SET) < 0)
149                 return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
150         else
151                 return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
152 }
153
154 ::FLAC__StreamEncoderTellStatus StreamEncoder::tell_callback(FLAC__uint64 *absolute_byte_offset)
155 {
156         FLAC__off_t pos;
157         if(layer_==LAYER_STREAM)
158                 return ::FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED;
159         else if((pos = ftello(file_)) < 0)
160                 return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
161         else {
162                 *absolute_byte_offset = (FLAC__uint64)pos;
163                 return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
164         }
165 }
166
167 void StreamEncoder::metadata_callback(const ::FLAC__StreamMetadata *metadata)
168 {
169         (void)metadata;
170 }
171
172 class FileEncoder : public FLAC::Encoder::File {
173 public:
174         Layer layer_;
175
176         FileEncoder(Layer layer): FLAC::Encoder::File(), layer_(layer) { }
177         ~FileEncoder() { }
178
179         // from FLAC::Encoder::File
180         void progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate);
181 };
182
183 void FileEncoder::progress_callback(FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate)
184 {
185         (void)bytes_written, (void)samples_written, (void)frames_written, (void)total_frames_estimate;
186 }
187
188 static FLAC::Encoder::Stream *new_by_layer(Layer layer)
189 {
190         if(layer < LAYER_FILE)
191                 return new StreamEncoder(layer);
192         else
193                 return new FileEncoder(layer);
194 }
195
196 static bool test_stream_encoder(Layer layer, bool is_ogg)
197 {
198         FLAC::Encoder::Stream *encoder;
199         ::FLAC__StreamEncoderInitStatus init_status;
200         FILE *file = 0;
201         FLAC__int32 samples[1024];
202         FLAC__int32 *samples_array[1] = { samples };
203         unsigned i;
204
205         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");
206
207         printf("allocating encoder instance... ");
208         encoder = new_by_layer(layer);
209         if(0 == encoder) {
210                 printf("FAILED, new returned NULL\n");
211                 return false;
212         }
213         printf("OK\n");
214
215         printf("testing is_valid()... ");
216         if(!encoder->is_valid()) {
217                 printf("FAILED, returned false\n");
218                 return false;
219         }
220         printf("OK\n");
221
222         if(is_ogg) {
223                 printf("testing set_ogg_serial_number()... ");
224                 if(!encoder->set_ogg_serial_number(file_utils__ogg_serial_number))
225                         return die_s_("returned false", encoder);
226                 printf("OK\n");
227         }
228
229         printf("testing set_verify()... ");
230         if(!encoder->set_verify(true))
231                 return die_s_("returned false", encoder);
232         printf("OK\n");
233
234         printf("testing set_streamable_subset()... ");
235         if(!encoder->set_streamable_subset(true))
236                 return die_s_("returned false", encoder);
237         printf("OK\n");
238
239         printf("testing set_channels()... ");
240         if(!encoder->set_channels(streaminfo_.data.stream_info.channels))
241                 return die_s_("returned false", encoder);
242         printf("OK\n");
243
244         printf("testing set_bits_per_sample()... ");
245         if(!encoder->set_bits_per_sample(streaminfo_.data.stream_info.bits_per_sample))
246                 return die_s_("returned false", encoder);
247         printf("OK\n");
248
249         printf("testing set_sample_rate()... ");
250         if(!encoder->set_sample_rate(streaminfo_.data.stream_info.sample_rate))
251                 return die_s_("returned false", encoder);
252         printf("OK\n");
253
254         printf("testing set_compression_level()... ");
255         if(!encoder->set_compression_level((unsigned)(-1)))
256                 return die_s_("returned false", encoder);
257         printf("OK\n");
258
259         printf("testing set_blocksize()... ");
260         if(!encoder->set_blocksize(streaminfo_.data.stream_info.min_blocksize))
261                 return die_s_("returned false", encoder);
262         printf("OK\n");
263
264         printf("testing set_do_mid_side_stereo()... ");
265         if(!encoder->set_do_mid_side_stereo(false))
266                 return die_s_("returned false", encoder);
267         printf("OK\n");
268
269         printf("testing set_loose_mid_side_stereo()... ");
270         if(!encoder->set_loose_mid_side_stereo(false))
271                 return die_s_("returned false", encoder);
272         printf("OK\n");
273
274         printf("testing set_max_lpc_order()... ");
275         if(!encoder->set_max_lpc_order(0))
276                 return die_s_("returned false", encoder);
277         printf("OK\n");
278
279         printf("testing set_qlp_coeff_precision()... ");
280         if(!encoder->set_qlp_coeff_precision(0))
281                 return die_s_("returned false", encoder);
282         printf("OK\n");
283
284         printf("testing set_do_qlp_coeff_prec_search()... ");
285         if(!encoder->set_do_qlp_coeff_prec_search(false))
286                 return die_s_("returned false", encoder);
287         printf("OK\n");
288
289         printf("testing set_do_escape_coding()... ");
290         if(!encoder->set_do_escape_coding(false))
291                 return die_s_("returned false", encoder);
292         printf("OK\n");
293
294         printf("testing set_do_exhaustive_model_search()... ");
295         if(!encoder->set_do_exhaustive_model_search(false))
296                 return die_s_("returned false", encoder);
297         printf("OK\n");
298
299         printf("testing set_min_residual_partition_order()... ");
300         if(!encoder->set_min_residual_partition_order(0))
301                 return die_s_("returned false", encoder);
302         printf("OK\n");
303
304         printf("testing set_max_residual_partition_order()... ");
305         if(!encoder->set_max_residual_partition_order(0))
306                 return die_s_("returned false", encoder);
307         printf("OK\n");
308
309         printf("testing set_rice_parameter_search_dist()... ");
310         if(!encoder->set_rice_parameter_search_dist(0))
311                 return die_s_("returned false", encoder);
312         printf("OK\n");
313
314         printf("testing set_total_samples_estimate()... ");
315         if(!encoder->set_total_samples_estimate(streaminfo_.data.stream_info.total_samples))
316                 return die_s_("returned false", encoder);
317         printf("OK\n");
318
319         printf("testing set_metadata()... ");
320         if(!encoder->set_metadata(metadata_sequence_, num_metadata_))
321                 return die_s_("returned false", encoder);
322         printf("OK\n");
323
324         if(layer < LAYER_FILENAME) {
325                 printf("opening file for FLAC output... ");
326                 file = ::flac_fopen(flacfilename(is_ogg), "w+b");
327                 if(0 == file) {
328                         printf("ERROR (%s)\n", strerror(errno));
329                         return false;
330                 }
331                 printf("OK\n");
332                 if(layer < LAYER_FILE)
333                         dynamic_cast<StreamEncoder*>(encoder)->file_ = file;
334         }
335
336         switch(layer) {
337                 case LAYER_STREAM:
338                 case LAYER_SEEKABLE_STREAM:
339                         printf("testing init%s()... ", is_ogg? "_ogg":"");
340                         init_status = is_ogg? encoder->init_ogg() : encoder->init();
341                         break;
342                 case LAYER_FILE:
343                         printf("testing init%s()... ", is_ogg? "_ogg":"");
344                         init_status = is_ogg?
345                                 dynamic_cast<FLAC::Encoder::File*>(encoder)->init_ogg(file) :
346                                 dynamic_cast<FLAC::Encoder::File*>(encoder)->init(file);
347                         break;
348                 case LAYER_FILENAME:
349                         printf("testing init%s()... ", is_ogg? "_ogg":"");
350                         init_status = is_ogg?
351                                 dynamic_cast<FLAC::Encoder::File*>(encoder)->init_ogg(flacfilename(is_ogg)) :
352                                 dynamic_cast<FLAC::Encoder::File*>(encoder)->init(flacfilename(is_ogg));
353                         break;
354                 default:
355                         die_("internal error 001");
356                         return false;
357         }
358         if(init_status != ::FLAC__STREAM_ENCODER_INIT_STATUS_OK)
359                 return die_s_(0, encoder);
360         printf("OK\n");
361
362         printf("testing get_state()... ");
363         FLAC::Encoder::Stream::State state = encoder->get_state();
364         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamEncoderState)state), state.as_cstring());
365
366         printf("testing get_verify_decoder_state()... ");
367         FLAC::Decoder::Stream::State dstate = encoder->get_verify_decoder_state();
368         printf("returned state = %u (%s)... OK\n", (unsigned)((::FLAC__StreamDecoderState)dstate), dstate.as_cstring());
369
370         {
371                 FLAC__uint64 absolute_sample;
372                 unsigned frame_number;
373                 unsigned channel;
374                 unsigned sample;
375                 FLAC__int32 expected;
376                 FLAC__int32 got;
377
378                 printf("testing get_verify_decoder_error_stats()... ");
379                 encoder->get_verify_decoder_error_stats(&absolute_sample, &frame_number, &channel, &sample, &expected, &got);
380                 printf("OK\n");
381         }
382
383         printf("testing get_verify()... ");
384         if(encoder->get_verify() != true) {
385                 printf("FAILED, expected true, got false\n");
386                 return false;
387         }
388         printf("OK\n");
389
390         printf("testing get_streamable_subset()... ");
391         if(encoder->get_streamable_subset() != true) {
392                 printf("FAILED, expected true, got false\n");
393                 return false;
394         }
395         printf("OK\n");
396
397         printf("testing get_do_mid_side_stereo()... ");
398         if(encoder->get_do_mid_side_stereo() != false) {
399                 printf("FAILED, expected false, got true\n");
400                 return false;
401         }
402         printf("OK\n");
403
404         printf("testing get_loose_mid_side_stereo()... ");
405         if(encoder->get_loose_mid_side_stereo() != false) {
406                 printf("FAILED, expected false, got true\n");
407                 return false;
408         }
409         printf("OK\n");
410
411         printf("testing get_channels()... ");
412         if(encoder->get_channels() != streaminfo_.data.stream_info.channels) {
413                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, encoder->get_channels());
414                 return false;
415         }
416         printf("OK\n");
417
418         printf("testing get_bits_per_sample()... ");
419         if(encoder->get_bits_per_sample() != streaminfo_.data.stream_info.bits_per_sample) {
420                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, encoder->get_bits_per_sample());
421                 return false;
422         }
423         printf("OK\n");
424
425         printf("testing get_sample_rate()... ");
426         if(encoder->get_sample_rate() != streaminfo_.data.stream_info.sample_rate) {
427                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, encoder->get_sample_rate());
428                 return false;
429         }
430         printf("OK\n");
431
432         printf("testing get_blocksize()... ");
433         if(encoder->get_blocksize() != streaminfo_.data.stream_info.min_blocksize) {
434                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, encoder->get_blocksize());
435                 return false;
436         }
437         printf("OK\n");
438
439         printf("testing get_max_lpc_order()... ");
440         if(encoder->get_max_lpc_order() != 0) {
441                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_lpc_order());
442                 return false;
443         }
444         printf("OK\n");
445
446         printf("testing get_qlp_coeff_precision()... ");
447         (void)encoder->get_qlp_coeff_precision();
448         /* we asked the encoder to auto select this so we accept anything */
449         printf("OK\n");
450
451         printf("testing get_do_qlp_coeff_prec_search()... ");
452         if(encoder->get_do_qlp_coeff_prec_search() != false) {
453                 printf("FAILED, expected false, got true\n");
454                 return false;
455         }
456         printf("OK\n");
457
458         printf("testing get_do_escape_coding()... ");
459         if(encoder->get_do_escape_coding() != false) {
460                 printf("FAILED, expected false, got true\n");
461                 return false;
462         }
463         printf("OK\n");
464
465         printf("testing get_do_exhaustive_model_search()... ");
466         if(encoder->get_do_exhaustive_model_search() != false) {
467                 printf("FAILED, expected false, got true\n");
468                 return false;
469         }
470         printf("OK\n");
471
472         printf("testing get_min_residual_partition_order()... ");
473         if(encoder->get_min_residual_partition_order() != 0) {
474                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_min_residual_partition_order());
475                 return false;
476         }
477         printf("OK\n");
478
479         printf("testing get_max_residual_partition_order()... ");
480         if(encoder->get_max_residual_partition_order() != 0) {
481                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_max_residual_partition_order());
482                 return false;
483         }
484         printf("OK\n");
485
486         printf("testing get_rice_parameter_search_dist()... ");
487         if(encoder->get_rice_parameter_search_dist() != 0) {
488                 printf("FAILED, expected %u, got %u\n", 0, encoder->get_rice_parameter_search_dist());
489                 return false;
490         }
491         printf("OK\n");
492
493         printf("testing get_total_samples_estimate()... ");
494         if(encoder->get_total_samples_estimate() != streaminfo_.data.stream_info.total_samples) {
495                 printf("FAILED, expected %" PRIu64 ", got %" PRIu64 "\n", streaminfo_.data.stream_info.total_samples, encoder->get_total_samples_estimate());
496                 return false;
497         }
498         printf("OK\n");
499
500         /* init the dummy sample buffer */
501         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
502                 samples[i] = i & 7;
503
504         printf("testing process()... ");
505         if(!encoder->process(samples_array, sizeof(samples) / sizeof(FLAC__int32)))
506                 return die_s_("returned false", encoder);
507         printf("OK\n");
508
509         printf("testing process_interleaved()... ");
510         if(!encoder->process_interleaved(samples, sizeof(samples) / sizeof(FLAC__int32)))
511                 return die_s_("returned false", encoder);
512         printf("OK\n");
513
514         printf("testing finish()... ");
515         if(!encoder->finish()) {
516                 state = encoder->get_state();
517                 printf("FAILED, returned false, state = %u (%s)\n", (unsigned)((::FLAC__StreamEncoderState)state), state.as_cstring());
518                 return false;
519         }
520         printf("OK\n");
521
522         if(layer < LAYER_FILE)
523                 ::fclose(dynamic_cast<StreamEncoder*>(encoder)->file_);
524
525         printf("freeing encoder instance... ");
526         delete encoder;
527         printf("OK\n");
528
529         printf("\nPASSED!\n");
530
531         return true;
532 }
533
534 bool test_encoders()
535 {
536         FLAC__bool is_ogg = false;
537
538         while(1) {
539                 init_metadata_blocks_();
540
541                 if(!test_stream_encoder(LAYER_STREAM, is_ogg))
542                         return false;
543
544                 if(!test_stream_encoder(LAYER_SEEKABLE_STREAM, is_ogg))
545                         return false;
546
547                 if(!test_stream_encoder(LAYER_FILE, is_ogg))
548                         return false;
549
550                 if(!test_stream_encoder(LAYER_FILENAME, is_ogg))
551                         return false;
552
553                 (void) grabbag__file_remove_file(flacfilename(is_ogg));
554
555                 free_metadata_blocks_();
556
557                 if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg)
558                         break;
559                 is_ogg = true;
560         }
561
562         return true;
563 }