fix compiler warnings
[flac.git] / src / test_libFLAC / encoders.c
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 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "encoders.h"
28 #include "FLAC/assert.h"
29 #include "FLAC/stream_encoder.h"
30 #include "share/grabbag.h"
31 #include "test_libs_common/file_utils_flac.h"
32 #include "test_libs_common/metadata_utils.h"
33
34 typedef enum {
35         LAYER_STREAM = 0, /* FLAC__stream_encoder_init_[ogg_]stream() without seeking */
36         LAYER_SEEKABLE_STREAM, /* FLAC__stream_encoder_init_[ogg_]stream() with seeking */
37         LAYER_FILE, /* FLAC__stream_encoder_init_[ogg_]FILE() */
38         LAYER_FILENAME /* FLAC__stream_encoder_init_[ogg_]file() */
39 } Layer;
40
41 static const char * const LayerString[] = {
42         "Stream",
43         "Seekable Stream",
44         "FILE*",
45         "Filename"
46 };
47
48 static FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_, cuesheet_, picture_, unknown_;
49 static FLAC__StreamMetadata *metadata_sequence_[] = { &vorbiscomment_, &padding_, &seektable_, &application1_, &application2_, &cuesheet_, &picture_, &unknown_ };
50 static const unsigned num_metadata_ = sizeof(metadata_sequence_) / sizeof(metadata_sequence_[0]);
51
52 static const char *flacfilename(FLAC__bool is_ogg)
53 {
54         return is_ogg? "metadata.ogg" : "metadata.flac";
55 }
56
57 static FLAC__bool die_(const char *msg)
58 {
59         printf("ERROR: %s\n", msg);
60         return false;
61 }
62
63 static FLAC__bool die_s_(const char *msg, const FLAC__StreamEncoder *encoder)
64 {
65         FLAC__StreamEncoderState state = FLAC__stream_encoder_get_state(encoder);
66
67         if(msg)
68                 printf("FAILED, %s", msg);
69         else
70                 printf("FAILED");
71
72         printf(", state = %u (%s)\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
73         if(state == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
74                 FLAC__StreamDecoderState dstate = FLAC__stream_encoder_get_verify_decoder_state(encoder);
75                 printf("      verify decoder state = %u (%s)\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
76         }
77
78         return false;
79 }
80
81 static void init_metadata_blocks_()
82 {
83         mutils__init_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
84 }
85
86 static void free_metadata_blocks_()
87 {
88         mutils__free_metadata_blocks(&streaminfo_, &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_, &cuesheet_, &picture_, &unknown_);
89 }
90
91 static FLAC__StreamEncoderReadStatus stream_encoder_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
92 {
93         (void)encoder, (void)buffer, (void)bytes, (void)client_data;
94         memset(buffer, 0, *bytes); /* init buffer to avoid valgrind errors */
95         return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
96 }
97
98 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)
99 {
100         (void)encoder, (void)buffer, (void)bytes, (void)samples, (void)current_frame, (void)client_data;
101         return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
102 }
103
104 static FLAC__StreamEncoderSeekStatus stream_encoder_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
105 {
106         (void)encoder, (void)absolute_byte_offset, (void)client_data;
107         return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
108 }
109
110 static FLAC__StreamEncoderTellStatus stream_encoder_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
111 {
112         (void)encoder, (void)client_data;
113         *absolute_byte_offset = 0;
114         return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
115 }
116
117 static void stream_encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
118 {
119         (void)encoder, (void)metadata, (void)client_data;
120 }
121
122 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)
123 {
124         (void)encoder, (void)bytes_written, (void)samples_written, (void)frames_written, (void)total_frames_estimate, (void)client_data;
125 }
126
127 static FLAC__bool test_stream_encoder(Layer layer, FLAC__bool is_ogg)
128 {
129         FLAC__StreamEncoder *encoder;
130         FLAC__StreamEncoderInitStatus init_status;
131         FLAC__StreamEncoderState state;
132         FLAC__StreamDecoderState dstate;
133         FILE *file = 0;
134         FLAC__int32 samples[1024];
135         FLAC__int32 *samples_array[1];
136         unsigned i;
137
138         samples_array[0] = samples;
139
140         printf("\n+++ libFLAC unit test: FLAC__StreamEncoder (layer: %s, format: %s)\n\n", LayerString[layer], is_ogg? "Ogg FLAC":"FLAC");
141
142         printf("testing FLAC__stream_encoder_new()... ");
143         encoder = FLAC__stream_encoder_new();
144         if(0 == encoder) {
145                 printf("FAILED, returned NULL\n");
146                 return false;
147         }
148         printf("OK\n");
149
150         if(is_ogg) {
151                 printf("testing FLAC__stream_encoder_set_ogg_serial_number()... ");
152                 if(!FLAC__stream_encoder_set_ogg_serial_number(encoder, file_utils__ogg_serial_number))
153                         return die_s_("returned false", encoder);
154                 printf("OK\n");
155         }
156
157         printf("testing FLAC__stream_encoder_set_verify()... ");
158         if(!FLAC__stream_encoder_set_verify(encoder, true))
159                 return die_s_("returned false", encoder);
160         printf("OK\n");
161
162         printf("testing FLAC__stream_encoder_set_streamable_subset()... ");
163         if(!FLAC__stream_encoder_set_streamable_subset(encoder, true))
164                 return die_s_("returned false", encoder);
165         printf("OK\n");
166
167         printf("testing FLAC__stream_encoder_set_channels()... ");
168         if(!FLAC__stream_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
169                 return die_s_("returned false", encoder);
170         printf("OK\n");
171
172         printf("testing FLAC__stream_encoder_set_bits_per_sample()... ");
173         if(!FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample))
174                 return die_s_("returned false", encoder);
175         printf("OK\n");
176
177         printf("testing FLAC__stream_encoder_set_sample_rate()... ");
178         if(!FLAC__stream_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
179                 return die_s_("returned false", encoder);
180         printf("OK\n");
181
182         printf("testing FLAC__stream_encoder_set_compression_level()... ");
183         if(!FLAC__stream_encoder_set_compression_level(encoder, (unsigned)(-1)))
184                 return die_s_("returned false", encoder);
185         printf("OK\n");
186
187         printf("testing FLAC__stream_encoder_set_blocksize()... ");
188         if(!FLAC__stream_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
189                 return die_s_("returned false", encoder);
190         printf("OK\n");
191
192         printf("testing FLAC__stream_encoder_set_do_mid_side_stereo()... ");
193         if(!FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false))
194                 return die_s_("returned false", encoder);
195         printf("OK\n");
196
197         printf("testing FLAC__stream_encoder_set_loose_mid_side_stereo()... ");
198         if(!FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false))
199                 return die_s_("returned false", encoder);
200         printf("OK\n");
201
202         printf("testing FLAC__stream_encoder_set_max_lpc_order()... ");
203         if(!FLAC__stream_encoder_set_max_lpc_order(encoder, 0))
204                 return die_s_("returned false", encoder);
205         printf("OK\n");
206
207         printf("testing FLAC__stream_encoder_set_qlp_coeff_precision()... ");
208         if(!FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0))
209                 return die_s_("returned false", encoder);
210         printf("OK\n");
211
212         printf("testing FLAC__stream_encoder_set_do_qlp_coeff_prec_search()... ");
213         if(!FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false))
214                 return die_s_("returned false", encoder);
215         printf("OK\n");
216
217         printf("testing FLAC__stream_encoder_set_do_escape_coding()... ");
218         if(!FLAC__stream_encoder_set_do_escape_coding(encoder, false))
219                 return die_s_("returned false", encoder);
220         printf("OK\n");
221
222         printf("testing FLAC__stream_encoder_set_do_exhaustive_model_search()... ");
223         if(!FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false))
224                 return die_s_("returned false", encoder);
225         printf("OK\n");
226
227         printf("testing FLAC__stream_encoder_set_min_residual_partition_order()... ");
228         if(!FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0))
229                 return die_s_("returned false", encoder);
230         printf("OK\n");
231
232         printf("testing FLAC__stream_encoder_set_max_residual_partition_order()... ");
233         if(!FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0))
234                 return die_s_("returned false", encoder);
235         printf("OK\n");
236
237         printf("testing FLAC__stream_encoder_set_rice_parameter_search_dist()... ");
238         if(!FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0))
239                 return die_s_("returned false", encoder);
240         printf("OK\n");
241
242         printf("testing FLAC__stream_encoder_set_total_samples_estimate()... ");
243         if(!FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
244                 return die_s_("returned false", encoder);
245         printf("OK\n");
246
247         printf("testing FLAC__stream_encoder_set_metadata()... ");
248         if(!FLAC__stream_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
249                 return die_s_("returned false", encoder);
250         printf("OK\n");
251
252         switch(layer) {
253                 case LAYER_STREAM:
254                         printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":"");
255                         init_status = is_ogg?
256                                 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=*/0) :
257                                 FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, stream_encoder_metadata_callback_, /*client_data=*/0);
258                         break;
259                 case LAYER_SEEKABLE_STREAM:
260                         printf("testing FLAC__stream_encoder_init_%sstream()... ", is_ogg? "ogg_":"");
261                         init_status = is_ogg?
262                                 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=*/0) :
263                                 FLAC__stream_encoder_init_stream(encoder, stream_encoder_write_callback_, stream_encoder_seek_callback_, stream_encoder_tell_callback_, /*metadata_callback=*/0, /*client_data=*/0);
264                         break;
265                 case LAYER_FILE:
266                         printf("opening file for FLAC output... ");
267                         file = fopen(flacfilename(is_ogg), "w+b");
268                         if(0 == file) {
269                                 printf("ERROR (%s)\n", strerror(errno));
270                                 return false;
271                         }
272                         printf("OK\n");
273
274                         printf("testing FLAC__stream_encoder_init_%sFILE()... ", is_ogg? "ogg_":"");
275                         init_status = is_ogg?
276                                 FLAC__stream_encoder_init_ogg_FILE(encoder, file, stream_encoder_progress_callback_, /*client_data=*/0) :
277                                 FLAC__stream_encoder_init_FILE(encoder, file, stream_encoder_progress_callback_, /*client_data=*/0);
278                         break;
279                 case LAYER_FILENAME:
280                         printf("testing FLAC__stream_encoder_init_%sfile()... ", is_ogg? "ogg_":"");
281                         init_status = is_ogg?
282                                 FLAC__stream_encoder_init_ogg_file(encoder, flacfilename(is_ogg), stream_encoder_progress_callback_, /*client_data=*/0) :
283                                 FLAC__stream_encoder_init_file(encoder, flacfilename(is_ogg), stream_encoder_progress_callback_, /*client_data=*/0);
284                         break;
285                 default:
286                         die_("internal error 001");
287                         return false;
288         }
289         if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK)
290                 return die_s_(0, encoder);
291         printf("OK\n");
292
293         printf("testing FLAC__stream_encoder_get_state()... ");
294         state = FLAC__stream_encoder_get_state(encoder);
295         printf("returned state = %u (%s)... OK\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
296
297         printf("testing FLAC__stream_encoder_get_verify_decoder_state()... ");
298         dstate = FLAC__stream_encoder_get_verify_decoder_state(encoder);
299         printf("returned state = %u (%s)... OK\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
300
301         {
302                 FLAC__uint64 absolute_sample;
303                 unsigned frame_number;
304                 unsigned channel;
305                 unsigned sample;
306                 FLAC__int32 expected;
307                 FLAC__int32 got;
308
309                 printf("testing FLAC__stream_encoder_get_verify_decoder_error_stats()... ");
310                 FLAC__stream_encoder_get_verify_decoder_error_stats(encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
311                 printf("OK\n");
312         }
313
314         printf("testing FLAC__stream_encoder_get_verify()... ");
315         if(FLAC__stream_encoder_get_verify(encoder) != true) {
316                 printf("FAILED, expected true, got false\n");
317                 return false;
318         }
319         printf("OK\n");
320
321         printf("testing FLAC__stream_encoder_get_streamable_subset()... ");
322         if(FLAC__stream_encoder_get_streamable_subset(encoder) != true) {
323                 printf("FAILED, expected true, got false\n");
324                 return false;
325         }
326         printf("OK\n");
327
328         printf("testing FLAC__stream_encoder_get_do_mid_side_stereo()... ");
329         if(FLAC__stream_encoder_get_do_mid_side_stereo(encoder) != false) {
330                 printf("FAILED, expected false, got true\n");
331                 return false;
332         }
333         printf("OK\n");
334
335         printf("testing FLAC__stream_encoder_get_loose_mid_side_stereo()... ");
336         if(FLAC__stream_encoder_get_loose_mid_side_stereo(encoder) != false) {
337                 printf("FAILED, expected false, got true\n");
338                 return false;
339         }
340         printf("OK\n");
341
342         printf("testing FLAC__stream_encoder_get_channels()... ");
343         if(FLAC__stream_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
344                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, FLAC__stream_encoder_get_channels(encoder));
345                 return false;
346         }
347         printf("OK\n");
348
349         printf("testing FLAC__stream_encoder_get_bits_per_sample()... ");
350         if(FLAC__stream_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
351                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, FLAC__stream_encoder_get_bits_per_sample(encoder));
352                 return false;
353         }
354         printf("OK\n");
355
356         printf("testing FLAC__stream_encoder_get_sample_rate()... ");
357         if(FLAC__stream_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
358                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, FLAC__stream_encoder_get_sample_rate(encoder));
359                 return false;
360         }
361         printf("OK\n");
362
363         printf("testing FLAC__stream_encoder_get_blocksize()... ");
364         if(FLAC__stream_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
365                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, FLAC__stream_encoder_get_blocksize(encoder));
366                 return false;
367         }
368         printf("OK\n");
369
370         printf("testing FLAC__stream_encoder_get_max_lpc_order()... ");
371         if(FLAC__stream_encoder_get_max_lpc_order(encoder) != 0) {
372                 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_lpc_order(encoder));
373                 return false;
374         }
375         printf("OK\n");
376
377         printf("testing FLAC__stream_encoder_get_qlp_coeff_precision()... ");
378         (void)FLAC__stream_encoder_get_qlp_coeff_precision(encoder);
379         /* we asked the encoder to auto select this so we accept anything */
380         printf("OK\n");
381
382         printf("testing FLAC__stream_encoder_get_do_qlp_coeff_prec_search()... ");
383         if(FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
384                 printf("FAILED, expected false, got true\n");
385                 return false;
386         }
387         printf("OK\n");
388
389         printf("testing FLAC__stream_encoder_get_do_escape_coding()... ");
390         if(FLAC__stream_encoder_get_do_escape_coding(encoder) != false) {
391                 printf("FAILED, expected false, got true\n");
392                 return false;
393         }
394         printf("OK\n");
395
396         printf("testing FLAC__stream_encoder_get_do_exhaustive_model_search()... ");
397         if(FLAC__stream_encoder_get_do_exhaustive_model_search(encoder) != false) {
398                 printf("FAILED, expected false, got true\n");
399                 return false;
400         }
401         printf("OK\n");
402
403         printf("testing FLAC__stream_encoder_get_min_residual_partition_order()... ");
404         if(FLAC__stream_encoder_get_min_residual_partition_order(encoder) != 0) {
405                 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_min_residual_partition_order(encoder));
406                 return false;
407         }
408         printf("OK\n");
409
410         printf("testing FLAC__stream_encoder_get_max_residual_partition_order()... ");
411         if(FLAC__stream_encoder_get_max_residual_partition_order(encoder) != 0) {
412                 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_residual_partition_order(encoder));
413                 return false;
414         }
415         printf("OK\n");
416
417         printf("testing FLAC__stream_encoder_get_rice_parameter_search_dist()... ");
418         if(FLAC__stream_encoder_get_rice_parameter_search_dist(encoder) != 0) {
419                 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_rice_parameter_search_dist(encoder));
420                 return false;
421         }
422         printf("OK\n");
423
424         printf("testing FLAC__stream_encoder_get_total_samples_estimate()... ");
425         if(FLAC__stream_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) {
426                 printf("FAILED, expected %llu, got %llu\n", (unsigned long long)streaminfo_.data.stream_info.total_samples, (unsigned long long)FLAC__stream_encoder_get_total_samples_estimate(encoder));
427                 return false;
428         }
429         printf("OK\n");
430
431         /* init the dummy sample buffer */
432         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
433                 samples[i] = i & 7;
434
435         printf("testing FLAC__stream_encoder_process()... ");
436         if(!FLAC__stream_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
437                 return die_s_("returned false", encoder);
438         printf("OK\n");
439
440         printf("testing FLAC__stream_encoder_process_interleaved()... ");
441         if(!FLAC__stream_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
442                 return die_s_("returned false", encoder);
443         printf("OK\n");
444
445         printf("testing FLAC__stream_encoder_finish()... ");
446         if(!FLAC__stream_encoder_finish(encoder))
447                 return die_s_("returned false", encoder);
448         printf("OK\n");
449
450         printf("testing FLAC__stream_encoder_delete()... ");
451         FLAC__stream_encoder_delete(encoder);
452         printf("OK\n");
453
454         printf("\nPASSED!\n");
455
456         return true;
457 }
458
459 FLAC__bool test_encoders()
460 {
461         FLAC__bool is_ogg = false;
462
463         while(1) {
464                 init_metadata_blocks_();
465
466                 if(!test_stream_encoder(LAYER_STREAM, is_ogg))
467                         return false;
468
469                 if(!test_stream_encoder(LAYER_SEEKABLE_STREAM, is_ogg))
470                         return false;
471
472                 if(!test_stream_encoder(LAYER_FILE, is_ogg))
473                         return false;
474
475                 if(!test_stream_encoder(LAYER_FILENAME, is_ogg))
476                         return false;
477
478                 (void) grabbag__file_remove_file(flacfilename(is_ogg));
479
480                 free_metadata_blocks_();
481
482                 if(!FLAC_API_SUPPORTS_OGG_FLAC || is_ogg)
483                         break;
484                 is_ogg = true;
485         }
486
487         return true;
488 }