revamp Makefile.lite system, use separate dirs and names for debug and release target...
[flac.git] / src / test_libOggFLAC / encoders.c
1 /* test_libOggFLAC - Unit tester for libOggFLAC
2  * Copyright (C) 2002  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 "file_utils.h"
21 #include "FLAC/assert.h"
22 #include "OggFLAC/stream_encoder.h"
23 #include "share/grabbag.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 static FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_;
29 static FLAC__StreamMetadata *metadata_sequence_[] = { &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_ };
30 static const unsigned num_metadata_ = 5;
31 static const char *oggflacfilename_ = "metadata.ogg";
32
33 static FLAC__bool die_s_(const char *msg, const OggFLAC__StreamEncoder *encoder)
34 {
35         OggFLAC__StreamEncoderState state = OggFLAC__stream_encoder_get_state(encoder);
36
37         if(msg)
38                 printf("FAILED, %s", msg);
39         else
40                 printf("FAILED");
41
42         printf(", state = %u (%s)\n", (unsigned)state, OggFLAC__StreamEncoderStateString[state]);
43         if(state == OggFLAC__STREAM_ENCODER_FLAC_STREAM_ENCODER_ERROR) {
44                 FLAC__StreamEncoderState state_ = OggFLAC__stream_encoder_get_FLAC_stream_encoder_state(encoder);
45                 printf("      FLAC stream encoder state = %u (%s)\n", (unsigned)state_, FLAC__StreamEncoderStateString[state_]);
46                 if(state_ == FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
47                         FLAC__StreamDecoderState dstate = OggFLAC__stream_encoder_get_verify_decoder_state(encoder);
48                         printf("      verify decoder state = %u (%s)\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
49                 }
50         }
51
52         return false;
53 }
54
55 static void *malloc_or_die_(size_t size)
56 {
57         void *x = malloc(size);
58         if(0 == x) {
59                 fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (unsigned)size);
60                 exit(1);
61         }
62         return x;
63 }
64
65 static void init_metadata_blocks_()
66 {
67         /*
68                 most of the actual numbers and data in the blocks don't matter,
69                 we just want to make sure the encoder encodes them correctly
70
71                 remember, the metadata interface gets tested after the encoders,
72                 so we do all the metadata manipulation here without it.
73         */
74
75         /* min/max_framesize and md5sum don't get written at first, so we have to leave them 0 */
76         streaminfo_.is_last = false;
77         streaminfo_.type = FLAC__METADATA_TYPE_STREAMINFO;
78         streaminfo_.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
79         streaminfo_.data.stream_info.min_blocksize = 576;
80         streaminfo_.data.stream_info.max_blocksize = 576;
81         streaminfo_.data.stream_info.min_framesize = 0;
82         streaminfo_.data.stream_info.max_framesize = 0;
83         streaminfo_.data.stream_info.sample_rate = 44100;
84         streaminfo_.data.stream_info.channels = 1;
85         streaminfo_.data.stream_info.bits_per_sample = 8;
86         streaminfo_.data.stream_info.total_samples = 0;
87         memset(streaminfo_.data.stream_info.md5sum, 0, 16);
88
89         padding_.is_last = false;
90         padding_.type = FLAC__METADATA_TYPE_PADDING;
91         padding_.length = 1234;
92
93         seektable_.is_last = false;
94         seektable_.type = FLAC__METADATA_TYPE_SEEKTABLE;
95         seektable_.data.seek_table.num_points = 2;
96         seektable_.length = seektable_.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
97         seektable_.data.seek_table.points = malloc_or_die_(seektable_.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
98         seektable_.data.seek_table.points[0].sample_number = 0;
99         seektable_.data.seek_table.points[0].stream_offset = 0;
100         seektable_.data.seek_table.points[0].frame_samples = streaminfo_.data.stream_info.min_blocksize;
101         seektable_.data.seek_table.points[1].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
102         seektable_.data.seek_table.points[1].stream_offset = 1000;
103         seektable_.data.seek_table.points[1].frame_samples = streaminfo_.data.stream_info.min_blocksize;
104
105         application1_.is_last = false;
106         application1_.type = FLAC__METADATA_TYPE_APPLICATION;
107         application1_.length = 8;
108         memcpy(application1_.data.application.id, "\xfe\xdc\xba\x98", 4);
109         application1_.data.application.data = malloc_or_die_(4);
110         memcpy(application1_.data.application.data, "\xf0\xe1\xd2\xc3", 4);
111
112         application2_.is_last = false;
113         application2_.type = FLAC__METADATA_TYPE_APPLICATION;
114         application2_.length = 4;
115         memcpy(application2_.data.application.id, "\x76\x54\x32\x10", 4);
116         application2_.data.application.data = 0;
117
118         vorbiscomment_.is_last = true;
119         vorbiscomment_.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
120         vorbiscomment_.length = (4 + 8) + 4 + (4 + 5) + (4 + 0);
121         vorbiscomment_.data.vorbis_comment.vendor_string.length = 8;
122         vorbiscomment_.data.vorbis_comment.vendor_string.entry = malloc_or_die_(8);
123         memcpy(vorbiscomment_.data.vorbis_comment.vendor_string.entry, "flac 1.x", 8);
124         vorbiscomment_.data.vorbis_comment.num_comments = 2;
125         vorbiscomment_.data.vorbis_comment.comments = malloc_or_die_(vorbiscomment_.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
126         vorbiscomment_.data.vorbis_comment.comments[0].length = 5;
127         vorbiscomment_.data.vorbis_comment.comments[0].entry = malloc_or_die_(5);
128         memcpy(vorbiscomment_.data.vorbis_comment.comments[0].entry, "ab=cd", 5);
129         vorbiscomment_.data.vorbis_comment.comments[1].length = 0;
130         vorbiscomment_.data.vorbis_comment.comments[1].entry = 0;
131 }
132
133 static void free_metadata_blocks_()
134 {
135         free(seektable_.data.seek_table.points);
136         free(application1_.data.application.data);
137         free(vorbiscomment_.data.vorbis_comment.vendor_string.entry);
138         free(vorbiscomment_.data.vorbis_comment.comments[0].entry);
139         free(vorbiscomment_.data.vorbis_comment.comments);
140 }
141
142 static FLAC__StreamEncoderWriteStatus stream_encoder_write_callback_(const OggFLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
143 {
144         (void)encoder, (void)buffer, (void)bytes, (void)samples, (void)current_frame, (void)client_data;
145         return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
146 }
147
148 static FLAC__bool test_stream_encoder()
149 {
150         OggFLAC__StreamEncoder *encoder;
151         OggFLAC__StreamEncoderState state;
152         FLAC__StreamEncoderState state_;
153         FLAC__StreamDecoderState dstate;
154         FLAC__int32 samples[1024];
155         FLAC__int32 *samples_array[1] = { samples };
156         unsigned i;
157
158         printf("\n+++ libOggFLAC unit test: OggFLAC__StreamEncoder\n\n");
159
160         printf("testing OggFLAC__stream_encoder_new()... ");
161         encoder = OggFLAC__stream_encoder_new();
162         if(0 == encoder) {
163                 printf("FAILED, returned NULL\n");
164                 return false;
165         }
166         printf("OK\n");
167
168         printf("testing OggFLAC__stream_encoder_set_serial_number()... ");
169         if(!OggFLAC__stream_encoder_set_serial_number(encoder, file_utils__serial_number))
170                 return die_s_("returned false", encoder);
171         printf("OK\n");
172
173         printf("testing OggFLAC__stream_encoder_set_verify()... ");
174         if(!OggFLAC__stream_encoder_set_verify(encoder, true))
175                 return die_s_("returned false", encoder);
176         printf("OK\n");
177
178         printf("testing OggFLAC__stream_encoder_set_streamable_subset()... ");
179         if(!OggFLAC__stream_encoder_set_streamable_subset(encoder, true))
180                 return die_s_("returned false", encoder);
181         printf("OK\n");
182
183         printf("testing OggFLAC__stream_encoder_set_do_mid_side_stereo()... ");
184         if(!OggFLAC__stream_encoder_set_do_mid_side_stereo(encoder, false))
185                 return die_s_("returned false", encoder);
186         printf("OK\n");
187
188         printf("testing OggFLAC__stream_encoder_set_loose_mid_side_stereo()... ");
189         if(!OggFLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false))
190                 return die_s_("returned false", encoder);
191         printf("OK\n");
192
193         printf("testing OggFLAC__stream_encoder_set_channels()... ");
194         if(!OggFLAC__stream_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
195                 return die_s_("returned false", encoder);
196         printf("OK\n");
197
198         printf("testing OggFLAC__stream_encoder_set_bits_per_sample()... ");
199         if(!OggFLAC__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");
202
203         printf("testing OggFLAC__stream_encoder_set_sample_rate()... ");
204         if(!OggFLAC__stream_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
205                 return die_s_("returned false", encoder);
206         printf("OK\n");
207
208         printf("testing OggFLAC__stream_encoder_set_blocksize()... ");
209         if(!OggFLAC__stream_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
210                 return die_s_("returned false", encoder);
211         printf("OK\n");
212
213         printf("testing OggFLAC__stream_encoder_set_max_lpc_order()... ");
214         if(!OggFLAC__stream_encoder_set_max_lpc_order(encoder, 0))
215                 return die_s_("returned false", encoder);
216         printf("OK\n");
217
218         printf("testing OggFLAC__stream_encoder_set_qlp_coeff_precision()... ");
219         if(!OggFLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0))
220                 return die_s_("returned false", encoder);
221         printf("OK\n");
222
223         printf("testing OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search()... ");
224         if(!OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false))
225                 return die_s_("returned false", encoder);
226         printf("OK\n");
227
228         printf("testing OggFLAC__stream_encoder_set_do_escape_coding()... ");
229         if(!OggFLAC__stream_encoder_set_do_escape_coding(encoder, false))
230                 return die_s_("returned false", encoder);
231         printf("OK\n");
232
233         printf("testing OggFLAC__stream_encoder_set_do_exhaustive_model_search()... ");
234         if(!OggFLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false))
235                 return die_s_("returned false", encoder);
236         printf("OK\n");
237
238         printf("testing OggFLAC__stream_encoder_set_min_residual_partition_order()... ");
239         if(!OggFLAC__stream_encoder_set_min_residual_partition_order(encoder, 0))
240                 return die_s_("returned false", encoder);
241         printf("OK\n");
242
243         printf("testing OggFLAC__stream_encoder_set_max_residual_partition_order()... ");
244         if(!OggFLAC__stream_encoder_set_max_residual_partition_order(encoder, 0))
245                 return die_s_("returned false", encoder);
246         printf("OK\n");
247
248         printf("testing OggFLAC__stream_encoder_set_rice_parameter_search_dist()... ");
249         if(!OggFLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0))
250                 return die_s_("returned false", encoder);
251         printf("OK\n");
252
253         printf("testing OggFLAC__stream_encoder_set_total_samples_estimate()... ");
254         if(!OggFLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
255                 return die_s_("returned false", encoder);
256         printf("OK\n");
257
258         printf("testing OggFLAC__stream_encoder_set_metadata()... ");
259         if(!OggFLAC__stream_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
260                 return die_s_("returned false", encoder);
261         printf("OK\n");
262
263         printf("testing OggFLAC__stream_encoder_set_write_callback()... ");
264         if(!OggFLAC__stream_encoder_set_write_callback(encoder, stream_encoder_write_callback_))
265                 return die_s_("returned false", encoder);
266         printf("OK\n");
267
268         printf("testing OggFLAC__stream_encoder_set_client_data()... ");
269         if(!OggFLAC__stream_encoder_set_client_data(encoder, 0))
270                 return die_s_("returned false", encoder);
271         printf("OK\n");
272
273         printf("testing OggFLAC__stream_encoder_init()... ");
274         if(OggFLAC__stream_encoder_init(encoder) != FLAC__STREAM_ENCODER_OK)
275                 return die_s_(0, encoder);
276         printf("OK\n");
277
278         printf("testing OggFLAC__stream_encoder_get_state()... ");
279         state = OggFLAC__stream_encoder_get_state(encoder);
280         printf("returned state = %u (%s)... OK\n", (unsigned)state, OggFLAC__StreamEncoderStateString[state]);
281
282         printf("testing OggFLAC__stream_encoder_get_FLAC_stream_encoder_state()... ");
283         state_ = OggFLAC__stream_encoder_get_FLAC_stream_encoder_state(encoder);
284         printf("returned state = %u (%s)... OK\n", (unsigned)state_, FLAC__StreamEncoderStateString[state_]);
285
286         printf("testing OggFLAC__stream_encoder_get_verify_decoder_state()... ");
287         dstate = OggFLAC__stream_encoder_get_verify_decoder_state(encoder);
288         printf("returned state = %u (%s)... OK\n", (unsigned)dstate, FLAC__StreamDecoderStateString[dstate]);
289
290         {
291                 FLAC__uint64 absolute_sample;
292                 unsigned frame_number;
293                 unsigned channel;
294                 unsigned sample;
295                 FLAC__int32 expected;
296                 FLAC__int32 got;
297
298                 printf("testing OggFLAC__stream_encoder_get_verify_decoder_error_stats()... ");
299                 OggFLAC__stream_encoder_get_verify_decoder_error_stats(encoder, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
300                 printf("OK\n");
301         }
302
303         printf("testing OggFLAC__stream_encoder_get_verify()... ");
304         if(OggFLAC__stream_encoder_get_verify(encoder) != true) {
305                 printf("FAILED, expected true, got false\n");
306                 return false;
307         }
308         printf("OK\n");
309
310         printf("testing OggFLAC__stream_encoder_get_streamable_subset()... ");
311         if(OggFLAC__stream_encoder_get_streamable_subset(encoder) != true) {
312                 printf("FAILED, expected true, got false\n");
313                 return false;
314         }
315         printf("OK\n");
316
317         printf("testing OggFLAC__stream_encoder_get_do_mid_side_stereo()... ");
318         if(OggFLAC__stream_encoder_get_do_mid_side_stereo(encoder) != false) {
319                 printf("FAILED, expected false, got true\n");
320                 return false;
321         }
322         printf("OK\n");
323
324         printf("testing OggFLAC__stream_encoder_get_loose_mid_side_stereo()... ");
325         if(OggFLAC__stream_encoder_get_loose_mid_side_stereo(encoder) != false) {
326                 printf("FAILED, expected false, got true\n");
327                 return false;
328         }
329         printf("OK\n");
330
331         printf("testing OggFLAC__stream_encoder_get_channels()... ");
332         if(OggFLAC__stream_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
333                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, OggFLAC__stream_encoder_get_channels(encoder));
334                 return false;
335         }
336         printf("OK\n");
337
338         printf("testing OggFLAC__stream_encoder_get_bits_per_sample()... ");
339         if(OggFLAC__stream_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
340                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, OggFLAC__stream_encoder_get_bits_per_sample(encoder));
341                 return false;
342         }
343         printf("OK\n");
344
345         printf("testing OggFLAC__stream_encoder_get_sample_rate()... ");
346         if(OggFLAC__stream_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
347                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, OggFLAC__stream_encoder_get_sample_rate(encoder));
348                 return false;
349         }
350         printf("OK\n");
351
352         printf("testing OggFLAC__stream_encoder_get_blocksize()... ");
353         if(OggFLAC__stream_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
354                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, OggFLAC__stream_encoder_get_blocksize(encoder));
355                 return false;
356         }
357         printf("OK\n");
358
359         printf("testing OggFLAC__stream_encoder_get_max_lpc_order()... ");
360         if(OggFLAC__stream_encoder_get_max_lpc_order(encoder) != 0) {
361                 printf("FAILED, expected %u, got %u\n", 0, OggFLAC__stream_encoder_get_max_lpc_order(encoder));
362                 return false;
363         }
364         printf("OK\n");
365
366         printf("testing OggFLAC__stream_encoder_get_qlp_coeff_precision()... ");
367         (void)OggFLAC__stream_encoder_get_qlp_coeff_precision(encoder);
368         /* we asked the encoder to auto select this so we accept anything */
369         printf("OK\n");
370
371         printf("testing OggFLAC__stream_encoder_get_do_qlp_coeff_prec_search()... ");
372         if(OggFLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
373                 printf("FAILED, expected false, got true\n");
374                 return false;
375         }
376         printf("OK\n");
377
378         printf("testing OggFLAC__stream_encoder_get_do_escape_coding()... ");
379         if(OggFLAC__stream_encoder_get_do_escape_coding(encoder) != false) {
380                 printf("FAILED, expected false, got true\n");
381                 return false;
382         }
383         printf("OK\n");
384
385         printf("testing OggFLAC__stream_encoder_get_do_exhaustive_model_search()... ");
386         if(OggFLAC__stream_encoder_get_do_exhaustive_model_search(encoder) != false) {
387                 printf("FAILED, expected false, got true\n");
388                 return false;
389         }
390         printf("OK\n");
391
392         printf("testing OggFLAC__stream_encoder_get_min_residual_partition_order()... ");
393         if(OggFLAC__stream_encoder_get_min_residual_partition_order(encoder) != 0) {
394                 printf("FAILED, expected %u, got %u\n", 0, OggFLAC__stream_encoder_get_min_residual_partition_order(encoder));
395                 return false;
396         }
397         printf("OK\n");
398
399         printf("testing OggFLAC__stream_encoder_get_max_residual_partition_order()... ");
400         if(OggFLAC__stream_encoder_get_max_residual_partition_order(encoder) != 0) {
401                 printf("FAILED, expected %u, got %u\n", 0, OggFLAC__stream_encoder_get_max_residual_partition_order(encoder));
402                 return false;
403         }
404         printf("OK\n");
405
406         printf("testing OggFLAC__stream_encoder_get_rice_parameter_search_dist()... ");
407         if(OggFLAC__stream_encoder_get_rice_parameter_search_dist(encoder) != 0) {
408                 printf("FAILED, expected %u, got %u\n", 0, OggFLAC__stream_encoder_get_rice_parameter_search_dist(encoder));
409                 return false;
410         }
411         printf("OK\n");
412
413         printf("testing OggFLAC__stream_encoder_get_total_samples_estimate()... ");
414         if(OggFLAC__stream_encoder_get_total_samples_estimate(encoder) != streaminfo_.data.stream_info.total_samples) {
415                 printf("FAILED, expected %llu, got %llu\n", streaminfo_.data.stream_info.total_samples, OggFLAC__stream_encoder_get_total_samples_estimate(encoder));
416                 return false;
417         }
418         printf("OK\n");
419
420         /* init the dummy sample buffer */
421         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
422                 samples[i] = i & 7;
423
424         printf("testing OggFLAC__stream_encoder_process()... ");
425         if(!OggFLAC__stream_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
426                 return die_s_("returned false", encoder);
427         printf("OK\n");
428
429         printf("testing OggFLAC__stream_encoder_process_interleaved()... ");
430         if(!OggFLAC__stream_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
431                 return die_s_("returned false", encoder);
432         printf("OK\n");
433
434         printf("testing OggFLAC__stream_encoder_finish()... ");
435         OggFLAC__stream_encoder_finish(encoder);
436         printf("OK\n");
437
438         printf("testing OggFLAC__stream_encoder_delete()... ");
439         OggFLAC__stream_encoder_delete(encoder);
440         printf("OK\n");
441
442         printf("\nPASSED!\n");
443
444         return true;
445 }
446
447 FLAC__bool test_encoders()
448 {
449         init_metadata_blocks_();
450
451         if(!test_stream_encoder())
452                 return false;
453
454         (void) grabbag__file_remove_file(oggflacfilename_);
455         free_metadata_blocks_();
456
457         return true;
458 }