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