another big glob of changes/fixes
[flac.git] / src / test_libFLAC / encoders.c
1 /* test_libFLAC - Unit tester for libFLAC
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 "FLAC/stream_encoder.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 static FLAC__StreamMetadata streaminfo_, padding_, seektable_, application1_, application2_, vorbiscomment_;
28 static FLAC__StreamMetadata *metadata_sequence_[] = { &padding_, &seektable_, &application1_, &application2_, &vorbiscomment_ };
29 static const unsigned num_metadata_ = 5;
30 static const char *flacfilename_ = "metadata.flac";
31
32 static FLAC__bool die_s_(const char *msg, const FLAC__StreamEncoder *encoder)
33 {
34         FLAC__StreamEncoderState state = FLAC__stream_encoder_get_state(encoder);
35
36         if(msg)
37                 printf("FAILED, %s, state = %u (%s)\n", msg, (unsigned)state, FLAC__StreamEncoderStateString[state]);
38         else
39                 printf("FAILED, state = %u (%s)\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
40
41         return false;
42 }
43
44 static void *malloc_or_die_(size_t size)
45 {
46         void *x = malloc(size);
47         if(0 == x) {
48                 fprintf(stderr, "ERROR: out of memory allocating %u bytes\n", (unsigned)size);
49                 exit(1);
50         }
51         return x;
52 }
53
54 static void init_metadata_blocks_()
55 {
56         /*
57                 most of the actual numbers and data in the blocks don't matter,
58                 we just want to make sure the encoder encodes them correctly
59
60                 remember, the metadata interface gets tested after the encoders,
61                 so we do all the metadata manipulation here without it.
62         */
63
64         /* min/max_framesize and md5sum don't get written at first, so we have to leave them 0 */
65     streaminfo_.is_last = false;
66     streaminfo_.type = FLAC__METADATA_TYPE_STREAMINFO;
67     streaminfo_.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
68     streaminfo_.data.stream_info.min_blocksize = 576;
69     streaminfo_.data.stream_info.max_blocksize = 576;
70     streaminfo_.data.stream_info.min_framesize = 0;
71     streaminfo_.data.stream_info.max_framesize = 0;
72     streaminfo_.data.stream_info.sample_rate = 44100;
73     streaminfo_.data.stream_info.channels = 1;
74     streaminfo_.data.stream_info.bits_per_sample = 8;
75     streaminfo_.data.stream_info.total_samples = 0;
76         memset(streaminfo_.data.stream_info.md5sum, 0, 16);
77
78     padding_.is_last = false;
79     padding_.type = FLAC__METADATA_TYPE_PADDING;
80     padding_.length = 1234;
81
82     seektable_.is_last = false;
83     seektable_.type = FLAC__METADATA_TYPE_SEEKTABLE;
84         seektable_.data.seek_table.num_points = 2;
85     seektable_.length = seektable_.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
86         seektable_.data.seek_table.points = malloc_or_die_(seektable_.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint));
87         seektable_.data.seek_table.points[0].sample_number = 0;
88         seektable_.data.seek_table.points[0].stream_offset = 0;
89         seektable_.data.seek_table.points[0].frame_samples = streaminfo_.data.stream_info.min_blocksize;
90         seektable_.data.seek_table.points[1].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
91         seektable_.data.seek_table.points[1].stream_offset = 1000;
92         seektable_.data.seek_table.points[1].frame_samples = streaminfo_.data.stream_info.min_blocksize;
93
94     application1_.is_last = false;
95     application1_.type = FLAC__METADATA_TYPE_APPLICATION;
96     application1_.length = 8;
97         memcpy(application1_.data.application.id, "\xfe\xdc\xba\x98", 4);
98         application1_.data.application.data = malloc_or_die_(4);
99         memcpy(application1_.data.application.data, "\xf0\xe1\xd2\xc3", 4);
100
101     application2_.is_last = false;
102     application2_.type = FLAC__METADATA_TYPE_APPLICATION;
103     application2_.length = 4;
104         memcpy(application2_.data.application.id, "\x76\x54\x32\x10", 4);
105         application2_.data.application.data = 0;
106
107     vorbiscomment_.is_last = true;
108     vorbiscomment_.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
109     vorbiscomment_.length = (4 + 8) + 4 + (4 + 5) + (4 + 0);
110         vorbiscomment_.data.vorbis_comment.vendor_string.length = 8;
111         vorbiscomment_.data.vorbis_comment.vendor_string.entry = malloc_or_die_(8);
112         memcpy(vorbiscomment_.data.vorbis_comment.vendor_string.entry, "flac 1.x", 8);
113         vorbiscomment_.data.vorbis_comment.num_comments = 2;
114         vorbiscomment_.data.vorbis_comment.comments = malloc_or_die_(vorbiscomment_.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry));
115         vorbiscomment_.data.vorbis_comment.comments[0].length = 5;
116         vorbiscomment_.data.vorbis_comment.comments[0].entry = malloc_or_die_(5);
117         memcpy(vorbiscomment_.data.vorbis_comment.comments[0].entry, "ab=cd", 5);
118         vorbiscomment_.data.vorbis_comment.comments[1].length = 0;
119         vorbiscomment_.data.vorbis_comment.comments[1].entry = 0;
120 }
121
122 static void free_metadata_blocks_()
123 {
124         free(seektable_.data.seek_table.points);
125         free(application1_.data.application.data);
126         free(vorbiscomment_.data.vorbis_comment.vendor_string.entry);
127         free(vorbiscomment_.data.vorbis_comment.comments[0].entry);
128         free(vorbiscomment_.data.vorbis_comment.comments);
129 }
130
131 static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
132 {
133         (void)encoder, (void)buffer, (void)bytes, (void)samples, (void)current_frame, (void)client_data;
134         return FLAC__STREAM_ENCODER_WRITE_OK;
135 }
136
137 static void encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
138 {
139         (void)encoder, (void)metadata, (void)client_data;
140 }
141
142 static FLAC__bool test_stream_encoder()
143 {
144         FLAC__StreamEncoder *encoder;
145         FLAC__StreamEncoderState state;
146         FLAC__int32 samples[1024];
147         FLAC__int32 *samples_array[1] = { samples };
148         unsigned i;
149
150         printf("\n+++ libFLAC unit test: FLAC__StreamEncoder\n\n");
151
152         printf("testing FLAC__stream_encoder_new()... ");
153         encoder = FLAC__stream_encoder_new();
154         if(0 == encoder) {
155                 printf("FAILED, returned NULL\n");
156                 return false;
157         }
158         printf("OK\n");
159
160         printf("testing FLAC__stream_encoder_set_streamable_subset()... ");
161         if(!FLAC__stream_encoder_set_streamable_subset(encoder, true))
162                 return die_s_("returned false", encoder);
163         printf("OK\n");
164
165         printf("testing FLAC__stream_encoder_set_do_mid_side_stereo()... ");
166         if(!FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false))
167                 return die_s_("returned false", encoder);
168         printf("OK\n");
169
170         printf("testing FLAC__stream_encoder_set_loose_mid_side_stereo()... ");
171         if(!FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false))
172                 return die_s_("returned false", encoder);
173         printf("OK\n");
174
175         printf("testing FLAC__stream_encoder_set_channels()... ");
176         if(!FLAC__stream_encoder_set_channels(encoder, streaminfo_.data.stream_info.channels))
177                 return die_s_("returned false", encoder);
178         printf("OK\n");
179
180         printf("testing FLAC__stream_encoder_set_bits_per_sample()... ");
181         if(!FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo_.data.stream_info.bits_per_sample))
182                 return die_s_("returned false", encoder);
183         printf("OK\n");
184
185         printf("testing FLAC__stream_encoder_set_sample_rate()... ");
186         if(!FLAC__stream_encoder_set_sample_rate(encoder, streaminfo_.data.stream_info.sample_rate))
187                 return die_s_("returned false", encoder);
188         printf("OK\n");
189
190         printf("testing FLAC__stream_encoder_set_blocksize()... ");
191         if(!FLAC__stream_encoder_set_blocksize(encoder, streaminfo_.data.stream_info.min_blocksize))
192                 return die_s_("returned false", encoder);
193         printf("OK\n");
194
195         printf("testing FLAC__stream_encoder_set_max_lpc_order()... ");
196         if(!FLAC__stream_encoder_set_max_lpc_order(encoder, 0))
197                 return die_s_("returned false", encoder);
198         printf("OK\n");
199
200         printf("testing FLAC__stream_encoder_set_qlp_coeff_precision()... ");
201         if(!FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0))
202                 return die_s_("returned false", encoder);
203         printf("OK\n");
204
205         printf("testing FLAC__stream_encoder_set_do_qlp_coeff_prec_search()... ");
206         if(!FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false))
207                 return die_s_("returned false", encoder);
208         printf("OK\n");
209
210         printf("testing FLAC__stream_encoder_set_do_escape_coding()... ");
211         if(!FLAC__stream_encoder_set_do_escape_coding(encoder, false))
212                 return die_s_("returned false", encoder);
213         printf("OK\n");
214
215         printf("testing FLAC__stream_encoder_set_do_exhaustive_model_search()... ");
216         if(!FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false))
217                 return die_s_("returned false", encoder);
218         printf("OK\n");
219
220         printf("testing FLAC__stream_encoder_set_min_residual_partition_order()... ");
221         if(!FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0))
222                 return die_s_("returned false", encoder);
223         printf("OK\n");
224
225         printf("testing FLAC__stream_encoder_set_max_residual_partition_order()... ");
226         if(!FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0))
227                 return die_s_("returned false", encoder);
228         printf("OK\n");
229
230         printf("testing FLAC__stream_encoder_set_rice_parameter_search_dist()... ");
231         if(!FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0))
232                 return die_s_("returned false", encoder);
233         printf("OK\n");
234
235         printf("testing FLAC__stream_encoder_set_total_samples_estimate()... ");
236         if(!FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo_.data.stream_info.total_samples))
237                 return die_s_("returned false", encoder);
238         printf("OK\n");
239
240         printf("testing FLAC__stream_encoder_set_metadata()... ");
241         if(!FLAC__stream_encoder_set_metadata(encoder, metadata_sequence_, num_metadata_))
242                 return die_s_("returned false", encoder);
243         printf("OK\n");
244
245         printf("testing FLAC__stream_encoder_set_write_callback()... ");
246         if(!FLAC__stream_encoder_set_write_callback(encoder, encoder_write_callback_))
247                 return die_s_("returned false", encoder);
248         printf("OK\n");
249
250         printf("testing FLAC__stream_encoder_set_metadata_callback()... ");
251         if(!FLAC__stream_encoder_set_metadata_callback(encoder, encoder_metadata_callback_))
252                 return die_s_("returned false", encoder);
253         printf("OK\n");
254
255         printf("testing FLAC__stream_encoder_set_client_data()... ");
256         if(!FLAC__stream_encoder_set_client_data(encoder, 0))
257                 return die_s_("returned false", encoder);
258         printf("OK\n");
259
260         printf("testing FLAC__stream_encoder_init()... ");
261         if(FLAC__stream_encoder_init(encoder) != FLAC__STREAM_ENCODER_OK)
262                 return die_s_(0, encoder);
263         printf("OK\n");
264
265         printf("testing FLAC__stream_encoder_get_state()... ");
266         state = FLAC__stream_encoder_get_state(encoder);
267         printf("returned state = %u (%s)... OK\n", (unsigned)state, FLAC__StreamEncoderStateString[state]);
268
269         printf("testing FLAC__stream_encoder_get_streamable_subset()... ");
270         if(FLAC__stream_encoder_get_streamable_subset(encoder) != true) {
271                 printf("FAILED, expected true, got false\n");
272                 return false;
273         }
274         printf("OK\n");
275
276         printf("testing FLAC__stream_encoder_get_do_mid_side_stereo()... ");
277         if(FLAC__stream_encoder_get_do_mid_side_stereo(encoder) != false) {
278                 printf("FAILED, expected false, got true\n");
279                 return false;
280         }
281         printf("OK\n");
282
283         printf("testing FLAC__stream_encoder_get_loose_mid_side_stereo()... ");
284         if(FLAC__stream_encoder_get_loose_mid_side_stereo(encoder) != false) {
285                 printf("FAILED, expected false, got true\n");
286                 return false;
287         }
288         printf("OK\n");
289
290         printf("testing FLAC__stream_encoder_get_channels()... ");
291         if(FLAC__stream_encoder_get_channels(encoder) != streaminfo_.data.stream_info.channels) {
292                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.channels, FLAC__stream_encoder_get_channels(encoder));
293                 return false;
294         }
295         printf("OK\n");
296
297         printf("testing FLAC__stream_encoder_get_bits_per_sample()... ");
298         if(FLAC__stream_encoder_get_bits_per_sample(encoder) != streaminfo_.data.stream_info.bits_per_sample) {
299                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.bits_per_sample, FLAC__stream_encoder_get_bits_per_sample(encoder));
300                 return false;
301         }
302         printf("OK\n");
303
304         printf("testing FLAC__stream_encoder_get_sample_rate()... ");
305         if(FLAC__stream_encoder_get_sample_rate(encoder) != streaminfo_.data.stream_info.sample_rate) {
306                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.sample_rate, FLAC__stream_encoder_get_sample_rate(encoder));
307                 return false;
308         }
309         printf("OK\n");
310
311         printf("testing FLAC__stream_encoder_get_blocksize()... ");
312         if(FLAC__stream_encoder_get_blocksize(encoder) != streaminfo_.data.stream_info.min_blocksize) {
313                 printf("FAILED, expected %u, got %u\n", streaminfo_.data.stream_info.min_blocksize, FLAC__stream_encoder_get_blocksize(encoder));
314                 return false;
315         }
316         printf("OK\n");
317
318         printf("testing FLAC__stream_encoder_get_max_lpc_order()... ");
319         if(FLAC__stream_encoder_get_max_lpc_order(encoder) != 0) {
320                 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_lpc_order(encoder));
321                 return false;
322         }
323         printf("OK\n");
324
325         printf("testing FLAC__stream_encoder_get_qlp_coeff_precision()... ");
326         (void)FLAC__stream_encoder_get_qlp_coeff_precision(encoder);
327         /* we asked the encoder to auto select this so we accept anything */
328         printf("OK\n");
329
330         printf("testing FLAC__stream_encoder_get_do_qlp_coeff_prec_search()... ");
331         if(FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder) != false) {
332                 printf("FAILED, expected false, got true\n");
333                 return false;
334         }
335         printf("OK\n");
336
337         printf("testing FLAC__stream_encoder_get_do_escape_coding()... ");
338         if(FLAC__stream_encoder_get_do_escape_coding(encoder) != false) {
339                 printf("FAILED, expected false, got true\n");
340                 return false;
341         }
342         printf("OK\n");
343
344         printf("testing FLAC__stream_encoder_get_do_exhaustive_model_search()... ");
345         if(FLAC__stream_encoder_get_do_exhaustive_model_search(encoder) != false) {
346                 printf("FAILED, expected false, got true\n");
347                 return false;
348         }
349         printf("OK\n");
350
351         printf("testing FLAC__stream_encoder_get_min_residual_partition_order()... ");
352         if(FLAC__stream_encoder_get_min_residual_partition_order(encoder) != 0) {
353                 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_min_residual_partition_order(encoder));
354                 return false;
355         }
356         printf("OK\n");
357
358         printf("testing FLAC__stream_encoder_get_max_residual_partition_order()... ");
359         if(FLAC__stream_encoder_get_max_residual_partition_order(encoder) != 0) {
360                 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_max_residual_partition_order(encoder));
361                 return false;
362         }
363         printf("OK\n");
364
365         printf("testing FLAC__stream_encoder_get_rice_parameter_search_dist()... ");
366         if(FLAC__stream_encoder_get_rice_parameter_search_dist(encoder) != 0) {
367                 printf("FAILED, expected %u, got %u\n", 0, FLAC__stream_encoder_get_rice_parameter_search_dist(encoder));
368                 return false;
369         }
370         printf("OK\n");
371
372         /* init the dummy sample buffer */
373         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
374                 samples[i] = i & 7;
375
376         printf("testing FLAC__stream_encoder_process()... ");
377         if(!FLAC__stream_encoder_process(encoder, (const FLAC__int32 * const *)samples_array, sizeof(samples) / sizeof(FLAC__int32)))
378                 return die_s_("returned false", encoder);
379         printf("OK\n");
380
381         printf("testing FLAC__stream_encoder_process_interleaved()... ");
382         if(!FLAC__stream_encoder_process_interleaved(encoder, samples, sizeof(samples) / sizeof(FLAC__int32)))
383                 return die_s_("returned false", encoder);
384         printf("OK\n");
385
386         printf("testing FLAC__stream_encoder_finish()... ");
387         FLAC__stream_encoder_finish(encoder);
388         printf("OK\n");
389
390         printf("testing FLAC__stream_encoder_delete()... ");
391         FLAC__stream_encoder_delete(encoder);
392         printf("OK\n");
393
394         printf("\nPASSED!\n");
395
396         return true;
397 }
398
399 FLAC__bool test_encoders()
400 {
401         init_metadata_blocks_();
402
403         if(!test_stream_encoder())
404                 return false;
405
406         (void) file_utils__remove_file(flacfilename_);
407         free_metadata_blocks_();
408
409         return true;
410 }